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.systemui.wm; 18 19 import android.os.Handler; 20 import android.os.RemoteException; 21 import android.view.IDisplayWindowRotationCallback; 22 import android.view.IDisplayWindowRotationController; 23 import android.view.IWindowManager; 24 import android.window.WindowContainerTransaction; 25 26 import 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 */ 34 public 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 DisplayChangeController(Handler mainHandler, IWindowManager wmService)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 */ addRotationListener(OnDisplayChangingListener listener)79 public void addRotationListener(OnDisplayChangingListener listener) { 80 synchronized (mRotationListener) { 81 mRotationListener.add(listener); 82 } 83 } 84 85 /** 86 * Removes a display rotation controller. 87 */ removeRotationListener(OnDisplayChangingListener listener)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 */ onRotateDisplay(int displayId, int fromRotation, int toRotation, WindowContainerTransaction t)106 void onRotateDisplay(int displayId, int fromRotation, int toRotation, 107 WindowContainerTransaction t); 108 } 109 } 110