1 /* 2 * Copyright (C) 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.android.customization.picker.grid; 17 18 import android.content.Context; 19 import android.view.SurfaceHolder; 20 import android.view.SurfaceView; 21 import android.view.ViewGroup; 22 23 import com.android.customization.model.grid.GridOption; 24 import com.android.customization.model.grid.GridOptionsManager; 25 import com.android.wallpaper.picker.WorkspaceSurfaceHolderCallback; 26 import com.android.wallpaper.util.PreviewUtils; 27 import com.android.wallpaper.util.SurfaceViewUtils; 28 29 /** A class to load the {@link GridOption} preview to the view. */ 30 class GridOptionPreviewer { 31 32 private final GridOptionsManager mGridManager; 33 private final ViewGroup mPreviewContainer; 34 35 private SurfaceView mGridOptionSurface; 36 private GridOption mGridOption; 37 private GridOptionSurfaceHolderCallback mSurfaceCallback; 38 GridOptionPreviewer(GridOptionsManager gridManager, ViewGroup previewContainer)39 GridOptionPreviewer(GridOptionsManager gridManager, ViewGroup previewContainer) { 40 mGridManager = gridManager; 41 mPreviewContainer = previewContainer; 42 } 43 44 /** Loads the Grid option into the container view. */ setGridOption(GridOption gridOption)45 public void setGridOption(GridOption gridOption) { 46 mGridOption = gridOption; 47 if (mGridOption != null) { 48 updateWorkspacePreview(); 49 } 50 } 51 52 /** Releases the view resource. */ release()53 public void release() { 54 if (mGridOptionSurface != null) { 55 mSurfaceCallback.cleanUp(); 56 mGridOptionSurface = null; 57 } 58 mPreviewContainer.removeAllViews(); 59 } 60 updateWorkspacePreview()61 private void updateWorkspacePreview() { 62 // Reattach SurfaceView to trigger #surfaceCreated to update preview for different option. 63 mPreviewContainer.removeAllViews(); 64 if (mSurfaceCallback != null) { 65 mSurfaceCallback.cleanUp(); 66 mSurfaceCallback.resetLastSurface(); 67 } 68 if (mGridOptionSurface == null) { 69 mGridOptionSurface = new SurfaceView(mPreviewContainer.getContext()); 70 mGridOptionSurface.setLayoutParams(new ViewGroup.LayoutParams( 71 ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); 72 mGridOptionSurface.setZOrderMediaOverlay(true); 73 mSurfaceCallback = new GridOptionSurfaceHolderCallback(mGridOptionSurface, 74 mGridOptionSurface.getContext()); 75 mGridOptionSurface.getHolder().addCallback(mSurfaceCallback); 76 } 77 mPreviewContainer.addView(mGridOptionSurface); 78 } 79 80 private class GridOptionSurfaceHolderCallback extends WorkspaceSurfaceHolderCallback { GridOptionSurfaceHolderCallback(SurfaceView workspaceSurface, Context context)81 private GridOptionSurfaceHolderCallback(SurfaceView workspaceSurface, Context context) { 82 super(workspaceSurface, context); 83 } 84 85 @Override surfaceCreated(SurfaceHolder holder)86 public void surfaceCreated(SurfaceHolder holder) { 87 if (mGridOption != null) { 88 super.surfaceCreated(holder); 89 } 90 } 91 92 @Override requestPreview(SurfaceView workspaceSurface, PreviewUtils.WorkspacePreviewCallback callback)93 protected void requestPreview(SurfaceView workspaceSurface, 94 PreviewUtils.WorkspacePreviewCallback callback) { 95 mGridManager.renderPreview( 96 SurfaceViewUtils.createSurfaceViewRequest(workspaceSurface), 97 mGridOption.name, callback); 98 } 99 } 100 } 101