-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathDeviceUtils.java
More file actions
352 lines (317 loc) · 14.3 KB
/
DeviceUtils.java
File metadata and controls
352 lines (317 loc) · 14.3 KB
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
package org.schabi.newpipe.util;
import static android.content.Context.INPUT_SERVICE;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.UiModeManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.Point;
import android.hardware.input.InputManager;
import android.os.BatteryManager;
import android.os.Build;
import android.provider.Settings;
import android.util.TypedValue;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.WindowInsets;
import android.view.WindowManager;
import android.webkit.CookieManager;
import androidx.annotation.Dimension;
import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import org.schabi.newpipe.App;
import org.schabi.newpipe.R;
import java.lang.reflect.Method;
public final class DeviceUtils {
private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv";
private static final boolean SAMSUNG = Build.MANUFACTURER.equals("samsung");
private static Boolean isTV = null;
private static Boolean isFireTV = null;
/**
* <p>The app version code that corresponds to the last update
* of the media tunneling device blacklist.</p>
* <p>The value of this variable needs to be updated everytime a new device that does not
* support media tunneling to match the <strong>upcoming</strong> version code.</p>
* @see #shouldSupportMediaTunneling()
*/
public static final int MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION = 994;
// region: devices not supporting media tunneling / media tunneling blacklist
/**
* <p>Formuler Z8 Pro, Z8, CC, Z Alpha, Z+ Neo.</p>
* <p>Blacklist reason: black screen</p>
* <p>Board: HiSilicon Hi3798MV200</p>
*/
private static final boolean HI3798MV200 = Build.VERSION.SDK_INT == 24
&& Build.DEVICE.equals("Hi3798MV200");
/**
* <p>Zephir TS43UHD-2.</p>
* <p>Blacklist reason: black screen</p>
*/
private static final boolean CVT_MT5886_EU_1G = Build.VERSION.SDK_INT == 24
&& Build.DEVICE.equals("cvt_mt5886_eu_1g");
/**
* Hilife TV.
* <p>Blacklist reason: black screen</p>
*/
private static final boolean REALTEKATV = Build.VERSION.SDK_INT == 25
&& Build.DEVICE.equals("RealtekATV");
/**
* <p>Phillips 4K (O)LED TV.</p>
* Supports custom ROMs with different API levels
*/
private static final boolean PH7M_EU_5596 = Build.VERSION.SDK_INT >= 26
&& Build.DEVICE.equals("PH7M_EU_5596");
/**
* <p>Philips QM16XE.</p>
* <p>Blacklist reason: black screen</p>
*/
private static final boolean QM16XE_U = Build.VERSION.SDK_INT == 23
&& Build.DEVICE.equals("QM16XE_U");
/**
* <p>Sony Bravia VH1.</p>
* <p>Processor: MT5895</p>
* <p>Blacklist reason: fullscreen crash / stuttering</p>
*/
private static final boolean BRAVIA_VH1 = Build.VERSION.SDK_INT == 29
&& Build.DEVICE.equals("BRAVIA_VH1");
/**
* <p>Sony Bravia VH2.</p>
* <p>Blacklist reason: fullscreen crash; this includes model A90J as reported in
* <a href="https://github.com/TeamNewPipe/NewPipe/issues/9023#issuecomment-1387106242">
* #9023</a></p>
*/
private static final boolean BRAVIA_VH2 = Build.VERSION.SDK_INT == 29
&& Build.DEVICE.equals("BRAVIA_VH2");
/**
* <p>Sony Bravia Android TV platform 2.</p>
* Uses a MediaTek MT5891 (MT5596) SoC.
* @see <a href="https://github.com/CiNcH83/bravia_atv2">
* https://github.com/CiNcH83/bravia_atv2</a>
*/
private static final boolean BRAVIA_ATV2 = Build.DEVICE.equals("BRAVIA_ATV2");
/**
* <p>Sony Bravia Android TV platform 3 4K.</p>
* <p>Uses ARM MT5891 and a {@link #BRAVIA_ATV2} motherboard.</p>
*
* @see <a href="https://browser.geekbench.com/v4/cpu/9101105">
* https://browser.geekbench.com/v4/cpu/9101105</a>
*/
private static final boolean BRAVIA_ATV3_4K = Build.DEVICE.equals("BRAVIA_ATV3_4K");
/**
* <p>Panasonic 4KTV-JUP.</p>
* <p>Blacklist reason: fullscreen crash</p>
*/
private static final boolean TX_50JXW834 = Build.DEVICE.equals("TX_50JXW834");
/**
* <p>Bouygtel4K / Bouygues Telecom Bbox 4K.</p>
* <p>Blacklist reason: black screen; reported at
* <a href="https://github.com/TeamNewPipe/NewPipe/pull/10122#issuecomment-1638475769">
* #10122</a></p>
*/
private static final boolean HMB9213NW = Build.DEVICE.equals("HMB9213NW");
// endregion
private DeviceUtils() {
}
public static boolean isFireTv() {
if (isFireTV != null) {
return isFireTV;
}
isFireTV =
App.getInstance().getPackageManager().hasSystemFeature(AMAZON_FEATURE_FIRE_TV);
return isFireTV;
}
public static boolean isTv(final Context context) {
if (isTV != null) {
return isTV;
}
final PackageManager pm = App.getInstance().getPackageManager();
// from doc: https://developer.android.com/training/tv/start/hardware.html#runtime-check
boolean isTv = ContextCompat.getSystemService(context, UiModeManager.class)
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION
|| isFireTv()
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
// from https://stackoverflow.com/a/58932366
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
final boolean isBatteryAbsent = context.getSystemService(BatteryManager.class)
.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) == 0;
isTv = isTv || (isBatteryAbsent
&& !pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
&& pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
&& pm.hasSystemFeature(PackageManager.FEATURE_ETHERNET));
}
DeviceUtils.isTV = isTv;
return DeviceUtils.isTV;
}
/**
* Checks if the device is in desktop or DeX mode. This function should only
* be invoked once on view load as it is using reflection for the DeX checks.
* @param context the context to use for services and config.
* @return true if the Android device is in desktop mode or using DeX.
*/
@SuppressWarnings("JavaReflectionMemberAccess")
public static boolean isDesktopMode(@NonNull final Context context) {
// Adapted from https://stackoverflow.com/a/64615568
// to check for all input devices that have an active cursor
final InputManager im = (InputManager) context.getSystemService(INPUT_SERVICE);
for (final int id : im.getInputDeviceIds()) {
final InputDevice inputDevice = im.getInputDevice(id);
if (inputDevice.supportsSource(InputDevice.SOURCE_BLUETOOTH_STYLUS)
|| inputDevice.supportsSource(InputDevice.SOURCE_MOUSE)
|| inputDevice.supportsSource(InputDevice.SOURCE_STYLUS)
|| inputDevice.supportsSource(InputDevice.SOURCE_TOUCHPAD)
|| inputDevice.supportsSource(InputDevice.SOURCE_TRACKBALL)) {
return true;
}
}
final UiModeManager uiModeManager =
ContextCompat.getSystemService(context, UiModeManager.class);
if (uiModeManager != null
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_DESK) {
return true;
}
if (!SAMSUNG) {
return false;
// DeX is Samsung-specific, skip the checks below on non-Samsung devices
}
// DeX check for standalone and multi-window mode, from:
// https://developer.samsung.com/samsung-dex/modify-optimizing.html
try {
final Configuration config = context.getResources().getConfiguration();
final Class<?> configClass = config.getClass();
final int semDesktopModeEnabledConst =
configClass.getField("SEM_DESKTOP_MODE_ENABLED").getInt(configClass);
final int currentMode =
configClass.getField("semDesktopModeEnabled").getInt(config);
if (semDesktopModeEnabledConst == currentMode) {
return true;
}
} catch (final NoSuchFieldException | IllegalAccessException ignored) {
// Device doesn't seem to support DeX
}
@SuppressLint("WrongConstant") final Object desktopModeManager = context
.getApplicationContext()
.getSystemService("desktopmode");
if (desktopModeManager != null) {
try {
final Method getDesktopModeStateMethod = desktopModeManager.getClass()
.getDeclaredMethod("getDesktopModeState");
final Object desktopModeState = getDesktopModeStateMethod
.invoke(desktopModeManager);
final Class<?> desktopModeStateClass = desktopModeState.getClass();
final Method getEnabledMethod = desktopModeStateClass
.getDeclaredMethod("getEnabled");
final int enabledStatus = (int) getEnabledMethod.invoke(desktopModeState);
if (enabledStatus == desktopModeStateClass
.getDeclaredField("ENABLED").getInt(desktopModeStateClass)) {
return true;
}
} catch (final Exception ignored) {
// Device does not support DeX 3.0 or something went wrong when trying to determine
// if it supports this feature
}
}
return false;
}
public static boolean isTablet(@NonNull final Context context) {
final String tabletModeSetting = PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(R.string.tablet_mode_key), "");
if (tabletModeSetting.equals(context.getString(R.string.tablet_mode_on_key))) {
return true;
} else if (tabletModeSetting.equals(context.getString(R.string.tablet_mode_off_key))) {
return false;
}
// else automatically determine whether we are in a tablet or not
return (context.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public static boolean isConfirmKey(final int keyCode) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_CENTER:
case KeyEvent.KEYCODE_ENTER:
case KeyEvent.KEYCODE_SPACE:
case KeyEvent.KEYCODE_NUMPAD_ENTER:
return true;
default:
return false;
}
}
public static int dpToPx(@Dimension(unit = Dimension.DP) final int dp,
@NonNull final Context context) {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
context.getResources().getDisplayMetrics());
}
public static int spToPx(@Dimension(unit = Dimension.SP) final int sp,
@NonNull final Context context) {
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_SP,
sp,
context.getResources().getDisplayMetrics());
}
public static boolean isLandscape(final Context context) {
return context.getResources().getDisplayMetrics().heightPixels < context.getResources()
.getDisplayMetrics().widthPixels;
}
public static boolean isInMultiWindow(final Activity activity) {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInMultiWindowMode();
}
public static boolean hasAnimationsAnimatorDurationEnabled(final Context context) {
return Settings.System.getFloat(
context.getContentResolver(),
Settings.Global.ANIMATOR_DURATION_SCALE,
1F) != 0F;
}
public static int getWindowHeight(@NonNull final WindowManager windowManager) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
final var windowMetrics = windowManager.getCurrentWindowMetrics();
final var windowInsets = windowMetrics.getWindowInsets();
final var insets = windowInsets.getInsetsIgnoringVisibility(
WindowInsets.Type.navigationBars() | WindowInsets.Type.displayCutout());
return windowMetrics.getBounds().height() - (insets.top + insets.bottom);
} else {
final Point point = new Point();
windowManager.getDefaultDisplay().getSize(point);
return point.y;
}
}
/**
* <p>Some devices have broken tunneled video playback but claim to support it.</p>
* <p>This can cause a black video player surface while attempting to play a video or
* crashes while entering or exiting the full screen player.
* The issue effects Android TVs most commonly.
* See <a href="https://github.com/TeamNewPipe/NewPipe/issues/5911">#5911</a> and
* <a href="https://github.com/TeamNewPipe/NewPipe/issues/9023">#9023</a> for more info.</p>
* @Note Update {@link #MEDIA_TUNNELING_DEVICE_BLACKLIST_VERSION}
* when adding a new device to the method.
* @return {@code false} if affected device; {@code true} otherwise
*/
public static boolean shouldSupportMediaTunneling() {
// Maintainers note: update MEDIA_TUNNELING_DEVICES_UPDATE_APP_VERSION_CODE
return !HI3798MV200
&& !CVT_MT5886_EU_1G
&& !REALTEKATV
&& !QM16XE_U
&& !BRAVIA_VH1
&& !BRAVIA_VH2
&& !BRAVIA_ATV2
&& !BRAVIA_ATV3_4K
&& !PH7M_EU_5596
&& !TX_50JXW834
&& !HMB9213NW;
}
/**
* @return whether the device has support for WebView, see
* <a href="https://stackoverflow.com/a/69626735">https://stackoverflow.com/a/69626735</a>
*/
public static boolean supportsWebView() {
try {
CookieManager.getInstance();
return true;
} catch (final Throwable ignored) {
return false;
}
}
}