现象:在日历应用中点击添加账户跳转失败,在设置中点击添加账户正常
日历中添加账户入口
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else if (item.getItemId() == R.id.action_add_account) {
Intent nextIntent = new Intent(Settings.ACTION_ADD_ACCOUNT);
final String[] array = { "com.android.calendar" };
nextIntent.putExtra(Settings.EXTRA_AUTHORITIES, array);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nextIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
设置中添加账户入口
@Override
public boolean onPreferenceClick(Preference preference) {
final int metricsCategory = mFragment.getMetricsCategory();
final int count = mProfiles.size();
for (int i = 0; i < count; i++) {
ProfileData profileData = mProfiles.valueAt(i);
if (preference == profileData.addAccountPreference) {
mMetricsFeatureProvider.logClickedPreference(preference, metricsCategory);
Intent intent = new Intent(ACTION_ADD_ACCOUNT);
intent.putExtra(EXTRA_USER, profileData.userInfo.getUserHandle());
intent.putExtra(EXTRA_AUTHORITIES, mAuthorities);
mContext.startActivity(intent);
return true;
}
}
return false;
}
这两处都是跳转到 AddAccountSettings.java中,然后又经过一些条件判断调用其中的requestChooseAccount()方法跳转到ChooseAccountActivity.java
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (um.isUserUnlocked(mUserHandle)) {
requestChooseAccount();
} else {
ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(this);
if (!helper.launchConfirmationActivity(UNLOCK_WORK_PROFILE_REQUEST,
getString(R.string.unlock_set_unlock_launch_picker_title),false,
mUserHandle.getIdentifier())) {
requestChooseAccount();
}
}
}
private void requestChooseAccount() {
final String[] authorities =
getIntent().getStringArrayExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY);
final String[] accountTypes =
getIntent().getStringArrayExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY);
final Intent intent = new Intent(this, Settings.ChooseAccountActivity.class);
if (authorities != null) {
intent.putExtra(AccountPreferenceBase.AUTHORITIES_FILTER_KEY, authorities);
}
if (accountTypes != null) {
intent.putExtra(AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY, accountTypes);
}
intent.putExtra(EXTRA_USER, mUserHandle);
startActivityForResult(intent, CHOOSE_ACCOUNT_REQUEST);
}
在ChooseAccountFragment.java中调用ChooseAccountPreferenceController.java的initialize()方法
@Override
public void onAttach(Context context) {
super.onAttach(context);
final String[] authorities = getIntent().getStringArrayExtra(
AccountPreferenceBase.AUTHORITIES_FILTER_KEY);
final String[] accountTypesFilter = getIntent().getStringArrayExtra(
AccountPreferenceBase.ACCOUNT_TYPES_FILTER_KEY);
final UserManager userManager = UserManager.get(getContext());
final UserHandle userHandle = Utils.getSecureTargetUser(getActivity().getActivityToken(),
userManager, null , getIntent().getExtras());
use(ChooseAccountPreferenceController.class).initialize(authorities, accountTypesFilter,
userHandle, getActivity());
}
最后在ChooseAccountPreferenceController.java判断是否显示可添加的账号类型列表。
public void initialize(String[] authorities, String[] accountTypesFilter, UserHandle userHandle,
Activity activity) {
mActivity = activity;
mAuthorities = authorities;
mUserHandle = userHandle;
if (accountTypesFilter != null) {
mAccountTypesFilter = new HashSet<>();
for (String accountType : accountTypesFilter) {
mAccountTypesFilter.add(accountType);
}
}
}
private void updateAuthDescriptions() {
mAuthDescs = AccountManager.get(mContext).getAuthenticatorTypesAsUser(
mUserHandle.getIdentifier());
for (int i = 0; i < mAuthDescs.length; i++) {
mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]);
}
onAuthDescriptionsUpdated();
}
private void onAuthDescriptionsUpdated() {
for (int i = 0; i < mAuthDescs.length; i++) {
final String accountType = mAuthDescs[i].type;
final CharSequence providerName = getLabelForType(accountType);
final List<String> accountAuths = getAuthoritiesForAccountType(accountType);
boolean addAccountPref = true;
if (mAuthorities != null && mAuthorities.length > 0 && accountAuths != null) {
addAccountPref = false;
for (int k = 0; k < mAuthorities.length; k++) {
if (accountAuths.contains(mAuthorities[k])) {
addAccountPref = true;
break;
}
}
}
if (addAccountPref && mAccountTypesFilter != null
&& !mAccountTypesFilter.contains(accountType)) {
addAccountPref = false;
}
if (addAccountPref) {
mProviderList.add(new ProviderEntry(providerName, accountType));
} else {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
Log.v(TAG, "Skipped pref " + providerName + ": has no authority we need");
}
}
}
final Context context = mScreen.getContext();
if (mProviderList.size() == 1) {
final RestrictedLockUtils.EnforcedAdmin admin =
RestrictedLockUtilsInternal.checkIfAccountManagementDisabled(
context, mProviderList.get(0).getType(), mUserHandle.getIdentifier());
if (admin != null) {
mActivity.setResult(RESULT_CANCELED,
RestrictedLockUtils.getShowAdminSupportDetailsIntent(
context, admin));
mActivity.finish();
} else {
finishWithAccountType(mProviderList.get(0).getType());
}
} else if (mProviderList.size() > 0) {
Collections.sort(mProviderList);
for (ProviderEntry pref : mProviderList) {
final Drawable drawable = getDrawableForType(pref.getType());
final ProviderPreference p = new ProviderPreference(context,
pref.getType(), drawable, pref.getName());
p.setKey(pref.getType().toString());
p.checkAccountManagementAndSetDisabled(mUserHandle.getIdentifier());
mScreen.addPreference(p);
}
} else {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
final StringBuilder auths = new StringBuilder();
for (String a : mAuthorities) {
auths.append(a);
auths.append(' ');
}
Log.v(TAG, "No providers found for authorities: " + auths);
}
mActivity.setResult(RESULT_CANCELED);
mActivity.finish();
}
}
所以我们只需修改传过来的mAuthorities的数组中包含com.android.email.provider,或者直接不传值
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else if (item.getItemId() == R.id.action_add_account) {
Intent nextIntent = new Intent(Settings.ACTION_ADD_ACCOUNT);
final String[] array = { "com.android.calendar", "com.android.email.provider"};
nextIntent.putExtra(Settings.EXTRA_AUTHORITIES, array);
nextIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(nextIntent);
return true;
}
return super.onOptionsItemSelected(item);
}
|