ThemePicker: apply grid settings to launcher

Use the API to apply the selected grid settings
and add wallpaper to the background of the preview.

Bug: 120560197
Change-Id: I3338deca3183761facd5121839b0145823aaa0b2
diff --git a/res/layout/fragment_grid_picker.xml b/res/layout/fragment_grid_picker.xml
index 536f1f7..db28f60 100644
--- a/res/layout/fragment_grid_picker.xml
+++ b/res/layout/fragment_grid_picker.xml
@@ -46,6 +46,7 @@
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
             <Button
+                android:id="@+id/apply_button"
                 style="@style/ActionPrimaryButton"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
diff --git a/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java b/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
index d43cdc7..8e7450f 100644
--- a/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
+++ b/src/com/android/customization/model/grid/LauncherGridOptionsProvider.java
@@ -16,6 +16,7 @@
 package com.android.customization.model.grid;
 
 import android.content.ContentResolver;
+import android.content.ContentValues;
 import android.content.Context;
 import android.content.pm.ProviderInfo;
 import android.content.res.Resources;
@@ -40,6 +41,7 @@
 
     private static final String LIST_OPTIONS = "list_options";
     private static final String PREVIEW = "preview";
+    private static final String DEFAULT_GRID = "default_grid";
 
     private static final String COL_NAME = "name";
     private static final String COL_ROWS = "rows";
@@ -111,6 +113,13 @@
     }
 
     void applyGrid(String name) {
-        //TODO: implement
+        Uri updateDefaultUri = new Uri.Builder()
+                .scheme(ContentResolver.SCHEME_CONTENT)
+                .authority(mProviderInfo.authority)
+                .appendPath(DEFAULT_GRID)
+                .build();
+        ContentValues values = new ContentValues();
+        values.put("name", name);
+        mContext.getContentResolver().update(updateDefaultUri, values, null, null);
     }
 }
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index 6733cb5..20fa720 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -16,14 +16,20 @@
 package com.android.customization.picker.grid;
 
 import android.app.Activity;
+import android.content.res.Resources;
+import android.graphics.drawable.BitmapDrawable;
 import android.net.Uri;
 import android.os.Bundle;
+import android.util.DisplayMetrics;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.View.OnLayoutChangeListener;
 import android.view.ViewGroup;
+import android.widget.ImageView;
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.cardview.widget.CardView;
 import androidx.recyclerview.widget.RecyclerView;
 
 import com.android.customization.model.grid.GridOption;
@@ -35,6 +41,9 @@
 import com.android.wallpaper.R;
 import com.android.wallpaper.asset.Asset;
 import com.android.wallpaper.asset.ContentUriAsset;
+import com.android.wallpaper.model.WallpaperInfo;
+import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
+import com.android.wallpaper.module.InjectorProvider;
 import com.android.wallpaper.picker.ToolbarFragment;
 
 import com.bumptech.glide.request.RequestOptions;
@@ -44,6 +53,13 @@
  */
 public class GridFragment extends ToolbarFragment {
 
+    private boolean mIsWallpaperInfoReady;
+    private WallpaperInfo mHomeWallpaper;
+    private float mScreenAspectRatio;
+    private int mPageHeight;
+    private int mPageWidth;
+    private GridPreviewAdapter mAdapter;
+
     public static GridFragment newInstance(CharSequence title, GridOptionsManager manager) {
         GridFragment fragment = new GridFragment();
         fragment.setManager(manager);
@@ -66,8 +82,35 @@
         setUpToolbar(view);
         mPreviewPager = view.findViewById(R.id.grid_preview_pager);
         mOptionsContainer = view.findViewById(R.id.options_container);
+        final Resources res = getContext().getResources();
+        DisplayMetrics dm = res.getDisplayMetrics();
+        mScreenAspectRatio = (float) dm.heightPixels / dm.widthPixels;
         setUpOptions();
+        view.findViewById(R.id.apply_button).setOnClickListener(v -> {
+            mGridManager.apply(mSelectedOption);
+            getActivity().finish();
+        });
+        CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector()
+                .getCurrentWallpaperFactory(getActivity().getApplicationContext());
 
+        factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) -> {
+            mHomeWallpaper = homeWallpaper;
+            mIsWallpaperInfoReady = true;
+            if (mAdapter != null) {
+                mAdapter.onWallpaperInfoLoaded();
+            }
+        }, false);
+        view.addOnLayoutChangeListener(new OnLayoutChangeListener() {
+            @Override
+            public void onLayoutChange(View v, int left, int top, int right, int bottom,
+                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
+                mPageHeight = mPreviewPager.getHeight() - mPreviewPager.getPaddingTop() -
+                        res.getDimensionPixelSize(R.dimen.indicator_container_height);
+                mPageWidth = (int) (mPageHeight / mScreenAspectRatio);
+                mPreviewPager.forceCardWidth(mPageWidth);
+                view.removeOnLayoutChangeListener(this);
+            }
+        });
         return view;
     }
 
