Snap for 6333744 from 0997905b9516dd7c6d14a8a40dfb634bf159c0b7 to rvc-release

Change-Id: Ie7b57e133dfcdc96fb3268c9b475341d60a9925e
diff --git a/res/values/override.xml b/res/values/override.xml
index 1b82e00..a070dbe 100644
--- a/res/values/override.xml
+++ b/res/values/override.xml
@@ -21,11 +21,6 @@
     <!-- Authority of a provider in System UI that will provide preview info for available clockfaces. -->
     <string name="clocks_provider_authority" translatable="false">com.android.keyguard.clock</string>
 
-    <!--Name of metadata in the main launcher Activity which values contains the authority
-        corresponding to a ContentProvider in launcher to provide available grids and
-        allow for changing them -->
-    <string name="grid_control_metadata_name" translatable="false">com.android.launcher3.grid.control</string>
-
     <string name="launcher_overlayable_package" translatable="false">com.android.launcher3</string>
 
     <!-- List of packages whose icons are used to preview the icon shape for a theme. These are
diff --git a/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java b/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
index 8eb7383..7d1d5cd 100644
--- a/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
+++ b/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
@@ -18,13 +18,8 @@
 import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
-import android.content.Intent;
-import android.content.pm.PackageManager;
-import android.content.pm.ProviderInfo;
-import android.content.pm.ResolveInfo;
 import android.content.res.Resources;
 import android.database.Cursor;
-import android.net.Uri;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.util.Pair;
@@ -36,6 +31,7 @@
 import com.android.customization.model.ResourceConstants;
 import com.android.systemui.shared.system.SurfaceViewRequestUtils;
 import com.android.wallpaper.R;
+import com.android.wallpaper.util.PreviewUtils;
 
 import com.bumptech.glide.Glide;
 
@@ -57,33 +53,20 @@
     private static final String COL_PREVIEW_COUNT = "preview_count";
     private static final String COL_IS_DEFAULT = "is_default";
 
-    private static final String METHOD_GET_PREVIEW = "get_preview";
     private static final String METADATA_KEY_PREVIEW_VERSION = "preview_version";
 
     private final Context mContext;
-    private final String mGridProviderAuthority;
-    private final ProviderInfo mProviderInfo;
+    private final PreviewUtils mPreviewUtils;
     private List<GridOption> mOptions;
     private String mVersion;
 
     public LauncherGridOptionsProvider(Context context, String authorityMetadataKey) {
+        mPreviewUtils = new PreviewUtils(context, authorityMetadataKey);
         mContext = context;
-        Intent homeIntent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME);
-
-        ResolveInfo info = context.getPackageManager().resolveActivity(homeIntent,
-                PackageManager.MATCH_DEFAULT_ONLY | PackageManager.GET_META_DATA);
-        if (info != null && info.activityInfo != null && info.activityInfo.metaData != null) {
-            mGridProviderAuthority = info.activityInfo.metaData.getString(authorityMetadataKey);
-        } else {
-            mGridProviderAuthority = null;
-        }
-        // TODO: check permissions if needed
-        mProviderInfo = TextUtils.isEmpty(mGridProviderAuthority) ? null
-                : mContext.getPackageManager().resolveContentProvider(mGridProviderAuthority, 0);
     }
 
     boolean areGridsAvailable() {
-        return mProviderInfo != null;
+        return mPreviewUtils.supportsPreview();
     }
 
     boolean usesSurfaceView() {
@@ -104,15 +87,11 @@
         if (mOptions != null && !reload) {
             return Pair.create(mOptions, mVersion);
         }
-        Uri optionsUri = new Uri.Builder()
-                .scheme(ContentResolver.SCHEME_CONTENT)
-                .authority(mProviderInfo.authority)
-                .appendPath(LIST_OPTIONS)
-                .build();
         ContentResolver resolver = mContext.getContentResolver();
         String iconPath = mContext.getResources().getString(Resources.getSystem().getIdentifier(
                 ResourceConstants.CONFIG_ICON_MASK, "string", ResourceConstants.ANDROID_PACKAGE));
-        try (Cursor c = resolver.query(optionsUri, null, null, null, null)) {
+        try (Cursor c = resolver.query(mPreviewUtils.getUri(LIST_OPTIONS), null, null, null,
+                null)) {
             mVersion = c.getExtras().getString(METADATA_KEY_PREVIEW_VERSION);
             mOptions = new ArrayList<>();
             while(c.moveToNext()) {
@@ -121,15 +100,9 @@
                 int cols = c.getInt(c.getColumnIndex(COL_COLS));
                 int previewCount = c.getInt(c.getColumnIndex(COL_PREVIEW_COUNT));
                 boolean isSet = Boolean.valueOf(c.getString(c.getColumnIndex(COL_IS_DEFAULT)));
-                Uri preview = new Uri.Builder()
-                        .scheme(ContentResolver.SCHEME_CONTENT)
-                        .authority(mProviderInfo.authority)
-                        .appendPath(PREVIEW)
-                        .appendPath(name)
-                        .build();
                 String title = mContext.getString(R.string.grid_title_pattern, cols, rows);
-                mOptions.add(new GridOption(title, name, isSet, rows, cols, preview, previewCount,
-                        iconPath));
+                mOptions.add(new GridOption(title, name, isSet, rows, cols,
+                        mPreviewUtils.getUri(PREVIEW), previewCount, iconPath));
             }
             Glide.get(mContext).clearDiskCache();
         } catch (Exception e) {
@@ -146,24 +119,14 @@
      *                  {@link SurfaceViewRequestUtils#createSurfaceBundle(SurfaceView)}.
      */
     void renderPreview(String name, Bundle bundle) {
-        Uri preview = new Uri.Builder()
-                .scheme(ContentResolver.SCHEME_CONTENT)
-                .authority(mProviderInfo.authority)
-                .appendPath(PREVIEW)
-                .appendPath(name)
-                .build();
         bundle.putString("name", name);
-        mContext.getContentResolver().call(preview, METHOD_GET_PREVIEW, null, bundle);
+        mPreviewUtils.renderPreview(bundle);
     }
 
     int applyGrid(String name) {
-        Uri updateDefaultUri = new Uri.Builder()
-                .scheme(ContentResolver.SCHEME_CONTENT)
-                .authority(mProviderInfo.authority)
-                .appendPath(DEFAULT_GRID)
-                .build();
         ContentValues values = new ContentValues();
         values.put("name", name);
-        return mContext.getContentResolver().update(updateDefaultUri, values, null, null);
+        return mContext.getContentResolver().update(mPreviewUtils.getUri(DEFAULT_GRID), values,
+                null, null);
     }
 }