Show Styles & Wallpaper in Settings

Show "Styles & Wallpaper" in Settings based on whether the ThemePicker
component is availalable. Also update dashboard summary for display.

Bug: 129874298
Test: m RunSettingsRoboTests
Change-Id: Id7e0bb9cbc689bb9e637919a10a7d1006397afab
diff --git a/res/values/config.xml b/res/values/config.xml
index cdf2810..496b5d2 100755
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -47,6 +47,8 @@
     <!-- Package name and fully-qualified class name for the wallpaper picker activity. -->
     <string name="config_wallpaper_picker_package" translatable="false">com.android.settings</string>
     <string name="config_wallpaper_picker_class" translatable="false">com.android.settings.Settings$WallpaperSettingsActivity</string>
+    <!-- Fully-qualified class name for the styles & wallpaper picker activity. -->
+    <string name="config_styles_and_wallpaper_picker_class" translatable="false"></string>
 
     <!-- Manufacturer backup settings to launch -->
     <string name="config_backup_settings_intent" translatable="false"></string>
diff --git a/res/xml/display_settings.xml b/res/xml/display_settings.xml
index 940e6de..a612a47 100644
--- a/res/xml/display_settings.xml
+++ b/res/xml/display_settings.xml
@@ -53,9 +53,6 @@
         settings:keywords="@string/keywords_display_wallpaper"
         settings:useAdminDisabledSummary="true"
         settings:controller="com.android.settings.display.WallpaperPreferenceController">
-        <intent
-            android:targetPackage="@string/config_wallpaper_picker_package"
-            android:targetClass="@string/config_wallpaper_picker_class" />
     </com.android.settingslib.RestrictedPreference>
 
 
diff --git a/src/com/android/settings/display/TopLevelDisplayPreferenceController.java b/src/com/android/settings/display/TopLevelDisplayPreferenceController.java
index ed85a4a..fbaea93 100644
--- a/src/com/android/settings/display/TopLevelDisplayPreferenceController.java
+++ b/src/com/android/settings/display/TopLevelDisplayPreferenceController.java
@@ -36,11 +36,15 @@
 
     @Override
     public CharSequence getSummary() {
-        if (new WallpaperPreferenceController(mContext, "dummy_key").isAvailable()) {
-            return mContext.getText(R.string.display_dashboard_summary);
+        final WallpaperPreferenceController controller =
+                new WallpaperPreferenceController(mContext, "dummy_key");
+        if (controller.isAvailable()) {
+            return mContext.getText(
+                    controller.areStylesAvailable()
+                    ? R.string.display_dashboard_summary_with_style
+                    : R.string.display_dashboard_summary);
         } else {
             return mContext.getText(R.string.display_dashboard_nowallpaper_summary);
         }
     }
-
 }
diff --git a/src/com/android/settings/display/WallpaperPreferenceController.java b/src/com/android/settings/display/WallpaperPreferenceController.java
index 0b09124..126be11 100644
--- a/src/com/android/settings/display/WallpaperPreferenceController.java
+++ b/src/com/android/settings/display/WallpaperPreferenceController.java
@@ -25,6 +25,7 @@
 import android.util.Log;
 
 import androidx.preference.Preference;
+import androidx.preference.PreferenceScreen;
 
 import com.android.settings.R;
 import com.android.settings.core.BasePreferenceController;
