Add SelectAccountActivity and make vCard importer use it.

Change-Id: Id7b39c82b506e90775bd4e0fada0045c11fccfd1
diff --git a/src/com/android/contacts/SelectAccountActivity.java b/src/com/android/contacts/SelectAccountActivity.java
new file mode 100644
index 0000000..cef00e0
--- /dev/null
+++ b/src/com/android/contacts/SelectAccountActivity.java
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.contacts;
+
+import android.accounts.Account;
+import android.app.Activity;
+import android.app.Dialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+import com.android.contacts.model.Sources;
+import com.android.contacts.util.AccountSelectionUtil;
+
+import java.util.List;
+
+public class SelectAccountActivity extends Activity {
+    private static final String LOG_TAG = "SelectAccountActivity";
+
+    /* package */ static final String ACCOUNT_NAME = "account_name";
+    /* package */ static final String ACCOUNT_TYPE = "account_type";
+
+    private class CancelListener
+            implements DialogInterface.OnClickListener, DialogInterface.OnCancelListener {
+        public void onClick(DialogInterface dialog, int which) {
+            finish();
+        }
+        public void onCancel(DialogInterface dialog) {
+            finish();
+        }
+    }
+
+    private AccountSelectionUtil.AccountSelectedListener mAccountSelectionListener;
+
+    @Override
+    protected void onCreate(Bundle bundle) {
+        super.onCreate(bundle);
+
+        // There's three possibilities:
+        // - more than one accounts -> ask the user
+        // - just one account -> use the account without asking the user
+        // - no account -> use phone-local storage without asking the user
+        final int resId = R.string.import_from_sdcard;
+        final Sources sources = Sources.getInstance(this);
+        final List<Account> accountList = sources.getAccounts(true);
+        if (accountList.size() == 0) {
+            Log.w(LOG_TAG, "Account does not exist");
+            finish();
+        } else if (accountList.size() == 1) {
+            final Account account = accountList.get(0);
+            final Intent intent = new Intent();
+            intent.putExtra(ACCOUNT_NAME, account.name);
+            intent.putExtra(ACCOUNT_TYPE, account.type);
+            setResult(RESULT_OK, intent);
+            finish();
+        }
+
+        // Multiple accounts. Let users to select one.
+        mAccountSelectionListener =
+                new AccountSelectionUtil.AccountSelectedListener(
+                        this, accountList, resId) {
+                    @Override
+                    public void onClick(DialogInterface dialog, int which) {
+                        dialog.dismiss();
+                        final Account account = mAccountList.get(which);
+                        final Intent intent = new Intent();
+                        intent.putExtra(ACCOUNT_NAME, account.name);
+                        intent.putExtra(ACCOUNT_TYPE, account.type);
+                        setResult(RESULT_OK, intent);
+                        finish();
+                    }
+                };
+        showDialog(resId);
+        return;
+    }
+
+    @Override
+    protected Dialog onCreateDialog(int resId, Bundle bundle) {
+        switch (resId) {
+            case R.string.import_from_sdcard: {
+                if (mAccountSelectionListener == null) {
+                    throw new NullPointerException(
+                            "mAccountSelectionListener must not be null.");
+                }
+                return AccountSelectionUtil.getSelectAccountDialog(this, resId,
+                        mAccountSelectionListener,
+                        new CancelListener());
+            }
+        }
+        return super.onCreateDialog(resId, bundle);
+    }
+}