mirror of
https://github.com/samsonjs/media.git
synced 2026-04-16 13:05:46 +00:00
Make public util method to get physical display size
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=131810070
This commit is contained in:
parent
f0c0b3efbf
commit
d65feb682b
2 changed files with 88 additions and 86 deletions
|
|
@ -15,14 +15,9 @@
|
|||
*/
|
||||
package com.google.android.exoplayer2.trackselection;
|
||||
|
||||
import android.annotation.TargetApi;
|
||||
import android.content.Context;
|
||||
import android.graphics.Point;
|
||||
import android.os.Handler;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.WindowManager;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.ExoPlaybackException;
|
||||
import com.google.android.exoplayer2.Format;
|
||||
|
|
@ -30,7 +25,6 @@ import com.google.android.exoplayer2.RendererCapabilities;
|
|||
import com.google.android.exoplayer2.source.TrackGroup;
|
||||
import com.google.android.exoplayer2.source.TrackGroupArray;
|
||||
import com.google.android.exoplayer2.util.Util;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
|
@ -47,7 +41,6 @@ public class DefaultTrackSelector extends MappingTrackSelector {
|
|||
*/
|
||||
private static final float FRACTION_TO_CONSIDER_FULLSCREEN = 0.98f;
|
||||
private static final int[] NO_TRACKS = new int[0];
|
||||
private static final String TAG = "DefaultTrackSelector";
|
||||
|
||||
private final TrackSelection.Factory adaptiveVideoTrackSelectionFactory;
|
||||
|
||||
|
|
@ -216,7 +209,7 @@ public class DefaultTrackSelector extends MappingTrackSelector {
|
|||
* @param orientationMayChange Whether orientation may change during playback.
|
||||
*/
|
||||
public void setViewportSizeFromContext(Context context, boolean orientationMayChange) {
|
||||
Point viewportSize = getDisplaySize(context); // Assume the viewport is fullscreen.
|
||||
Point viewportSize = Util.getPhysicalDisplaySize(context); // Assume the viewport is fullscreen.
|
||||
setViewportSize(viewportSize.x, viewportSize.y, orientationMayChange);
|
||||
}
|
||||
|
||||
|
|
@ -635,79 +628,4 @@ public class DefaultTrackSelector extends MappingTrackSelector {
|
|||
}
|
||||
}
|
||||
|
||||
private static Point getDisplaySize(Context context) {
|
||||
// Before API 25 the platform Display object does not provide a working way to identify Android
|
||||
// TVs that can show 4k resolution in a SurfaceView, so check for supported devices here.
|
||||
if (Util.SDK_INT < 25) {
|
||||
if ("Sony".equals(Util.MANUFACTURER) && Util.MODEL.startsWith("BRAVIA")
|
||||
&& context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) {
|
||||
return new Point(3840, 2160);
|
||||
} else if ("NVIDIA".equals(Util.MANUFACTURER) && Util.MODEL != null
|
||||
&& Util.MODEL.contains("SHIELD")) {
|
||||
// Attempt to read sys.display-size.
|
||||
String sysDisplaySize = null;
|
||||
try {
|
||||
Class<?> systemProperties = Class.forName("android.os.SystemProperties");
|
||||
Method getMethod = systemProperties.getMethod("get", String.class);
|
||||
sysDisplaySize = (String) getMethod.invoke(systemProperties, "sys.display-size");
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to read sys.display-size", e);
|
||||
}
|
||||
// If we managed to read sys.display-size, attempt to parse it.
|
||||
if (!TextUtils.isEmpty(sysDisplaySize)) {
|
||||
try {
|
||||
String[] sysDisplaySizeParts = sysDisplaySize.trim().split("x");
|
||||
if (sysDisplaySizeParts.length == 2) {
|
||||
int width = Integer.parseInt(sysDisplaySizeParts[0]);
|
||||
int height = Integer.parseInt(sysDisplaySizeParts[1]);
|
||||
if (width > 0 && height > 0) {
|
||||
return new Point(width, height);
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// Do nothing.
|
||||
}
|
||||
Log.e(TAG, "Invalid sys.display-size: " + sysDisplaySize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = windowManager.getDefaultDisplay();
|
||||
Point displaySize = new Point();
|
||||
if (Util.SDK_INT >= 23) {
|
||||
getDisplaySizeV23(display, displaySize);
|
||||
} else if (Util.SDK_INT >= 17) {
|
||||
getDisplaySizeV17(display, displaySize);
|
||||
} else if (Util.SDK_INT >= 16) {
|
||||
getDisplaySizeV16(display, displaySize);
|
||||
} else {
|
||||
getDisplaySizeV9(display, displaySize);
|
||||
}
|
||||
return displaySize;
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private static void getDisplaySizeV23(Display display, Point outSize) {
|
||||
Display.Mode mode = display.getMode();
|
||||
outSize.x = mode.getPhysicalWidth();
|
||||
outSize.y = mode.getPhysicalHeight();
|
||||
}
|
||||
|
||||
@TargetApi(17)
|
||||
private static void getDisplaySizeV17(Display display, Point outSize) {
|
||||
display.getRealSize(outSize);
|
||||
}
|
||||
|
||||
@TargetApi(16)
|
||||
private static void getDisplaySizeV16(Display display, Point outSize) {
|
||||
display.getSize(outSize);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static void getDisplaySizeV9(Display display, Point outSize) {
|
||||
outSize.x = display.getWidth();
|
||||
outSize.y = display.getHeight();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,9 +22,13 @@ import android.content.Context;
|
|||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.graphics.Point;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.Display;
|
||||
import android.view.WindowManager;
|
||||
import com.google.android.exoplayer2.C;
|
||||
import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
|
||||
import com.google.android.exoplayer2.upstream.DataSource;
|
||||
|
|
@ -34,6 +38,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.MessageDigest;
|
||||
|
|
@ -104,17 +109,15 @@ public final class Util {
|
|||
*/
|
||||
public static final int TYPE_OTHER = 3;
|
||||
|
||||
private static final String TAG = "Util";
|
||||
private static final Pattern XS_DATE_TIME_PATTERN = Pattern.compile(
|
||||
"(\\d\\d\\d\\d)\\-(\\d\\d)\\-(\\d\\d)[Tt]"
|
||||
+ "(\\d\\d):(\\d\\d):(\\d\\d)(\\.(\\d+))?"
|
||||
+ "([Zz]|((\\+|\\-)(\\d\\d):(\\d\\d)))?");
|
||||
|
||||
private static final Pattern XS_DURATION_PATTERN =
|
||||
Pattern.compile("^(-)?P(([0-9]*)Y)?(([0-9]*)M)?(([0-9]*)D)?"
|
||||
+ "(T(([0-9]*)H)?(([0-9]*)M)?(([0-9.]*)S)?)?$");
|
||||
|
||||
private static final Pattern ESCAPED_CHARACTER_PATTERN = Pattern.compile("%([A-Fa-f0-9]{2})");
|
||||
|
||||
private static final char[] HEX_DIGITS = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
private Util() {}
|
||||
|
|
@ -901,6 +904,87 @@ public final class Util {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the physical size of the default display, in pixels.
|
||||
*
|
||||
* @param context Any context.
|
||||
* @return The physical display size, in pixels.
|
||||
*/
|
||||
public static Point getPhysicalDisplaySize(Context context) {
|
||||
// Before API 25 the platform Display object does not provide a working way to identify Android
|
||||
// TVs that can show 4k resolution in a SurfaceView, so check for supported devices here.
|
||||
if (Util.SDK_INT < 25) {
|
||||
if ("Sony".equals(Util.MANUFACTURER) && Util.MODEL.startsWith("BRAVIA")
|
||||
&& context.getPackageManager().hasSystemFeature("com.sony.dtv.hardware.panel.qfhd")) {
|
||||
return new Point(3840, 2160);
|
||||
} else if ("NVIDIA".equals(Util.MANUFACTURER) && Util.MODEL != null
|
||||
&& Util.MODEL.contains("SHIELD")) {
|
||||
// Attempt to read sys.display-size.
|
||||
String sysDisplaySize = null;
|
||||
try {
|
||||
Class<?> systemProperties = Class.forName("android.os.SystemProperties");
|
||||
Method getMethod = systemProperties.getMethod("get", String.class);
|
||||
sysDisplaySize = (String) getMethod.invoke(systemProperties, "sys.display-size");
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to read sys.display-size", e);
|
||||
}
|
||||
// If we managed to read sys.display-size, attempt to parse it.
|
||||
if (!TextUtils.isEmpty(sysDisplaySize)) {
|
||||
try {
|
||||
String[] sysDisplaySizeParts = sysDisplaySize.trim().split("x");
|
||||
if (sysDisplaySizeParts.length == 2) {
|
||||
int width = Integer.parseInt(sysDisplaySizeParts[0]);
|
||||
int height = Integer.parseInt(sysDisplaySizeParts[1]);
|
||||
if (width > 0 && height > 0) {
|
||||
return new Point(width, height);
|
||||
}
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
// Do nothing.
|
||||
}
|
||||
Log.e(TAG, "Invalid sys.display-size: " + sysDisplaySize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
|
||||
Display display = windowManager.getDefaultDisplay();
|
||||
Point displaySize = new Point();
|
||||
if (Util.SDK_INT >= 23) {
|
||||
getDisplaySizeV23(display, displaySize);
|
||||
} else if (Util.SDK_INT >= 17) {
|
||||
getDisplaySizeV17(display, displaySize);
|
||||
} else if (Util.SDK_INT >= 16) {
|
||||
getDisplaySizeV16(display, displaySize);
|
||||
} else {
|
||||
getDisplaySizeV9(display, displaySize);
|
||||
}
|
||||
return displaySize;
|
||||
}
|
||||
|
||||
@TargetApi(23)
|
||||
private static void getDisplaySizeV23(Display display, Point outSize) {
|
||||
Display.Mode mode = display.getMode();
|
||||
outSize.x = mode.getPhysicalWidth();
|
||||
outSize.y = mode.getPhysicalHeight();
|
||||
}
|
||||
|
||||
@TargetApi(17)
|
||||
private static void getDisplaySizeV17(Display display, Point outSize) {
|
||||
display.getRealSize(outSize);
|
||||
}
|
||||
|
||||
@TargetApi(16)
|
||||
private static void getDisplaySizeV16(Display display, Point outSize) {
|
||||
display.getSize(outSize);
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
private static void getDisplaySizeV9(Display display, Point outSize) {
|
||||
outSize.x = display.getWidth();
|
||||
outSize.y = display.getHeight();
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows the CRC calculation to be done byte by byte instead of bit per bit being the order
|
||||
* "most significant bit first".
|
||||
|
|
|
|||
Loading…
Reference in a new issue