@@ -34,16 +35,26 @@
 import java.util.List;
 
 public class WallpaperPreferenceController extends BasePreferenceController {
-
     private static final String TAG = "WallpaperPrefController";
 
     private final String mWallpaperPackage;
     private final String mWallpaperClass;
+    private final String mStylesAndWallpaperClass;
 
     public WallpaperPreferenceController(Context context, String key) {
         super(context, key);
         mWallpaperPackage = mContext.getString(R.string.config_wallpaper_picker_package);
         mWallpaperClass = mContext.getString(R.string.config_wallpaper_picker_class);
+        mStylesAndWallpaperClass =
+                mContext.getString(R.string.config_styles_and_wallpaper_picker_class);
+    }
+
+    @Override
+    public void displayPreference(PreferenceScreen screen) {
+        super.displayPreference(screen);
+        Preference preference = screen.findPreference(getPreferenceKey());
+        preference.setTitle(mContext.getString(areStylesAvailable()
+                ? R.string.style_and_wallpaper_settings_title : R.string.wallpaper_settings_title));
     }
 
     @Override
@@ -52,14 +63,7 @@
             Log.e(TAG, "No Wallpaper picker specified!");
             return UNSUPPORTED_ON_DEVICE;
         }
-        final ComponentName componentName =
-                new ComponentName(mWallpaperPackage, mWallpaperClass);
-        final PackageManager pm = mContext.getPackageManager();
-        final Intent intent = new Intent();
-        intent.setComponent(componentName);
-        final List<ResolveInfo> resolveInfos =
-                pm.queryIntentActivities(intent, 0 /* flags */);
-        return resolveInfos != null && !resolveInfos.isEmpty()
+        return canResolveWallpaperComponent(mWallpaperClass)
                 ? AVAILABLE_UNSEARCHABLE : CONDITIONALLY_UNAVAILABLE;
     }
 
@@ -68,6 +72,31 @@
         disablePreferenceIfManaged((RestrictedPreference) preference);
     }
 
