summaryrefslogtreecommitdiffstats
path: root/core/java
diff options
context:
space:
mode:
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/accounts/AccountManager.java40
-rw-r--r--core/java/android/accounts/ChooseAccountTypeActivity.java220
-rw-r--r--core/java/android/accounts/ChooseTypeAndAccountActivity.java274
3 files changed, 534 insertions, 0 deletions
diff --git a/core/java/android/accounts/AccountManager.java b/core/java/android/accounts/AccountManager.java
index dbf4de8..530ecf1 100644
--- a/core/java/android/accounts/AccountManager.java
+++ b/core/java/android/accounts/AccountManager.java
@@ -32,6 +32,7 @@ import android.util.Log;
import android.text.TextUtils;
import java.io.IOException;
+import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
@@ -41,6 +42,7 @@ import java.util.concurrent.TimeUnit;
import java.util.HashMap;
import java.util.Map;
+import com.google.android.collect.Lists;
import com.google.android.collect.Maps;
/**
@@ -1769,6 +1771,44 @@ public class AccountManager {
return task;
}
+ /**
+ * Returns an intent to an {@link Activity} that prompts the user to choose from a list of
+ * accounts.
+ * The caller will then typically start the activity by calling
+ * <code>startActivityWithResult(intent, ...);</code>.
+ * <p>
+ * On success the activity returns a Bundle with the account name and type specified using
+ * keys {@link #KEY_ACCOUNT_NAME} and {@link #KEY_ACCOUNT_TYPE}.
+ * <p>
+ * The most common case is to call this with one account type, e.g.:
+ * <p>
+ * <pre> newChooseAccountsIntent(null, null, new String[]{"com.google"}, null);</pre>
+ * @param selectedAccount if specified, indicates that the {@link Account} is the currently
+ * selected one, according to the caller's definition of selected.
+ * @param allowableAccounts an optional {@link ArrayList} of accounts that are allowed to be
+ * shown. If not specified then this field will not limit the displayed accounts.
+ * @param allowableAccountTypes an optional string array of account types. These are used
+ * both to filter the shown accounts and to filter the list of account types that are shown
+ * when adding an account.
+ * @param addAccountOptions This {@link Bundle} is passed as the addAccount options
+ * @return an {@link Intent} that can be used to launch the ChooseAccount activity flow.
+ */
+ static public Intent newChooseAccountIntent(Account selectedAccount,
+ ArrayList<Account> allowableAccounts,
+ String[] allowableAccountTypes,
+ Bundle addAccountOptions) {
+ Intent intent = new Intent();
+ intent.setClassName("android", "android.accounts.ChooseTypeAndAccountActivity");
+ intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST,
+ allowableAccounts);
+ intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST,
+ allowableAccountTypes != null ? Lists.newArrayList(allowableAccountTypes) : 0);
+ intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
+ addAccountOptions);
+ intent.putExtra(ChooseTypeAndAccountActivity.EXTRA_SELECTED_ACCOUNT, selectedAccount);
+ return intent;
+ }
+
private final HashMap<OnAccountsUpdateListener, Handler> mAccountsUpdatedListeners =
Maps.newHashMap();
diff --git a/core/java/android/accounts/ChooseAccountTypeActivity.java b/core/java/android/accounts/ChooseAccountTypeActivity.java
new file mode 100644
index 0000000..836164c
--- /dev/null
+++ b/core/java/android/accounts/ChooseAccountTypeActivity.java
@@ -0,0 +1,220 @@
+/*
+ * Copyright (C) 2011 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 android.accounts;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.TextView;
+import com.android.internal.R;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * @hide
+ */
+public class ChooseAccountTypeActivity extends Activity implements AccountManagerCallback<Bundle> {
+ private static final String TAG = "AccountManager";
+
+ private HashMap<String, AuthInfo> mTypeToAuthenticatorInfo = new HashMap<String, AuthInfo>();
+ private ArrayList<AuthInfo> mAuthenticatorInfosToDisplay;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.choose_account);
+
+ // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
+ Set<String> setOfAllowableAccountTypes = null;
+ ArrayList<String> validAccountTypes = getIntent().getStringArrayListExtra(
+ ChooseTypeAndAccountActivity.EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST);
+ if (validAccountTypes != null) {
+ setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.size());
+ for (String type : validAccountTypes) {
+ setOfAllowableAccountTypes.add(type);
+ }
+ }
+
+ // create a map of account authenticators
+ buildTypeToAuthDescriptionMap();
+
+ // Create a list of authenticators that are allowable. Filter out those that
+ // don't match the allowable account types, if provided.
+ mAuthenticatorInfosToDisplay = new ArrayList<AuthInfo>(mTypeToAuthenticatorInfo.size());
+ for (Map.Entry<String, AuthInfo> entry: mTypeToAuthenticatorInfo.entrySet()) {
+ final String type = entry.getKey();
+ final AuthInfo info = entry.getValue();
+ if (setOfAllowableAccountTypes != null
+ && !setOfAllowableAccountTypes.contains(type)) {
+ continue;
+ }
+ mAuthenticatorInfosToDisplay.add(info);
+ }
+
+ if (mAuthenticatorInfosToDisplay.isEmpty()) {
+ Bundle bundle = new Bundle();
+ bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "no allowable account types");
+ setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+ finish();
+ return;
+ }
+
+ if (mAuthenticatorInfosToDisplay.size() == 1) {
+ runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(0));
+ return;
+ }
+
+ // Setup the list
+ ListView list = (ListView) findViewById(android.R.id.list);
+ // Use an existing ListAdapter that will map an array of strings to TextViews
+ list.setAdapter(new AccountArrayAdapter(this,
+ android.R.layout.simple_list_item_1, mAuthenticatorInfosToDisplay));
+ list.setChoiceMode(ListView.CHOICE_MODE_NONE);
+ list.setTextFilterEnabled(false);
+ list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
+ runAddAccountForAuthenticator(mAuthenticatorInfosToDisplay.get(position));
+ }
+ });
+ }
+
+ private void buildTypeToAuthDescriptionMap() {
+ for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
+ String name = null;
+ Drawable icon = null;
+ try {
+ Context authContext = createPackageContext(desc.packageName, 0);
+ icon = authContext.getResources().getDrawable(desc.iconId);
+ final CharSequence sequence = authContext.getResources().getText(desc.labelId);
+ if (sequence != null) {
+ name = sequence.toString();
+ }
+ name = sequence.toString();
+ } catch (PackageManager.NameNotFoundException e) {
+ // Nothing we can do much here, just log
+ if (Log.isLoggable(TAG, Log.WARN)) {
+ Log.w(TAG, "No icon name for account type " + desc.type);
+ }
+ } catch (Resources.NotFoundException e) {
+ // Nothing we can do much here, just log
+ if (Log.isLoggable(TAG, Log.WARN)) {
+ Log.w(TAG, "No icon resource for account type " + desc.type);
+ }
+ }
+ AuthInfo authInfo = new AuthInfo(desc, name, icon);
+ mTypeToAuthenticatorInfo.put(desc.type, authInfo);
+ }
+ }
+
+ protected void runAddAccountForAuthenticator(AuthInfo authInfo) {
+ Log.d(TAG, "selected account type " + authInfo.name);
+ Bundle options = getIntent().getBundleExtra(
+ ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
+ AccountManager.get(this).addAccount(authInfo.desc.type, null, null, options,
+ this, this, null);
+ }
+
+ public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
+ try {
+ Bundle accountManagerResult = accountManagerFuture.getResult();
+ Bundle bundle = new Bundle();
+ bundle.putString(AccountManager.KEY_ACCOUNT_NAME,
+ accountManagerResult.getString(AccountManager.KEY_ACCOUNT_NAME));
+ bundle.putString(AccountManager.KEY_ACCOUNT_TYPE,
+ accountManagerResult.getString(AccountManager.KEY_ACCOUNT_TYPE));
+ setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+ finish();
+ return;
+ } catch (OperationCanceledException e) {
+ setResult(Activity.RESULT_CANCELED);
+ finish();
+ return;
+ } catch (IOException e) {
+ } catch (AuthenticatorException e) {
+ }
+ Bundle bundle = new Bundle();
+ bundle.putString(AccountManager.KEY_ERROR_MESSAGE, "error communicating with server");
+ setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+ finish();
+ }
+
+ private static class AuthInfo {
+ final AuthenticatorDescription desc;
+ final String name;
+ final Drawable drawable;
+
+ AuthInfo(AuthenticatorDescription desc, String name, Drawable drawable) {
+ this.desc = desc;
+ this.name = name;
+ this.drawable = drawable;
+ }
+ }
+
+ private static class ViewHolder {
+ ImageView icon;
+ TextView text;
+ }
+
+ private static class AccountArrayAdapter extends ArrayAdapter<AuthInfo> {
+ private LayoutInflater mLayoutInflater;
+ private ArrayList<AuthInfo> mInfos;
+
+ public AccountArrayAdapter(Context context, int textViewResourceId,
+ ArrayList<AuthInfo> infos) {
+ super(context, textViewResourceId, infos);
+ mInfos = infos;
+ mLayoutInflater = (LayoutInflater) context.getSystemService(
+ Context.LAYOUT_INFLATER_SERVICE);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ ViewHolder holder;
+
+ if (convertView == null) {
+ convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null);
+ holder = new ViewHolder();
+ holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
+ holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
+ convertView.setTag(holder);
+ } else {
+ holder = (ViewHolder) convertView.getTag();
+ }
+
+ holder.text.setText(mInfos.get(position).name);
+ holder.icon.setImageDrawable(mInfos.get(position).drawable);
+
+ return convertView;
+ }
+ }
+}
diff --git a/core/java/android/accounts/ChooseTypeAndAccountActivity.java b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
new file mode 100644
index 0000000..a903399
--- /dev/null
+++ b/core/java/android/accounts/ChooseTypeAndAccountActivity.java
@@ -0,0 +1,274 @@
+/*
+ * Copyright (C) 2011 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 android.accounts;
+
+import android.app.Activity;
+import android.content.Context;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+import android.content.res.Resources;
+import android.graphics.drawable.Drawable;
+import android.os.Bundle;
+import android.os.Parcelable;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.ImageView;
+import android.widget.ListView;
+import android.widget.TextView;
+import com.android.internal.R;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @hide
+ */
+public class ChooseTypeAndAccountActivity extends Activity {
+ private static final String TAG = "AccountManager";
+
+ /**
+ * A Parcelable ArrayList of Account objects that limits the choosable accounts to those
+ * in this list, if this parameter is supplied.
+ */
+ public static final String EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST = "allowableAccounts";
+
+ /**
+ * A Parcelable ArrayList of String objects that limits the accounts to choose to those
+ * that match the types in this list, if this parameter is supplied. This list is also
+ * used to filter the allowable account types if add account is selected.
+ */
+ public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST = "allowableAccountTypes";
+
+ /**
+ * This is passed as the options bundle in AccountManager.addAccount() if it is called.
+ */
+ public static final String EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE = "addAccountOptions";
+
+ /**
+ * If set then the specified account is already "selected".
+ */
+ public static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount";
+
+ private ArrayList<AccountInfo> mAccountInfos;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.choose_type_and_account);
+ final AccountManager accountManager = AccountManager.get(this);
+
+ // build an efficiently queryable map of account types to authenticator descriptions
+ final HashMap<String, AuthenticatorDescription> typeToAuthDescription =
+ new HashMap<String, AuthenticatorDescription>();
+ for(AuthenticatorDescription desc : accountManager.getAuthenticatorTypes()) {
+ typeToAuthDescription.put(desc.type, desc);
+ }
+
+ // Read the validAccounts, if present, and add them to the setOfAllowableAccounts
+ Set<Account> setOfAllowableAccounts = null;
+ final ArrayList<Parcelable> validAccounts =
+ getIntent().getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST);
+ if (validAccounts != null) {
+ setOfAllowableAccounts = new HashSet<Account>(validAccounts.size());
+ for (Parcelable parcelable : validAccounts) {
+ setOfAllowableAccounts.add((Account)parcelable);
+ }
+ }
+
+ // Read the validAccountTypes, if present, and add them to the setOfAllowableAccountTypes
+ Set<String> setOfAllowableAccountTypes = null;
+ final ArrayList<String> validAccountTypes =
+ getIntent().getStringArrayListExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST);
+ if (validAccountTypes != null) {
+ setOfAllowableAccountTypes = new HashSet<String>(validAccountTypes.size());
+ for (String type : validAccountTypes) {
+ setOfAllowableAccountTypes.add(type);
+ }
+ }
+
+ // Create a list of AccountInfo objects for each account that is allowable. Filter out
+ // accounts that don't match the allowable types, if provided, or that don't match the
+ // allowable accounts, if provided.
+ final Account[] accounts = accountManager.getAccounts();
+ mAccountInfos = new ArrayList<AccountInfo>(accounts.length);
+ for (Account account : accounts) {
+ if (setOfAllowableAccounts != null
+ && !setOfAllowableAccounts.contains(account)) {
+ continue;
+ }
+ if (setOfAllowableAccountTypes != null
+ && !setOfAllowableAccountTypes.contains(account.type)) {
+ continue;
+ }
+ mAccountInfos.add(new AccountInfo(account,
+ getDrawableForType(typeToAuthDescription, account.type)));
+ }
+
+ // If there are no allowable accounts go directly to add account
+ if (mAccountInfos.isEmpty()) {
+ startChooseAccountTypeActivity();
+ return;
+ }
+
+ // if there is only one allowable account return it
+ if (mAccountInfos.size() == 1) {
+ Account account = mAccountInfos.get(0).account;
+ setResultAndFinish(account.name, account.type);
+ return;
+ }
+
+ // there is more than one allowable account. initialize the list adapter to allow
+ // the user to select an account.
+ ListView list = (ListView) findViewById(android.R.id.list);
+ list.setAdapter(new AccountArrayAdapter(this,
+ android.R.layout.simple_list_item_1, mAccountInfos));
+ list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
+ list.setTextFilterEnabled(false);
+ list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
+ public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
+ onListItemClick((ListView)parent, v, position, id);
+ }
+ });
+
+ // set the listener for the addAccount button
+ Button addAccountButton = (Button) findViewById(R.id.addAccount);
+ addAccountButton.setOnClickListener(new View.OnClickListener() {
+ public void onClick(final View v) {
+ startChooseAccountTypeActivity();
+ }
+ });
+ }
+
+ // Called when the choose account type activity (for adding an account) returns.
+ // If it was a success read the account and set it in the result. In all cases
+ // return the result and finish this activity.
+ @Override
+ protected void onActivityResult(final int requestCode, final int resultCode,
+ final Intent data) {
+ if (resultCode == RESULT_OK && data != null) {
+ String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
+ String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
+ if (accountName != null && accountType != null) {
+ setResultAndFinish(accountName, accountType);
+ return;
+ }
+ }
+ setResult(Activity.RESULT_CANCELED);
+ finish();
+ }
+
+ private Drawable getDrawableForType(
+ final HashMap<String, AuthenticatorDescription> typeToAuthDescription,
+ String accountType) {
+ Drawable icon = null;
+ if (typeToAuthDescription.containsKey(accountType)) {
+ try {
+ AuthenticatorDescription desc = typeToAuthDescription.get(accountType);
+ Context authContext = createPackageContext(desc.packageName, 0);
+ icon = authContext.getResources().getDrawable(desc.iconId);
+ } catch (PackageManager.NameNotFoundException e) {
+ // Nothing we can do much here, just log
+ if (Log.isLoggable(TAG, Log.WARN)) {
+ Log.w(TAG, "No icon name for account type " + accountType);
+ }
+ } catch (Resources.NotFoundException e) {
+ // Nothing we can do much here, just log
+ if (Log.isLoggable(TAG, Log.WARN)) {
+ Log.w(TAG, "No icon resource for account type " + accountType);
+ }
+ }
+ }
+ return icon;
+ }
+
+ protected void onListItemClick(ListView l, View v, int position, long id) {
+ AccountInfo accountInfo = mAccountInfos.get(position);
+ Log.d(TAG, "selected account " + accountInfo.account);
+ setResultAndFinish(accountInfo.account.name, accountInfo.account.type);
+ }
+
+ private void setResultAndFinish(final String accountName, final String accountType) {
+ Bundle bundle = new Bundle();
+ bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName);
+ bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType);
+ setResult(Activity.RESULT_OK, new Intent().putExtras(bundle));
+ finish();
+ }
+
+ private void startChooseAccountTypeActivity() {
+ final Intent intent = new Intent(this, ChooseAccountTypeActivity.class);
+ intent.putStringArrayListExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST,
+ getIntent().getStringArrayListExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST));
+ intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
+ getIntent().getBundleExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_ARRAYLIST));
+ startActivityForResult(intent, 0);
+ }
+
+ private static class AccountInfo {
+ final Account account;
+ final Drawable drawable;
+
+ AccountInfo(Account account, Drawable drawable) {
+ this.account = account;
+ this.drawable = drawable;
+ }
+ }
+
+ private static class ViewHolder {
+ ImageView icon;
+ TextView text;
+ }
+
+ private static class AccountArrayAdapter extends ArrayAdapter<AccountInfo> {
+ private LayoutInflater mLayoutInflater;
+ private ArrayList<AccountInfo> mInfos;
+
+ public AccountArrayAdapter(Context context, int textViewResourceId,
+ ArrayList<AccountInfo> infos) {
+ super(context, textViewResourceId, infos);
+ mInfos = infos;
+ mLayoutInflater = (LayoutInflater) context.getSystemService(
+ Context.LAYOUT_INFLATER_SERVICE);
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ ViewHolder holder;
+
+ if (convertView == null) {
+ convertView = mLayoutInflater.inflate(R.layout.choose_account_row, null);
+ holder = new ViewHolder();
+ holder.text = (TextView) convertView.findViewById(R.id.account_row_text);
+ holder.icon = (ImageView) convertView.findViewById(R.id.account_row_icon);
+ convertView.setTag(holder);
+ } else {
+ holder = (ViewHolder) convertView.getTag();
+ }
+
+ holder.text.setText(mInfos.get(position).account.name);
+ holder.icon.setImageDrawable(mInfos.get(position).drawable);
+
+ return convertView;
+ }
+ }
+}