package android.accounts;
import com.google.android.collect.Sets;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.UserHandle;
import android.os.UserManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import com.android.internal.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
public class ChooseTypeAndAccountActivity extends Activity
implements AccountManagerCallback<Bundle> {
private static final String TAG = "AccountChooser";
public static final String EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST = "allowableAccounts";
public static final String EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY = "allowableAccountTypes";
public static final String EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE = "addAccountOptions";
public static final String EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY =
"addAccountRequiredFeatures";
public static final String EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING = "authTokenType";
public static final String EXTRA_SELECTED_ACCOUNT = "selectedAccount";
@Deprecated
public static final String EXTRA_ALWAYS_PROMPT_FOR_ACCOUNT =
"alwaysPromptForAccount";
public static final String EXTRA_DESCRIPTION_TEXT_OVERRIDE = "descriptionTextOverride";
public static final int REQUEST_NULL = 0;
public static final int REQUEST_CHOOSE_TYPE = 1;
public static final int REQUEST_ADD_ACCOUNT = 2;
private static final String KEY_INSTANCE_STATE_PENDING_REQUEST = "pendingRequest";
private static final String KEY_INSTANCE_STATE_EXISTING_ACCOUNTS = "existingAccounts";
private static final String KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME = "selectedAccountName";
private static final String KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT = "selectedAddAccount";
private static final String KEY_INSTANCE_STATE_ACCOUNTS_LIST = "accountsList";
private static final String KEY_INSTANCE_STATE_VISIBILITY_LIST = "visibilityList";
private static final int SELECTED_ITEM_NONE = -1;
private Set<Account> mSetOfAllowableAccounts;
private Set<String> mSetOfRelevantAccountTypes;
private String mSelectedAccountName = null;
private boolean mSelectedAddNewAccount = false;
private String mDescriptionOverride;
private LinkedHashMap<Account, Integer> mAccounts;
private ArrayList<Account> mPossiblyVisibleAccounts;
private int mPendingRequest = REQUEST_NULL;
private Parcelable[] mExistingAccounts = null;
private int mSelectedItemIndex;
private Button mOkButton;
private int mCallingUid;
private String mCallingPackage;
private boolean mDisallowAddAccounts;
private boolean mDontShowPicker;
@Override
public void onCreate(Bundle savedInstanceState) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "ChooseTypeAndAccountActivity.onCreate(savedInstanceState="
+ savedInstanceState + ")");
}
String message = null;
try {
IBinder activityToken = getActivityToken();
mCallingUid = ActivityManager.getService().getLaunchedFromUid(activityToken);
mCallingPackage = ActivityManager.getService().getLaunchedFromPackage(
activityToken);
if (mCallingUid != 0 && mCallingPackage != null) {
Bundle restrictions = UserManager.get(this)
.getUserRestrictions(new UserHandle(UserHandle.getUserId(mCallingUid)));
mDisallowAddAccounts =
restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false);
}
} catch (RemoteException re) {
Log.w(getClass().getSimpleName(), "Unable to get caller identity \n" + re);
}
final Intent intent = getIntent();
mSetOfAllowableAccounts = getAllowableAccountSet(intent);
mSetOfRelevantAccountTypes = getReleventAccountTypes(intent);
mDescriptionOverride = intent.getStringExtra(EXTRA_DESCRIPTION_TEXT_OVERRIDE);
if (savedInstanceState != null) {
mPendingRequest = savedInstanceState.getInt(KEY_INSTANCE_STATE_PENDING_REQUEST);
mExistingAccounts =
savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS);
mSelectedAccountName =
savedInstanceState.getString(KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME);
mSelectedAddNewAccount =
savedInstanceState.getBoolean(KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, false);
Parcelable[] accounts =
savedInstanceState.getParcelableArray(KEY_INSTANCE_STATE_ACCOUNTS_LIST);
ArrayList<Integer> visibility =
savedInstanceState.getIntegerArrayList(KEY_INSTANCE_STATE_VISIBILITY_LIST);
mAccounts = new LinkedHashMap<>();
for (int i = 0; i < accounts.length; i++) {
mAccounts.put((Account) accounts[i], visibility.get(i));
}
} else {
mPendingRequest = REQUEST_NULL;
mExistingAccounts = null;
Account selectedAccount = (Account) intent.getParcelableExtra(EXTRA_SELECTED_ACCOUNT);
if (selectedAccount != null) {
mSelectedAccountName = selectedAccount.name;
}
mAccounts = getAcceptableAccountChoices(AccountManager.get(this));
}
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "selected account name is " + mSelectedAccountName);
}
mPossiblyVisibleAccounts = new ArrayList<>(mAccounts.size());
for (Map.Entry<Account, Integer> entry : mAccounts.entrySet()) {
if (AccountManager.VISIBILITY_NOT_VISIBLE != entry.getValue()) {
mPossiblyVisibleAccounts.add(entry.getKey());
}
}
if (mPossiblyVisibleAccounts.isEmpty() && mDisallowAddAccounts) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.app_not_authorized);
mDontShowPicker = true;
}
if (mDontShowPicker) {
super.onCreate(savedInstanceState);
return;
}
if (mPendingRequest == REQUEST_NULL) {
if (mPossiblyVisibleAccounts.isEmpty()) {
setNonLabelThemeAndCallSuperCreate(savedInstanceState);
if (mSetOfRelevantAccountTypes.size() == 1) {
runAddAccountForAuthenticator(mSetOfRelevantAccountTypes.iterator().next());
} else {
startChooseAccountTypeActivity();
}
}
}
String[] listItems = getListOfDisplayableOptions(mPossiblyVisibleAccounts);
mSelectedItemIndex = getItemIndexToSelect(mPossiblyVisibleAccounts, mSelectedAccountName,
mSelectedAddNewAccount);
super.onCreate(savedInstanceState);
setContentView(R.layout.choose_type_and_account);
overrideDescriptionIfSupplied(mDescriptionOverride);
populateUIAccountList(listItems);
mOkButton = findViewById(android.R.id.button2);
mOkButton.setEnabled(mSelectedItemIndex != SELECTED_ITEM_NONE);
}
@Override
protected void onDestroy() {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "ChooseTypeAndAccountActivity.onDestroy()");
}
super.onDestroy();
}
@Override
protected void onSaveInstanceState(final Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_INSTANCE_STATE_PENDING_REQUEST, mPendingRequest);
if (mPendingRequest == REQUEST_ADD_ACCOUNT) {
outState.putParcelableArray(KEY_INSTANCE_STATE_EXISTING_ACCOUNTS, mExistingAccounts);
}
if (mSelectedItemIndex != SELECTED_ITEM_NONE) {
if (mSelectedItemIndex == mPossiblyVisibleAccounts.size()) {
outState.putBoolean(KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, true);
} else {
outState.putBoolean(KEY_INSTANCE_STATE_SELECTED_ADD_ACCOUNT, false);
outState.putString(KEY_INSTANCE_STATE_SELECTED_ACCOUNT_NAME,
mPossiblyVisibleAccounts.get(mSelectedItemIndex).name);
}
}
Parcelable[] accounts = new Parcelable[mAccounts.size()];
ArrayList<Integer> visibility = new ArrayList<>(mAccounts.size());
int i = 0;
for (Map.Entry<Account, Integer> e : mAccounts.entrySet()) {
accounts[i++] = e.getKey();
visibility.add(e.getValue());
}
outState.putParcelableArray(KEY_INSTANCE_STATE_ACCOUNTS_LIST, accounts);
outState.putIntegerArrayList(KEY_INSTANCE_STATE_VISIBILITY_LIST, visibility);
}
public void onCancelButtonClicked(View view) {
onBackPressed();
}
public void onOkButtonClicked(View view) {
if (mSelectedItemIndex == mPossiblyVisibleAccounts.size()) {
startChooseAccountTypeActivity();
} else if (mSelectedItemIndex != SELECTED_ITEM_NONE) {
onAccountSelected(mPossiblyVisibleAccounts.get(mSelectedItemIndex));
}
}
@Override
protected void onActivityResult(final int requestCode, final int resultCode,
final Intent data) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
if (data != null && data.getExtras() != null) data.getExtras().keySet();
Bundle extras = data != null ? data.getExtras() : null;
Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult(reqCode=" + requestCode
+ ", resCode=" + resultCode + ", extras=" + extras + ")");
}
mPendingRequest = REQUEST_NULL;
if (resultCode == RESULT_CANCELED) {
if (mPossiblyVisibleAccounts.isEmpty()) {
setResult(Activity.RESULT_CANCELED);
finish();
}
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CHOOSE_TYPE) {
if (data != null) {
String accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
if (accountType != null) {
runAddAccountForAuthenticator(accountType);
return;
}
}
Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find account "
+ "type, pretending the request was canceled");
} else if (requestCode == REQUEST_ADD_ACCOUNT) {
String accountName = null;
String accountType = null;
if (data != null) {
accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
accountType = data.getStringExtra(AccountManager.KEY_ACCOUNT_TYPE);
}
if (accountName == null || accountType == null) {
Account[] currentAccounts = AccountManager.get(this).getAccountsForPackage(
mCallingPackage, mCallingUid);
Set<Account> preExistingAccounts = new HashSet<Account>();
for (Parcelable accountParcel : mExistingAccounts) {
preExistingAccounts.add((Account) accountParcel);
}
for (Account account : currentAccounts) {
if (!preExistingAccounts.contains(account)) {
accountName = account.name;
accountType = account.type;
break;
}
}
}
if (accountName != null || accountType != null) {
setResultAndFinish(accountName, accountType);
return;
}
}
Log.d(TAG, "ChooseTypeAndAccountActivity.onActivityResult: unable to find added "
+ "account, pretending the request was canceled");
}
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "ChooseTypeAndAccountActivity.onActivityResult: canceled");
}
setResult(Activity.RESULT_CANCELED);
finish();
}
protected void runAddAccountForAuthenticator(String type) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "runAddAccountForAuthenticator: " + type);
}
final Bundle options = getIntent().getBundleExtra(
ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE);
final String[] requiredFeatures = getIntent().getStringArrayExtra(
ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY);
final String authTokenType = getIntent().getStringExtra(
ChooseTypeAndAccountActivity.EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING);
AccountManager.get(this).addAccount(type, authTokenType, requiredFeatures,
options, null , this , null );
}
@Override
public void run(final AccountManagerFuture<Bundle> accountManagerFuture) {
try {
final Bundle accountManagerResult = accountManagerFuture.getResult();
final Intent intent = (Intent)accountManagerResult.getParcelable(
AccountManager.KEY_INTENT);
if (intent != null) {
mPendingRequest = REQUEST_ADD_ACCOUNT;
mExistingAccounts = AccountManager.get(this).getAccountsForPackage(mCallingPackage,
mCallingUid);
intent.setFlags(intent.getFlags() & ~Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, REQUEST_ADD_ACCOUNT);
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 void setNonLabelThemeAndCallSuperCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_DeviceDefault_Light_Dialog_NoActionBar);
super.onCreate(savedInstanceState);
}
private void onAccountSelected(Account account) {
Log.d(TAG, "selected account " + account);
setResultAndFinish(account.name, account.type);
}
private void setResultAndFinish(final String accountName, final String accountType) {
Account account = new Account(accountName, accountType);
Integer oldVisibility =
AccountManager.get(this).getAccountVisibility(account, mCallingPackage);
if (oldVisibility != null
&& oldVisibility == AccountManager.VISIBILITY_USER_MANAGED_NOT_VISIBLE) {
AccountManager.get(this).setAccountVisibility(account, mCallingPackage,
AccountManager.VISIBILITY_USER_MANAGED_VISIBLE);
}
if (oldVisibility != null && oldVisibility == AccountManager.VISIBILITY_NOT_VISIBLE) {
setResult(Activity.RESULT_CANCELED);
finish();
return;
}
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));
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "ChooseTypeAndAccountActivity.setResultAndFinish: selected account "
+ accountName + ", " + accountType);
}
finish();
}
private void startChooseAccountTypeActivity() {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "ChooseAccountTypeActivity.startChooseAccountTypeActivity()");
}
final Intent intent = new Intent(this, ChooseAccountTypeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY,
getIntent().getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY));
intent.putExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE,
getIntent().getBundleExtra(EXTRA_ADD_ACCOUNT_OPTIONS_BUNDLE));
intent.putExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY,
getIntent().getStringArrayExtra(EXTRA_ADD_ACCOUNT_REQUIRED_FEATURES_STRING_ARRAY));
intent.putExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING,
getIntent().getStringExtra(EXTRA_ADD_ACCOUNT_AUTH_TOKEN_TYPE_STRING));
startActivityForResult(intent, REQUEST_CHOOSE_TYPE);
mPendingRequest = REQUEST_CHOOSE_TYPE;
}
private int getItemIndexToSelect(ArrayList<Account> accounts, String selectedAccountName,
boolean selectedAddNewAccount) {
if (selectedAddNewAccount) {
return accounts.size();
}
for (int i = 0; i < accounts.size(); i++) {
if (accounts.get(i).name.equals(selectedAccountName)) {
return i;
}
}
return SELECTED_ITEM_NONE;
}
private String[] getListOfDisplayableOptions(ArrayList<Account> accounts) {
String[] listItems = new String[accounts.size() + (mDisallowAddAccounts ? 0 : 1)];
for (int i = 0; i < accounts.size(); i++) {
listItems[i] = accounts.get(i).name;
}
if (!mDisallowAddAccounts) {
listItems[accounts.size()] = getResources().getString(
R.string.add_account_button_label);
}
return listItems;
}
private LinkedHashMap<Account, Integer> getAcceptableAccountChoices(AccountManager accountManager) {
Map<Account, Integer> accountsAndVisibilityForCaller =
accountManager.getAccountsAndVisibilityForPackage(mCallingPackage, null);
Account[] allAccounts = accountManager.getAccounts();
LinkedHashMap<Account, Integer> accountsToPopulate =
new LinkedHashMap<>(accountsAndVisibilityForCaller.size());
for (Account account : allAccounts) {
if (mSetOfAllowableAccounts != null
&& !mSetOfAllowableAccounts.contains(account)) {
continue;
}
if (mSetOfRelevantAccountTypes != null
&& !mSetOfRelevantAccountTypes.contains(account.type)) {
continue;
}
if (accountsAndVisibilityForCaller.get(account) != null) {
accountsToPopulate.put(account, accountsAndVisibilityForCaller.get(account));
}
}
return accountsToPopulate;
}
private Set<String> getReleventAccountTypes(final Intent intent) {
Set<String> setOfRelevantAccountTypes = null;
final String[] allowedAccountTypes =
intent.getStringArrayExtra(EXTRA_ALLOWABLE_ACCOUNT_TYPES_STRING_ARRAY);
AuthenticatorDescription[] descs = AccountManager.get(this).getAuthenticatorTypes();
Set<String> supportedAccountTypes = new HashSet<String>(descs.length);
for (AuthenticatorDescription desc : descs) {
supportedAccountTypes.add(desc.type);
}
if (allowedAccountTypes != null) {
setOfRelevantAccountTypes = Sets.newHashSet(allowedAccountTypes);
setOfRelevantAccountTypes.retainAll(supportedAccountTypes);
} else {
setOfRelevantAccountTypes = supportedAccountTypes;
}
return setOfRelevantAccountTypes;
}
private Set<Account> getAllowableAccountSet(final Intent intent) {
Set<Account> setOfAllowableAccounts = null;
final ArrayList<Parcelable> validAccounts =
intent.getParcelableArrayListExtra(EXTRA_ALLOWABLE_ACCOUNTS_ARRAYLIST);
if (validAccounts != null) {
setOfAllowableAccounts = new HashSet<Account>(validAccounts.size());
for (Parcelable parcelable : validAccounts) {
setOfAllowableAccounts.add((Account)parcelable);
}
}
return setOfAllowableAccounts;
}
private void overrideDescriptionIfSupplied(String descriptionOverride) {
TextView descriptionView = findViewById(R.id.description);
if (!TextUtils.isEmpty(descriptionOverride)) {
descriptionView.setText(descriptionOverride);
} else {
descriptionView.setVisibility(View.GONE);
}
}
private final void populateUIAccountList(String[] listItems) {
ListView list = findViewById(android.R.id.list);
list.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice, listItems));
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setItemsCanFocus(false);
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
mSelectedItemIndex = position;
mOkButton.setEnabled(true);
}
});
if (mSelectedItemIndex != SELECTED_ITEM_NONE) {
list.setItemChecked(mSelectedItemIndex, true);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "List item " + mSelectedItemIndex + " should be selected");
}
}
}
}