package com.lianxin.pubqq.main;

import java.util.Timer;
import java.util.TimerTask;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lianxin.pubqq.R;
import com.lianxin.pubqq.activity.UserListActivity;
//import com.lianxin.panqq.common.Constants;
import com.lianxin.panqq.common.DefineUser;
import com.lianxin.panqq.common.GloableParams;
import com.lianxin.panqq.common.Utils;
import com.lianxin.panqq.common.utils.CaptionUtils;
import com.lianxin.pubqq.main.widght.WarnTipDialog;
import com.lianxin.pubqq.main.TitleMenu.ActionItem;
import com.lianxin.pubqq.main.TitleMenu.TitlePopup;
import com.lianxin.pubqq.main.TitleMenu.TitlePopup.OnItemOnClickListener;
import com.lianxin.pubqq.main.Fragment_Depart;
import com.lianxin.pubqq.main.Fragment_Recent;
import com.lianxin.pubqq.main.Fragment_Contact;
import com.lianxin.pubqq.main.Fragment_Setup;
import com.lianxin.pubqq.main.LoginActivity;
import com.lianxin.pubqq.main.RegisterActivity;
import com.lianxin.panqq.main.BaseFragmentActivity;
import com.lianxin.panqq.main.bean.RecentItem;
import com.lianxin.pubqq.chat.EMChatActivity;
import com.lianxin.panqq.main.EMChatManager;
import com.lianxin.panqq.utils.PreferenceUtils;
import com.lianxin.panqq.chat.EMConversation;
import com.lianxin.panqq.chat.entity.EMMessage;
import com.lianxin.panqq.edit.MyAlertDialog;

public class MainActivity extends BaseFragmentActivity implements OnClickListener {
	//主模块
	private TextView txt_title,txt_left;
	private ImageView img_right;
	private WarnTipDialog Tipdialog;		
	protected static final String TAG = "MainActivity";
	private TitlePopup titlePopup;
	private TextView unreaMsgdLabel;// 未读消息textview
	private TextView unreadAddressLable;// 未读通讯录textview
	private TextView unreadFindLable;// 发现	
	
	//private String[] txttitles;	
	public Fragment_Recent homefragment;  //最新消息
	private Fragment_Contact contactlistfragment;  //最新消息
	private Fragment_Nearby nearbyfragment;	  //局域网用户
	private Fragment_Live livelistfragment;	  //直播和教学
	private Fragment_Friend friendfragment;	  //注册好友
	private Fragment_Depart departfragment;  //部门	
	private Fragment_Office officefragment;  //办公	
	private Fragment_Movie  moviefragment;	  //电影	
	private Fragment_Setup setupfragment;  //设置
	private Fragment_Class classfragment;  //我的班级
	private Fragment_Crowd crowdfragment;  //自建群
	private Fragment_City cityfragment;	  //社区或者校友录
	//底部Tab控件数组
	private ImageView[] imageButtons;  //底部TabImageView
	private TextView[] textViews;  //底部TabImageView
	private int currentTabIndex = 0;// 当前fragment的index	
	//可供调用的Fragment数组（不可修改，否则二次开发三次开发有错误）
	private Fragment[] fragMents;	//可供调用的Fragment数组（不可修改，否则二次开发三次开发有错误）
	private int curFragmentIndex = 0;// 当前fragment的index
	//用户自定义的底部Tab菜单内容
	private int[] TabIndex = {1,2,6,10,0};  //指向n-1个	Fragment，0代表不显示
	private String[] TabTitle = {"消息","好友","部门","管理","我"}; //4个Tab的文字内容
	private int[] curTabSelect = {0,0,0,0,0}; //5个Tab各自选中的是哪一个,0代表还没选中
	//用户自定义的顶部Menu菜单内容	
	private int[][] MenuIndex={{21,22,23,24},
                       {1,2,3,4},{5,6,7,8},{9,10,11,0},{0,0,0,0}}; //极意点击后，显示哪个Fragment	
	private String[][] MenuTitle = {{" 登      录   "," 注      册  "," 扫  一 扫  "," 支    付 "},{" 注册好友"," 通  讯  簿"," 直     播"," 附近的人"},
			{" 部        门"," 校  友  录"," 自  建  群"," 社        区"},{" 设        置"," 视        频"," 管        理"," 管        理"},{"设    置","视    频","管    理","管    理"}};
	//用户自定义的FragmentTitle	
	private String[] FragmentTitle = { "联信", "好友","通讯簿","直播","附近的人",
			"部门","同学录", "自建群","社区","设置" ,"视频" ,"管理"};	
    //杳信TM：解禁下面，备注上面红色部分
	//private int[] TabIndex = {1,2,6,8,10};  //指向n-1个	Fragment，0代表不显示
	//private String[] TabTitle = {"会话","好友","部门","发现","我"}; //4个Tab的文字内容	
	//private int[][] MenuIndex={{21,22,0,0},
    //        {0,2,3,4},{0,6,7,8},{0,10,11,0},{0,0,0,0}}; //极意点击后，显示哪个Fragment	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		MyApp.getInstance().addActivity(this);			
		findViewById();
		initData();		