@@ -76,7 +119,8 @@
     }
 
     private void createAdapter() {
-        mPreviewPager.setAdapter(new GridPreviewAdapter(mSelectedOption));
+        mAdapter = new GridPreviewAdapter(mSelectedOption);
+        mPreviewPager.setAdapter(mAdapter);
     }
 
     private void setUpOptions() {
@@ -101,13 +145,15 @@
         });
     }
 
-    private static class GridPreviewPage extends PreviewPage {
+    private class GridPreviewPage extends PreviewPage {
         private final int mPageId;
         private final Asset mPreviewAsset;
         private final int mCols;
         private final int mRows;
         private final Activity mActivity;
 
+		private ImageView mPreview;
+
         private GridPreviewPage(Activity activity, int id, Uri previewUri, int rows, int cols) {
             super(null);
             mPageId = id;
@@ -118,9 +164,28 @@
             mActivity = activity;
         }
 
+        @Override
+        public void setCard(CardView card) {
+        	super.setCard(card);
+        	mPreview = card.findViewById(R.id.grid_preview_image);
+        }
+
         public void bindPreviewContent() {
-            mPreviewAsset.loadDrawable(mActivity, card.findViewById(R.id.grid_preview_image),
-                    card.getContext().getResources().getColor(R.color.primary_color, null));
+            Resources resources = card.getResources();
+            bindWallpaperIfAvailable();
+            mPreviewAsset.loadDrawable(mActivity, mPreview,
+                    resources.getColor(android.R.color.transparent, null));
+        }
+
+        void bindWallpaperIfAvailable() {
+            if (card != null && mIsWallpaperInfoReady && mHomeWallpaper != null) {
+                mHomeWallpaper.getThumbAsset(card.getContext()).decodeBitmap(mPageWidth,
+                        mPageHeight,
+                        bitmap -> {
+                            mPreview.setBackground(
+                                    new BitmapDrawable(card.getResources(), bitmap));
+                        });
+            }
         }
     }
     /**
@@ -138,5 +203,11 @@
                         gridOption.rows, gridOption.cols));
             }
         }
+
+        void onWallpaperInfoLoaded() {
+            for (GridPreviewPage page : mPages) {
+                page.bindWallpaperIfAvailable();
+            }
+        }
     }
 }
diff --git a/src/com/android/customization/widget/PreviewPager.java b/src/com/android/customization/widget/PreviewPager.java
index 0ea43fc..ff13284 100644
--- a/src/com/android/customization/widget/PreviewPager.java
+++ b/src/com/android/customization/widget/PreviewPager.java
@@ -82,6 +82,20 @@
         mViewPager.addOnPageChangeListener(mPageListener);
     }
 
+    public void forceCardWidth(int widthPixels) {
+        mViewPager.addOnLayoutChangeListener(new OnLayoutChangeListener() {
+            @Override
+            public void onLayoutChange(View v, int left, int top, int right, int bottom,
+                    int oldLeft, int oldTop, int oldRight, int oldBottom) {
+                int hPadding = (mViewPager.getWidth() - widthPixels) / 2;
+                mViewPager.setPadding(hPadding, mViewPager.getPaddingTop(),
+                        hPadding, mViewPager.getPaddingBottom());
+                mViewPager.removeOnLayoutChangeListener(this);
+            }
+        });
+        mViewPager.invalidate();
+    }
+
     /**
      * Call this method to set the {@link PagerAdapter} backing the {@link ViewPager} in this
      * widget.