Add support for LiveWallpaper options

Support optional options for a theme's LiveWallpaper in the
theme spec, and allow for extending classes to apply them
by making ThemeManager "injectable".

Bug: 133793299
Change-Id: Id9187d7a38f6714c0e1ec6e07b9aa8b041800c39
diff --git a/src/com/android/customization/model/theme/DefaultThemeProvider.java b/src/com/android/customization/model/theme/DefaultThemeProvider.java
index 11e7f5c..d7c250f 100644
--- a/src/com/android/customization/model/theme/DefaultThemeProvider.java
+++ b/src/com/android/customization/model/theme/DefaultThemeProvider.java
@@ -15,6 +15,8 @@
  */
 package com.android.customization.model.theme;
 
+import static android.content.res.Resources.ID_NULL;
+
 import static com.android.customization.model.ResourceConstants.ANDROID_PACKAGE;
 import static com.android.customization.model.ResourceConstants.ICONS_FOR_PREVIEW;
 import static com.android.customization.model.ResourceConstants.OVERLAY_CATEGORY_COLOR;
@@ -86,6 +88,7 @@
     private static final String WALLPAPER_ATTRIBUTION_PREFIX = "theme_wallpaper_attribution_";
     private static final String WALLPAPER_THUMB_PREFIX = "theme_wallpaper_thumbnail_";
     private static final String WALLPAPER_ACTION_PREFIX = "theme_wallpaper_action_";
+    private static final String WALLPAPER_OPTIONS_PREFIX = "theme_wallpaper_options_";
 
     private static final String DEFAULT_THEME_NAME= "default";
     private static final String THEME_TITLE_FIELD = "_theme_title";
@@ -187,7 +190,7 @@
             String wallpaperThumbnailResName = WALLPAPER_THUMB_PREFIX + themeName;
             int wallpaperThumbnailResId = mStubApkResources.getIdentifier(wallpaperThumbnailResName,
                     "drawable", mStubPackageName);
