ChooseAccountActivitypublic class ChooseAccountActivity extends android.app.Activity
Fields Summary |
---|
private static final String | TAG | private android.os.Parcelable[] | mAccounts | private AccountManagerResponse | mAccountManagerResponse | private android.os.Bundle | mResult | private HashMap | mTypeToAuthDescription |
Methods Summary |
---|
public void | finish()
if (mAccountManagerResponse != null) {
if (mResult != null) {
mAccountManagerResponse.onResult(mResult);
} else {
mAccountManagerResponse.onError(AccountManager.ERROR_CODE_CANCELED, "canceled");
}
}
super.finish();
| private void | getAuthDescriptions()
for(AuthenticatorDescription desc : AccountManager.get(this).getAuthenticatorTypes()) {
mTypeToAuthDescription.put(desc.type, desc);
}
| private android.graphics.drawable.Drawable | getDrawableForType(java.lang.String accountType)
Drawable icon = null;
if(mTypeToAuthDescription.containsKey(accountType)) {
try {
AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
Context authContext = createPackageContext(desc.packageName, 0);
icon = authContext.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;
| public void | onCreate(android.os.Bundle savedInstanceState)
super.onCreate(savedInstanceState);
mAccounts = getIntent().getParcelableArrayExtra(AccountManager.KEY_ACCOUNTS);
mAccountManagerResponse =
getIntent().getParcelableExtra(AccountManager.KEY_ACCOUNT_MANAGER_RESPONSE);
// KEY_ACCOUNTS is a required parameter
if (mAccounts == null) {
setResult(RESULT_CANCELED);
finish();
return;
}
getAuthDescriptions();
AccountInfo[] mAccountInfos = new AccountInfo[mAccounts.length];
for (int i = 0; i < mAccounts.length; i++) {
mAccountInfos[i] = new AccountInfo(((Account) mAccounts[i]).name,
getDrawableForType(((Account) mAccounts[i]).type));
}
setContentView(R.layout.choose_account);
// 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, mAccountInfos));
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setTextFilterEnabled(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
onListItemClick((ListView)parent, v, position, id);
}
});
| protected void | onListItemClick(android.widget.ListView l, android.view.View v, int position, long id)
Account account = (Account) mAccounts[position];
Log.d(TAG, "selected account " + account);
Bundle bundle = new Bundle();
bundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
mResult = bundle;
finish();
|
|