• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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.common.R;
36 import com.android.contacts.common.model.AccountTypeManager;
37 import com.android.contacts.common.model.account.AccountType;
38 import com.android.contacts.common.model.account.AccountWithDataSet;
39 import com.android.contacts.common.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 
getSelectAccountDialog(Activity activity, int resId)87     public static Dialog getSelectAccountDialog(Activity activity, int resId) {
88         return getSelectAccountDialog(activity, resId, null, null);
89     }
90 
getSelectAccountDialog(Activity activity, int resId, DialogInterface.OnClickListener onClickListener)91     public static Dialog getSelectAccountDialog(Activity activity, int resId,
92             DialogInterface.OnClickListener onClickListener) {
93         return getSelectAccountDialog(activity, resId, onClickListener, null);
94     }
95 
96     /**
97      * When OnClickListener or OnCancelListener is null, uses a default listener.
98      * The default OnCancelListener just closes itself with {@link Dialog#dismiss()}.
99      */
getSelectAccountDialog(Activity activity, int resId, DialogInterface.OnClickListener onClickListener, DialogInterface.OnCancelListener onCancelListener)100     public static Dialog getSelectAccountDialog(Activity activity, int resId,
101             DialogInterface.OnClickListener onClickListener,
102             DialogInterface.OnCancelListener onCancelListener) {
103         final AccountTypeManager accountTypes = AccountTypeManager.getInstance(activity);
104         final List<AccountWithDataSet> writableAccountList = accountTypes.getAccounts(true);
105 
106         Log.i(LOG_TAG, "The number of available accounts: " + writableAccountList.size());
107 
108         // Assume accountList.size() > 1
109 
110         // Wrap our context to inflate list items using correct theme
111         final Context dialogContext = new ContextThemeWrapper(
112                 activity, android.R.style.Theme_Light);
113         final LayoutInflater dialogInflater = (LayoutInflater)dialogContext
114                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
115         final ArrayAdapter<AccountWithDataSet> accountAdapter =
116             new ArrayAdapter<AccountWithDataSet>(
117                     activity, R.layout.account_selector_list_item_condensed, writableAccountList) {
118             @Override
119             public View getView(int position, View convertView, ViewGroup parent) {
120                 if (convertView == null) {
121                     convertView = dialogInflater.inflate(
122                             R.layout.account_selector_list_item_condensed,
123                             parent, false);
124                 }
125 
126                 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
127                 final TextView text2 = (TextView) convertView.findViewById(android.R.id.text2);
128                 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
129 
130                 final AccountWithDataSet account = this.getItem(position);
131                 final AccountType accountType = accountTypes.getAccountType(
132                         account.type, account.dataSet);
133                 final Context context = getContext();
134 
135                 text1.setText(accountType.getDisplayLabel(context));
136                 text2.setText(account.name);
137                 icon.setImageDrawable(accountType.getDisplayIcon(getContext()));
138 
139                 return convertView;
140             }
141         };
142 
143         if (onClickListener == null) {
144             AccountSelectedListener accountSelectedListener =
145                 new AccountSelectedListener(activity, writableAccountList, resId);
146             onClickListener = accountSelectedListener;
147         }
148         if (onCancelListener == null) {
149             onCancelListener = new DialogInterface.OnCancelListener() {
150                 public void onCancel(DialogInterface dialog) {
151                     dialog.dismiss();
152                 }
153             };
154         }
155         return new AlertDialog.Builder(activity)
156             .setTitle(R.string.dialog_new_contact_account)
157             .setSingleChoiceItems(accountAdapter, 0, onClickListener)
158             .setOnCancelListener(onCancelListener)
159             .create();
160     }
161 
doImport(Activity activity, int resId, AccountWithDataSet account, int subscriptionId)162    public static void doImport(Activity activity, int resId, AccountWithDataSet account,
163             int subscriptionId) {
164         if (resId == R.string.import_from_sim) {
165             doImportFromSim(activity, account, subscriptionId);
166         } else if (resId == R.string.import_from_vcf_file) {
167             doImportFromVcfFile(activity, account);
168         }
169     }
170 
doImportFromSim(Context context, AccountWithDataSet account, int subscriptionId)171     public static void doImportFromSim(Context context, AccountWithDataSet account,
172             int subscriptionId) {
173         Intent importIntent = new Intent(Intent.ACTION_VIEW);
174         importIntent.setType("vnd.android.cursor.item/sim-contact");
175         if (account != null) {
176             importIntent.putExtra("account_name", account.name);
177             importIntent.putExtra("account_type", account.type);
178             importIntent.putExtra("data_set", account.dataSet);
179         }
180         importIntent.putExtra("subscription_id", (Integer) subscriptionId);
181         importIntent.setClassName("com.android.phone", "com.android.phone.SimContacts");
182         context.startActivity(importIntent);
183     }
184 
doImportFromVcfFile(Activity activity, AccountWithDataSet account)185     public static void doImportFromVcfFile(Activity activity, AccountWithDataSet account) {
186         Intent importIntent = new Intent(activity, ImportVCardActivity.class);
187         if (account != null) {
188             importIntent.putExtra("account_name", account.name);
189             importIntent.putExtra("account_type", account.type);
190             importIntent.putExtra("data_set", account.dataSet);
191         }
192 
193         if (mVCardShare) {
194             importIntent.setAction(Intent.ACTION_VIEW);
195             importIntent.setData(mPath);
196         }
197         mVCardShare = false;
198         mPath = null;
199         activity.startActivityForResult(importIntent, 0);
200     }
201 }
202