Commit 413d5cb9 authored by Tanmoy Mollik's avatar Tanmoy Mollik Committed by Commit Bot

[Android] Dynamically change OnPreferenceClick action for sync row

The sync row in main settings page should launch sign-in screen when
there is no sync consent account in chrome. On the other hand when there
is an account present it should launch ManageSyncSettings page.

This cl adds a OnPreferenceClickListener for the sync row. Also the sync
row is always shown if there is a signed-in account present.

Bug: 1125464
Change-Id: I97fb6c3cca04654fde148e5c70b211fa3fc3bc27
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2398682
Commit-Queue: Tanmoy Mollik <triploblastic@chromium.org>
Reviewed-by: default avatarBoris Sazonov <bsazonov@chromium.org>
Reviewed-by: default avatarAlice Wang <aliceywang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807913}
parent 28c4ea55
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
android:key="manage_sync" android:key="manage_sync"
android:order="5" android:order="5"
android:title="@string/sync_category_title" android:title="@string/sync_category_title"
android:fragment="org.chromium.chrome.browser.sync.settings.ManageSyncSettings"
app:isPreferenceVisible="false"/> app:isPreferenceVisible="false"/>
<org.chromium.components.browser_ui.settings.ChromeBasePreference <org.chromium.components.browser_ui.settings.ChromeBasePreference
android:key="google_services" android:key="google_services"
......
...@@ -32,8 +32,10 @@ import org.chromium.chrome.browser.profiles.Profile; ...@@ -32,8 +32,10 @@ import org.chromium.chrome.browser.profiles.Profile;
import org.chromium.chrome.browser.safety_check.SafetyCheckSettingsFragment; import org.chromium.chrome.browser.safety_check.SafetyCheckSettingsFragment;
import org.chromium.chrome.browser.search_engines.TemplateUrlServiceFactory; import org.chromium.chrome.browser.search_engines.TemplateUrlServiceFactory;
import org.chromium.chrome.browser.signin.IdentityServicesProvider; import org.chromium.chrome.browser.signin.IdentityServicesProvider;
import org.chromium.chrome.browser.signin.SigninActivityLauncherImpl;
import org.chromium.chrome.browser.signin.SigninManager; import org.chromium.chrome.browser.signin.SigninManager;
import org.chromium.chrome.browser.sync.ProfileSyncService; import org.chromium.chrome.browser.sync.ProfileSyncService;
import org.chromium.chrome.browser.sync.settings.ManageSyncSettings;
import org.chromium.chrome.browser.sync.settings.SignInPreference; import org.chromium.chrome.browser.sync.settings.SignInPreference;
import org.chromium.chrome.browser.sync.settings.SyncPromoPreference; import org.chromium.chrome.browser.sync.settings.SyncPromoPreference;
import org.chromium.chrome.browser.sync.settings.SyncSettingsUtils; import org.chromium.chrome.browser.sync.settings.SyncSettingsUtils;
...@@ -43,6 +45,9 @@ import org.chromium.components.browser_ui.settings.ManagedPreferenceDelegate; ...@@ -43,6 +45,9 @@ import org.chromium.components.browser_ui.settings.ManagedPreferenceDelegate;
import org.chromium.components.browser_ui.settings.SettingsUtils; import org.chromium.components.browser_ui.settings.SettingsUtils;
import org.chromium.components.search_engines.TemplateUrl; import org.chromium.components.search_engines.TemplateUrl;
import org.chromium.components.search_engines.TemplateUrlService; import org.chromium.components.search_engines.TemplateUrlService;
import org.chromium.components.signin.base.CoreAccountInfo;
import org.chromium.components.signin.identitymanager.ConsentLevel;
import org.chromium.components.signin.metrics.SigninAccessPoint;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -271,16 +276,7 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -271,16 +276,7 @@ public class MainSettings extends PreferenceFragmentCompat
removePreferenceIfPresent(PREF_SIGN_IN); removePreferenceIfPresent(PREF_SIGN_IN);
} }
boolean hasPrimaryAccount = IdentityServicesProvider.get() updateSyncPreference();
.getIdentityManager(Profile.getLastUsedRegularProfile())
.hasPrimaryAccount();
boolean isSyncPromoHidden =
mSyncPromoPreference.getState() == SyncPromoPreference.State.PROMO_HIDDEN;
mManageSync.setVisible(
ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
&& hasPrimaryAccount && isSyncPromoHidden);
updateSyncAndServicesPreference();
updateSearchEnginePreference(); updateSearchEnginePreference();
Preference homepagePref = addPreferenceIfAbsent(PREF_HOMEPAGE); Preference homepagePref = addPreferenceIfAbsent(PREF_HOMEPAGE);
...@@ -314,15 +310,52 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -314,15 +310,52 @@ public class MainSettings extends PreferenceFragmentCompat
if (preference != null) getPreferenceScreen().removePreference(preference); if (preference != null) getPreferenceScreen().removePreference(preference);
} }
private void updateSyncPreference() {
if (ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)) {
updateManageSyncPreference();
} else {
updateSyncAndServicesPreference();
}
}
private void updateSyncAndServicesPreference() { private void updateSyncAndServicesPreference() {
ChromeBasePreference preference = findPreference( ChromeBasePreference preference = findPreference(PREF_SYNC_AND_SERVICES);
ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
? PREF_MANAGE_SYNC
: PREF_SYNC_AND_SERVICES);
preference.setIcon(SyncSettingsUtils.getSyncStatusIcon(getActivity())); preference.setIcon(SyncSettingsUtils.getSyncStatusIcon(getActivity()));
preference.setSummary(SyncSettingsUtils.getSyncStatusSummary(getActivity())); preference.setSummary(SyncSettingsUtils.getSyncStatusSummary(getActivity()));
} }
private void updateManageSyncPreference() {
String primaryAccountName = CoreAccountInfo.getEmailFrom(
IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile())
.getPrimaryAccountInfo(ConsentLevel.NOT_REQUIRED));
boolean showManageSync =
ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)
&& primaryAccountName != null;
mManageSync.setVisible(showManageSync);
if (!showManageSync) {
return;
}
boolean isSyncConsentAvailable =
IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile())
.getPrimaryAccountInfo(ConsentLevel.SYNC)
!= null;
mManageSync.setIcon(SyncSettingsUtils.getSyncStatusIcon(getActivity()));
mManageSync.setSummary(SyncSettingsUtils.getSyncStatusSummary(getActivity()));
mManageSync.setOnPreferenceClickListener(pref -> {
if (isSyncConsentAvailable) {
SettingsLauncher settingsLauncher = new SettingsLauncherImpl();
settingsLauncher.launchSettingsActivity(getContext(), ManageSyncSettings.class);
} else {
SigninActivityLauncherImpl.get().launchActivityForPromoDefaultFlow(
getContext(), SigninAccessPoint.SETTINGS, primaryAccountName);
}
return true;
});
}
private void updateSearchEnginePreference() { private void updateSearchEnginePreference() {
if (!TemplateUrlServiceFactory.get().isLoaded()) { if (!TemplateUrlServiceFactory.get().isLoaded()) {
ChromeBasePreference searchEnginePref = ChromeBasePreference searchEnginePref =
...@@ -391,7 +424,7 @@ public class MainSettings extends PreferenceFragmentCompat ...@@ -391,7 +424,7 @@ public class MainSettings extends PreferenceFragmentCompat
@Override @Override
public void syncStateChanged() { public void syncStateChanged() {
updateSyncAndServicesPreference(); updateSyncPreference();
} }
@VisibleForTesting @VisibleForTesting
......
...@@ -202,19 +202,23 @@ public class SyncSettingsUtils { ...@@ -202,19 +202,23 @@ public class SyncSettingsUtils {
* Return a short summary of the current sync status. * Return a short summary of the current sync status.
*/ */
public static String getSyncStatusSummary(Context context) { public static String getSyncStatusSummary(Context context) {
Resources res = context.getResources();
if (!IdentityServicesProvider.get() if (!IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile()) .getIdentityManager(Profile.getLastUsedRegularProfile())
.hasPrimaryAccount()) { .hasPrimaryAccount()) {
if (ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY)) {
// There is no account with sync consent available.
return res.getString(R.string.sync_is_disabled);
}
return ""; return "";
} }
ProfileSyncService profileSyncService = ProfileSyncService.get();
Resources res = context.getResources();
if (!AndroidSyncSettings.get().doesMasterSyncSettingAllowChromeSync()) { if (!AndroidSyncSettings.get().doesMasterSyncSettingAllowChromeSync()) {
return res.getString(R.string.sync_android_system_sync_disabled); return res.getString(R.string.sync_android_system_sync_disabled);
} }
ProfileSyncService profileSyncService = ProfileSyncService.get();
if (profileSyncService == null) { if (profileSyncService == null) {
return res.getString(R.string.sync_is_disabled); return res.getString(R.string.sync_is_disabled);
} }
...@@ -265,14 +269,16 @@ public class SyncSettingsUtils { ...@@ -265,14 +269,16 @@ public class SyncSettingsUtils {
* Returns an icon that represents the current sync state. * Returns an icon that represents the current sync state.
*/ */
public static @Nullable Drawable getSyncStatusIcon(Context context) { public static @Nullable Drawable getSyncStatusIcon(Context context) {
boolean useNewIcon =
ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY);
if (!IdentityServicesProvider.get() if (!IdentityServicesProvider.get()
.getIdentityManager(Profile.getLastUsedRegularProfile()) .getIdentityManager(Profile.getLastUsedRegularProfile())
.hasPrimaryAccount()) { .hasPrimaryAccount()) {
return null; return useNewIcon ? AppCompatResources.getDrawable(context, R.drawable.ic_sync_off_48dp)
: null;
} }
boolean useNewIcon =
ChromeFeatureList.isEnabled(ChromeFeatureList.MOBILE_IDENTITY_CONSISTENCY);
ProfileSyncService profileSyncService = ProfileSyncService.get(); ProfileSyncService profileSyncService = ProfileSyncService.get();
if (profileSyncService == null || !AndroidSyncSettings.get().isSyncEnabled()) { if (profileSyncService == null || !AndroidSyncSettings.get().isSyncEnabled()) {
return useNewIcon return useNewIcon
......
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