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
197903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.app.AlertDialog;
207903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.app.Dialog;
217903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.Context;
227903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.DialogInterface;
237903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.content.Intent;
247903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.net.Uri;
257903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.util.Log;
267903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.ContextThemeWrapper;
277903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.LayoutInflater;
287903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.View;
297903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.view.ViewGroup;
307903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.widget.ArrayAdapter;
317903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport android.widget.TextView;
327903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
337903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.R;
347903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.AccountTypeManager;
357903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.account.AccountType;
367903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.model.account.AccountWithDataSet;
377903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport com.android.contacts.common.vcard.ImportVCardActivity;
387903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
397903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengimport java.util.List;
407903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
417903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng/**
427903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng * Utility class for selectiong an Account for importing contact(s)
437903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng */
447903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Chengpublic class AccountSelectionUtil {
457903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    // TODO: maybe useful for EditContactActivity.java...
467903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    private static final String LOG_TAG = "AccountSelectionUtil";
477903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
487903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static boolean mVCardShare = false;
497903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
507903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static Uri mPath;
517903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
527903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static class AccountSelectedListener
537903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            implements DialogInterface.OnClickListener {
547903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
557903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final private Context mContext;
567903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final private int mResId;
577903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
587903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final protected List<AccountWithDataSet> mAccountList;
597903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
607903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        public AccountSelectedListener(Context context, List<AccountWithDataSet> accountList,
617903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                int resId) {
627903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            if (accountList == null || accountList.size() == 0) {
637903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                Log.e(LOG_TAG, "The size of Account list is 0.");
647903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
657903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            mContext = context;
667903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            mAccountList = accountList;
677903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            mResId = resId;
687903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
697903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
707903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        public void onClick(DialogInterface dialog, int which) {
717903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            dialog.dismiss();
727903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            doImport(mContext, mResId, mAccountList.get(which));
737903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
747903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
757903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
767903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static Dialog getSelectAccountDialog(Context context, int resId) {
777903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        return getSelectAccountDialog(context, resId, null, null);
787903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
797903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
807903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static Dialog getSelectAccountDialog(Context context, int resId,
817903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnClickListener onClickListener) {
827903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        return getSelectAccountDialog(context, resId, onClickListener, null);
837903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
847903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
857903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    /**
867903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     * When OnClickListener or OnCancelListener is null, uses a default listener.
877903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
887903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng     */
897903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static Dialog getSelectAccountDialog(Context context, int resId,
907903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnClickListener onClickListener,
917903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            DialogInterface.OnCancelListener onCancelListener) {
927903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context);
937903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
947903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
957903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
967903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
977903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        // Assume accountList.size() > 1
987903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
997903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        // Wrap our context to inflate list items using correct theme
1007903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final Context dialogContext = new ContextThemeWrapper(
1017903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                context, android.R.style.Theme_Light);
1027903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
1037903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1047903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        final ArrayAdapter<AccountWithDataSet> accountAdapter =
1057903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            new ArrayAdapter<AccountWithDataSet>(context, android.R.layout.simple_list_item_2,
1067903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                    writableAccountList) {
1077903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1087903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            @Override
1097903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            public View getView(int position, View convertView, ViewGroup parent) {
1107903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                if (convertView == null) {
1117903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                    convertView = dialogInflater.inflate(
1127903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                            android.R.layout.simple_list_item_2,
1137903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                            parent, false);
1147903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                }
1157903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1167903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                // TODO: show icon along with title
1177903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final TextView text1 =
1187903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                        (TextView)convertView.findViewById(android.R.id.text1);
1197903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final TextView text2 =
1207903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                        (TextView)convertView.findViewById(android.R.id.text2);
1217903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1227903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final AccountWithDataSet account = this.getItem(position);
1237903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final AccountType accountType = accountTypes.getAccountType(
1247903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                        account.type, account.dataSet);
1257903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                final Context context = getContext();
1267903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1277903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                text1.setText(account.name);
1287903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                text2.setText(accountType.getDisplayLabel(context));
1297903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1307903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                return convertView;
1317903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
1327903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        };
1337903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1347903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (onClickListener == null) {
1357903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            AccountSelectedListener accountSelectedListener =
1367903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                new AccountSelectedListener(context, writableAccountList, resId);
1377903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            onClickListener = accountSelectedListener;
1387903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1397903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (onCancelListener == null) {
1407903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            onCancelListener = new DialogInterface.OnCancelListener() {
1417903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                public void onCancel(DialogInterface dialog) {
1427903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                    dialog.dismiss();
1437903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                }
1447903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            };
1457903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1467903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        return new AlertDialog.Builder(context)
1477903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setTitle(R.string.dialog_new_contact_account)
1487903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setSingleChoiceItems(accountAdapter, 0, onClickListener)
1497903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .setOnCancelListener(onCancelListener)
1507903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            .create();
1517903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1527903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1537903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static void doImport(Context context, int resId, AccountWithDataSet account) {
1547903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        switch (resId) {
1557903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            case R.string.import_from_sim: {
1567903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                doImportFromSim(context, account);
1577903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                break;
1587903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
1597903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            case R.string.import_from_sdcard: {
1607903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                doImportFromSdCard(context, account);
1617903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng                break;
1627903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            }
1637903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1647903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1657903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1667903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static void doImportFromSim(Context context, AccountWithDataSet account) {
1677903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        Intent importIntent = new Intent(Intent.ACTION_VIEW);
1687903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        importIntent.setType("vnd.android.cursor.item/sim-contact");
1697903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (account != null) {
1707903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_name", account.name);
1717903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_type", account.type);
1727903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("data_set", account.dataSet);
1737903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1747903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
1757903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        context.startActivity(importIntent);
1767903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1777903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1787903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    public static void doImportFromSdCard(Context context, AccountWithDataSet account) {
1797903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        Intent importIntent = new Intent(context, ImportVCardActivity.class);
1807903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (account != null) {
1817903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_name", account.name);
1827903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("account_type", account.type);
1837903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.putExtra("data_set", account.dataSet);
1847903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1857903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng
1867903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        if (mVCardShare) {
1877903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.setAction(Intent.ACTION_VIEW);
1887903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng            importIntent.setData(mPath);
1897903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        }
1907903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        mVCardShare = false;
1917903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        mPath = null;
1927903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng        context.startActivity(importIntent);
1937903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng    }
1947903d2473e1120e32fa5380a7d7532d0a21e2180Chiao Cheng}
195