From 77597b329e200fdceff3ac7fc5398f60d3d1e77b Mon Sep 17 00:00:00 2001 From: bopol Date: Sun, 26 Apr 2020 00:36:45 +0200 Subject: [PATCH] store isTv value to prevent calculating it again and again --- .../org/schabi/newpipe/util/AndroidTvUtils.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/util/AndroidTvUtils.java b/app/src/main/java/org/schabi/newpipe/util/AndroidTvUtils.java index 8b90a2424..db2ab4aa7 100644 --- a/app/src/main/java/org/schabi/newpipe/util/AndroidTvUtils.java +++ b/app/src/main/java/org/schabi/newpipe/util/AndroidTvUtils.java @@ -14,17 +14,25 @@ import static android.content.Context.BATTERY_SERVICE; import static android.content.Context.UI_MODE_SERVICE; public final class AndroidTvUtils { + private static final String AMAZON_FEATURE_FIRE_TV = "amazon.hardware.fire_tv"; + private static Boolean isTV = null; private AndroidTvUtils() { } public static boolean isTv(final Context context) { + if (AndroidTvUtils.isTV != null) { + return AndroidTvUtils.isTV; + } + PackageManager pm = App.getApp().getPackageManager(); // from doc: https://developer.android.com/training/tv/start/hardware.html#runtime-check 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 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { @@ -40,9 +48,8 @@ public final class AndroidTvUtils { isTv = isTv || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK); } - return isTv - || pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV) - || pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION); + AndroidTvUtils.isTV = isTv; + return AndroidTvUtils.isTV; } public static boolean isConfirmKey(final int keyCode) {