Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.contacts.util;
     18 
     19 import com.android.contacts.R;
     20 import com.android.contacts.model.AccountType;
     21 import com.android.contacts.model.AccountTypeManager;
     22 import com.android.contacts.model.AccountWithDataSet;
     23 
     24 import android.app.AlertDialog;
     25 import android.app.Dialog;
     26 import android.content.Context;
     27 import android.content.DialogInterface;
     28 import android.content.Intent;
     29 import android.net.Uri;
     30 import android.util.Log;
     31 import android.view.ContextThemeWrapper;
     32 import android.view.LayoutInflater;
     33 import android.view.View;
     34 import android.view.ViewGroup;
     35 import android.widget.ArrayAdapter;
     36 import android.widget.TextView;
     37 
     38 import java.util.List;
     39 
     40 /**
     41  * Utility class for selectiong an Account for importing contact(s)
     42  */
     43 public class AccountSelectionUtil {
     44     // TODO: maybe useful for EditContactActivity.java...
     45     private static final String LOG_TAG = "AccountSelectionUtil";
     46 
     47     public static boolean mVCardShare = false;
     48 
     49     public static Uri mPath;
     50 
     51     public static class AccountSelectedListener
     52             implements DialogInterface.OnClickListener {
     53 
     54         final private Context mContext;
     55         final private int mResId;
     56 
     57         final protected List<AccountWithDataSet> mAccountList;
     58 
     59         public AccountSelectedListener(Context context, List<AccountWithDataSet> accountList,
     60                 int resId) {
     61             if (accountList == null || accountList.size() == 0) {
     62                 Log.e(LOG_TAG, "The size of Account list is 0.");
     63             }
     64             mContext = context;
     65             mAccountList = accountList;
     66             mResId = resId;
     67         }
     68 
     69         public void onClick(DialogInterface dialog, int which) {
     70             dialog.dismiss();
     71             doImport(mContext, mResId, mAccountList.get(which));
     72         }
     73     }
     74 
     75     public static Dialog getSelectAccountDialog(Context context, int resId) {
     76         return getSelectAccountDialog(context, resId, null, null);
     77     }
     78 
     79     public static Dialog getSelectAccountDialog(Context context, int resId,
     80             DialogInterface.OnClickListener onClickListener) {
     81         return getSelectAccountDialog(context, resId, onClickListener, null);
     82     }
     83 
     84     /**
     85      * When OnClickListener or OnCancelListener is null, uses a default listener.
     86      * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
     87      */
     88     public static Dialog getSelectAccountDialog(Context context, int resId,
     89             DialogInterface.OnClickListener onClickListener,
     90             DialogInterface.OnCancelListener onCancelListener) {
     91         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(context);
     92         final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
     93 
     94         Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
     95 
     96         // Assume accountList.size() > 1
     97 
     98         // Wrap our context to inflate list items using correct theme
     99         final Context dialogContext = new ContextThemeWrapper(
    100                 context, android.R.style.Theme_Light);
    101         final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
    102                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    103         final ArrayAdapter<AccountWithDataSet> accountAdapter =
    104             new ArrayAdapter<AccountWithDataSet>(context, android.R.layout.simple_list_item_2,
    105                     writableAccountList) {
    106 
    107             @Override
    108             public View getView(int position, View convertView, ViewGroup parent) {
    109                 if (convertView == null) {
    110                     convertView = dialogInflater.inflate(
    111                             android.R.layout.simple_list_item_2,
    112                             parent, false);
    113                 }
    114 
    115                 // TODO: show icon along with title
    116                 final TextView text1 =
    117                         (TextView)convertView.findViewById(android.R.id.text1);
    118                 final TextView text2 =
    119                         (TextView)convertView.findViewById(android.R.id.text2);
    120 
    121                 final AccountWithDataSet account = this.getItem(position);
    122                 final AccountType accountType = accountTypes.getAccountType(
    123                         account.type, account.dataSet);
    124                 final Context context = getContext();
    125 
    126                 text1.setText(account.name);
    127                 text2.setText(accountType.getDisplayLabel(context));
    128 
    129                 return convertView;
    130             }
    131         };
    132 
    133         if (onClickListener == null) {
    134             AccountSelectedListener accountSelectedListener =
    135                 new AccountSelectedListener(context, writableAccountList, resId);
    136             onClickListener = accountSelectedListener;
    137         }
    138         if (onCancelListener == null) {
    139             onCancelListener = new DialogInterface.OnCancelListener() {
    140                 public void onCancel(DialogInterface dialog) {
    141                     dialog.dismiss();
    142                 }
    143             };
    144         }
    145         return new AlertDialog.Builder(context)
    146             .setTitle(R.string.dialog_new_contact_account)
    147             .setSingleChoiceItems(accountAdapter, 0, onClickListener)
    148             .setOnCancelListener(onCancelListener)
    149             .create();
    150     }
    151 
    152     public static void doImport(Context context, int resId, AccountWithDataSet account) {
    153         switch (resId) {
    154             case R.string.import_from_sim: {
    155                 doImportFromSim(context, account);
    156                 break;
    157             }
    158             case R.string.import_from_sdcard: {
    159                 doImportFromSdCard(context, account);
    160                 break;
    161             }
    162         }
    163     }
    164 
    165     public static void doImportFromSim(Context context, AccountWithDataSet account) {
    166         Intent importIntent = new Intent(Intent.ACTION_VIEW);
    167         importIntent.setType("vnd.android.cursor.item/sim-contact");
    168         if (account != null) {
    169             importIntent.putExtra("account_name", account.name);
    170             importIntent.putExtra("account_type", account.type);
    171             importIntent.putExtra("data_set", account.dataSet);
    172         }
    173         importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
    174         context.startActivity(importIntent);
    175     }
    176 
    177     public static void doImportFromSdCard(Context context, AccountWithDataSet account) {
    178         Intent importIntent = new Intent(context,
    179                 com.android.contacts.vcard.ImportVCardActivity.class);
    180         if (account != null) {
    181             importIntent.putExtra("account_name", account.name);
    182             importIntent.putExtra("account_type", account.type);
    183             importIntent.putExtra("data_set", account.dataSet);
    184         }
    185 
    186         if (mVCardShare) {
    187             importIntent.setAction(Intent.ACTION_VIEW);
    188             importIntent.setData(mPath);
    189         }
    190         mVCardShare = false;
    191         mPath = null;
    192         context.startActivity(importIntent);
    193     }
    194 }
    195