Merge "Extract common logic for handling my photos" into ub-launcher3-master
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 4b9877a..0192398 100755
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -12,6 +12,7 @@
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   <uses-permission android:name="android.permission.INTERNET" />
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
+  <uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
   <uses-permission android:name="android.permission.SET_WALLPAPER" />
   <uses-permission android:name="android.permission.WAKE_LOCK" />
diff --git a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
index 3887309..25540f1 100755
--- a/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
+++ b/src/com/android/wallpaper/picker/TopLevelPickerActivity.java
@@ -721,26 +721,18 @@
 
         if (requestCode == WallpaperPickerDelegate.SHOW_CATEGORY_REQUEST_CODE
                 && resultCode == Activity.RESULT_OK) {
-            Uri imageUri = (data == null) ? null : data.getData();
-            if (imageUri != null) {
-                // User selected an image from the system picker, so launch the preview for that
-                // image.
-                ImageWallpaperInfo imageWallpaper = new ImageWallpaperInfo(imageUri);
-                if (mDelegate.getFormFactor() == FormFactorChecker.FORM_FACTOR_DESKTOP) {
+            if (mDelegate.getFormFactor() == FormFactorChecker.FORM_FACTOR_DESKTOP) {
+                Uri imageUri = (data == null) ? null : data.getData();
+                if (imageUri != null) {
+                    // User selected an image from the system picker, so launch the preview for that
+                    // image.
+                    ImageWallpaperInfo imageWallpaper = new ImageWallpaperInfo(imageUri);
                     setCustomPhotoWallpaper(imageWallpaper);
                     return;
                 }
-
-                imageWallpaper.showPreview(this, mDelegate.getPreviewIntentFactory(),
-                        WallpaperPickerDelegate.PREVIEW_WALLPAPER_REQUEST_CODE);
-            } else {
-                // User finished viewing a category without any data, which implies that the user previewed
-                // and selected a wallpaper in-app, so finish this activity.
-                finishActivityWithResultOk();
             }
-        } else if (requestCode == WallpaperPickerDelegate.PREVIEW_WALLPAPER_REQUEST_CODE
-                && resultCode == Activity.RESULT_OK) {
-            // User previewed and selected a wallpaper, so finish this activity.
+        }
+        if (mDelegate.handleActivityResult(requestCode, resultCode, data)) {
             finishActivityWithResultOk();
         }
     }
diff --git a/src/com/android/wallpaper/picker/WallpaperPickerDelegate.java b/src/com/android/wallpaper/picker/WallpaperPickerDelegate.java
index e50f47e..6254d20 100644
--- a/src/com/android/wallpaper/picker/WallpaperPickerDelegate.java
+++ b/src/com/android/wallpaper/picker/WallpaperPickerDelegate.java
@@ -16,15 +16,18 @@
 package com.android.wallpaper.picker;
 
 import android.Manifest.permission;
+import android.app.Activity;
 import android.app.WallpaperManager;
 import android.content.Intent;
 import android.content.pm.PackageManager;
+import android.net.Uri;
 import android.os.Build.VERSION;
 import android.os.Build.VERSION_CODES;
 import android.service.wallpaper.WallpaperService;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.core.os.BuildCompat;
 import androidx.fragment.app.FragmentActivity;
 
 import com.android.wallpaper.R;
@@ -32,6 +35,7 @@
 import com.android.wallpaper.model.Category;
 import com.android.wallpaper.model.CategoryProvider;
 import com.android.wallpaper.model.CategoryReceiver;
+import com.android.wallpaper.model.ImageWallpaperInfo;
 import com.android.wallpaper.model.InlinePreviewIntentFactory;
 import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.FormFactorChecker;
@@ -75,6 +79,8 @@
     private PackageStatusNotifier.Listener mLiveWallpaperStatusListener;
     private PackageStatusNotifier.Listener mThirdPartyStatusListener;
     private CategoryProvider mCategoryProvider;
+    private static final String READ_PERMISSION = BuildCompat.isAtLeastQ() ?
+            permission.READ_MEDIA_IMAGES : permission.READ_EXTERNAL_STORAGE;
 
     public WallpaperPickerDelegate(WallpapersUiContainer container, FragmentActivity activity,
             Injector injector) {
@@ -135,7 +141,7 @@
     public void requestExternalStoragePermission(PermissionChangedListener listener) {
         mPermissionChangedListeners.add(listener);
         mActivity.requestPermissions(
-                new String[]{permission.READ_EXTERNAL_STORAGE},
+                new String[]{READ_PERMISSION},
                 READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
     }
 
@@ -398,14 +404,13 @@
             @NonNull int[] grantResults) {
         if (requestCode == WallpaperPickerDelegate.READ_EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE
                 && permissions.length > 0
-                && permissions[0].equals(permission.READ_EXTERNAL_STORAGE)
+                && permissions[0].equals(READ_PERMISSION)
                 && grantResults.length > 0) {
             if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                 for (PermissionChangedListener listener : getPermissionChangedListeners()) {
                     listener.onPermissionsGranted();
                 }
-            } else if (!mActivity.shouldShowRequestPermissionRationale(
-                    permission.READ_EXTERNAL_STORAGE)) {
+            } else if (!mActivity.shouldShowRequestPermissionRationale(READ_PERMISSION)) {
                 for (PermissionChangedListener listener : getPermissionChangedListeners()) {
                     listener.onPermissionsDenied(true /* dontAskAgain */);
                 }
@@ -417,4 +422,33 @@
         }
        getPermissionChangedListeners().clear();
     }
+
+    /**
+     * To be called from an Activity's onActivityResult method.
+     * Checks the result for ones that are handled by this delegate
+     * @return true if the intent was handled and calling Activity needs to finish with result
+     * OK, false otherwise.
+     */
+    public boolean handleActivityResult(int requestCode, int resultCode, Intent data) {
+        if (requestCode == SHOW_CATEGORY_REQUEST_CODE  && resultCode == Activity.RESULT_OK) {
+            Uri imageUri = (data == null) ? null : data.getData();
+            if (imageUri != null) {
+                // User selected an image from the system picker, so launch the preview for that
+                // image.
+                ImageWallpaperInfo imageWallpaper = new ImageWallpaperInfo(imageUri);
+
+                imageWallpaper.showPreview(mActivity, getPreviewIntentFactory(),
+                        PREVIEW_WALLPAPER_REQUEST_CODE);
+            } else {
+                // User finished viewing a category without any data, which implies that the user
+                // previewed and selected a wallpaper in-app, so finish this activity.
+                return true;
+            }
+        } else if (requestCode == PREVIEW_WALLPAPER_REQUEST_CODE
+                && resultCode == Activity.RESULT_OK) {
+            // User previewed and selected a wallpaper, so finish this activity.
+            return true;
+        }
+        return false;
+    }
 }