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.imalu.alyou.activity; import java.util.List; import org.apache.http.Header; import org.json.JSONArray; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; 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.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.WindowManager; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.imalu.alyou.AlUApplication; import com.imalu.alyou.Constant; import com.imalu.alyou.R; import com.imalu.alyou.adapter.FriendAdapter; import com.imalu.alyou.domain.Friend; import com.imalu.alyou.domain.HXUser; import com.imalu.alyou.net.JsonHttpResponseHandler; import com.imalu.alyou.net.NetManager; import com.imalu.alyou.net.request.SearchFriendRequest; import com.imalu.alyou.net.response.FriendInfo; import com.imalu.alyou.net.response.FriendListResponse; import com.imalu.alyou.widget.Sidebar; //import com.easemob.chat.EMContactManager; /** * -----? * */ public class FriendlistFragment extends Fragment { private FriendAdapter adapter; private List<HXUser> contactList; private ListView listView; // FriendListResponse friendlist; private boolean hidden; private Sidebar sidebar; private InputMethodManager inputMethodManager; private List<String> blackList; private Button bt; private TextView add_friend; protected static final String TAG = "MainActivity"; // ?textview private TextView unreadLabel; // textview private TextView unreadAddressLable; private Button[] mTabs; private FriendlistFragment contactListFragment; //private ChatHistoryFragment chatHistoryFragment; private ChatAllHistoryFragment chatHistoryFragment; private SettingsFragment settingFragment; private MainPageFragment mainpageFragment; private RelativeLayout relativeLayout; private RelativeLayout groups; private Fragment[] fragments; private int index; private RelativeLayout[] tab_containers; // ?fragmentindex private int currentTabIndex; //private NewMessageBroadcastReceiver msgReceiver; // ? public boolean isConflict = false; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.contact_friend_list, container, false); } @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); //T??home???appcrash if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) return; inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); listView = (ListView) getView().findViewById(R.id.list); sidebar = (Sidebar) getView().findViewById(R.id.sidebar); relativeLayout = (RelativeLayout) getView().findViewById(R.id.add_friend_layout); groups = (RelativeLayout) getView().findViewById(R.id.groups); sidebar.setListView(listView); sidebar.setVisibility(ViewGroup.GONE); relativeLayout.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(getActivity(), FindFriendActivity.class)); } }); groups.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(getActivity(), GroupsActivity.class)); } }); showListView(); // //??? // //blackList = EMContactManager.getInstance().getBlackListUsernames(); // contactList = new ArrayList<HXUser>(); // // ?contactlist // getContactList(); // // adapter // adapter = new ContactAdapter(getActivity(), R.layout.row_contact, contactList, sidebar); // 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)) { // // ? // HXUser user = AlUApplication.getInstance().getContactList().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 { // // demo?? // startActivity(new Intent(getActivity(), ChatActivity.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); // setView(); // setLisetener(); } private void showListView() { adapter = new FriendAdapter(getActivity(), R.layout.row_contact, R.layout.row_contact2, AlUApplication.getFriends().getFriendList(), sidebar); 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)) { // ? HXUser user = AlUApplication.getInstance().getContactList().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 { // demo?? startActivity(new Intent(getActivity(), ChatActivity.class).putExtra("userId", adapter.getItem(position).getHxname())); } } }); 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; } }); // TextView addContactView = (TextView) getView().findViewById(R.id.add_friend); // // ? addContactView.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), AddContactActivity.class)); } }); registerForContextMenu(listView); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); // ??menu if (((AdapterContextMenuInfo) menuInfo).position > 2) { getActivity().getMenuInflater().inflate(R.menu.context_contact_list, menu); } } @Override public boolean onContextItemSelected(MenuItem item) { if (item.getItemId() == R.id.delete_contact) { Friend 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) { Friend 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(); } } @Override public void onResume() { super.onResume(); if (!hidden) { //refresh(); } } /** * ? * * @param toDeleteUser */ public void deleteContact(final HXUser 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()); // AlUApplication.getInstance().getContactList().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,false); getActivity().runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getActivity(), "????", 0).show(); //refresh(); } }); //} // catch (EaseMobException e) { // e.printStackTrace(); // getActivity().runOnUiThread(new Runnable() { // public void run() { // pd.dismiss(); // Toast.makeText(getActivity(), "???", 0).show(); // } // }); // } } }).start(); } // ui public void refresh() { try { // ?? getActivity().runOnUiThread(new Runnable() { public void run() { getContactList(); adapter.notifyDataSetChanged(); } }); } catch (Exception e) { e.printStackTrace(); } } /** * ?????? */ private void getContactList() { SearchFriendRequest searchFriendReq = new SearchFriendRequest(); searchFriendReq.setUserKey(AlUApplication.getMyInfo().getKey()); // friendlist = new FriendListResponse(); // // NetManager.execute(NetManager.MY_FRIEND_OPERATION, searchFriendReq, new JsonHttpResponseHandler() { // @Override // public void onSuccess(int statusCode, Header[] headers, JSONArray response) { // Log.e("FriendListFragament", response.toString()); // super.onSuccess(statusCode, headers, response); // friendlist.setJsonObject(response); // AlUApplication.setFriends(friendlist); // showListView(); // } // }); // contactList.clear(); //?? // Map<String, HXUser> users = AlUApplication.getInstance().getContactList(); // Iterator<Entry<String, HXUser>> iterator = users.entrySet().iterator(); // while (iterator.hasNext()) { // Entry<String, HXUser> entry = iterator.next(); // if (!entry.getKey().equals(Constant.NEW_FRIENDS_USERNAME) && !entry.getKey().equals(Constant.GROUP_USERNAME) // && !blackList.contains(entry.getKey())) // contactList.add(entry.getValue()); // } // ? // Collections.sort(contactList, new Comparator<HXUser>() { // // @Override // public int compare(HXUser lhs, HXUser rhs) { // return lhs.getUsername().compareTo(rhs.getUsername()); // } // }); // """?" // contactList.add(0, users.get(Constant.GROUP_USERNAME)); // ""? // contactList.add(0, users.get(Constant.NEW_FRIENDS_USERNAME)); } @Override public void onSaveInstanceState(Bundle outState) { if (((MainActivity) getActivity()).isConflict) outState.putBoolean("isConflict", true); super.onSaveInstanceState(outState); } }