Commit b4fc4420 authored by Tanmoy Mollik's avatar Tanmoy Mollik Committed by Commit Bot

[Android] Get rid of explicit register/unregister in SignInPreference

Replace register and unregister with onAttached and onDetached methods
in SignInPreference

Bug: 1127990
Change-Id: If4f385691b44ea2845428c9c76143d566c82cce5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2409964
Commit-Queue: Tanmoy Mollik <triploblastic@chromium.org>
Reviewed-by: default avatarMarc Treib <treib@chromium.org>
Cr-Commit-Position: refs/heads/master@{#822664}
parent a08cc488
...@@ -131,7 +131,6 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -131,7 +131,6 @@ public class MainSettings extends PreferenceFragmentCompat
Profile.getLastUsedRegularProfile()); Profile.getLastUsedRegularProfile());
if (signinManager.isSigninSupported()) { if (signinManager.isSigninSupported()) {
signinManager.addSignInStateObserver(this); signinManager.addSignInStateObserver(this);
mSignInPreference.registerForUpdates();
} }
ProfileSyncService syncService = ProfileSyncService.get(); ProfileSyncService syncService = ProfileSyncService.get();
if (syncService != null) { if (syncService != null) {
...@@ -146,7 +145,6 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -146,7 +145,6 @@ public class MainSettings extends PreferenceFragmentCompat
Profile.getLastUsedRegularProfile()); Profile.getLastUsedRegularProfile());
if (signinManager.isSigninSupported()) { if (signinManager.isSigninSupported()) {
signinManager.removeSignInStateObserver(this); signinManager.removeSignInStateObserver(this);
mSignInPreference.unregisterForUpdates();
} }
ProfileSyncService syncService = ProfileSyncService.get(); ProfileSyncService syncService = ProfileSyncService.get();
if (syncService != null) { if (syncService != null) {
...@@ -409,14 +407,10 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -409,14 +407,10 @@ public class MainSettings extends PreferenceFragmentCompat
// "You and Google" section header if the personalized sync promo is shown. // "You and Google" section header if the personalized sync promo is shown.
boolean isShowingPersonalizedPromo = boolean isShowingPersonalizedPromo =
mSignInPreference.getState() == SignInPreference.State.PERSONALIZED_PROMO; mSignInPreference.getState() == SignInPreference.State.PERSONALIZED_PROMO;
if (ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)) { String prefName = ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
findPreference(PREF_ACCOUNT_AND_GOOGLE_SERVICES_SECTION) ? PREF_ACCOUNT_AND_GOOGLE_SERVICES_SECTION
.setVisible(!isShowingPersonalizedPromo); : PREF_ACCOUNT_SECTION;
} else if (isShowingPersonalizedPromo) { findPreference(prefName).setVisible(!isShowingPersonalizedPromo);
removePreferenceIfPresent(PREF_ACCOUNT_SECTION);
} else {
addPreferenceIfAbsent(PREF_ACCOUNT_SECTION);
}
} }
// TemplateUrlService.LoadListener implementation. // TemplateUrlService.LoadListener implementation.
......
...@@ -94,10 +94,10 @@ public class SignInPreference ...@@ -94,10 +94,10 @@ public class SignInPreference
mState = State.SIGNED_IN; mState = State.SIGNED_IN;
} }
/** @Override
* Starts listening for updates to the sign-in and sync state. public void onAttached() {
*/ super.onAttached();
public void registerForUpdates() {
mAccountManagerFacade.addObserver(this); mAccountManagerFacade.addObserver(this);
IdentityServicesProvider.get() IdentityServicesProvider.get()
.getSigninManager(Profile.getLastUsedRegularProfile()) .getSigninManager(Profile.getLastUsedRegularProfile())
...@@ -114,11 +114,10 @@ public class SignInPreference ...@@ -114,11 +114,10 @@ public class SignInPreference
update(); update();
} }
/** @Override
* Stops listening for updates to the sign-in and sync state. Every call to registerForUpdates() public void onDetached() {
* must be matched with a call to this method. super.onDetached();
*/
public void unregisterForUpdates() {
mAccountManagerFacade.removeObserver(this); mAccountManagerFacade.removeObserver(this);
IdentityServicesProvider.get() IdentityServicesProvider.get()
.getSigninManager(Profile.getLastUsedRegularProfile()) .getSigninManager(Profile.getLastUsedRegularProfile())
......
...@@ -337,7 +337,6 @@ public class SyncAndServicesSettings extends PreferenceFragmentCompat ...@@ -337,7 +337,6 @@ public class SyncAndServicesSettings extends PreferenceFragmentCompat
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
mProfileSyncService.addSyncStateChangedListener(this); mProfileSyncService.addSyncStateChangedListener(this);
mSigninPreference.registerForUpdates();
if (!mIsFromSigninScreen if (!mIsFromSigninScreen
|| IdentityServicesProvider.get() || IdentityServicesProvider.get()
...@@ -363,7 +362,6 @@ public class SyncAndServicesSettings extends PreferenceFragmentCompat ...@@ -363,7 +362,6 @@ public class SyncAndServicesSettings extends PreferenceFragmentCompat
public void onStop() { public void onStop() {
super.onStop(); super.onStop();
mSigninPreference.unregisterForUpdates();
mProfileSyncService.removeSyncStateChangedListener(this); mProfileSyncService.removeSyncStateChangedListener(this);
} }
......
...@@ -220,8 +220,8 @@ public class MainSettingsFragmentTest { ...@@ -220,8 +220,8 @@ public class MainSettingsFragmentTest {
// SignInPreference status check. // SignInPreference status check.
// As the user is not signed in, sign in promo will show, section header will be hidden. // As the user is not signed in, sign in promo will show, section header will be hidden.
Assert.assertNull("Account section header should be hidden.", Assert.assertFalse("Account section header should be hidden.",
mMainSettings.findPreference(MainSettings.PREF_ACCOUNT_SECTION)); mMainSettings.findPreference(MainSettings.PREF_ACCOUNT_SECTION).isVisible());
// Assert for "Basics" section // Assert for "Basics" section
assertSettingsExists(MainSettings.PREF_SEARCH_ENGINE, SearchEngineSettings.class); assertSettingsExists(MainSettings.PREF_SEARCH_ENGINE, SearchEngineSettings.class);
...@@ -379,8 +379,8 @@ public class MainSettingsFragmentTest { ...@@ -379,8 +379,8 @@ public class MainSettingsFragmentTest {
(SignInPreference) assertSettingsExists(MainSettings.PREF_SIGN_IN, null); (SignInPreference) assertSettingsExists(MainSettings.PREF_SIGN_IN, null);
Assert.assertEquals("SignInPreference should be at the personalized promo state. ", Assert.assertEquals("SignInPreference should be at the personalized promo state. ",
signInPreference.getState(), State.PERSONALIZED_PROMO); signInPreference.getState(), State.PERSONALIZED_PROMO);
Assert.assertNull("Account section header should be hidden when promo is shown.", Assert.assertFalse("Account section header should be hidden when promo is shown.",
mMainSettings.findPreference(MainSettings.PREF_ACCOUNT_SECTION)); mMainSettings.findPreference(MainSettings.PREF_ACCOUNT_SECTION).isVisible());
// SignIn to see the changes // SignIn to see the changes
mSyncTestRule.setUpAccountAndEnableSyncForTesting(); mSyncTestRule.setUpAccountAndEnableSyncForTesting();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment