Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.settings.accounts; |
| 18 | |
Tony Mantler | 1c9e2f6 | 2017-06-13 15:16:50 -0700 | [diff] [blame] | 19 | import static android.content.Intent.EXTRA_USER; |
| 20 | import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS; |
| 21 | import static android.os.UserManager.DISALLOW_REMOVE_MANAGED_PROFILE; |
| 22 | import static android.provider.Settings.ACTION_ADD_ACCOUNT; |
| 23 | import static android.provider.Settings.EXTRA_AUTHORITIES; |
| 24 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 25 | import android.accounts.Account; |
| 26 | import android.accounts.AccountManager; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
| 31 | import android.content.pm.ApplicationInfo; |
| 32 | import android.content.pm.PackageManager; |
| 33 | import android.content.pm.UserInfo; |
| 34 | import android.content.res.Resources; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 35 | import android.graphics.drawable.Drawable; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 36 | import android.os.Bundle; |
| 37 | import android.os.UserHandle; |
| 38 | import android.os.UserManager; |
Fan Zhang | fa9849e | 2018-04-18 15:58:50 -0700 | [diff] [blame] | 39 | import android.text.BidiFormatter; |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 40 | import android.util.ArrayMap; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 41 | import android.util.Log; |
| 42 | import android.util.SparseArray; |
| 43 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 44 | import androidx.annotation.VisibleForTesting; |
| 45 | import androidx.preference.Preference; |
| 46 | import androidx.preference.Preference.OnPreferenceClickListener; |
| 47 | import androidx.preference.PreferenceGroup; |
| 48 | import androidx.preference.PreferenceScreen; |
| 49 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 50 | import com.android.settings.AccessiblePreferenceCategory; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 51 | import com.android.settings.R; |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 52 | import com.android.settings.SettingsPreferenceFragment; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 53 | import com.android.settings.Utils; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 54 | import com.android.settings.core.PreferenceControllerMixin; |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 55 | import com.android.settings.core.SubSettingLauncher; |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 56 | import com.android.settings.dashboard.profileselector.ProfileSelectFragment; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 57 | import com.android.settings.overlay.FeatureFactory; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 58 | import com.android.settingslib.RestrictedPreference; |
| 59 | import com.android.settingslib.accounts.AuthenticatorHelper; |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 60 | import com.android.settingslib.core.AbstractPreferenceController; |
Leif Hendrik Wilden | 28dee1f | 2018-01-11 10:15:36 -0800 | [diff] [blame] | 61 | import com.android.settingslib.core.instrumentation.MetricsFeatureProvider; |
Juan Lang | 777ed25 | 2017-05-09 15:42:36 -0700 | [diff] [blame] | 62 | import com.android.settingslib.core.lifecycle.LifecycleObserver; |
| 63 | import com.android.settingslib.core.lifecycle.events.OnPause; |
| 64 | import com.android.settingslib.core.lifecycle.events.OnResume; |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 65 | import com.android.settingslib.search.SearchIndexableRaw; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 66 | |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Collections; |
| 69 | import java.util.Comparator; |
| 70 | import java.util.List; |
| 71 | |
Tony Mantler | 1d583e1 | 2017-06-13 13:09:25 -0700 | [diff] [blame] | 72 | public class AccountPreferenceController extends AbstractPreferenceController |
| 73 | implements PreferenceControllerMixin, AuthenticatorHelper.OnAccountsUpdateListener, |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 74 | OnPreferenceClickListener, LifecycleObserver, OnPause, OnResume { |
| 75 | |
| 76 | private static final String TAG = "AccountPrefController"; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 77 | |
| 78 | private static final int ORDER_ACCOUNT_PROFILES = 1; |
| 79 | private static final int ORDER_LAST = 1002; |
| 80 | private static final int ORDER_NEXT_TO_LAST = 1001; |
| 81 | private static final int ORDER_NEXT_TO_NEXT_TO_LAST = 1000; |
| 82 | |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 83 | private static final String PREF_KEY_REMOVE_PROFILE = "remove_profile"; |
| 84 | private static final String PREF_KEY_WORK_PROFILE_SETTING = "work_profile_setting"; |
| 85 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 86 | private UserManager mUm; |
| 87 | private SparseArray<ProfileData> mProfiles = new SparseArray<ProfileData>(); |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 88 | private ManagedProfileBroadcastReceiver mManagedProfileBroadcastReceiver = |
| 89 | new ManagedProfileBroadcastReceiver(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 90 | private Preference mProfileNotAvailablePreference; |
| 91 | private String[] mAuthorities; |
| 92 | private int mAuthoritiesCount = 0; |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 93 | private SettingsPreferenceFragment mFragment; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 94 | private int mAccountProfileOrder = ORDER_ACCOUNT_PROFILES; |
| 95 | private AccountRestrictionHelper mHelper; |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 96 | private MetricsFeatureProvider mMetricsFeatureProvider; |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 97 | private @ProfileSelectFragment.ProfileType int mType; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Holds data related to the accounts belonging to one profile. |
| 101 | */ |
| 102 | public static class ProfileData { |
| 103 | /** |
| 104 | * The preference that displays the accounts. |
| 105 | */ |
| 106 | public PreferenceGroup preferenceGroup; |
| 107 | /** |
| 108 | * The preference that displays the add account button. |
| 109 | */ |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 110 | public RestrictedPreference addAccountPreference; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 111 | /** |
| 112 | * The preference that displays the button to remove the managed profile |
| 113 | */ |
| 114 | public RestrictedPreference removeWorkProfilePreference; |
| 115 | /** |
| 116 | * The preference that displays managed profile settings. |
| 117 | */ |
| 118 | public Preference managedProfilePreference; |
| 119 | /** |
| 120 | * The {@link AuthenticatorHelper} that holds accounts data for this profile. |
| 121 | */ |
| 122 | public AuthenticatorHelper authenticatorHelper; |
| 123 | /** |
| 124 | * The {@link UserInfo} of the profile. |
| 125 | */ |
| 126 | public UserInfo userInfo; |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 127 | /** |
| 128 | * The {@link UserInfo} of the profile. |
| 129 | */ |
| 130 | public boolean pendingRemoval; |
| 131 | /** |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 132 | * The map from account key to account preference |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 133 | */ |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 134 | public ArrayMap<String, AccountTypePreference> accountPreferences = new ArrayMap<>(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 137 | public AccountPreferenceController(Context context, SettingsPreferenceFragment parent, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 138 | String[] authorities, @ProfileSelectFragment.ProfileType int type) { |
| 139 | this(context, parent, authorities, new AccountRestrictionHelper(context), type); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @VisibleForTesting |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 143 | AccountPreferenceController(Context context, SettingsPreferenceFragment parent, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 144 | String[] authorities, AccountRestrictionHelper helper, |
| 145 | @ProfileSelectFragment.ProfileType int type) { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 146 | super(context); |
| 147 | mUm = (UserManager) context.getSystemService(Context.USER_SERVICE); |
| 148 | mAuthorities = authorities; |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 149 | mFragment = parent; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 150 | if (mAuthorities != null) { |
| 151 | mAuthoritiesCount = mAuthorities.length; |
| 152 | } |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 153 | final FeatureFactory featureFactory = FeatureFactory.getFactory(mContext); |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 154 | mMetricsFeatureProvider = featureFactory.getMetricsFeatureProvider(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 155 | mHelper = helper; |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 156 | mType = type; |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public boolean isAvailable() { |
| 161 | return !mUm.isManagedProfile(); |
| 162 | } |
| 163 | |
| 164 | @Override |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 165 | public String getPreferenceKey() { |
| 166 | return null; |
| 167 | } |
| 168 | |
| 169 | @Override |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 170 | public void displayPreference(PreferenceScreen screen) { |
| 171 | super.displayPreference(screen); |
| 172 | updateUi(); |
| 173 | } |
| 174 | |
| 175 | @Override |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 176 | public void updateDynamicRawDataToIndex(List<SearchIndexableRaw> rawData) { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 177 | if (!isAvailable()) { |
| 178 | return; |
| 179 | } |
| 180 | final Resources res = mContext.getResources(); |
| 181 | final String screenTitle = res.getString(R.string.account_settings_title); |
| 182 | |
| 183 | List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId()); |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 184 | for (final UserInfo userInfo : profiles) { |
| 185 | if (userInfo.isEnabled() && userInfo.isManagedProfile()) { |
| 186 | if (!mHelper.hasBaseUserRestriction(DISALLOW_REMOVE_MANAGED_PROFILE, |
| 187 | UserHandle.myUserId())) { |
| 188 | final SearchIndexableRaw data = new SearchIndexableRaw(mContext); |
| 189 | data.key = PREF_KEY_REMOVE_PROFILE; |
| 190 | data.title = res.getString(R.string.remove_managed_profile_label); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 191 | data.screenTitle = screenTitle; |
| 192 | rawData.add(data); |
| 193 | } |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 194 | final SearchIndexableRaw data = new SearchIndexableRaw(mContext); |
| 195 | data.key = PREF_KEY_WORK_PROFILE_SETTING; |
| 196 | data.title = res.getString(R.string.managed_profile_settings_title); |
| 197 | data.screenTitle = screenTitle; |
| 198 | rawData.add(data); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onResume() { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 205 | updateUi(); |
| 206 | mManagedProfileBroadcastReceiver.register(mContext); |
| 207 | listenToAccountUpdates(); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public void onPause() { |
| 212 | stopListeningToAccountUpdates(); |
| 213 | mManagedProfileBroadcastReceiver.unregister(mContext); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public void onAccountsUpdate(UserHandle userHandle) { |
| 218 | final ProfileData profileData = mProfiles.get(userHandle.getIdentifier()); |
| 219 | if (profileData != null) { |
| 220 | updateAccountTypes(profileData); |
| 221 | } else { |
| 222 | Log.w(TAG, "Missing Settings screen for: " + userHandle.getIdentifier()); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | @Override |
| 227 | public boolean onPreferenceClick(Preference preference) { |
| 228 | // Check the preference |
| 229 | final int count = mProfiles.size(); |
| 230 | for (int i = 0; i < count; i++) { |
| 231 | ProfileData profileData = mProfiles.valueAt(i); |
| 232 | if (preference == profileData.addAccountPreference) { |
Tony Mantler | 1c9e2f6 | 2017-06-13 15:16:50 -0700 | [diff] [blame] | 233 | Intent intent = new Intent(ACTION_ADD_ACCOUNT); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 234 | intent.putExtra(EXTRA_USER, profileData.userInfo.getUserHandle()); |
| 235 | intent.putExtra(EXTRA_AUTHORITIES, mAuthorities); |
| 236 | mContext.startActivity(intent); |
| 237 | return true; |
| 238 | } |
| 239 | if (preference == profileData.removeWorkProfilePreference) { |
| 240 | final int userId = profileData.userInfo.id; |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 241 | RemoveUserFragment.newInstance(userId).show(mFragment.getFragmentManager(), |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 242 | "removeUser"); |
| 243 | return true; |
| 244 | } |
| 245 | if (preference == profileData.managedProfilePreference) { |
| 246 | Bundle arguments = new Bundle(); |
| 247 | arguments.putParcelable(Intent.EXTRA_USER, profileData.userInfo.getUserHandle()); |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 248 | new SubSettingLauncher(mContext) |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 249 | .setSourceMetricsCategory(mFragment.getMetricsCategory()) |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 250 | .setDestination(ManagedProfileSettings.class.getName()) |
hjchangliao | e86eec0 | 2018-05-02 13:01:07 +0800 | [diff] [blame] | 251 | .setTitleRes(R.string.managed_profile_settings_title) |
Fan Zhang | 7cf99f5 | 2018-02-16 10:37:37 -0800 | [diff] [blame] | 252 | .setArguments(arguments) |
| 253 | .launch(); |
| 254 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 255 | return true; |
| 256 | } |
| 257 | } |
| 258 | return false; |
| 259 | } |
| 260 | |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 261 | private void updateUi() { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 262 | if (!isAvailable()) { |
| 263 | // This should not happen |
| 264 | Log.e(TAG, "We should not be showing settings for a managed profile"); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 265 | return; |
| 266 | } |
| 267 | |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 268 | for (int i = 0, size = mProfiles.size(); i < size; i++) { |
| 269 | mProfiles.valueAt(i).pendingRemoval = true; |
| 270 | } |
James Lemieux | 22a39c2 | 2018-02-26 00:51:42 -0800 | [diff] [blame] | 271 | if (mUm.isRestrictedProfile()) { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 272 | // Restricted user or similar |
| 273 | UserInfo userInfo = mUm.getUserInfo(UserHandle.myUserId()); |
| 274 | updateProfileUi(userInfo); |
| 275 | } else { |
| 276 | List<UserInfo> profiles = mUm.getProfiles(UserHandle.myUserId()); |
| 277 | final int profilesCount = profiles.size(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 278 | for (int i = 0; i < profilesCount; i++) { |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 279 | if (profiles.get(i).isManagedProfile() |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 280 | && (mType & ProfileSelectFragment.ProfileType.WORK) != 0) { |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 281 | updateProfileUi(profiles.get(i)); |
| 282 | } else if (!profiles.get(i).isManagedProfile() |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 283 | && (mType & ProfileSelectFragment.ProfileType.PERSONAL) != 0) { |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 284 | updateProfileUi(profiles.get(i)); |
| 285 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 286 | } |
| 287 | } |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 288 | cleanUpPreferences(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 289 | |
| 290 | // Add all preferences, starting with one for the primary profile. |
| 291 | // Note that we're relying on the ordering given by the SparseArray keys, and on the |
| 292 | // value of UserHandle.USER_OWNER being smaller than all the rest. |
| 293 | final int profilesCount = mProfiles.size(); |
| 294 | for (int i = 0; i < profilesCount; i++) { |
| 295 | updateAccountTypes(mProfiles.valueAt(i)); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | private void updateProfileUi(final UserInfo userInfo) { |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 300 | if (mFragment.getPreferenceManager() == null) { |
Fan Zhang | 81a030f | 2017-01-11 10:46:45 -0800 | [diff] [blame] | 301 | return; |
| 302 | } |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 303 | final ProfileData data = mProfiles.get(userInfo.id); |
| 304 | if (data != null) { |
| 305 | data.pendingRemoval = false; |
Doris Ling | ce5f9c0 | 2018-07-23 16:13:39 -0700 | [diff] [blame] | 306 | data.userInfo = userInfo; |
Doris Ling | 7109067 | 2017-05-16 17:23:55 -0700 | [diff] [blame] | 307 | if (userInfo.isEnabled()) { |
| 308 | // recreate the authentication helper to refresh the list of enabled accounts |
| 309 | data.authenticatorHelper = |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 310 | new AuthenticatorHelper(mContext, userInfo.getUserHandle(), this); |
Doris Ling | 7109067 | 2017-05-16 17:23:55 -0700 | [diff] [blame] | 311 | } |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 312 | return; |
| 313 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 314 | final Context context = mContext; |
| 315 | final ProfileData profileData = new ProfileData(); |
| 316 | profileData.userInfo = userInfo; |
| 317 | AccessiblePreferenceCategory preferenceGroup = |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 318 | mHelper.createAccessiblePreferenceCategory( |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 319 | mFragment.getPreferenceManager().getContext()); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 320 | preferenceGroup.setOrder(mAccountProfileOrder++); |
| 321 | if (isSingleProfile()) { |
Doris Ling | 829d3aa | 2017-02-03 15:06:56 -0800 | [diff] [blame] | 322 | preferenceGroup.setTitle(context.getString(R.string.account_for_section_header, |
Fan Zhang | fa9849e | 2018-04-18 15:58:50 -0700 | [diff] [blame] | 323 | BidiFormatter.getInstance().unicodeWrap(userInfo.name))); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 324 | preferenceGroup.setContentDescription( |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 325 | mContext.getString(R.string.account_settings)); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 326 | } else if (userInfo.isManagedProfile()) { |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 327 | if (mType == ProfileSelectFragment.ProfileType.ALL) { |
Raff Tsai | f5277ba | 2019-11-07 14:53:54 +0800 | [diff] [blame] | 328 | preferenceGroup.setTitle(R.string.category_work); |
| 329 | final String workGroupSummary = getWorkGroupSummary(context, userInfo); |
| 330 | preferenceGroup.setSummary(workGroupSummary); |
| 331 | preferenceGroup.setContentDescription( |
| 332 | mContext.getString(R.string.accessibility_category_work, workGroupSummary)); |
| 333 | } |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 334 | profileData.removeWorkProfilePreference = newRemoveWorkProfilePreference(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 335 | mHelper.enforceRestrictionOnPreference(profileData.removeWorkProfilePreference, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 336 | DISALLOW_REMOVE_MANAGED_PROFILE, UserHandle.myUserId()); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 337 | profileData.managedProfilePreference = newManagedProfileSettings(); |
| 338 | } else { |
Raff Tsai | 1e5d814 | 2019-12-12 11:45:41 +0800 | [diff] [blame] | 339 | if (mType == ProfileSelectFragment.ProfileType.ALL) { |
Raff Tsai | f5277ba | 2019-11-07 14:53:54 +0800 | [diff] [blame] | 340 | preferenceGroup.setTitle(R.string.category_personal); |
| 341 | preferenceGroup.setContentDescription( |
| 342 | mContext.getString(R.string.accessibility_category_personal)); |
| 343 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 344 | } |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 345 | final PreferenceScreen screen = mFragment.getPreferenceScreen(); |
Fan Zhang | 424443b | 2017-01-10 11:50:01 -0800 | [diff] [blame] | 346 | if (screen != null) { |
| 347 | screen.addPreference(preferenceGroup); |
| 348 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 349 | profileData.preferenceGroup = preferenceGroup; |
| 350 | if (userInfo.isEnabled()) { |
| 351 | profileData.authenticatorHelper = new AuthenticatorHelper(context, |
| 352 | userInfo.getUserHandle(), this); |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 353 | profileData.addAccountPreference = newAddAccountPreference(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 354 | mHelper.enforceRestrictionOnPreference(profileData.addAccountPreference, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 355 | DISALLOW_MODIFY_ACCOUNTS, userInfo.id); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 356 | } |
| 357 | mProfiles.put(userInfo.id, profileData); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 360 | private RestrictedPreference newAddAccountPreference() { |
| 361 | RestrictedPreference preference = |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 362 | new RestrictedPreference(mFragment.getPreferenceManager().getContext()); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 363 | preference.setTitle(R.string.add_account_label); |
Amin Shaikh | 3f1de20 | 2019-05-01 17:29:02 -0400 | [diff] [blame] | 364 | preference.setIcon(R.drawable.ic_add_24dp); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 365 | preference.setOnPreferenceClickListener(this); |
| 366 | preference.setOrder(ORDER_NEXT_TO_NEXT_TO_LAST); |
| 367 | return preference; |
| 368 | } |
| 369 | |
Fan Zhang | 12b03b2 | 2017-10-29 13:57:29 -0700 | [diff] [blame] | 370 | private RestrictedPreference newRemoveWorkProfilePreference() { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 371 | RestrictedPreference preference = new RestrictedPreference( |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 372 | mFragment.getPreferenceManager().getContext()); |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 373 | preference.setKey(PREF_KEY_REMOVE_PROFILE); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 374 | preference.setTitle(R.string.remove_managed_profile_label); |
Antony Sargent | 0d9afde | 2018-05-17 11:55:40 -0700 | [diff] [blame] | 375 | preference.setIcon(R.drawable.ic_delete); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 376 | preference.setOnPreferenceClickListener(this); |
| 377 | preference.setOrder(ORDER_LAST); |
| 378 | return preference; |
| 379 | } |
| 380 | |
| 381 | |
| 382 | private Preference newManagedProfileSettings() { |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 383 | Preference preference = new Preference(mFragment.getPreferenceManager().getContext()); |
Stanley Wang | 8015cba | 2019-11-27 10:39:59 +0800 | [diff] [blame] | 384 | preference.setKey(PREF_KEY_WORK_PROFILE_SETTING); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 385 | preference.setTitle(R.string.managed_profile_settings_title); |
Fan Zhang | 41c4e5f | 2018-04-03 13:21:34 -0700 | [diff] [blame] | 386 | preference.setIcon(R.drawable.ic_settings_24dp); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 387 | preference.setOnPreferenceClickListener(this); |
| 388 | preference.setOrder(ORDER_NEXT_TO_LAST); |
| 389 | return preference; |
| 390 | } |
| 391 | |
| 392 | private String getWorkGroupSummary(Context context, UserInfo userInfo) { |
| 393 | PackageManager packageManager = context.getPackageManager(); |
| 394 | ApplicationInfo adminApplicationInfo = Utils.getAdminApplicationInfo(context, userInfo.id); |
| 395 | if (adminApplicationInfo == null) { |
| 396 | return null; |
| 397 | } |
| 398 | CharSequence appLabel = packageManager.getApplicationLabel(adminApplicationInfo); |
| 399 | return mContext.getString(R.string.managing_admin, appLabel); |
| 400 | } |
| 401 | |
| 402 | void cleanUpPreferences() { |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 403 | PreferenceScreen screen = mFragment.getPreferenceScreen(); |
Fan Zhang | 424443b | 2017-01-10 11:50:01 -0800 | [diff] [blame] | 404 | if (screen == null) { |
| 405 | return; |
| 406 | } |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 407 | final int count = mProfiles.size(); |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 408 | for (int i = count - 1; i >= 0; i--) { |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 409 | final ProfileData data = mProfiles.valueAt(i); |
| 410 | if (data.pendingRemoval) { |
| 411 | screen.removePreference(data.preferenceGroup); |
| 412 | mProfiles.removeAt(i); |
| 413 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 414 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | private void listenToAccountUpdates() { |
| 418 | final int count = mProfiles.size(); |
| 419 | for (int i = 0; i < count; i++) { |
| 420 | AuthenticatorHelper authenticatorHelper = mProfiles.valueAt(i).authenticatorHelper; |
| 421 | if (authenticatorHelper != null) { |
| 422 | authenticatorHelper.listenToAccountUpdates(); |
| 423 | } |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | private void stopListeningToAccountUpdates() { |
| 428 | final int count = mProfiles.size(); |
| 429 | for (int i = 0; i < count; i++) { |
| 430 | AuthenticatorHelper authenticatorHelper = mProfiles.valueAt(i).authenticatorHelper; |
| 431 | if (authenticatorHelper != null) { |
| 432 | authenticatorHelper.stopListeningToAccountUpdates(); |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | private void updateAccountTypes(ProfileData profileData) { |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 438 | if (mFragment.getPreferenceManager() == null |
Fan Zhang | c7804c1 | 2017-01-13 14:57:01 -0800 | [diff] [blame] | 439 | || profileData.preferenceGroup.getPreferenceManager() == null) { |
Fan Zhang | 81a030f | 2017-01-11 10:46:45 -0800 | [diff] [blame] | 440 | // This could happen if activity is finishing |
| 441 | return; |
| 442 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 443 | if (profileData.userInfo.isEnabled()) { |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 444 | final ArrayMap<String, AccountTypePreference> preferenceToRemove = |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 445 | new ArrayMap<>(profileData.accountPreferences); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 446 | final ArrayList<AccountTypePreference> preferences = getAccountTypePreferences( |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 447 | profileData.authenticatorHelper, profileData.userInfo.getUserHandle(), |
| 448 | preferenceToRemove); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 449 | final int count = preferences.size(); |
| 450 | for (int i = 0; i < count; i++) { |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 451 | final AccountTypePreference preference = preferences.get(i); |
| 452 | preference.setOrder(i); |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 453 | final String key = preference.getKey(); |
| 454 | if (!profileData.accountPreferences.containsKey(key)) { |
| 455 | profileData.preferenceGroup.addPreference(preference); |
| 456 | profileData.accountPreferences.put(key, preference); |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 457 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 458 | } |
| 459 | if (profileData.addAccountPreference != null) { |
| 460 | profileData.preferenceGroup.addPreference(profileData.addAccountPreference); |
| 461 | } |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 462 | for (String key : preferenceToRemove.keySet()) { |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 463 | profileData.preferenceGroup.removePreference( |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 464 | profileData.accountPreferences.get(key)); |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 465 | profileData.accountPreferences.remove(key); |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 466 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 467 | } else { |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 468 | profileData.preferenceGroup.removeAll(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 469 | // Put a label instead of the accounts list |
| 470 | if (mProfileNotAvailablePreference == null) { |
| 471 | mProfileNotAvailablePreference = |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 472 | new Preference(mFragment.getPreferenceManager().getContext()); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 473 | } |
| 474 | mProfileNotAvailablePreference.setEnabled(false); |
| 475 | mProfileNotAvailablePreference.setIcon(R.drawable.empty_icon); |
| 476 | mProfileNotAvailablePreference.setTitle(null); |
| 477 | mProfileNotAvailablePreference.setSummary( |
| 478 | R.string.managed_profile_not_available_label); |
| 479 | profileData.preferenceGroup.addPreference(mProfileNotAvailablePreference); |
| 480 | } |
| 481 | if (profileData.removeWorkProfilePreference != null) { |
| 482 | profileData.preferenceGroup.addPreference(profileData.removeWorkProfilePreference); |
| 483 | } |
| 484 | if (profileData.managedProfilePreference != null) { |
| 485 | profileData.preferenceGroup.addPreference(profileData.managedProfilePreference); |
| 486 | } |
| 487 | } |
| 488 | |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 489 | private ArrayList<AccountTypePreference> getAccountTypePreferences(AuthenticatorHelper helper, |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 490 | UserHandle userHandle, ArrayMap<String, AccountTypePreference> preferenceToRemove) { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 491 | final String[] accountTypes = helper.getEnabledAccountTypes(); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 492 | final ArrayList<AccountTypePreference> accountTypePreferences = |
Fan Zhang | c6ca314 | 2017-02-14 15:02:35 -0800 | [diff] [blame] | 493 | new ArrayList<>(accountTypes.length); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 494 | |
| 495 | for (int i = 0; i < accountTypes.length; i++) { |
| 496 | final String accountType = accountTypes[i]; |
| 497 | // Skip showing any account that does not have any of the requested authorities |
| 498 | if (!accountTypeHasAnyRequestedAuthorities(helper, accountType)) { |
| 499 | continue; |
| 500 | } |
| 501 | final CharSequence label = helper.getLabelForType(mContext, accountType); |
| 502 | if (label == null) { |
| 503 | continue; |
| 504 | } |
| 505 | final String titleResPackageName = helper.getPackageForType(accountType); |
| 506 | final int titleResId = helper.getLabelIdForType(accountType); |
| 507 | |
| 508 | final Account[] accounts = AccountManager.get(mContext) |
| 509 | .getAccountsByTypeAsUser(accountType, userHandle); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 510 | final Drawable icon = helper.getDrawableForType(mContext, accountType); |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 511 | final Context prefContext = mFragment.getPreferenceManager().getContext(); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 512 | |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 513 | // Add a preference row for each individual account |
| 514 | for (Account account : accounts) { |
Fan Zhang | c754f59 | 2017-05-17 12:32:59 -0700 | [diff] [blame] | 515 | final AccountTypePreference preference = |
| 516 | preferenceToRemove.remove(AccountTypePreference.buildKey(account)); |
Doris Ling | 633250b | 2017-05-02 11:28:14 -0700 | [diff] [blame] | 517 | if (preference != null) { |
| 518 | accountTypePreferences.add(preference); |
| 519 | continue; |
| 520 | } |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 521 | final ArrayList<String> auths = |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 522 | helper.getAuthoritiesForAccountType(account.type); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 523 | if (!AccountRestrictionHelper.showAccount(mAuthorities, auths)) { |
| 524 | continue; |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 525 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 526 | final Bundle fragmentArguments = new Bundle(); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 527 | fragmentArguments.putParcelable(AccountDetailDashboardFragment.KEY_ACCOUNT, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 528 | account); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 529 | fragmentArguments.putParcelable(AccountDetailDashboardFragment.KEY_USER_HANDLE, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 530 | userHandle); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 531 | fragmentArguments.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_TYPE, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 532 | accountType); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 533 | fragmentArguments.putString(AccountDetailDashboardFragment.KEY_ACCOUNT_LABEL, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 534 | label.toString()); |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 535 | fragmentArguments.putInt(AccountDetailDashboardFragment.KEY_ACCOUNT_TITLE_RES, |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 536 | titleResId); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 537 | fragmentArguments.putParcelable(EXTRA_USER, userHandle); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 538 | accountTypePreferences.add(new AccountTypePreference( |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 539 | prefContext, mMetricsFeatureProvider.getMetricsCategory(mFragment), |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 540 | account, titleResPackageName, titleResId, label, |
| 541 | AccountDetailDashboardFragment.class.getName(), fragmentArguments, icon)); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 542 | } |
| 543 | helper.preloadDrawableForType(mContext, accountType); |
| 544 | } |
| 545 | // Sort by label |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 546 | Collections.sort(accountTypePreferences, new Comparator<AccountTypePreference>() { |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 547 | @Override |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 548 | public int compare(AccountTypePreference t1, AccountTypePreference t2) { |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 549 | int result = t1.getSummary().toString().compareTo(t2.getSummary().toString()); |
Doris Ling | 20d4b04 | 2016-11-22 16:37:06 -0800 | [diff] [blame] | 550 | return result != 0 |
Raff Tsai | 6586629 | 2019-10-05 09:32:19 +0800 | [diff] [blame] | 551 | ? result : t1.getTitle().toString().compareTo(t2.getTitle().toString()); |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 552 | } |
| 553 | }); |
| 554 | return accountTypePreferences; |
| 555 | } |
| 556 | |
| 557 | private boolean accountTypeHasAnyRequestedAuthorities(AuthenticatorHelper helper, |
| 558 | String accountType) { |
| 559 | if (mAuthoritiesCount == 0) { |
| 560 | // No authorities required |
| 561 | return true; |
| 562 | } |
| 563 | final ArrayList<String> authoritiesForType = helper.getAuthoritiesForAccountType( |
| 564 | accountType); |
| 565 | if (authoritiesForType == null) { |
| 566 | Log.d(TAG, "No sync authorities for account type: " + accountType); |
| 567 | return false; |
| 568 | } |
| 569 | for (int j = 0; j < mAuthoritiesCount; j++) { |
| 570 | if (authoritiesForType.contains(mAuthorities[j])) { |
| 571 | return true; |
| 572 | } |
| 573 | } |
| 574 | return false; |
| 575 | } |
| 576 | |
| 577 | private boolean isSingleProfile() { |
| 578 | return mUm.isLinkedUser() || mUm.getProfiles(UserHandle.myUserId()).size() == 1; |
| 579 | } |
| 580 | |
| 581 | private class ManagedProfileBroadcastReceiver extends BroadcastReceiver { |
| 582 | private boolean mListeningToManagedProfileEvents; |
| 583 | |
| 584 | @Override |
| 585 | public void onReceive(Context context, Intent intent) { |
| 586 | final String action = intent.getAction(); |
| 587 | Log.v(TAG, "Received broadcast: " + action); |
| 588 | if (action.equals(Intent.ACTION_MANAGED_PROFILE_REMOVED) |
| 589 | || action.equals(Intent.ACTION_MANAGED_PROFILE_ADDED)) { |
Raff Tsai | 5c075ba | 2019-12-16 12:30:05 +0800 | [diff] [blame] | 590 | if (mFragment instanceof AccountWorkProfileDashboardFragment) { |
| 591 | mFragment.getActivity().finish(); |
| 592 | } else { |
| 593 | // Clean old state |
| 594 | stopListeningToAccountUpdates(); |
| 595 | // Build new state |
| 596 | updateUi(); |
| 597 | listenToAccountUpdates(); |
| 598 | } |
Doris Ling | bfac31b | 2016-11-10 15:12:52 -0800 | [diff] [blame] | 599 | return; |
| 600 | } |
| 601 | Log.w(TAG, "Cannot handle received broadcast: " + intent.getAction()); |
| 602 | } |
| 603 | |
| 604 | public void register(Context context) { |
| 605 | if (!mListeningToManagedProfileEvents) { |
| 606 | IntentFilter intentFilter = new IntentFilter(); |
| 607 | intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_REMOVED); |
| 608 | intentFilter.addAction(Intent.ACTION_MANAGED_PROFILE_ADDED); |
| 609 | context.registerReceiver(this, intentFilter); |
| 610 | mListeningToManagedProfileEvents = true; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | public void unregister(Context context) { |
| 615 | if (mListeningToManagedProfileEvents) { |
| 616 | context.unregisterReceiver(this); |
| 617 | mListeningToManagedProfileEvents = false; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | } |