GridPicker get rid of PreviewPager
Also extract the common part of GridFragment and GridFullPreviewFragment into WallpaperPreviewer and GridOptionPreviewer.
Video: https://drive.google.com/file/d/1FkkarDMTfoBzxpDxm04kbb872KyjNpEe/view?usp=sharing
Test: Manually
Bug: 156059583
Change-Id: I0005f4acc3c8b3588d2aca3150b9e17893715871
diff --git a/res/layout-land/fragment_grid_picker.xml b/res/layout-land/fragment_grid_picker.xml
index 6c2d8c5..dab9308 100644
--- a/res/layout-land/fragment_grid_picker.xml
+++ b/res/layout-land/fragment_grid_picker.xml
@@ -31,13 +31,16 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
- <com.android.wallpaper.widget.PreviewPager
- android:id="@+id/grid_preview_pager"
+ <FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
- android:background="@color/secondary_color"
- app:card_style="screen_aspect_ratio"/>
+ android:clipToPadding="false"
+ android:paddingTop="@dimen/preview_content_padding_top"
+ android:paddingBottom="@dimen/preview_content_padding_bottom"
+ android:background="@color/fullscreen_preview_background">
+ <include layout="@layout/grid_preview_card"/>
+ </FrameLayout>
<FrameLayout
android:id="@+id/options_section"
diff --git a/res/layout/fragment_grid_full_preview.xml b/res/layout/fragment_grid_full_preview.xml
index 0484fa7..d39be36 100644
--- a/res/layout/fragment_grid_full_preview.xml
+++ b/res/layout/fragment_grid_full_preview.xml
@@ -31,29 +31,6 @@
android:clipToPadding="false"
android:background="@color/fullscreen_preview_background">
- <androidx.cardview.widget.CardView
- style="@style/FullContentPreviewCard"
- android:id="@+id/grid_full_preview_card"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_gravity="center">
-
- <ImageView
- android:id="@+id/grid_full_preview_image"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/primary_color"/>
-
- <SurfaceView
- android:id="@+id/grid_full_preview_option_surface"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
-
- <SurfaceView
- android:id="@+id/grid_full_preview_wallpaper_surface"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
-
- </androidx.cardview.widget.CardView>
+ <include layout="@layout/grid_preview_card"/>
</FrameLayout>
</LinearLayout>
diff --git a/res/layout/fragment_grid_picker.xml b/res/layout/fragment_grid_picker.xml
index 5bf96e1..5238137 100644
--- a/res/layout/fragment_grid_picker.xml
+++ b/res/layout/fragment_grid_picker.xml
@@ -16,7 +16,6 @@
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@@ -27,26 +26,21 @@
android:layout_width="match_parent"
android:layout_height="match_parent">
- <androidx.constraintlayout.widget.ConstraintLayout
+ <LinearLayout
android:id="@+id/content_section"
android:layout_width="match_parent"
- android:layout_height="match_parent">
+ android:layout_height="match_parent"
+ android:orientation="vertical">
- <com.android.wallpaper.widget.PreviewPager
- android:id="@+id/grid_preview_pager"
+ <FrameLayout
android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:background="@color/preview_pager_background"
- app:card_style="screen_aspect_ratio"
- app:layout_constrainedHeight="true"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintBottom_toTopOf="@id/options_title"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHeight_max="@dimen/preview_pager_max_height"
- app:layout_constraintHeight_min="@dimen/preview_pager_min_height"
- app:layout_constraintVertical_bias="0.0"
- app:layout_constraintVertical_chainStyle="spread_inside"/>
+ android:layout_height="@dimen/preview_content_height"
+ android:paddingTop="@dimen/preview_content_padding_top"
+ android:paddingBottom="@dimen/preview_content_padding_bottom"
+ android:clipToPadding="false"
+ android:background="@color/fullscreen_preview_background">
+ <include layout="@layout/grid_preview_card"/>
+ </FrameLayout>
<TextView
android:id="@+id/options_title"
@@ -57,22 +51,14 @@
android:lineHeight="24dp"
android:singleLine="true"
android:text="@string/grid_options_title"
- android:textAppearance="@style/TitleTextAppearance"
- app:layout_constraintTop_toBottomOf="@id/grid_preview_pager"
- app:layout_constraintBottom_toTopOf="@id/options_container"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintEnd_toEndOf="parent"/>
+ android:textAppearance="@style/TitleTextAppearance"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/options_container"
android:layout_width="match_parent"
android:layout_height="@dimen/options_container_height"
- android:layout_gravity="center_horizontal"
- app:layout_constraintTop_toBottomOf="@+id/options_title"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintVertical_bias="1.0"/>
- </androidx.constraintlayout.widget.ConstraintLayout>
+ android:layout_gravity="center_horizontal"/>
+ </LinearLayout>
<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/loading_indicator"
diff --git a/res/layout/grid_preview_card.xml b/res/layout/grid_preview_card.xml
index 689d9a6..cdad6e9 100644
--- a/res/layout/grid_preview_card.xml
+++ b/res/layout/grid_preview_card.xml
@@ -18,22 +18,23 @@
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/FullContentPreviewCard"
android:layout_width="match_parent"
- android:layout_height="match_parent">
+ android:layout_height="match_parent"
+ android:layout_gravity="center">
+
+ <FrameLayout
+ android:id="@+id/grid_preview_container"
+ android:layout_width="match_parent"
+ android:layout_height="match_parent"
+ android:background="@color/primary_color" />
<ImageView
- android:id="@+id/grid_preview_image"
+ android:id="@+id/wallpaper_preview_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color" />
<SurfaceView
- android:id="@+id/grid_preview_surface"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:background="@color/primary_color" />
-
- <SurfaceView
- android:id="@+id/wallpaper_surface"
+ android:id="@+id/wallpaper_preview_surface"
android:layout_width="match_parent"
android:layout_height="match_parent" />
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 3c78967..b8fe190 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -70,6 +70,11 @@
<dimen name="preview_theme_content_bottom">@dimen/min_taptarget_height</dimen>
<dimen name="preview_theme_cover_content_bottom">@dimen/preview_theme_content_bottom</dimen>
+ <!-- For the customization previews on the picker. -->
+ <dimen name="preview_content_height">@dimen/preview_pager_height</dimen>
+ <dimen name="preview_content_padding_top">@dimen/preview_page_top_margin</dimen>
+ <dimen name="preview_content_padding_bottom">@dimen/indicator_container_height</dimen>
+
<dimen name="font_preview_body_width">200dp</dimen>
<dimen name="font_preview_divider_gap">24dp</dimen>
diff --git a/src/com/android/customization/picker/WallpaperPreviewer.java b/src/com/android/customization/picker/WallpaperPreviewer.java
new file mode 100644
index 0000000..0185925
--- /dev/null
+++ b/src/com/android/customization/picker/WallpaperPreviewer.java
@@ -0,0 +1,219 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.customization.picker;
+
+import static android.view.View.MeasureSpec.EXACTLY;
+import static android.view.View.MeasureSpec.makeMeasureSpec;
+
+import android.app.Activity;
+import android.content.Intent;
+import android.graphics.Rect;
+import android.graphics.RectF;
+import android.service.wallpaper.WallpaperService;
+import android.view.Surface;
+import android.view.SurfaceControlViewHost;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import androidx.annotation.MainThread;
+import androidx.cardview.widget.CardView;
+import androidx.core.content.ContextCompat;
+import androidx.lifecycle.Lifecycle;
+import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.OnLifecycleEvent;
+
+import com.android.wallpaper.R;
+import com.android.wallpaper.model.LiveWallpaperInfo;
+import com.android.wallpaper.model.WallpaperInfo;
+import com.android.wallpaper.util.ScreenSizeCalculator;
+import com.android.wallpaper.util.SizeCalculator;
+import com.android.wallpaper.util.WallpaperConnection;
+import com.android.wallpaper.widget.LiveTileOverlay;
+
+/** A class to load the wallpaper to the view. */
+public class WallpaperPreviewer implements LifecycleObserver {
+
+ private final Rect mPreviewLocalRect = new Rect();
+ private final Rect mPreviewGlobalRect = new Rect();
+ private final int[] mLivePreviewLocation = new int[2];
+
+ private final Activity mActivity;
+ private final ImageView mHomePreview;
+ private final SurfaceView mWallpaperSurface;
+
+ private WallpaperInfo mWallpaper;
+ private WallpaperConnection mWallpaperConnection;
+ // Home workspace surface is behind the app window, and so must the home image wallpaper like
+ // the live wallpaper. This view is rendered on mWallpaperSurface for home image wallpaper.
+ private ImageView mHomeImageWallpaper;
+
+ public WallpaperPreviewer(Lifecycle lifecycle, Activity activity, ImageView homePreview,
+ SurfaceView wallpaperSurface) {
+ lifecycle.addObserver(this);
+
+ mActivity = activity;
+ mHomePreview = homePreview;
+ mWallpaperSurface = wallpaperSurface;
+ mWallpaperSurface.setZOrderMediaOverlay(false);
+ mWallpaperSurface.getHolder().addCallback(mSurfaceCallback);
+ }
+
+ @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
+ @MainThread
+ public void onResume() {
+ if (mWallpaperConnection != null) {
+ mWallpaperConnection.setVisibility(true);
+ }
+ }
+
+ @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
+ @MainThread
+ public void onPause() {
+ if (mWallpaperConnection != null) {
+ mWallpaperConnection.setVisibility(false);
+ }
+ }
+
+ @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
+ @MainThread
+ public void onDestroy() {
+ LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
+ if (mWallpaperConnection != null) {
+ mWallpaperConnection.disconnect();
+ mWallpaperConnection = null;
+ }
+
+ mWallpaperSurface.getHolder().removeCallback(mSurfaceCallback);
+ Surface surface = mWallpaperSurface.getHolder().getSurface();
+ if (surface != null) {
+ surface.release();
+ }
+ }
+
+ /** Updates the preview card view radius to match the device radius. */
+ public void updatePreviewCardRadius() {
+ final float screenAspectRatio =
+ ScreenSizeCalculator.getInstance().getScreenAspectRatio(mActivity);
+ CardView cardView = (CardView) mHomePreview.getParent();
+ final int cardWidth = (int) (cardView.getMeasuredHeight() / screenAspectRatio);
+ ViewGroup.LayoutParams layoutParams = cardView.getLayoutParams();
+ layoutParams.width = cardWidth;
+ cardView.setLayoutParams(layoutParams);
+ cardView.setRadius(SizeCalculator.getPreviewCornerRadius(mActivity, cardWidth));
+ }
+
+ /** Loads the wallpaper. */
+ public void setWallpaper(WallpaperInfo wallpaperInfo) {
+ mWallpaper = wallpaperInfo;
+ setUpWallpaperPreview();
+ }
+
+ private void setUpWallpaperPreview() {
+ if (mWallpaper != null && mHomeImageWallpaper != null) {
+ boolean renderInImageWallpaperSurface = !(mWallpaper instanceof LiveWallpaperInfo);
+ mWallpaper.getThumbAsset(mActivity.getApplicationContext())
+ .loadPreviewImage(mActivity,
+ renderInImageWallpaperSurface ? mHomeImageWallpaper : mHomePreview,
+ mActivity.getResources().getColor(R.color.secondary_color));
+ LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
+ if (mWallpaper instanceof LiveWallpaperInfo) {
+ mWallpaper.getThumbAsset(mActivity.getApplicationContext())
+ .loadPreviewImage(
+ mActivity,
+ mHomeImageWallpaper,
+ mActivity.getColor(R.color.secondary_color));
+ setUpLiveWallpaperPreview(mWallpaper);
+ } else {
+ if (mWallpaperConnection != null) {
+ mWallpaperConnection.disconnect();
+ mWallpaperConnection = null;
+ }
+ }
+ }
+ }
+
+ private void setUpLiveWallpaperPreview(WallpaperInfo homeWallpaper) {
+ if (mActivity == null || mActivity.isFinishing()) {
+ return;
+ }
+
+ if (mWallpaperConnection != null) {
+ mWallpaperConnection.disconnect();
+ }
+
+ mHomePreview.getLocationOnScreen(mLivePreviewLocation);
+ mPreviewGlobalRect.set(0, 0, mHomePreview.getMeasuredWidth(),
+ mHomePreview.getMeasuredHeight());
+ mPreviewLocalRect.set(mPreviewGlobalRect);
+ mPreviewGlobalRect.offset(mLivePreviewLocation[0], mLivePreviewLocation[1]);
+
+ mWallpaperConnection = new WallpaperConnection(
+ getWallpaperIntent(homeWallpaper.getWallpaperComponent()), mActivity,
+ new WallpaperConnection.WallpaperConnectionListener() {
+ @Override
+ public void onEngineShown() {}
+ }, mPreviewGlobalRect);
+
+ LiveTileOverlay.INSTANCE.update(new RectF(mPreviewLocalRect),
+ ((CardView) mHomePreview.getParent()).getRadius());
+
+ mWallpaperConnection.setVisibility(true);
+ mHomePreview.post(() -> {
+ if (!mWallpaperConnection.connect()) {
+ mWallpaperConnection = null;
+ LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
+ }
+ });
+ }
+
+ private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
+
+ private Surface mLastSurface;
+
+ @Override
+ public void surfaceCreated(SurfaceHolder holder) {
+ if (mLastSurface != holder.getSurface()) {
+ mLastSurface = holder.getSurface();
+ mHomeImageWallpaper = new ImageView(mActivity);
+ mHomeImageWallpaper.setBackgroundColor(
+ ContextCompat.getColor(mActivity, R.color.primary_color));
+ mHomeImageWallpaper.measure(makeMeasureSpec(mHomePreview.getWidth(), EXACTLY),
+ makeMeasureSpec(mHomePreview.getHeight(), EXACTLY));
+ mHomeImageWallpaper.layout(0, 0, mHomePreview.getWidth(), mHomePreview.getHeight());
+
+ SurfaceControlViewHost host = new SurfaceControlViewHost(mActivity,
+ mActivity.getDisplay(), mWallpaperSurface.getHostToken());
+ host.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
+ mHomeImageWallpaper.getHeight());
+ mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
+ }
+ setUpWallpaperPreview();
+ }
+
+ @Override
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
+
+ @Override
+ public void surfaceDestroyed(SurfaceHolder holder) {}
+ };
+
+ private static Intent getWallpaperIntent(android.app.WallpaperInfo info) {
+ return new Intent(WallpaperService.SERVICE_INTERFACE)
+ .setClassName(info.getPackageName(), info.getServiceName());
+ }
+}
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index c9b655d..a8f6eda 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -16,8 +16,6 @@
package com.android.customization.picker.grid;
import static android.app.Activity.RESULT_OK;
-import static android.view.View.MeasureSpec.EXACTLY;
-import static android.view.View.MeasureSpec.makeMeasureSpec;
import static com.android.customization.picker.ViewOnlyFullPreviewActivity.SECTION_GRID;
import static com.android.customization.picker.grid.GridFullPreviewFragment.EXTRA_GRID_OPTION;
@@ -25,21 +23,11 @@
import static com.android.customization.picker.grid.GridFullPreviewFragment.EXTRA_WALLPAPER_INFO;
import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
-import android.app.Activity;
import android.content.Context;
import android.content.Intent;
-import android.graphics.Rect;
-import android.graphics.RectF;
-import android.net.Uri;
import android.os.Bundle;
-import android.os.Message;
-import android.os.RemoteException;
-import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.LayoutInflater;
-import android.view.Surface;
-import android.view.SurfaceControlViewHost;
-import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
@@ -48,8 +36,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import androidx.cardview.widget.CardView;
-import androidx.core.content.ContextCompat;
import androidx.core.widget.ContentLoadingProgressBar;
import androidx.recyclerview.widget.RecyclerView;
@@ -58,26 +44,17 @@
import com.android.customization.model.grid.GridOption;
import com.android.customization.model.grid.GridOptionsManager;
import com.android.customization.module.ThemesUserEventLogger;
-import com.android.customization.picker.BasePreviewAdapter;
-import com.android.customization.picker.BasePreviewAdapter.PreviewPage;
import com.android.customization.picker.ViewOnlyFullPreviewActivity;
+import com.android.customization.picker.WallpaperPreviewer;
import com.android.customization.widget.OptionSelectorController;
import com.android.wallpaper.R;
-import com.android.wallpaper.asset.Asset;
-import com.android.wallpaper.asset.ContentUriAsset;
-import com.android.wallpaper.model.LiveWallpaperInfo;
import com.android.wallpaper.model.WallpaperInfo;
import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.picker.AppbarFragment;
-import com.android.wallpaper.util.SurfaceViewUtils;
-import com.android.wallpaper.util.WallpaperConnection;
import com.android.wallpaper.widget.BottomActionBar;
-import com.android.wallpaper.widget.LiveTileOverlay;
-import com.android.wallpaper.widget.PreviewPager;
import com.bumptech.glide.Glide;
-import com.bumptech.glide.request.RequestOptions;
import java.util.List;
@@ -86,16 +63,10 @@
*/
public class GridFragment extends AppbarFragment {
- static final int PREVIEW_FADE_DURATION_MS = 100;
-
private static final int FULL_PREVIEW_REQUEST_CODE = 1000;
private static final String TAG = "GridFragment";
- private final Rect mPreviewLocalRect = new Rect();
- private final Rect mPreviewGlobalRect = new Rect();
- private final int[] mLivePreviewLocation = new int[2];
-
/**
* Interface to be implemented by an Activity hosting a {@link GridFragment}
*/
@@ -110,19 +81,20 @@
}
private WallpaperInfo mHomeWallpaper;
- private GridPreviewAdapter mAdapter;
private RecyclerView mOptionsContainer;
private OptionSelectorController<GridOption> mOptionsController;
private GridOptionsManager mGridManager;
private GridOption mSelectedOption;
- private PreviewPager mPreviewPager;
private ContentLoadingProgressBar mLoading;
+ private ViewGroup mGridPreviewContainer;
private View mContent;
private View mError;
private BottomActionBar mBottomActionBar;
private ThemesUserEventLogger mEventLogger;
private boolean mReloadOptionsAfterApplying;
+ private GridOptionPreviewer mGridOptionPreviewer;
+
private final Callback mApplyGridCallback = new Callback() {
@Override
public void onSuccess() {
@@ -172,8 +144,8 @@
View view = inflater.inflate(
R.layout.fragment_grid_picker, container, /* attachToRoot */ false);
setUpToolbar(view);
+ mGridPreviewContainer = view.findViewById(R.id.grid_preview_container);
mContent = view.findViewById(R.id.content_section);
- mPreviewPager = view.findViewById(R.id.grid_preview_pager);
mOptionsContainer = view.findViewById(R.id.options_container);
mLoading = view.findViewById(R.id.loading_indicator);
mError = view.findViewById(R.id.error_section);
@@ -182,35 +154,36 @@
Glide.get(getContext()).clearMemory();
setUpOptions();
+ ImageView wallpaperPreviewImage = view.findViewById(R.id.wallpaper_preview_image);
+ wallpaperPreviewImage.setOnClickListener(v -> showFullPreview());
+ SurfaceView wallpaperSurface = view.findViewById(R.id.wallpaper_preview_surface);
+ WallpaperPreviewer wallpaperPreviewer = new WallpaperPreviewer(
+ getLifecycle(), getActivity(), wallpaperPreviewImage, wallpaperSurface);
+
+ // Loads current Wallpaper.
CurrentWallpaperInfoFactory factory = InjectorProvider.getInjector()
.getCurrentWallpaperFactory(getContext().getApplicationContext());
+ factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) -> {
+ mHomeWallpaper = homeWallpaper;
+ wallpaperPreviewer.setWallpaper(mHomeWallpaper);
+ }, false);
- factory.createCurrentWallpaperInfos((homeWallpaper, lockWallpaper, presentationMode) ->
- mHomeWallpaper = homeWallpaper, false);
+ view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
+ @Override
+ public void onLayoutChange(View v, int left, int top, int right, int bottom,
+ int oldLeft, int oldTop, int oldRight, int oldBottom) {
+ wallpaperPreviewer.updatePreviewCardRadius();
+ view.removeOnLayoutChangeListener(this);
+ }
+ });
return view;
}
@Override
- public void onResume() {
- super.onResume();
- if (mAdapter != null) {
- mAdapter.setWallpaperConnectionVisibility(true);
- }
- }
-
- @Override
- public void onPause() {
- super.onPause();
- if (mAdapter != null) {
- mAdapter.setWallpaperConnectionVisibility(false);
- }
- }
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- if (mAdapter != null) {
- mAdapter.disconnectWallpaperConnection();
+ public void onDestroy() {
+ super.onDestroy();
+ if (mGridOptionPreviewer != null) {
+ mGridOptionPreviewer.release();
}
}
@@ -235,9 +208,13 @@
mGridManager.apply(gridOption, mApplyGridCallback);
}
- private void createAdapter() {
- mAdapter = new GridPreviewAdapter(mSelectedOption);
- mPreviewPager.setAdapter(mAdapter);
+ private void updatePreview() {
+ if (mGridOptionPreviewer != null) {
+ mGridOptionPreviewer.release();
+ }
+ mGridOptionPreviewer = new GridOptionPreviewer(
+ getContext(), mGridManager, mGridPreviewContainer);
+ mGridOptionPreviewer.setGridOption(mSelectedOption, mGridManager.usesSurfaceView());
}
private void setUpOptions() {
@@ -257,11 +234,11 @@
}
mBottomActionBar.show();
mEventLogger.logGridSelected(mSelectedOption);
- createAdapter();
+ updatePreview();
});
mOptionsController.initOptions(mGridManager);
mSelectedOption = getSelectedOption(options);
- createAdapter();
+ updatePreview();
}
@Override
@@ -301,253 +278,4 @@
Intent intent = ViewOnlyFullPreviewActivity.newIntent(getContext(), SECTION_GRID, bundle);
startActivityForResult(intent, FULL_PREVIEW_REQUEST_CODE);
}
-
- private Intent getWallpaperIntent(android.app.WallpaperInfo info) {
- return new Intent(WallpaperService.SERVICE_INTERFACE)
- .setClassName(info.getPackageName(), info.getServiceName());
- }
-
- // TODO(b/156059583): Remove the usage of PreviewPage, and add util class to load live wallpaper
- // for GridFragment and GridFullPreviewFragment.
- 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 final String mName;
-
- private ImageView mHomePreview;
- private SurfaceView mGridPreviewSurface;
- private SurfaceView mWallpaperSurface;
-
- private WallpaperConnection mWallpaperConnection;
-
- // Home workspace surface is behind the app window, and so must the home image wallpaper
- // like the live wallpaper. This view is rendered on mWallpaperSurface for home image
- // wallpaper.
- private ImageView mHomeImageWallpaper;
-
- private final SurfaceHolder.Callback mGridSurfaceCallback = new SurfaceHolder.Callback() {
-
- private Surface mLastSurface;
- private Message mCallback;
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if (mLastSurface != holder.getSurface()) {
- mLastSurface = holder.getSurface();
- Bundle result = mGridManager.renderPreview(
- SurfaceViewUtils.createSurfaceViewRequest(mGridPreviewSurface), mName);
- if (result != null) {
- mGridPreviewSurface.setChildSurfacePackage(
- SurfaceViewUtils.getSurfacePackage(result));
- mCallback = SurfaceViewUtils.getCallback(result);
- }
- }
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width,
- int height) {}
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- if (mCallback != null) {
- try {
- mCallback.replyTo.send(mCallback);
- } catch (RemoteException e) {
- e.printStackTrace();
- } finally {
- mCallback = null;
- }
- }
- }
- };
-
- private final SurfaceHolder.Callback mWallpaperSurfaceCallback =
- new SurfaceHolder.Callback() {
- private Surface mLastSurface;
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if (mLastSurface != holder.getSurface()) {
- mLastSurface = holder.getSurface();
- mHomeImageWallpaper = new ImageView(getContext());
- mHomeImageWallpaper.setBackgroundColor(
- ContextCompat.getColor(getContext(), R.color.primary_color));
- mHomeImageWallpaper.measure(makeMeasureSpec(mHomePreview.getWidth(), EXACTLY),
- makeMeasureSpec(mHomePreview.getHeight(), EXACTLY));
- mHomeImageWallpaper.layout(
- 0, 0, mHomePreview.getWidth(), mHomePreview.getHeight());
-
- SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(),
- getContext().getDisplay(), mWallpaperSurface.getHostToken());
- host.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
- mHomeImageWallpaper.getHeight());
- mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
- }
- setUpWallpaperPreview();
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {}
- };
-
- private GridPreviewPage(Activity activity, int id, Uri previewUri, String name, int rows,
- int cols) {
- super(null, activity);
- mPageId = id;
- mPreviewAsset = new ContentUriAsset(activity, previewUri,
- RequestOptions.fitCenterTransform());
- mName = name;
- mRows = rows;
- mCols = cols;
- mActivity = activity;
- }
-
- @Override
- public void setCard(CardView card) {
- super.setCard(card);
- mHomePreview = card.findViewById(R.id.grid_preview_image);
- mGridPreviewSurface = card.findViewById(R.id.grid_preview_surface);
- mWallpaperSurface = card.findViewById(R.id.wallpaper_surface);
- mGridPreviewSurface.setVisibility(View.GONE);
- // PreviewSurface is the top of its window(card view), due to #setZOrderOnTop(true).
- mGridPreviewSurface.setOnClickListener(view -> showFullPreview());
- }
-
- @Override
- public void bindPreviewContent() {
- updateWallpaperSurface();
- updateWorkspaceSurface();
- }
-
- private void updateWallpaperSurface() {
- mWallpaperSurface.setZOrderMediaOverlay(false);
- mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
- }
-
- private void updateWorkspaceSurface() {
- final boolean usesSurfaceViewForPreview = mGridManager.usesSurfaceView();
- if (usesSurfaceViewForPreview) {
- mGridPreviewSurface.setZOrderOnTop(true);
- mGridPreviewSurface.getHolder().addCallback(mGridSurfaceCallback);
- mGridPreviewSurface.setVisibility(View.VISIBLE);
- } else {
- mPreviewAsset.loadDrawableWithTransition(mActivity,
- mHomePreview /* imageView */,
- PREVIEW_FADE_DURATION_MS /* duration */,
- null /* drawableLoadedListener */,
- card.getResources().getColor(android.R.color.transparent,
- null) /* placeHolderColorJ */);
- }
- }
-
- private void setUpWallpaperPreview() {
- if (mHomeWallpaper != null && mHomeImageWallpaper != null) {
- boolean renderInImageWallpaperSurface =
- !(mHomeWallpaper instanceof LiveWallpaperInfo);
- mHomeWallpaper.getThumbAsset(getContext())
- .loadPreviewImage(getActivity(),
- renderInImageWallpaperSurface ? mHomeImageWallpaper : mHomePreview,
- getResources().getColor(R.color.secondary_color));
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- if (mHomeWallpaper instanceof LiveWallpaperInfo) {
- mHomeWallpaper.getThumbAsset(getContext().getApplicationContext())
- .loadPreviewImage(
- getActivity(),
- mHomeImageWallpaper,
- getContext().getColor(R.color.secondary_color));
- setUpLiveWallpaperPreview(mHomeWallpaper);
- } else {
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- mWallpaperConnection = null;
- }
- }
- }
- }
-
- private void setUpLiveWallpaperPreview(WallpaperInfo homeWallpaper) {
- Activity activity = getActivity();
- if (activity == null) {
- return;
- }
-
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- }
-
- mHomePreview.getLocationOnScreen(mLivePreviewLocation);
- mPreviewGlobalRect.set(0, 0, mHomePreview.getMeasuredWidth(),
- mHomePreview.getMeasuredHeight());
- mPreviewLocalRect.set(mPreviewGlobalRect);
- mPreviewGlobalRect.offset(mLivePreviewLocation[0], mLivePreviewLocation[1]);
-
- mWallpaperConnection = new WallpaperConnection(
- getWallpaperIntent(homeWallpaper.getWallpaperComponent()), activity,
- new WallpaperConnection.WallpaperConnectionListener() {
- @Override
- public void onEngineShown() {}
- }, mPreviewGlobalRect);
-
- LiveTileOverlay.INSTANCE.update(new RectF(mPreviewLocalRect), card.getRadius());
-
- mWallpaperConnection.setVisibility(true);
- mHomePreview.post(() -> {
- if (!mWallpaperConnection.connect()) {
- mWallpaperConnection = null;
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- }
- });
- }
-
- void setWallpaperConnectionVisibility(boolean visibility) {
- if (mWallpaperConnection != null) {
- mWallpaperConnection.setVisibility(visibility);
- }
- }
-
- void disconnectWallpaperConnection() {
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- mWallpaperConnection = null;
- }
- }
- }
-
- /**
- * Adapter class for mPreviewPager.
- * This is a ViewPager as it allows for a nice pagination effect (ie, pages snap on swipe,
- * we don't want to just scroll)
- */
- class GridPreviewAdapter extends BasePreviewAdapter<GridPreviewPage> {
-
- GridPreviewAdapter(GridOption gridOption) {
- super(getContext(), R.layout.grid_preview_card);
- for (int i = 0; i < gridOption.previewPagesCount; i++) {
- addPage(new GridPreviewPage(getActivity(), i,
- gridOption.previewImageUri.buildUpon().appendPath("" + i).build(),
- gridOption.name, gridOption.rows, gridOption.cols));
- }
- }
-
- void setWallpaperConnectionVisibility(boolean visibility) {
- for (GridPreviewPage page : mPages) {
- page.setWallpaperConnectionVisibility(visibility);
- }
- }
-
- void disconnectWallpaperConnection() {
- for (GridPreviewPage page : mPages) {
- page.disconnectWallpaperConnection();
- }
- }
- }
}
diff --git a/src/com/android/customization/picker/grid/GridFullPreviewFragment.java b/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
index 46de83b..68ecc5b 100644
--- a/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
+++ b/src/com/android/customization/picker/grid/GridFullPreviewFragment.java
@@ -16,24 +16,13 @@
package com.android.customization.picker.grid;
import static android.app.Activity.RESULT_OK;
-import static android.view.View.MeasureSpec.EXACTLY;
-import static android.view.View.MeasureSpec.makeMeasureSpec;
-import static com.android.customization.picker.grid.GridFragment.PREVIEW_FADE_DURATION_MS;
import static com.android.wallpaper.widget.BottomActionBar.BottomAction.APPLY;
import android.app.Activity;
import android.content.Intent;
-import android.graphics.Rect;
-import android.graphics.RectF;
import android.os.Bundle;
-import android.os.Message;
-import android.os.RemoteException;
-import android.service.wallpaper.WallpaperService;
import android.view.LayoutInflater;
-import android.view.Surface;
-import android.view.SurfaceControlViewHost;
-import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
@@ -41,29 +30,21 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
-import androidx.cardview.widget.CardView;
-import androidx.core.content.ContextCompat;
import com.android.customization.model.grid.GridOption;
import com.android.customization.model.grid.GridOptionsManager;
import com.android.customization.model.grid.LauncherGridOptionsProvider;
import com.android.customization.module.CustomizationInjector;
import com.android.customization.module.ThemesUserEventLogger;
+import com.android.customization.picker.WallpaperPreviewer;
import com.android.wallpaper.R;
-import com.android.wallpaper.asset.Asset;
-import com.android.wallpaper.asset.ContentUriAsset;
-import com.android.wallpaper.model.LiveWallpaperInfo;
import com.android.wallpaper.model.WallpaperInfo;
import com.android.wallpaper.module.InjectorProvider;
import com.android.wallpaper.picker.AppbarFragment;
-import com.android.wallpaper.util.ScreenSizeCalculator;
-import com.android.wallpaper.util.SizeCalculator;
-import com.android.wallpaper.util.SurfaceViewUtils;
-import com.android.wallpaper.util.WallpaperConnection;
import com.android.wallpaper.widget.BottomActionBar;
-import com.android.wallpaper.widget.LiveTileOverlay;
-import com.bumptech.glide.request.RequestOptions;
+import com.bumptech.glide.Glide;
+
/** A Fragment for grid full preview page. */
public class GridFullPreviewFragment extends AppbarFragment {
@@ -72,24 +53,12 @@
static final String EXTRA_GRID_OPTION = "grid_option";
static final String EXTRA_GRID_USES_SURFACE_VIEW = "uses_surface_view";
- private final Rect mPreviewLocalRect = new Rect();
- private final Rect mPreviewGlobalRect = new Rect();
- private final int[] mLivePreviewLocation = new int[2];
-
- private GridOptionsManager mGridManager;
private WallpaperInfo mWallpaper;
private GridOption mGridOption;
private boolean mUsesSurfaceView;
- private CardView mCardView;
- private ImageView mHomePreview;
- private SurfaceView mGridOptionSurface;
- private SurfaceView mWallpaperSurface;
- private WallpaperConnection mWallpaperConnection;
-
- // Home workspace surface is behind the app window, and so must the home image wallpaper like
- // the live wallpaper. This view is rendered on mWallpaperSurface for home image wallpaper.
- private ImageView mHomeImageWallpaper;
+ private WallpaperPreviewer mWallpaperPreviewer;
+ private GridOptionPreviewer mGridOptionPreviewer;
/**
* Returns a new {@link GridFullPreviewFragment} with the provided title and bundle arguments
@@ -110,14 +79,6 @@
mWallpaper = getArguments().getParcelable(EXTRA_WALLPAPER_INFO);
mGridOption = getArguments().getParcelable(EXTRA_GRID_OPTION);
mUsesSurfaceView = getArguments().getBoolean(EXTRA_GRID_USES_SURFACE_VIEW);
-
- CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
- ThemesUserEventLogger eventLogger = (ThemesUserEventLogger) injector.getUserEventLogger(
- getContext());
-
- mGridManager = new GridOptionsManager(new LauncherGridOptionsProvider(getContext(),
- getString(R.string.grid_control_metadata_name)),
- eventLogger);
}
@Nullable
@@ -128,25 +89,31 @@
R.layout.fragment_grid_full_preview, container, /* attachToRoot */ false);
setUpToolbar(view);
- mCardView = view.findViewById(R.id.grid_full_preview_card);
- mHomePreview = view.findViewById(R.id.grid_full_preview_image);
- mGridOptionSurface = view.findViewById(R.id.grid_full_preview_option_surface);
- mWallpaperSurface = view.findViewById(R.id.grid_full_preview_wallpaper_surface);
- mGridOptionSurface.setVisibility(View.GONE);
+ // Clear memory cache whenever grid fragment view is being loaded.
+ Glide.get(getContext()).clearMemory();
- final float screenAspectRatio =
- ScreenSizeCalculator.getInstance().getScreenAspectRatio(getContext());
+ ImageView wallpaperPreviewImage = view.findViewById(R.id.wallpaper_preview_image);
+ SurfaceView wallpaperSurface = view.findViewById(R.id.wallpaper_preview_surface);
+ mWallpaperPreviewer = new WallpaperPreviewer(
+ getLifecycle(), getActivity(), wallpaperPreviewImage, wallpaperSurface);
+
+ CustomizationInjector injector = (CustomizationInjector) InjectorProvider.getInjector();
+ ThemesUserEventLogger eventLogger = (ThemesUserEventLogger) injector.getUserEventLogger(
+ getContext());
+ final GridOptionsManager gridManager = new GridOptionsManager(
+ new LauncherGridOptionsProvider(getContext(),
+ getString(R.string.grid_control_metadata_name)),
+ eventLogger);
+
+ ViewGroup gridPreviewContainer = view.findViewById(R.id.grid_preview_container);
+ mGridOptionPreviewer = new GridOptionPreviewer(
+ getContext(), gridManager, gridPreviewContainer);
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
- final int cardWidth = (int) (mCardView.getMeasuredHeight() / screenAspectRatio);
- ViewGroup.LayoutParams layoutParams = mCardView.getLayoutParams();
- layoutParams.width = cardWidth;
- mCardView.setLayoutParams(layoutParams);
- mCardView.setRadius(SizeCalculator.getPreviewCornerRadius(
- getActivity(), cardWidth));
+ mWallpaperPreviewer.updatePreviewCardRadius();
view.removeOnLayoutChangeListener(this);
}
});
@@ -156,8 +123,16 @@
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
- updateWallpaperSurface();
- updateWorkspaceSurface();
+ mWallpaperPreviewer.setWallpaper(mWallpaper);
+ mGridOptionPreviewer.setGridOption(mGridOption, mUsesSurfaceView);
+ }
+
+ @Override
+ public void onDestroy() {
+ super.onDestroy();
+ if (mGridOptionPreviewer != null) {
+ mGridOptionPreviewer.release();
+ }
}
@Override
@@ -168,32 +143,6 @@
bottomActionBar.show();
}
- @Override
- public void onResume() {
- super.onResume();
- if (mWallpaperConnection != null) {
- mWallpaperConnection.setVisibility(true);
- }
- }
-
- @Override
- public void onPause() {
- super.onPause();
- if (mWallpaperConnection != null) {
- mWallpaperConnection.setVisibility(false);
- }
- }
-
- @Override
- public void onDestroyView() {
- super.onDestroyView();
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- mWallpaperConnection = null;
- }
- }
-
private void finishActivityWithResultOk() {
Activity activity = requireActivity();
activity.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
@@ -202,159 +151,4 @@
activity.setResult(RESULT_OK, intent);
activity.finish();
}
-
- private void updateWallpaperSurface() {
- mWallpaperSurface.setZOrderMediaOverlay(false);
- mWallpaperSurface.getHolder().addCallback(mWallpaperSurfaceCallback);
- }
-
- private void updateWorkspaceSurface() {
- if (mUsesSurfaceView) {
- mGridOptionSurface.setZOrderOnTop(true);
- mGridOptionSurface.getHolder().addCallback(mGridOptionSurfaceCallback);
- mGridOptionSurface.setVisibility(View.VISIBLE);
- } else {
- final Asset previewAsset = new ContentUriAsset(
- getContext(),
- mGridOption.previewImageUri,
- RequestOptions.fitCenterTransform());
- previewAsset.loadDrawableWithTransition(getContext(),
- mHomePreview /* imageView */,
- PREVIEW_FADE_DURATION_MS /* duration */,
- null /* drawableLoadedListener */,
- getResources().getColor(android.R.color.transparent,
- null) /* placeHolderColorJ */);
- }
- }
-
- private void setUpWallpaperPreview() {
- if (mWallpaper != null && mHomeImageWallpaper != null) {
- boolean renderInImageWallpaperSurface = !(mWallpaper instanceof LiveWallpaperInfo);
- mWallpaper.getThumbAsset(getContext())
- .loadPreviewImage(getActivity(),
- renderInImageWallpaperSurface ? mHomeImageWallpaper : mHomePreview,
- getResources().getColor(R.color.secondary_color));
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- if (mWallpaper instanceof LiveWallpaperInfo) {
- mWallpaper.getThumbAsset(getContext().getApplicationContext())
- .loadPreviewImage(
- getActivity(),
- mHomeImageWallpaper,
- getContext().getColor(R.color.secondary_color));
- setUpLiveWallpaperPreview(mWallpaper);
- } else {
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- mWallpaperConnection = null;
- }
- }
- }
- }
-
- private void setUpLiveWallpaperPreview(WallpaperInfo homeWallpaper) {
- Activity activity = getActivity();
- if (activity == null) {
- return;
- }
-
- if (mWallpaperConnection != null) {
- mWallpaperConnection.disconnect();
- }
-
- mHomePreview.getLocationOnScreen(mLivePreviewLocation);
- mPreviewGlobalRect.set(0, 0, mHomePreview.getMeasuredWidth(),
- mHomePreview.getMeasuredHeight());
- mPreviewLocalRect.set(mPreviewGlobalRect);
- mPreviewGlobalRect.offset(mLivePreviewLocation[0], mLivePreviewLocation[1]);
-
- mWallpaperConnection = new WallpaperConnection(
- getWallpaperIntent(homeWallpaper.getWallpaperComponent()), activity,
- new WallpaperConnection.WallpaperConnectionListener() {
- @Override
- public void onEngineShown() {}
- }, mPreviewGlobalRect);
-
- LiveTileOverlay.INSTANCE.update(new RectF(mPreviewLocalRect), mCardView.getRadius());
-
- mWallpaperConnection.setVisibility(true);
- mHomePreview.post(() -> {
- if (!mWallpaperConnection.connect()) {
- mWallpaperConnection = null;
- LiveTileOverlay.INSTANCE.detach(mHomePreview.getOverlay());
- }
- });
- }
-
- private Intent getWallpaperIntent(android.app.WallpaperInfo info) {
- return new Intent(WallpaperService.SERVICE_INTERFACE)
- .setClassName(info.getPackageName(), info.getServiceName());
- }
-
- private final SurfaceHolder.Callback mGridOptionSurfaceCallback = new SurfaceHolder.Callback() {
-
- private Surface mLastSurface;
- private Message mCallback;
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if (mLastSurface != holder.getSurface()) {
- mLastSurface = holder.getSurface();
- Bundle result = mGridManager.renderPreview(
- SurfaceViewUtils.createSurfaceViewRequest(mGridOptionSurface),
- mGridOption.name);
- if (result != null) {
- mGridOptionSurface.setChildSurfacePackage(
- SurfaceViewUtils.getSurfacePackage(result));
- mCallback = SurfaceViewUtils.getCallback(result);
- }
- }
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {
- if (mCallback != null) {
- try {
- mCallback.replyTo.send(mCallback);
- } catch (RemoteException e) {
- e.printStackTrace();
- } finally {
- mCallback = null;
- }
- }
- }
- };
-
- private final SurfaceHolder.Callback mWallpaperSurfaceCallback = new SurfaceHolder.Callback() {
-
- private Surface mLastSurface;
-
- @Override
- public void surfaceCreated(SurfaceHolder holder) {
- if (mLastSurface != holder.getSurface()) {
- mLastSurface = holder.getSurface();
- mHomeImageWallpaper = new ImageView(getContext());
- mHomeImageWallpaper.setBackgroundColor(
- ContextCompat.getColor(getContext(), R.color.primary_color));
- mHomeImageWallpaper.measure(makeMeasureSpec(mHomePreview.getWidth(), EXACTLY),
- makeMeasureSpec(mHomePreview.getHeight(), EXACTLY));
- mHomeImageWallpaper.layout(0, 0, mHomePreview.getWidth(), mHomePreview.getHeight());
-
- SurfaceControlViewHost host = new SurfaceControlViewHost(getContext(),
- getContext().getDisplay(), mWallpaperSurface.getHostToken());
- host.setView(mHomeImageWallpaper, mHomeImageWallpaper.getWidth(),
- mHomeImageWallpaper.getHeight());
- mWallpaperSurface.setChildSurfacePackage(host.getSurfacePackage());
- }
- setUpWallpaperPreview();
- }
-
- @Override
- public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
-
- @Override
- public void surfaceDestroyed(SurfaceHolder holder) {}
- };
}
diff --git a/src/com/android/customization/picker/grid/GridOptionPreviewer.java b/src/com/android/customization/picker/grid/GridOptionPreviewer.java
new file mode 100644
index 0000000..993d873
--- /dev/null
+++ b/src/com/android/customization/picker/grid/GridOptionPreviewer.java
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2020 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.customization.picker.grid;
+
+import android.content.Context;
+import android.os.Bundle;
+import android.os.Message;
+import android.os.RemoteException;
+import android.view.Surface;
+import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.view.ViewGroup;
+import android.widget.ImageView;
+
+import com.android.customization.model.grid.GridOption;
+import com.android.customization.model.grid.GridOptionsManager;
+import com.android.wallpaper.asset.Asset;
+import com.android.wallpaper.asset.ContentUriAsset;
+import com.android.wallpaper.util.SurfaceViewUtils;
+
+import com.bumptech.glide.request.RequestOptions;
+
+/** A class to load the {@link GridOption} preview to the view. */
+class GridOptionPreviewer {
+
+ private static final int PREVIEW_FADE_DURATION_MS = 100;
+
+ private final Context mContext;
+ private final GridOptionsManager mGridManager;
+ private final ViewGroup mPreviewContainer;
+
+ private SurfaceView mGridOptionSurface;
+ private GridOption mGridOption;
+
+ GridOptionPreviewer(Context context, GridOptionsManager gridManager,
+ ViewGroup previewContainer) {
+ mContext = context;
+ mGridManager = gridManager;
+ mPreviewContainer = previewContainer;
+ }
+
+ /** Loads the Grid option into the container view. */
+ public void setGridOption(GridOption gridOption, boolean usesSurfaceView) {
+ mGridOption = gridOption;
+ updateWorkspacePreview(usesSurfaceView);
+ }
+
+ /** Releases the view resource. */
+ public void release() {
+ if (mGridOptionSurface != null) {
+ mGridOptionSurface.getHolder().removeCallback(mSurfaceCallback);
+ Surface surface = mGridOptionSurface.getHolder().getSurface();
+ if (surface != null) {
+ surface.release();
+ }
+ }
+ mPreviewContainer.removeAllViews();
+ }
+
+ private void updateWorkspacePreview(boolean usesSurfaceView) {
+ if (mGridOption == null) {
+ return;
+ }
+ mPreviewContainer.removeAllViews();
+
+ if (usesSurfaceView) {
+ mGridOptionSurface = new SurfaceView(mContext);
+ mPreviewContainer.addView(mGridOptionSurface);
+ mGridOptionSurface.setZOrderOnTop(true);
+ mGridOptionSurface.getHolder().addCallback(mSurfaceCallback);
+ } else {
+ final ImageView previewImage = new ImageView(mContext);
+ mPreviewContainer.addView(previewImage);
+ final Asset previewAsset = new ContentUriAsset(
+ mContext,
+ mGridOption.previewImageUri,
+ RequestOptions.fitCenterTransform());
+ previewAsset.loadDrawableWithTransition(mContext,
+ previewImage /* imageView */,
+ PREVIEW_FADE_DURATION_MS /* duration */,
+ null /* drawableLoadedListener */,
+ mContext.getResources().getColor(android.R.color.transparent,
+ null) /* placeHolderColorJ */);
+ }
+ }
+
+ private final SurfaceHolder.Callback mSurfaceCallback = new SurfaceHolder.Callback() {
+ private Surface mLastSurface;
+ private Message mCallback;
+
+ @Override
+ public void surfaceCreated(SurfaceHolder holder) {
+ if (mLastSurface != holder.getSurface() && mGridOption != null) {
+ mLastSurface = holder.getSurface();
+ Bundle result = mGridManager.renderPreview(
+ SurfaceViewUtils.createSurfaceViewRequest(mGridOptionSurface),
+ mGridOption.name);
+ if (result != null) {
+ mGridOptionSurface.setChildSurfacePackage(
+ SurfaceViewUtils.getSurfacePackage(result));
+ mCallback = SurfaceViewUtils.getCallback(result);
+ }
+ }
+ }
+
+ @Override
+ public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {}
+
+ @Override
+ public void surfaceDestroyed(SurfaceHolder holder) {
+ if (mCallback != null) {
+ try {
+ mCallback.replyTo.send(mCallback);
+ } catch (RemoteException e) {
+ e.printStackTrace();
+ } finally {
+ mCallback.recycle();
+ mCallback = null;
+ }
+ }
+ }
+ };
+}