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.grass.caishi.cc.frag; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.inputmethod.InputMethodManager; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageView; import android.widget.ListView; import android.widget.Toast; import com.easemob.chat.EMContactManager; import com.easemob.exceptions.EaseMobException; import com.grass.caishi.cc.Constant; import com.grass.caishi.cc.MyApplication; import com.grass.caishi.cc.R; import com.grass.caishi.cc.activity.AddContactActivity; import com.grass.caishi.cc.activity.ChatActivity; import com.grass.caishi.cc.activity.GroupsActivity; import com.grass.caishi.cc.activity.MainActivity; import com.grass.caishi.cc.activity.NewFriendsMsgActivity; import com.grass.caishi.cc.activity.Userinfo; import com.grass.caishi.cc.adapter.ContactAdapter; import com.grass.caishi.cc.db.InviteMessgeDao; import com.grass.caishi.cc.db.UserDao; import com.grass.caishi.cc.domain.User; import com.grass.caishi.cc.utils.CommonUtils; import com.grass.caishi.cc.widget.Sidebar; /** * ? * */ public class FriendsListFragment extends Fragment { private ContactAdapter adapter; private List<User> contactList; private ListView listView; private boolean hidden; private Sidebar sidebar; private InputMethodManager inputMethodManager; private boolean isInitDone = false; private SwipeRefreshLayout swipeRefreshLayout; // private EditText query; // private ImageButton clearSearch; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_conversation_friends, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return; inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); listView = (ListView) getView().findViewById(R.id.list); // listView.setFootRefresh(false); // listView.setPullLoadEnable(true); swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipeLayout); // swipeRefreshLayout.setColorSchemeResources(R.color.swipe_color_1, // R.color.swipe_color_2, R.color.swipe_color_3, R.color.swipe_color_4); // swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE); // swipeRefreshLayout.setProgressBackgroundColor(R.color.swipe_background_color); swipeRefreshLayout.setProgressViewEndTarget(true, 100); swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { ((MainActivity) getActivity()).refreshFriends(); } }); sidebar = (Sidebar) getView().findViewById(R.id.sidebar); sidebar.setListView(listView); contactList = new ArrayList<User>(); // ?contactlist getContactList(); // adapter adapter = new ContactAdapter(getActivity(), R.layout.row_contact, contactList); listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String username = adapter.getItem(position).getUsername(); if (Constant.NEW_FRIENDS_USERNAME.equals(username)) { // ? User user = MyApplication.getInstance().getContactList(false) .get(Constant.NEW_FRIENDS_USERNAME); user.setUnreadMsgCount(0); startActivity(new Intent(getActivity(), NewFriendsMsgActivity.class)); } else if (Constant.GROUP_USERNAME.equals(username)) { // ?? startActivity(new Intent(getActivity(), GroupsActivity.class)); } else if (Constant.KEFU.equals(username)) { // ?? startActivity(new Intent(getActivity(), ChatActivity.class).putExtra("userId", Constant.KEFU) .putExtra("chatType", 0)); } else { startActivity(new Intent(getActivity(), Userinfo.class).putExtra("userId", adapter.getItem(position).getUsername())); } } }); listView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { // ?? if (getActivity().getWindow() .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) { if (getActivity().getCurrentFocus() != null) inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } return false; } }); ImageView addContactView = (ImageView) getView().findViewById(R.id.iv_new_contact); // ? addContactView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), AddContactActivity.class)); } }); registerForContextMenu(listView); isInitDone = true; refresh(); // // ? // query = (EditText) getView().findViewById(R.id.query); // // ?button // clearSearch = (ImageButton) getView().findViewById(R.id.search_clear); // query.addTextChangedListener(new TextWatcher() { // public void onTextChanged(CharSequence s, int start, int before, int count) { // adapter.getFilter().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(); // } // }); } public void setRefreshDone() { if (swipeRefreshLayout.isRefreshing()) { try { swipeRefreshLayout.setRefreshing(false); } catch (Exception e) { // TODO: handle exception } } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // ??menu if (((AdapterContextMenuInfo) menuInfo).position > 2) { // if (((AdapterContextMenuInfo) menuInfo).position > 1) // {//?,?? getActivity().getMenuInflater().inflate(R.menu.context_contact_list, menu); } } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getItemId() == R.id.delete_contact) { User tobeDeleteUser = adapter.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position); // ? deleteContact(tobeDeleteUser); // ? InviteMessgeDao dao = new InviteMessgeDao(getActivity()); dao.deleteMessage(tobeDeleteUser.getUsername()); return true; } else if (item.getItemId() == R.id.add_to_blacklist) { User user = adapter.getItem(((AdapterContextMenuInfo) item.getMenuInfo()).position); moveToBlacklist(user.getUsername()); return true; } return super.onContextItemSelected(item); } @Override public void onHiddenChanged(boolean hidden) { super.onHiddenChanged(hidden); this.hidden = hidden; if (!hidden) { refresh(); } } /** * ? * * @param toDeleteUser */ public void deleteContact(final User tobeDeleteUser) { final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setMessage("..."); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { try { EMContactManager.getInstance().deleteContact(tobeDeleteUser.getUsername()); // db? UserDao dao = new UserDao(getActivity()); dao.deleteContact(tobeDeleteUser.getUsername()); MyApplication.getInstance().getContactList(false).remove(tobeDeleteUser.getUsername()); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); adapter.remove(tobeDeleteUser); adapter.notifyDataSetChanged(); } }); } catch (final Exception e) { getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), ": " + e.getMessage(), 1).show(); } }); } } }).start(); } /** * user??? */ private void moveToBlacklist(final String username) { final ProgressDialog pd = new ProgressDialog(getActivity()); pd.setMessage("???..."); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { try { // ??? EMContactManager.getInstance().addUserToBlackList(username, true); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), "????", 0).show(); } }); } catch (EaseMobException e) { e.printStackTrace(); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), "???", 0).show(); } }); } } }).start(); } // ui public void refresh() { if (!isInitDone) { return; } try { // ?? getActivity().runOnUiThread(new Runnable() { public void run() { getContactList(); adapter.notifyDataSetChanged(); } }); } catch (Exception e) { e.printStackTrace(); } } @Override public void onResume() { super.onResume(); if (!hidden) { refresh(); } } private void getContactList() { Map<String, User> users = MyApplication.getInstance().getContactList(false); if (CommonUtils.isNullOrEmpty(users)) { Log.d("users", "-------------------users is no------"); } else { contactList.clear(); Iterator<Entry<String, User>> iterator = users.entrySet().iterator(); while (iterator.hasNext()) { Entry<String, User> entry = iterator.next(); if (!entry.getKey().equals(Constant.KEFU) && !entry.getKey().equals(Constant.NEW_FRIENDS_USERNAME) && !entry.getKey().equals(Constant.GROUP_USERNAME) && !CommonUtils.isNullOrEmpty(entry.getKey())) { contactList.add(entry.getValue()); } } // ? Collections.sort(contactList, new Comparator<User>() { @Override public int compare(User lhs, User rhs) { return lhs.getHeader().compareTo(rhs.getHeader());// abc } }); // User kefu = new User(); // kefu.setUsername("kefu"); // kefu.setNick("??"); // kefu.setLogo(""); // kefu.setHeader(""); // contactList.add(0, kefu); // User newFriends = new User(); // newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME); // newFriends.setNick(""); // newFriends.setHeader(""); // contactList.add(0, newFriends); // ?? // """?" contactList.add(0, users.get(Constant.KEFU)); // ""? contactList.add(0, users.get(Constant.NEW_FRIENDS_USERNAME)); } } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (((MainActivity) getActivity()).isConflict) { outState.putBoolean("isConflict", true); } else if (((MainActivity) getActivity()).getCurrentAccountRemoved()) { outState.putBoolean(Constant.ACCOUNT_REMOVED, true); } } }