Java tutorial
/** * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * <p/> * 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.sven.im.ui; import android.app.ProgressDialog; import android.content.BroadcastReceiver; import android.content.Intent; import android.support.v4.content.LocalBroadcastManager; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.Toast; import com.hyphenate.EMContactListener; import com.hyphenate.chat.EMClient; import com.hyphenate.easeui.domain.EaseUser; import com.hyphenate.easeui.ui.EaseContactListFragment; import com.hyphenate.util.EMLog; import com.sven.R; import com.sven.im.DemoHelper; import com.sven.im.db.InviteMessgeDao; import com.sven.im.db.UserDao; import com.sven.im.widget.ContactItemView; import com.sven.view.activity.NearUserActivity; import java.util.Hashtable; import java.util.Map; /** * ? */ public class ContactListFragment extends EaseContactListFragment { private static final String TAG = ContactListFragment.class.getSimpleName(); /*?*/ private ContactSyncListener contactSyncListener; private BlackListSyncListener blackListSyncListener; private ContactInfoSyncListener contactInfoSyncListener; private View loadingView; private ContactItemView applicationItem; private ContactItemView locationItem; private InviteMessgeDao inviteMessgeDao; private BroadcastReceiver broadcastReceiver; private LocalBroadcastManager broadcastManager; @Override protected void initView() { super.initView(); //localdemohelper??? /*registerBroadcastReceiver();*/ EMClient.getInstance().contactManager().setContactListener(new MyContactListener()); /*View view =LayoutInflater.from(getActivity()).inflate(R.layout.ease_fragment_contact_list,null); EaseTitleBar titleBar= (EaseTitleBar) view.findViewById(R.id.title_bar); titleBar.setVisibility(View.GONE);*/ View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.em_contacts_header, null); HeaderItemClickListener clickListener = new HeaderItemClickListener(); applicationItem = (ContactItemView) headerView.findViewById(R.id.application_item); applicationItem.setOnClickListener(clickListener); locationItem = (ContactItemView) headerView.findViewById(R.id.item_location); locationItem.setOnClickListener(clickListener); /*,?,*/ headerView.findViewById(R.id.group_item).setOnClickListener(clickListener); headerView.findViewById(R.id.chat_room_item).setOnClickListener(clickListener); headerView.findViewById(R.id.robot_item).setOnClickListener(clickListener); //headerview listView.addHeaderView(headerView); //???loading view loadingView = LayoutInflater.from(getActivity()).inflate(R.layout.em_layout_loading_data, null); contentContainer.addView(loadingView); //?? registerForContextMenu(listView); } /*??*/ public class MyContactListener implements EMContactListener { @Override public void onContactAdded(String username) { } @Override public void onContactDeleted(final String username) { /*if (ChatActivity.activityInstance != null && ChatActivity.activityInstance.toChatUsername != null && username.equals(ChatActivity.activityInstance.toChatUsername)) { String st10 = getResources().getString(R.string.have_you_removed); Toast.makeText(getActivity(), ChatActivity.activityInstance.getToChatUsername() + st10, Toast.LENGTH_SHORT) .show(); ChatActivity.activityInstance.finish(); }*/ refresh(); } @Override public void onContactInvited(String username, String reason) { } @Override public void onContactAgreed(String username) { } @Override public void onContactRefused(String username) { } } /*????*//* private void registerBroadcastReceiver() { broadcastManager = LocalBroadcastManager.getInstance(getActivity()); 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) { refresh(); String action = intent.getAction(); if (action.equals(Constant.ACTION_GROUP_CHANAGED)) { if (EaseCommonUtils.getTopActivity(getActivity()).equals(GroupsActivity.class.getName())) { GroupsActivity.instance.onResume(); } } } }; broadcastManager.registerReceiver(broadcastReceiver, intentFilter); }*/ @Override public void refresh() { Map<String, EaseUser> m = DemoHelper.getInstance().getContactList(); if (m instanceof Hashtable<?, ?>) { m = (Map<String, EaseUser>) ((Hashtable<String, EaseUser>) m).clone(); } setContactsMap(m); super.refresh(); if (inviteMessgeDao == null) { inviteMessgeDao = new InviteMessgeDao(getActivity()); } if (inviteMessgeDao.getUnreadMessagesCount() > 0) { applicationItem.showUnreadMsgView(); } else { applicationItem.hideUnreadMsgView(); } } @SuppressWarnings("unchecked") @Override protected void setUpView() { titleBar.setRightImageResource(R.drawable.em_add); titleBar.setRightLayoutClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), AddContactActivity.class)); } }); //?? Map<String, EaseUser> m = DemoHelper.getInstance().getContactList(); if (m instanceof Hashtable<?, ?>) { m = (Map<String, EaseUser>) ((Hashtable<String, EaseUser>) m).clone(); } setContactsMap(m); super.setUpView(); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String username = ((EaseUser) listView.getItemAtPosition(position)).getUsername(); // demo?? startActivity(new Intent(getActivity(), ChatActivity.class).putExtra("userId", username)); } }); // ? titleBar.getRightLayout().setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), AddContactActivity.class)); } }); contactSyncListener = new ContactSyncListener(); DemoHelper.getInstance().addSyncContactListener(contactSyncListener); blackListSyncListener = new BlackListSyncListener(); DemoHelper.getInstance().addSyncBlackListListener(blackListSyncListener); contactInfoSyncListener = new ContactInfoSyncListener(); DemoHelper.getInstance().getUserProfileManager().addSyncContactInfoListener(contactInfoSyncListener); if (DemoHelper.getInstance().isContactsSyncedWithServer()) { loadingView.setVisibility(View.GONE); } else if (DemoHelper.getInstance().isSyncingContactsWithServer()) { loadingView.setVisibility(View.VISIBLE); } } @Override public void onDestroy() { super.onDestroy(); if (contactSyncListener != null) { DemoHelper.getInstance().removeSyncContactListener(contactSyncListener); contactSyncListener = null; } if (blackListSyncListener != null) { DemoHelper.getInstance().removeSyncBlackListListener(blackListSyncListener); } if (contactInfoSyncListener != null) { DemoHelper.getInstance().getUserProfileManager().removeSyncContactInfoListener(contactInfoSyncListener); } } protected class HeaderItemClickListener implements OnClickListener { @Override public void onClick(View v) { switch (v.getId()) { case R.id.application_item: // ? startActivity(new Intent(getActivity(), NewFriendsMsgActivity.class)); break; case R.id.group_item: // ?? startActivity(new Intent(getActivity(), GroupsActivity.class)); break; case R.id.chat_room_item: //?? startActivity(new Intent(getActivity(), PublicChatRoomsActivity.class)); break; case R.id.robot_item: //Robot? startActivity(new Intent(getActivity(), RobotsActivity.class)); break; case R.id.item_location: // startActivity(new Intent(getActivity(), NearUserActivity.class)); break; default: break; } } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { Log.e("oncreateContextMemu:", ""); super.onCreateContextMenu(menu, v, menuInfo); toBeProcessUser = (EaseUser) listView.getItemAtPosition(((AdapterContextMenuInfo) menuInfo).position); toBeProcessUsername = toBeProcessUser.getUsername(); getActivity().getMenuInflater().inflate(R.menu.em_context_contact_list, menu); } @Override public boolean onContextItemSelected(MenuItem item) { Log.e("?:", ""); if (item.getItemId() == R.id.delete_contact) { try { // ? deleteContact(toBeProcessUser); // ? InviteMessgeDao dao = new InviteMessgeDao(getActivity()); dao.deleteMessage(toBeProcessUser.getUsername()); } catch (Exception e) { e.printStackTrace(); } return true; } else if (item.getItemId() == R.id.add_to_blacklist) { moveToBlacklist(toBeProcessUsername); return true; } return super.onContextItemSelected(item); } public void deleteContact(final EaseUser tobeDeleteUser) { String st1 = getResources().getString(R.string.deleting); final String st2 = getResources().getString(R.string.Delete_failed); final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setMessage(st1); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { try { EMClient.getInstance().contactManager().deleteContact(tobeDeleteUser.getUsername()); // db? UserDao dao = new UserDao(getActivity()); dao.deleteContact(tobeDeleteUser.getUsername()); DemoHelper.getInstance().getContactList().remove(tobeDeleteUser.getUsername()); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); contactList.remove(tobeDeleteUser); contactListLayout.refresh(); } }); } catch (final Exception e) { getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), st2 + e.getMessage(), Toast.LENGTH_SHORT).show(); } }); } } }).start(); } /*??*/ class ContactSyncListener implements DemoHelper.DataSyncListener { @Override public void onSyncComplete(final boolean success) { EMLog.d(TAG, "on contact list sync success:" + success); getActivity().runOnUiThread(new Runnable() { public void run() { getActivity().runOnUiThread(new Runnable() { @Override public void run() { if (success) { loadingView.setVisibility(View.GONE); refresh(); } else { String s1 = getResources().getString(R.string.get_failed_please_check); Toast.makeText(getActivity(), s1, Toast.LENGTH_SHORT).show(); loadingView.setVisibility(View.GONE); } } }); } }); } } class BlackListSyncListener implements DemoHelper.DataSyncListener { @Override public void onSyncComplete(boolean success) { getActivity().runOnUiThread(new Runnable() { @Override public void run() { refresh(); } }); } } class ContactInfoSyncListener implements DemoHelper.DataSyncListener { @Override public void onSyncComplete(final boolean success) { EMLog.d(TAG, "on contactinfo list sync success:" + success); getActivity().runOnUiThread(new Runnable() { @Override public void run() { loadingView.setVisibility(View.GONE); if (success) { refresh(); } } }); } } }