1 /* 2 * Copyright (C) 2010 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 package com.android.contacts.vcard; 17 18 import android.app.Activity; 19 import android.app.Dialog; 20 import android.content.DialogInterface; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.util.Log; 24 25 import com.android.contacts.R; 26 import com.android.contacts.model.AccountTypeManager; 27 import com.android.contacts.model.account.AccountWithDataSet; 28 import com.android.contacts.util.AccountSelectionUtil; 29 30 import java.util.List; 31 32 public class SelectAccountActivity extends Activity { 33 private static final String LOG_TAG = "SelectAccountActivity"; 34 35 public static final String ACCOUNT_NAME = "account_name"; 36 public static final String ACCOUNT_TYPE = "account_type"; 37 public static final String DATA_SET = "data_set"; 38 39 private class CancelListener 40 implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener { onClick(DialogInterface dialog, int which)41 public void onClick(DialogInterface dialog, int which) { 42 finish(); 43 } onCancel(DialogInterface dialog)44 public void onCancel(DialogInterface dialog) { 45 finish(); 46 } 47 } 48 49 private AccountSelectionUtil.AccountSelectedListener mAccountSelectionListener; 50 51 @Override onCreate(Bundle bundle)52 protected void onCreate(Bundle bundle) { 53 super.onCreate(bundle); 54 55 getWindow().setHideOverlayWindows(true); 56 57 // There's three possibilities: 58 // - more than one accounts -> ask the user 59 // - just one account -> use the account without asking the user 60 // - no account -> use phone-local storage without asking the user 61 final int resId = R.string.import_from_vcf_file; 62 final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); 63 final List<AccountWithDataSet> accountList = accountTypes.blockForWritableAccounts(); 64 if (accountList.size() == 0) { 65 Log.w(LOG_TAG, "Account does not exist"); 66 finish(); 67 return; 68 } else if (accountList.size() == 1) { 69 final AccountWithDataSet account = accountList.get(0); 70 final Intent intent = new Intent(); 71 intent.putExtra(ACCOUNT_NAME, account.name); 72 intent.putExtra(ACCOUNT_TYPE, account.type); 73 intent.putExtra(DATA_SET, account.dataSet); 74 setResult(RESULT_OK, intent); 75 finish(); 76 return; 77 } 78 79 Log.i(LOG_TAG, "The number of available accounts: " + accountList.size()); 80 81 // Multiple accounts. Let users to select one. 82 mAccountSelectionListener = 83 new AccountSelectionUtil.AccountSelectedListener( 84 this, accountList, resId) { 85 @Override 86 public void onClick(DialogInterface dialog, int which) { 87 dialog.dismiss(); 88 final AccountWithDataSet account = mAccountList.get(which); 89 final Intent intent = new Intent(); 90 intent.putExtra(ACCOUNT_NAME, account.name); 91 intent.putExtra(ACCOUNT_TYPE, account.type); 92 intent.putExtra(DATA_SET, account.dataSet); 93 setResult(RESULT_OK, intent); 94 finish(); 95 } 96 }; 97 showDialog(resId); 98 return; 99 } 100 101 @Override onCreateDialog(int resId, Bundle bundle)102 protected Dialog onCreateDialog(int resId, Bundle bundle) { 103 if (resId == R.string.import_from_vcf_file) { 104 if (mAccountSelectionListener == null) { 105 throw new NullPointerException( 106 "mAccountSelectionListener must not be null."); 107 } 108 return AccountSelectionUtil.getSelectAccountDialog(this, resId, 109 mAccountSelectionListener, 110 new CancelListener()); 111 } 112 return super.onCreateDialog(resId, bundle); 113 } 114 } 115