Opening the system default wallpaper picker on clicking the wallpaper tile

Bug: 28790378
Change-Id: If283b60a0b9563ab8e80d49b0ffa195fc3ffda8a
diff --git a/res/values/config.xml b/res/values/config.xml
index d689f1b..88aa7fd 100644
--- a/res/values/config.xml
+++ b/res/values/config.xml
@@ -75,6 +75,9 @@
     <!-- Name of an icon provider class. -->
     <string name="icon_provider_class" translatable="false"></string>
 
+    <!-- Package name of the default wallpaper picker. -->
+    <string name="wallpaper_picker_package" translatable="false"></string>
+
     <!-- View ID to use for QSB widget -->
     <item type="id" name="qsb_widget" />
 
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index 6d5b203..3ce07e3 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -115,6 +115,7 @@
 import com.android.launcher3.userevent.nano.LauncherLogProto;
 import com.android.launcher3.util.ComponentKey;
 import com.android.launcher3.logging.FileLog;
+import com.android.launcher3.util.PackageManagerHelper;
 import com.android.launcher3.util.TestingUtils;
 import com.android.launcher3.util.Thunk;
 import com.android.launcher3.util.ViewOnDrawExecutor;
@@ -2715,10 +2716,17 @@
             return;
         }
 
-        if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
+        String pickerPackage = getString(R.string.wallpaper_picker_package);
+        if (TextUtils.isEmpty(pickerPackage)) {
+            pickerPackage =  PackageManagerHelper.getWallpaperPickerPackage(getPackageManager());
+        }
+
         int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
         float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
-        // TODO: Start the system wallpaper picker
+        startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER)
+                .setPackage(pickerPackage)
+                .putExtra(Utilities.EXTRA_WALLPAPER_OFFSET, offset),
+                REQUEST_PICK_WALLPAPER);
     }
 
     /**
diff --git a/src/com/android/launcher3/util/PackageManagerHelper.java b/src/com/android/launcher3/util/PackageManagerHelper.java
index 08e8e86..3c4c79a 100644
--- a/src/com/android/launcher3/util/PackageManagerHelper.java
+++ b/src/com/android/launcher3/util/PackageManagerHelper.java
@@ -16,17 +16,22 @@
 
 package com.android.launcher3.util;
 
+import android.content.Intent;
 import android.content.pm.ApplicationInfo;
 import android.content.pm.PackageManager;
+import android.content.pm.ResolveInfo;
 
 import com.android.launcher3.Utilities;
 
+import java.util.ArrayList;
+
 /**
  * Utility methods using package manager
  */
 public class PackageManagerHelper {
 
     private static final int FLAG_SUSPENDED = 1<<30;
+    private static final String LIVE_WALLPAPER_PICKER_PKG = "com.android.wallpaper.livepicker";
 
     /**
      * Returns true if the app can possibly be on the SDCard. This is just a workaround and doesn't
@@ -68,4 +73,27 @@
             return false;
         }
     }
+
+    /**
+     * Returns the package for a wallpaper picker system app giving preference to a app which
+     * is not as image picker.
+     */
+    public static String getWallpaperPickerPackage(PackageManager pm) {
+        ArrayList<String> excludePackages = new ArrayList<>();
+        // Exclude packages which contain an image picker
+        for (ResolveInfo info : pm.queryIntentActivities(
+                new Intent(Intent.ACTION_GET_CONTENT).setType("image/*"), 0)) {
+            excludePackages.add(info.activityInfo.packageName);
+        }
+        excludePackages.add(LIVE_WALLPAPER_PICKER_PKG);
+
+        for (ResolveInfo info : pm.queryIntentActivities(
+                new Intent(Intent.ACTION_SET_WALLPAPER), 0)) {
+            if (!excludePackages.contains(info.activityInfo.packageName) &&
+                    (info.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
+                return info.activityInfo.packageName;
+            }
+        }
+        return excludePackages.get(0);
+    }
 }