Merge pull request #3483 from B0pol/tv

Better detection of TV devices
This commit is contained in:
Tobias Groza 2020-04-26 12:55:13 +02:00 committed by GitHub
commit 4ea273b297
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 55 additions and 21 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,55 @@
package org.schabi.newpipe.util; 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.pm.PackageManager;
import android.content.res.Configuration;
import android.os.BatteryManager;
import android.os.Build;
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 static Boolean isTV = null;
private AndroidTvUtils() { } private AndroidTvUtils() {
}
@SuppressLint("InlinedApi") public static boolean isTv(final Context context) {
public static boolean isTv() { if (AndroidTvUtils.isTV != null) {
PackageManager pm = App.getApp().getPackageManager(); return AndroidTvUtils.isTV;
}
return pm.hasSystemFeature(AMAZON_FEATURE_FIRE_TV) PackageManager pm = App.getApp().getPackageManager();
|| pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
// 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
|| 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) {
boolean isBatteryAbsent = ((BatteryManager) context.getSystemService(BATTERY_SERVICE))
.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));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
isTv = isTv || pm.hasSystemFeature(PackageManager.FEATURE_LEANBACK);
}
AndroidTvUtils.isTV = isTv;
return AndroidTvUtils.isTV;
} }
public static boolean isConfirmKey(final int keyCode) { public static boolean isConfirmKey(final int keyCode) {