| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.wm.shell.compatui; |
| 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.PRIVATE_FLAG_NO_MOVE_ANIMATION; |
| 22 |
import static android.view.WindowManager.LayoutParams.PRIVATE_FLAG_TRUSTED_OVERLAY; |
| 23 |
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY; |
| 24 |
|
| 25 |
import static com.android.internal.annotations.VisibleForTesting.Visibility.PACKAGE; |
| 26 |
import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
| 27 |
import static com.android.internal.annotations.VisibleForTesting.Visibility.PROTECTED; |
| 28 |
|
| 29 |
import android.annotation.Nullable; |
| 30 |
import android.app.TaskInfo; |
| 31 |
import android.content.Context; |
| 32 |
import android.content.res.Configuration; |
| 33 |
import android.graphics.PixelFormat; |
| 34 |
import android.graphics.Rect; |
| 35 |
import android.os.Binder; |
| 36 |
import android.util.Log; |
| 37 |
import android.view.IWindow; |
| 38 |
import android.view.SurfaceControl; |
| 39 |
import android.view.SurfaceControlViewHost; |
| 40 |
import android.view.SurfaceSession; |
| 41 |
import android.view.View; |
| 42 |
import android.view.WindowManager; |
| 43 |
import android.view.WindowlessWindowManager; |
| 44 |
|
| 45 |
import com.android.internal.annotations.VisibleForTesting; |
| 46 |
import com.android.wm.shell.ShellTaskOrganizer; |
| 47 |
import com.android.wm.shell.common.DisplayLayout; |
| 48 |
import com.android.wm.shell.common.SyncTransactionQueue; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public abstract class CompatUIWindowManagerAbstract extends WindowlessWindowManager { |
| 57 |
|
| 58 |
protected final int mTaskId; |
| 59 |
protected Context mContext; |
| 60 |
|
| 61 |
private final SyncTransactionQueue mSyncQueue; |
| 62 |
private final int mDisplayId; |
| 63 |
private Configuration mTaskConfig; |
| 64 |
private ShellTaskOrganizer.TaskListener mTaskListener; |
| 65 |
private DisplayLayout mDisplayLayout; |
| 66 |
private final Rect mStableBounds; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@Nullable |
| 73 |
protected SurfaceControlViewHost mViewHost; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
@Nullable |
| 80 |
protected SurfaceControl mLeash; |
| 81 |
|
| 82 |
protected CompatUIWindowManagerAbstract(Context context, TaskInfo taskInfo, |
| 83 |
SyncTransactionQueue syncQueue, ShellTaskOrganizer.TaskListener taskListener, |
| 84 |
DisplayLayout displayLayout) { |
| 85 |
super(taskInfo.configuration, null , null ); |
| 86 |
mContext = context; |
| 87 |
mSyncQueue = syncQueue; |
| 88 |
mTaskConfig = taskInfo.configuration; |
| 89 |
mDisplayId = mContext.getDisplayId(); |
| 90 |
mTaskId = taskInfo.taskId; |
| 91 |
mTaskListener = taskListener; |
| 92 |
mDisplayLayout = displayLayout; |
| 93 |
mStableBounds = new Rect(); |
| 94 |
mDisplayLayout.getStableBounds(mStableBounds); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
protected abstract int getZOrder(); |
| 104 |
|
| 105 |
|
| 106 |
protected abstract @Nullable View getLayout(); |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
@VisibleForTesting(visibility = PROTECTED) |
| 118 |
public boolean createLayout(boolean canShow) { |
| 119 |
if (!eligibleToShowLayout()) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
if (!canShow || getLayout() != null) { |
| 123 |
|
| 124 |
return true; |
| 125 |
} |
| 126 |
|
| 127 |
if (mViewHost != null) { |
| 128 |
throw new IllegalStateException( |
| 129 |
"A UI has already been created with this window manager."); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
mViewHost = createSurfaceViewHost(); |
| 134 |
mViewHost.setView(createLayout(), getWindowLayoutParams()); |
| 135 |
|
| 136 |
updateSurfacePosition(); |
| 137 |
|
| 138 |
return true; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
protected abstract View createLayout(); |
| 143 |
|
| 144 |
protected abstract void removeLayout(); |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected abstract boolean eligibleToShowLayout(); |
| 150 |
|
| 151 |
@Override |
| 152 |
public void setConfiguration(Configuration configuration) { |
| 153 |
super.setConfiguration(configuration); |
| 154 |
mContext = mContext.createConfigurationContext(configuration); |
| 155 |
} |
| 156 |
|
| 157 |
@Override |
| 158 |
protected void attachToParentSurface(IWindow window, SurfaceControl.Builder b) { |
| 159 |
String className = getClass().getSimpleName(); |
| 160 |
final SurfaceControl.Builder builder = new SurfaceControl.Builder(new SurfaceSession()) |
| 161 |
.setContainerLayer() |
| 162 |
.setName(className + "Leash") |
| 163 |
.setHidden(false) |
| 164 |
.setCallsite(className + "#attachToParentSurface"); |
| 165 |
attachToParentSurface(builder); |
| 166 |
mLeash = builder.build(); |
| 167 |
b.setParent(mLeash); |
| 168 |
|
| 169 |
initSurface(mLeash); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
private void initSurface(SurfaceControl leash) { |
| 174 |
final int z = getZOrder(); |
| 175 |
mSyncQueue.runInSync(t -> { |
| 176 |
if (leash == null || !leash.isValid()) { |
| 177 |
Log.w(getTag(), "The leash has been released."); |
| 178 |
return; |
| 179 |
} |
| 180 |
t.setLayer(leash, z); |
| 181 |
}); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
@VisibleForTesting(visibility = PROTECTED) |
| 193 |
public boolean updateCompatInfo(TaskInfo taskInfo, |
| 194 |
ShellTaskOrganizer.TaskListener taskListener, boolean canShow) { |
| 195 |
final Configuration prevTaskConfig = mTaskConfig; |
| 196 |
final ShellTaskOrganizer.TaskListener prevTaskListener = mTaskListener; |
| 197 |
mTaskConfig = taskInfo.configuration; |
| 198 |
mTaskListener = taskListener; |
| 199 |
|
| 200 |
|
| 201 |
setConfiguration(mTaskConfig); |
| 202 |
|
| 203 |
if (!eligibleToShowLayout()) { |
| 204 |
release(); |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
View layout = getLayout(); |
| 209 |
if (layout == null || prevTaskListener != taskListener) { |
| 210 |
|
| 211 |
|
| 212 |
release(); |
| 213 |
return createLayout(canShow); |
| 214 |
} |
| 215 |
|
| 216 |
boolean boundsUpdated = !mTaskConfig.windowConfiguration.getBounds().equals( |
| 217 |
prevTaskConfig.windowConfiguration.getBounds()); |
| 218 |
boolean layoutDirectionUpdated = |
| 219 |
mTaskConfig.getLayoutDirection() != prevTaskConfig.getLayoutDirection(); |
| 220 |
if (boundsUpdated || layoutDirectionUpdated) { |
| 221 |
onParentBoundsChanged(); |
| 222 |
} |
| 223 |
|
| 224 |
if (layout != null && layoutDirectionUpdated) { |
| 225 |
|
| 226 |
layout.setLayoutDirection(mTaskConfig.getLayoutDirection()); |
| 227 |
} |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
@VisibleForTesting(visibility = PACKAGE) |
| 238 |
public void updateVisibility(boolean canShow) { |
| 239 |
View layout = getLayout(); |
| 240 |
if (layout == null) { |
| 241 |
|
| 242 |
createLayout(canShow); |
| 243 |
return; |
| 244 |
} |
| 245 |
|
| 246 |
final int newVisibility = canShow && eligibleToShowLayout() ? View.VISIBLE : View.GONE; |
| 247 |
if (layout.getVisibility() != newVisibility) { |
| 248 |
layout.setVisibility(newVisibility); |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
@VisibleForTesting(visibility = PACKAGE) |
| 254 |
public void updateDisplayLayout(DisplayLayout displayLayout) { |
| 255 |
final Rect prevStableBounds = mStableBounds; |
| 256 |
final Rect curStableBounds = new Rect(); |
| 257 |
displayLayout.getStableBounds(curStableBounds); |
| 258 |
mDisplayLayout = displayLayout; |
| 259 |
if (!prevStableBounds.equals(curStableBounds)) { |
| 260 |
|
| 261 |
mStableBounds.set(curStableBounds); |
| 262 |
onParentBoundsChanged(); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
@VisibleForTesting(visibility = PRIVATE) |
| 268 |
void attachToParentSurface(SurfaceControl.Builder b) { |
| 269 |
mTaskListener.attachChildSurfaceToTask(mTaskId, b); |
| 270 |
} |
| 271 |
|
| 272 |
public int getDisplayId() { |
| 273 |
return mDisplayId; |
| 274 |
} |
| 275 |
|
| 276 |
public int getTaskId() { |
| 277 |
return mTaskId; |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
public void release() { |
| 282 |
|
| 283 |
View layout = getLayout(); |
| 284 |
if (layout != null) { |
| 285 |
layout.setVisibility(View.GONE); |
| 286 |
} |
| 287 |
removeLayout(); |
| 288 |
|
| 289 |
if (mViewHost != null) { |
| 290 |
mViewHost.release(); |
| 291 |
mViewHost = null; |
| 292 |
} |
| 293 |
|
| 294 |
if (mLeash != null) { |
| 295 |
final SurfaceControl leash = mLeash; |
| 296 |
mSyncQueue.runInSync(t -> t.remove(leash)); |
| 297 |
mLeash = null; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
void relayout() { |
| 303 |
relayout(getWindowLayoutParams()); |
| 304 |
} |
| 305 |
|
| 306 |
protected void relayout(WindowManager.LayoutParams windowLayoutParams) { |
| 307 |
if (mViewHost == null) { |
| 308 |
return; |
| 309 |
} |
| 310 |
mViewHost.relayout(windowLayoutParams); |
| 311 |
updateSurfacePosition(); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
protected void onParentBoundsChanged() { |
| 319 |
updateSurfacePosition(); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
protected abstract void updateSurfacePosition(); |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
protected void updateSurfacePosition(int positionX, int positionY) { |
| 332 |
if (mLeash == null) { |
| 333 |
return; |
| 334 |
} |
| 335 |
mSyncQueue.runInSync(t -> { |
| 336 |
if (mLeash == null || !mLeash.isValid()) { |
| 337 |
Log.w(getTag(), "The leash has been released."); |
| 338 |
return; |
| 339 |
} |
| 340 |
t.setPosition(mLeash, positionX, positionY); |
| 341 |
}); |
| 342 |
} |
| 343 |
|
| 344 |
protected int getLayoutDirection() { |
| 345 |
return mContext.getResources().getConfiguration().getLayoutDirection(); |
| 346 |
} |
| 347 |
|
| 348 |
protected Rect getTaskBounds() { |
| 349 |
return mTaskConfig.windowConfiguration.getBounds(); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
protected Rect getTaskStableBounds() { |
| 354 |
final Rect result = new Rect(mStableBounds); |
| 355 |
result.intersect(getTaskBounds()); |
| 356 |
return result; |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
@VisibleForTesting(visibility = PRIVATE) |
| 361 |
public SurfaceControlViewHost createSurfaceViewHost() { |
| 362 |
return new SurfaceControlViewHost(mContext, mContext.getDisplay(), this); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
protected WindowManager.LayoutParams getWindowLayoutParams() { |
| 367 |
View layout = getLayout(); |
| 368 |
if (layout == null) { |
| 369 |
return new WindowManager.LayoutParams(); |
| 370 |
} |
| 371 |
|
| 372 |
layout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); |
| 373 |
return getWindowLayoutParams(layout.getMeasuredWidth(), layout.getMeasuredHeight()); |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
protected WindowManager.LayoutParams getWindowLayoutParams(int width, int height) { |
| 378 |
final WindowManager.LayoutParams winParams = new WindowManager.LayoutParams( |
| 379 |
|
| 380 |
width, height, |
| 381 |
TYPE_APPLICATION_OVERLAY, |
| 382 |
FLAG_NOT_FOCUSABLE | FLAG_NOT_TOUCH_MODAL, |
| 383 |
PixelFormat.TRANSLUCENT); |
| 384 |
winParams.token = new Binder(); |
| 385 |
winParams.setTitle(getClass().getSimpleName() + mTaskId); |
| 386 |
winParams.privateFlags |= PRIVATE_FLAG_NO_MOVE_ANIMATION | PRIVATE_FLAG_TRUSTED_OVERLAY; |
| 387 |
return winParams; |
| 388 |
} |
| 389 |
|
| 390 |
protected final String getTag() { |
| 391 |
return getClass().getSimpleName(); |
| 392 |
} |
| 393 |
} |