Allow extending LiveWallpaperInfo and Assets

Make LiveWallpaperInfo and LiveWallpaperThumbAsset
extensible and add a class to allow selecting a layer from a
LayerDrawable.

Bug: 136497932
Change-Id: Iab6b763101fe86a529ac1ab00870c0e1f45637d9
diff --git a/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java b/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
index 0214030..5df68c5 100755
--- a/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
+++ b/src/com/android/wallpaper/asset/LiveWallpaperThumbAsset.java
@@ -17,6 +17,7 @@
 
 import android.app.Activity;
 import android.content.Context;
+import android.content.pm.PackageManager;
 import android.graphics.Bitmap;
 import android.graphics.Rect;
 import android.graphics.drawable.BitmapDrawable;
@@ -35,9 +36,9 @@
 /**
  * Asset wrapping a drawable for a live wallpaper thumbnail.
  */
-public final class LiveWallpaperThumbAsset extends Asset {
-    private final Context mContext;
-    private final android.app.WallpaperInfo mInfo;
+public class LiveWallpaperThumbAsset extends Asset {
+    protected final Context mContext;
+    protected final android.app.WallpaperInfo mInfo;
 
     public LiveWallpaperThumbAsset(Context context, android.app.WallpaperInfo info) {
         mContext = context.getApplicationContext();
@@ -48,7 +49,7 @@
     public void decodeBitmap(int targetWidth, int targetHeight,
                              BitmapReceiver receiver) {
         // No scaling is needed, as the thumbnail is already a thumbnail.
-        LoadThumbnailTask task = new LoadThumbnailTask(mInfo, receiver);
+        LoadThumbnailTask task = new LoadThumbnailTask(mContext, mInfo, receiver);
         task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
     }
 
@@ -91,7 +92,7 @@
      * Returns the thumbnail drawable for the live wallpaper synchronously. Should not be called on
      * the main UI thread.
      */
-    Drawable getThumbnailDrawable() {
+    protected Drawable getThumbnailDrawable() {
         return mInfo.loadThumbnail(mContext.getPackageManager());
     }
 
@@ -147,18 +148,21 @@
      * AsyncTask subclass which loads the live wallpaper's thumbnail bitmap off the main UI thread.
      * Resolves with null if live wallpaper thumbnail is not a bitmap.
      */
-    private class LoadThumbnailTask extends AsyncTask<Void, Void, Bitmap> {
+    private static class LoadThumbnailTask extends AsyncTask<Void, Void, Bitmap> {
+        private final PackageManager mPackageManager;
         private android.app.WallpaperInfo mInfo;
         private BitmapReceiver mReceiver;
 
-        public LoadThumbnailTask(android.app.WallpaperInfo info, BitmapReceiver receiver) {
+        public LoadThumbnailTask(Context context, android.app.WallpaperInfo info,
+                BitmapReceiver receiver) {
             mInfo = info;
             mReceiver = receiver;
+            mPackageManager = context.getPackageManager();
         }
 
         @Override
         protected Bitmap doInBackground(Void... unused) {
-            Drawable thumb = mInfo.loadThumbnail(mContext.getPackageManager());
+            Drawable thumb = mInfo.loadThumbnail(mPackageManager);
 
             // Live wallpaper components may or may not specify a thumbnail drawable.
             if (thumb != null && thumb instanceof BitmapDrawable) {
diff --git a/src/com/android/wallpaper/model/LiveWallpaperInfo.java b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
index 5fc3cea..a408aed 100755
--- a/src/com/android/wallpaper/model/LiveWallpaperInfo.java
+++ b/src/com/android/wallpaper/model/LiveWallpaperInfo.java
@@ -28,10 +28,14 @@
 import android.service.wallpaper.WallpaperService;
 import android.util.Log;
 
+import androidx.annotation.Nullable;
+
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
 import com.android.wallpaper.asset.LiveWallpaperThumbAsset;
 import com.android.wallpaper.compat.BuildCompat;
+import com.android.wallpaper.module.InjectorProvider;
+import com.android.wallpaper.module.LiveWallpaperInfoFactory;
 import com.android.wallpaper.util.ActivityUtils;
 
 import org.xmlpull.v1.XmlPullParserException;
@@ -45,8 +49,6 @@
 import java.util.Iterator;
 import java.util.List;
 
-import androidx.annotation.Nullable;
-
 /**
  * Represents a live wallpaper from the system.
  */
@@ -64,8 +66,8 @@
                 }
             };
     private static final String TAG = "LiveWallpaperInfo";
-    private android.app.WallpaperInfo mInfo;
-    private LiveWallpaperThumbAsset mThumbAsset;
+    protected android.app.WallpaperInfo mInfo;
+    protected LiveWallpaperThumbAsset mThumbAsset;
     private boolean mVisibleTitle;
 
     /**
@@ -100,7 +102,8 @@
                                              @Nullable List<String> excludedPackageNames) {
         List<ResolveInfo> resolveInfos = getAllOnDevice(context);
         List<WallpaperInfo> wallpaperInfos = new ArrayList<>();
-
+        LiveWallpaperInfoFactory factory =
+                InjectorProvider.getInjector().getLiveWallpaperInfoFactory(context);
         for (int i = 0; i < resolveInfos.size(); i++) {
             ResolveInfo resolveInfo = resolveInfos.get(i);
             android.app.WallpaperInfo wallpaperInfo;
@@ -119,7 +122,7 @@
                 continue;
             }
 
-            wallpaperInfos.add(new LiveWallpaperInfo(wallpaperInfo));
+            wallpaperInfos.add(factory.getLiveWallpaperInfo(wallpaperInfo));
         }
 
         return wallpaperInfos;
@@ -139,6 +142,8 @@
             resolveInfos = getAllOnDevice(context);
         }
         List<WallpaperInfo> wallpaperInfos = new ArrayList<>();
+        LiveWallpaperInfoFactory factory =
+                InjectorProvider.getInjector().getLiveWallpaperInfoFactory(context);
 
         for (int i = 0; i < resolveInfos.size(); i++) {
             ResolveInfo resolveInfo = resolveInfos.get(i);
@@ -162,7 +167,7 @@
                 continue;
             }
 
-            wallpaperInfos.add(new LiveWallpaperInfo(wallpaperInfo, shouldShowTitle));
+            wallpaperInfos.add(factory.getLiveWallpaperInfo(wallpaperInfo, shouldShowTitle));
         }
 
         return wallpaperInfos;
diff --git a/src/com/android/wallpaper/module/BaseWallpaperInjector.java b/src/com/android/wallpaper/module/BaseWallpaperInjector.java
index 15ed84b..f8191fc 100755
--- a/src/com/android/wallpaper/module/BaseWallpaperInjector.java
+++ b/src/com/android/wallpaper/module/BaseWallpaperInjector.java
@@ -43,6 +43,7 @@
     private FormFactorChecker mFormFactorChecker;
     private PackageStatusNotifier mPackageStatusNotifier;
     private LiveWallpaperInfoFactory mLiveWallpaperInfoFactory;
+    private DrawableLayerResolver mDrawableLayerResolver;
 
     @Override
     public synchronized BitmapCropper getBitmapCropper() {
@@ -187,4 +188,12 @@
         }
         return mLiveWallpaperInfoFactory;
     }
+
+    @Override
+    public DrawableLayerResolver getDrawableLayerResolver() {
+        if (mDrawableLayerResolver == null) {
+            mDrawableLayerResolver = new DefaultDrawableLayerResolver();
+        }
+        return mDrawableLayerResolver;
+    }
 }
diff --git a/src/com/android/wallpaper/module/DefaultDrawableLayerResolver.java b/src/com/android/wallpaper/module/DefaultDrawableLayerResolver.java
new file mode 100644
index 0000000..4e52ff8
--- /dev/null
+++ b/src/com/android/wallpaper/module/DefaultDrawableLayerResolver.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.wallpaper.module;
+
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
+
+/**
+ * Default implementation of {@link DrawableLayerResolver}
+ */
+public class DefaultDrawableLayerResolver implements DrawableLayerResolver {
+
+    /**
+     * Picks and returns the top most layer of the given {@link LayerDrawable}
+     */
+    @Override
+    public Drawable resolveLayer(LayerDrawable layerDrawable) {
+        return layerDrawable.getDrawable(layerDrawable.getNumberOfLayers() - 1);
+    }
+}
diff --git a/src/com/android/wallpaper/module/DefaultLiveWallpaperInfoFactory.java b/src/com/android/wallpaper/module/DefaultLiveWallpaperInfoFactory.java
index 4a06ce5..774ad89 100644
--- a/src/com/android/wallpaper/module/DefaultLiveWallpaperInfoFactory.java
+++ b/src/com/android/wallpaper/module/DefaultLiveWallpaperInfoFactory.java
@@ -1,3 +1,18 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package com.android.wallpaper.module;
 
 import com.android.wallpaper.model.LiveWallpaperInfo;
@@ -13,4 +28,10 @@
     public WallpaperInfo getLiveWallpaperInfo(android.app.WallpaperInfo info) {
         return new LiveWallpaperInfo(info);
     }
+
+    @Override
+    public WallpaperInfo getLiveWallpaperInfo(android.app.WallpaperInfo info,
+            boolean shouldShowTitle) {
+        return new LiveWallpaperInfo(info, shouldShowTitle);
+    }
 }
diff --git a/src/com/android/wallpaper/module/DrawableLayerResolver.java b/src/com/android/wallpaper/module/DrawableLayerResolver.java
new file mode 100644
index 0000000..5c61433
--- /dev/null
+++ b/src/com/android/wallpaper/module/DrawableLayerResolver.java
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.wallpaper.module;
+
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.LayerDrawable;
+
+/**
+ * Interface for a class that can be used to arbitrarily select a Layer from a LayerDrawable
+ */
+public interface DrawableLayerResolver {
+
+    /**
+     * Picks a layer from a given {@link LayerDrawable}
+     */
+    Drawable resolveLayer(LayerDrawable layerDrawable);
+}
diff --git a/src/com/android/wallpaper/module/Injector.java b/src/com/android/wallpaper/module/Injector.java
index 6402496..3afaeb4 100755
--- a/src/com/android/wallpaper/module/Injector.java
+++ b/src/com/android/wallpaper/module/Injector.java
@@ -17,6 +17,8 @@
 
 import android.content.Context;
 
+import androidx.fragment.app.Fragment;
+
 import com.android.wallpaper.compat.WallpaperManagerCompat;
 import com.android.wallpaper.model.CategoryProvider;
 import com.android.wallpaper.model.WallpaperInfo;
@@ -25,8 +27,6 @@
 import com.android.wallpaper.picker.PreviewFragment.PreviewMode;
 import com.android.wallpaper.picker.individual.IndividualPickerFragment;
 
-import androidx.fragment.app.Fragment;
-
 /**
  * Interface for a provider of "injected dependencies." (NOTE: The term "injector" is somewhat of a
  * misnomer; this is more aptly a service registry as part of a service locator design pattern.)
@@ -83,4 +83,6 @@
     IndividualPickerFragment getIndividualPickerFragment(String collectionId);
 
     LiveWallpaperInfoFactory getLiveWallpaperInfoFactory(Context context);
+
+    DrawableLayerResolver getDrawableLayerResolver();
 }
diff --git a/src/com/android/wallpaper/module/LiveWallpaperInfoFactory.java b/src/com/android/wallpaper/module/LiveWallpaperInfoFactory.java
index 0c9bfee..838bbb6 100644
--- a/src/com/android/wallpaper/module/LiveWallpaperInfoFactory.java
+++ b/src/com/android/wallpaper/module/LiveWallpaperInfoFactory.java
@@ -1,6 +1,20 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package com.android.wallpaper.module;
 
-import com.android.wallpaper.model.LiveWallpaperInfo;
 import com.android.wallpaper.model.WallpaperInfo;
 
 /**
@@ -8,5 +22,7 @@
  */
 public interface LiveWallpaperInfoFactory {
 
-    public WallpaperInfo getLiveWallpaperInfo(android.app.WallpaperInfo info);
+    WallpaperInfo getLiveWallpaperInfo(android.app.WallpaperInfo info);
+
+    WallpaperInfo getLiveWallpaperInfo(android.app.WallpaperInfo info, boolean shouldShowTitle);
 }