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