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

ImUrlActivity

public class ImUrlActivity extends android.app.Activity

Fields Summary
private static final String[]
ACCOUNT_PROJECTION
private static final int
ACCOUNT_ID_COLUMN
private static final int
ACCOUNT_PW_COLUMN
private String
mProviderName
private String
mToAddress
private ImApp
mApp
private com.android.im.IImConnection
mConn
Constructors Summary
Methods Summary
private voidaddAccount(long providerId)

        Intent  intent = new Intent(this, AccountActivity.class);
        intent.setAction(Intent.ACTION_INSERT);
        intent.setData(ContentUris.withAppendedId(Im.Provider.CONTENT_URI, providerId));
        intent.putExtra(ImApp.EXTRA_INTENT_SEND_TO_USER, mToAddress);
        startActivity(intent);
    
private voideditAccount(long accountId)

        Uri accountUri = ContentUris.withAppendedId(Im.Account.CONTENT_URI, accountId);
        Intent intent = new Intent(this, AccountActivity.class);
        intent.setAction(Intent.ACTION_EDIT);
        intent.setData(accountUri);
        intent.putExtra(ImApp.EXTRA_INTENT_SEND_TO_USER, mToAddress);
        startActivity(intent);
    
private java.lang.StringfindMatchingProvider(java.lang.String provider)

        if (TextUtils.isEmpty(provider)) {
            return null;
        }

        if (Im.ProviderNames.AIM.equalsIgnoreCase(provider)) {
            return Im.ProviderNames.AIM;
        }

        if (Im.ProviderNames.MSN.equalsIgnoreCase(provider)) {
            return Im.ProviderNames.MSN;
        }

        if (Im.ProviderNames.YAHOO.equalsIgnoreCase(provider)) {
            return Im.ProviderNames.YAHOO;
        }

        return null;
    
private java.lang.StringgetProviderNameForCategory(java.lang.String providerCategory)

        if (providerCategory != null) {
            if (providerCategory.equalsIgnoreCase("com.android.im.category.AIM")) {
                return Im.ProviderNames.AIM;
            } else if (providerCategory.equalsIgnoreCase("com.android.im.category.MSN")) {
                return Im.ProviderNames.MSN;
            } else if (providerCategory.equalsIgnoreCase("com.android.im.category.YAHOO")) {
                return Im.ProviderNames.YAHOO;
            }
        }

        return null;
    
voidhandleIntent()

        ContentResolver cr = getContentResolver();
        long providerId = Im.Provider.getProviderIdForName(cr, mProviderName);
        long accountId;

        mConn= mApp.getConnection(providerId);
        if (mConn == null) {
            Cursor c = DatabaseUtils.queryAccountsForProvider(cr, ACCOUNT_PROJECTION, providerId);
            if (c == null) {
                addAccount(providerId);
            } else {
                accountId = c.getLong(ACCOUNT_ID_COLUMN);
                if (c.isNull(ACCOUNT_PW_COLUMN)) {
                    editAccount(accountId);
                } else {
                    signInAccount(accountId);
                }
            }
        } else {
            try {
                int state = mConn.getState();
                accountId = mConn.getAccountId();

                if (state < ImConnection.LOGGED_IN) {
                    signInAccount(accountId);
                } else if (state == ImConnection.LOGGED_IN || state == ImConnection.SUSPENDED) {
                    if (!isValidToAddress()) {
                        showContactList(accountId);
                    } else {
                        openChat(providerId, accountId);
                    }
                }
            } catch (RemoteException e) {
                // Ouch!  Service died!  We'll just disappear.
                Log.w("ImUrlActivity", "Connection disappeared!");
            }
        }
        finish();
    
private booleanisValidToAddress()

        if (TextUtils.isEmpty(mToAddress)) {
            return false;
        }

        if (mToAddress.indexOf('/") != -1) {
            return false;
        }

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

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


    
        
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        if (Intent.ACTION_SENDTO.equals(intent.getAction())) {
            if (!resolveIntent(intent)) {
                finish();
                return;
            }

            if (TextUtils.isEmpty(mToAddress)) {
                Log.w(ImApp.LOG_TAG, "<ImUrlActivity>Invalid to address:" + mToAddress);
                finish();
                return;
            }
            mApp = ImApp.getApplication(this);
            mApp.callWhenServiceConnected(new Handler(), new Runnable(){
                public void run() {
                    handleIntent();
                }});

        } else {
            finish();
        }
    
private voidopenChat(long provider, long account)

        try {
            IChatSessionManager manager = mConn.getChatSessionManager();
            IChatSession session = manager.getChatSession(mToAddress);
            if(session == null) {
                session = manager.createChatSession(mToAddress);
            }

            Uri data = ContentUris.withAppendedId(Im.Chats.CONTENT_URI, session.getId());
            Intent i = new Intent(Intent.ACTION_VIEW, data);
            i.putExtra("from", mToAddress);
            i.putExtra("providerId", provider);
            i.putExtra("accountId", account);
            i.addCategory(ImApp.IMPS_CATEGORY);
            startActivity(i);
        } catch (RemoteException e) {
            // Ouch!  Service died!  We'll just disappear.
            Log.w("ImUrlActivity", "Connection disappeared!");
        }
    
private booleanresolveIntent(android.content.Intent intent)

        Uri data = intent.getData();
        String host = data.getHost();

        if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) {
            log("resolveIntent: host=" + host);
        }

        if (TextUtils.isEmpty(host)) {
            Set<String> categories = intent.getCategories();
            if (categories != null) {
                Iterator<String> iter = categories.iterator();
                if (iter.hasNext()) {
                    String category = iter.next();
                    String providerName = getProviderNameForCategory(category);
                    mProviderName = findMatchingProvider(providerName);
                    if (mProviderName == null) {
                        Log.w(ImApp.LOG_TAG,
                                "resolveIntent: IM provider "+ category + " not supported");
                        return false;
                    }
                }
            }
            mToAddress = data.getSchemeSpecificPart();
        } else {
            mProviderName = findMatchingProvider(host);

            if (mProviderName == null) {
                Log.w(ImApp.LOG_TAG, "resolveIntent: IM provider "+ host + " not supported");
                return false;
            }

            String path = data.getPath();

            if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) log("resolveIntent: path=" + path);

            if (!TextUtils.isEmpty(path)) {
                int index;
                if ((index = path.indexOf('/")) != -1) {
                    mToAddress = path.substring(index+1);
                }
            }
        }

        if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)) {
            log("resolveIntent: provider=" + mProviderName + ", to=" + mToAddress);
        }

        return true;
    
private voidshowContactList(long accountId)

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Im.Contacts.CONTENT_URI);
        intent.addCategory(ImApp.IMPS_CATEGORY);
        intent.putExtra("accountId", accountId);

        startActivity(intent);
    
private voidsignInAccount(long accountId)

        Uri accountUri = ContentUris.withAppendedId(Im.Account.CONTENT_URI, accountId);
        Intent intent = new Intent(this, SigningInActivity.class);
        intent.setData(accountUri);
        intent.putExtra(ImApp.EXTRA_INTENT_SEND_TO_USER, mToAddress);
        startActivity(intent);