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