package com.imofan.android.develop.sns.activity;

import java.io.File;

import org.json.JSONObject;

import com.imofan.android.develop.sns.MFSnsAuthListener;
import com.imofan.android.develop.sns.MFSnsConfig;
import com.imofan.android.develop.sns.MFSnsHttpUtil;
import com.imofan.android.develop.sns.MFSnsSSOLogin;
import com.imofan.android.develop.sns.MFSnsService;
import com.imofan.android.develop.sns.MFSnsShareContent;
import com.imofan.android.develop.sns.MFSnsShareListener;
import com.imofan.android.develop.sns.MFSnsUser;
import com.imofan.android.develop.sns.MFSnsUtil;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.SendMessageToWX;
import com.tencent.mm.sdk.openapi.WXAPIFactory;
import com.tencent.mm.sdk.openapi.WXMediaMessage;
import com.tencent.mm.sdk.openapi.WXWebpageObject;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.tencent.tauth.IUiListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/***
 * 分享确定页面
 * 
 * @author poble
 * 
 */

public class MFSnsUploadNewActivity extends Activity {
	private static MFSnsShareListener shareListener = null;
	private IWXAPI api = null;
	private MFSnsShareContent contentMessage = null;
	private int contentlength = 140;        //文本框可以输入的文字长度
	private FrameLayout subimt = null;
	private FrameLayout back = null;
	private EditText contentEditText = null;
	private ProgressBar weiboShareProgressBar = null;
	private TextView countTextView = null;
	
	 //private ImageView image;
    private boolean isHttpImage = false;
    private boolean imageDownDone = false;
    private int qq_weibo_state = 0;      //腾讯微博分享状态
    private int qq_qzone_state = 0;      //qq空间分享状态
    private int renren_state = 0;      //人人网分享状态
    private int sina_state = 0;      //新浪微博分享状态
    
    private String qq_weibo_result = "";      //腾讯微博分享结果
    private String qq_qzone_result = "";      //qq空间分享结果
    private String renren_result = "";      //人人网分享结果
    private String sina_result = "";      //新浪微博分享结果
    
    private final static int  STATE_NONE = 0;//未认证，未选择
    private final static int  STATE_AUTHORIZE = 1;//已经认证，没有选择
    private final static int STATE_SELECTED = 2;//已认证，已选择
    
    private FrameLayout imageLayout = null;
	private ImageView shareImage = null;
	 //腾讯微博相关
    private ImageView qq_weibo_img = null;
    private ImageView qq_weibo_selected_img = null;
    private TextView qq_weibo_text = null;
    private RelativeLayout qq_weibo_layout = null;
    
    //qq空间相关
    private ImageView qq_qzone_img = null;
    private ImageView qq_qzone_selected_img = null;
    private TextView qq_qzone_text = null;
    private RelativeLayout qq_qzone_layout = null;
    //新浪微博相关
    private ImageView sina_img = null;
    private ImageView sina_selected_img = null;
    private TextView sina_text = null;
    private RelativeLayout sina_layout = null;
    
    //微信
    private LinearLayout weixin_layout = null;
    //朋友圈
    private LinearLayout weixin_friends_layout = null;
    //qq好友
    private LinearLayout qq_friends_layout = null;
    //低部
    private LinearLayout  bottomLayout = null;
    
    //private MFSnsSinaAuth sinaAuth;
    //private MFSnsTencentAuth tencentAuth;
    private MFSnsSSOLogin ssoLogin;
    private Tencent mTencent;
    