-            if (wallpaperResId != Resources.ID_NULL) {
+            if (wallpaperResId != ID_NULL) {
                 builder.setWallpaperInfo(mStubPackageName, wallpaperResName,
                         themeName, wallpaperResId,
                         mStubApkResources.getIdentifier(WALLPAPER_TITLE_PREFIX + themeName,
@@ -197,20 +200,26 @@
                                 mStubPackageName),
                         mStubApkResources.getIdentifier(WALLPAPER_ACTION_PREFIX + themeName,
                                 "string", mStubPackageName))
-                        .setWallpaperAsset(wallpaperThumbnailResId != Resources.ID_NULL ?
+                        .setWallpaperAsset(wallpaperThumbnailResId != ID_NULL ?
                                 getDrawableResourceAsset(WALLPAPER_THUMB_PREFIX, themeName)
                                 : getDrawableResourceAsset(WALLPAPER_PREFIX, themeName));
             } else {
                 // Try to see if it's a live wallpaper reference
                 wallpaperResId = mStubApkResources.getIdentifier(wallpaperResName,
                         "string", mStubPackageName);
-                if (wallpaperResId != Resources.ID_NULL) {
+                if (wallpaperResId != ID_NULL) {
                     String wpComponent = mStubApkResources.getString(wallpaperResId);
+
+                    int wallpaperOptionsResId = mStubApkResources.getIdentifier(
+                            WALLPAPER_OPTIONS_PREFIX + themeName, "string", mStubPackageName);
+                    String wallpaperOptions = wallpaperOptionsResId != ID_NULL
+                            ? mStubApkResources.getString(wallpaperOptionsResId) : null;
+
                     String[] componentParts = wpComponent.split("/");
                     Intent liveWpIntent =  new Intent(WallpaperService.SERVICE_INTERFACE);
                     liveWpIntent.setComponent(
-                            new ComponentName(componentParts[0],
-                                    componentParts[0] + componentParts[1]));
+                            new ComponentName(componentParts[0], componentParts[1]));
+
                     Context appContext = mContext.getApplicationContext();
                     PackageManager pm = appContext.getPackageManager();
                     ResolveInfo resolveInfo =
@@ -221,9 +230,10 @@
                             wallpaperInfo = new android.app.WallpaperInfo(appContext, resolveInfo);
                             LiveWallpaperInfo liveInfo = new LiveWallpaperInfo(wallpaperInfo);
                             builder.setLiveWallpaperInfo(liveInfo).setWallpaperAsset(
-                                    wallpaperThumbnailResId != Resources.ID_NULL ?
+                                    wallpaperThumbnailResId != ID_NULL ?
                                         getDrawableResourceAsset(WALLPAPER_THUMB_PREFIX, themeName)
-                                        : liveInfo.getThumbAsset(mContext));
+                                        : liveInfo.getThumbAsset(mContext))
+                                    .setWallpaperOptions(wallpaperOptions);
                         } catch (XmlPullParserException | IOException e) {
                             Log.w(TAG, "Skipping wallpaper " + resolveInfo.serviceInfo, e);
                         }
diff --git a/src/com/android/customization/model/theme/ThemeBundle.java b/src/com/android/customization/model/theme/ThemeBundle.java
index 12ec32c..7c543d4 100644
--- a/src/com/android/customization/model/theme/ThemeBundle.java
+++ b/src/com/android/customization/model/theme/ThemeBundle.java
@@ -75,17 +75,19 @@
     private final boolean mIsDefault;
     protected final Map<String, String> mPackagesByCategory;
     @Nullable private final WallpaperInfo mWallpaperInfo;
+    @Nullable private final String mWallpaperOptions;
     private WallpaperInfo mOverrideWallpaper;
     private Asset mOverrideWallpaperAsset;
     private CharSequence mContentDescription;
 
     protected ThemeBundle(String title, Map<String, String> overlayPackages,
             boolean isDefault, @Nullable WallpaperInfo wallpaperInfo,
-            PreviewInfo previewInfo) {
+            @Nullable String wallpaperOptions, PreviewInfo previewInfo) {
         mTitle = title;
         mIsDefault = isDefault;
         mPreviewInfo = previewInfo;
         mWallpaperInfo = wallpaperInfo;
+        mWallpaperOptions = wallpaperOptions;
         mPackagesByCategory = Collections.unmodifiableMap(overlayPackages);
     }
 
@@ -179,6 +181,11 @@
         return mWallpaperInfo;
     }
 
+    @Nullable
+    public String getWallpaperOptions() {
+        return mWallpaperOptions;
+    }
+
     boolean isDefault() {
         return mIsDefault;
     }
@@ -300,11 +307,12 @@
         @Dimension private int mCornerRadius;
         private Asset mWallpaperAsset;
         private WallpaperInfo mWallpaperInfo;
+        private String mWallpaperOptions;
         protected Map<String, String> mPackages = new HashMap<>();
         private List<Drawable> mAppIcons = new ArrayList<>();
 
         public ThemeBundle build(Context context) {
-            return new ThemeBundle(mTitle, mPackages, mIsDefault, mWallpaperInfo,
+            return new ThemeBundle(mTitle, mPackages, mIsDefault, mWallpaperInfo, mWallpaperOptions,
                     createPreviewInfo(context));
         }
 
@@ -409,6 +417,11 @@
             return this;
         }
 
+        public Builder setWallpaperOptions(String wallpaperOptions) {
+            mWallpaperOptions = wallpaperOptions;
+            return this;
+        }
+
         public Builder asDefault() {
             mIsDefault = true;
             return this;
diff --git a/src/com/android/customization/model/theme/ThemeManager.java b/src/com/android/customization/model/theme/ThemeManager.java
index f7bbd00..8846338 100644
--- a/src/com/android/customization/model/theme/ThemeManager.java
+++ b/src/com/android/customization/model/theme/ThemeManager.java
@@ -26,8 +26,8 @@
 
 import android.graphics.Point;
 import android.provider.Settings;
-
 import android.text.TextUtils;
+
 import androidx.annotation.Nullable;
 import androidx.fragment.app.FragmentActivity;
 
@@ -42,6 +42,7 @@
 import com.android.wallpaper.module.WallpaperSetter;
 import com.android.wallpaper.picker.SetWallpaperDialogFragment.Listener;
 import com.android.wallpaper.util.WallpaperCropUtils;
+
 import org.json.JSONObject;
 
 import java.util.HashSet;
@@ -67,7 +68,7 @@
     private final OverlayManagerCompat mOverlayManagerCompat;
 
     private final WallpaperSetter mWallpaperSetter;
-    private final FragmentActivity mActivity;
+    protected final FragmentActivity mActivity;
     private final ThemesUserEventLogger mEventLogger;
 
     private Map<String, String> mCurrentOverlays;
@@ -122,6 +123,7 @@
         return new SetWallpaperCallback() {
             @Override
             public void onSuccess() {
+                applyWallpaperOptions(theme);
                 applyOverlays(theme, callback);
             }
 
@@ -132,6 +134,10 @@
         };
     }
 
+    protected void applyWallpaperOptions(ThemeBundle theme) {
+        //Do nothing.
+    }
+
     private void applyWallpaper(ThemeBundle theme, int destination,
             SetWallpaperCallback callback) {
         Point defaultCropSurfaceSize = WallpaperCropUtils.getDefaultCropSurfaceSize(
diff --git a/src/com/android/customization/model/theme/custom/CustomTheme.java b/src/com/android/customization/model/theme/custom/CustomTheme.java
index 97a1876..8a7ca05 100644
--- a/src/com/android/customization/model/theme/custom/CustomTheme.java
+++ b/src/com/android/customization/model/theme/custom/CustomTheme.java
@@ -41,7 +41,7 @@
 
     public CustomTheme(@NonNull String id, String title, Map<String, String> overlayPackages,
             @Nullable PreviewInfo previewInfo) {
-        super(title, overlayPackages, false, null, previewInfo);
+        super(title, overlayPackages, false, null, null, previewInfo);
         mId = id;
     }
 
diff --git a/src/com/android/customization/module/CustomizationInjector.java b/src/com/android/customization/module/CustomizationInjector.java
index 90333f0..d25f925 100644
--- a/src/com/android/customization/module/CustomizationInjector.java
+++ b/src/com/android/customization/module/CustomizationInjector.java
@@ -17,9 +17,19 @@
 
 import android.content.Context;
 
+import androidx.fragment.app.FragmentActivity;
+
+import com.android.customization.model.theme.OverlayManagerCompat;
+import com.android.customization.model.theme.ThemeBundleProvider;
+import com.android.customization.model.theme.ThemeManager;
 import com.android.wallpaper.module.Injector;
+import com.android.wallpaper.module.WallpaperSetter;
 
 public interface CustomizationInjector extends Injector {
 
     CustomizationPreferences getCustomizationPreferences(Context context);
+
+    ThemeManager getThemeManager(ThemeBundleProvider provider, FragmentActivity activity,
+            WallpaperSetter wallpaperSetter, OverlayManagerCompat overlayManagerCompat,
+            ThemesUserEventLogger logger);
 }
diff --git a/src/com/android/customization/module/DefaultCustomizationInjector.java b/src/com/android/customization/module/DefaultCustomizationInjector.java
index 7af90a3..3d47b72 100644
--- a/src/com/android/customization/module/DefaultCustomizationInjector.java
+++ b/src/com/android/customization/module/DefaultCustomizationInjector.java
@@ -18,16 +18,19 @@
 import android.content.Context;
 
 import androidx.fragment.app.Fragment;
+import androidx.fragment.app.FragmentActivity;
 
+import com.android.customization.model.theme.OverlayManagerCompat;
+import com.android.customization.model.theme.ThemeBundleProvider;
+import com.android.customization.model.theme.ThemeManager;
 import com.android.wallpaper.model.CategoryProvider;
 import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.BaseWallpaperInjector;
 import com.android.wallpaper.module.DefaultCategoryProvider;
 import com.android.wallpaper.module.LoggingOptInStatusProvider;
-import com.android.wallpaper.module.NoOpUserEventLogger;
-import com.android.wallpaper.module.UserEventLogger;
 import com.android.wallpaper.module.WallpaperPreferences;
 import com.android.wallpaper.module.WallpaperRotationRefresher;
+import com.android.wallpaper.module.WallpaperSetter;
 import com.android.wallpaper.monitor.PerformanceMonitor;
 import com.android.wallpaper.picker.PreviewFragment;
 
@@ -109,4 +112,11 @@
         return null;
     }
 
+    @Override
+    public ThemeManager getThemeManager(ThemeBundleProvider provider, FragmentActivity activity,
+            WallpaperSetter wallpaperSetter, OverlayManagerCompat overlayManagerCompat,
+            ThemesUserEventLogger logger) {
+        return new ThemeManager(provider, activity, wallpaperSetter, overlayManagerCompat, logger);
+    }
+
 }
diff --git a/src/com/android/customization/picker/CustomizationPickerActivity.java b/src/com/android/customization/picker/CustomizationPickerActivity.java
index 4b3c2c2..5b1623c 100644
--- a/src/com/android/customization/picker/CustomizationPickerActivity.java
+++ b/src/com/android/customization/picker/CustomizationPickerActivity.java
@@ -19,7 +19,6 @@
 import android.content.Intent;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
-import android.os.Build;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.Gravity;
@@ -191,7 +190,7 @@
                 injector.getPreferences(this), mUserEventLogger, false);
         ThemesUserEventLogger eventLogger = (ThemesUserEventLogger) injector.getUserEventLogger(
                 this);
-        ThemeManager themeManager = new ThemeManager(
+        ThemeManager themeManager = injector.getThemeManager(
                 new DefaultThemeProvider(this, injector.getCustomizationPreferences(this)),
                 this,
                 mWallpaperSetter, new OverlayManagerCompat(this), eventLogger);
diff --git a/src/com/android/customization/picker/theme/CustomThemeActivity.java b/src/com/android/customization/picker/theme/CustomThemeActivity.java
index 2bc371d..fe537ba 100644
--- a/src/com/android/customization/picker/theme/CustomThemeActivity.java
+++ b/src/com/android/customization/picker/theme/CustomThemeActivity.java
@@ -15,7 +15,6 @@
  */
 package com.android.customization.picker.theme;
 
-import android.app.AlertDialog;
 import android.app.AlertDialog.Builder;
 import android.content.Intent;
 import android.os.Bundle;
@@ -104,7 +103,7 @@
             }
         }
 
-        mThemeManager = new ThemeManager(
+        mThemeManager = injector.getThemeManager(
                 new DefaultThemeProvider(this, injector.getCustomizationPreferences(this)),
                 this,
                 new WallpaperSetter(injector.getWallpaperPersister(this),