Reduce potential latency of popping up destination dialog

Use WallpaperManager's information to know the status of
system wallpaper instead of creating the WallpaperInfo by ourselves
(it will create bitmap to generate the hashcode which may take time).

Bug: 197921329
Test: manual
Change-Id: If74080ec466807baca4165c55251a46d42147092
Merged-In: If74080ec466807baca4165c55251a46d42147092
diff --git a/src/com/android/wallpaper/model/CurrentWallpaperInfoVN.java b/src/com/android/wallpaper/model/CurrentWallpaperInfoVN.java
index cc2677a..3522dc5 100755
--- a/src/com/android/wallpaper/model/CurrentWallpaperInfoVN.java
+++ b/src/com/android/wallpaper/model/CurrentWallpaperInfoVN.java
@@ -18,8 +18,6 @@
 import android.app.Activity;
 import android.content.Context;
 import android.os.Parcel;
-import android.os.ParcelFileDescriptor;
-import android.util.Log;
 
 import androidx.annotation.DrawableRes;
 import androidx.annotation.StringRes;
@@ -31,7 +29,6 @@
 import com.android.wallpaper.compat.WallpaperManagerCompat.WallpaperLocation;
 import com.android.wallpaper.module.InjectorProvider;
 
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -140,23 +137,10 @@
      * Constructs and returns an Asset instance representing the currently-set wallpaper asset.
      */
     private Asset createCurrentWallpaperAssetVN(Context context) {
-        WallpaperManagerCompat wallpaperManagerCompat = InjectorProvider.getInjector()
-                .getWallpaperManagerCompat(context);
-
-        ParcelFileDescriptor systemWallpaperFile = wallpaperManagerCompat.getWallpaperFile(
-                WallpaperManagerCompat.FLAG_SYSTEM);
-
         // Whether the wallpaper this object represents is the default built-in wallpaper.
         boolean isSystemBuiltIn = mWallpaperManagerFlag == WallpaperManagerCompat.FLAG_SYSTEM
-                && systemWallpaperFile == null;
-
-        if (systemWallpaperFile != null) {
-            try {
-                systemWallpaperFile.close();
-            } catch (IOException e) {
-                Log.e(TAG, "Unable to close system wallpaper ParcelFileDescriptor", e);
-            }
-        }
+                && !InjectorProvider.getInjector().getWallpaperStatusChecker()
+                .isHomeStaticWallpaperSet(context);
 
         return (isSystemBuiltIn)
                 ? new BuiltInWallpaperAsset(context)
diff --git a/src/com/android/wallpaper/module/BaseWallpaperInjector.java b/src/com/android/wallpaper/module/BaseWallpaperInjector.java
index 0dc5578..ea429a6 100755
--- a/src/com/android/wallpaper/module/BaseWallpaperInjector.java
+++ b/src/com/android/wallpaper/module/BaseWallpaperInjector.java
@@ -33,6 +33,7 @@
     private WallpaperRefresher mWallpaperRefresher;
     private Requester mRequester;
     private WallpaperManagerCompat mWallpaperManagerCompat;
+    private WallpaperStatusChecker mWallpaperStatusChecker;
     private CurrentWallpaperInfoFactory mCurrentWallpaperFactory;
     private NetworkStatusNotifier mNetworkStatusNotifier;
     private AlarmManagerWrapper mAlarmManagerWrapper;
@@ -101,6 +102,14 @@
     }
 
     @Override
