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

ChatSwitcher

public class ChatSwitcher extends Object

Fields Summary
private static final boolean
LOCAL_DEBUG
private static final String[]
PROVIDER_CATEGORY_PROJECTION
private static final int
PROVIDER_CATEGORY_COLUMN
private boolean
mPreferMenuShortcut
public SwitcherAdapter
mSwitcherAdapter
private android.app.Activity
mActivity
private android.os.Handler
mHandler
private ImApp
mApp
private String
mQuerySelection
private String[]
mQuerySelectionArgs
private android.view.LayoutInflater
mInflater
private SwitcherRunnable
mSwitcherCallback
private ArrayList
mViews
private boolean
mOkToShowEmptyView
private ChatSwitcherDialog
mChatSwitcherDialog
private android.content.AsyncQueryHandler
mQueryHandler
private static final int
sQueryToken
private static final long
sPeriodicUpdatePeriod
public int
mContactIdColumn
public int
mProviderIdColumn
public int
mAccountIdColumn
public int
mUsernameColumn
public int
mNicknameColumn
public int
mAvatarDataColumn
public int
mPresenceStatusColumn
public int
mLastUnreadMessageColumn
public int
mShortcutColumn
public int
mLastChatColumn
public int
mGroupChatColumn
private android.database.DataSetObserver
mObserver
private Runnable
mSimpleUpdateRunnable
Constructors Summary
public ChatSwitcher(android.app.Activity a, android.os.Handler handler, ImApp app, android.view.LayoutInflater inflater, SwitcherRunnable switcher)

        mActivity = a;
        mApp = app;
        mHandler = handler;
        mInflater = inflater;
        mSwitcherCallback = switcher;

        buildQueryParams();

        mSwitcherAdapter = new SwitcherAdapter(null, a);

        mQueryHandler = new AsyncQueryHandler(mActivity.getContentResolver()) {
            @Override
            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {

                if (cursor != null) {
                    mContactIdColumn = cursor.getColumnIndexOrThrow(Im.Contacts._ID);
                    mProviderIdColumn = cursor.getColumnIndexOrThrow(Im.Contacts.PROVIDER);
                    mAccountIdColumn = cursor.getColumnIndexOrThrow(Im.Contacts.ACCOUNT);
                    mUsernameColumn = cursor.getColumnIndexOrThrow(Im.Contacts.USERNAME);
                    mNicknameColumn = cursor.getColumnIndexOrThrow(Im.Contacts.NICKNAME);
                    mPresenceStatusColumn = cursor.getColumnIndexOrThrow(Im.Contacts.PRESENCE_STATUS);
                    mLastUnreadMessageColumn = cursor.getColumnIndexOrThrow(Im.Chats.LAST_UNREAD_MESSAGE);
                    mAvatarDataColumn = cursor.getColumnIndexOrThrow(Im.Contacts.AVATAR_DATA);
                    mShortcutColumn = cursor.getColumnIndexOrThrow(Im.Chats.SHORTCUT);
                    mLastChatColumn = cursor.getColumnIndexOrThrow(Im.Chats.LAST_MESSAGE_DATE);
                    mGroupChatColumn = cursor.getColumnIndexOrThrow(Im.Chats.GROUP_CHAT);
                }

                mOkToShowEmptyView = true;
                update();

                OnQueryCompleteRunnable r = (OnQueryCompleteRunnable) cookie;
                if (r != null) {
                    r.onComplete(cursor);
                }
            }

            @Override
            protected void onUpdateComplete(int token, Object cookie, int result) {
                super.onUpdateComplete(token, cookie, result);
            }
        };
    
