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

ContactListActivity

public class ContactListActivity extends android.app.Activity implements View.OnCreateContextMenuListener

Fields Summary
private static final int
MENU_START_CONVERSATION
private static final int
MENU_VIEW_PROFILE
private static final int
MENU_BLOCK_CONTACT
private static final int
MENU_DELETE_CONTACT
private static final int
MENU_END_CONVERSATION
private static final String
FILTER_STATE_KEY
ImApp
mApp
long
mProviderId
long
mAccountId
com.android.im.IImConnection
mConn
ContactListView
mContactListView
ContactListFilterView
mFilterView
SimpleAlertHandler
mHandler
ContextMenuHandler
mContextMenuHandler
boolean
mIsFiltering
Im.ProviderSettings.QueryMap
mSettingMap
boolean
mDestroyed
Constructors Summary
Methods Summary
voidclearConnectionStatus()

        ContentResolver cr = getContentResolver();
        ContentValues values = new ContentValues(3);

        values.put(Im.AccountStatus.ACCOUNT, mAccountId);
        values.put(Im.AccountStatus.PRESENCE_STATUS, Im.Presence.OFFLINE);
        values.put(Im.AccountStatus.CONNECTION_STATUS, Im.ConnectionStatus.OFFLINE);
        // insert on the "account_status" uri actually replaces the existing value 
        cr.insert(Im.AccountStatus.CONTENT_URI, values);
    
public booleandispatchKeyEvent(android.view.KeyEvent event)

        int keyCode = event.getKeyCode();

        boolean handled = false;
        if (mIsFiltering) {
            handled = mFilterView.dispatchKeyEvent(event);
            if (!handled && (KeyEvent.KEYCODE_BACK == keyCode)
                    && (KeyEvent.ACTION_DOWN == event.getAction())) {
                showContactListView();
                handled = true;
            }
        } else {
            handled = mContactListView.dispatchKeyEvent(event);
            if (!handled && isReadable(keyCode, event)
                    && (KeyEvent.ACTION_DOWN == event.getAction())) {
                showFilterView();
                handled = mFilterView.dispatchKeyEvent(event);
            }
        }

        if (!handled) {
            handled = super.dispatchKeyEvent(event);
        }

        return handled;
    
private static booleanisReadable(int keyCode, android.view.KeyEvent event)

        if (KeyEvent.isModifierKey(keyCode) || event.isSystem()) {
            return false;
        }

        switch (keyCode) {
        case KeyEvent.KEYCODE_DPAD_CENTER:
        case KeyEvent.KEYCODE_DPAD_DOWN:
        case KeyEvent.KEYCODE_DPAD_LEFT:
        case KeyEvent.KEYCODE_DPAD_RIGHT:
        case KeyEvent.KEYCODE_DPAD_UP:
        case KeyEvent.KEYCODE_ENTER:
            return false;
        }

        return true;
    
static voidlog(java.lang.String msg)

        Log.d(ImApp.LOG_TAG, "<ContactListActivity> " +msg);
    
protected voidonCreate(android.os.Bundle icicle)


    
        
        super.onCreate(icicle);

        getWindow().requestFeature(Window.FEATURE_LEFT_ICON);

        LayoutInflater inflate = getLayoutInflater();
        mContactListView = (ContactListView) inflate.inflate(
                R.layout.contact_list_view, null);

        setContentView(mContactListView);

        Intent intent = getIntent();
        mAccountId = intent.getLongExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, -1);
        if (mAccountId == -1) {
            finish();
            return;
        }
        mApp = ImApp.getApplication(this);

        ContentResolver cr = getContentResolver();
        Cursor c = cr.query(ContentUris.withAppendedId(Im.Account.CONTENT_URI, mAccountId),
                null, null, null, null);
        if (c == null) {
            finish();
            return;
        }
        if (!c.moveToFirst()) {
            c.close();
            finish();
            return;
        }

        mProviderId = c.getLong(c.getColumnIndexOrThrow(Im.Account.PROVIDER));
        mHandler = new MyHandler(this);
        String username = c.getString(c.getColumnIndexOrThrow(Im.Account.USERNAME));

        BrandingResources brandingRes = mApp.getBrandingResource(mProviderId);
        setTitle(brandingRes.getString(
                BrandingResourceIDs.STRING_BUDDY_LIST_TITLE, username));
        getWindow().setFeatureDrawable(Window.FEATURE_LEFT_ICON,
                brandingRes.getDrawable(BrandingResourceIDs.DRAWABLE_LOGO));

        mSettingMap = new Im.ProviderSettings.QueryMap(
                getContentResolver(), mProviderId, true, null);

        mApp.callWhenServiceConnected(mHandler, new Runnable(){
            public void run() {
                if (!mDestroyed) {
                    mApp.dismissNotifications(mProviderId);
                    mConn = mApp.getConnection(mProviderId);
                    if (mConn == null) {
                        Log.e(ImApp.LOG_TAG, "The connection has disappeared!");
                        clearConnectionStatus();
                        finish();
                    } else {
                        mContactListView.setConnection(mConn);
                        mContactListView.setHideOfflineContacts(
                                mSettingMap.getHideOfflineContacts());
                    }
                }
            }
        });

        mContextMenuHandler = new ContextMenuHandler();
        mContactListView.getListView().setOnCreateContextMenuListener(this);

        mSettingMap.addObserver(new Observer() {
            public void update(Observable observed, Object updateData) {
                if (!mDestroyed) {
                    mContactListView.setHideOfflineContacts(
                            mSettingMap.getHideOfflineContacts());
                }
            }
        });
    
