Java tutorial
/** * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.xuejian.client.lxp.module.toolbox.im; import android.app.Activity; import android.app.AlertDialog; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.text.TextUtils; import android.util.Log; import android.view.Display; import android.view.Gravity; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; import com.aizou.core.http.HttpCallBack; import com.aizou.core.utils.LocalDisplay; import com.aizou.core.widget.pagerIndicator.indicator.FixedIndicatorView; import com.aizou.core.widget.pagerIndicator.indicator.IndicatorViewPager; import com.aizou.core.widget.pagerIndicator.indicator.slidebar.ColorBar; import com.aizou.core.widget.pagerIndicator.viewpager.FixedViewPager; import com.aizou.core.widget.popupmenu.PopupMenuCompat; import com.easemob.chat.CmdMessageBody; import com.easemob.chat.EMChat; import com.easemob.chat.EMChatManager; import com.easemob.chat.EMConversation; import com.easemob.chat.EMGroupManager; import com.easemob.chat.EMMessage; import com.easemob.chat.EMMessage.ChatType; import com.easemob.chat.EMMessage.Type; import com.easemob.chat.EMNotifier; import com.easemob.chat.GroupChangeListener; import com.easemob.chat.TextMessageBody; import com.easemob.exceptions.EaseMobException; import com.umeng.analytics.MobclickAgent; import com.xuejian.client.lxp.R; import com.xuejian.client.lxp.base.ChatBaseActivity; import com.xuejian.client.lxp.bean.ContactListBean; import com.xuejian.client.lxp.bean.PeachUser; import com.xuejian.client.lxp.common.account.AccountManager; import com.xuejian.client.lxp.common.api.UserApi; import com.xuejian.client.lxp.common.gson.CommonJson; import com.xuejian.client.lxp.common.utils.CommonUtils; import com.xuejian.client.lxp.common.utils.IMUtils; import com.xuejian.client.lxp.config.Constant; import com.xuejian.client.lxp.db.IMUser; import com.xuejian.client.lxp.db.InviteMessage; import com.xuejian.client.lxp.db.InviteStatus; import com.xuejian.client.lxp.db.respository.IMUserRepository; import com.xuejian.client.lxp.db.respository.InviteMsgRepository; import com.xuejian.client.lxp.module.MainActivity; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; public class IMMainActivity extends ChatBaseActivity { public static final int NEW_CHAT_REQUEST_CODE = 101; protected static final String TAG = "MainActivity"; // ?textview private TextView unreadLabel; // textview private TextView unreadAddressLable; FixedIndicatorView mIMIndicator; FixedViewPager mIMViewPager; private IndicatorViewPager indicatorViewPager; private IMMainAdapter mIMdapter; private ContactlistFragment contactListFragment; // private ChatHistoryFragment chatHistoryFragment; private ChatAllHistoryFragment chatHistoryFragment; private Fragment[] fragments; // ?fragmentindex private int currentTabIndex; private NewMessageBroadcastReceiver msgReceiver; private MyGroupChangeListener groupChangeListener; // ? private boolean isConflict = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_im_main); initView(); //fragment??? // chatHistoryFragment = new ChatHistoryFragment(); //?fragment chatHistoryFragment = new ChatAllHistoryFragment(); contactListFragment = new ContactlistFragment(); fragments = new Fragment[] { chatHistoryFragment, contactListFragment }; if (savedInstanceState == null) { } else { currentTabIndex = savedInstanceState.getInt("currentTabIndex"); } indicatorViewPager = new IndicatorViewPager(mIMIndicator, mIMViewPager); indicatorViewPager.setPageOffscreenLimit(2); mIMViewPager.setPrepareNumber(2); indicatorViewPager.setAdapter(mIMdapter = new IMMainAdapter(getSupportFragmentManager())); indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() { @Override public void onIndicatorPageChange(int preItem, int currentItem) { currentTabIndex = currentItem; } }); indicatorViewPager.setCurrentItem(currentTabIndex, false); // EMGroupManager.getInstance().asyncGetGroupsFromServer(new EMValueCallBack<List<EMGroup>>() { // @Override // public void onSuccess(List<EMGroup> emGroups) { // if (chatHistoryFragment != null) { // chatHistoryFragment.refresh(); // } // } // // @Override // public void onError(int i, String s) { // // } // }); //? getContactFromServer(); // cmd?BroadcastReceiver IntentFilter cmdIntentFilter = new IntentFilter(EMChatManager.getInstance().getCmdMessageBroadcastAction()); cmdIntentFilter.setPriority(3); mContext.registerReceiver(cmdMessageReceiver, cmdIntentFilter); // ?BroadcastReceiver msgReceiver = new NewMessageBroadcastReceiver(); IntentFilter intentFilter = new IntentFilter(EMChatManager.getInstance().getNewMessageBroadcastAction()); intentFilter.setPriority(3); registerReceiver(msgReceiver, intentFilter); // ack?BroadcastReceiver IntentFilter ackMessageIntentFilter = new IntentFilter( EMChatManager.getInstance().getAckMessageBroadcastAction()); ackMessageIntentFilter.setPriority(3); registerReceiver(ackMessageReceiver, ackMessageIntentFilter); // ?listener groupChangeListener = new MyGroupChangeListener(); EMGroupManager.getInstance().addGroupChangeListener(groupChangeListener); // sdkUI ??receiverlistener, ??broadcast EMChat.getInstance().setAppInited(); } @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); indicatorViewPager.setCurrentItem(0, false); } public void refreshChatHistoryFragment() { if (chatHistoryFragment != null) { chatHistoryFragment.refresh(); } } public void refreshContactListFragment() { if (contactListFragment != null) { contactListFragment.refresh(); } } @Override public void finish() { super.finish(); } /** * ? */ private void initView() { initTitleBar(); mIMIndicator = (FixedIndicatorView) findViewById(R.id.im_indicator); mIMViewPager = (FixedViewPager) findViewById(R.id.im_viewpager); ColorBar colorBar = new ColorBar(this, getResources().getColor(R.color.app_theme_color_secondary), LocalDisplay.dp2px(5)); colorBar.setWidth(LocalDisplay.dp2px(45)); mIMIndicator.setScrollBar(colorBar); } private void initTitleBar() { findViewById(R.id.ly_title_bar_right).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // BlurMenu fragment = new BlurMenu(); // Bundle args = new Bundle(); // args.putInt(SupportBlurDialogFragment.BUNDLE_KEY_BLUR_RADIUS, 5); // args.putFloat(SupportBlurDialogFragment.BUNDLE_KEY_DOWN_SCALE_FACTOR, 6); // fragment.setArguments(args); // fragment.show(getSupportFragmentManager(), "blur_menu"); showActionDialog(); } }); findViewById(R.id.ly_title_bar_left).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(mContext, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finishWithNoAnim(); overridePendingTransition(0, android.R.anim.fade_out); } }); } private void showActionDialog() { final Activity act = this; final AlertDialog dialog = new AlertDialog.Builder(act).create(); View contentView = View.inflate(act, R.layout.dialog_city_detail_action, null); Button btn = (Button) contentView.findViewById(R.id.btn_go_plan); btn.setText("Talk"); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MobclickAgent.onEvent(mContext, "event_create_new_talk"); startActivityForResult(new Intent(IMMainActivity.this, PickContactsWithCheckboxActivity.class) .putExtra("request", NEW_CHAT_REQUEST_CODE), NEW_CHAT_REQUEST_CODE); dialog.dismiss(); } }); Button btn1 = (Button) contentView.findViewById(R.id.btn_go_share); btn1.setText("?"); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { MobclickAgent.onEvent(mContext, "event_add_new_friend"); startActivity(new Intent(IMMainActivity.this, AddContactActivity.class)); dialog.dismiss(); } }); contentView.findViewById(R.id.btn_cancle).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); WindowManager windowManager = act.getWindowManager(); Window window = dialog.getWindow(); window.setContentView(contentView); Display display = windowManager.getDefaultDisplay(); WindowManager.LayoutParams lp = window.getAttributes(); lp.width = (int) (display.getWidth()); // window.setAttributes(lp); window.setGravity(Gravity.BOTTOM); // ?dialog? window.setWindowAnimations(R.style.SelectPicDialog); // } @Override public void onBackPressed() { Intent intent = new Intent(mContext, MainActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); finishWithNoAnim(); overridePendingTransition(0, R.anim.fade_out); } private void showMoreMenu(View view) { PopupMenuCompat menu = new PopupMenuCompat(this, view); menu.inflate(R.menu.menu_im_add_more); menu.setOnMenuItemClickListener(new PopupMenuCompat.OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { switch (item.getItemId()) { case R.id.menu_add_friends: startActivity(new Intent(mContext, AddContactActivity.class)); break; case R.id.menu_new_message: startActivity(new Intent(mContext, PickContactsWithCheckboxActivity.class).putExtra("request", NEW_CHAT_REQUEST_CODE)); break; } return false; } }); menu.show(); } private void getContactFromServer() { UserApi.getContact(new HttpCallBack<String>() { @Override public void doSucess(String result, String method) { CommonJson<ContactListBean> contactResult = CommonJson.fromJson(result, ContactListBean.class); if (contactResult.code == 0) { IMUserRepository.clearMyFriendsContact(mContext); AccountManager.getInstance().setContactList(null); Map<String, IMUser> userlist = new HashMap<String, IMUser>(); // user"" IMUser newFriends = new IMUser(); newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME); newFriends.setNick("?"); newFriends.setHeader(""); newFriends.setIsMyFriends(true); newFriends.setUnreadMsgCount((int) InviteMsgRepository.getUnAcceptMsgCount(mContext)); userlist.put(Constant.NEW_FRIENDS_USERNAME, newFriends); // // "?" // IMUser groupUser = new IMUser(); // groupUser.setUsername(Constant.GROUP_USERNAME); // groupUser.setNick("?"); // groupUser.setHeader(""); // groupUser.setUnreadMsgCount(0); // userlist.put(Constant.GROUP_USERNAME, groupUser); // for (PeachUser peachUser : contactResult.result.contacts) { IMUser user = new IMUser(); user.setUserId(peachUser.userId); user.setMemo(peachUser.memo); user.setNick(peachUser.nickName); user.setUsername(peachUser.easemobUser); user.setUnreadMsgCount(0); user.setAvatar(peachUser.avatar); user.setAvatarSmall(peachUser.avatarSmall); user.setSignature(peachUser.signature); user.setIsMyFriends(true); user.setGender(peachUser.gender); IMUtils.setUserHead(user); userlist.put(peachUser.easemobUser, user); } // db List<IMUser> users = new ArrayList<IMUser>(userlist.values()); IMUserRepository.saveContactList(mContext, users); AccountManager.getInstance().setContactList(userlist); refreshContactListFragment(); refreshChatHistoryFragment(); } } @Override public void doFailure(Exception error, String msg, String method) { // ToastUtil.getInstance(IMMainActivity.this).showToast(getResources().getString(R.string.request_network_failed)); } }); } private class IMMainAdapter extends IndicatorViewPager.IndicatorFragmentPagerAdapter { public IMMainAdapter(FragmentManager fragmentManager) { super(fragmentManager); } @Override public int getCount() { return fragments.length; } @Override public View getViewForTab(int position, View convertView, ViewGroup container) { View view = convertView; if (view == null) { if (position == 0) { view = View.inflate(mContext, R.layout.tab_im_conversation, null); } else { view = View.inflate(mContext, R.layout.tab_im_contact, null); } } if (position == 0) { unreadLabel = (TextView) view.findViewById(R.id.unread_msg_notify); } else { unreadAddressLable = (TextView) view.findViewById(R.id.unread_address_number); } return view; } @Override public Fragment getFragmentForPage(int position) { return fragments[position]; } } @Override protected void onDestroy() { super.onDestroy(); // try { unregisterReceiver(msgReceiver); } catch (Exception e) { } try { unregisterReceiver(ackMessageReceiver); } catch (Exception e) { } try { unregisterReceiver(cmdMessageReceiver); } catch (Exception e) { } try { // ?listener EMGroupManager.getInstance().removeGroupChangeListener(groupChangeListener); } catch (Exception e) { } } /** * ? */ public void updateUnreadLabel() { int count = getUnreadMsgCountTotal(); if (count > 0) { unreadLabel.setVisibility(View.VISIBLE); unreadLabel.setText(count + ""); } else { unreadLabel.setVisibility(View.GONE); } } /** * ? */ public void updateUnreadAddressLable() { runOnUiThread(new Runnable() { public void run() { int count = getUnreadAddressCountTotal(); if (count > 0) { unreadAddressLable.setText(String.valueOf(count)); unreadAddressLable.setVisibility(View.VISIBLE); } else { unreadAddressLable.setVisibility(View.GONE); } } }); } /** * ?? * * @return */ public int getUnreadAddressCountTotal() { int unreadAddressCountTotal = 0; unreadAddressCountTotal = (int) InviteMsgRepository.getUnAcceptMsgCount(this); if (AccountManager.getInstance().getContactList(this).get(Constant.NEW_FRIENDS_USERNAME) != null) { IMUser imUser = AccountManager.getInstance().getContactList(this).get(Constant.NEW_FRIENDS_USERNAME); imUser.setUnreadMsgCount(unreadAddressCountTotal); IMUserRepository.saveContact(this, imUser); } return unreadAddressCountTotal; } /** * ?? * * @return */ public int getUnreadMsgCountTotal() { int unreadMsgCountTotal = 0; unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount(); return unreadMsgCountTotal; } /** * cmd?BroadcastReceiver */ private BroadcastReceiver cmdMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //?cmd message String msgId = intent.getStringExtra("msgid"); EMMessage message = intent.getParcelableExtra("message"); //??body CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody(); String aciton = cmdMsgBody.action;//?action //? try { int cmdType = message.getIntAttribute("CMDType"); String content = message.getStringAttribute("content"); //?? if (cmdType == 1) { // bottom bar? updateUnreadAddressLable(); // ??ui if (currentTabIndex == 1) contactListFragment.refresh(); } //???(?) else if (cmdType == 2) { updateUnreadLabel(); if (chatHistoryFragment != null) chatHistoryFragment.refresh(); if (contactListFragment != null) contactListFragment.refresh(); } //? else if (cmdType == 3) { // ui if (chatHistoryFragment != null) chatHistoryFragment.refresh(); if (contactListFragment != null) contactListFragment.refresh(); } } catch (EaseMobException e) { e.printStackTrace(); } } }; /** * ? */ private class NewMessageBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { //????????chat? // ?id String username = intent.getStringExtra("from"); String msgid = intent.getStringExtra("msgid"); // message?db?id?mesage final EMMessage message = EMChatManager.getInstance().getMessage(msgid); final String fromUser = message.getStringAttribute(Constant.FROM_USER, ""); final String finalUsername = username; new Thread(new Runnable() { @Override public void run() { if (!TextUtils.isEmpty(fromUser)) { IMUser imUser = IMUtils.getUserInfoFromMessage(mContext, message); IMUserRepository.saveContact(mContext, imUser); } } }).start(); // bottom bar? updateUnreadLabel(); if (currentTabIndex == 0) { // ?????? if (chatHistoryFragment != null) { chatHistoryFragment.refresh(); } } // ?ChatActivity abortBroadcast(); } } /** * ?BroadcastReceiver */ private BroadcastReceiver ackMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String msgid = intent.getStringExtra("msgid"); String from = intent.getStringExtra("from"); EMConversation conversation = EMChatManager.getInstance().getConversation(from); if (conversation != null) { // message EMMessage msg = conversation.getMessage(msgid); if (msg != null) { msg.isAcked = true; } } abortBroadcast(); } }; /** * ?BroadcastReceiver * sdk ????clientreceiverUI ??? * UI ???? */ // private BroadcastReceiver offlineMessageReceiver = new BroadcastReceiver() { // // @Override // public void onReceive(Context context, Intent intent) { // String[] users = intent.getStringArrayExtra("fromuser"); // String[] groups = intent.getStringArrayExtra("fromgroup"); // if (users != null) { // for (String user : users) { // System.out.println("user?" + user); // } // } // if (groups != null) { // for (String group : groups) { // System.out.println("group?" + group); // } // } // abortBroadcast(); // } // }; /** * ???? * * @param msg */ private void notifyNewIviteMessage(InviteMessage msg) { // bottom bar? updateUnreadAddressLable(); // ??ui if (currentTabIndex == 1) contactListFragment.refresh(); } /** * MyGroupChangeListener */ private class MyGroupChangeListener implements GroupChangeListener { @Override public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) { if (chatHistoryFragment != null) { chatHistoryFragment.refresh(); } } @Override public void onInvitationAccpted(String groupId, String inviter, String reason) { if (chatHistoryFragment != null) { chatHistoryFragment.refresh(); } } @Override public void onInvitationDeclined(String groupId, String invitee, String reason) { } @Override public void onUserRemoved(String groupId, String groupName) { // ??Tdemo? // ui runOnUiThread(new Runnable() { public void run() { try { updateUnreadLabel(); if (currentTabIndex == 0) chatHistoryFragment.refresh(); if (CommonUtils.getTopActivity(IMMainActivity.this) .equals(GroupsActivity.class.getName())) { GroupsActivity.instance.onResume(); } } catch (Exception e) { Log.e("###", "refresh exception " + e.getMessage()); } } }); } @Override public void onGroupDestroy(String groupId, String groupName) { // // ??,demo? // ui runOnUiThread(new Runnable() { public void run() { updateUnreadLabel(); if (currentTabIndex == 0) chatHistoryFragment.refresh(); if (CommonUtils.getTopActivity(IMMainActivity.this).equals(GroupsActivity.class.getName())) { GroupsActivity.instance.onResume(); } } }); } @Override public void onApplicationReceived(String groupId, String groupName, String applyer, String reason) { // ? InviteMessage msg = new InviteMessage(); msg.setFrom(applyer); msg.setTime(System.currentTimeMillis()); msg.setGroupId(groupId); msg.setGroupName(groupName); msg.setReason(reason); msg.setStatus(InviteStatus.BEAPPLYED); notifyNewIviteMessage(msg); } @Override public void onApplicationAccept(String groupId, String groupName, String accepter) { //?? EMMessage msg = EMMessage.createReceiveMessage(Type.TXT); msg.setChatType(ChatType.GroupChat); msg.setFrom(accepter); msg.setTo(groupId); msg.setMsgId(UUID.randomUUID().toString()); msg.addBody(new TextMessageBody(accepter + "???")); // ???? EMChatManager.getInstance().saveMessage(msg); // ??? EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg(); runOnUiThread(new Runnable() { public void run() { updateUnreadLabel(); // ui if (currentTabIndex == 0) chatHistoryFragment.refresh(); if (CommonUtils.getTopActivity(IMMainActivity.this).equals(GroupsActivity.class.getName())) { GroupsActivity.instance.onResume(); } } }); } @Override public void onApplicationDeclined(String groupId, String groupName, String decliner, String reason) { //?demo } } @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt("currentTabIndex", currentTabIndex); super.onSaveInstanceState(outState); } @Override protected void onResume() { super.onResume(); if (!isConflict) { updateUnreadLabel(); updateUnreadAddressLable(); EMChatManager.getInstance().activityResumed(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case NEW_CHAT_REQUEST_CODE: int chatType = data.getIntExtra("chatType", 0); String toId = data.getStringExtra("toId"); if (chatType == ChatActivity.CHATTYPE_GROUP) { //? Intent intent = new Intent(mContext, ChatActivity.class); // it is group chat intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP); intent.putExtra("groupId", toId); startActivity(intent); } else { Intent intent = new Intent(mContext, ChatActivity.class); // it is single chat intent.putExtra("chatType", ChatActivity.CHATTYPE_SINGLE); intent.putExtra("userId", toId); startActivity(intent); } } } } }