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.ImportVCardActivity;
     20 import com.android.contacts.R;
     21 import com.android.contacts.model.ContactsSource;
     22 import com.android.contacts.model.GoogleSource;
     23 import com.android.contacts.model.Sources;
     24 
     25 import android.accounts.Account;
     26 import android.app.AlertDialog;
     27 import android.app.Dialog;
     28 import android.content.Context;
     29 import android.content.DialogInterface;
     30 import android.content.Intent;
     31 import android.util.Log;
     32 import android.view.ContextThemeWrapper;
     33 import android.view.LayoutInflater;
     34 import android.view.View;
     35 import android.view.ViewGroup;
     36 import android.widget.ArrayAdapter;
     37 import android.widget.TextView;
     38 import android.net.Uri;
     39 
     40 import java.util.List;
     41 
     42 /**
     43  * Utility class for selectiong an Account for importing contact(s)
     44  */
     45 public class AccountSelectionUtil {
     46     // TODO: maybe useful for EditContactActivity.java...
     47     private static final String LOG_TAG = "AccountSelectionUtil";
     48 
     49     public static boolean mVCardShare = false;
     50 
     51     public static Uri mPath;
     52 
     53     public static class AccountSelectedListener
     54             implements DialogInterface.OnClickListener {
     55 
     56         final private Context mContext;
     57         final private int mResId;
     58 
     59         final protected List<Account> mAccountList;
     60 
     61         public AccountSelectedListener(Context context, List<Account> accountList, int resId) {
     62             if (accountList == null || accountList.size() == 0) {
     63                 Log.e(LOG_TAG, "The size of Account list is 0.");
     64             }
     65             mContext = context;
     66             mAccountList = accountList;
     67             mResId = resId;
     68         }
     69 
     70         public void onClick(DialogInterface dialog, int which) {
     71             dialog.dismiss();
     72             doImport(mContext, mResId, mAccountList.get(which));
     73         }
     74     }
     75 
     76     public static Dialog getSelectAccountDialog(Context context, int resId) {
     77         return getSelectAccountDialog(context, resId, null, null);
     78     }
     79 
     80     public static Dialog getSelectAccountDialog(Context context, int resId,
     81             DialogInterface.OnClickListener onClickListener) {
     82         return getSelectAccountDialog(context, resId, onClickListener, null);
     83     }
     84 
     85     /**
     86      * When OnClickListener or OnCancelListener is null, uses a default listener.
     87      * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
     88      */
     89     public static Dialog getSelectAccountDialog(Context context, int resId,
     90             DialogInterface.OnClickListener onClickListener,
     91             DialogInterface.OnCancelListener onCancelListener) {
     92         final Sources sources = Sources.getInstance(context);
     93         final List<Account> writableAccountList = sources.getAccounts(true);
     94 
     95         // Assume accountList.size() > 1
     96 
     97         // Wrap our context to inflate list items using correct theme
     98         final Context dialogContext = new ContextThemeWrapper(
     99                 context, android.R.style.Theme_Light);
    100         final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
    101                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    102         final ArrayAdapter<Account> accountAdapter =
    103             new ArrayAdapter<Account>(context, android.R.layout.simple_list_item_2,
    104                     writableAccountList) {
    105 
    106             @Override
    107             public View getView(int position, View convertView, ViewGroup parent) {
    108                 if (convertView == null) {
    109                     convertView = dialogInflater.inflate(
    110                             android.R.layout.simple_list_item_2,
    111                             parent, false);
    112                 }
    113 
    114                 // TODO: show icon along with title
    115                 final TextView text1 =
    116                         (TextView)convertView.findViewById(android.R.id.text1);
    117                 final TextView text2 =
    118                         (TextView)convertView.findViewById(android.R.id.text2);
    119 
    120                 final Account account = this.getItem(position);
    121                 final ContactsSource source =
    122                     sources.getInflatedSource(account.type,
    123                             ContactsSource.LEVEL_SUMMARY);
    124                 final Context context = getContext();
    125 
    126                 text1.setText(account.name);
    127                 text2.setText(source.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, Account 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, Account account) {
    166         if (account != null) {
    167             GoogleSource.createMyContactsIfNotExist(account, context);
    168         }
    169 
    170         Intent importIntent = new Intent(Intent.ACTION_VIEW);
    171         importIntent.setType("vnd.android.cursor.item/sim-contact");
    172         if (account != null) {
    173             importIntent.putExtra("account_name", account.name);
    174             importIntent.putExtra("account_type", account.type);
    175         }
    176         importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
    177         context.startActivity(importIntent);
    178     }
    179 
    180     public static void doImportFromSdCard(Context context, Account account) {
    181         if (account != null) {
    182             GoogleSource.createMyContactsIfNotExist(account, context);
    183         }
    184 
    185         Intent importIntent = new Intent(context, ImportVCardActivity.class);
    186         if (account != null) {
    187             importIntent.putExtra("account_name", account.name);
    188             importIntent.putExtra("account_type", account.type);
    189         }
    190 
    191         if (mVCardShare) {
    192             importIntent.setAction(Intent.ACTION_VIEW);
    193             importIntent.setData(mPath);
    194         }
    195         mVCardShare = false;
    196         mPath = null;
    197         context.startActivity(importIntent);
    198     }
    199 }
    200