public voidonCreateContextMenu(android.view.ContextMenu menu, android.view.View v, android.view.ContextMenu.ContextMenuInfo menuInfo)

        boolean chatSelected = false;
        boolean contactSelected = false;
        Cursor contactCursor;
        if (mIsFiltering) {
            AdapterView.AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
            mContextMenuHandler.mPosition = info.position;
            contactSelected = true;
            contactCursor = mFilterView.getContactAtPosition(info.position);
        } else {
            ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) menuInfo;
            mContextMenuHandler.mPosition = info.packedPosition;
            contactSelected = mContactListView.isContactAtPosition(info.packedPosition);
            chatSelected = mContactListView.isConversationAtPosition(info.packedPosition);
            contactCursor = mContactListView.getContactAtPosition(info.packedPosition);
        }

        boolean allowBlock = true;
        if (contactCursor != null) {
            //XXX HACK: Yahoo! doesn't allow to block a friend. We can only block a temporary contact.
            ProviderDef provider = mApp.getProvider(mProviderId);
            if (Im.ProviderNames.YAHOO.equals(provider.mName)) {
                int type = contactCursor.getInt(contactCursor.getColumnIndexOrThrow(Im.Contacts.TYPE));
                allowBlock = (type == Im.Contacts.TYPE_TEMPORARY);
            }

            int nickNameIndex = contactCursor.getColumnIndexOrThrow(Im.Contacts.NICKNAME);

            menu.setHeaderTitle(contactCursor.getString(nickNameIndex));
        }

        BrandingResources brandingRes = mApp.getBrandingResource(mProviderId);
        String menu_end_conversation = brandingRes.getString(
                BrandingResourceIDs.STRING_MENU_END_CHAT);
        String menu_view_profile = brandingRes.getString(
                BrandingResourceIDs.STRING_MENU_VIEW_PROFILE);
        String menu_block_contact = brandingRes.getString(
                BrandingResourceIDs.STRING_MENU_BLOCK_CONTACT);
        String menu_start_conversation = brandingRes.getString(
                BrandingResourceIDs.STRING_MENU_START_CHAT);
        String menu_delete_contact = brandingRes.getString(
                BrandingResourceIDs.STRING_MENU_DELETE_CONTACT);

        if (chatSelected) {
            menu.add(0, MENU_END_CONVERSATION, 0, menu_end_conversation)
                    .setIcon(com.android.internal.R.drawable.ic_menu_end_conversation)
                    .setOnMenuItemClickListener(mContextMenuHandler);
            menu.add(0, MENU_VIEW_PROFILE, 0, menu_view_profile)
                    .setIcon(R.drawable.ic_menu_my_profile)
                    .setOnMenuItemClickListener(mContextMenuHandler);
            if (allowBlock) {
                menu.add(0, MENU_BLOCK_CONTACT, 0, menu_block_contact)
                        .setIcon(com.android.internal.R.drawable.ic_menu_block)
                        .setOnMenuItemClickListener(mContextMenuHandler);
            }
        } else if (contactSelected) {
            menu.add(0, MENU_START_CONVERSATION, 0, menu_start_conversation)
                    .setIcon(com.android.internal.R.drawable.ic_menu_start_conversation)
                    .setOnMenuItemClickListener(mContextMenuHandler);
            menu.add(0, MENU_VIEW_PROFILE, 0, menu_view_profile)
                    .setIcon(R.drawable.ic_menu_view_profile)
                    .setOnMenuItemClickListener(mContextMenuHandler);
            if (allowBlock) {
                menu.add(0, MENU_BLOCK_CONTACT, 0, menu_block_contact)
                        .setIcon(com.android.internal.R.drawable.ic_menu_block)
                        .setOnMenuItemClickListener(mContextMenuHandler);
            }
            menu.add(0, MENU_DELETE_CONTACT, 0, menu_delete_contact)
                    .setIcon(android.R.drawable.ic_menu_delete)
                    .setOnMenuItemClickListener(mContextMenuHandler);
        }
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.contact_list_menu, menu);

        BrandingResources brandingRes = mApp.getBrandingResource(mProviderId);
        menu.findItem(R.id.menu_invite_user).setTitle(
            brandingRes.getString(BrandingResourceIDs.STRING_MENU_ADD_CONTACT));

        return true;
    