+    @Override
+    public boolean handlePreferenceTreeClick(Preference preference) {
+        if (getPreferenceKey().equals(preference.getKey())) {
+            final ComponentName componentName = new ComponentName(mWallpaperPackage,
+                    areStylesAvailable() ? mStylesAndWallpaperClass : mWallpaperClass);
+            preference.getContext().startActivity(new Intent().setComponent(componentName));
+            return true;
+        }
+        return super.handlePreferenceTreeClick(preference);
+    }
+
+    /** Returns whether Styles & Wallpaper is enabled and available. */
+    public boolean areStylesAvailable() {
+        return !TextUtils.isEmpty(mStylesAndWallpaperClass)
+                && canResolveWallpaperComponent(mStylesAndWallpaperClass);
+    }
+
+    private boolean canResolveWallpaperComponent(String className) {
+        final ComponentName componentName = new ComponentName(mWallpaperPackage, className);
+        final PackageManager pm = mContext.getPackageManager();
+        final Intent intent = new Intent().setComponent(componentName);
+        final List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0 /* flags */);
+        return resolveInfos != null && !resolveInfos.isEmpty();
+    }
+
     private void disablePreferenceIfManaged(RestrictedPreference pref) {
         final String restriction = DISALLOW_SET_WALLPAPER;
         if (pref != null) {
diff --git a/tests/robotests/src/com/android/settings/display/TopLevelDisplayPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/TopLevelDisplayPreferenceControllerTest.java
index 23d794c..d16a6d2 100644
--- a/tests/robotests/src/com/android/settings/display/TopLevelDisplayPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/TopLevelDisplayPreferenceControllerTest.java
@@ -39,14 +39,13 @@
 import org.junit.runner.RunWith;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import org.robolectric.annotation.Config;
-import org.robolectric.RobolectricTestRunner;
-import org.robolectric.RuntimeEnvironment;
-
 @RunWith(RobolectricTestRunner.class)
 public class TopLevelDisplayPreferenceControllerTest {
     private Context mContext;
@@ -89,6 +88,19 @@
     }
 
     @Test
+    public void getSummary_hasWallpaperWithStyles_shouldReturnWallpaperSummary() {
+        when(mContext.getString(R.string.config_styles_and_wallpaper_picker_class))
+                .thenReturn("any.nonempty.class");
+        final List<ResolveInfo> resolveInfos = new ArrayList<>();
+        resolveInfos.add(mock(ResolveInfo.class));
+        when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
+                .thenReturn(resolveInfos);
+
+        assertThat(mController.getSummary())
+                .isEqualTo(mContext.getText(R.string.display_dashboard_summary_with_style));
+    }
+
+    @Test
     public void getSummary_hasWallpaper_shouldReturnNoWallpaperSummary() {
         final List<ResolveInfo> resolveInfos = new ArrayList<>();
         when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
diff --git a/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java b/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java
index a641b19..ca9dfcc 100644
--- a/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java
+++ b/tests/robotests/src/com/android/settings/display/WallpaperPreferenceControllerTest.java
@@ -18,74 +18,132 @@
 
 import static com.google.common.truth.Truth.assertThat;
 
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyInt;
 import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
 
-import android.content.Context;
+import android.content.ComponentName;
 import android.content.Intent;
-import android.content.pm.PackageManager;
 import android.content.pm.ResolveInfo;
 
+import androidx.fragment.app.FragmentActivity;
+import androidx.preference.Preference;
+
 import com.android.settings.R;
+import com.android.settings.testutils.shadow.SettingsShadowResources;
+
+import com.google.common.collect.Lists;
 
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-
-import java.util.ArrayList;
-import java.util.List;
+import org.robolectric.Robolectric;
 import org.robolectric.RobolectricTestRunner;
+import org.robolectric.Shadows;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowPackageManager;
 
 @RunWith(RobolectricTestRunner.class)
+@Config(shadows = {SettingsShadowResources.class})
 public class WallpaperPreferenceControllerTest {
-
-    private static final String WALLPAPER_PACKAGE = "TestPkg";
-    private static final String WALLPAPER_CLASS = "TestCls";
     private static final String TEST_KEY = "test_key";
 
-    @Mock
-    private Context mContext;
-    @Mock
-    private PackageManager mPackageManager;
+    private Intent mWallpaperIntent;
+    private Intent mStylesAndWallpaperIntent;
+    private FragmentActivity mContext;
+    private ShadowPackageManager mShadowPackageManager;
 
     private WallpaperPreferenceController mController;
 
     @Before
     public void setUp() {
         MockitoAnnotations.initMocks(this);
-        when(mContext.getString(R.string.config_wallpaper_picker_package))
-                .thenReturn(WALLPAPER_PACKAGE);
-        when(mContext.getString(R.string.config_wallpaper_picker_class))
-                .thenReturn(WALLPAPER_CLASS);
-        when(mContext.getPackageManager()).thenReturn(mPackageManager);
-
+        mContext = Robolectric.buildActivity(FragmentActivity.class).get();
+        SettingsShadowResources.overrideResource(
+                R.string.config_wallpaper_picker_package, "bogus.package.for.testing");
+        SettingsShadowResources.overrideResource(
+                R.string.config_styles_and_wallpaper_picker_class, "bogus.package.class");
+        mWallpaperIntent =  new Intent().setComponent(new ComponentName(
+                mContext.getString(R.string.config_wallpaper_picker_package),
+                mContext.getString(R.string.config_wallpaper_picker_class)));
+        mStylesAndWallpaperIntent = new Intent().setComponent(new ComponentName(
+                mContext.getString(R.string.config_wallpaper_picker_package),
+                mContext.getString(R.string.config_styles_and_wallpaper_picker_class)));
+        mShadowPackageManager = Shadows.shadowOf(mContext.getPackageManager());
         mController = new WallpaperPreferenceController(mContext, TEST_KEY);
     }
 
     @Test
     public void isAvailable_wallpaperPickerEnabled_shouldReturnTrue() {
-        final List<ResolveInfo> resolveInfos = new ArrayList<>();
-        resolveInfos.add(mock(ResolveInfo.class));
-        when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
-                .thenReturn(resolveInfos);
+        mShadowPackageManager.setResolveInfosForIntent(
+                mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
 
         assertThat(mController.isAvailable()).isTrue();
     }
 
     @Test
     public void isAvailable_wallpaperPickerDisabled_shouldReturnFalse() {
-        when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt())).thenReturn(null);
+        mShadowPackageManager.setResolveInfosForIntent(
+                mWallpaperIntent, Lists.newArrayList());
 
         assertThat(mController.isAvailable()).isFalse();
+    }
 
-        final List<ResolveInfo> resolveInfos = new ArrayList<>();
-        when(mPackageManager.queryIntentActivities(any(Intent.class), anyInt()))
-                .thenReturn(resolveInfos);
+    @Test
+    public void areStylesAvailable_noComponentSpecified() {
+        SettingsShadowResources.overrideResource(
+                R.string.config_styles_and_wallpaper_picker_class, "");
+        mShadowPackageManager.setResolveInfosForIntent(
+                mStylesAndWallpaperIntent, Lists.newArrayList());
 
-        assertThat(mController.isAvailable()).isFalse();
+        assertThat(mController.areStylesAvailable()).isFalse();
+    }
+
+    @Test
+    public void areStylesAvailable_componentUnresolveable() {
+        mShadowPackageManager.setResolveInfosForIntent(
+                mStylesAndWallpaperIntent, Lists.newArrayList());
+
+        assertThat(mController.areStylesAvailable()).isFalse();
+    }
+
+    @Test
+    public void areStylesAvailable_componentResolved() {
+        mShadowPackageManager.setResolveInfosForIntent(
+                mStylesAndWallpaperIntent,
+                Lists.newArrayList(mock(ResolveInfo.class)));
+
+        assertThat(mController.areStylesAvailable()).isTrue();
+    }
+
+    @Test
+    public void handlePreferenceTreeClick_wallpaperOnly() {
+        mShadowPackageManager.setResolveInfosForIntent(
+                mWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
+        mShadowPackageManager.setResolveInfosForIntent(
+                mStylesAndWallpaperIntent, Lists.newArrayList());
+        Preference preference = new Preference(mContext);
+        preference.setKey(TEST_KEY);
+
+        mController.handlePreferenceTreeClick(preference);
+
+        assertThat(Shadows.shadowOf(mContext)
+                .getNextStartedActivityForResult().intent.getComponent().getClassName())
+                .isEqualTo(mContext.getString(R.string.config_wallpaper_picker_class));
+    }
+
+    @Test
+    public void handlePreferenceTreeClick_stylesAndWallpaper() {
+        mShadowPackageManager.setResolveInfosForIntent(
+                mWallpaperIntent, Lists.newArrayList());
+        mShadowPackageManager.setResolveInfosForIntent(
+                mStylesAndWallpaperIntent, Lists.newArrayList(mock(ResolveInfo.class)));
+        Preference preference = new Preference(mContext);
+        preference.setKey(TEST_KEY);
+
+        mController.handlePreferenceTreeClick(preference);
+
+        assertThat(Shadows.shadowOf(mContext)
+                .getNextStartedActivityForResult().intent.getComponent().getClassName())
+                .isEqualTo(mContext.getString(R.string.config_styles_and_wallpaper_picker_class));
     }
 }
diff --git a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java
index b68a4ee..8ca577c 100644
--- a/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java
+++ b/tests/robotests/src/com/android/settings/testutils/shadow/SettingsShadowResources.java
@@ -12,6 +12,7 @@
 import org.robolectric.annotation.Implementation;
 import org.robolectric.annotation.Implements;
 import org.robolectric.annotation.RealObject;
+import org.robolectric.annotation.Resetter;
 import org.robolectric.shadows.ShadowResources;
 import org.robolectric.util.ReflectionHelpers.ClassParameter;
 
@@ -40,6 +41,7 @@
         overrideResource(resId, value);
     }
 
+    @Resetter
     public static void reset() {
         sResourceOverrides.clear();
     }