Skip to content

Commit

Permalink
Better detection of TV devices
Browse files Browse the repository at this point in the history
Some devices were not detected as TV even though they are
  • Loading branch information
B0pol committed Apr 25, 2020
1 parent b0c0249 commit 00262b4
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 19 deletions.
6 changes: 3 additions & 3 deletions app/src/main/java/org/schabi/newpipe/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@
import org.schabi.newpipe.fragments.detail.VideoDetailFragment;
import org.schabi.newpipe.fragments.list.search.SearchFragment;
import org.schabi.newpipe.report.ErrorActivity;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.AndroidTvUtils;
import org.schabi.newpipe.util.Constants;
import org.schabi.newpipe.util.KioskTranslator;
import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper;
Expand Down Expand Up @@ -145,7 +145,7 @@ && getSupportFragmentManager().getBackStackEntryCount() == 0) {
ErrorActivity.reportUiError(this, e);
}

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this);
}
}
Expand Down Expand Up @@ -532,7 +532,7 @@ public void onBackPressed() {
Log.d(TAG, "onBackPressed() called");
}

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
View drawerPanel = findViewById(R.id.navigation);
if (drawer.isDrawerOpen(drawerPanel)) {
drawer.closeDrawers();
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/org/schabi/newpipe/RouterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private void showDialog(final List<AdapterChoiceItem> choices) {

alertDialog.show();

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(alertDialog);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public void onGlobalLayout() {
}
});

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {

thumbnailBackgroundButton.requestFocus();

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(getContext())) {
// remove ripple effects from detail controls
final int transparent = getResources().getColor(R.color.transparent_background_color);
detailControlsAddToPlaylist.setBackgroundColor(transparent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ private void initSearchListeners() {
if (isSuggestionsEnabled && errorPanelRoot.getVisibility() != View.VISIBLE) {
showSuggestionsPanel();
}
if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(getContext())) {
showKeyboardSearch();
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void updateFromItem(final InfoItem infoItem,


itemView.setOnLongClickListener(view -> {
if (!AndroidTvUtils.isTv()) {
if (!AndroidTvUtils.isTv(itemBuilder.getContext())) {
ClipboardManager clipboardManager = (ClipboardManager) itemBuilder.getContext()
.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setPrimaryClip(ClipData.newPlainText(null, commentText));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void onChange(final boolean selfChange) {
final String orientKey = getString(R.string.last_orientation_landscape_key);

final boolean lastOrientationWasLandscape = defaultPreferences
.getBoolean(orientKey, AndroidTvUtils.isTv());
.getBoolean(orientKey, AndroidTvUtils.isTv(getApplicationContext()));
setLandscape(lastOrientationWasLandscape);
} else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Expand All @@ -191,7 +191,7 @@ public void onChange(final boolean selfChange) {
Settings.System.getUriFor(Settings.System.ACCELEROMETER_ROTATION),
false, rotationObserver);

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this);
}
}
Expand Down Expand Up @@ -223,7 +223,8 @@ public boolean onKeyDown(final int keyCode, final KeyEvent event) {
default:
break;
case KeyEvent.KEYCODE_BACK:
if (AndroidTvUtils.isTv() && playerImpl.isControlsVisible()) {
if (AndroidTvUtils.isTv(getApplicationContext())
&& playerImpl.isControlsVisible()) {
playerImpl.hideControls(0, 0);
hideSystemUi();
return true;
Expand Down Expand Up @@ -271,7 +272,7 @@ protected void onResume() {
final String orientKey = getString(R.string.last_orientation_landscape_key);

boolean lastOrientationWasLandscape = defaultPreferences
.getBoolean(orientKey, AndroidTvUtils.isTv());
.getBoolean(orientKey, AndroidTvUtils.isTv(getApplicationContext()));
setLandscape(lastOrientationWasLandscape);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected void onCreate(final Bundle savedInstanceBundle) {
.commit();
}

if (AndroidTvUtils.isTv()) {
if (AndroidTvUtils.isTv(this)) {
FocusOverlayView.setupFocusObserver(this);
}
}
Expand Down
35 changes: 29 additions & 6 deletions app/src/main/java/org/schabi/newpipe/util/AndroidTvUtils.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
package org.schabi.newpipe.util;

import android.annotation.SuppressLint;
import android.app.UiModeManager;
import android.content.Context;
import android.content.pm.PackageManager;

import android.content.res.Configuration;
import android.os.BatteryManager;
import android.view.KeyEvent;

import org.schabi.newpipe.App;

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 AndroidTvUtils() { }
private AndroidTvUtils() {
}

@SuppressLint("InlinedApi")
public static boolean isTv() {
PackageManager pm = App.getApp().getPackageManager();
public static boolean isTv(final Context context) {
PackageManager pm = App.getApp().getPackageManager();


// 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)

return pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV)
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
//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) {
Expand Down

0 comments on commit 00262b4

Please sign in to comment.