protected voidonDestroy()

        mDestroyed = true;
        // set connection to null to unregister listeners.
        mContactListView.setConnection(null);
        if (mSettingMap != null) {
            mSettingMap.close();
        }
        super.onDestroy();
    
public booleanonOptionsItemSelected(android.view.MenuItem item)

        switch (item.getItemId()) {
            case R.id.menu_invite_user:
                Intent i = new Intent(ContactListActivity.this, AddContactActivity.class);
                i.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, mProviderId);
                i.putExtra(ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, mAccountId);
                i.putExtra(ImServiceConstants.EXTRA_INTENT_LIST_NAME,
                        mContactListView.getSelectedContactList());
                startActivity(i);
                return true;

            case R.id.menu_blocked_contacts:
                Uri.Builder builder = Im.BlockedList.CONTENT_URI.buildUpon();
                ContentUris.appendId(builder, mProviderId);
                ContentUris.appendId(builder, mAccountId);
                startActivity(new Intent(Intent.ACTION_VIEW, builder.build()));
                return true;

            case R.id.menu_view_accounts:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setType(Im.Provider.CONTENT_TYPE);
                startActivity(intent);
                finish();
                return true;

            case R.id.menu_settings:
                intent = new Intent(this, SettingActivity.class);
                intent.putExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, mProviderId);
                startActivity(intent);
                return true;

            case R.id.menu_sign_out:
                try {
                    if (mConn != null) {
                        mConn.logout();
                    }
                } catch (RemoteException e) {
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    
protected voidonPause()

        super.onPause();
        mApp.unregisterForConnEvents(mHandler);
    
protected voidonRestoreInstanceState(android.os.Bundle savedInstanceState)

        boolean isFiltering = savedInstanceState.getBoolean(FILTER_STATE_KEY);
        if (isFiltering) {
            showFilterView();
        }
        super.onRestoreInstanceState(savedInstanceState);
    
protected voidonResume()

        super.onResume();
        mApp.registerForConnEvents(mHandler);
        mContactListView.setAutoRefreshContacts(true);
    
protected voidonSaveInstanceState(android.os.Bundle outState)

        super.onSaveInstanceState(outState);
        outState.putBoolean(FILTER_STATE_KEY, mIsFiltering);

    
voidshowContactListView()

        if (mIsFiltering) {
            setContentView(mContactListView);
            mContactListView.requestFocus();
            mContactListView.invalidate();
            mIsFiltering = false;
        }
    
private voidshowFilterView()

        if (mFilterView == null ) {
            mFilterView = (ContactListFilterView)getLayoutInflater().inflate(
                    R.layout.contact_list_filter_view, null);
            mFilterView.getListView().setOnCreateContextMenuListener(this);
        }
        Uri uri = mSettingMap.getHideOfflineContacts() ? Im.Contacts.CONTENT_URI_ONLINE_CONTACTS_BY
                : Im.Contacts.CONTENT_URI_CONTACTS_BY;
        uri = ContentUris.withAppendedId(uri, mProviderId);
        uri = ContentUris.withAppendedId(uri, mAccountId);
        mFilterView.doFilter(uri, null);

        setContentView(mFilterView);
        mFilterView.requestFocus();
        mIsFiltering = true;