Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 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.common.vcard; |
| 17 | |
Brian Attwell | c2e912c | 2014-10-27 12:29:44 -0700 | [diff] [blame] | 18 | import android.app.Activity; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 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.common.R; |
| 26 | import com.android.contacts.common.model.AccountTypeManager; |
| 27 | import com.android.contacts.common.model.account.AccountWithDataSet; |
| 28 | import com.android.contacts.common.util.AccountSelectionUtil; |
| 29 | |
| 30 | import java.util.List; |
| 31 | |
Brian Attwell | c2e912c | 2014-10-27 12:29:44 -0700 | [diff] [blame] | 32 | public class SelectAccountActivity extends Activity { |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 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 { |
| 41 | public void onClick(DialogInterface dialog, int which) { |
| 42 | finish(); |
| 43 | } |
| 44 | public void onCancel(DialogInterface dialog) { |
| 45 | finish(); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private AccountSelectionUtil.AccountSelectedListener mAccountSelectionListener; |
| 50 | |
| 51 | @Override |
| 52 | protected void onCreate(Bundle bundle) { |
| 53 | super.onCreate(bundle); |
| 54 | |
| 55 | // There's three possibilities: |
| 56 | // - more than one accounts -> ask the user |
| 57 | // - just one account -> use the account without asking the user |
| 58 | // - no account -> use phone-local storage without asking the user |
Walter Jang | 6321e5a | 2015-07-14 10:56:03 -0700 | [diff] [blame] | 59 | final int resId = R.string.import_from_vcf_file; |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 60 | final AccountTypeManager accountTypes = AccountTypeManager.getInstance(this); |
| 61 | final List<AccountWithDataSet> accountList = accountTypes.getAccounts(true); |
| 62 | if (accountList.size() == 0) { |
| 63 | Log.w(LOG_TAG, "Account does not exist"); |
| 64 | finish(); |
| 65 | return; |
| 66 | } else if (accountList.size() == 1) { |
| 67 | final AccountWithDataSet account = accountList.get(0); |
| 68 | final Intent intent = new Intent(); |
| 69 | intent.putExtra(ACCOUNT_NAME, account.name); |
| 70 | intent.putExtra(ACCOUNT_TYPE, account.type); |
| 71 | intent.putExtra(DATA_SET, account.dataSet); |
| 72 | setResult(RESULT_OK, intent); |
| 73 | finish(); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | Log.i(LOG_TAG, "The number of available accounts: " + accountList.size()); |
| 78 | |
| 79 | // Multiple accounts. Let users to select one. |
| 80 | mAccountSelectionListener = |
| 81 | new AccountSelectionUtil.AccountSelectedListener( |
| 82 | this, accountList, resId) { |
| 83 | @Override |
| 84 | public void onClick(DialogInterface dialog, int which) { |
| 85 | dialog.dismiss(); |
| 86 | final AccountWithDataSet account = mAccountList.get(which); |
| 87 | final Intent intent = new Intent(); |
| 88 | intent.putExtra(ACCOUNT_NAME, account.name); |
| 89 | intent.putExtra(ACCOUNT_TYPE, account.type); |
| 90 | intent.putExtra(DATA_SET, account.dataSet); |
| 91 | setResult(RESULT_OK, intent); |
| 92 | finish(); |
| 93 | } |
| 94 | }; |
| 95 | showDialog(resId); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | protected Dialog onCreateDialog(int resId, Bundle bundle) { |
Sailesh Nepal | b4a522e | 2016-02-20 21:17:32 -0800 | [diff] [blame] | 101 | if (resId == R.string.import_from_vcf_file) { |
| 102 | if (mAccountSelectionListener == null) { |
| 103 | throw new NullPointerException( |
| 104 | "mAccountSelectionListener must not be null."); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 105 | } |
Sailesh Nepal | b4a522e | 2016-02-20 21:17:32 -0800 | [diff] [blame] | 106 | return AccountSelectionUtil.getSelectAccountDialog(this, resId, |
| 107 | mAccountSelectionListener, |
| 108 | new CancelListener()); |
Chiao Cheng | d80c434 | 2012-12-03 17:15:58 -0800 | [diff] [blame] | 109 | } |
| 110 | return super.onCreateDialog(resId, bundle); |
| 111 | } |
| 112 | } |