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

AddContactActivity

public class AddContactActivity extends android.app.Activity

Fields Summary
private static final String[]
CONTACT_LIST_PROJECTION
private static final int
CONTACT_LIST_NAME_COLUMN
private android.widget.MultiAutoCompleteTextView
mAddressList
private android.widget.Spinner
mListSpinner
android.widget.Button
mInviteButton
ImApp
mApp
SimpleAlertHandler
mHandler
private long
mProviderId
private long
mAccountId
private String
mDefaultDomain
private View.OnClickListener
mButtonHandler
private android.text.TextWatcher
mTextWatcher
Constructors Summary
Methods Summary
private com.android.im.IContactListgetContactList(com.android.im.IImConnection conn)

        if (conn == null) {
            return null;
        }

        try {
            IContactListManager contactListMgr = conn.getContactListManager();
            String listName = getSelectedListName();
            if (!TextUtils.isEmpty(listName)) {
                return contactListMgr.getContactList(listName);
            } else {
                // Use the default list
                List<IBinder> lists = contactListMgr.getContactLists();
                for (IBinder binder : lists) {
                    IContactList list = IContactList.Stub.asInterface(binder);
                    if (list.isDefault()) {
                        return list;
                    }
                }
                // No default list, use the first one as default list
                if (!lists.isEmpty()) {
                    return IContactList.Stub.asInterface(lists.get(0));
                }
                return null;
            }
        } catch (RemoteException e) {
            // If the service has died, there is no list for now.
            return null;
        }
    
private java.lang.StringgetSelectedListName()

        Cursor c = (Cursor) mListSpinner.getSelectedItem();
        return (c == null) ? null : c.getString(CONTACT_LIST_NAME_COLUMN);
    
voidinviteBuddies()

        Rfc822Token[] recipients = Rfc822Tokenizer.tokenize(mAddressList.getText());
        try {
            IImConnection conn = mApp.getConnection(mProviderId);
            IContactList list = getContactList(conn);
            if (list == null) {
                Log.e(ImApp.LOG_TAG, "<AddContactActivity> can't find given contact list:"
                        + getSelectedListName());
                finish();
            } else {
                boolean fail = false;
                for (Rfc822Token recipient : recipients) {
                    String username = recipient.getAddress();
                    if (mDefaultDomain != null && username.indexOf('@") == -1) {
                        username = username + "@" + mDefaultDomain;
                    }
                    if (Log.isLoggable(ImApp.LOG_TAG, Log.DEBUG)){
                        log("addContact:" + username);
                    }
                    int res = list.addContact(username);
                    if (res != ImErrorInfo.NO_ERROR) {
                        fail = true;
                        mHandler.showAlert(R.string.error,
                                ErrorResUtils.getErrorRes(getResources(), res, username));
                    }
                }
                // close the screen if there's no error.
                if (!fail) {
                    finish();
                }
            }
        } catch (RemoteException ex) {
            Log.e(ImApp.LOG_TAG, "<AddContactActivity> inviteBuddies: caught " + ex);
        }
    
private static voidlog(java.lang.String msg)


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


    
        
        super.onCreate(savedInstanceState);

        mApp = ImApp.getApplication(this);
        mHandler = new SimpleAlertHandler(this);
        resolveIntent(getIntent());

        setContentView(R.layout.add_contact_activity);

        BrandingResources brandingRes = mApp.getBrandingResource(mProviderId);
        setTitle(brandingRes.getString(BrandingResourceIDs.STRING_ADD_CONTACT_TITLE));

        TextView label = (TextView) findViewById(R.id.input_contact_label);
        label.setText(brandingRes.getString(BrandingResourceIDs.STRING_LABEL_INPUT_CONTACT));

        mAddressList = (MultiAutoCompleteTextView) findViewById(R.id.email);
        mAddressList.setAdapter(new EmailAddressAdapter(this));
        mAddressList.setTokenizer(new Rfc822Tokenizer());
        mAddressList.addTextChangedListener(mTextWatcher);

        mListSpinner = (Spinner) findViewById(R.id.choose_list);

        Cursor c = queryContactLists();
        int initSelection = searchInitListPos(c, getIntent().getStringExtra(
                ImServiceConstants.EXTRA_INTENT_LIST_NAME));
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                android.R.layout.simple_spinner_item,
                c,
                new String[] {Im.ContactList.NAME},
                new int[] {android.R.id.text1});
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mListSpinner.setAdapter(adapter);
        mListSpinner.setSelection(initSelection);

        mInviteButton = (Button) findViewById(R.id.invite);
        mInviteButton.setText(brandingRes.getString(
                BrandingResourceIDs.STRING_BUTTON_ADD_CONTACT));
        mInviteButton.setOnClickListener(mButtonHandler);
        mInviteButton.setEnabled(false);
    
private android.database.CursorqueryContactLists()

        Uri uri = Im.ContactList.CONTENT_URI;
        uri = ContentUris.withAppendedId(uri, mProviderId);
        uri = ContentUris.withAppendedId(uri, mAccountId);
        Cursor c = managedQuery(uri, CONTACT_LIST_PROJECTION, null, null);
        return c;
    
private voidresolveIntent(android.content.Intent intent)

        mProviderId = intent.getLongExtra(
                ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, -1);
        mAccountId = intent.getLongExtra(
                ImServiceConstants.EXTRA_INTENT_ACCOUNT_ID, -1);
        mDefaultDomain = Im.ProviderSettings.getStringValue(getContentResolver(),
                mProviderId, ImpsConfigNames.DEFAULT_DOMAIN);
    
private intsearchInitListPos(android.database.Cursor c, java.lang.String listName)

        if (TextUtils.isEmpty(listName)) {
            return 0;
        }
        c.moveToPosition(-1);
        while (c.moveToNext()) {
            if (listName.equals(c.getString(CONTACT_LIST_NAME_COLUMN))) {
                return c.getPosition();
            }
        }
        return 0;