1 /*
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 
17 package com.android.wm.shell.common;
18 
19 import android.os.RemoteException;
20 import android.util.Slog;
21 import android.view.IDisplayWindowRotationCallback;
22 import android.view.IDisplayWindowRotationController;
23 import android.view.IWindowManager;
24 import android.window.WindowContainerTransaction;
25 
26 import androidx.annotation.BinderThread;
27 
28 import com.android.wm.shell.common.annotations.ShellMainThread;
29 
30 import java.util.ArrayList;
31 import java.util.concurrent.CopyOnWriteArrayList;
32 
33 /**
34  * This module deals with display rotations coming from WM. When WM starts a rotation: after it has
35  * frozen the screen, it will call into this class. This will then call all registered local
36  * controllers and give them a chance to queue up task changes to be applied synchronously with that
37  * rotation.
38  */
39 public class DisplayChangeController {
40     private static final String TAG = DisplayChangeController.class.getSimpleName();
41 
42     private final ShellExecutor mMainExecutor;
43     private final IWindowManager mWmService;
44     private final IDisplayWindowRotationController mControllerImpl;
45 
46     private final CopyOnWriteArrayList<OnDisplayChangingListener> mRotationListener =
47             new CopyOnWriteArrayList<>();
48 
DisplayChangeController(IWindowManager wmService, ShellExecutor mainExecutor)49     public DisplayChangeController(IWindowManager wmService, ShellExecutor mainExecutor) {
50         mMainExecutor = mainExecutor;
51         mWmService = wmService;
52         mControllerImpl = new DisplayWindowRotationControllerImpl();
53         try {
54             mWmService.setDisplayWindowRotationController(mControllerImpl);
55         } catch (RemoteException e) {
56             throw new RuntimeException("Unable to register rotation controller");
57         }
58     }
59 
60     /**
61      * Adds a display rotation controller.
62      */
addRotationListener(OnDisplayChangingListener listener)63     public void addRotationListener(OnDisplayChangingListener listener) {
64         mRotationListener.add(listener);
65     }
66 
67     /**
68      * Removes a display rotation controller.
69      */
removeRotationListener(OnDisplayChangingListener listener)70     public void removeRotationListener(OnDisplayChangingListener listener) {
71         mRotationListener.remove(listener);
72     }
73 
onRotateDisplay(int displayId, final int fromRotation, final int toRotation, IDisplayWindowRotationCallback callback)74     private void onRotateDisplay(int displayId, final int fromRotation, final int toRotation,
75             IDisplayWindowRotationCallback callback) {
76         WindowContainerTransaction t = new WindowContainerTransaction();
77         for (OnDisplayChangingListener c : mRotationListener) {
78             c.onRotateDisplay(displayId, fromRotation, toRotation, t);
79         }
80         try {
81             callback.continueRotateDisplay(toRotation, t);
82         } catch (RemoteException e) {
83             Slog.e(TAG, "Failed to continue rotation", e);
84         }
85     }
86 
87     @BinderThread
88     private class DisplayWindowRotationControllerImpl
89             extends IDisplayWindowRotationController.Stub {
90         @Override
onRotateDisplay(int displayId, final int fromRotation, final int toRotation, IDisplayWindowRotationCallback callback)91         public void onRotateDisplay(int displayId, final int fromRotation,
92                 final int toRotation, IDisplayWindowRotationCallback callback) {
93             mMainExecutor.execute(() -> {
94                 DisplayChangeController.this.onRotateDisplay(displayId, fromRotation, toRotation,
95                         callback);
96             });
97         }
98     }
99 
100     /**
101      * Give a listener a chance to queue up configuration changes to execute as part of a
102      * display rotation. The contents of {@link #onRotateDisplay} must run synchronously.
103      */
104     @ShellMainThread
105     public interface OnDisplayChangingListener {
106         /**
107          * Called before the display is rotated. Contents of this method must run synchronously.
108          * @param displayId Id of display that is rotating.
109          * @param fromRotation starting rotation of the display.
110          * @param toRotation target rotation of the display (after rotating).
111          * @param t A task transaction to populate.
112          */
onRotateDisplay(int displayId, int fromRotation, int toRotation, WindowContainerTransaction t)113         void onRotateDisplay(int displayId, int fromRotation, int toRotation,
114                 WindowContainerTransaction t);
115     }
116 }
117