1 /*
2  * Copyright (C) 2023 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.server.wm;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.view.Display;
22 import android.view.Surface;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 /**
27  * Singleton for coordinating rotation across multiple displays. Used to notify non-default
28  * displays when the default display rotates.
29  *
30  * Note that this class does not need locking because it is always protected by WindowManagerService
31  * mGlobalLock.
32  */
33 class DisplayRotationCoordinator {
34 
35     private static final String TAG = "DisplayRotationCoordinator";
36 
37     @Surface.Rotation
38     private int mDefaultDisplayDefaultRotation;
39 
40     @Nullable
41     @VisibleForTesting
42     Runnable mDefaultDisplayRotationChangedCallback;
43 
44     @Surface.Rotation
45     private int mDefaultDisplayCurrentRotation;
46 
47     /**
48      * Notifies clients when the default display rotation changes.
49      */
onDefaultDisplayRotationChanged(@urface.Rotation int rotation)50     void onDefaultDisplayRotationChanged(@Surface.Rotation int rotation) {
51         mDefaultDisplayCurrentRotation = rotation;
52 
53         if (mDefaultDisplayRotationChangedCallback != null) {
54             mDefaultDisplayRotationChangedCallback.run();
55         }
56     }
57 
setDefaultDisplayDefaultRotation(@urface.Rotation int rotation)58     void setDefaultDisplayDefaultRotation(@Surface.Rotation int rotation) {
59         mDefaultDisplayDefaultRotation = rotation;
60     }
61 
62     @Surface.Rotation
getDefaultDisplayCurrentRotation()63     int getDefaultDisplayCurrentRotation() {
64         return mDefaultDisplayCurrentRotation;
65     }
66 
67     /**
68      * Register a callback to be notified when the default display's rotation changes. Clients can
69      * query the default display's current rotation via {@link #getDefaultDisplayCurrentRotation()}.
70      */
setDefaultDisplayRotationChangedCallback(@onNull Runnable callback)71     void setDefaultDisplayRotationChangedCallback(@NonNull Runnable callback) {
72         if (mDefaultDisplayRotationChangedCallback != null) {
73             throw new UnsupportedOperationException("Multiple clients unsupported");
74         }
75 
76         mDefaultDisplayRotationChangedCallback = callback;
77 
78         if (mDefaultDisplayCurrentRotation != mDefaultDisplayDefaultRotation) {
79             callback.run();
80         }
81     }
82 
83     /**
84      * Removes the callback that was added via
85      * {@link #setDefaultDisplayRotationChangedCallback(Runnable)}.
86      */
removeDefaultDisplayRotationChangedCallback()87     void removeDefaultDisplayRotationChangedCallback() {
88         mDefaultDisplayRotationChangedCallback = null;
89     }
90 
isSecondaryInternalDisplay(@onNull DisplayContent displayContent)91     static boolean isSecondaryInternalDisplay(@NonNull DisplayContent displayContent) {
92         if (displayContent.isDefaultDisplay) {
93             return false;
94         } else if (displayContent.mDisplay == null) {
95             return false;
96         }
97         return displayContent.mDisplay.getType() == Display.TYPE_INTERNAL;
98     }
99 }
100