FileDocCategorySizeDatePackage
ContactListView.javaAPI DocAndroid 1.5 API18895Wed May 06 22:42:46 BST 2009com.android.im.app

ContactListView

public class ContactListView extends android.widget.LinearLayout

Fields Summary
android.app.Activity
mScreen
com.android.im.IImConnection
mConn
SimpleAlertHandler
mHandler
private final com.android.im.IContactListListener
mContactListListener
UserPresenceView
mPresenceView
android.widget.ExpandableListView
mContactsList
private ContactListTreeAdapter
mAdapter
private boolean
mHideOfflineContacts
private SavedState
mSavedState
private boolean
mAutoRefresh
private final ISubscriptionListener.Stub
mSubscriptionListener
private final android.widget.ExpandableListView.OnChildClickListener
mOnChildClickListener
Constructors Summary
public ContactListView(android.content.Context screen, android.util.AttributeSet attrs)


         
        super(screen, attrs);
        mScreen = (Activity)screen;
        mHandler = new SimpleAlertHandler(mScreen);
        mContactListListener = new MyContactListListener(mHandler);
    
Methods Summary
public voidblockContact()

        blockContact(getSelectedContact());
    
voidblockContact(android.database.Cursor c)

        if (c == null) {
            mHandler.showAlert(R.string.error, R.string.select_contact);
        } else {
            String nickname = c.getString(c.getColumnIndexOrThrow(Im.Contacts.NICKNAME));
            final String address = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
            DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int whichButton) {
                    try {
                        IContactListManager manager = mConn.getContactListManager();
                        int res = manager.blockContact(address);
                        if (res != ImErrorInfo.NO_ERROR) {
                            mHandler.showAlert(R.string.error,
                                    ErrorResUtils.getErrorRes(getResources(), res, address));
                        }
                    } catch (RemoteException e) {
                        mHandler.showServiceErrorAlert();
                    }
                }
            };

            Resources r = getResources();

            new AlertDialog.Builder(mContext)
                .setTitle(R.string.confirm)
                .setMessage(r.getString(R.string.confirm_block_contact, nickname))
                .setPositiveButton(R.string.yes, confirmListener) // default button
                .setNegativeButton(R.string.no, null)
                .setCancelable(false)
                .show();
            clearFocusIfEmpty(c);
        }
    
public voidblockContactAtPosition(long packedPosition)

        blockContact(getContactAtPosition(packedPosition));
    
private voidclearFocusIfEmpty(android.database.Cursor c)

        // clear focus if there's only one item so that it would focus on the
        // "empty" item after the contact removed.
        if (c.getCount() == 1) {
            clearFocus();
        }
    
public voidendChat()

        endChat(getSelectedContact());
    
voidendChat(android.database.Cursor c)

        if(c != null) {
            String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
            try {
                IChatSessionManager manager = mConn.getChatSessionManager();
                IChatSession session = manager.getChatSession(username);
                if(session != null) {
                    session.leave();
                }
            } catch (RemoteException e) {
                mHandler.showServiceErrorAlert();
            }
            clearFocusIfEmpty(c);
        }
    
public voidendChatAtPosition(long packedPosition)

        endChat(getContactAtPosition(packedPosition));
    
public android.database.CursorgetContactAtPosition(long packedPosition)

        int type = ExpandableListView.getPackedPositionType(packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
            int childPosition = ExpandableListView.getPackedPositionChild(packedPosition);
            return (Cursor) mAdapter.getChild(groupPosition, childPosition);
        }
        return null;
    
public android.widget.ExpandableListViewgetListView()

        return mContactsList;
    
public android.database.CursorgetSelectedContact()

        long pos = mContactsList.getSelectedPosition();
        if (ExpandableListView.getPackedPositionType(pos)
                == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            return (Cursor)mContactsList.getSelectedItem();
        }
        return null;
    
public java.lang.StringgetSelectedContactList()

        long pos = mContactsList.getSelectedPosition();
        int groupPos = ExpandableListView.getPackedPositionGroup(pos);
        if (groupPos == -1) {
            return null;
        }

        Cursor cursor = (Cursor)mAdapter.getGroup(groupPos);
        if (cursor == null) {
            return null;
        }
        return cursor.getString(cursor.getColumnIndexOrThrow(Im.ContactList.NAME));
    