		initTabView();
		initAvailableFragment();		
	   // initVersion();
		//initViews();		
		//setOnListener();
		initPopWindow();
		initOtherViews();	
		initReceiver();
		initService();
	//	updateRecentName();			
		//获取屏幕分辨率		
		DisplayMetrics dm = getResources().getDisplayMetrics();
		GloableParams.WIN_WIDTH = dm.widthPixels;
		GloableParams.WIN_HEIGHT = dm.heightPixels;	
    //    PollingUtils.startPollingService(this, 5, PollingService.class, PollingService.ACTION);
		String strChecker = PreferenceUtils.getInstance(MyApp.getInstance()).getPermissionChecker();
		int Checker = Integer.parseInt(strChecker.trim());	
		if(Checker<10){
			PreferenceUtils.getInstance(this).setPermissionChecker(""+10);//已经能够正常打开了			
		}		
				
	}

	private void initTabView() {
		
		//unreaMsgdLabel = (TextView) findViewById(R.id.unread_msg_number);
		//unreadAddressLable = (TextView) findViewById(R.id.unread_address_number);
		//unreadFindLable = (TextView) findViewById(R.id.unread_find_number);

		imageButtons = new ImageView[5];
		imageButtons[0] = (ImageView) findViewById(R.id.ib_recent);
		imageButtons[1] = (ImageView) findViewById(R.id.ib_friend);
		imageButtons[2] = (ImageView) findViewById(R.id.ib_depart);
		imageButtons[3] = (ImageView) findViewById(R.id.ib_manage);
		imageButtons[4] = (ImageView) findViewById(R.id.ib_keep);
		
		imageButtons[0].setSelected(true);
		textViews = new TextView[5];
		textViews[0] = (TextView) findViewById(R.id.tv_recent);
		textViews[1] = (TextView) findViewById(R.id.tv_friend);
		textViews[2] = (TextView) findViewById(R.id.tv_depart);
		textViews[3] = (TextView) findViewById(R.id.tv_manage);
		textViews[4] = (TextView) findViewById(R.id.tv_keep);		
		textViews[0].setTextColor(0xFF45C01A);
		//用定制的Tab文本
		for(int ni=0;ni<5;ni++){
			textViews[ni].setText(TabTitle[ni]);		
		}
		//底部Tab是显示4项还是显示5项
		if(TabIndex[4] == 0){
			findViewById(R.id.re_keep).setVisibility(View.GONE);			
		}else{
			findViewById(R.id.re_keep).setVisibility(View.VISIBLE);			
		}	
	}

	private void initAvailableFragment() {
	
		homefragment = new Fragment_Recent();	
		friendfragment = new Fragment_Friend();
		contactlistfragment = new Fragment_Contact();
		nearbyfragment = new Fragment_Nearby();	
		livelistfragment = new Fragment_Live();		
		departfragment = new Fragment_Depart();			
		classfragment = new Fragment_Class();
		crowdfragment = new Fragment_Crowd();
		cityfragment = new Fragment_City();	
		officefragment = new Fragment_Office();	
		moviefragment = new Fragment_Movie();			
		setupfragment = new Fragment_Setup();		
		
		fragMents = new Fragment[] { homefragment, friendfragment,contactlistfragment,livelistfragment,nearbyfragment,
				departfragment,classfragment, crowdfragment,cityfragment,setupfragment,moviefragment,officefragment };
		
		// 添加显示第一个fragment
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fragment_container, homefragment)
				.add(R.id.fragment_container, friendfragment)				
				.add(R.id.fragment_container, contactlistfragment)
				.add(R.id.fragment_container, livelistfragment)				
				.add(R.id.fragment_container, nearbyfragment)			

				.add(R.id.fragment_container, departfragment)

				.add(R.id.fragment_container, classfragment)	
				.add(R.id.fragment_container, crowdfragment)
				.add(R.id.fragment_container, cityfragment)	
	            .add(R.id.fragment_container, officefragment)
	            .add(R.id.fragment_container, moviefragment)	            
				.add(R.id.fragment_container, setupfragment)
					
				.hide(friendfragment)
				.hide(contactlistfragment)
				.hide(livelistfragment)					
				.hide(nearbyfragment)
				.hide(departfragment)				
				.hide(classfragment)
				.hide(crowdfragment)
				.hide(cityfragment)	
	            .hide(officefragment)
	            .hide(moviefragment)			
				.hide(setupfragment)	
					
				.show(homefragment).commit();

		//updateUnreadLabel();	
	}	
	
	protected void loadCustomCaption() {

	}	
	
	public void onTabClicked(View view) {
		img_right.setVisibility(View.GONE);
		int index = 0;
		int id = view.getId();
		if (id == R.id.re_recent) {
			index = 0;
		} else if (id == R.id.re_friend) {
			index = 1;
		} else if (id == R.id.re_depart) {
			index = 2;
		} else if (id == R.id.re_manage) {
			index = 3;
		} else if (id == R.id.re_keep) {
			index = 4;
		}
		//txt_title.setText(TabTitle[index]);		
		if(index == 0){
			img_right.setVisibility(View.VISIBLE);
			img_right.setImageResource(R.drawable.icon_add);			
		}else{
			if(MenuIndex[index][0]!=0){
				img_right.setVisibility(View.VISIBLE);
				img_right.setImageResource(R.drawable.icon_titleaddfriend);				
			}
		}
		//修改当前选中的Fragment
		int frgindex = 0;		
		if(index != 0){
			if(curTabSelect[index]==0){  //还没选中菜单
				frgindex = TabIndex[index]-1;	
			}else{
			    int menuindex = curTabSelect[index] - 1;
			    frgindex = MenuIndex[index][menuindex];	
			}
		}
		if( frgindex != curFragmentIndex ){
			SwitchFragment( frgindex );				
		}		
		
		onFragmentChanged(frgindex);		
		txt_title.setText(FragmentTitle[frgindex]);			
		String	str = "" + GloableParams.m_szUserId + "\n" + GloableParams.m_szUserName + "\n" + GloableParams.m_szServerIp;//用户呢称		
		txt_left.setText(str);
		txt_left.setVisibility(View.VISIBLE);		
        //修改Tab选中状态
		imageButtons[currentTabIndex].setSelected(false);
		imageButtons[index].setSelected(true);
		textViews[currentTabIndex].setTextColor(0xFF999999);
		textViews[index].setText(TabTitle[index]);		
		textViews[index].setTextColor(0xFF45C01A);
		currentTabIndex = index;
	}

	private int initPopWindow() {
		if(MenuIndex[currentTabIndex][0]==0)
			return 0;
		// 实例化标题栏弹窗
		titlePopup = new TitlePopup(this, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		titlePopup.setItemOnClickListener(onitemClick);
		// 给标题栏弹窗添加子类	
		titlePopup.addAction(new ActionItem(this, MenuTitle[currentTabIndex][0], R.drawable.icon_menu_group));	
		if(MenuIndex[currentTabIndex][1]!=0)
			titlePopup.addAction(new ActionItem(this, MenuTitle[currentTabIndex][1], R.drawable.icon_menu_addfriend));
		if(MenuIndex[currentTabIndex][2]!=0)
			titlePopup.addAction(new ActionItem(this, MenuTitle[currentTabIndex][2], R.drawable.icon_menu_sao));
		if(MenuIndex[currentTabIndex][3]!=0)
			titlePopup.addAction(new ActionItem(this, MenuTitle[currentTabIndex][3], R.drawable.icon_menu_abv));		

		return 1;
	}

	protected void initVersion() {
		// TODO 检查版本更新
		String versionInfo = Utils.getValue(this, "VersionInfo");
		if (!TextUtils.isEmpty(versionInfo)) {
			Tipdialog = new WarnTipDialog(this,
					"发现新版本：  1、立即更新   2、稍后更新？");
			Tipdialog.setBtnOkLinstener(onclick);
			Tipdialog.show();
		}
	}

	private DialogInterface.OnClickListener onclick1 = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Utils.showLongToast(getApplicationContext(), "正在下载...");// TODO
			Tipdialog.dismiss();
		}
	};	
	
	private OnItemOnClickListener onitemClick = new OnItemOnClickListener() {

		@Override
		public void onItemClick(ActionItem item, int position) {
			Intent intent = new Intent();
			if (currentTabIndex == 0) {
				switch (position) {
				case 0:// 登录
					intent.setClass(MainActivity.this, LoginActivity.class);
					Utils.start_Activity(MainActivity.this,intent);				
					break;
				case 1:// 注册
					intent.setClass(MainActivity.this, RegisterActivity.class);
					Utils.start_Activity(MainActivity.this,intent);				
					break;
				case 2:// 扫一扫
					//Utils.start_Activity(MainActivity.this, CaptureActivity.class);
					break;
				case 3:// 收钱
					intent.setClass(MainActivity.this, GetMoneyActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
				curTabSelect[currentTabIndex] = position + 1; 				
			}else{
				int frgindex = MenuIndex[currentTabIndex][position];
				if(frgindex!=0&&frgindex!=curFragmentIndex){
					SwitchFragment( frgindex );					
				}
				curTabSelect[currentTabIndex] = position + 1; 
			}
		}
	};
		
    //切换Fragment
	private void SwitchFragment( int frgindex ) {
		FragmentTransaction trx = getSupportFragmentManager()
				.beginTransaction();
		trx.hide(fragMents[curFragmentIndex]);
		if (!fragMents[frgindex].isAdded()) {
			trx.add(R.id.fragment_container, fragMents[frgindex]);
		}
		trx.show(fragMents[frgindex]).commit();
		curFragmentIndex = frgindex;
	}	

	private void findViewById() {
		txt_title = (TextView) findViewById(R.id.txt_title);
		img_right = (ImageView) findViewById(R.id.img_right);
		txt_left = (TextView) findViewById(R.id.txt_left);
	}

	private void initOtherViews() {	
		// 设置消息页面为初始页面
		img_right.setVisibility(View.VISIBLE);
		img_right.setImageResource(R.drawable.icon_add);	
		img_right.setOnClickListener(this);	
		
		unreaMsgdLabel = (TextView) findViewById(R.id.unread_msg_number);
		unreadAddressLable = (TextView) findViewById(R.id.unread_address_number);
		unreadFindLable = (TextView) findViewById(R.id.unread_find_number);	
		
		updateUnreadLabel();		
	}	
	
	private int keyBackClickCount = 0;
	public int layBottomVisible = 1;
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (layBottomVisible == 0) {
				layBottomVisible = 1;
				LinearLayout lay_bottom = (LinearLayout) findViewById(R.id.main_bottom);
				lay_bottom.setVisibility(View.VISIBLE);
				return true;
			}
			switch (keyBackClickCount++) {
			case 0:
				Toast.makeText(this, "再次按返回键退出", Toast.LENGTH_SHORT).show();
				Timer timer = new Timer();
				timer.schedule(new TimerTask() {
					@Override
					public void run() {
						keyBackClickCount = 0;
					}
				}, 3000);
				break;
			case 1:
				MyApp.getInstance().exit();
				finish();
				overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
				break;
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void setLayBottomVisible() {
		LinearLayout lay_bottom = (LinearLayout) findViewById(R.id.main_bottom);
		if (layBottomVisible == 0) {
			layBottomVisible = 1;
			lay_bottom.setVisibility(View.VISIBLE);
		} else {
			layBottomVisible = 0;
			lay_bottom.setVisibility(View.GONE);
		}
	}
	@Override
	public void onClick(View v) {
		if (v.getId() == R.id.img_right) {
			int ret = initPopWindow();
			if (ret != 0)
				titlePopup.show(findViewById(R.id.layout_bar));
		}
	}

	public void onFragmentChanged(int curIndex) {
		switch (curIndex) {
		case 0:
			if (homefragment != null) {			
				homefragment.refresh();
			}
			break;
		case 1:
			if (friendfragment != null) {
			//	friendfragment.refresh();
			}
			break;
		case 2:
			if (contactlistfragment != null) {
				contactlistfragment.refresh();
			}
			break;
		case 3:
			if (livelistfragment != null) {
	
			}
			break;			
		case 4:
			if (nearbyfragment != null) {
				nearbyfragment.refresh();
			}
			break;
		case 5:
			if (departfragment != null) {
				departfragment.refresh();
			}
			break;	
		case 6:
			if (classfragment != null) {
				classfragment.refresh();
			}
			break;
		case 7:
			if (crowdfragment != null) {
				crowdfragment.refresh();
			}
			break;
		case 8:
			if (cityfragment != null) {
				cityfragment.refresh();
			}
			break;				
		default:
			break;
		}
	}	

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);	
		if (resultCode == 50) {
			setResult(RESULT_OK);
			finish();
			return;
		}
		if (resultCode == RESULT_OK) { // 清空消息
			Intent intent = new Intent();			
			switch (requestCode) {
			case 20:
				intent.setClass(MainActivity.this, UserListActivity.class);			
			    startActivity(intent);
				break;
			case 1:
				if (friendfragment != null) {
			//		friendfragment.refresh();
				}
				break;
			case 2:
				if (contactlistfragment != null) {
					contactlistfragment.refresh();
				}
				break;
			
			default:
				break;
			}			
		}
	}

	private DialogInterface.OnClickListener onclick = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			Utils.showLongToast(MainActivity.this, "正在下载...");// TODO
			Tipdialog.dismiss();
		}
	};

	/**
	 * 获取未读消息数
	 */
	public void updateUnreadLabel() {
		int count = 0;
        for(RecentItem mitem : GloableParams.RecentList){     
        	count += mitem.newNum; 
   
        }		
		if (count > 0) {
			unreaMsgdLabel.setText(String.valueOf(count));
			unreaMsgdLabel.setVisibility(View.VISIBLE);
		} else {
			unreaMsgdLabel.setVisibility(View.INVISIBLE);
		}
	}

	public void updateRecentName1() {
        if(GloableParams.RecentList==null) 
        	return;
		for (RecentItem entity : GloableParams.RecentList) {
			String strMsg = MyApp.getInstance().getRecentName(entity.userId,entity.type);			
	        String[] arrStr = strMsg.toString().split("\\,");
			if (arrStr.length < 2) 
				return;        
			String strName = arrStr[0];
			int image = Integer.parseInt(arrStr[1]);			
			entity.setName(	strName);
			entity.setHeadImg(image);			
		} 
	//	homefragment.refresh();		
	}
	
	@Override
	protected void NewUserLoginCastonReceive(Context context, Intent intent) {
		//nMode=1:LoginOk  nMode=2:LogOff
		//nMode=3:RegOut  nMode=4:OtherLogin		
		int nMode = intent.getIntExtra("mode", 0);  //nMode=1:LoginOk			
		// 注销广播接收者，否则在ChatActivity中会收到这个广播
	//	abortBroadcast();
		if( TextUtils.isEmpty(GloableParams.m_szServerIp) ){ //如果是退出登录
			initData();		
		}else if(nMode==0||nMode==2){
			txt_left.setText("");
			txt_left.setVisibility(View.INVISIBLE);		
		}else if(nMode==1||nMode==3){
			String	str = "" + GloableParams.m_szUserId + "\n" + GloableParams.m_szUserName + "\n" + GloableParams.m_szServerIp;//用户呢称		
			txt_left.setText(str);
			txt_left.setVisibility(View.VISIBLE);	
		}else if(nMode==4){
			txt_left.setText("");
			txt_left.setVisibility(View.INVISIBLE);			
			OnUser_OtherLogin();				
		}
		if(nMode>=2)
			return;
		//刷新用户自定义头像
		updataMyUserHead();		
		// 当前页面如果为聊天历史页面，刷新此页面
		if (homefragment != null) {
			homefragment.refreshNewUser();
		}
		if (setupfragment != null) {
			setupfragment.refreshNewUser();
		}		
	}
	
	public void OnUser_OtherLogin() {
		new MyAlertDialog(MainActivity.this,"登录提醒",
				"你的PQ号已经在另外一个地方登录，\n你需要重新登录吗吗?",
				new MyAlertDialog.OnSetListener() {
					@Override
					public void onSelect(int comm) {
						//User_LogAgain( nUserId );							
					}
				}).show();		
			
	}	
	@Override
	protected void NewNearPeopleCastonReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		// 注销广播接收者，否则在ChatActivity中会收到这个广播
	//	abortBroadcast();
		// 当前页面如果为聊天历史页面，刷新此页面
		if (nearbyfragment != null) {		
		//if (nearbyfragment != null && curfragmentIndex == 4) {
			nearbyfragment.refresh();
		}	
	
	}
	@Override
	protected void NewLivePersonCastonReceive(Context context, Intent intent) {
		int userid = intent.getIntExtra("UserID",0);
		//if (livelistfragment != null) {		
		if (livelistfragment != null && curFragmentIndex == 3) {
			if(userid>1000)
			livelistfragment.updateItemData(userid);
			else
			livelistfragment.refresh();			
		}
			
	}	
	@Override
	protected void UpdataCastonReceive(Context context, Intent intent) {

		int nType = intent.getIntExtra("Chat_Type", 0);			
	//	abortBroadcast();			
		switch (nType) {		
		case DefineUser.TYPE_RECENT:
			if (homefragment != null) {
				homefragment.refreshNewUser();
			} 			
			break;
		case DefineUser.TYPE_FRIEND:
			if (friendfragment != null) {
				friendfragment.refreshNewUser();
			} 			
			break;				
		case DefineUser.TYPE_CROWD:
			if (crowdfragment != null) {
				crowdfragment.refreshNewUser();
			} 			
			break;			
		case DefineUser.TYPE_MYCLASS:
			if (classfragment != null) {
				classfragment.refreshNewUser();
			}			
			break;
		case DefineUser.TYPE_DEPART:
			if (departfragment != null) {
				departfragment.refreshNewUser();
			} 			
			break;			
		case DefineUser.TYPE_PARTY:
			if (departfragment != null) {
				departfragment.refreshNewUser();
			}		
			break;
		case DefineUser.TYPE_GROUP:
			if (cityfragment != null) {
				cityfragment.refreshNewUser();
			}		
			break;
		case DefineUser.TYPE_CITY:
			if (cityfragment != null) {
				cityfragment.refreshNewUser();
			}			
			break;	
		case 99:
			updataUserHeadEx();  //刷新用户自定义头像			
			break;			
		default:
		
			break;
		}						
	}

	@Override
	protected void EditUserCastonReceive(Context context, Intent intent) {
		int nType = intent.getIntExtra("Chat_Type", 0);			
	//	abortBroadcast();			
		switch (nType) {		
		case DefineUser.TYPE_RECENT:
			if (homefragment != null) {
				homefragment.refreshNewUser();
			} 			
			break;
		case DefineUser.TYPE_FRIEND:
			if (friendfragment != null) {
				friendfragment.refreshNewUser();
			} 			
			break;				
		case DefineUser.TYPE_CROWD:
			if (crowdfragment != null) {
				crowdfragment.refreshNewUser();
			} 			
			break;			
		case DefineUser.TYPE_MYCLASS:
			if (classfragment != null) {
				classfragment.refreshNewUser();
			}			
			break;
		case DefineUser.TYPE_DEPART:
			if (departfragment != null) {
				departfragment.refreshNewUser();
			} 			
			break;			
		case DefineUser.TYPE_PARTY:
			/*if (partyfragment != null) {
				partyfragment.refreshNewUser();
			}*/	
			break;
		case DefineUser.TYPE_GROUP:
			if (cityfragment != null) {
				cityfragment.refreshNewUser();
			}		
			break;
		case DefineUser.TYPE_CITY:
			if (cityfragment != null) {
				cityfragment.refreshNewUser();
			}			
			break;				
		default:
		
			break;
		}					
	}	
	@Override
	protected void ackMessageCastonReceive(Context context, Intent intent) {
	//	abortBroadcast();
		// 刷新bottom bar消息未读数
		updateUnreadLabel();
		String msgid = intent.getStringExtra("msgid");
		int Destid = intent.getIntExtra("Chat_ID",10002);
		int Type = intent.getIntExtra("Chat_Type", 0);
		EMConversation conversation = EMChatManager.getInstance()
				.getConversation(Destid,Type);
		if (conversation != null) {
			// 把message设为已读
			EMMessage msg = conversation.getMessage(msgid);

			if (msg != null) {
				msg.isAcked = true;
			}
		}		
	}
	@Override
	protected void SysMessageBroadcastonReceive(Context context, Intent intent) {
		// 主页面收到消息后，主要为了提示未读，实际消息内容需要到chat页面查看
		int Destid = intent.getIntExtra("Chat_ID",10002);
		int Type = intent.getIntExtra("Chat_Type", 0);

		// 注销广播接收者，否则在ChatActivity中会收到这个广播
	//	abortBroadcast();
		// 刷新bottom bar消息未读数
		updateUnreadLabel();
		if (currentTabIndex == 0) {
			// 当前页面如果为聊天历史页面，刷新此页面
			if (homefragment != null) {
				homefragment.refresh();
			}
		}		
	}
	@Override
	protected void NewMessageBroadcastonReceive(Context context, Intent intent) {
		// 主页面收到消息后，主要为了提示未读，实际消息内容需要到chat页面查看

		String from = intent.getStringExtra("from");
		// 消息id
		String msgId = intent.getStringExtra("msgid");
	
		int Destid = intent.getIntExtra("Chat_ID",10002);
		int Type = intent.getIntExtra("Chat_Type", 0);
		int toNext = 0;
		if (EMChatActivity.activityInstance != null) {
			/*if (Destid == EMChatActivity.activityInstance.toChatDestid) {
				if (Type == EMChatActivity.activityInstance.chatType)
					toNext = 1;
			//		return;
			}*/
		}			

		// 刷新bottom bar消息未读数
		updateUnreadLabel();
		if (currentTabIndex == 0) {
			// 当前页面如果为聊天历史页面，刷新此页面
			if (homefragment != null) {
				homefragment.refresh();
			}
		}		
	}	
}