• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.Bundle;
20 import android.view.SurfaceHolder;
21 import android.view.SurfaceView;
22 import android.view.ViewGroup;
23 
24 import com.android.customization.model.grid.GridOption;
25 import com.android.customization.model.grid.GridOptionsManager;
26 import com.android.wallpaper.picker.WorkspaceSurfaceHolderCallback;
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.resetLastSurface();
66         }
67         if (mGridOptionSurface == null) {
68             mGridOptionSurface = new SurfaceView(mPreviewContainer.getContext());
69             mGridOptionSurface.setLayoutParams(new ViewGroup.LayoutParams(
70                     ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
71             mGridOptionSurface.setZOrderMediaOverlay(true);
72             mSurfaceCallback = new GridOptionSurfaceHolderCallback(mGridOptionSurface,
73                     mGridOptionSurface.getContext());
74             mGridOptionSurface.getHolder().addCallback(mSurfaceCallback);
75         }
76         mPreviewContainer.addView(mGridOptionSurface);
77     }
78 
79     private class GridOptionSurfaceHolderCallback extends WorkspaceSurfaceHolderCallback {
GridOptionSurfaceHolderCallback(SurfaceView workspaceSurface, Context context)80         private GridOptionSurfaceHolderCallback(SurfaceView workspaceSurface, Context context) {
81             super(workspaceSurface, context);
82         }
83 
84         @Override
surfaceCreated(SurfaceHolder holder)85         public void surfaceCreated(SurfaceHolder holder) {
86             if (mGridOption != null) {
87                 super.surfaceCreated(holder);
88             }
89         }
90 
91         @Override
renderPreview(SurfaceView workspaceSurface)92         protected Bundle renderPreview(SurfaceView workspaceSurface) {
93             return mGridManager.renderPreview(
94                     SurfaceViewUtils.createSurfaceViewRequest(workspaceSurface),
95                     mGridOption.name);
96         }
97     }
98 }
99