Use theme when loading drawables on API 21+

Issue: androidx/media#220
PiperOrigin-RevId: 495642588
This commit is contained in:
bachinger 2022-12-15 19:00:04 +00:00 committed by Tianyi Feng
parent d11e0a35c1
commit 22dfd4cb32
5 changed files with 85 additions and 33 deletions

View File

@ -18,6 +18,8 @@ Release notes
([#10776](https://github.com/google/ExoPlayer/issues/10776)). ([#10776](https://github.com/google/ExoPlayer/issues/10776)).
* Add parameter to `BasePlayer.seekTo` to also indicate the command used * Add parameter to `BasePlayer.seekTo` to also indicate the command used
for seeking. for seeking.
* Use theme when loading drawables on API 21+
([#220](https://github.com/androidx/media/issues/220)).
* Audio: * Audio:
* Use the compressed audio format bitrate to calculate the min buffer size * Use the compressed audio format bitrate to calculate the min buffer size
for `AudioTrack` in direct playbacks (passthrough). for `AudioTrack` in direct playbacks (passthrough).

View File

@ -48,6 +48,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.graphics.drawable.Drawable;
import android.hardware.display.DisplayManager; import android.hardware.display.DisplayManager;
import android.media.AudioFormat; import android.media.AudioFormat;
import android.media.AudioManager; import android.media.AudioManager;
@ -68,6 +69,8 @@ import android.util.SparseLongArray;
import android.view.Display; import android.view.Display;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.WindowManager; import android.view.WindowManager;
import androidx.annotation.DoNotInline;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import androidx.media3.common.C; import androidx.media3.common.C;
@ -2907,6 +2910,23 @@ public final class Util {
return sum; return sum;
} }
/**
* Returns a {@link Drawable} for the given resource or throws a {@link
* Resources.NotFoundException} if not found.
*
* @param context The context to get the theme from starting with API 21.
* @param resources The resources to load the drawable from.
* @param drawableRes The drawable resource int.
* @return The loaded {@link Drawable}.
*/
@UnstableApi
public static Drawable getDrawable(
Context context, Resources resources, @DrawableRes int drawableRes) {
return SDK_INT >= 21
? Api21.getDrawable(context, resources, drawableRes)
: resources.getDrawable(drawableRes);
}
@Nullable @Nullable
private static String getSystemProperty(String name) { private static String getSystemProperty(String name) {
try { try {
@ -3143,4 +3163,12 @@ public final class Util {
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4,
0xF3 0xF3
}; };
@RequiresApi(21)
private static final class Api21 {
@DoNotInline
public static Drawable getDrawable(Context context, Resources resources, @DrawableRes int res) {
return resources.getDrawable(res, context.getTheme());
}
}
} }

View File

@ -28,6 +28,7 @@ import static androidx.media3.common.Player.EVENT_POSITION_DISCONTINUITY;
import static androidx.media3.common.Player.EVENT_REPEAT_MODE_CHANGED; import static androidx.media3.common.Player.EVENT_REPEAT_MODE_CHANGED;
import static androidx.media3.common.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED; import static androidx.media3.common.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED;
import static androidx.media3.common.Player.EVENT_TIMELINE_CHANGED; import static androidx.media3.common.Player.EVENT_TIMELINE_CHANGED;
import static androidx.media3.common.util.Util.getDrawable;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
@ -498,11 +499,16 @@ public class LegacyPlayerControlView extends FrameLayout {
buttonAlphaDisabled = buttonAlphaDisabled =
(float) resources.getInteger(R.integer.exo_media_button_opacity_percentage_disabled) / 100; (float) resources.getInteger(R.integer.exo_media_button_opacity_percentage_disabled) / 100;
repeatOffButtonDrawable = resources.getDrawable(R.drawable.exo_legacy_controls_repeat_off); repeatOffButtonDrawable =
repeatOneButtonDrawable = resources.getDrawable(R.drawable.exo_legacy_controls_repeat_one); getDrawable(context, resources, R.drawable.exo_legacy_controls_repeat_off);
repeatAllButtonDrawable = resources.getDrawable(R.drawable.exo_legacy_controls_repeat_all); repeatOneButtonDrawable =
shuffleOnButtonDrawable = resources.getDrawable(R.drawable.exo_legacy_controls_shuffle_on); getDrawable(context, resources, R.drawable.exo_legacy_controls_repeat_one);
shuffleOffButtonDrawable = resources.getDrawable(R.drawable.exo_legacy_controls_shuffle_off); repeatAllButtonDrawable =
getDrawable(context, resources, R.drawable.exo_legacy_controls_repeat_all);
shuffleOnButtonDrawable =
getDrawable(context, resources, R.drawable.exo_legacy_controls_shuffle_on);
shuffleOffButtonDrawable =
getDrawable(context, resources, R.drawable.exo_legacy_controls_shuffle_off);
repeatOffButtonContentDescription = repeatOffButtonContentDescription =
resources.getString(R.string.exo_controls_repeat_off_description); resources.getString(R.string.exo_controls_repeat_off_description);
repeatOneButtonContentDescription = repeatOneButtonContentDescription =

View File

@ -34,6 +34,7 @@ import static androidx.media3.common.Player.EVENT_TIMELINE_CHANGED;
import static androidx.media3.common.Player.EVENT_TRACKS_CHANGED; import static androidx.media3.common.Player.EVENT_TRACKS_CHANGED;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Util.castNonNull; import static androidx.media3.common.util.Util.castNonNull;
import static androidx.media3.common.util.Util.getDrawable;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
@ -53,7 +54,9 @@ import android.widget.FrameLayout;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.PopupWindow; import android.widget.PopupWindow;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.DrawableRes;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.core.content.res.ResourcesCompat; import androidx.core.content.res.ResourcesCompat;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.Format; import androidx.media3.common.Format;
@ -529,11 +532,11 @@ public class PlayerControlView extends FrameLayout {
settingTexts[SETTINGS_PLAYBACK_SPEED_POSITION] = settingTexts[SETTINGS_PLAYBACK_SPEED_POSITION] =
resources.getString(R.string.exo_controls_playback_speed); resources.getString(R.string.exo_controls_playback_speed);
settingIcons[SETTINGS_PLAYBACK_SPEED_POSITION] = settingIcons[SETTINGS_PLAYBACK_SPEED_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_speed); getDrawable(context, resources, R.drawable.exo_styled_controls_speed);
settingTexts[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] = settingTexts[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getString(R.string.exo_track_selection_title_audio); resources.getString(R.string.exo_track_selection_title_audio);
settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] = settingIcons[SETTINGS_AUDIO_TRACK_SELECTION_POSITION] =
resources.getDrawable(R.drawable.exo_styled_controls_audiotrack); getDrawable(context, resources, R.drawable.exo_styled_controls_audiotrack);
settingsAdapter = new SettingsAdapter(settingTexts, settingIcons); settingsAdapter = new SettingsAdapter(settingTexts, settingIcons);
settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset); settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset);
settingsView = settingsView =
@ -553,8 +556,10 @@ public class PlayerControlView extends FrameLayout {
needToHideBars = true; needToHideBars = true;
trackNameProvider = new DefaultTrackNameProvider(getResources()); trackNameProvider = new DefaultTrackNameProvider(getResources());
subtitleOnButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_subtitle_on); subtitleOnButtonDrawable =
subtitleOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_subtitle_off); getDrawable(context, resources, R.drawable.exo_styled_controls_subtitle_on);
subtitleOffButtonDrawable =
getDrawable(context, resources, R.drawable.exo_styled_controls_subtitle_off);
subtitleOnContentDescription = subtitleOnContentDescription =
resources.getString(R.string.exo_controls_cc_enabled_description); resources.getString(R.string.exo_controls_cc_enabled_description);
subtitleOffContentDescription = subtitleOffContentDescription =
@ -565,14 +570,20 @@ public class PlayerControlView extends FrameLayout {
new PlaybackSpeedAdapter( new PlaybackSpeedAdapter(
resources.getStringArray(R.array.exo_controls_playback_speeds), PLAYBACK_SPEEDS); resources.getStringArray(R.array.exo_controls_playback_speeds), PLAYBACK_SPEEDS);
fullScreenExitDrawable = resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_exit); fullScreenExitDrawable =
getDrawable(context, resources, R.drawable.exo_styled_controls_fullscreen_exit);
fullScreenEnterDrawable = fullScreenEnterDrawable =
resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_enter); getDrawable(context, resources, R.drawable.exo_styled_controls_fullscreen_enter);
repeatOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_off); repeatOffButtonDrawable =
repeatOneButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_one); getDrawable(context, resources, R.drawable.exo_styled_controls_repeat_off);
repeatAllButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_all); repeatOneButtonDrawable =
shuffleOnButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_shuffle_on); getDrawable(context, resources, R.drawable.exo_styled_controls_repeat_one);
shuffleOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_shuffle_off); repeatAllButtonDrawable =
getDrawable(context, resources, R.drawable.exo_styled_controls_repeat_all);
shuffleOnButtonDrawable =
getDrawable(context, resources, R.drawable.exo_styled_controls_shuffle_on);
shuffleOffButtonDrawable =
getDrawable(context, resources, R.drawable.exo_styled_controls_shuffle_off);
fullScreenExitContentDescription = fullScreenExitContentDescription =
resources.getString(R.string.exo_controls_fullscreen_exit_description); resources.getString(R.string.exo_controls_fullscreen_exit_description);
fullScreenEnterContentDescription = fullScreenEnterContentDescription =
@ -955,17 +966,20 @@ public class PlayerControlView extends FrameLayout {
return; return;
} }
if (playPauseButton != null) { if (playPauseButton != null) {
if (shouldShowPauseButton()) { boolean shouldShowPauseButton = shouldShowPauseButton();
((ImageView) playPauseButton) @DrawableRes
.setImageDrawable(resources.getDrawable(R.drawable.exo_styled_controls_pause)); int drawableRes =
playPauseButton.setContentDescription( shouldShowPauseButton
resources.getString(R.string.exo_controls_pause_description)); ? R.drawable.exo_styled_controls_pause
} else { : R.drawable.exo_styled_controls_play;
((ImageView) playPauseButton) @StringRes
.setImageDrawable(resources.getDrawable(R.drawable.exo_styled_controls_play)); int stringRes =
playPauseButton.setContentDescription( shouldShowPauseButton
resources.getString(R.string.exo_controls_play_description)); ? R.string.exo_controls_pause_description
} : R.string.exo_controls_play_description;
((ImageView) playPauseButton)
.setImageDrawable(getDrawable(getContext(), resources, drawableRes));
playPauseButton.setContentDescription(resources.getString(stringRes));
} }
} }

