Better detection of TV devices

Some devices were not detected as TV even though they are
This commit is contained in:
bopol 2020-04-25 11:35:15 +02:00
parent b0c0249ce6
commit 00262b4a49
9 changed files with 43 additions and 19 deletions

View file

@ -64,8 +64,8 @@ import org.schabi.newpipe.fragments.MainFragment;
import org.schabi.newpipe.fragments.detail.VideoDetailFragment; import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.fragments.list.search.SearchFragment; import org.schabi.newpipe.fragments.list.search.SearchFragment;
import org.schabi.newpipe.report.ErrorActivity; import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.AndroidTvUtils; import org.schabi.newpipe.util.AndroidTvUtils;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.KioskTranslator; import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.Localization; import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.NavigationHelper;
@ -145,7 +145,7 @@ public class MainActivity extends AppCompatActivity {
ErrorActivity.reportUiError(this, e); ErrorActivity.reportUiError(this, e);
} }
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this); FocusOverlayView.setupFocusObserver(this);
} }
} }
@ -532,7 +532,7 @@ public class MainActivity extends AppCompatActivity {
Log.d(TAG, "onBackPressed() called"); Log.d(TAG, "onBackPressed() called");
} }
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
View drawerPanel = findViewById(R.id.navigation); View drawerPanel = findViewById(R.id.navigation);
if (drawer.isDrawerOpen(drawerPanel)) { if (drawer.isDrawerOpen(drawerPanel)) {
drawer.closeDrawers(); drawer.closeDrawers();

View file

@ -344,7 +344,7 @@ public class RouterActivity extends AppCompatActivity {
alertDialog.show(); alertDialog.show();
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(alertDialog); FocusOverlayView.setupFocusObserver(alertDialog);
} }
} }

View file

@ -57,7 +57,7 @@ public class DownloadActivity extends AppCompatActivity {
} }
}); });
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this); FocusOverlayView.setupFocusObserver(this);
} }
} }

View file

@ -539,7 +539,7 @@ public class VideoDetailFragment extends BaseStateFragment<StreamInfo>
thumbnailBackgroundButton.requestFocus(); thumbnailBackgroundButton.requestFocus();
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(getContext())) {
// remove ripple effects from detail controls // remove ripple effects from detail controls
final int transparent = getResources().getColor(R.color.transparent_background_color); final int transparent = getResources().getColor(R.color.transparent_background_color);
detailControlsAddToPlaylist.setBackgroundColor(transparent); detailControlsAddToPlaylist.setBackgroundColor(transparent);

View file

@ -511,7 +511,7 @@ public class SearchFragment extends BaseListFragment<SearchInfo, ListExtractor.I
if (isSuggestionsEnabled && errorPanelRoot.getVisibility() != View.VISIBLE) { if (isSuggestionsEnabled && errorPanelRoot.getVisibility() != View.VISIBLE) {
showSuggestionsPanel(); showSuggestionsPanel();
} }
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(getContext())) {
showKeyboardSearch(); showKeyboardSearch();
} }
}); });

View file

@ -129,7 +129,7 @@ public class CommentsMiniInfoItemHolder extends InfoItemHolder {
itemView.setOnLongClickListener(view -> { itemView.setOnLongClickListener(view -> {
if (!AndroidTvUtils.isTv()) { if (!AndroidTvUtils.isTv(itemBuilder.getContext())) {
ClipboardManager clipboardManager = (ClipboardManager) itemBuilder.getContext() ClipboardManager clipboardManager = (ClipboardManager) itemBuilder.getContext()
.getSystemService(Context.CLIPBOARD_SERVICE); .getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, commentText)); clipboardManager.setPrimaryClip(ClipData.newPlainText(null, commentText));

View file

@ -179,7 +179,7 @@ public final class MainVideoPlayer extends AppCompatActivity
final String orientKey = getString(R.string.last_orientation_landscape_key); final String orientKey = getString(R.string.last_orientation_landscape_key);
final boolean lastOrientationWasLandscape = defaultPreferences final boolean lastOrientationWasLandscape = defaultPreferences
.getBoolean(orientKey, AndroidTvUtils.isTv()); .getBoolean(orientKey, AndroidTvUtils.isTv(getApplicationContext()));
setLandscape(lastOrientationWasLandscape); setLandscape(lastOrientationWasLandscape);
} else { } else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
@ -191,7 +191,7 @@ public final class MainVideoPlayer extends AppCompatActivity
Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION), Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
false, rotationObserver); false, rotationObserver);
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this); FocusOverlayView.setupFocusObserver(this);
} }
} }
@ -223,7 +223,8 @@ public final class MainVideoPlayer extends AppCompatActivity
default: default:
break; break;
case KeyEvent.KEYCODE_BACK: case KeyEvent.KEYCODE_BACK:
if (AndroidTvUtils.isTv() && playerImpl.isControlsVisible()) { if (AndroidTvUtils.isTv(getApplicationContext())
&& playerImpl.isControlsVisible()) {
playerImpl.hideControls(0, 0); playerImpl.hideControls(0, 0);
hideSystemUi(); hideSystemUi();
return true; return true;
@ -271,7 +272,7 @@ public final class MainVideoPlayer extends AppCompatActivity
final String orientKey = getString(R.string.last_orientation_landscape_key); final String orientKey = getString(R.string.last_orientation_landscape_key);
boolean lastOrientationWasLandscape = defaultPreferences boolean lastOrientationWasLandscape = defaultPreferences
.getBoolean(orientKey, AndroidTvUtils.isTv()); .getBoolean(orientKey, AndroidTvUtils.isTv(getApplicationContext()));
setLandscape(lastOrientationWasLandscape); setLandscape(lastOrientationWasLandscape);
} }

View file

@ -62,7 +62,7 @@ public class SettingsActivity extends AppCompatActivity
.commit(); .commit();
} }
if (AndroidTvUtils.isTv()) { if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this); FocusOverlayView.setupFocusObserver(this);
} }
} }

View file

@ -1,22 +1,45 @@
package org.schabi.newpipe.util; package org.schabi.newpipe.util;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.UiModeManager;
import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.os.BatteryManager;
import android.view.KeyEvent; import android.view.KeyEvent;
import org.schabi.newpipe.App; import org.schabi.newpipe.App;
import static android.content.Context.BATTERY_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 AndroidTvUtils() { } private AndroidTvUtils() {
}
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
public static boolean isTv() { public static boolean isTv(final Context context) {
PackageManager pm = App.getApp().getPackageManager(); PackageManager pm = App.getApp().getPackageManager();
return pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV)
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK); // from doc: https://developer.android.com/training/tv/start/hardware.html#runtime-check
boolean isAndroidTv = ((UiModeManager) context.getSystemService(UI_MODE_SERVICE))
.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
boolean isBatteryAbsent = ((BatteryManager) context.getSystemService(BATTERY_SERVICE))
.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY) == 0;
return isAndroidTv
|| pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV)
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK)
|| pm.hasSystemFeature(PackageManager.FEATURE_TELEVISION)
//from https://stackoverflow.com/a/58932366
|| (isBatteryAbsent && !pm.hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN)
&& pm.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
&& pm.hasSystemFeature(PackageManager.FEATURE_ETHERNET));
} }
public static boolean isConfirmKey(final int keyCode) { public static boolean isConfirmKey(final int keyCode) {