+    public WallpaperStatusChecker getWallpaperStatusChecker() {
+        if (mWallpaperStatusChecker == null) {
+            mWallpaperStatusChecker = new DefaultWallpaperStatusChecker();
+        }
+        return mWallpaperStatusChecker;
+    }
+
+    @Override
     public synchronized CurrentWallpaperInfoFactory getCurrentWallpaperFactory(Context context) {
         if (mCurrentWallpaperFactory == null) {
             mCurrentWallpaperFactory =
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
index abe1728..9148b9f 100755
--- a/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
+++ b/src/com/android/wallpaper/module/DefaultWallpaperPersister.java
@@ -656,8 +656,9 @@
             }
 
 
-            boolean wasLockWallpaperSet = LockWallpaperStatusChecker.isLockWallpaperSet(
-                    mAppContext);
+            boolean wasLockWallpaperSet =
+                    InjectorProvider.getInjector().getWallpaperStatusChecker().isLockWallpaperSet(
+                            mAppContext);
 
             boolean allowBackup = mWallpaper.getBackupPermission() == WallpaperInfo.BACKUP_ALLOWED;
             final int wallpaperId;
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperRefresher.java b/src/com/android/wallpaper/module/DefaultWallpaperRefresher.java
index d85e04e..246b017 100755
--- a/src/com/android/wallpaper/module/DefaultWallpaperRefresher.java
+++ b/src/com/android/wallpaper/module/DefaultWallpaperRefresher.java
@@ -49,6 +49,7 @@
     private final Context mAppContext;
     private final WallpaperPreferences mWallpaperPreferences;
     private final WallpaperManager mWallpaperManager;
+    private final WallpaperStatusChecker mWallpaperStatusChecker;
 
     /**
      * @param context The application's context.
@@ -58,6 +59,7 @@
 
         Injector injector = InjectorProvider.getInjector();
         mWallpaperPreferences = injector.getPreferences(mAppContext);
+        mWallpaperStatusChecker = injector.getWallpaperStatusChecker();
 
         // Retrieve WallpaperManager using Context#getSystemService instead of
         // WallpaperManager#getInstance so it can be mocked out in test.
@@ -98,8 +100,8 @@
                 setFallbackHomeScreenWallpaperMetadata();
             }
 
-            boolean isLockScreenWallpaperCurrentlySet = LockWallpaperStatusChecker
-                    .isLockWallpaperSet(mAppContext);
+            boolean isLockScreenWallpaperCurrentlySet = mWallpaperStatusChecker.isLockWallpaperSet(
+                    mAppContext);
 
             if (!BuildCompat.isAtLeastN() || !isLockScreenWallpaperCurrentlySet) {
                 // Return only home metadata if pre-N device or lock screen wallpaper is not explicitly set.
@@ -226,7 +228,7 @@
 
         private long getCurrentLockWallpaperHashCode() {
             if (mCurrentLockWallpaperHashCode == 0
-                    && LockWallpaperStatusChecker.isLockWallpaperSet(mAppContext)) {
+                    && mWallpaperStatusChecker.isLockWallpaperSet(mAppContext)) {
                 Bitmap wallpaperBitmap = getLockWallpaperBitmap();
                 mCurrentLockWallpaperHashCode = BitmapUtils.generateHashCode(wallpaperBitmap);
             }
diff --git a/src/com/android/wallpaper/module/DefaultWallpaperStatusChecker.java b/src/com/android/wallpaper/module/DefaultWallpaperStatusChecker.java
new file mode 100644
index 0000000..372ff7f
--- /dev/null
+++ b/src/com/android/wallpaper/module/DefaultWallpaperStatusChecker.java
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2021 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 static android.app.WallpaperManager.FLAG_LOCK;
+
+import android.app.WallpaperManager;
+import android.content.Context;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+import com.android.wallpaper.compat.WallpaperManagerCompat;
+
+import java.io.IOException;
+
+/**
+ * Default implementation of {@link WallpaperStatusChecker}.
+ */
+public final class DefaultWallpaperStatusChecker implements WallpaperStatusChecker {
+
+    private static final String TAG = "DefaultWallpaperStatusChecker";
+
+    @Override
+    public boolean isHomeStaticWallpaperSet(Context context) {
+        ParcelFileDescriptor systemWallpaperFile =
+                InjectorProvider.getInjector().getWallpaperManagerCompat(context).getWallpaperFile(
+                        WallpaperManagerCompat.FLAG_SYSTEM);
+        boolean isStaticWallpaperSet = systemWallpaperFile != null;
+
+        if (systemWallpaperFile != null) {
+            try {
+                systemWallpaperFile.close();
+            } catch (IOException e) {
+                Log.e(TAG, "Unable to close system wallpaper ParcelFileDescriptor", e);
+            }
+        }
+
+        return isStaticWallpaperSet;
+    }
+
+    @Override
+    public boolean isLockWallpaperSet(Context context) {
+        return WallpaperManager.getInstance(context).getWallpaperId(FLAG_LOCK) > 0;
+    }
+}
diff --git a/src/com/android/wallpaper/module/Injector.java b/src/com/android/wallpaper/module/Injector.java
index 1c40e93..919d1ba 100755
--- a/src/com/android/wallpaper/module/Injector.java
+++ b/src/com/android/wallpaper/module/Injector.java
@@ -62,6 +62,8 @@
 
     WallpaperManagerCompat getWallpaperManagerCompat(Context context);
 
+    WallpaperStatusChecker getWallpaperStatusChecker();
+
     WallpaperPersister getWallpaperPersister(Context context);
 
     WallpaperPreferences getPreferences(Context context);
diff --git a/src/com/android/wallpaper/module/LockWallpaperStatusChecker.java b/src/com/android/wallpaper/module/LockWallpaperStatusChecker.java
deleted file mode 100755
index 2ae93b0..0000000
--- a/src/com/android/wallpaper/module/LockWallpaperStatusChecker.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (C) 2017 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.content.Context;
-import android.os.ParcelFileDescriptor;
-import android.util.Log;
-
-import com.android.wallpaper.compat.BuildCompat;
-import com.android.wallpaper.compat.WallpaperManagerCompat;
-
-import java.io.IOException;
-
-/**
- * Checks whether an explicit lock wallpaper is set to the device (i.e., independently shown under
- * the keyguard and separate from the wallpaper shown under the user's launcher).
- */
-public class LockWallpaperStatusChecker {
-
-    private static final String TAG = "LockWPStatusChecker";
-
-    /**
-     * Returns whether a lock screen wallpaper is independently set to the device.
-     */
-    public static boolean isLockWallpaperSet(Context context) {
-        // Lock screen wallpapers are not supported until Android N.
-        if (!BuildCompat.isAtLeastN()) {
-            return false;
-        }
-
-        WallpaperManagerCompat wallpaperManagerCompat = InjectorProvider.getInjector()
-                .getWallpaperManagerCompat(context);
-        ParcelFileDescriptor parcelFd =
-                wallpaperManagerCompat.getWallpaperFile(WallpaperManagerCompat.FLAG_LOCK);
-        boolean isSet = parcelFd != null;
-        if (isSet) {
-            try {
-                // Close the ParcelFileDescriptor if it's not null to avoid a resource leak.
-                parcelFd.close();
-            } catch (IOException e) {
-                Log.e(TAG, "IO exception when closing the lock screen wallpaper file descriptor.");
-            }
-        }
-        return isSet;
-    }
-}
diff --git a/src/com/android/wallpaper/module/WallpaperSetter.java b/src/com/android/wallpaper/module/WallpaperSetter.java
index 4cdc8bc..428ff86 100644
--- a/src/com/android/wallpaper/module/WallpaperSetter.java
+++ b/src/com/android/wallpaper/module/WallpaperSetter.java
@@ -1,5 +1,7 @@
 package com.android.wallpaper.module;
 
+import static android.app.WallpaperManager.FLAG_LOCK;
+
 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.app.WallpaperColors;
@@ -22,7 +24,6 @@
 
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
-import com.android.wallpaper.asset.BuiltInWallpaperAsset;
 import com.android.wallpaper.model.LiveWallpaperInfo;
 import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.UserEventLogger.WallpaperSetFailureReason;
@@ -205,7 +206,7 @@
                     activity.getWindow().getDecorView().getRootView().getWindowToken(),
                     0.5f /* xOffset */, 0.0f /* yOffset */);
             if (destination == WallpaperPersister.DEST_BOTH) {
-                wallpaperManager.clear(WallpaperManager.FLAG_LOCK);
+                wallpaperManager.clear(FLAG_LOCK);
             }
             mPreferences.storeLatestHomeWallpaper(wallpaper.getWallpaperId(), wallpaper,
                     colors != null ? colors :
@@ -284,8 +285,6 @@
      */
     public void requestDestination(Activity activity, FragmentManager fragmentManager,
             @StringRes int titleResId, Listener listener, boolean isLiveWallpaper) {
-        CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector()
-                .getCurrentWallpaperFactory(activity);
         saveAndLockScreenOrientationIfNeeded(activity);
         Listener listenerWrapper = new Listener() {
             @Override
@@ -305,28 +304,34 @@
                 }
             }
         };
