[go: nahoru, domu]

Skip to content

Commit

Permalink
Use theme when loading drawables on API 21+
Browse files Browse the repository at this point in the history
Issue: androidx/media#220
PiperOrigin-RevId: 495642588
  • Loading branch information
marcbaechinger authored and tianyif committed Dec 21, 2022
1 parent 8dea624 commit 33f8f40
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 =
Expand All @@ -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 =
Expand All @@ -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 =
Expand Down Expand Up @@ -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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 33f8f40

Please sign in to comment.