17903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng/*
27903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * Copyright (C) 2009 The Android Open Source Project
37903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng *
47903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * Licensed under the Apache License, Version 2.0 (the "License");
57903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * you may not use this file except in compliance with the License.
67903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * You may obtain a copy of the License at
77903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng *
87903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng *      http://www.apache.org/licenses/LICENSE-2.0
97903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng *
107903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * Unless required by applicable law or agreed to in writing, software
117903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * distributed under the License is distributed on an "AS IS" BASIS,
127903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * See the License for the specific language governing permissions and
147903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * limitations under the License.
157903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng */
167903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
177903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengpackage com.android.contacts.common.util;
187903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
190b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jangimport android.app.Activity;
207903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.app.AlertDialog;
217903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.app.Dialog;
227903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.Context;
237903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.DialogInterface;
247903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.Intent;
257903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.net.Uri;
267903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.util.Log;
277903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.ContextThemeWrapper;
287903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.LayoutInflater;
297903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.View;
307903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.ViewGroup;
317903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.widget.ArrayAdapter;
32d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wangimport android.widget.ImageView;
337903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.widget.TextView;
347903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
357903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.R;
367903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
377903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.account.AccountType;
387903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet;
397903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.vcard.ImportVCardActivity;
407903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
417903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport java.util.List;
427903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
437903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng/**
4490b8b6dfa7ee6ab6f2e473ea3cfa7d80e526bf9aBrian Attwell * Utility class for selecting an Account for importing contact(s)
457903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng */
467903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengpublic class AccountSelectionUtil {
477903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    // TODO: maybe useful for EditContactActivity.java...
487903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    private static final String LOG_TAG = "AccountSelectionUtil";
497903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
507903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static boolean mVCardShare = false;
517903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
527903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static Uri mPath;
537903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
547903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static class AccountSelectedListener
557903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            implements DialogInterface.OnClickListener {
567903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
570b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        final private Activity mActivity;
587903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final private int mResId;
59d09e43ca0bd57c25cbd4e4a87bff0b9fc10a1459Wink Saville        final private int mSubscriptionId;
607903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
617903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final protected List<AccountWithDataSet> mAccountList;
627903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
630b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
64d09e43ca0bd57c25cbd4e4a87bff0b9fc10a1459Wink Saville                int resId, int subscriptionId) {
657903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            if (accountList == null || accountList.size() == 0) {
667903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                Log.e(LOG_TAG, "The size of Account list is 0.");
677903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
680b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang            mActivity = activity;
697903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            mAccountList = accountList;
707903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            mResId = resId;
719524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell            mSubscriptionId = subscriptionId;
729524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell        }
739524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell
74b49ff73fbeb64a5aeffbe26bbdd015031e288c79Walter Jang        public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
759524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell                int resId) {
769524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell            // Subscription id is only needed for importing from SIM card. We can safely ignore
779524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell            // its value for SD card importing.
784718349e14c08e21f78e48e306a99d1b0fb5cd2eWalter Jang            this(activity, accountList, resId, /* subscriptionId = */ -1);
797903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
807903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
817903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        public void onClick(DialogInterface dialog, int which) {
827903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            dialog.dismiss();
83b49ff73fbeb64a5aeffbe26bbdd015031e288c79Walter Jang            doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId);
847903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
857903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
867903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
870b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang    public static Dialog getSelectAccountDialog(Activity activity, int resId) {
880b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        return getSelectAccountDialog(activity, resId, null, null);
897903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
907903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
910b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang    public static Dialog getSelectAccountDialog(Activity activity, int resId,
927903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnClickListener onClickListener) {
930b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        return getSelectAccountDialog(activity, resId, onClickListener, null);
947903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
957903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
967903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    /**
977903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     * When OnClickListener or OnCancelListener is null, uses a default listener.
987903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
997903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     */
1000b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang    public static Dialog getSelectAccountDialog(Activity activity, int resId,
1017903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnClickListener onClickListener,
1027903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnCancelListener onCancelListener) {
1030b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
1047903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
1057903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1067903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
1077903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1087903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        // Assume accountList.size() > 1
1097903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1107903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        // Wrap our context to inflate list items using correct theme
1117903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final Context dialogContext = new ContextThemeWrapper(
1120b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang                activity, android.R.style.Theme_Light);
1137903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
1147903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1157903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final ArrayAdapter<AccountWithDataSet> accountAdapter =
116d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang            new ArrayAdapter<AccountWithDataSet>(
117b7eecdd9a1276b721bb9644a0762dfd969e52709Walter Jang                    activity, R.layout.account_selector_list_item_condensed, writableAccountList) {
1187903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            @Override
1197903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            public View getView(int position, View convertView, ViewGroup parent) {
1207903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                if (convertView == null) {
1217903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                    convertView = dialogInflater.inflate(
122d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                            R.layout.account_selector_list_item_condensed,
1237903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                            parent, false);
1247903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                }
1257903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
126d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
127d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
128d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
1297903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1307903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final AccountWithDataSet account = this.getItem(position);
1317903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final AccountType accountType = accountTypes.getAccountType(
1327903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                        account.type, account.dataSet);
1337903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final Context context = getContext();
1347903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
135d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                text1.setText(accountType.getDisplayLabel(context));
136d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                text2.setText(account.name);
137d80e3bbb55a9cc093affc15b1d4ff5e7262fa4adWenyi Wang                icon.setImageDrawable(accountType.getDisplayIcon(getContext()));
1387903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1397903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                return convertView;
1407903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
1417903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        };
1427903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1437903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (onClickListener == null) {
1447903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            AccountSelectedListener accountSelectedListener =
1450b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang                new AccountSelectedListener(activity, writableAccountList, resId);
1467903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            onClickListener = accountSelectedListener;
1477903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1487903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (onCancelListener == null) {
1497903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            onCancelListener = new DialogInterface.OnCancelListener() {
1507903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                public void onCancel(DialogInterface dialog) {
1517903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                    dialog.dismiss();
1527903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                }
1537903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            };
1547903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1550b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        return new AlertDialog.Builder(activity)
1567903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setTitle(R.string.dialog_new_contact_account)
1577903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setSingleChoiceItems(accountAdapter, 0, onClickListener)
1587903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setOnCancelListener(onCancelListener)
1597903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .create();
1607903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1617903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
162b49ff73fbeb64a5aeffbe26bbdd015031e288c79Walter Jang   public static void doImport(Activity activity, int resId, AccountWithDataSet account,
1632f3fd5f7b5ee3ef74362509ef596063cea122f89Jay Shrauner            int subscriptionId) {
164b1361f9f379001fd27511c80655c52dd70a15122Sailesh Nepal        if (resId == R.string.import_from_sim) {
165b7eecdd9a1276b721bb9644a0762dfd969e52709Walter Jang            doImportFromSim(activity, account, subscriptionId);
166b1361f9f379001fd27511c80655c52dd70a15122Sailesh Nepal        } else if (resId == R.string.import_from_vcf_file) {
167b7eecdd9a1276b721bb9644a0762dfd969e52709Walter Jang            doImportFromVcfFile(activity, account);
1687903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1697903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1707903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1719524fd1d86a83d323b2c5fc7a57667906ced827cBrian Attwell    public static void doImportFromSim(Context context, AccountWithDataSet account,
1722f3fd5f7b5ee3ef74362509ef596063cea122f89Jay Shrauner            int subscriptionId) {
1737903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        Intent importIntent = new Intent(Intent.ACTION_VIEW);
1747903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        importIntent.setType("vnd.android.cursor.item/sim-contact");
1757903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (account != null) {
1767903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_name", account.name);
1777903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_type", account.type);
1787903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("data_set", account.dataSet);
1797903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1802f3fd5f7b5ee3ef74362509ef596063cea122f89Jay Shrauner        importIntent.putExtra("subscription_id", (Integer) subscriptionId);
1817903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
1826084957f19cf42bc7ccbbefaabfd0f96ef376587Brian Attwell        context.startActivity(importIntent);
1837903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1847903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1854718349e14c08e21f78e48e306a99d1b0fb5cd2eWalter Jang    public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) {
1860b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        Intent importIntent = new Intent(activity, ImportVCardActivity.class);
1877903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (account != null) {
1887903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_name", account.name);
1897903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_type", account.type);
1907903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("data_set", account.dataSet);
1917903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1927903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1937903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (mVCardShare) {
1947903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.setAction(Intent.ACTION_VIEW);
1957903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.setData(mPath);
1967903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1977903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        mVCardShare = false;
1987903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        mPath = null;
1990b6c338eb238a4ca061372874e5213205d5ea1b1Walter Jang        activity.startActivityForResult(importIntent, 0);
2007903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
2017903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng}
202