mirror of
https://github.com/androidx/media.git
synced 2025-05-17 12:39:52 +08:00
Avoid IncorrectContextUseViolation on Android 11
Applications may need to pass a non-visual context when creating a DefaultTrackSelector (e.g., because they're audio-only or are creating the selector in a background service). Obtaining the default display via DisplayManager avoids the strict mode violation that occurs when retrieving it via WindowManager. #minor-release PiperOrigin-RevId: 384487363
This commit is contained in:
parent
58541fa1d1
commit
949f715c6b
@ -36,6 +36,7 @@ import android.content.res.Resources;
|
|||||||
import android.database.DatabaseUtils;
|
import android.database.DatabaseUtils;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
import android.graphics.Point;
|
import android.graphics.Point;
|
||||||
|
import android.hardware.display.DisplayManager;
|
||||||
import android.media.AudioFormat;
|
import android.media.AudioFormat;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
@ -2244,9 +2245,23 @@ public final class Util {
|
|||||||
* @return The size of the current mode, in pixels.
|
* @return The size of the current mode, in pixels.
|
||||||
*/
|
*/
|
||||||
public static Point getCurrentDisplayModeSize(Context context) {
|
public static Point getCurrentDisplayModeSize(Context context) {
|
||||||
|
@Nullable Display defaultDisplay = null;
|
||||||
|
if (Util.SDK_INT >= 17) {
|
||||||
|
@Nullable
|
||||||
|
DisplayManager displayManager =
|
||||||
|
(DisplayManager) context.getSystemService(Context.DISPLAY_SERVICE);
|
||||||
|
// We don't expect displayManager to ever be null, so this check is just precautionary.
|
||||||
|
// Consider removing it when the library minSdkVersion is increased to 17 or higher.
|
||||||
|
if (displayManager != null) {
|
||||||
|
defaultDisplay = displayManager.getDisplay(Display.DEFAULT_DISPLAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (defaultDisplay == null) {
|
||||||
WindowManager windowManager =
|
WindowManager windowManager =
|
||||||
checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
|
checkNotNull((WindowManager) context.getSystemService(Context.WINDOW_SERVICE));
|
||||||
return getCurrentDisplayModeSize(context, windowManager.getDefaultDisplay());
|
defaultDisplay = windowManager.getDefaultDisplay();
|
||||||
|
}
|
||||||
|
return getCurrentDisplayModeSize(context, defaultDisplay);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2278,6 +2293,7 @@ public final class Util {
|
|||||||
|
|
||||||
// Otherwise check the system property for display size. From API 28 treble may prevent the
|
// Otherwise check the system property for display size. From API 28 treble may prevent the
|
||||||
// system from writing sys.display-size so we check vendor.display-size instead.
|
// system from writing sys.display-size so we check vendor.display-size instead.
|
||||||
|
@Nullable
|
||||||
String displaySize =
|
String displaySize =
|
||||||
Util.SDK_INT < 28
|
Util.SDK_INT < 28
|
||||||
? getSystemProperty("sys.display-size")
|
? getSystemProperty("sys.display-size")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user