blob: 6eba9acbab4eb091910b7f5d863f60d1d0a2aa66 [file] [log] [blame]
Winson Chung95c9fca2020-01-22 09:48:40 -08001/*
2 * Copyright (C) 2019 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
17package com.android.systemui.wm;
18
19import android.os.Handler;
20import android.os.RemoteException;
21import android.view.IDisplayWindowRotationCallback;
22import android.view.IDisplayWindowRotationController;
23import android.view.IWindowManager;
Wale Ogunwale57946582020-03-21 14:29:07 -070024import android.window.WindowContainerTransaction;
Winson Chung95c9fca2020-01-22 09:48:40 -080025
26import java.util.ArrayList;
27
28/**
29 * This module deals with display rotations coming from WM. When WM starts a rotation: after it has
30 * frozen the screen, it will call into this class. This will then call all registered local
31 * controllers and give them a chance to queue up task changes to be applied synchronously with that
32 * rotation.
33 */
34public class DisplayChangeController {
35
36 private final Handler mHandler;
37 private final IWindowManager mWmService;
38
39 private final ArrayList<OnDisplayChangingListener> mRotationListener =
40 new ArrayList<>();
41 private final ArrayList<OnDisplayChangingListener> mTmpListeners = new ArrayList<>();
42
43 private final IDisplayWindowRotationController mDisplayRotationController =
44 new IDisplayWindowRotationController.Stub() {
45 @Override
46 public void onRotateDisplay(int displayId, final int fromRotation,
47 final int toRotation, IDisplayWindowRotationCallback callback) {
48 mHandler.post(() -> {
49 WindowContainerTransaction t = new WindowContainerTransaction();
50 synchronized (mRotationListener) {
51 mTmpListeners.clear();
52 // Make a local copy in case the handlers add/remove themselves.
53 mTmpListeners.addAll(mRotationListener);
54 }
55 for (OnDisplayChangingListener c : mTmpListeners) {
56 c.onRotateDisplay(displayId, fromRotation, toRotation, t);
57 }
58 try {
59 callback.continueRotateDisplay(toRotation, t);
60 } catch (RemoteException e) {
61 }
62 });
63 }
64 };
65
66 public DisplayChangeController(Handler mainHandler, IWindowManager wmService) {
67 mHandler = mainHandler;
68 mWmService = wmService;
69 try {
70 mWmService.setDisplayWindowRotationController(mDisplayRotationController);
71 } catch (RemoteException e) {
72 throw new RuntimeException("Unable to register rotation controller");
73 }
74 }
75
76 /**
77 * Adds a display rotation controller.
78 */
79 public void addRotationListener(OnDisplayChangingListener listener) {
80 synchronized (mRotationListener) {
81 mRotationListener.add(listener);
82 }
83 }
84
85 /**
86 * Removes a display rotation controller.
87 */
88 public void removeRotationListener(OnDisplayChangingListener listener) {
89 synchronized (mRotationListener) {
90 mRotationListener.remove(listener);
91 }
92 }
93
94 /**
95 * Give a listener a chance to queue up configuration changes to execute as part of a
96 * display rotation. The contents of {@link #onRotateDisplay} must run synchronously.
97 */
98 public interface OnDisplayChangingListener {
99 /**
100 * Called before the display is rotated. Contents of this method must run synchronously.
101 * @param displayId Id of display that is rotating.
102 * @param fromRotation starting rotation of the display.
103 * @param toRotation target rotation of the display (after rotating).
104 * @param t A task transaction to populate.
105 */
106 void onRotateDisplay(int displayId, int fromRotation, int toRotation,
107 WindowContainerTransaction t);
108 }
109}