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