-        factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) -> {
-            SetWallpaperDialogFragment setWallpaperDialog = new SetWallpaperDialogFragment();
-            setWallpaperDialog.setTitleResId(titleResId);
-            setWallpaperDialog.setListener(listenerWrapper);
-            boolean isBuiltIn = homeWallpaper.getAsset(activity) instanceof BuiltInWallpaperAsset;
-            if ((homeWallpaper instanceof LiveWallpaperInfo || isBuiltIn)
-                    && lockWallpaper == null) {
-                if (isLiveWallpaper) {
-                    // If lock wallpaper is live and we're setting a live wallpaper, we can only
-                    // set it to both, so bypass the dialog.
-                    listener.onSet(WallpaperPersister.DEST_BOTH);
-                    restoreScreenOrientationIfNeeded(activity);
-                    return;
-                }
-                // if the lock wallpaper is a live wallpaper, we cannot set a home-only static one
-                setWallpaperDialog.setHomeOptionAvailable(false);
-            }
+
+        WallpaperStatusChecker wallpaperStatusChecker =
+                InjectorProvider.getInjector().getWallpaperStatusChecker();
+        boolean isLiveWallpaperSet =
+                WallpaperManager.getInstance(activity).getWallpaperInfo() != null;
+        // Alternative of ag/15567276
+        boolean isBuiltIn = !isLiveWallpaperSet
+                && !wallpaperStatusChecker.isHomeStaticWallpaperSet(activity);
+
+        SetWallpaperDialogFragment setWallpaperDialog = new SetWallpaperDialogFragment();
+        setWallpaperDialog.setTitleResId(titleResId);
+        setWallpaperDialog.setListener(listenerWrapper);
+        if ((isLiveWallpaperSet || isBuiltIn)
+                && !wallpaperStatusChecker.isLockWallpaperSet(activity)) {
             if (isLiveWallpaper) {
-                setWallpaperDialog.setLockOptionAvailable(false);
+                // If lock wallpaper is live and we're setting a live wallpaper, we can only
+                // set it to both, so bypass the dialog.
+                listener.onSet(WallpaperPersister.DEST_BOTH);
+                restoreScreenOrientationIfNeeded(activity);
+                return;
             }
-            setWallpaperDialog.show(fragmentManager, TAG_SET_WALLPAPER_DIALOG_FRAGMENT);
-        }, true); // Force refresh as the wallpaper may have been set while this fragment was paused
+            // if the lock wallpaper is a live wallpaper, we cannot set a home-only static one
+            setWallpaperDialog.setHomeOptionAvailable(false);
+        }
+        if (isLiveWallpaper) {
+            setWallpaperDialog.setLockOptionAvailable(false);
+        }
+        setWallpaperDialog.show(fragmentManager, TAG_SET_WALLPAPER_DIALOG_FRAGMENT);
     }
 
     private void saveAndLockScreenOrientationIfNeeded(Activity activity) {
diff --git a/src/com/android/wallpaper/module/WallpaperStatusChecker.java b/src/com/android/wallpaper/module/WallpaperStatusChecker.java
new file mode 100644
index 0000000..04a2a2d
--- /dev/null
+++ b/src/com/android/wallpaper/module/WallpaperStatusChecker.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2021 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.content.Context;
+
+/**
+ * Checks the status of the home/lock screen wallpaper in the device.
+ */
+public interface WallpaperStatusChecker {
+
+    /**
+     * Returns whether a static home wallpaper is set to the device.
+     */
+    boolean isHomeStaticWallpaperSet(Context context);
+
+    /**
+     * Returns an explicit lock wallpaper is set to the device independently (i.e.,independently
+     * shown under the keyguard and separate from the wallpaper shown under the user's launcher).
+     */
+    boolean isLockWallpaperSet(Context context);
+}
diff --git a/tests/src/com/android/wallpaper/testing/TestInjector.java b/tests/src/com/android/wallpaper/testing/TestInjector.java
index fd9f68a..cffa8c5 100644
--- a/tests/src/com/android/wallpaper/testing/TestInjector.java
+++ b/tests/src/com/android/wallpaper/testing/TestInjector.java
@@ -44,6 +44,7 @@
 import com.android.wallpaper.module.WallpaperPreferences;
 import com.android.wallpaper.module.WallpaperRefresher;
 import com.android.wallpaper.module.WallpaperRotationRefresher;
+import com.android.wallpaper.module.WallpaperStatusChecker;
 import com.android.wallpaper.monitor.PerformanceMonitor;
 import com.android.wallpaper.network.Requester;
 import com.android.wallpaper.picker.ImagePreviewFragment;
@@ -136,6 +137,11 @@
     }
 
     @Override
+    public WallpaperStatusChecker getWallpaperStatusChecker() {
+        return null;
+    }
+
+    @Override
     public CurrentWallpaperInfoFactory getCurrentWallpaperFactory(Context context) {
         if (mCurrentWallpaperInfoFactory == null) {
             mCurrentWallpaperInfoFactory =