From 33f8f406929c739e98bbea475b3609beb7db2b5f Mon Sep 17 00:00:00 2001 From: bachinger Date: Thu, 15 Dec 2022 19:00:04 +0000 Subject: [PATCH] Use theme when loading drawables on API 21+ Issue: androidx/media#220 PiperOrigin-RevId: 495642588 --- .../google/android/exoplayer2/util/Util.java | 27 +++++++++ .../exoplayer2/ui/PlayerControlView.java | 11 ++-- .../android/exoplayer2/ui/PlayerView.java | 14 +++-- .../ui/StyledPlayerControlView.java | 58 ++++++++++++------- .../exoplayer2/ui/StyledPlayerView.java | 14 +++-- 5 files changed, 85 insertions(+), 39 deletions(-) diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index c0f0256da84..739ee00cdf7 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -48,6 +48,7 @@ import android.database.DatabaseUtils; import android.database.sqlite.SQLiteDatabase; import android.graphics.Point; +import android.graphics.drawable.Drawable; import android.hardware.display.DisplayManager; import android.media.AudioFormat; import android.media.AudioManager; @@ -68,6 +69,8 @@ import android.view.Display; import android.view.SurfaceView; import android.view.WindowManager; +import androidx.annotation.DoNotInline; +import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; @@ -2786,6 +2789,22 @@ public static long sum(long... summands) { 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}. + */ + public static Drawable getDrawable( + Context context, Resources resources, @DrawableRes int drawableRes) { + return SDK_INT >= 21 + ? Api21.getDrawable(context, resources, drawableRes) + : resources.getDrawable(drawableRes); + } + @Nullable private static String getSystemProperty(String name) { try { @@ -3022,4 +3041,12 @@ private static String maybeReplaceLegacyLanguageTags(String languageTag) { 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 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()); + } + } } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java index e880b0f2548..b36cac18570 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerControlView.java @@ -28,6 +28,7 @@ import static com.google.android.exoplayer2.Player.EVENT_REPEAT_MODE_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_SHUFFLE_MODE_ENABLED_CHANGED; import static com.google.android.exoplayer2.Player.EVENT_TIMELINE_CHANGED; +import static com.google.android.exoplayer2.util.Util.getDrawable; import android.annotation.SuppressLint; import android.content.Context; @@ -488,11 +489,11 @@ public PlayerControlView( buttonAlphaDisabled = (float) resources.getInteger(R.integer.exo_media_button_opacity_percentage_disabled) / 100; - repeatOffButtonDrawable = resources.getDrawable(R.drawable.exo_controls_repeat_off); - repeatOneButtonDrawable = resources.getDrawable(R.drawable.exo_controls_repeat_one); - repeatAllButtonDrawable = resources.getDrawable(R.drawable.exo_controls_repeat_all); - shuffleOnButtonDrawable = resources.getDrawable(R.drawable.exo_controls_shuffle_on); - shuffleOffButtonDrawable = resources.getDrawable(R.drawable.exo_controls_shuffle_off); + repeatOffButtonDrawable = getDrawable(context, resources, R.drawable.exo_controls_repeat_off); + repeatOneButtonDrawable = getDrawable(context, resources, R.drawable.exo_controls_repeat_one); + repeatAllButtonDrawable = getDrawable(context, resources, R.drawable.exo_controls_repeat_all); + shuffleOnButtonDrawable = getDrawable(context, resources, R.drawable.exo_controls_shuffle_on); + shuffleOffButtonDrawable = getDrawable(context, resources, R.drawable.exo_controls_shuffle_off); repeatOffButtonContentDescription = resources.getString(R.string.exo_controls_repeat_off_description); repeatOneButtonContentDescription = diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java index 9824d984a0c..504a6c0250c 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/PlayerView.java @@ -17,6 +17,7 @@ import static com.google.android.exoplayer2.Player.COMMAND_GET_TEXT; import static com.google.android.exoplayer2.Player.COMMAND_SET_VIDEO_SURFACE; +import static com.google.android.exoplayer2.util.Util.getDrawable; import static java.lang.annotation.ElementType.TYPE_USE; import android.annotation.SuppressLint; @@ -342,9 +343,9 @@ public PlayerView(Context context, @Nullable AttributeSet attrs, int defStyleAtt overlayFrameLayout = null; ImageView logo = new ImageView(context); if (Util.SDK_INT >= 23) { - configureEditModeLogoV23(getResources(), logo); + configureEditModeLogoV23(context, getResources(), logo); } else { - configureEditModeLogo(getResources(), logo); + configureEditModeLogo(context, getResources(), logo); } addView(logo); return; @@ -1388,13 +1389,14 @@ private void updateControllerVisibility() { } @RequiresApi(23) - private static void configureEditModeLogoV23(Resources resources, ImageView logo) { - logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo, null)); + private static void configureEditModeLogoV23( + 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)); } - private static void configureEditModeLogo(Resources resources, ImageView logo) { - logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo)); + private static void configureEditModeLogo(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)); } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlView.java index f277a99ba8a..e8d72983f0b 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerControlView.java @@ -34,6 +34,7 @@ import static com.google.android.exoplayer2.Player.EVENT_TRACKS_CHANGED; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; import static com.google.android.exoplayer2.util.Util.castNonNull; +import static com.google.android.exoplayer2.util.Util.getDrawable; import android.annotation.SuppressLint; import android.content.Context; @@ -53,7 +54,9 @@ import android.widget.ImageView; import android.widget.PopupWindow; import android.widget.TextView; +import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; +import androidx.annotation.StringRes; import androidx.core.content.res.ResourcesCompat; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -535,11 +538,11 @@ public StyledPlayerControlView( settingTexts[SETTINGS_PLAYBACK_SPEED_POSITION] = resources.getString(R.string.exo_controls_playback_speed); 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] = resources.getString(R.string.exo_track_selection_title_audio); 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); settingsWindowMargin = resources.getDimensionPixelSize(R.dimen.exo_settings_offset); settingsView = @@ -559,8 +562,10 @@ public StyledPlayerControlView( needToHideBars = true; trackNameProvider = new DefaultTrackNameProvider(getResources()); - subtitleOnButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_subtitle_on); - subtitleOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_subtitle_off); + subtitleOnButtonDrawable = + getDrawable(context, resources, R.drawable.exo_styled_controls_subtitle_on); + subtitleOffButtonDrawable = + getDrawable(context, resources, R.drawable.exo_styled_controls_subtitle_off); subtitleOnContentDescription = resources.getString(R.string.exo_controls_cc_enabled_description); subtitleOffContentDescription = @@ -571,14 +576,20 @@ public StyledPlayerControlView( new PlaybackSpeedAdapter( 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 = - resources.getDrawable(R.drawable.exo_styled_controls_fullscreen_enter); - repeatOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_off); - repeatOneButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_one); - repeatAllButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_repeat_all); - shuffleOnButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_shuffle_on); - shuffleOffButtonDrawable = resources.getDrawable(R.drawable.exo_styled_controls_shuffle_off); + getDrawable(context, resources, R.drawable.exo_styled_controls_fullscreen_enter); + repeatOffButtonDrawable = + getDrawable(context, resources, R.drawable.exo_styled_controls_repeat_off); + repeatOneButtonDrawable = + getDrawable(context, resources, R.drawable.exo_styled_controls_repeat_one); + 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 = resources.getString(R.string.exo_controls_fullscreen_exit_description); fullScreenEnterContentDescription = @@ -961,17 +972,20 @@ private void updatePlayPauseButton() { return; } if (playPauseButton != null) { - if (shouldShowPauseButton()) { - ((ImageView) playPauseButton) - .setImageDrawable(resources.getDrawable(R.drawable.exo_styled_controls_pause)); - playPauseButton.setContentDescription( - resources.getString(R.string.exo_controls_pause_description)); - } else { - ((ImageView) playPauseButton) - .setImageDrawable(resources.getDrawable(R.drawable.exo_styled_controls_play)); - playPauseButton.setContentDescription( - resources.getString(R.string.exo_controls_play_description)); - } + boolean shouldShowPauseButton = shouldShowPauseButton(); + @DrawableRes + int drawableRes = + shouldShowPauseButton + ? R.drawable.exo_styled_controls_pause + : R.drawable.exo_styled_controls_play; + @StringRes + int stringRes = + shouldShowPauseButton + ? R.string.exo_controls_pause_description + : R.string.exo_controls_play_description; + ((ImageView) playPauseButton) + .setImageDrawable(getDrawable(getContext(), resources, drawableRes)); + playPauseButton.setContentDescription(resources.getString(stringRes)); } } diff --git a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java index da17c4c1c7d..33b95c087b4 100644 --- a/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java +++ b/library/ui/src/main/java/com/google/android/exoplayer2/ui/StyledPlayerView.java @@ -18,6 +18,7 @@ import static com.google.android.exoplayer2.Player.COMMAND_GET_TEXT; import static com.google.android.exoplayer2.Player.COMMAND_SET_VIDEO_SURFACE; import static com.google.android.exoplayer2.util.Assertions.checkNotNull; +import static com.google.android.exoplayer2.util.Util.getDrawable; import static java.lang.annotation.ElementType.TYPE_USE; import android.annotation.SuppressLint; @@ -287,9 +288,9 @@ public StyledPlayerView(Context context, @Nullable AttributeSet attrs, int defSt overlayFrameLayout = null; ImageView logo = new ImageView(context); if (Util.SDK_INT >= 23) { - configureEditModeLogoV23(getResources(), logo); + configureEditModeLogoV23(context, getResources(), logo); } else { - configureEditModeLogo(getResources(), logo); + configureEditModeLogo(context, getResources(), logo); } addView(logo); return; @@ -1415,13 +1416,14 @@ private void updateAspectRatio() { } @RequiresApi(23) - private static void configureEditModeLogoV23(Resources resources, ImageView logo) { - logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo, null)); + private static void configureEditModeLogoV23( + 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)); } - private static void configureEditModeLogo(Resources resources, ImageView logo) { - logo.setImageDrawable(resources.getDrawable(R.drawable.exo_edit_mode_logo)); + private static void configureEditModeLogo(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)); }