ContactListView.java
上传用户:szyujian
上传日期:2016-09-20
资源大小:320k
文件大小:18k
源码类别:

android开发

开发平台:

C/C++

  1. /*
  2.  * Copyright (C) 2007-2008 Esmertec AG.
  3.  * Copyright (C) 2007-2008 The Android Open Source Project
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package com.android.im.app;
  18. import com.android.im.IChatSession;
  19. import com.android.im.IChatSessionManager;
  20. import com.android.im.IContactListListener;
  21. import com.android.im.IContactListManager;
  22. import com.android.im.IImConnection;
  23. import com.android.im.ISubscriptionListener;
  24. import com.android.im.R;
  25. import com.android.im.app.adapter.ContactListListenerAdapter;
  26. import com.android.im.engine.Contact;
  27. import com.android.im.engine.ContactListManager;
  28. import com.android.im.engine.ImErrorInfo;
  29. import com.android.im.service.ImServiceConstants;
  30. import android.app.Activity;
  31. import android.app.AlertDialog;
  32. import android.content.ContentUris;
  33. import android.content.Context;
  34. import android.content.DialogInterface;
  35. import android.content.Intent;
  36. import android.content.res.Resources;
  37. import android.database.Cursor;
  38. import android.net.Uri;
  39. import android.os.Parcel;
  40. import android.os.Parcelable;
  41. import android.os.RemoteException;
  42. import android.provider.Im;
  43. import android.util.AttributeSet;
  44. import android.util.Log;
  45. import android.view.View;
  46. import android.widget.ExpandableListView;
  47. import android.widget.LinearLayout;
  48. import android.widget.ExpandableListView.OnChildClickListener;
  49. public class ContactListView extends LinearLayout {
  50.     Activity mScreen;
  51.     IImConnection mConn;
  52.     SimpleAlertHandler mHandler;
  53.     private final IContactListListener mContactListListener;
  54.     UserPresenceView mPresenceView;
  55.     ExpandableListView mContactsList;
  56.     private ContactListTreeAdapter mAdapter;
  57.     private boolean mHideOfflineContacts;
  58.     private SavedState mSavedState;
  59.     public ContactListView(Context screen, AttributeSet attrs) {
  60.         super(screen, attrs);
  61.         mScreen = (Activity)screen;
  62.         mHandler = new SimpleAlertHandler(mScreen);
  63.         mContactListListener = new MyContactListListener(mHandler);
  64.     }
  65.     private class MyContactListListener extends ContactListListenerAdapter {
  66.         public MyContactListListener(SimpleAlertHandler handler) {
  67.             super(handler);
  68.         }
  69.         @Override
  70.         public void onAllContactListsLoaded() {
  71.             if (mAdapter != null) {
  72.                 mAdapter.startAutoRequery();
  73.             }
  74.         }
  75.     }
  76.     private final ISubscriptionListener.Stub mSubscriptionListener = new ISubscriptionListener.Stub() {
  77.         public void onSubScriptionRequest(Contact from) {
  78.             querySubscription();
  79.         }
  80.         public void onSubscriptionApproved(String contact) {
  81.             querySubscription();
  82.         }
  83.         public void onSubscriptionDeclined(String contact) {
  84.             querySubscription();
  85.         }
  86.         private void querySubscription() {
  87.             if (mAdapter != null) {
  88.                 mAdapter.startQuerySubscriptions();
  89.             }
  90.         }
  91.      };
  92.     @Override
  93.     protected void onFinishInflate() {
  94.         super.onFinishInflate();
  95.         mPresenceView = (UserPresenceView)findViewById(R.id.userPresence);
  96.         mContactsList = (ExpandableListView) findViewById(R.id.contactsList);
  97.         mContactsList.setOnChildClickListener(mOnChildClickListener);
  98.     }
  99.     public ExpandableListView getListView() {
  100.         return mContactsList;
  101.     }
  102.     public void setConnection(IImConnection conn) {
  103.         if (mConn != conn) {
  104.             if (mConn != null) {
  105.                 unregisterListeners();
  106.             }
  107.             mConn = conn;
  108.             if (conn != null) {
  109.                 registerListeners();
  110.                 mPresenceView.setConnection(conn);
  111.                 if (mAdapter == null) {
  112.                     mAdapter = new ContactListTreeAdapter(conn, mScreen);
  113.                     mAdapter.setHideOfflineContacts(mHideOfflineContacts);
  114.                     mContactsList.setAdapter(mAdapter);
  115.                     mContactsList.setOnScrollListener(mAdapter);
  116.                     if (mSavedState != null) {
  117.                         int[] expandedGroups = mSavedState.mExpandedGroups;
  118.                         if(expandedGroups != null) {
  119.                             for (int group : expandedGroups) {
  120.                                 mContactsList.expandGroup(group);
  121.                             }
  122.                         }
  123.                     }
  124.                 } else {
  125.                     mAdapter.changeConnection(conn);
  126.                 }
  127.                 try {
  128.                     IContactListManager listMgr = conn.getContactListManager();
  129.                     if (listMgr.getState() == ContactListManager.LISTS_LOADED) {
  130.                         mAdapter.startAutoRequery();
  131.                     }
  132.                 } catch (RemoteException e) {
  133.                     Log.e(ImApp.LOG_TAG, "Service died!");
  134.                 }
  135.             }
  136.         } else {
  137.             mContactsList.invalidateViews();
  138.         }
  139.     }
  140.     public void setHideOfflineContacts(boolean hide) {
  141.         if (mAdapter != null) {
  142.             mAdapter.setHideOfflineContacts(hide);
  143.         } else {
  144.             mHideOfflineContacts = hide;
  145.         }
  146.     }
  147.     public void startChat() {
  148.         startChat(getSelectedContact());
  149.     }
  150.     public void startChatAtPosition(long packedPosition) {
  151.         startChat(getContactAtPosition(packedPosition));
  152.     }
  153.     void startChat(Cursor c) {
  154.         if (c != null) {
  155.             long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID));
  156.             String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
  157.             try {
  158.                 IChatSessionManager manager = mConn.getChatSessionManager();
  159.                 IChatSession session = manager.getChatSession(username);
  160.                 if(session == null) {
  161.                     manager.createChatSession(username);
  162.                 }
  163.                 Uri data = ContentUris.withAppendedId(Im.Chats.CONTENT_URI, id);
  164.                 Intent i = new Intent(Intent.ACTION_VIEW, data);
  165.                 mScreen.startActivity(i);
  166.             } catch (RemoteException e) {
  167.                 mHandler.showServiceErrorAlert();
  168.             }
  169.             clearFocusIfEmpty(c);
  170.         }
  171.     }
  172.     private void clearFocusIfEmpty(Cursor c) {
  173.         // clear focus if there's only one item so that it would focus on the
  174.         // "empty" item after the contact removed.
  175.         if (c.getCount() == 1) {
  176.             clearFocus();
  177.         }
  178.     }
  179.     public void endChat() {
  180.         endChat(getSelectedContact());
  181.     }
  182.     public void endChatAtPosition(long packedPosition) {
  183.         endChat(getContactAtPosition(packedPosition));
  184.     }
  185.     void endChat(Cursor c) {
  186.         if(c != null) {
  187.             String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
  188.             try {
  189.                 IChatSessionManager manager = mConn.getChatSessionManager();
  190.                 IChatSession session = manager.getChatSession(username);
  191.                 if(session != null) {
  192.                     session.leave();
  193.                 }
  194.             } catch (RemoteException e) {
  195.                 mHandler.showServiceErrorAlert();
  196.             }
  197.             clearFocusIfEmpty(c);
  198.         }
  199.     }
  200.     public void viewContactPresence() {
  201.         viewContactPresence(getSelectedContact());
  202.     }
  203.     public void viewContactPresenceAtPostion(long packedPosition) {
  204.         viewContactPresence(getContactAtPosition(packedPosition));
  205.     }
  206.     public void viewContactPresence(Cursor c) {
  207.         if (c != null) {
  208.             long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID));
  209.             Uri data = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, id);
  210.             Intent i = new Intent(Intent.ACTION_VIEW, data);
  211.             mScreen.startActivity(i);
  212.         }
  213.     }
  214.     public boolean isContactAtPosition(long packedPosition) {
  215.         int type = ExpandableListView.getPackedPositionType(packedPosition);
  216.         int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
  217.         return (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
  218.                 && !mAdapter.isPosForSubscription(groupPosition);
  219.     }
  220.     public boolean isContactSelected() {
  221.         long pos = mContactsList.getSelectedPosition();
  222.         return isContactAtPosition(pos);
  223.     }
  224.     public boolean isConversationAtPosition(long packedPosition) {
  225.         int type = ExpandableListView.getPackedPositionType(packedPosition);
  226.         int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
  227.         return (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
  228.                 && mAdapter.isPosForOngoingConversation(groupPosition);
  229.     }
  230.     public boolean isConversationSelected () {
  231.         long pos = mContactsList.getSelectedPosition();
  232.         return isConversationAtPosition(pos);
  233.     }
  234.     public boolean isContactsLoaded() {
  235.         try {
  236.             IContactListManager manager = mConn.getContactListManager();
  237.             return (manager.getState() == ContactListManager.LISTS_LOADED);
  238.         } catch (RemoteException e) {
  239.             mHandler.showServiceErrorAlert();
  240.             return false;
  241.         }
  242.     }
  243.     public void removeContact() {
  244.         removeContact(getSelectedContact());
  245.     }
  246.     public void removeContactAtPosition(long packedPosition) {
  247.         removeContact(getContactAtPosition(packedPosition));
  248.     }
  249.     void removeContact(Cursor c) {
  250.         if (c == null) {
  251.             mHandler.showAlert(R.string.error, R.string.select_contact);
  252.         } else {
  253.             String nickname = c.getString(c.getColumnIndexOrThrow(Im.Contacts.NICKNAME));
  254.             final String address = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
  255.             DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){
  256.                 public void onClick(DialogInterface dialog, int whichButton) {
  257.                     try {
  258.                         IContactListManager manager = mConn.getContactListManager();
  259.                         int res = manager.removeContact(address);
  260.                         if (res != ImErrorInfo.NO_ERROR) {
  261.                             mHandler.showAlert(R.string.error,
  262.                                     ErrorResUtils.getErrorRes(getResources(), res, address));
  263.                         }
  264.                     } catch (RemoteException e) {
  265.                         mHandler.showServiceErrorAlert();
  266.                     }
  267.                 }
  268.             };
  269.             Resources r = getResources();
  270.             new AlertDialog.Builder(mContext)
  271.                 .setTitle(R.string.confirm)
  272.                 .setMessage(r.getString(R.string.confirm_delete_contact, nickname))
  273.                 .setPositiveButton(R.string.yes, confirmListener) // default button
  274.                 .setNegativeButton(R.string.no, null)
  275.                 .setCancelable(false)
  276.                 .show();
  277.             clearFocusIfEmpty(c);
  278.         }
  279.     }
  280.     public void blockContact() {
  281.         blockContact(getSelectedContact());
  282.     }
  283.     public void blockContactAtPosition(long packedPosition) {
  284.         blockContact(getContactAtPosition(packedPosition));
  285.     }
  286.     void blockContact(Cursor c) {
  287.         if (c == null) {
  288.             mHandler.showAlert(R.string.error, R.string.select_contact);
  289.         } else {
  290.             String nickname = c.getString(c.getColumnIndexOrThrow(Im.Contacts.NICKNAME));
  291.             final String address = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
  292.             DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){
  293.                 public void onClick(DialogInterface dialog, int whichButton) {
  294.                     try {
  295.                         IContactListManager manager = mConn.getContactListManager();
  296.                         int res = manager.blockContact(address);
  297.                         if (res != ImErrorInfo.NO_ERROR) {
  298.                             mHandler.showAlert(R.string.error,
  299.                                     ErrorResUtils.getErrorRes(getResources(), res, address));
  300.                         }
  301.                     } catch (RemoteException e) {
  302.                         mHandler.showServiceErrorAlert();
  303.                     }
  304.                 }
  305.             };
  306.             Resources r = getResources();
  307.             new AlertDialog.Builder(mContext)
  308.                 .setTitle(R.string.confirm)
  309.                 .setMessage(r.getString(R.string.confirm_block_contact, nickname))
  310.                 .setPositiveButton(R.string.yes, confirmListener) // default button
  311.                 .setNegativeButton(R.string.no, null)
  312.                 .setCancelable(false)
  313.                 .show();
  314.             clearFocusIfEmpty(c);
  315.         }
  316.     }
  317.     public Cursor getContactAtPosition(long packedPosition) {
  318.         int type = ExpandableListView.getPackedPositionType(packedPosition);
  319.         if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
  320.             int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
  321.             int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
  322.             return (Cursor) mAdapter.getChild(groupPosition, childPosition);
  323.         }
  324.         return null;
  325.     }
  326.     public Cursor getSelectedContact() {
  327.         long pos = mContactsList.getSelectedPosition();
  328.         if (ExpandableListView.getPackedPositionType(pos)
  329.                 == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
  330.             return (Cursor)mContactsList.getSelectedItem();
  331.         }
  332.         return null;
  333.     }
  334.     public String getSelectedContactList() {
  335.         long pos = mContactsList.getSelectedPosition();
  336.         int groupPos = ExpandableListView.getPackedPositionGroup(pos);
  337.         if (groupPos == -1) {
  338.             return null;
  339.         }
  340.         Cursor cursor = (Cursor)mAdapter.getGroup(groupPos);
  341.         if (cursor == null) {
  342.             return null;
  343.         }
  344.         return cursor.getString(cursor.getColumnIndexOrThrow(Im.ContactList.NAME));
  345.     }
  346.     private void registerListeners() {
  347.         try{
  348.             IContactListManager listManager = mConn.getContactListManager();
  349.             listManager.registerContactListListener(mContactListListener);
  350.             listManager.registerSubscriptionListener(mSubscriptionListener);
  351.         }catch(RemoteException e) {
  352.             mHandler.showServiceErrorAlert();
  353.         }
  354.     }
  355.     private void unregisterListeners() {
  356.         try{
  357.             IContactListManager listManager = mConn.getContactListManager();
  358.             listManager.unregisterContactListListener(mContactListListener);
  359.             listManager.unregisterSubscriptionListener(mSubscriptionListener);
  360.         }catch(RemoteException e) {
  361.             mHandler.showServiceErrorAlert();
  362.         }
  363.     }
  364.     private final OnChildClickListener mOnChildClickListener = new OnChildClickListener() {
  365.         public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
  366.                 int childPosition, long id) {
  367.             Cursor cursor = (Cursor)parent.getExpandableListAdapter().getChild(
  368.                     groupPosition, childPosition);
  369.             int subscriptionType = cursor.getInt(ContactView.COLUMN_SUBSCRIPTION_TYPE);
  370.             int subscriptionStatus = cursor.getInt(ContactView.COLUMN_SUBSCRIPTION_STATUS);
  371.             if ((subscriptionType == Im.Contacts.SUBSCRIPTION_TYPE_FROM)
  372.                     && (subscriptionStatus == Im.Contacts.SUBSCRIPTION_STATUS_SUBSCRIBE_PENDING)){
  373.                 long providerId = cursor.getLong(ContactView.COLUMN_CONTACT_PROVIDER);
  374.                 String username = cursor.getString(ContactView.COLUMN_CONTACT_USERNAME);
  375.                 Intent intent = new Intent(ImServiceConstants.ACTION_MANAGE_SUBSCRIPTION,
  376.                         ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, id));
  377.                 intent.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, providerId);
  378.                 intent.putExtra(ImServiceConstants.EXTRA_INTENT_FROM_ADDRESS, username);
  379.                 mScreen.startActivity(intent);
  380.             } else {
  381.                 startChat(cursor);
  382.             }
  383.             return true;
  384.         }
  385.     };
  386.     static class SavedState extends BaseSavedState {
  387.         int[] mExpandedGroups;
  388.         SavedState(Parcelable superState, int[] expandedGroups) {
  389.             super(superState);
  390.             mExpandedGroups = expandedGroups;
  391.         }
  392.         private SavedState(Parcel in) {
  393.             super(in);
  394.             mExpandedGroups = in.createIntArray();
  395.         }
  396.         @Override
  397.         public void writeToParcel(Parcel out, int flags) {
  398.             super.writeToParcel(out, flags);
  399.             out.writeIntArray(mExpandedGroups);
  400.         }
  401.         public static final Parcelable.Creator<SavedState> CREATOR
  402.                 = new Parcelable.Creator<SavedState>() {
  403.             public SavedState createFromParcel(Parcel in) {
  404.                 return new SavedState(in);
  405.             }
  406.             public SavedState[] newArray(int size) {
  407.                 return new SavedState[size];
  408.             }
  409.         };
  410.     }
  411.     @Override
  412.     public Parcelable onSaveInstanceState() {
  413.         Parcelable superState = super.onSaveInstanceState();
  414.         int[] expandedGroups = mAdapter == null ? null
  415.                 : mAdapter.getExpandedGroups();
  416.         return new SavedState(superState, expandedGroups);
  417.     }
  418.     @Override
  419.     public void onRestoreInstanceState(Parcelable state) {
  420.         SavedState ss = (SavedState) state;
  421.         super.onRestoreInstanceState(ss.getSuperState());
  422.         mSavedState = ss;
  423.     }
  424. }