Methods Summary
private voidbuildQueryParams()

        StringBuilder buf = new StringBuilder("(");
        List<ProviderDef> list = mApp.getProviders();

        mQuerySelectionArgs = new String[list.size()];

        int i = 0;
        for (ProviderDef providerDef : list) {
            //Log.i(ImApp.LOG_TAG, "[ChatSwitcher] buildQueryParams: provider " + providerDef.mName +
            //        ", " + providerDef.mId);

            if (i > 0) {
                buf.append(" OR ");
            }
            
            buf.append(Im.Contacts.PROVIDER).append("=?");
            mQuerySelectionArgs[i] = String.valueOf(providerDef.mId);
            i++;
        }

        buf.append(')");
        mQuerySelection = buf.toString();

        //Log.i(ImApp.LOG_TAG, "[ChatSwitcher] buildQueryParams: selection => " + mQuerySelection);
    
private voidcancelPreviousQuery()


       
        mQueryHandler.cancelOperation(sQueryToken);
    
public voidclose()


       
        if (isOpen()) {
            mChatSwitcherDialog.dismiss();
            mChatSwitcherDialog = null;

            // if we're in the midst of querying then don't
            cancelPreviousQuery();

            // clean up the adapter, close cursors, etc.
            mSwitcherAdapter.unregisterDataSetObserver(mObserver);
            mSwitcherAdapter.changeCursor(null);

            mViews.clear();

            mHandler.removeCallbacks(mSimpleUpdateRunnable);
        }
    
private static java.lang.StringfindCategory(android.content.ContentResolver resolver, long providerId)

        // find the provider category for this chat
        Cursor providerCursor = resolver.query(
                Im.Provider.CONTENT_URI,
                PROVIDER_CATEGORY_PROJECTION,
                "_id = " + providerId,
                null /* selection args */,
                null /* sort order */
        );
        String category = null;

        try {
            if (providerCursor.moveToFirst()) {
                category = providerCursor.getString(PROVIDER_CATEGORY_COLUMN);
            }
        } finally {
            providerCursor.close();
        }

        return category;
    
private intfindCurrent(android.database.Cursor c, java.lang.String contact, long accountId, long providerId)

        c.moveToPosition(-1);
        while (c.moveToNext()) {
            if (c.getLong(mAccountIdColumn) != accountId) {
                continue;
            }
            if (c.getLong(mProviderIdColumn) != providerId) {
                continue;
            }
            if (!c.getString(mUsernameColumn).equals(contact)) {
                continue;
            }

            return c.getPosition();
        }
        return -1;
    
private voidhandleShortcut(android.database.Cursor c, int key)

        if (mActivity.isFinishing()) {
            return;
        }

        c.moveToPosition(-1);
        while (c.moveToNext()) {
            long shortcut = c.getLong(mShortcutColumn);
            if (shortcut >= 0 && shortcut < 10 && key == (shortcut + '0")) {
                select(c, c.getPosition());
                break;
            }
        }
    
public voidhandleShortcut(int key)

        if (mSwitcherAdapter.getCursor() != null) {
            handleShortcut(mSwitcherAdapter.getCursor(), key);
        } else {
            startQuery(new OnQueryCompleteRunnable() {
                public void onComplete(Cursor c) {
                    handleShortcut(c, key);
                    c.close();
                }
            });
        }
    
public booleanisOpen()

        return mChatSwitcherDialog != null;
    
private voidlog(java.lang.String msg)

        Log.d(ImApp.LOG_TAG, "[ChatSwitcher] " + msg);
    
public static android.content.IntentmakeChatIntent(android.content.ContentResolver resolver, long provider, long account, java.lang.String contact, long contactId, int groupChat)

        Intent i = new Intent(Intent.ACTION_VIEW,
                ContentUris.withAppendedId(Im.Chats.CONTENT_URI, contactId));
        i.addCategory(findCategory(resolver, provider));
        i.putExtra("from", contact);
        i.putExtra("providerId", provider);
        i.putExtra("accountId", account);

        if (groupChat != 0) {
            i.putExtra("groupChat", groupChat);
        }

        return i;
    
public voidonResume()

        if (isOpen() && !mActivity.isFinishing()) {
            mChatSwitcherDialog.updateTimes();
        }
    
public voidopen()

        mChatSwitcherDialog = new ChatSwitcherDialog(mActivity, this);
        mChatSwitcherDialog.show();

        mOkToShowEmptyView = false;
        mPreferMenuShortcut = false;

        mSwitcherAdapter.registerDataSetObserver(mObserver);
        startQuery();

        mHandler.postDelayed(mSimpleUpdateRunnable, sPeriodicUpdatePeriod);
    
private voidrotateChat(android.database.Cursor c, int direction, java.lang.String contact, long accountId, long providerId)

        int count = c.getCount();
        if (count < 2) {
            return;
        }

        int position = findCurrent(c, contact, accountId, providerId);
        if (position == -1) {
            return;
        }

        position += direction;
        if (position == count) {
            position = 0;
        } else if (position == -1) {
            position = count - 1;
        }

        select(c, position);
    
public voidrotateChat(int direction, java.lang.String contact, long accountId, long providerId)

        if (direction != 1 && direction != -1) {
            return;
        }
        if (mSwitcherAdapter.getCursor() != null) {
            rotateChat(mSwitcherAdapter.getCursor(), direction, contact, accountId, providerId);
        } else {
            startQuery(new OnQueryCompleteRunnable() {
                public void onComplete(Cursor c) {
                    rotateChat(c, direction, contact, accountId, providerId);
                    c.close();
                }
            });
        }
    
public voidselect(android.database.Cursor c, int position)

        if (!c.moveToPosition(position)) {
            Log.e(ImApp.LOG_TAG, "select: moved to pos=" + position + " failed");
            return;
        }

        long contactId = c.getLong(mContactIdColumn);
        String contact = c.getString(mUsernameColumn);
        long accountId = c.getLong(mAccountIdColumn);
        long providerId = c.getLong(mProviderIdColumn);
        int groupChat = c.getInt(mGroupChatColumn);
        Intent intent = ChatSwitcher.makeChatIntent(
                mActivity.getContentResolver(), providerId, accountId,
                contact, contactId, groupChat);

        if (mSwitcherCallback == null || !mSwitcherCallback.switchTo(contact, accountId, intent)) {
            mActivity.startActivity(intent);
            mActivity.finish();
        }
        close();
    
private voidstartQuery()

        startQuery(new OnQueryCompleteRunnable() {
            public void onComplete(Cursor c) {
                mSwitcherAdapter.changeCursor(c);
            }
        });
    
private voidstartQuery(com.android.im.app.ChatSwitcher$OnQueryCompleteRunnable runnable)

        cancelPreviousQuery();
        mQueryHandler.startQuery(
                sQueryToken,
                runnable,
                Im.Contacts.CONTENT_URI_CHAT_CONTACTS,
                null, /*projection*/
                mQuerySelection,
                mQuerySelectionArgs,
                null  /*orderBy*/);
    
private voidupdate()

        if (mChatSwitcherDialog != null) {
            mChatSwitcherDialog.update();
        }