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