AddContactActivity.java
上传用户:szyujian
上传日期:2016-09-20
资源大小:320k
文件大小:9k
- /*
- * Copyright (C) 2008 Esmertec AG.
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.android.im.app;
- import static android.provider.Contacts.ContactMethods.CONTENT_EMAIL_URI;
- import android.app.Activity;
- import android.content.ContentResolver;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.DatabaseUtils;
- import android.os.Bundle;
- import android.os.IBinder;
- import android.os.RemoteException;
- import android.provider.Im;
- import android.provider.Contacts.ContactMethods;
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.text.util.Rfc822Token;
- import android.text.util.Rfc822Tokenizer;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.MultiAutoCompleteTextView;
- import android.widget.ResourceCursorAdapter;
- import android.widget.TextView;
- import com.android.im.IContactList;
- import com.android.im.IContactListManager;
- import com.android.im.IImConnection;
- import com.android.im.R;
- import com.android.im.engine.ImErrorInfo;
- import com.android.im.plugin.BrandingResourceIDs;
- import com.android.im.plugin.ImpsConfigNames;
- import com.android.im.service.ImServiceConstants;
- import java.util.List;
- public class AddContactActivity extends Activity {
- private MultiAutoCompleteTextView mAddressList;
- Button mInviteButton;
- ImApp mApp;
- SimpleAlertHandler mHandler;
- private long mProviderId;
- private String mListName;
- private String mDefaultDomain;
- @Override
- protected void onCreate(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);
- mInviteButton = (Button) findViewById(R.id.invite);
- mInviteButton.setText(brandingRes.getString(BrandingResourceIDs.STRING_BUTTON_ADD_CONTACT));
- mInviteButton.setOnClickListener(mButtonHandler);
- mInviteButton.setEnabled(false);
- }
- private void resolveIntent(Intent intent) {
- mProviderId = intent.getLongExtra(ImServiceConstants.EXTRA_INTENT_PROVIDER_ID, -1);
- mListName = intent.getStringExtra(ImServiceConstants.EXTRA_INTENT_LIST_NAME);
- mDefaultDomain = Im.ProviderSettings.getStringValue(getContentResolver(),
- mProviderId, ImpsConfigNames.DEFAULT_DOMAIN);
- }
- void inviteBuddies() {
- 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:" + mListName);
- 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 IContactList getContactList(IImConnection conn) {
- if (conn == null) {
- return null;
- }
- try {
- IContactListManager contactListMgr = conn.getContactListManager();
- if (!TextUtils.isEmpty(mListName)) {
- return contactListMgr.getContactList(mListName);
- } 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 View.OnClickListener mButtonHandler = new View.OnClickListener() {
- public void onClick(View v) {
- mApp.callWhenServiceConnected(mHandler, new Runnable() {
- public void run() {
- inviteBuddies();
- }
- });
- }
- };
- private TextWatcher mTextWatcher = new TextWatcher() {
- public void afterTextChanged(Editable s) {
- mInviteButton.setEnabled(s.length() != 0);
- }
- public void beforeTextChanged(CharSequence s, int start, int count, int after) {
- // noop
- }
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- // noop
- }
- };
- private static void log(String msg) {
- Log.d(ImApp.LOG_TAG, "<AddContactActivity> " + msg);
- }
- static class EmailAddressAdapter extends ResourceCursorAdapter {
- public static final int DATA_INDEX = 1;
- private static final String SORT_ORDER = "people.name, contact_methods.data";
- private ContentResolver mContentResolver;
- private static final String[] PROJECTION = {
- ContactMethods._ID, // 0
- ContactMethods.DATA // 1
- };
- public EmailAddressAdapter(Context context) {
- super(context, android.R.layout.simple_dropdown_item_1line, null);
- mContentResolver = context.getContentResolver();
- }
- @Override
- public final String convertToString(Cursor cursor) {
- return cursor.getString(DATA_INDEX);
- }
- @Override
- public final void bindView(View view, Context context, Cursor cursor) {
- ((TextView) view).setText(cursor.getString(DATA_INDEX));
- }
- @Override
- public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
- String where = null;
- if (constraint != null) {
- String filter = DatabaseUtils.sqlEscapeString(constraint.toString() + '%');
- StringBuilder s = new StringBuilder();
- s.append("(people.name LIKE ");
- s.append(filter);
- s.append(") OR (contact_methods.data LIKE ");
- s.append(filter);
- s.append(")");
- where = s.toString();
- }
- return mContentResolver.query(CONTENT_EMAIL_URI, PROJECTION, where, null, SORT_ORDER);
- }
- }
- }