Show account selector in PopupMenu to be less intrusive
Also show an account icon in the dialog-based account-selector
Bug:3117457
Change-Id: I625a25d9aeb751aec0b8e9865be76ed2ecd916d1
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 0c5ee70..54f7939 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -18,11 +18,13 @@
package="com.android.contacts.tests">
<uses-permission android:name="android.permission.READ_CONTACTS" />
+ <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<application>
<uses-library android:name="android.test.runner" />
<meta-data android:name="com.android.contacts.iconset" android:resource="@xml/iconset" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
+ <uses-permission android:name="android.permission.GET_ACCOUNTS" />
<activity android:name=".allintents.AllIntentsActivity"
android:label="@string/contactsIntents"
diff --git a/tests/res/values/donottranslate_strings.xml b/tests/res/values/donottranslate_strings.xml
index 25e10b0..419ae98 100644
--- a/tests/res/values/donottranslate_strings.xml
+++ b/tests/res/values/donottranslate_strings.xml
@@ -71,6 +71,8 @@
<item>EDIT (legacy style uri)</item>
<item>EDIT (create new contact)</item>
<item>EDIT (create new contact with data)</item>
+ <item>EDIT (create new contact for account)</item>
+ <item>EDIT (create new contact for account with data)</item>
<item>EDIT (create new raw contact)</item>
<item>EDIT (create new legacy)</item>
diff --git a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
index 5283f5f..bf75ed1 100644
--- a/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
+++ b/tests/src/com/android/contacts/tests/allintents/AllIntentsActivity.java
@@ -21,6 +21,7 @@
import com.android.contacts.tests.R;
import com.google.android.collect.Lists;
+import android.accounts.Account;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.ComponentName;
@@ -53,7 +54,8 @@
* Useful for manual and scripted tests.
*/
@SuppressWarnings("deprecation")
-public class AllIntentsActivity extends ListActivity {
+public class AllIntentsActivity extends ListActivity
+ implements SelectAccountDialogFragment.Listener {
private static final String ANDROID_CONTACTS_PACKAGE = "com.android.contacts";
@@ -114,14 +116,16 @@
private static final int EDIT_LEGACY = 46;
private static final int EDIT_NEW_CONTACT = 47;
private static final int EDIT_NEW_CONTACT_WITH_DATA = 48;
- private static final int EDIT_NEW_RAW_CONTACT = 49;
- private static final int EDIT_NEW_LEGACY = 50;
+ private static final int EDIT_NEW_CONTACT_FOR_ACCOUNT = 49;
+ private static final int EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA = 50;
+ private static final int EDIT_NEW_RAW_CONTACT = 51;
+ private static final int EDIT_NEW_LEGACY = 52;
- private static final int VIEW_CONTACT = 51;
- private static final int VIEW_CONTACT_LOOKUP = 52;
- private static final int VIEW_CONTACT_LOOKUP_ID = 53;
- private static final int VIEW_RAW_CONTACT = 54;
- private static final int VIEW_LEGACY = 55;
+ private static final int VIEW_CONTACT = 53;
+ private static final int VIEW_CONTACT_LOOKUP = 54;
+ private static final int VIEW_CONTACT_LOOKUP_ID = 55;
+ private static final int VIEW_RAW_CONTACT = 56;
+ private static final int VIEW_LEGACY = 57;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -444,6 +448,13 @@
startActivity(intent);
break;
}
+ case EDIT_NEW_CONTACT_FOR_ACCOUNT:
+ case EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA: {
+ final SelectAccountDialogFragment dialog = new SelectAccountDialogFragment();
+ dialog.setArguments(SelectAccountDialogFragment.createBundle(position));
+ dialog.show(getFragmentManager(), SelectAccountDialogFragment.TAG);
+ break;
+ }
case EDIT_NEW_RAW_CONTACT: {
startActivity(new Intent(Intent.ACTION_INSERT, RawContacts.CONTENT_URI));
break;
@@ -576,4 +587,35 @@
return -1;
}
+
+ @Override
+ public void onAccountChosen(Account account, int tag) {
+ switch (tag) {
+ case EDIT_NEW_CONTACT_FOR_ACCOUNT: {
+ final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
+ intent.putExtra(Insert.ACCOUNT, account);
+ startActivity(intent);
+ break;
+ }
+ case EDIT_NEW_CONTACT_FOR_ACCOUNT_WITH_DATA: {
+ final Intent intent = new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI);
+
+ intent.putExtra(Insert.ACCOUNT, account);
+ ContentValues row1 = new ContentValues();
+ row1.put(Data.MIMETYPE, Organization.CONTENT_ITEM_TYPE);
+ row1.put(Organization.COMPANY, "Android");
+
+ ContentValues row2 = new ContentValues();
+ row2.put(Data.MIMETYPE, Email.CONTENT_ITEM_TYPE);
+ row2.put(Email.TYPE, Email.TYPE_CUSTOM);
+ row2.put(Email.LABEL, "Green Bot");
+ row2.put(Email.ADDRESS, "android@android.com");
+
+ intent.putParcelableArrayListExtra(Insert.DATA, Lists.newArrayList(row1, row2));
+
+ startActivity(intent);
+ break;
+ }
+ }
+ }
}
diff --git a/tests/src/com/android/contacts/tests/allintents/SelectAccountDialogFragment.java b/tests/src/com/android/contacts/tests/allintents/SelectAccountDialogFragment.java
new file mode 100644
index 0000000..e074a0b
--- /dev/null
+++ b/tests/src/com/android/contacts/tests/allintents/SelectAccountDialogFragment.java
@@ -0,0 +1,97 @@
+/*
+ * 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.tests.allintents;
+
+import android.accounts.Account;
+import android.accounts.AccountManager;
+import android.app.AlertDialog;
+import android.app.Dialog;
+import android.app.DialogFragment;
+import android.content.DialogInterface;
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.ArrayAdapter;
+import android.widget.TextView;
+
+/**
+ * Shows a dialog asking the user which account to chose.
+ * The result is passed back to the owning Activity
+ * Does not perform any action by itself.
+ */
+public class SelectAccountDialogFragment extends DialogFragment {
+ public static final String TAG = "SelectAccountDialogFragment";
+
+ private static final String EXTRA_TAG = "tag";
+
+ @Override
+ public Dialog onCreateDialog(Bundle savedInstanceState) {
+ final Bundle parameters = getArguments();
+
+ final Account[] accounts = AccountManager.get(getActivity()).getAccounts();
+
+ final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
+ final LayoutInflater inflater = LayoutInflater.from(builder.getContext());
+
+ final ArrayAdapter<Account> accountAdapter = new ArrayAdapter<Account>(builder.getContext(),
+ android.R.layout.simple_list_item_2, accounts) {
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ final View resultView = convertView == null
+ ? inflater.inflate(android.R.layout.simple_list_item_2, parent, false)
+ : convertView;
+
+ final TextView text1 = (TextView)resultView.findViewById(android.R.id.text1);
+ final TextView text2 = (TextView)resultView.findViewById(android.R.id.text2);
+
+ final Account account = getItem(position);
+
+ text1.setText("Name: " + account.name);
+ text2.setText("Type: " + account.type);
+
+ return resultView;
+ }
+ };
+
+ final DialogInterface.OnClickListener clickListener =
+ new DialogInterface.OnClickListener() {
+ @Override
+ public void onClick(DialogInterface dialog, int which) {
+ dialog.dismiss();
+
+ ((Listener) getActivity()).onAccountChosen(accountAdapter.getItem(which),
+ parameters.getInt(EXTRA_TAG));
+ }
+ };
+
+ builder.setTitle("Choose account to send to editor");
+ builder.setSingleChoiceItems(accountAdapter, 0, clickListener);
+ final AlertDialog result = builder.create();
+ return result;
+ }
+
+ public static Bundle createBundle(int tag) {
+ final Bundle result = new Bundle();
+ result.putInt(EXTRA_TAG, tag);
+ return result;
+ }
+
+ public interface Listener {
+ void onAccountChosen(Account account, int tag);
+ }
+}