Finish support for third-party wallpaper pickers

Clicking on the pickers didn't do anything before.

Change-Id: I103caafbe8d44d27d1797436ce479fb769c4c0f4
diff --git a/src/com/android/launcher3/Launcher.java b/src/com/android/launcher3/Launcher.java
index d91fee8..d5fafac 100644
--- a/src/com/android/launcher3/Launcher.java
+++ b/src/com/android/launcher3/Launcher.java
@@ -50,7 +50,6 @@
 import android.database.ContentObserver;
 import android.graphics.Bitmap;
 import android.graphics.Canvas;
-import android.graphics.Color;
 import android.graphics.Point;
 import android.graphics.PorterDuff;
 import android.graphics.Rect;
@@ -107,12 +106,8 @@
 import java.io.PrintWriter;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Set;
 
 /**
  * Default launcher application.
@@ -1896,7 +1891,7 @@
             Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
             intent.setComponent(appWidgetInfo.configure);
             intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-            startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
+            Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
         } else {
             // Otherwise just add it
             completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
@@ -2002,9 +1997,9 @@
             Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
             pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
             pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
-            startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
+            Utilities.startActivityForResultSafely(this, pickIntent, REQUEST_PICK_APPLICATION);
         } else {
-            startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
+            Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
         }
     }
 
@@ -2320,19 +2315,6 @@
         return success;
     }
 
-    void startActivityForResultSafely(Intent intent, int requestCode) {
-        try {
-            startActivityForResult(intent, requestCode);
-        } catch (ActivityNotFoundException e) {
-            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
-        } catch (SecurityException e) {
-            Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
-            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
-                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
-                    "or use the exported attribute for this activity.", e);
-        }
-    }
-
     private void handleFolderClick(FolderIcon folderIcon) {
         final FolderInfo info = folderIcon.getFolderInfo();
         Folder openFolder = mWorkspace.getFolderForTag(info);
diff --git a/src/com/android/launcher3/Utilities.java b/src/com/android/launcher3/Utilities.java
index 0529cfb..90db5ab 100644
--- a/src/com/android/launcher3/Utilities.java
+++ b/src/com/android/launcher3/Utilities.java
@@ -16,9 +16,10 @@
 
 package com.android.launcher3;
 
-import java.util.Random;
-
+import android.app.Activity;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
+import android.content.Intent;
 import android.content.res.Resources;
 import android.graphics.Bitmap;
 import android.graphics.BlurMaskFilter;
@@ -28,19 +29,17 @@
 import android.graphics.Matrix;
 import android.graphics.Paint;
 import android.graphics.PaintFlagsDrawFilter;
-import android.graphics.PorterDuff;
 import android.graphics.Rect;
 import android.graphics.drawable.BitmapDrawable;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.PaintDrawable;
 import android.util.DisplayMetrics;
+import android.util.Log;
 import android.view.View;
-import android.view.ViewGroup;
+import android.widget.Toast;
 
 import java.util.ArrayList;
 
-import com.android.launcher3.R;
-
 /**
  * Various utilities shared amongst the Launcher's classes.
  */
@@ -298,4 +297,18 @@
         sIconWidth = sIconHeight = widthPx;
         sIconTextureWidth = sIconTextureHeight = widthPx;
     }
+
+    public static void startActivityForResultSafely(
+            Activity activity, Intent intent, int requestCode) {
+        try {
+            activity.startActivityForResult(intent, requestCode);
+        } catch (ActivityNotFoundException e) {
+            Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
+        } catch (SecurityException e) {
+            Toast.makeText(activity, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
+            Log.e(TAG, "Launcher does not have the permission to launch " + intent +
+                    ". Make sure to create a MAIN intent-filter for the corresponding activity " +
+                    "or use the exported attribute for this activity.", e);
+        }
+    }
 }
diff --git a/src/com/android/launcher3/WallpaperPickerActivity.java b/src/com/android/launcher3/WallpaperPickerActivity.java
index 0327421..dfc3ef9 100644
--- a/src/com/android/launcher3/WallpaperPickerActivity.java
+++ b/src/com/android/launcher3/WallpaperPickerActivity.java
@@ -61,6 +61,7 @@
     private static final String TAG = "Launcher.WallpaperPickerActivity";
 
     private static final int IMAGE_PICK = 5;
+    private static final int PICK_WALLPAPER_THIRD_PARTY_ACTIVITY = 6;
     private static final float WALLPAPER_SCREENS_SPAN = 2f;
 
     private ArrayList<Integer> mThumbs;
@@ -91,7 +92,8 @@
             if (meta.mLaunchesGallery) {
                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
                 intent.setType("image/*");
-                startActivityForResult(intent, IMAGE_PICK);
+                Utilities.startActivityForResultSafely(
+                        WallpaperPickerActivity.this, intent, IMAGE_PICK);
             } else if (meta.mGalleryImageUri != null) {
                 mCropView.setTileSource(new BitmapRegionTileSource(WallpaperPickerActivity.this,
                         meta.mGalleryImageUri, 1024, 0), null);
@@ -150,6 +152,10 @@
             meta.mGalleryImageUri = uri;
             pickedImageThumbnail.setTag(meta);
             mThumbnailOnClickListener.onClick(pickedImageThumbnail);
+        } else if (requestCode == PICK_WALLPAPER_THIRD_PARTY_ACTIVITY) {
+            // No result code is returned; just return
+            setResult(RESULT_OK);
+            finish();
         }
     }
 
@@ -242,6 +248,16 @@
         frameLayout.setForeground(new ZeroPaddingDrawable(frameLayout.getForeground()));
     }
 
+    public boolean onMenuItemSelected(int featureId, MenuItem item) {
+        if (item.getIntent() == null) {
+            return super.onMenuItemSelected(featureId, item);
+        } else {
+            Utilities.startActivityForResultSafely(
+                    this, item.getIntent(), PICK_WALLPAPER_THIRD_PARTY_ACTIVITY);
+            return true;
+        }
+    }
+
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         final Intent pickWallpaperIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
@@ -266,21 +282,25 @@
 
         outerLoop:
         for (ResolveInfo info : apps) {
-            final ComponentName componentName =
+            final ComponentName itemComponentName =
                     new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
+            final String itemPackageName = itemComponentName.getPackageName();
             // Exclude anything from our own package, and the old Launcher
-            if (componentName.getPackageName().equals(getPackageName()) ||
-                    componentName.getPackageName().equals("com.android.launcher")) {
+            if (itemPackageName.equals(getPackageName()) ||
+                    itemPackageName.equals("com.android.launcher")) {
                 continue;
             }
             // Exclude any package that already responds to the image picker intent
             for (ResolveInfo imagePickerActivityInfo : imagePickerActivities) {
-                if (componentName.getPackageName().equals(
+                if (itemPackageName.equals(
                         imagePickerActivityInfo.activityInfo.packageName)) {
                     continue outerLoop;
                 }
             }
             MenuItem mi = sub.add(info.loadLabel(pm));
+            Intent launchIntent = new Intent(Intent.ACTION_SET_WALLPAPER);
+            launchIntent.setComponent(itemComponentName);
+            mi.setIntent(launchIntent);
             Drawable icon = info.loadIcon(pm);
             if (icon != null) {
                 mi.setIcon(icon);