package com.talk.demo.intimate;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.talk.demo.R;
import com.talk.demo.util.NetworkUtilities;

import net.sectorsieteg.avatars.AvatarDrawableFactory;

public class EditIntimateActivity extends Activity {
	private static String TAG = "EditIntimateActivity";
	private int friend_id;
	private String friend_name;
	private TextView tvFriendName;
	private ImageView ivAvatar;
	private TextView tvLinkageTo, tvLinkageFrom;
	private TextView tvNumberTo, tvNumberFrom;
	private TextView tvPressAdd;
	private TextView tvChannel;
	private int numberTo;
	private int oneStep;

	private static final int ONE_STEP = 29;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_intimate);
        
        Bundle bundle = getIntent().getExtras();
        friend_id = bundle.getInt("id");
        friend_name = bundle.getString("friend");
        tvFriendName = (TextView) findViewById(R.id.friend_name);
        tvFriendName.setText(friend_name);

        ivAvatar = (ImageView) findViewById(R.id.friend_avatar);
        
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inMutable = false;
        Bitmap avatar = NetworkUtilities.downloadPhoto(friend_name);
        if(null == avatar) {
            avatar = BitmapFactory.decodeResource(this.getResources(), R.drawable.avatar, options);
        }

        AvatarDrawableFactory avatarDrawableFactory = new AvatarDrawableFactory(this.getResources());
        Drawable roundedAvatarDrawable = avatarDrawableFactory.getRoundedAvatarDrawable(avatar);
        ivAvatar.setImageDrawable(roundedAvatarDrawable);
        
        tvLinkageTo = (TextView) findViewById(R.id.linkage_from);
        tvNumberTo = (TextView) findViewById(R.id.number_to);
        numberTo = 61;
        tvNumberTo.setText(String.valueOf(numberTo));
        
        tvLinkageTo.setText("我对"+friend_name+"亲密度");
        tvLinkageFrom = (TextView) findViewById(R.id.linkage_to);
        tvLinkageFrom.setText(friend_name+"对我亲密度");
        tvNumberFrom = (TextView) findViewById(R.id.number_from);
        
        tvPressAdd = (TextView) findViewById(R.id.add_press);
        tvPressAdd.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent newIntent = new Intent(EditIntimateActivity.this, AddPressActivity.class);
	            Bundle mBundle = new Bundle();
                mBundle.putInt("id", friend_id);
                newIntent.putExtras(mBundle);
				startActivityForResult(newIntent, ONE_STEP);
			}
        	
        });
        
        tvChannel = (TextView) findViewById(R.id.subject_wall);
        tvChannel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			    Intent newIntent = new Intent(EditIntimateActivity.this, IntimateChannelActivity.class);
	             Bundle mBundle = new Bundle();
	             mBundle.putString("friend", friend_name);
	             newIntent.putExtras(mBundle);
	             startActivity(newIntent);
			}
        	
        });
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		Log.d(TAG, "got the return :" + requestCode + " :" + resultCode);
		switch (requestCode) {
		case ONE_STEP:
			if (resultCode == RESULT_OK) {
				Bundle extras = data.getExtras();
				oneStep = extras.getInt("STEP");
				
		        numberTo += oneStep;
		        tvNumberTo.setText(String.valueOf(numberTo));
			}
			break;
		}
	}
    
}

@@ -196,24 +196,53 @@
 e.Drawable;%0A
+import android.os.AsyncTask;%0A
 import andro
@@ -251,24 +251,24 @@
 .os.Bundle;%0A
-
 import andro
@@ -1613,462 +1613,81 @@
     
-Bitmap avatar = NetworkUtilities.downloadPhoto(friend_name);%0A        if(null == avatar) %7B%0A            avatar = BitmapFactory.decodeResource(this.getResources(), R.drawable.avatar, options);%0A        %7D%0A%0A        AvatarDrawableFactory avatarDrawableFactory = new AvatarDrawableFactory(this.getResources());%0A        Drawable roundedAvatarDrawable = avatarDrawableFactory.getRoundedAvatarDrawable(avatar);%0A        ivAvatar.setImageDrawable(roundedAvatarDrawabl
+//update avatar of friend%0A        new DownAvatarTask().execute(friend_nam
 e);%0A
@@ -3549,18 +3549,945 @@
 ak;%0A%09%09%7D%0A
-
 %09%7D%0A
+%09%0A    private Bitmap downAvatarServer(String fileName) %7B%0A        return NetworkUtilities.downloadPhoto(fileName);%0A    %7D%0A    %0A    private class DownAvatarTask extends AsyncTask%3CString, Void, Bitmap%3E %7B%0A        @Override%0A        protected Bitmap doInBackground(String... params) %7B%0A            return downAvatarServer(params%5B0%5D);%0A        %7D%0A%0A        @Override%0A        protected void onPostExecute(Bitmap avatar) %7B%0A            if(null == avatar) %7B%0A                avatar = BitmapFactory.decodeResource(this.getResources(), R.drawable.avatar, options);%0A            %7D%0A            %0A            AvatarDrawableFactory avatarDrawableFactory = new AvatarDrawableFactory(this.getResources());%0A            Drawable roundedAvatarDrawable = avatarDrawableFactory.getRoundedAvatarDrawable(avatar);%0A            ivAvatar.setImageDrawable(roundedAvatarDrawable);%0A        %7D%0A%0A        @Override%0A        protected void onCancelled() %7B%0A        %7D%0A    %7D%09%0A
     %0A%7D%0A

fix crash