public booleanisContactAtPosition(long packedPosition)

        int type = ExpandableListView.getPackedPositionType(packedPosition);
        int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
        return (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
                && !mAdapter.isPosForSubscription(groupPosition);
    
public booleanisContactSelected()

        long pos = mContactsList.getSelectedPosition();
        return isContactAtPosition(pos);
    
public booleanisContactsLoaded()

        try {
            IContactListManager manager = mConn.getContactListManager();
            return (manager.getState() == ContactListManager.LISTS_LOADED);
        } catch (RemoteException e) {
            mHandler.showServiceErrorAlert();
            return false;
        }
    
public booleanisConversationAtPosition(long packedPosition)

        int type = ExpandableListView.getPackedPositionType(packedPosition);
        int groupPosition = ExpandableListView.getPackedPositionGroup(packedPosition);
        return (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD)
                && mAdapter.isPosForOngoingConversation(groupPosition);
    
public booleanisConversationSelected()

        long pos = mContactsList.getSelectedPosition();
        return isConversationAtPosition(pos);
    
protected voidonFinishInflate()


    
       
        super.onFinishInflate();
        mPresenceView = (UserPresenceView)findViewById(R.id.userPresence);
        mContactsList = (ExpandableListView) findViewById(R.id.contactsList);
        mContactsList.setOnChildClickListener(mOnChildClickListener);
    
protected voidonLayout(boolean changed, int l, int t, int r, int b)

        if (mAutoRefresh) {
            super.onLayout(changed, l, t, r, b);
        }
    
public voidonRestoreInstanceState(android.os.Parcelable state)

        SavedState ss = (SavedState) state;

        super.onRestoreInstanceState(ss.getSuperState());

        mSavedState = ss;
    
public android.os.ParcelableonSaveInstanceState()

    

    
       
        Parcelable superState = super.onSaveInstanceState();
        int[] expandedGroups = mAdapter == null ? null
                : mAdapter.getExpandedGroups();
        return new SavedState(superState, expandedGroups);
    
private voidregisterListeners()

        try{
            IContactListManager listManager = mConn.getContactListManager();
            listManager.registerContactListListener(mContactListListener);
            listManager.registerSubscriptionListener(mSubscriptionListener);
        }catch(RemoteException e) {
            mHandler.showServiceErrorAlert();
        }
    
public voidremoveContact()

        removeContact(getSelectedContact());
    
voidremoveContact(android.database.Cursor c)

        if (c == null) {
            mHandler.showAlert(R.string.error, R.string.select_contact);
        } else {
            String nickname = c.getString(c.getColumnIndexOrThrow(Im.Contacts.NICKNAME));
            final String address = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
            DialogInterface.OnClickListener confirmListener = new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int whichButton) {
                    try {
                        IContactListManager manager = mConn.getContactListManager();
                        int res = manager.removeContact(address);
                        if (res != ImErrorInfo.NO_ERROR) {
                            mHandler.showAlert(R.string.error,
                                    ErrorResUtils.getErrorRes(getResources(), res, address));
                        }
                    } catch (RemoteException e) {
                        mHandler.showServiceErrorAlert();
                    }
                }
            };
            Resources r = getResources();

            new AlertDialog.Builder(mContext)
                .setTitle(R.string.confirm)
                .setMessage(r.getString(R.string.confirm_delete_contact, nickname))
                .setPositiveButton(R.string.yes, confirmListener) // default button
                .setNegativeButton(R.string.no, null)
                .setCancelable(false)
                .show();

            clearFocusIfEmpty(c);
        }
    
public voidremoveContactAtPosition(long packedPosition)

        removeContact(getContactAtPosition(packedPosition));
    
protected voidsetAutoRefreshContacts(boolean isRefresh)

        mAutoRefresh = isRefresh;
    
public voidsetConnection(com.android.im.IImConnection conn)

        if (mConn != conn) {
            if (mConn != null) {
                unregisterListeners();
            }
            mConn = conn;

            if (conn != null) {
                registerListeners();
                mPresenceView.setConnection(conn);

                if (mAdapter == null) {
                    mAdapter = new ContactListTreeAdapter(conn, mScreen);
                    mAdapter.setHideOfflineContacts(mHideOfflineContacts);
                    mContactsList.setAdapter(mAdapter);
                    mContactsList.setOnScrollListener(mAdapter);
                    if (mSavedState != null) {
                        int[] expandedGroups = mSavedState.mExpandedGroups;
                        if(expandedGroups != null) {
                            for (int group : expandedGroups) {
                                mContactsList.expandGroup(group);
                            }
                        }
                    }
                } else {
                    mAdapter.changeConnection(conn);
                }
                try {
                    IContactListManager listMgr = conn.getContactListManager();
                    if (listMgr.getState() == ContactListManager.LISTS_LOADED) {
                        mAdapter.startAutoRequery();
                    }
                } catch (RemoteException e) {
                    Log.e(ImApp.LOG_TAG, "Service died!");
                }
            }
        } else {
            mContactsList.invalidateViews();
        }
    
public voidsetHideOfflineContacts(boolean hide)

        if (mAdapter != null) {
            mAdapter.setHideOfflineContacts(hide);
        } else {
            mHideOfflineContacts = hide;
        }
    
public voidstartChat()

        startChat(getSelectedContact());
    
voidstartChat(android.database.Cursor c)

        if (c != null) {
            long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID));
            String username = c.getString(c.getColumnIndexOrThrow(Im.Contacts.USERNAME));
            try {
                IChatSessionManager manager = mConn.getChatSessionManager();
                IChatSession session = manager.getChatSession(username);
                if(session == null) {
                    manager.createChatSession(username);
                }

                Uri data = ContentUris.withAppendedId(Im.Chats.CONTENT_URI, id);
                Intent i = new Intent(Intent.ACTION_VIEW, data);
                i.addCategory(ImApp.IMPS_CATEGORY);
                mScreen.startActivity(i);
                setAutoRefreshContacts(false);
            } catch (RemoteException e) {
                mHandler.showServiceErrorAlert();
            }
            clearFocusIfEmpty(c);
        }
    
public voidstartChatAtPosition(long packedPosition)

        startChat(getContactAtPosition(packedPosition));
    
private voidunregisterListeners()

        try{
            IContactListManager listManager = mConn.getContactListManager();
            listManager.unregisterContactListListener(mContactListListener);
            listManager.unregisterSubscriptionListener(mSubscriptionListener);
        }catch(RemoteException e) {
            mHandler.showServiceErrorAlert();
        }
    
public voidviewContactPresence()

        viewContactPresence(getSelectedContact());
    
public voidviewContactPresence(android.database.Cursor c)

        if (c != null) {
            long id = c.getLong(c.getColumnIndexOrThrow(Im.Contacts._ID));
            Uri data = ContentUris.withAppendedId(Im.Contacts.CONTENT_URI, id);
            Intent i = new Intent(Intent.ACTION_VIEW, data);
            mScreen.startActivity(i);
        }
    
public voidviewContactPresenceAtPostion(long packedPosition)

        viewContactPresence(getContactAtPosition(packedPosition));