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.hyphenate.easeui.ui; import android.app.ProgressDialog; import android.os.Bundle; import android.os.Handler; import android.support.v4.widget.SwipeRefreshLayout; import android.text.Editable; import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Toast; import com.hyphenate.EMConnectionListener; import com.hyphenate.EMError; import com.hyphenate.chat.EMClient; import com.hyphenate.chat.EMMessage; import com.hyphenate.easeui.EaseConstant; import com.hyphenate.easeui.R; import com.hyphenate.easeui.bean.ContactBean; import com.hyphenate.easeui.bean.EaseInitBean; import com.hyphenate.easeui.domain.EaseUser; import com.hyphenate.easeui.utils.EaseCommonUtils; import com.hyphenate.easeui.widget.EaseContactList; import com.hyphenate.exceptions.HyphenateException; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * contact list * */ public class EaseContactListFragment extends EaseBaseFragment { private static final String TAG = "EaseContactListFragment"; protected List<EaseUser> contactList; protected ListView listView; protected boolean hidden; protected ImageButton clearSearch; protected EditText query; protected Handler handler = new Handler(); protected EaseUser toBeProcessUser; protected String toBeProcessUsername; protected EaseContactList contactListLayout; protected boolean isConflict; protected FrameLayout contentContainer; private Map<String, EaseUser> contactsMap; protected SwipeRefreshLayout swipeRefreshLayout; protected Map<String, ContactBean> userInfo = new HashMap<>(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.ease_fragment_contact_list, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { //to avoid crash when open app after long time stay in background after user logged into another device if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return; super.onActivityCreated(savedInstanceState); } @Override protected void initView() { contentContainer = (FrameLayout) getView().findViewById(R.id.content_container); contactListLayout = (EaseContactList) getView().findViewById(R.id.contact_list); listView = contactListLayout.getListView(); swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.contact_swipe_layout); swipeRefreshLayout.setColorSchemeResources(R.color.holo_blue_bright, R.color.holo_green_light, R.color.holo_orange_light, R.color.holo_red_light); //search query = (EditText) getView().findViewById(R.id.query); clearSearch = (ImageButton) getView().findViewById(R.id.search_clear); } @Override protected void setUpView() { EMClient.getInstance().addConnectionListener(connectionListener); contactList = new ArrayList<EaseUser>(); getContactList(); //init list contactListLayout.init(contactList, EaseInitBean.map); if (listItemClickListener != null) { listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { EaseUser user = (EaseUser) listView.getItemAtPosition(position); listItemClickListener.onListItemClicked(user); } }); } query.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { contactListLayout.filter(s); if (s.length() > 0) { clearSearch.setVisibility(View.VISIBLE); } else { clearSearch.setVisibility(View.INVISIBLE); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); clearSearch.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { query.getText().clear(); hideSoftKeyboard(); } }); listView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { hideSoftKeyboard(); return false; } }); } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); this.hidden = hidden; if (!hidden) { refresh(); } } @Override public void onResume() { super.onResume(); if (!hidden) { refresh(); } } /** * move user to blacklist */ protected void moveToBlacklist(final String username) { final ProgressDialog pd = new ProgressDialog(getActivity()); String st1 = getResources().getString(R.string.Is_moved_into_blacklist); final String st2 = getResources().getString(R.string.Move_into_blacklist_success); final String st3 = getResources().getString(R.string.Move_into_blacklist_failure); pd.setMessage(st1); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { try { //move to blacklist EMClient.getInstance().contactManager().addUserToBlackList(username, false); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), st2, Toast.LENGTH_SHORT).show(); refresh(); } }); } catch (HyphenateException e) { e.printStackTrace(); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), st3, Toast.LENGTH_SHORT).show(); } }); } } }).start(); } // refresh ui public void refresh() { //getContactList(); setUpView(); contactListLayout.refresh(); } @Override public void onDestroy() { EMClient.getInstance().removeConnectionListener(connectionListener); super.onDestroy(); } /** * get contact list and sort, will filter out users in blacklist */ protected void getContactList() { contactList.clear(); if (contactsMap == null) { return; } synchronized (this.contactsMap) { Iterator<Entry<String, EaseUser>> iterator = contactsMap.entrySet().iterator(); List<String> blackList = EMClient.getInstance().contactManager().getBlackListUsernames(); while (iterator.hasNext()) { Entry<String, EaseUser> entry = iterator.next(); // to make it compatible with data in previous version, you can remove this check if this is new app if (!entry.getKey().equals("item_new_friends") && !entry.getKey().equals("item_groups") && !entry.getKey().equals("item_chatroom") && !entry.getKey().equals("item_robots")) { if (!blackList.contains(entry.getKey())) { //filter out users in blacklist EaseUser user = entry.getValue(); EaseCommonUtils.setUserInitialLetter(user); contactList.add(user); } } } } // sorting Collections.sort(contactList, new Comparator<EaseUser>() { @Override public int compare(EaseUser lhs, EaseUser rhs) { if (lhs.getInitialLetter().equals(rhs.getInitialLetter())) { return lhs.getNick().compareTo(rhs.getNick()); } else { if ("#".equals(lhs.getInitialLetter())) { return 1; } else if ("#".equals(rhs.getInitialLetter())) { return -1; } return lhs.getInitialLetter().compareTo(rhs.getInitialLetter()); } } }); } protected EMConnectionListener connectionListener = new EMConnectionListener() { @Override public void onDisconnected(int error) { if (error == EMError.USER_REMOVED || error == EMError.USER_LOGIN_ANOTHER_DEVICE || error == EMError.SERVER_SERVICE_RESTRICTED) { isConflict = true; } else { getActivity().runOnUiThread(new Runnable() { public void run() { onConnectionDisconnected(); } }); } } @Override public void onConnected() { if (getActivity() != null) { getActivity().runOnUiThread(new Runnable() { public void run() { onConnectionConnected(); } }); } } }; private EaseContactListItemClickListener listItemClickListener; protected void onConnectionDisconnected() { } protected void onConnectionConnected() { } /** * set contacts map, key is the hyphenate id * @param contactsMap */ public void setContactsMap(Map<String, EaseUser> contactsMap) { this.contactsMap = contactsMap; } public interface EaseContactListItemClickListener { /** * on click event for item in contact list * @param user --the user of item */ void onListItemClicked(EaseUser user); } /** * set contact list item click listener * @param listItemClickListener */ public void setContactListItemClickListener(EaseContactListItemClickListener listItemClickListener) { this.listItemClickListener = listItemClickListener; } }