    public static void setShareListener(MFSnsShareListener shareListener) {
    	MFSnsUploadNewActivity.shareListener = shareListener;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if(MFSnsConfig.NIGHTMODE){
        	setContentView(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this,"layout", "imofan_weibo_share_new_activity_night"));
        }else{
        	setContentView(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this,"layout", "imofan_weibo_share_new_activity"));
        }
        initView();
        api = WXAPIFactory.createWXAPI(MFSnsUploadNewActivity.this, MFSnsConfig.CONSUMER_WEIXIN_APPID, true);
        api.registerApp(MFSnsConfig.CONSUMER_WEIXIN_APPID);
        mTencent = Tencent.createInstance(MFSnsConfig.CONSUMER_KEY_TECENT, getApplicationContext());
    }

   
    private void initView() {
        // 获取Intent中传递信息
        contentMessage = (MFSnsShareContent) this.getIntent().getSerializableExtra("content");
        if(null!=contentMessage&&null!=contentMessage.getQqWeiboHideContent()){//计算文本可以输入的字符个数
            contentlength = contentlength - MFSnsUtil.lengthOfQuanJiao(contentMessage.getQqWeiboHideContent());
        }else{
            contentlength = contentlength - MFSnsUtil.lengthOfQuanJiao(contentMessage.getHideContent());
        }
        

        // 初始化组件信息
        back = (FrameLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id","imofan_weibo_share_activity_back"));
        subimt = (FrameLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id","imofan_weibo_share_activity_submit"));
        contentEditText = (EditText) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id","imofan_weibo_share_activity_content"));
        weiboShareProgressBar = (ProgressBar) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id","imofan_weibo_share_refresh_loadprogress"));
        contentEditText.setText(contentMessage.getContent());
        
      //初始化ImageView 
        imageLayout = (FrameLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_right_img_layout"));
        shareImage = (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_share_image"));
        
        if(null==contentMessage.getImage()||contentMessage.getImage().equals("")){
        	imageLayout.setVisibility(View.GONE);
        }else{
        	imageLayout.setVisibility(View.VISIBLE);
        	setImage(contentMessage.getImage());
        }
        countTextView = (TextView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weibo_share_count"));
        int textLength = contentEditText.getText().toString().length();
        if(contentlength-textLength<=0){
            countTextView.setText((contentlength - textLength)+"");
            countTextView.setTextColor(Color.parseColor("#CF1111"));
        }else{
            countTextView.setText((contentlength - contentEditText.getText().toString().length())+"");
            countTextView.setTextColor(Color.parseColor("#3C3C3C"));
        }
        
        //腾讯微博组件
        qq_weibo_img = (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_weibo_img"));
        qq_weibo_selected_img =  (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_weibo_selected_img"));
        qq_weibo_text = (TextView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_weibo_text"));
        qq_weibo_layout = (RelativeLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_weibo_layout"));
        qq_weibo_layout.setOnClickListener(listener);
        
        //qq空间组件
        qq_qzone_img = (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_qzone_img"));
        qq_qzone_selected_img =  (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_qzone_selected_img"));
        qq_qzone_text = (TextView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_qzone_text"));
        qq_qzone_layout = (RelativeLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_qzone_layout"));
        qq_qzone_layout.setOnClickListener(listener);
        
        //新浪微博组件
        sina_img = (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_sina_img"));
        sina_selected_img =  (ImageView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_sina_selected_img"));
        sina_text = (TextView) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_sina_text"));
        sina_layout = (RelativeLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_sina_layout"));
        sina_layout.setOnClickListener(listener);
        
        //微信
        weixin_layout = (LinearLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weixin_layout"));
        weixin_layout.setOnClickListener(listener);
        //qq好友
        qq_friends_layout = (LinearLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_friends_layout"));
        qq_friends_layout.setOnClickListener(listener);
        
        //朋友圈
        weixin_friends_layout = (LinearLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weixin_friends_layout"));
        weixin_friends_layout.setOnClickListener(listener);
        bottomLayout = (LinearLayout) findViewById(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_bottom_layout"));
        bottomLayout.setOnClickListener(listener);
        //输入框控制
        contentEditText.addTextChangedListener(new TextWatcher() {
        	
			@Override
			public void afterTextChanged(Editable s) {
				int length = contentEditText.getText().toString().length();
                if(length>contentlength){
                    countTextView.setText((contentlength - length) + "");
                    countTextView.setTextColor(Color.parseColor("#CF1111"));
                }else{
                    countTextView.setText((contentlength - length) + "");
                    countTextView.setTextColor(Color.parseColor("#3C3C3C"));
                }
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				
			}

        });
        // 设置发布按钮监听事件
        subimt.setOnClickListener(listener);
        back.setOnClickListener(listener);
        initQQWeiboState();
        initQQQzoneState();
        initSinaState();
    }
    
    OnClickListener listener = new OnClickListener() {
        
        @Override
        public void onClick(View v) {
            int viewId = v.getId();
            if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_weibo_layout")){
                QQWeiBoClick();//腾讯微博
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_qzone_layout")){
                QQQzoneClick();//qq空间
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_sina_layout")){
                SinaClick();//新浪微博
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weixin_friends_layout")){//微信朋友圈
            	WeiXinClick(SendMessageToWX.Req.WXSceneTimeline);//微信朋友圈
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weibo_share_activity_submit")){
                submit();//发表
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weixin_layout")){
                WeiXinClick(SendMessageToWX.Req.WXSceneSession);//微信
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_qq_friends_layout")){
            	qqFriendsClick();//qq好友
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_weibo_share_activity_back")){//返回
            	back();
            }else if(viewId == MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "id", "imofan_bottom_layout")){//底部返回
            	back();
            }
        }
    };
    
    private void back(){
    	
    	 new Handler().postDelayed(new Runnable() {
             @Override
             public void run() {
            	MFSnsUploadNewActivity.this.finish();
             	MFSnsUploadNewActivity.this.overridePendingTransition(0,MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "anim", "imofan_top_fade_out"));
             }
         },300);

    	
    }
  
    private void qqFriendsClick(){
    	/*Bundle params = new Bundle();
		params.putString(Constants.PARAM_TITLE,contentMessage.getTitle());
		params.putString(Constants.PARAM_IMAGE_URL	,contentMessage.getImage());
		params.putString(Constants.PARAM_TARGET_URL,contentMessage.getWapUrl());
		params.putString(Constants.PARAM_SUMMARY,contentEditText.getText().toString());
		params.putString(Constants.PARAM_APP_SOURCE,"应用");
		if(null!=shareListener){
            shareListener.onTencentQQSucceeded(MFSnsUploadNewActivity.this);
        }
    	mTencent.shareToQQ(MFSnsUploadNewActivity.this, params, new IUiListener(){
			@Override
			public void onCancel() {
				
			}

			@Override
			public void onComplete(JSONObject arg0) {
				if(null!=shareListener){
	                 shareListener.onTencentQQSucceeded(MFSnsUploadNewActivity.this);
	             }
			}

			@Override
			public void onError(UiError arg0) {
			}

               
          });    */	
    }
    
    private void initQQWeiboState(){
        //腾讯微博初始化状态
        int qq_weibo_tag =  checkPlatformState(MFSnsService.PLATFORM_QQ_WEIBO);
        if(qq_weibo_tag == STATE_NONE){
            qq_weibo_state = STATE_NONE;
            if(MFSnsConfig.NIGHTMODE){
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo_unauth_night"));
            }else{
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo_unauth"));
            }
            
            qq_weibo_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
            //qq_weibo_text.setTextColor(Color.parseColor("#9F9F9F"));
        }else if(qq_weibo_tag == STATE_AUTHORIZE){
            qq_weibo_state = STATE_AUTHORIZE;
            if(MFSnsConfig.NIGHTMODE){
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo_unauth_night"));
            }else{
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo_unauth"));
            }
            
            qq_weibo_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
            //qq_weibo_text.setTextColor(Color.parseColor("#404040"));
        }else if(qq_weibo_tag == STATE_SELECTED){
            qq_weibo_state = STATE_SELECTED;
            if(MFSnsConfig.NIGHTMODE){
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo_night"));
            }else{
            	qq_weibo_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_tencent_weibo_logo"));
            }
            qq_weibo_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_selected"));
            //qq_weibo_text.setTextColor(Color.parseColor("#404040"));
        }
    }
    
    private void initQQQzoneState(){
      //QQ空间初始化状态
        int qq_qzone_tag =  checkPlatformState(MFSnsService.PLATFORM_QQ_QZONE);
        if(qq_qzone_tag == STATE_NONE){
            qq_qzone_state = STATE_NONE;
            if(MFSnsConfig.NIGHTMODE){
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo_unauth_night"));
            }else{
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo_unauth"));
            }
            
            qq_qzone_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
            //qq_qzone_text.setTextColor(Color.parseColor("#9F9F9F"));
        }else if(qq_qzone_tag == STATE_AUTHORIZE){
            qq_qzone_state = STATE_AUTHORIZE;
            if(MFSnsConfig.NIGHTMODE){
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo_unauth_night"));
            }else{
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo_unauth"));
            }
            qq_qzone_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
            //qq_qzone_text.setTextColor(Color.parseColor("#404040"));
        }else if(qq_qzone_tag == STATE_SELECTED){
            qq_qzone_state = STATE_SELECTED;
            if(MFSnsConfig.NIGHTMODE){
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo_night"));
            }else{
            	qq_qzone_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_qzone_logo"));
            }
            
            qq_qzone_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_selected"));
           // qq_qzone_text.setTextColor(Color.parseColor("#404040"));
        }
    }
   
    private void initSinaState(){
      //新浪微博初始化状态
        int sina_tag =  checkPlatformState(MFSnsService.PLATFORM_SINA_WEIBO);
        if(sina_tag == STATE_NONE){
            sina_state = STATE_NONE;
            if(MFSnsConfig.NIGHTMODE){
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo_unauth_night"));
            }else{
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo_unauth"));
            }
            
            sina_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
            //sina_text.setTextColor(Color.parseColor("#9F9F9F"));
        }else if(sina_tag == STATE_AUTHORIZE){
            sina_state = STATE_AUTHORIZE;
            if(MFSnsConfig.NIGHTMODE){
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo_unauth_night"));
            }else{
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo_unauth"));
            }
            sina_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_unselected"));
           // sina_text.setTextColor(Color.parseColor("#404040"));
        }else if(sina_tag == STATE_SELECTED){
            sina_state = STATE_SELECTED;
            if(MFSnsConfig.NIGHTMODE){
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo_night"));
            }else{
            	sina_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_share_sina_logo"));
            }
            
            sina_selected_img.setImageResource(MFSnsConfig.getIdByReflection(MFSnsUploadNewActivity.this, "drawable", "imofan_item_selected"));
           // sina_text.setTextColor(Color.parseColor("#404040"));
        }
    }
   
  
    private void SinaClick(){

        //新浪微博
        int tag =  checkPlatformState(MFSnsService.PLATFORM_SINA_WEIBO);
        if(tag == STATE_NONE){
            MFSnsAuthListener listener = new MFSnsAuthListener() {
                
                @Override
                public void onSucceeded(Context context, MFSnsUser openUser) {
                    MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_SINA_WEIBO,"on");
                    showToast("登录成功，已绑定分享到新浪微博");
                    initSinaState();
                }
                @Override
                public void onFail(Context context, String errMessage) {
                	showToast(errMessage);
                }
            };
            //sinaAuth = new MFSnsSinaAuth();
            ssoLogin = new MFSnsSSOLogin();
            ssoLogin.SSOLogin(MFSnsUploadNewActivity.this, MFSnsService.PLATFORM_SINA_WEIBO, listener);
            //sinaAuth.authSina(MFSnsUploadNewActivity.this, listener);
            //MFSnsService.auth(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_SINA_WEIBO, listener);
            
        }else if(tag == STATE_AUTHORIZE){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_SINA_WEIBO,"on");
            showToast("已绑定分享到新浪微博");
            initSinaState();
        }else if(tag == STATE_SELECTED){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_SINA_WEIBO,"off");
            showToast("已取消分享到新浪微博");
            initSinaState();
        }
    
    }
  
    private void QQWeiBoClick(){

        //腾讯微博
        int tag =  checkPlatformState(MFSnsService.PLATFORM_QQ_WEIBO);
        if(tag == STATE_NONE){
            MFSnsAuthListener listener = new MFSnsAuthListener() {
                
                @Override
                public void onSucceeded(Context context, MFSnsUser openUser) {
                    MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_WEIBO,"on");
                    savePlatformSelected("on");
                    //((Activity)context).finish();
                    showToast("登录成功，已绑定分享到腾讯微博");
                    initQQWeiboState();
                    initQQQzoneState();
                }
                @Override
                public void onFail(Context context, String errMessage) {
                	showToast(errMessage);
                }
            };
            ssoLogin = new MFSnsSSOLogin();
            ssoLogin.SSOLogin(MFSnsUploadNewActivity.this, MFSnsService.PLATFORM_QQ_WEIBO, listener);
        }else if(tag == STATE_AUTHORIZE){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_WEIBO,"on");
            showToast("已绑定分享到腾讯微博");
            initQQWeiboState();
        }else if(tag == STATE_SELECTED){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_WEIBO,"off");
            showToast("已取消分享到腾讯微博");
            initQQWeiboState();
        }
    
    }
   
    private void QQQzoneClick(){

        //qq空间
        int tag =  checkPlatformState(MFSnsService.PLATFORM_QQ_QZONE);
        if(tag == STATE_NONE){
            MFSnsAuthListener listener = new MFSnsAuthListener() {
                
                @Override
                public void onSucceeded(Context context, MFSnsUser openUser) {
                    MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_QZONE,"on");
                    savePlatformSelected("on");
                    showToast("登录成功，已绑定分享到QQ空间");
                    initQQWeiboState();
                    initQQQzoneState();
                }
                @Override
                public void onFail(Context context, String errMessage) {
                	showToast(errMessage);
                }
            };
            ssoLogin = new MFSnsSSOLogin();
            ssoLogin.SSOLogin(MFSnsUploadNewActivity.this, MFSnsService.PLATFORM_QQ_QZONE, listener);
        }else if(tag == STATE_AUTHORIZE){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_QZONE,"on");
            showToast("已绑定分享到QQ空间");
            initQQQzoneState();
        }else if(tag == STATE_SELECTED){
            MFSnsUtil.savePlatformSelected(MFSnsUploadNewActivity.this,MFSnsService.PLATFORM_QQ_QZONE,"off");
            showToast("已取消分享到QQ空间");
            initQQQzoneState();
        }
    
    }
  
    private void WeiXinClick(int type){

    	 if (isHttpImage) { // 是网络图片，判断网络图片是否下载完成
             if (!imageDownDone) {
                 showToast("图片尚未下载完成，请稍等");
                 return;
             }
         }
         File imageFile = null;
         if(null!=contentMessage.getImage()){
             if (isHttpImage) {
                 imageFile = new File(getCachUrl());
             } else {
                 imageFile = new File(contentMessage.getImage());
             }  
         }
        
         final WXWebpageObject appdata = new WXWebpageObject();
         appdata.webpageUrl = contentMessage.getWapUrl();
        
        
         final WXMediaMessage msg = new WXMediaMessage();
         if(null!=imageFile){
             //msg.setThumbImage(MFSnsUtil.extractThumbNail(imageFile.getPath(), 150, 150, true));
        	 Bitmap tmp = BitmapFactory.decodeFile(imageFile.getPath());
             msg.setThumbImage(ThumbnailUtils.extractThumbnail(tmp, 150, 150));
         }
         msg.title = contentMessage.getTitle();
         if(null!=contentMessage.getDescription()){
             msg.description = contentMessage.getDescription();
         }
         msg.mediaObject = appdata;
         
         SendMessageToWX.Req req = new SendMessageToWX.Req();
         req.transaction = String.valueOf(System.currentTimeMillis());
         req.message = msg;
         req.scene = type;
         api.sendReq(req);
         if(type == SendMessageToWX.Req.WXSceneTimeline){
        	 if(null!=shareListener){
                 shareListener.onWeiXinFriendsSucceeded(MFSnsUploadNewActivity.this);
             }
         }else if(type == SendMessageToWX.Req.WXSceneSession){
        	 if(null!=shareListener){
                 shareListener.onWeiXinSucceeded(MFSnsUploadNewActivity.this);
             }
         }
    }
   
    private void submit(){
        //提交
        if (MFSnsHttpUtil.isNetworkAvailable(MFSnsUploadNewActivity.this)) {
            if (isHttpImage) { // 是网络图片，判断网络图片是否下载完成
                if (!imageDownDone) {
                    showToast("图片尚未下载完成，请稍等");
                    return;
                }
            }
            if(qq_weibo_state!=STATE_SELECTED&&qq_qzone_state!=STATE_SELECTED&&sina_state!=STATE_SELECTED&&renren_state!=STATE_SELECTED){
                showToast("请至少选择一个分享平台");
                return;
            }
            if (null == contentEditText.getText().toString() || "".equals(contentEditText.getText().toString().trim())) {
                showToast("内容不能为空");
                return;
            }
            if (null == contentEditText.getText().toString() || contentEditText.getText().toString().trim().length()>contentlength) {
                showToast("字数超过限制");
                return;
            }
            weiboShareProgressBar.setVisibility(View.VISIBLE);
            subimt.setEnabled(false);
            sendThread.start();
        } else {
            showToast("没有找到可用网络");
        }
    
    }
   
    private Handler doresult = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            // 成功，回调成功方法，失败，回调失败方法
            weiboShareProgressBar.setVisibility(View.GONE);
            subimt.setEnabled(true);
            if (null != shareListener) {
                if(qq_weibo_result.equals("mf_share_success")){
                    shareListener.onTencentWeiBoSucceeded(MFSnsUploadNewActivity.this);
                }
                if(qq_qzone_result.equals("mf_share_success")){
                    shareListener.onTencentQzoneSucceeded(MFSnsUploadNewActivity.this);
                }
                if(sina_result.equals("mf_share_success")){
                    shareListener.onSinaSucceeded(MFSnsUploadNewActivity.this);
                }
                if(renren_result.equals("mf_share_success")){
                    shareListener.onRenrenSucceeded(MFSnsUploadNewActivity.this);
                }
                if(null!=shareListener){
	                 shareListener.onSucceeded(MFSnsUploadNewActivity.this);
	             }
            }
            String share_result = "";
            //根据各个平台分享结果，做提示
            if(!qq_weibo_result.equals("")&&!qq_weibo_result.equals("mf_share_success")){
                share_result += " 腾讯微博  ";
            }
            if(!qq_qzone_result.equals("")&&!qq_qzone_result.equals("mf_share_success")){
                share_result += " QQ空间  ";
            }
            if(!sina_result.equals("")&&!sina_result.equals("mf_share_success")){
                share_result += " 新浪微博  ";
            }
            if(!renren_result.equals("")&&!renren_result.equals("mf_share_success")){
                share_result += " 人人网 ";
            }
            
            if(share_result.equals("")){
                share_result = "分享成功";
            }else{
                share_result += "分享失败";
            }
            //状态回归
            qq_weibo_result = "";
            qq_qzone_result = "";
            sina_result = "";
            renren_result = "";
            showToast(share_result);
            MFSnsUploadNewActivity.this.finish();
        }
    };
   
    private Thread sendThread = new Thread(new Runnable() {

        @Override
        public void run() {
            if(qq_weibo_state == STATE_SELECTED){
                qq_weibo_result = sendToPlatform(MFSnsService.PLATFORM_QQ_WEIBO);
            }
            
            if(qq_qzone_state == STATE_SELECTED){
                qq_qzone_result = sendToPlatform(MFSnsService.PLATFORM_QQ_QZONE);
            }
            
            if(sina_state == STATE_SELECTED){
                sina_result = sendToPlatform(MFSnsService.PLATFORM_SINA_WEIBO);
            }
            
            if(renren_state == STATE_SELECTED){
                renren_result = sendToPlatform(MFSnsService.PLATFORM_RENREN);
            }
            
            Message msg = new Message();
            msg.obj = "result";
            doresult.sendMessage(msg);
        }
    });
    
    private String sendToPlatform(int platform){
        // 根据平台信息，获取认证后的信息
          MFSnsUser user = MFSnsUtil.getOpenUser(MFSnsUploadNewActivity.this,
                  platform);
          String result = null;
          
          String eidtTextString = null == contentEditText.getText()?"":contentEditText.getText().toString();
          String hideMessage = null == contentMessage.getHideContent()?"":contentMessage.getHideContent().toString();
          String imageUrl = null == contentMessage.getImage()?"":contentMessage.getImage().toString();
          String tittle = null ==contentMessage.getTitle()?"":contentMessage.getTitle().toString();
          String tittleUrl = null ==contentMessage.getUrl()?"":contentMessage.getUrl().toString();

          if (platform == MFSnsService.PLATFORM_QQ_QZONE) {// qq空间分享内容与其他微博不同，需要做特殊处理
              result = MFSnsUtil.addShare(eidtTextString+hideMessage, user.getAccessToken(), user.getOpenId(), imageUrl, tittle,tittleUrl,eidtTextString);
          } else if (platform == MFSnsService.PLATFORM_RENREN) {//向人人网发送
              result = MFSnsUtil.shareToRenRen(user.getAccessToken(), eidtTextString+hideMessage, tittleUrl);
          } else if(platform == MFSnsService.PLATFORM_SINA_WEIBO){  //新浪微博
              // 发送分享信息
              if (null == contentMessage.getImage()
                      || "".equals(contentMessage.getImage())) {
                  result = MFSnsUtil.upload(platform, eidtTextString+hideMessage, user.getAccessToken(), user.getOpenId());
              } else {
                  File imageFile = null;
                  if (isHttpImage) {
                      imageFile = new File(getCachUrl());
                  } else {
                      imageFile = new File(contentMessage.getImage());
                  }
                  result = MFSnsUtil.uploadWithPic(platform, eidtTextString+hideMessage, user.getAccessToken(), user.getOpenId(), imageFile);
              }
          } else if(platform == MFSnsService.PLATFORM_QQ_WEIBO){  //腾讯微博
              // 发送分享信息
              if (null == contentMessage.getImage()
                      || "".equals(contentMessage.getImage())) {
                  result = MFSnsUtil.upload(platform, eidtTextString+hideMessage, user.getAccessToken(), user.getOpenId());
              } else {
                  File imageFile = null;
                  if (isHttpImage) {
                      imageFile = new File(getCachUrl());
                  } else {
                      imageFile = new File(contentMessage.getImage());
                  }
                  result = MFSnsUtil.uploadWithPic(platform, eidtTextString+hideMessage, user.getAccessToken(), user.getOpenId(), imageFile);
              }
          }
          return result;
    }
  
    private void setImage(String url) {
        if (url.startsWith("http")) {
            isHttpImage = true;
            if (getCach(url)) {
                imageDownDone = true;
                viewSetImage(getCachUrl(), shareImage);
            } else {
                DownloadTask task = new DownloadTask();
                task.execute(url);
                return;
            }

        } else {
        	imageDownDone = true;
        	viewSetImage(url, shareImage);
        }
    }

    private class DownloadTask extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... params) {
            File file = MFSnsUtil.saveImage(MFSnsUploadNewActivity.this,params[0]);
            setCach(params[0]);
            return file.getAbsolutePath();
        }

        @Override
        protected void onPostExecute(String result) {
        	super.onPostExecute(result);
        	if(null!=result&&!result.equals("")){
        		imageDownDone = true;
                viewSetImage(result, shareImage);
        	}
        }
    }

  
    private boolean getCach(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences(
                "uploadImage", Context.MODE_PRIVATE);
        String url = sharedPreferences.getString("imageUrl", "");
        if (key.equals(url)) {
            return true;
        } else {
            return false;
        }
    }

   
    private void setCach(String key) {
        SharedPreferences sharedPreferences = getSharedPreferences(
                "uploadImage", Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();// 获取编辑器
        editor.putString("imageUrl", key);
        editor.commit();// 提交修改
    }

    private String getCachUrl() {
        String path = MFSnsUploadNewActivity.this.getDir("cacheImg", Activity.MODE_PRIVATE).getAbsolutePath().toString()+"/shareImg.cache";
        return path;
    }
    
    private int checkPlatformState(int platform){
        if(MFSnsUtil.isAuthorized(MFSnsUploadNewActivity.this, platform)){
            SharedPreferences sharedPreferences = getSharedPreferences(
                    "platform_state", Context.MODE_PRIVATE);
            String state = sharedPreferences.getString("platform_state"+platform, "off");
            if(state.equals("on")){
                return this.STATE_SELECTED;
            }else{
                return this.STATE_AUTHORIZE;
            }
        }else{
            return MFSnsUploadNewActivity.STATE_NONE;
        }
    }
    @Override
    protected void onResume() {
    	 if(null!=shareListener){
         	shareListener.onResume(MFSnsUploadNewActivity.this);
         }
        super.onResume();
    }
    @Override
    protected void onPause() {
    	 if(null!=shareListener){
         	shareListener.onPause(MFSnsUploadNewActivity.this);
         }
    	super.onPause();
    }
   
    private void showToast(String message){
        Toast.makeText(MFSnsUploadNewActivity.this, message,Toast.LENGTH_SHORT).show();
    }
   
    private void savePlatformSelected(String onOrOff){
        SharedPreferences sharedPreferences = getSharedPreferences(
                "to_tencent", Context.MODE_PRIVATE);
        Editor editor = sharedPreferences.edit();
        editor.putString("to_tencent_state", onOrOff);
        editor.commit();// 提交修改
    }
    private void viewSetImage(String filePath,ImageView view){
    	BitmapFactory.Options options = new BitmapFactory.Options();   
		//设置工厂以240的密度来读取图片文件
        options.inDensity = 240;
        BitmapDrawable image = new BitmapDrawable(BitmapFactory.decodeFile(filePath,options));
        //设置图片在手机中以手机本身的密度来显示图片
        image.setTargetDensity(MFSnsUploadNewActivity.this.getApplicationContext().getResources().getDisplayMetrics());
	    view.setImageDrawable(image);
    }
    @Override
    public void onBackPressed() {
    	back();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	if(null!=ssoLogin){
    		ssoLogin.onActivityResult(requestCode, resultCode, data);
    	}
    }
}
