FileDocCategorySizeDatePackage
ImConnectionAdapter.javaAPI DocAndroid 1.5 API17015Wed May 06 22:42:46 BST 2009com.android.im.service

ImConnectionAdapter

public class ImConnectionAdapter extends IImConnection.Stub

Fields Summary
private static final String
TAG
private static final String[]
SESSION_COOKIE_PROJECTION
private static final int
COLUMN_SESSION_COOKIE_NAME
private static final int
COLUMN_SESSION_COOKIE_VALUE
com.android.im.engine.ImConnection
mConnection
private ConnectionListenerAdapter
mConnectionListener
private InvitationListenerAdapter
mInvitationListener
final android.os.RemoteCallbackList
mRemoteConnListeners
ChatSessionManagerAdapter
mChatSessionManager
ContactListManagerAdapter
mContactListManager
com.android.im.engine.ChatGroupManager
mGroupManager
RemoteImService
mService
long
mProviderId
long
mAccountId
boolean
mAutoLoadContacts
int
mConnectionState
Constructors Summary
public ImConnectionAdapter(long providerId, com.android.im.engine.ImConnection connection, RemoteImService service)


        
              
        mProviderId = providerId;
        mConnection = connection;
        mService = service;
        mConnectionListener = new ConnectionListenerAdapter();
        mConnection.addConnectionListener(mConnectionListener);
        if ((connection.getCapability() & ImConnection.CAPABILITY_GROUP_CHAT) != 0) {
            mGroupManager = mConnection.getChatGroupManager();
            mInvitationListener = new InvitationListenerAdapter();
            mGroupManager.setInvitationListener(mInvitationListener);
        }
    
Methods Summary
public voidacceptInvitation(long id)

        handleInvitation(id, true);
    
public synchronized voidcancelLogin()

        if (mConnectionState >= ImConnection.LOGGED_IN) {
            // too late
            return;
        }

        logout();
    
voidclearSessionCookie(android.content.ContentResolver cr)

        cr.delete(getSessionCookiesUri(), null, null);
    
private static intconvertConnStateForDb(int state)

        switch (state) {
        case ImConnection.DISCONNECTED:
        case ImConnection.LOGGING_OUT:
            return Im.ConnectionStatus.OFFLINE;

        case ImConnection.LOGGING_IN:
            return Im.ConnectionStatus.CONNECTING;

        case ImConnection.LOGGED_IN:
            return Im.ConnectionStatus.ONLINE;

        case ImConnection.SUSPENDED:
        case ImConnection.SUSPENDING:
            return Im.ConnectionStatus.SUSPENDED;

        default:
            return Im.ConnectionStatus.OFFLINE;
        }
    
public longgetAccountId()

        return mAccountId;
    
public com.android.im.engine.ImConnectiongetAdaptee()

        return mConnection;
    
public intgetChatSessionCount()

        if (mChatSessionManager == null) {
            return 0;
        }
        return mChatSessionManager.getChatSessionCount();
    
public com.android.im.IChatSessionManagergetChatSessionManager()

        return mChatSessionManager;
    
public com.android.im.IContactListManagergetContactListManager()

        return mContactListManager;
    
public RemoteImServicegetContext()

        return mService;
    
public com.android.im.engine.ContactgetLoginUser()

        return mConnection.getLoginUser();
    
public longgetProviderId()

        return mProviderId;
    
private android.net.UrigetSessionCookiesUri()

        Uri.Builder builder = Im.SessionCookies.CONTENT_URI_SESSION_COOKIES_BY.buildUpon();
        ContentUris.appendId(builder, mProviderId);
        ContentUris.appendId(builder, mAccountId);

        return builder.build();
    
public intgetState()

        return mConnectionState;
    
public int[]getSupportedPresenceStatus()

        return mConnection.getSupportedPresenceStatus();
    
public com.android.im.engine.PresencegetUserPresence()

        return mConnection.getUserPresence();
    
private voidhandleInvitation(long id, boolean accept)

        if(mGroupManager == null) {
            return;
        }
        ContentResolver cr = mService.getContentResolver();
        Cursor c = cr.query(ContentUris.withAppendedId(Im.Invitation.CONTENT_URI, id), null, null, null, null);
        if(c == null) {
            return;
        }
        if(c.moveToFirst()) {
            String inviteId = c.getString(c.getColumnIndexOrThrow(Im.Invitation.INVITE_ID));
            int status;
            if(accept) {
                mGroupManager.acceptInvitationAsync(inviteId);
                status = Im.Invitation.STATUS_ACCEPTED;
            } else {
                mGroupManager.rejectInvitationAsync(inviteId);
                status = Im.Invitation.STATUS_REJECTED;
            }
            c.updateInt(c.getColumnIndexOrThrow(Im.Invitation.STATUS), status);
            c.commitUpdates();
        }
        c.close();
    
