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 android.app.Activity;
20 import android.app.AlertDialog;
21 import android.app.Dialog;
22 import android.content.Context;
23 import android.content.DialogInterface;
24 import android.content.Intent;
25 import android.net.Uri;
26 import android.util.Log;
27 import android.view.ContextThemeWrapper;
28 import android.view.LayoutInflater;
29 import android.view.View;
30 import android.view.ViewGroup;
31 import android.widget.ArrayAdapter;
32 import android.widget.ImageView;
33 import android.widget.TextView;
34 
35 import com.android.contacts.R;
36 import com.android.contacts.model.AccountTypeManager;
37 import com.android.contacts.model.account.AccountType;
38 import com.android.contacts.model.account.AccountWithDataSet;
39 import com.android.contacts.vcard.ImportVCardActivity;
40 
41 import java.util.List;
42 
43 /**
44  * Utility class for selecting an Account for importing contact(s)
45  */
46 public class AccountSelectionUtil {
47     // TODO: maybe useful for EditContactActivity.java...
48     private static final String LOG_TAG = "AccountSelectionUtil";
49 
50     public static boolean mVCardShare = false;
51 
52     public static Uri mPath;
53 
54     public static class AccountSelectedListener
55             implements DialogInterface.OnClickListener {
56 
57         final private Activity mActivity;
58         final private int mResId;
59         final private int mSubscriptionId;
60 
61         final protected List<AccountWithDataSet> mAccountList;
62 
AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, int resId, int subscriptionId)63         public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
64                 int resId, int subscriptionId) {
65             if (accountList == null || accountList.size() == 0) {
66                 Log.e(LOG_TAG, "The size of Account list is 0.");
67             }
68             mActivity = activity;
69             mAccountList = accountList;
70             mResId = resId;
71             mSubscriptionId = subscriptionId;
72         }
73 
AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList, int resId)74         public AccountSelectedListener(Activity activity, List<AccountWithDataSet> accountList,
75                 int resId) {
76             // Subscription id is only needed for importing from SIM card. We can safely ignore
77             // its value for SD card importing.
78             this(activity, accountList, resId, /* subscriptionId = */ -1);
79         }
80 
onClick(DialogInterface dialog, int which)81         public void onClick(DialogInterface dialog, int which) {
82             dialog.dismiss();
83             doImport(mActivity, mResId, mAccountList.get(which), mSubscriptionId);
84         }
85     }
86 
87     /**
88      * When OnClickListener or OnCancelListener is null, uses a default listener.
89      * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
90      */
getSelectAccountDialog(Activity activity, int resId, DialogInterface.OnClickListener onClickListener, DialogInterface.OnCancelListener onCancelListener)91     public static Dialog getSelectAccountDialog(Activity activity, int resId,
92             DialogInterface.OnClickListener onClickListener,
93             DialogInterface.OnCancelListener onCancelListener) {
94         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
95         final List<AccountWithDataSet> writableAccountList =
96                 accountTypes.blockForWritableAccounts();
97 
98         Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
99 
100         // Assume accountList.size() > 1
101 
102         // Wrap our context to inflate list items using correct theme
103         final Context dialogContext = new ContextThemeWrapper(
104                 activity, android.R.style.Theme_Light);
105         final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
106                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
107         final ArrayAdapter<AccountWithDataSet> accountAdapter =
108             new ArrayAdapter<AccountWithDataSet>(
109                     activity, R.layout.account_selector_list_item_condensed, writableAccountList) {
110             @Override
111             public View getView(int position, View convertView, ViewGroup parent) {
112                 if (convertView == null) {
113                     convertView = dialogInflater.inflate(
114                             R.layout.account_selector_list_item_condensed,
115                             parent, false);
116                 }
117 
118                 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
119                 final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
120                 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
121 
122                 final AccountWithDataSet account = this.getItem(position);
123                 final AccountType accountType = accountTypes.getAccountType(
124                         account.type, account.dataSet);
125                 final Context context = getContext();
126 
127                 text1.setText(accountType.getDisplayLabel(context));
128                 text2.setText(account.name);
129                 icon.setImageDrawable(accountType.getDisplayIcon(getContext()));
130 
131                 return convertView;
132             }
133         };
134 
135         if (onClickListener == null) {
136             AccountSelectedListener accountSelectedListener =
137                 new AccountSelectedListener(activity, writableAccountList, resId);
138             onClickListener = accountSelectedListener;
139         }
140         if (onCancelListener == null) {
141             onCancelListener = new DialogInterface.OnCancelListener() {
142                 public void onCancel(DialogInterface dialog) {
143                     dialog.dismiss();
144                 }
145             };
146         }
147         final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
148         final TextView title = (TextView) View.inflate(activity, R.layout.dialog_title, null);
149         title.setText(R.string.dialog_new_contact_account);
150         builder.setCustomTitle(title);
151         builder.setSingleChoiceItems(accountAdapter, 0, onClickListener);
152         builder.setOnCancelListener(onCancelListener);
153         final AlertDialog result = builder.create();
154         return result;
155     }
156 
doImport(Activity activity, int resId, AccountWithDataSet account, int subscriptionId)157     public static void doImport(Activity activity, int resId, AccountWithDataSet account,
158             int subscriptionId) {
159         if (resId == R.string.import_from_sim) {
160             doImportFromSim(activity, account, subscriptionId);
161         } else if (resId == R.string.import_from_vcf_file) {
162             doImportFromVcfFile(activity, account);
163         }
164     }
165 
doImportFromSim(Context context, AccountWithDataSet account, int subscriptionId)166     public static void doImportFromSim(Context context, AccountWithDataSet account,
167             int subscriptionId) {
168         Intent importIntent = new Intent(Intent.ACTION_VIEW);
169         importIntent.setType("vnd.android.cursor.item/sim-contact");
170         if (account != null) {
171             importIntent.putExtra("account_name", account.name);
172             importIntent.putExtra("account_type", account.type);
173             importIntent.putExtra("data_set", account.dataSet);
174         }
175         importIntent.putExtra("subscription_id", (Integer) subscriptionId);
176         importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
177         context.startActivity(importIntent);
178     }
179 
doImportFromVcfFile(Activity activity, AccountWithDataSet account)180     public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) {
181         Intent importIntent = new Intent(activity, ImportVCardActivity.class);
182         if (account != null) {
183             importIntent.putExtra("account_name", account.name);
184             importIntent.putExtra("account_type", account.type);
185             importIntent.putExtra("data_set", account.dataSet);
186         }
187 
188         if (mVCardShare) {
189             importIntent.setAction(Intent.ACTION_VIEW);
190             importIntent.setData(mPath);
191         }
192         mVCardShare = false;
193         mPath = null;
194         activity.startActivityForResult(importIntent, 0);
195     }
196 }
197