1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2024-12-25 09:30:32 +00:00

store isTv value to prevent calculating it again and again

This commit is contained in:
bopol 2020-04-26 00:36:45 +02:00
parent f62f00b4ad
commit 77597b329e

View File

@ -14,17 +14,25 @@ import static android.content.Context.BATTERY_SERVICE;
import static android.content.Context.UI_MODE_SERVICE; import static android.content.Context.UI_MODE_SERVICE;
public final class AndroidTvUtils { public final class AndroidTvUtils {
private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv"; private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv";
private static Boolean isTV = null;
private AndroidTvUtils() { private AndroidTvUtils() {
} }
public static boolean isTv(final Context context) { public static boolean isTv(final Context context) {
if (AndroidTvUtils.isTV != null) {
return AndroidTvUtils.isTV;
}
PackageManager pm = App.getApp().getPackageManager(); PackageManager pm = App.getApp().getPackageManager();
// from doc: https://developer.android.com/training/tv/start/hardware.html#runtime-check // from doc: https://developer.android.com/training/tv/start/hardware.html#runtime-check
boolean isTv = ((UiModeManager) context.getSystemService(UI_MODE_SERVICE)) boolean isTv = ((UiModeManager) context.getSystemService(UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION; .getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION
|| pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV)
|| pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION);
// from https://stackoverflow.com/a/58932366 // from https://stackoverflow.com/a/58932366
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
@ -40,9 +48,8 @@ public final class AndroidTvUtils {
isTv = isTv || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK); isTv = isTv || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
} }
return isTv AndroidTvUtils.isTV = isTv;
|| pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV) return AndroidTvUtils.isTV;
|| pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION);
} }
public static boolean isConfirmKey(final int keyCode) { public static boolean isConfirmKey(final int keyCode) {