From d007ff9395e93c9630388262e29e0f1d7a7ab735 Mon Sep 17 00:00:00 2001 From: Doris Ling Date: Wed, 5 Apr 2017 16:03:02 -0700 Subject: [PATCH] Add subtext to Default apps. Retrieve the default Sms app, default Browser app, and default Phone app and append the label together to form the summary text for Default apps settings. Bug: 36376411 Test: make RunSettingsRoboTests Change-Id: I8ccf71dde43bba04c4bc4a900c9a181513e7710b --- .../applications/AdvancedAppSettings.java | 54 +++++++++++ .../DefaultAppPreferenceController.java | 16 +++- .../DefaultBrowserPreferenceController.java | 23 +++-- .../applications/AdvancedAppSettingsTest.java | 90 +++++++++++++++++-- 4 files changed, 167 insertions(+), 16 deletions(-) diff --git a/src/com/android/settings/applications/AdvancedAppSettings.java b/src/com/android/settings/applications/AdvancedAppSettings.java index e59f94b400f..caa9da1411e 100644 --- a/src/com/android/settings/applications/AdvancedAppSettings.java +++ b/src/com/android/settings/applications/AdvancedAppSettings.java @@ -15,9 +15,11 @@ */ package com.android.settings.applications; +import android.app.Activity; import android.content.Context; import android.provider.SearchIndexableResource; +import android.text.TextUtils; import com.android.internal.logging.nano.MetricsProto.MetricsEvent; import com.android.settings.R; import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController; @@ -29,6 +31,7 @@ import com.android.settings.applications.defaultapps.DefaultWorkBrowserPreferenc import com.android.settings.applications.defaultapps.DefaultWorkPhonePreferenceController; import com.android.settings.core.PreferenceController; import com.android.settings.dashboard.DashboardFragment; +import com.android.settings.dashboard.SummaryLoader; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.Indexable; @@ -78,4 +81,55 @@ public class AdvancedAppSettings extends DashboardFragment { return Arrays.asList(sir); } }; + + static class SummaryProvider implements SummaryLoader.SummaryProvider { + + private final Context mContext; + private final SummaryLoader mSummaryLoader; + private final DefaultSmsPreferenceController mDefaultSmsPreferenceController; + private final DefaultBrowserPreferenceController mDefaultBrowserPreferenceController; + private final DefaultPhonePreferenceController mDefaultPhonePreferenceController; + + public SummaryProvider(Context context, SummaryLoader summaryLoader) { + mContext = context; + mSummaryLoader = summaryLoader; + mDefaultSmsPreferenceController = new DefaultSmsPreferenceController(mContext); + mDefaultBrowserPreferenceController = new DefaultBrowserPreferenceController(mContext); + mDefaultPhonePreferenceController = new DefaultPhonePreferenceController(mContext); + } + + @Override + public void setListening(boolean listening) { + if (!listening) { + return; + } + CharSequence summary = concatSummaryText( + mDefaultSmsPreferenceController.getDefaultAppLabel(), + mDefaultBrowserPreferenceController.getDefaultAppLabel()); + summary = concatSummaryText(summary, + mDefaultPhonePreferenceController.getDefaultAppLabel()); + if (!TextUtils.isEmpty(summary)) { + mSummaryLoader.setSummary(this, summary); + } + } + + private CharSequence concatSummaryText(CharSequence summary1, CharSequence summary2) { + if (TextUtils.isEmpty(summary1)) { + return summary2; + } + if (TextUtils.isEmpty(summary2)) { + return summary1; + } + return mContext.getString(R.string.join_many_items_middle, summary1, summary2); + } + } + + public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY = + new SummaryLoader.SummaryProviderFactory() { + @Override + public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity, + SummaryLoader summaryLoader) { + return new AdvancedAppSettings.SummaryProvider(activity, summaryLoader); + } + }; } diff --git a/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java index 93fc6a601e9..b53a335fdb9 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultAppPreferenceController.java @@ -49,10 +49,7 @@ public abstract class DefaultAppPreferenceController extends PreferenceControlle @Override public void updateState(Preference preference) { final DefaultAppInfo app = getDefaultAppInfo(); - CharSequence defaultAppLabel = null; - if (app != null) { - defaultAppLabel = app.loadLabel(); - } + CharSequence defaultAppLabel = getDefaultAppLabel(); if (!TextUtils.isEmpty(defaultAppLabel)) { preference.setSummary(defaultAppLabel); } else { @@ -84,4 +81,15 @@ public abstract class DefaultAppPreferenceController extends PreferenceControlle //By default return null. It's up to subclasses to provide logic. return null; } + + public CharSequence getDefaultAppLabel() { + if (!isAvailable()) { + return null; + } + final DefaultAppInfo app = getDefaultAppInfo(); + if (app != null) { + return app.loadLabel(); + } + return null; + } } diff --git a/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java b/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java index 661fb5b24cc..5c0f9a3bf7a 100644 --- a/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java +++ b/src/com/android/settings/applications/defaultapps/DefaultBrowserPreferenceController.java @@ -51,13 +51,9 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont @Override public void updateState(Preference preference) { super.updateState(preference); - final DefaultAppInfo defaultApp = getDefaultAppInfo(); - final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null; - if (TextUtils.isEmpty(defaultAppLabel)) { - final String onlyAppLabel = getOnlyAppLabel(); - if (!TextUtils.isEmpty(onlyAppLabel)) { - preference.setSummary(onlyAppLabel); - } + final CharSequence defaultAppLabel = getDefaultAppLabel(); + if (!TextUtils.isEmpty(defaultAppLabel)) { + preference.setSummary(defaultAppLabel); } } @@ -72,6 +68,19 @@ public class DefaultBrowserPreferenceController extends DefaultAppPreferenceCont } } + @Override + public CharSequence getDefaultAppLabel() { + if (!isAvailable()) { + return null; + } + final DefaultAppInfo defaultApp = getDefaultAppInfo(); + final CharSequence defaultAppLabel = defaultApp != null ? defaultApp.loadLabel() : null; + if (!TextUtils.isEmpty(defaultAppLabel)) { + return defaultAppLabel; + } + return getOnlyAppLabel(); + } + private List getCandidates() { return mPackageManager.queryIntentActivitiesAsUser(BROWSE_PROBE, PackageManager.MATCH_ALL, mUserId); diff --git a/tests/robotests/src/com/android/settings/applications/AdvancedAppSettingsTest.java b/tests/robotests/src/com/android/settings/applications/AdvancedAppSettingsTest.java index b8f3fc420c9..bedb0a54ee6 100644 --- a/tests/robotests/src/com/android/settings/applications/AdvancedAppSettingsTest.java +++ b/tests/robotests/src/com/android/settings/applications/AdvancedAppSettingsTest.java @@ -22,22 +22,30 @@ import com.android.settings.R; import com.android.settings.SettingsRobolectricTestRunner; import com.android.settings.TestConfig; +import com.android.settings.applications.defaultapps.DefaultBrowserPreferenceController; +import com.android.settings.applications.defaultapps.DefaultPhonePreferenceController; +import com.android.settings.applications.defaultapps.DefaultSmsPreferenceController; +import com.android.settings.dashboard.SummaryLoader; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; -import org.mockito.Answers; -import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.robolectric.RuntimeEnvironment; import org.robolectric.annotation.Config; -import org.robolectric.shadows.ShadowApplication; +import org.robolectric.util.ReflectionHelpers; import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @RunWith(SettingsRobolectricTestRunner.class) @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) public class AdvancedAppSettingsTest { - @Mock(answer = Answers.RETURNS_DEEP_STUBS) private Context mContext; private AdvancedAppSettings mFragment; @@ -46,8 +54,9 @@ public class AdvancedAppSettingsTest { public void setUp() { MockitoAnnotations.initMocks(this); + mContext = RuntimeEnvironment.application; mFragment = new AdvancedAppSettings(); - mFragment.onAttach(ShadowApplication.getInstance().getApplicationContext()); + mFragment.onAttach(mContext); } @Test @@ -56,4 +65,75 @@ public class AdvancedAppSettingsTest { R.xml.app_default_settings); } + @Test + public void setListening_shouldUpdateSummary() { + final SummaryLoader summaryLoader = mock(SummaryLoader.class); + final AdvancedAppSettings.SummaryProvider summaryProvider = + new AdvancedAppSettings.SummaryProvider(mContext, summaryLoader); + final DefaultSmsPreferenceController defaultSms = + mock(DefaultSmsPreferenceController.class); + final DefaultBrowserPreferenceController defaultBrowser = + mock(DefaultBrowserPreferenceController.class); + final DefaultPhonePreferenceController defaultPhone = + mock(DefaultPhonePreferenceController.class); + ReflectionHelpers.setField(summaryProvider, "mDefaultSmsPreferenceController", defaultSms); + ReflectionHelpers.setField( + summaryProvider, "mDefaultBrowserPreferenceController", defaultBrowser); + ReflectionHelpers.setField( + summaryProvider, "mDefaultPhonePreferenceController", defaultPhone); + + // all available + when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1"); + when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1"); + when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1"); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Sms1, Browser1, Phone1"); + + // 2 available + when(defaultSms.getDefaultAppLabel()).thenReturn(null); + when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1"); + when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1"); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Browser1, Phone1"); + + when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1"); + when(defaultBrowser.getDefaultAppLabel()).thenReturn(null); + when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1"); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Sms1, Phone1"); + + when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1"); + when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1"); + when(defaultPhone.getDefaultAppLabel()).thenReturn(null); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Sms1, Browser1"); + + // 1 available + when(defaultSms.getDefaultAppLabel()).thenReturn(null); + when(defaultBrowser.getDefaultAppLabel()).thenReturn("Browser1"); + when(defaultPhone.getDefaultAppLabel()).thenReturn(null); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Browser1"); + + when(defaultSms.getDefaultAppLabel()).thenReturn("Sms1"); + when(defaultBrowser.getDefaultAppLabel()).thenReturn(null); + when(defaultPhone.getDefaultAppLabel()).thenReturn(null); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Sms1"); + + when(defaultSms.getDefaultAppLabel()).thenReturn(null); + when(defaultBrowser.getDefaultAppLabel()).thenReturn(null); + when(defaultPhone.getDefaultAppLabel()).thenReturn("Phone1"); + summaryProvider.setListening(true); + verify(summaryLoader).setSummary(summaryProvider, "Phone1"); + + // None available + when(defaultSms.getDefaultAppLabel()).thenReturn(null); + when(defaultBrowser.getDefaultAppLabel()).thenReturn(null); + when(defaultPhone.getDefaultAppLabel()).thenReturn(null); + summaryProvider.setListening(true); + verify(summaryLoader, never()).setSummary(summaryProvider, eq(anyString())); + + } + }