public voidlogin(long accountId, java.lang.String userName, java.lang.String password, boolean autoLoadContacts)

        mAccountId = accountId;
        mAutoLoadContacts = autoLoadContacts;
        mConnectionState = ImConnection.LOGGING_IN;

        mConnection.loginAsync(new LoginInfo(userName, password));

        mChatSessionManager = new ChatSessionManagerAdapter(this);
        mContactListManager = new ContactListManagerAdapter(this);
    
public voidlogout()

        mConnectionState = ImConnection.LOGGING_OUT;
        mConnection.logoutAsync();
    
public voidnetworkTypeChanged()

        mConnection.networkTypeChanged();
    
private java.util.HashMapquerySessionCookie(android.content.ContentResolver cr)

        Cursor c = cr.query(getSessionCookiesUri(), SESSION_COOKIE_PROJECTION, null, null, null);
        if (c == null) {
            return null;
        }

        HashMap<String, String> cookie = null;
        if (c.getCount() > 0) {
            cookie = new HashMap<String, String>();
            while(c.moveToNext()) {
                cookie.put(c.getString(COLUMN_SESSION_COOKIE_NAME),
                    c.getString(COLUMN_SESSION_COOKIE_VALUE));
            }
        }

        c.close();
        return cookie;
    
voidreestablishSession()

        mConnectionState = ImConnection.LOGGING_IN;

        ContentResolver cr = mService.getContentResolver();
        if ((mConnection.getCapability() & ImConnection.CAPABILITY_SESSION_REESTABLISHMENT) != 0) {
            HashMap<String, String> cookie = querySessionCookie(cr);
            if (cookie != null) {
                Log.d(TAG, "re-establish session");
                try {
                    mConnection.reestablishSessionAsync(cookie);
                } catch (IllegalArgumentException e) {
                    Log.e(TAG, "Invalid session cookie, probably modified by others.");
                    clearSessionCookie(cr);
                }
            }
        }
    
public voidregisterConnectionListener(com.android.im.IConnectionListener listener)

        if (listener != null) {
            mRemoteConnListeners.register(listener);
        }
    
public voidrejectInvitation(long id)

        handleInvitation(id, false);
    
voidsaveSessionCookie(android.content.ContentResolver cr)

        HashMap<String, String> cookies = mConnection.getSessionContext();

        int i = 0;
        ContentValues[] valuesList = new ContentValues[cookies.size()];

        for(Map.Entry<String,String> entry : cookies.entrySet()){
            ContentValues values = new ContentValues(2);

            values.put(Im.SessionCookies.NAME, entry.getKey());
            values.put(Im.SessionCookies.VALUE, entry.getValue());

            valuesList[i++] = values;
        }

        cr.bulkInsert(getSessionCookiesUri(), valuesList);
    
public voidsetInvitationListener(com.android.im.IInvitationListener listener)

        if(mInvitationListener != null) {
            mInvitationListener.mRemoteListener = listener;
        }
    
voidsuspend()

        mConnectionState = ImConnection.SUSPENDING;
        mConnection.suspend();
    
public voidunregisterConnectionListener(com.android.im.IConnectionListener listener)

        if (listener != null) {
            mRemoteConnListeners.unregister(listener);
        }
    
voidupdateAccountStatusInDb()

        Presence p = getUserPresence();
        int presenceStatus = Im.Presence.OFFLINE;
        int connectionStatus = convertConnStateForDb(mConnectionState);

        if (p != null) {
            presenceStatus = ContactListManagerAdapter.convertPresenceStatus(p);
        }

        ContentResolver cr = mService.getContentResolver();
        Uri uri = Im.AccountStatus.CONTENT_URI;
        ContentValues values = new ContentValues();

        values.put(Im.AccountStatus.ACCOUNT, mAccountId);
        values.put(Im.AccountStatus.PRESENCE_STATUS, presenceStatus);
        values.put(Im.AccountStatus.CONNECTION_STATUS, connectionStatus);

        cr.insert(uri, values);
    
public intupdateUserPresence(com.android.im.engine.Presence newPresence)

        try {
            mConnection.updateUserPresenceAsync(newPresence);
        } catch (ImException e) {
            return e.getImError().getCode();
        }

        return ImErrorInfo.NO_ERROR;