Java tutorial
/** * Copyright (C) 2016 Hyphenate Inc. 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.ai.ui.partybuild.message; import java.util.List; import com.ai.framework.business.BusinessApplication; import com.hyphenate.EMMessageListener; import com.hyphenate.chat.EMClient; import com.hyphenate.chat.EMConversation; import com.hyphenate.chat.EMConversation.EMConversationType; import com.hyphenate.chat.EMMessage; import com.hyphenate.chatuidemo.Constant; import com.ai.partybuild.R; import com.hyphenate.chatuidemo.runtimepermissions.PermissionsManager; import com.hyphenate.chatuidemo.runtimepermissions.PermissionsResultAction; import com.hyphenate.chatuidemo.ui.BaseActivity; import com.hyphenate.chatuidemo.ui.ConversationListFragment; import com.hyphenate.chatuidemo.ui.GroupFragment; import com.hyphenate.easeui.utils.EaseCommonUtils; import android.annotation.SuppressLint; import android.annotation.TargetApi; 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.FragmentTransaction; import android.support.v4.content.LocalBroadcastManager; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; @SuppressLint("NewApi") public class HyphenateMainActivity extends BaseActivity { protected static final String TAG = "HyphenateMainActivity"; // textview for unread message count private TextView unreadLabel; // textview for unread event message private TextView unreadAddressLable; private Button[] mTabs; private GroupFragment groupFragment; private Fragment[] fragments; private int index; private int currentTabIndex; // user logged into another device public boolean isConflict = false; // user account was removed private boolean isCurrentAccountRemoved = false; /** * check if current user account was remove */ public boolean getCurrentAccountRemoved() { return isCurrentAccountRemoved; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.em_activity_main); // runtime permission for android 6.0, just require all permissions here for simple requestPermissions(); initView(); // if (getIntent().getBooleanExtra(Constant.ACCOUNT_CONFLICT, false) && !isConflictDialogShow) { // showConflictDialog(); // } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false) && !isAccountRemovedDialogShow) { // showAccountRemovedDialog(); // } conversationListFragment = new ConversationListFragment(); groupFragment = new GroupFragment(); fragments = new Fragment[] { conversationListFragment, groupFragment }; getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, conversationListFragment) .add(R.id.fragment_container, groupFragment).hide(groupFragment).show(conversationListFragment) .commit(); //register broadcast receiver to receive the change of group from DemoHelper registerBroadcastReceiver(); //debug purpose only } @TargetApi(23) private void requestPermissions() { PermissionsManager.getInstance().requestAllManifestPermissionsIfNecessary(this, new PermissionsResultAction() { @Override public void onGranted() { // Toast.makeText(HyphenateMainActivity.this, "All permissions have been granted", Toast.LENGTH_SHORT).show(); } @Override public void onDenied(String permission) { //Toast.makeText(HyphenateMainActivity.this, "Permission " + permission + " has been denied", Toast.LENGTH_SHORT).show(); } }); } /** * init views */ private void initView() { unreadLabel = (TextView) findViewById(R.id.unread_msg_number); unreadAddressLable = (TextView) findViewById(R.id.unread_address_number); mTabs = new Button[3]; mTabs[0] = (Button) findViewById(R.id.btn_conversation); mTabs[1] = (Button) findViewById(R.id.btn_address_list); // select first tab mTabs[0].setSelected(true); } /** * on tab clicked * * @param view */ public void onTabClicked(View view) { switch (view.getId()) { case R.id.btn_conversation: index = 0; break; case R.id.btn_address_list: index = 1; break; } if (currentTabIndex != index) { FragmentTransaction trx = getSupportFragmentManager().beginTransaction(); trx.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { trx.add(R.id.fragment_container, fragments[index]); } trx.show(fragments[index]).commit(); } mTabs[currentTabIndex].setSelected(false); // set current tab selected mTabs[index].setSelected(true); currentTabIndex = index; } EMMessageListener messageListener = new EMMessageListener() { @Override public void onMessageReceived(List<EMMessage> messages) { // notify new message // for (EMMessage message : messages) { // BusinessApplication.easeUI.getNotifier().onNewMsg(message); // } refreshUIWithMessage(); } @Override public void onCmdMessageReceived(List<EMMessage> messages) { refreshUIWithMessage(); } @Override public void onMessageReadAckReceived(List<EMMessage> messages) { } @Override public void onMessageDeliveryAckReceived(List<EMMessage> message) { } @Override public void onMessageChanged(EMMessage message, Object change) { } }; private void refreshUIWithMessage() { runOnUiThread(new Runnable() { public void run() { // refresh unread count updateUnreadLabel(); if (currentTabIndex == 0) { // refresh conversation list if (conversationListFragment != null) { conversationListFragment.refresh(); } } } }); } @Override public void back(View view) { super.back(view); } @Override protected void onDestroy() { super.onDestroy(); if (conflictBuilder != null) { conflictBuilder.create().dismiss(); conflictBuilder = null; } unregisterBroadcastReceiver(); } private void registerBroadcastReceiver() { broadcastManager = LocalBroadcastManager.getInstance(this); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Constant.ACTION_CONTACT_CHANAGED); intentFilter.addAction(Constant.ACTION_GROUP_CHANAGED); broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { updateUnreadLabel(); if (currentTabIndex == 0) { // refresh conversation list if (conversationListFragment != null) { conversationListFragment.refresh(); } } String action = intent.getAction(); if (action.equals(Constant.ACTION_GROUP_CHANAGED)) { GroupFragment.instance.onResume(); } } }; broadcastManager.registerReceiver(broadcastReceiver, intentFilter); } private void unregisterBroadcastReceiver() { broadcastManager.unregisterReceiver(broadcastReceiver); } /** * update unread message count */ public void updateUnreadLabel() { int count = getUnreadMsgCountTotal(); if (count > 0) { unreadLabel.setText(String.valueOf(count)); unreadLabel.setVisibility(View.VISIBLE); } else { unreadLabel.setVisibility(View.INVISIBLE); } } /** * get unread message count * * @return */ public int getUnreadMsgCountTotal() { int unreadMsgCountTotal = 0; int chatroomUnreadMsgCount = 0; unreadMsgCountTotal = EMClient.getInstance().chatManager().getUnreadMsgsCount(); for (EMConversation conversation : EMClient.getInstance().chatManager().getAllConversations().values()) { if (conversation.getType() == EMConversationType.ChatRoom) chatroomUnreadMsgCount = chatroomUnreadMsgCount + conversation.getUnreadMsgCount(); } return unreadMsgCountTotal - chatroomUnreadMsgCount; } @Override protected void onResume() { super.onResume(); if (!isConflict && !isCurrentAccountRemoved) { updateUnreadLabel(); } // unregister this event listener when this activity enters the // background EMClient.getInstance().chatManager().addMessageListener(messageListener); } @Override protected void onStop() { EMClient.getInstance().chatManager().removeMessageListener(messageListener); super.onStop(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(false); return true; } return super.onKeyDown(keyCode, event); } private android.app.AlertDialog.Builder conflictBuilder; private android.app.AlertDialog.Builder accountRemovedBuilder; private boolean isConflictDialogShow; private boolean isAccountRemovedDialogShow; private BroadcastReceiver internalDebugReceiver; private ConversationListFragment conversationListFragment; private BroadcastReceiver broadcastReceiver; private LocalBroadcastManager broadcastManager; }