Debian Codegraph @

libs/WindowManager/Shell/src/com/android/wm/shell/onehanded/BackgroundWindowManager.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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
17 package com.android.wm.shell.onehanded;
18
19 import static android.view.WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
20 import static android.view.WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
21 import static android.view.WindowManager.LayoutParams.FLAG_SLIPPERY;
22 import static android.view.WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
23 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_NO_MOVE_ANIMATION;
24 import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY;
25
26 import static com.android.wm.shell.onehanded.OneHandedState.STATE_ACTIVE;
27 import static com.android.wm.shell.onehanded.OneHandedState.STATE_ENTERING;
28
29 import android.content.Context;
30 import android.content.res.Configuration;
31 import android.graphics.Color;
32 import android.graphics.PixelFormat;
33 import android.graphics.Rect;
34 import android.os.Binder;
35 import android.util.Slog;
36 import android.view.ContextThemeWrapper;
37 import android.view.IWindow;
38 import android.view.LayoutInflater;
39 import android.view.SurfaceControl;
40 import android.view.SurfaceControlViewHost;
41 import android.view.SurfaceSession;
42 import android.view.View;
43 import android.view.WindowManager;
44 import android.view.WindowlessWindowManager;
45
46 import androidx.annotation.NonNull;
47 import androidx.annotation.Nullable;
48
49 import com.android.wm.shell.R;
50 import com.android.wm.shell.common.DisplayLayout;
51
52 import java.io.PrintWriter;
53
54 /**
55 * Holds view hierarchy of a root surface and helps inflate a themeable view for background.
56 */
57 public final class BackgroundWindowManager extends WindowlessWindowManager {
58 private static final String TAG = BackgroundWindowManager.class.getSimpleName();
59 private static final int THEME_COLOR_OFFSET = 10;
60
61 private final OneHandedSurfaceTransactionHelper.SurfaceControlTransactionFactory
62 mTransactionFactory;
63
64 private Context mContext;
65 private Rect mDisplayBounds;
66 private SurfaceControlViewHost mViewHost;
67 private SurfaceControl mLeash;
68 private View mBackgroundView;
69 private @OneHandedState.State int mCurrentState;
70
71 public BackgroundWindowManager(Context context) {
72 super(context.getResources().getConfiguration(), null /* rootSurface */,
73 null /* hostInputToken */);
74 mContext = context;
75 mTransactionFactory = SurfaceControl.Transaction::new;
76 }
77
78 @Override
79 public SurfaceControl getSurfaceControl(IWindow window) {
80 return super.getSurfaceControl(window);
81 }
82
83 @Override
84 public void setConfiguration(Configuration configuration) {
85 super.setConfiguration(configuration);
86 mContext = mContext.createConfigurationContext(configuration);
87 }
88
89 /**
90 * onConfigurationChanged events for updating background theme color.
91 */
92 public void onConfigurationChanged() {
93 if (mCurrentState == STATE_ENTERING || mCurrentState == STATE_ACTIVE) {
94 updateThemeOnly();
95 }
96 }
97
98 /**
99 * One-handed mode state changed callback
100 * @param newState of One-handed mode representing by {@link OneHandedState}
101 */
102 public void onStateChanged(int newState) {
103 mCurrentState = newState;
104 }
105
106 @Override
107 protected void attachToParentSurface(IWindow window, SurfaceControl.Builder b) {
108 final SurfaceControl.Builder builder = new SurfaceControl.Builder(new SurfaceSession())
109 .setColorLayer()
110 .setBufferSize(mDisplayBounds.width(), mDisplayBounds.height())
111 .setFormat(PixelFormat.RGB_888)
112 .setOpaque(true)
113 .setName(TAG)
114 .setCallsite("BackgroundWindowManager#attachToParentSurface");
115 mLeash = builder.build();
116 b.setParent(mLeash);
117 }
118
119 /** Inflates background view on to the root surface. */
120 boolean initView() {
121 if (mBackgroundView != null || mViewHost != null) {
122 return false;
123 }
124
125 mViewHost = new SurfaceControlViewHost(mContext, mContext.getDisplay(), this);
126 mBackgroundView = (View) LayoutInflater.from(mContext)
127 .inflate(R.layout.background_panel, null /* root */);
128 WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
129 mDisplayBounds.width(), mDisplayBounds.height(), 0 /* TYPE NONE */,
130 FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL | FLAG_WATCH_OUTSIDE_TOUCH
131 | FLAG_SLIPPERY, PixelFormat.TRANSLUCENT);
132 lp.token = new Binder();
133 lp.setTitle("background-panel");
134 lp.privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION | PRIVATE_FLAG_TRUSTED_OVERLAY;
135 mBackgroundView.setBackgroundColor(getThemeColorForBackground());
136 mViewHost.setView(mBackgroundView, lp);
137 return true;
138 }
139
140 /**
141 * Called when onDisplayAdded() or onDisplayRemoved() callback.
142 * @param displayLayout The latest {@link DisplayLayout} for display bounds.
143 */
144 public void onDisplayChanged(DisplayLayout displayLayout) {
145 mDisplayBounds = new Rect(0, 0, displayLayout.width(), displayLayout.height());
146 }
147
148 private void updateThemeOnly() {
149 if (mBackgroundView == null || mViewHost == null || mLeash == null) {
150 Slog.w(TAG, "Background view or SurfaceControl does not exist when trying to "
151 + "update theme only!");
152 return;
153 }
154
155 WindowManager.LayoutParams lp = (WindowManager.LayoutParams)
156 mBackgroundView.getLayoutParams();
157 mBackgroundView.setBackgroundColor(getThemeColorForBackground());
158 mViewHost.setView(mBackgroundView, lp);
159 }
160
161 /**
162 * Shows the background layer when One-handed mode triggered.
163 */
164 public void showBackgroundLayer() {
165 if (!initView()) {
166 updateThemeOnly();
167 return;
168 }
169 if (mLeash == null) {
170 Slog.w(TAG, "SurfaceControl mLeash is null, can't show One-handed mode "
171 + "background panel!");
172 return;
173 }
174
175 mTransactionFactory.getTransaction()
176 .setAlpha(mLeash, 1.0f)
177 .setLayer(mLeash, -1 /* at bottom-most layer */)
178 .show(mLeash)
179 .apply();
180 }
181
182 /**
183 * Remove the leash of background layer after stop One-handed mode.
184 */
185 public void removeBackgroundLayer() {
186 if (mBackgroundView != null) {
187 mBackgroundView = null;
188 }
189
190 if (mViewHost != null) {
191 mViewHost.release();
192 mViewHost = null;
193 }
194
195 if (mLeash != null) {
196 mTransactionFactory.getTransaction().remove(mLeash).apply();
197 mLeash = null;
198 }
199 }
200
201 /**
202 * Gets {@link SurfaceControl} of the background layer.
203 * @return {@code null} if not exist.
204 */
205 @Nullable
206 SurfaceControl getSurfaceControl() {
207 return mLeash;
208 }
209
210 private int getThemeColor() {
211 final Context themedContext = new ContextThemeWrapper(mContext,
212 com.android.internal.R.style.Theme_DeviceDefault_DayNight);
213 return themedContext.getColor(R.color.one_handed_tutorial_background_color);
214 }
215
216 int getThemeColorForBackground() {
217 final int origThemeColor = getThemeColor();
218 return android.graphics.Color.argb(Color.alpha(origThemeColor),
219 Color.red(origThemeColor) - THEME_COLOR_OFFSET,
220 Color.green(origThemeColor) - THEME_COLOR_OFFSET,
221 Color.blue(origThemeColor) - THEME_COLOR_OFFSET);
222 }
223
224 private float adjustColor(int origColor) {
225 return Math.max(origColor - THEME_COLOR_OFFSET, 0) / 255.0f;
226 }
227
228 void dump(@NonNull PrintWriter pw) {
229 final String innerPrefix = " ";
230 pw.println(TAG);
231 pw.print(innerPrefix + "mDisplayBounds=");
232 pw.println(mDisplayBounds);
233 pw.print(innerPrefix + "mViewHost=");
234 pw.println(mViewHost);
235 pw.print(innerPrefix + "mLeash=");
236 pw.println(mLeash);
237 pw.print(innerPrefix + "mBackgroundView=");
238 pw.println(mBackgroundView);
239 }
240
241 }