View File

@ -18,6 +18,7 @@ package androidx.media3.ui;
import static androidx.media3.common.Player.COMMAND_GET_TEXT; import static androidx.media3.common.Player.COMMAND_GET_TEXT;
import static androidx.media3.common.Player.COMMAND_SET_VIDEO_SURFACE; import static androidx.media3.common.Player.COMMAND_SET_VIDEO_SURFACE;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.common.util.Assertions.checkNotNull;
import static androidx.media3.common.util.Util.getDrawable;
import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.ElementType.TYPE_USE;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
@ -291,9 +292,9 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
overlayFrameLayout = null; overlayFrameLayout = null;
ImageView logo = new ImageView(context); ImageView logo = new ImageView(context);
if (Util.SDK_INT >= 23) { if (Util.SDK_INT >= 23) {
configureEditModeLogoV23(getResources(), logo); configureEditModeLogoV23(context, getResources(), logo);
} else { } else {
configureEditModeLogo(getResources(), logo); configureEditModeLogo(context, getResources(), logo);
} }
addView(logo); addView(logo);
return; return;
@ -1450,13 +1451,14 @@ public class PlayerView extends FrameLayout implements AdViewProvider {
} }
@RequiresApi(23) @RequiresApi(23)
private static void configureEditModeLogoV23(Resources resources, ImageView logo) { private static void configureEditModeLogoV23(
logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo, null)); Context context, Resources resources, ImageView logo) {
logo.setImageDrawable(getDrawable(context, resources, R.drawable.exo_edit_mode_logo));
logo.setBackgroundColor(resources.getColor(R.color.exo_edit_mode_background_color, null)); logo.setBackgroundColor(resources.getColor(R.color.exo_edit_mode_background_color, null));
} }
private static void configureEditModeLogo(Resources resources, ImageView logo) { private static void configureEditModeLogo(Context context, Resources resources, ImageView logo) {
logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo)); logo.setImageDrawable(getDrawable(context, resources, R.drawable.exo_edit_mode_logo));
logo.setBackgroundColor(resources.getColor(R.color.exo_edit_mode_background_color)); logo.setBackgroundColor(resources.getColor(R.color.exo_edit_mode_background_color));
} }