1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.launcher3.util;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
import static com.android.launcher3.Utilities.dpiFromPx;
import static com.android.launcher3.util.Executors.MAIN_EXECUTOR;
import static com.android.launcher3.util.Executors.UI_HELPER_EXECUTOR;
import static com.android.launcher3.util.WindowManagerCompat.MIN_TABLET_WIDTH;
import static java.util.Collections.emptyMap;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ComponentCallbacks;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.graphics.Point;
import android.hardware.display.DisplayManager;
import android.hardware.display.DisplayManager.DisplayListener;
import android.os.Build;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.view.Display;
import androidx.annotation.AnyThread;
import androidx.annotation.UiThread;
import androidx.annotation.WorkerThread;
import com.android.launcher3.Utilities;
import com.android.launcher3.uioverrides.ApiWrapper;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
/**
* Utility class to cache properties of default display to avoid a system RPC on every call.
*/
@SuppressLint("NewApi")
public class DisplayController implements DisplayListener, ComponentCallbacks, SafeCloseable {
private static final String TAG = "DisplayController";
public static final MainThreadInitializedObject<DisplayController> INSTANCE =
new MainThreadInitializedObject<>(DisplayController::new);
public static final int CHANGE_ACTIVE_SCREEN = 1 << 0;
public static final int CHANGE_ROTATION = 1 << 1;
public static final int CHANGE_FRAME_DELAY = 1 << 2;
public static final int CHANGE_DENSITY = 1 << 3;
public static final int CHANGE_SUPPORTED_BOUNDS = 1 << 4;
public static final int CHANGE_ALL = CHANGE_ACTIVE_SCREEN | CHANGE_ROTATION
| CHANGE_FRAME_DELAY | CHANGE_DENSITY | CHANGE_SUPPORTED_BOUNDS;
private final Context mContext;
private final DisplayManager mDM;
// Null for SDK < S
private final Context mWindowContext;
// The callback in this listener updates DeviceProfile, which other listeners might depend on
private DisplayInfoChangeListener mPriorityListener;
private final ArrayList<DisplayInfoChangeListener> mListeners = new ArrayList<>();
private Info mInfo;
private boolean mDestroyed = false;
private DisplayController(Context context) {
mContext = context;
mDM = context.getSystemService(DisplayManager.class);
Display display = mDM.getDisplay(DEFAULT_DISPLAY);
if (Utilities.ATLEAST_S) {
mWindowContext = mContext.createWindowContext(display, TYPE_APPLICATION, null);
mWindowContext.registerComponentCallbacks(this);
} else {
mWindowContext = null;
SimpleBroadcastReceiver configChangeReceiver =
new SimpleBroadcastReceiver(this::onConfigChanged);
mContext.registerReceiver(configChangeReceiver,
new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED));
}
mInfo = new Info(getDisplayInfoContext(display), display,
getInternalDisplays(mDM), emptyMap());
mDM.registerDisplayListener(this, UI_HELPER_EXECUTOR.getHandler());
}
private static ArrayMap<String, PortraitSize> getInternalDisplays(
DisplayManager displayManager) {
Display[] displays = displayManager.getDisplays();
ArrayMap<String, PortraitSize> internalDisplays = new ArrayMap<>();
for (Display display : displays) {
if (ApiWrapper.isInternalDisplay(display)) {
Point size = new Point();
display.getRealSize(size);
internalDisplays.put(ApiWrapper.getUniqueId(display),
new PortraitSize(size.x, size.y));
}
}
return internalDisplays;
}
@Override
public void close() {
mDestroyed = true;
if (mWindowContext != null) {
mWindowContext.unregisterComponentCallbacks(this);
} else {
// TODO: unregister broadcast receiver
}
mDM.unregisterDisplayListener(this);
}
@Override
public final void onDisplayAdded(int displayId) { }
@Override
public final void onDisplayRemoved(int displayId) { }
@WorkerThread
@Override
public final void onDisplayChanged(int displayId) {
if (displayId != DEFAULT_DISPLAY) {
return;
}
Display display = mDM.getDisplay(DEFAULT_DISPLAY);
if (display == null) {
return;
}
if (Utilities.ATLEAST_S) {
// Only check for refresh rate. Everything else comes from component callbacks
if (getSingleFrameMs(display) == mInfo.singleFrameMs) {
return;
}
}
handleInfoChange(display);
}
public static int getSingleFrameMs(Context context) {
return INSTANCE.get(context).getInfo().singleFrameMs;
}
/**
* Interface for listening for display changes
*/
public interface DisplayInfoChangeListener {
/**
* Invoked when display info has changed.
* @param context updated context associated with the display.
* @param info updated display information.
* @param flags bitmask indicating type of change.
*/
void onDisplayInfoChanged(Context context, Info info, int flags);
}
/**
* Only used for pre-S
*/
private void onConfigChanged(Intent intent) {
if (mDestroyed) {
return;
}
Configuration config = mContext.getResources().getConfiguration();
if (mInfo.fontScale != config.fontScale || mInfo.densityDpi != config.densityDpi) {
Log.d(TAG, "Configuration changed, notifying listeners");
Display display = mDM.getDisplay(DEFAULT_DISPLAY);
if (display != null) {
handleInfoChange(display);
}
}
}
@UiThread
@Override
@TargetApi(Build.VERSION_CODES.S)
public final void onConfigurationChanged(Configuration config) {
Display display = mWindowContext.getDisplay();
if (config.densityDpi != mInfo.densityDpi
|| config.fontScale != mInfo.fontScale
|| display.getRotation() != mInfo.rotation
|| !mInfo.mScreenSizeDp.equals(
new PortraitSize(config.screenHeightDp, config.screenWidthDp))) {
handleInfoChange(display);
}
}
@Override
public final void onLowMemory() { }
public void setPriorityListener(DisplayInfoChangeListener listener) {
mPriorityListener = listener;
}
public void addChangeListener(DisplayInfoChangeListener listener) {
mListeners.add(listener);
}
public void removeChangeListener(DisplayInfoChangeListener listener) {
mListeners.remove(listener);
}
public Info getInfo() {
return mInfo;
}
private Context getDisplayInfoContext(Display display) {
return Utilities.ATLEAST_S ? mWindowContext : mContext.createDisplayContext(display);
}
@AnyThread
private void handleInfoChange(Display display) {
Info oldInfo = mInfo;
Context displayContext = getDisplayInfoContext(display);
Info newInfo = new Info(displayContext, display,
oldInfo.mInternalDisplays, oldInfo.mPerDisplayBounds);
if (newInfo.densityDpi != oldInfo.densityDpi || newInfo.fontScale != oldInfo.fontScale) {
// Cache may not be valid anymore, recreate without cache
newInfo = new Info(displayContext, display, getInternalDisplays(mDM), emptyMap());
}
int change = 0;
if (!newInfo.displayId.equals(oldInfo.displayId)) {
change |= CHANGE_ACTIVE_SCREEN;
}
if (newInfo.rotation != oldInfo.rotation) {
change |= CHANGE_ROTATION;
}
if (newInfo.singleFrameMs != oldInfo.singleFrameMs) {
change |= CHANGE_FRAME_DELAY;
}
if (newInfo.densityDpi != oldInfo.densityDpi || newInfo.fontScale != oldInfo.fontScale) {
change |= CHANGE_DENSITY;
}
if (!newInfo.supportedBounds.equals(oldInfo.supportedBounds)) {
change |= CHANGE_SUPPORTED_BOUNDS;
PortraitSize realSize = new PortraitSize(newInfo.currentSize.x, newInfo.currentSize.y);
PortraitSize expectedSize = oldInfo.mInternalDisplays.get(
ApiWrapper.getUniqueId(display));
if (newInfo.supportedBounds.size() != oldInfo.supportedBounds.size()) {
Log.e("b/198965093",
"Inconsistent number of displays"
+ "\ndisplay state: " + display.getState()
+ "\noldInfo.supportedBounds: " + oldInfo.supportedBounds
+ "\nnewInfo.supportedBounds: " + newInfo.supportedBounds);
}
if (!realSize.equals(expectedSize) && display.getState() == Display.STATE_OFF) {
Log.e("b/198965093", "Display size changed while display is off, ignoring change");
return;
}
}
if (change != 0) {
mInfo = newInfo;
final int flags = change;
MAIN_EXECUTOR.execute(() -> notifyChange(displayContext, flags));
}
}
private void notifyChange(Context context, int flags) {
if (mPriorityListener != null) {
mPriorityListener.onDisplayInfoChanged(context, mInfo, flags);
}
for (int i = mListeners.size() - 1; i >= 0; i--) {
mListeners.get(i).onDisplayInfoChanged(context, mInfo, flags);
}
}
public static class Info {
public final int singleFrameMs;
// Configuration properties
public final int rotation;
public final float fontScale;
public final int densityDpi;
private final PortraitSize mScreenSizeDp;
public final Point currentSize;
public String displayId;
public final Set<WindowBounds> supportedBounds = new ArraySet<>();
private final Map<String, Set<WindowBounds>> mPerDisplayBounds = new ArrayMap<>();
private final ArrayMap<String, PortraitSize> mInternalDisplays;
public Info(Context context, Display display) {
this(context, display, new ArrayMap<>(), emptyMap());
}
private Info(Context context, Display display,
ArrayMap<String, PortraitSize> internalDisplays,
Map<String, Set<WindowBounds>> perDisplayBoundsCache) {
mInternalDisplays = internalDisplays;
rotation = display.getRotation();
Configuration config = context.getResources().getConfiguration();
fontScale = config.fontScale;
densityDpi = config.densityDpi;
mScreenSizeDp = new PortraitSize(config.screenHeightDp, config.screenWidthDp);
singleFrameMs = getSingleFrameMs(display);
currentSize = new Point();
display.getRealSize(currentSize);
displayId = ApiWrapper.getUniqueId(display);
Set<WindowBounds> currentSupportedBounds =
getSupportedBoundsForDisplay(display, currentSize);
mPerDisplayBounds.put(displayId, currentSupportedBounds);
supportedBounds.addAll(currentSupportedBounds);
if (ApiWrapper.isInternalDisplay(display) && internalDisplays.size() > 1) {
int displayCount = internalDisplays.size();
for (int i = 0; i < displayCount; i++) {
String displayKey = internalDisplays.keyAt(i);
if (TextUtils.equals(displayId, displayKey)) {
continue;
}
Set<WindowBounds> displayBounds = perDisplayBoundsCache.get(displayKey);
if (displayBounds == null) {
// We assume densityDpi is the same across all internal displays
displayBounds = WindowManagerCompat.estimateDisplayProfiles(
context, internalDisplays.valueAt(i), densityDpi,
ApiWrapper.TASKBAR_DRAWN_IN_PROCESS);
}
supportedBounds.addAll(displayBounds);
mPerDisplayBounds.put(displayKey, displayBounds);
}
}
}
private static Set<WindowBounds> getSupportedBoundsForDisplay(Display display, Point size) {
Point smallestSize = new Point();
Point largestSize = new Point();
display.getCurrentSizeRange(smallestSize, largestSize);
int portraitWidth = Math.min(size.x, size.y);
int portraitHeight = Math.max(size.x, size.y);
Set<WindowBounds> result = new ArraySet<>();
result.add(new WindowBounds(portraitWidth, portraitHeight,
smallestSize.x, largestSize.y));
result.add(new WindowBounds(portraitHeight, portraitWidth,
largestSize.x, smallestSize.y));
return result;
}
/**
* Returns true if the bounds represent a tablet
*/
public boolean isTablet(WindowBounds bounds) {
return dpiFromPx(Math.min(bounds.bounds.width(), bounds.bounds.height()),
densityDpi) >= MIN_TABLET_WIDTH;
}
}
/**
* Utility class to hold a size information in an orientation independent way
*/
public static class PortraitSize {
public final int width, height;
public PortraitSize(int w, int h) {
width = Math.min(w, h);
height = Math.max(w, h);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
PortraitSize that = (PortraitSize) o;
return width == that.width && height == that.height;
}
@Override
public int hashCode() {
return Objects.hash(width, height);
}
}
private static int getSingleFrameMs(Display display) {
float refreshRate = display.getRefreshRate();
return refreshRate > 0 ? (int) (1000 / refreshRate) : 16;
}
}
|