Use recommended way of checking for TV

Switch to the recommended way of checking whether the app is running on a TV
device.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=215177587
This commit is contained in:
andrewlewis 2018-10-01 01:50:07 -07:00 committed by Oliver Woodman
parent 5d5b641c1d
commit f59f557704
2 changed files with 19 additions and 15 deletions

View File

@ -711,7 +711,7 @@ public class MediaCodecAudioRenderer extends MediaCodecRenderer implements Media
// Android TV running M, so there's no point requesting a non-default input size. Doing so may // Android TV running M, so there's no point requesting a non-default input size. Doing so may
// cause a native crash, whereas not doing so will cause a more controlled failure when // cause a native crash, whereas not doing so will cause a more controlled failure when
// attempting to fill an input buffer. See: https://github.com/google/ExoPlayer/issues/4057. // attempting to fill an input buffer. See: https://github.com/google/ExoPlayer/issues/4057.
if (Util.SDK_INT < 24 && !(Util.SDK_INT == 23 && Util.isAndroidTv(context))) { if (Util.SDK_INT < 24 && !(Util.SDK_INT == 23 && Util.isTv(context))) {
return Format.NO_VALUE; return Format.NO_VALUE;
} }
} }

View File

@ -15,16 +15,20 @@
*/ */
package com.google.android.exoplayer2.util; package com.google.android.exoplayer2.util;
import static android.content.Context.UI_MODE_SERVICE;
import android.Manifest.permission; import android.Manifest.permission;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.app.UiModeManager;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Configuration;
import android.graphics.Point; import android.graphics.Point;
import android.media.AudioFormat; import android.media.AudioFormat;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
@ -173,7 +177,7 @@ public final class Util {
return false; return false;
} }
for (Uri uri : uris) { for (Uri uri : uris) {
if (Util.isLocalFileUri(uri)) { if (isLocalFileUri(uri)) {
if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE) if (activity.checkSelfPermission(permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) { != PackageManager.PERMISSION_GRANTED) {
activity.requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE}, 0); activity.requestPermissions(new String[] {permission.READ_EXTERNAL_STORAGE}, 0);
@ -242,7 +246,7 @@ public final class Util {
*/ */
public static boolean contains(Object[] items, Object item) { public static boolean contains(Object[] items, Object item) {
for (Object arrayItem : items) { for (Object arrayItem : items) {
if (Util.areEqual(arrayItem, item)) { if (areEqual(arrayItem, item)) {
return true; return true;
} }
} }
@ -1352,7 +1356,7 @@ public final class Util {
* @return The derived {@link UUID}, or {@code null} if one could not be derived. * @return The derived {@link UUID}, or {@code null} if one could not be derived.
*/ */
public static @Nullable UUID getDrmUuid(String drmScheme) { public static @Nullable UUID getDrmUuid(String drmScheme) {
switch (Util.toLowerInvariant(drmScheme)) { switch (toLowerInvariant(drmScheme)) {
case "widevine": case "widevine":
return C.WIDEVINE_UUID; return C.WIDEVINE_UUID;
case "playready": case "playready":
@ -1402,7 +1406,7 @@ public final class Util {
*/ */
@C.ContentType @C.ContentType
public static int inferContentType(String fileName) { public static int inferContentType(String fileName) {
fileName = Util.toLowerInvariant(fileName); fileName = toLowerInvariant(fileName);
if (fileName.endsWith(".mpd")) { if (fileName.endsWith(".mpd")) {
return C.TYPE_DASH; return C.TYPE_DASH;
} else if (fileName.endsWith(".m3u8")) { } else if (fileName.endsWith(".m3u8")) {
@ -1565,7 +1569,7 @@ public final class Util {
* and is not declared to be thrown. * and is not declared to be thrown.
*/ */
public static void sneakyThrow(Throwable t) { public static void sneakyThrow(Throwable t) {
Util.sneakyThrowInternal(t); sneakyThrowInternal(t);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@ -1729,15 +1733,17 @@ public final class Util {
} }
/** /**
* Returns whether the device is an Android TV. * Returns whether the app is running on a TV device.
* *
* @param context Any context. * @param context Any context.
* @return Whether the device is an Android TV. * @return Whether the app is running on a TV device.
*/ */
public static boolean isAndroidTv(Context context) { public static boolean isTv(Context context) {
PackageManager packageManager = context.getPackageManager(); // See https://developer.android.com/training/tv/start/hardware.html#runtime-check.
return packageManager != null UiModeManager uiModeManager =
&& packageManager.hasSystemFeature(PackageManager.FEATURE_LEANBACK); (UiModeManager) context.getApplicationContext().getSystemService(UI_MODE_SERVICE);
return uiModeManager != null
&& uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION;
} }
/** /**
@ -1759,9 +1765,7 @@ public final class Util {
* @return The physical display size, in pixels. * @return The physical display size, in pixels.
*/ */
public static Point getPhysicalDisplaySize(Context context, Display display) { public static Point getPhysicalDisplaySize(Context context, Display display) {
if (Util.SDK_INT <= 28 if (Util.SDK_INT <= 28 && display.getDisplayId() == Display.DEFAULT_DISPLAY && isTv(context)) {
&& display.getDisplayId() == Display.DEFAULT_DISPLAY
&& Util.isAndroidTv(context)) {
// On Android TVs it is common for the UI to be configured for a lower resolution than // On Android TVs it is common for the UI to be configured for a lower resolution than
// SurfaceViews can output. Before API 26 the Display object does not provide a way to // SurfaceViews can output. Before API 26 the Display object does not provide a way to
// identify this case, and up to and including API 28 many devices still do not correctly set // identify this case, and up to and including API 28 many devices still do not correctly set