Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 767930b

Browse files
sim0629Gerrit Code Review
authored andcommitted
Merge "Prevent findControlButton from throwing NPE" into androidx-master-dev
2 parents 83b63ca + ab1e553 commit 767930b

File tree

1 file changed

+70
-56
lines changed

1 file changed

+70
-56
lines changed

media2/widget/src/main/java/androidx/media2/widget/MediaControlView.java

Lines changed: 70 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.content.DialogInterface;
2626
import android.content.res.Resources;
2727
import android.graphics.drawable.ColorDrawable;
28+
import android.graphics.drawable.Drawable;
2829
import android.net.Uri;
2930
import android.util.AttributeSet;
3031
import android.util.Log;
@@ -147,6 +148,10 @@ public class MediaControlView extends ViewGroup {
147148
private static final int SIZE_TYPE_FULL = 1;
148149
private static final int SIZE_TYPE_MINIMAL = 2;
149150

151+
private static final int PLAY_BUTTON_PAUSE = 0;
152+
private static final int PLAY_BUTTON_PLAY = 1;
153+
private static final int PLAY_BUTTON_REPLAY = 2;
154+
150155
// Int for defining the UX state where all the views (TitleBar, ProgressBar, BottomBar) are
151156
// all visible.
152157
private static final int UX_STATE_ALL_VISIBLE = 0;
@@ -883,7 +888,7 @@ public void onAnimationStart(Animator animation) {
883888
public void onAnimationEnd(Animator animation) {
884889
mBasicControls.setVisibility(View.INVISIBLE);
885890

886-
findControlButton(SIZE_TYPE_FULL, R.id.ffwd).setVisibility(
891+
findFullSizedControlButton(R.id.ffwd).setVisibility(
887892
mPlayer != null && mPlayer.canSeekForward() ? View.INVISIBLE : View.GONE);
888893
}
889894
});
@@ -901,7 +906,7 @@ public void onAnimationUpdate(ValueAnimator animation) {
901906
public void onAnimationStart(Animator animation) {
902907
mBasicControls.setVisibility(View.VISIBLE);
903908

904-
findControlButton(SIZE_TYPE_FULL, R.id.ffwd).setVisibility(
909+
findFullSizedControlButton(R.id.ffwd).setVisibility(
905910
mPlayer != null && mPlayer.canSeekForward() ? View.VISIBLE : View.GONE);
906911
}
907912

@@ -983,8 +988,8 @@ long setProgress() {
983988
} else {
984989
if (mAdSkipView.getVisibility() == View.VISIBLE) {
985990
mAdSkipView.setVisibility(View.GONE);
986-
findControlButton(SIZE_TYPE_FULL, R.id.next).setEnabled(true);
987-
findControlButton(SIZE_TYPE_FULL, R.id.next).clearColorFilter();
991+
findFullSizedControlButton(R.id.next).setEnabled(true);
992+
findFullSizedControlButton(R.id.next).clearColorFilter();
988993
}
989994
}
990995
}
@@ -1004,22 +1009,15 @@ long setProgress() {
10041009
void togglePausePlayState() {
10051010
ensurePlayerIsNotNull();
10061011

1007-
ImageButton playPauseButton = findControlButton(mSizeType, R.id.pause);
10081012
if (mPlayer.isPlaying()) {
10091013
mPlayer.pause();
1010-
playPauseButton.setImageDrawable(
1011-
mResources.getDrawable(R.drawable.ic_play_circle_filled));
1012-
playPauseButton.setContentDescription(
1013-
mResources.getString(R.string.mcv2_play_button_desc));
1014+
updatePlayButton(PLAY_BUTTON_PLAY);
10141015
} else {
10151016
if (mIsShowingReplayButton) {
10161017
mPlayer.seekTo(0);
10171018
}
10181019
mPlayer.play();
1019-
playPauseButton.setImageDrawable(
1020-
mResources.getDrawable(R.drawable.ic_pause_circle_filled));
1021-
playPauseButton.setContentDescription(
1022-
mResources.getString(R.string.mcv2_pause_button_desc));
1020+
updatePlayButton(PLAY_BUTTON_PAUSE);
10231021
}
10241022
}
10251023

@@ -1417,13 +1415,13 @@ void updateLayoutForAd() {
14171415
ensurePlayerIsNotNull();
14181416

14191417
if (mIsAdvertisement) {
1420-
findControlButton(SIZE_TYPE_FULL, R.id.rew).setVisibility(View.GONE);
1421-
findControlButton(SIZE_TYPE_FULL, R.id.ffwd).setVisibility(View.GONE);
1422-
findControlButton(SIZE_TYPE_FULL, R.id.prev).setVisibility(View.GONE);
1418+
findFullSizedControlButton(R.id.rew).setVisibility(View.GONE);
1419+
findFullSizedControlButton(R.id.ffwd).setVisibility(View.GONE);
1420+
findFullSizedControlButton(R.id.prev).setVisibility(View.GONE);
14231421

1424-
findControlButton(SIZE_TYPE_FULL, R.id.next).setVisibility(View.VISIBLE);
1425-
findControlButton(SIZE_TYPE_FULL, R.id.next).setEnabled(false);
1426-
findControlButton(SIZE_TYPE_FULL, R.id.next).setColorFilter(R.color.gray);
1422+
findFullSizedControlButton(R.id.next).setVisibility(View.VISIBLE);
1423+
findFullSizedControlButton(R.id.next).setEnabled(false);
1424+
findFullSizedControlButton(R.id.next).setColorFilter(R.color.gray);
14271425

14281426
mTimeView.setVisibility(View.GONE);
14291427
mAdSkipView.setVisibility(View.VISIBLE);
@@ -1432,17 +1430,17 @@ void updateLayoutForAd() {
14321430

14331431
mProgress.setEnabled(false);
14341432
} else {
1435-
findControlButton(SIZE_TYPE_FULL, R.id.rew).setVisibility(
1433+
findFullSizedControlButton(R.id.rew).setVisibility(
14361434
mPlayer.canSeekBackward() ? View.VISIBLE : View.GONE);
1437-
findControlButton(SIZE_TYPE_FULL, R.id.ffwd).setVisibility(
1435+
findFullSizedControlButton(R.id.ffwd).setVisibility(
14381436
mPlayer.canSeekForward() ? View.VISIBLE : View.GONE);
1439-
findControlButton(SIZE_TYPE_FULL, R.id.prev).setVisibility(
1437+
findFullSizedControlButton(R.id.prev).setVisibility(
14401438
mPlayer.canSkipToPrevious() ? View.VISIBLE : View.GONE);
14411439

1442-
findControlButton(SIZE_TYPE_FULL, R.id.next).setVisibility(
1440+
findFullSizedControlButton(R.id.next).setVisibility(
14431441
mPlayer.canSkipToNext() ? View.VISIBLE : View.GONE);
1444-
findControlButton(SIZE_TYPE_FULL, R.id.next).setEnabled(true);
1445-
findControlButton(SIZE_TYPE_FULL, R.id.next).clearColorFilter();
1442+
findFullSizedControlButton(R.id.next).setEnabled(true);
1443+
findFullSizedControlButton(R.id.next).clearColorFilter();
14461444

14471445
mTimeView.setVisibility(View.VISIBLE);
14481446
mAdSkipView.setVisibility(View.GONE);
@@ -1532,8 +1530,22 @@ private void initializeSettingsLists() {
15321530
mCustomPlaybackSpeedIndex = -1;
15331531
}
15341532

1533+
@Nullable
15351534
ImageButton findControlButton(int sizeType, @IdRes int id) {
1536-
return mTransportControlsMap.get(sizeType).findViewById(id);
1535+
View transportControl = mTransportControlsMap.get(sizeType);
1536+
if (transportControl == null) {
1537+
return null;
1538+
}
1539+
return transportControl.findViewById(id);
1540+
}
1541+
1542+
@NonNull
1543+
ImageButton findFullSizedControlButton(@IdRes int id) {
1544+
ImageButton button = findControlButton(SIZE_TYPE_FULL, id);
1545+
if (button == null) {
1546+
throw new IllegalArgumentException("Couldn't find a view that has the given id");
1547+
}
1548+
return button;
15371549
}
15381550

15391551
/**
@@ -1591,10 +1603,10 @@ void animateOverflow(float animatedValue) {
15911603
mTimeView.setAlpha(1 - animatedValue);
15921604
mBasicControls.setAlpha(1 - animatedValue);
15931605

1594-
int transportControlLeftWidth = findControlButton(SIZE_TYPE_FULL, R.id.pause).getLeft();
1606+
int transportControlLeftWidth = findFullSizedControlButton(R.id.pause).getLeft();
15951607
int transportControlTranslationX = (-1) * (int) (transportControlLeftWidth * animatedValue);
15961608
mFullTransportControls.setTranslationX(transportControlTranslationX);
1597-
findControlButton(SIZE_TYPE_FULL, R.id.ffwd).setAlpha(1 - animatedValue);
1609+
findFullSizedControlButton(R.id.ffwd).setAlpha(1 - animatedValue);
15981610
}
15991611

16001612
void resetHideCallbacks() {
@@ -1701,34 +1713,20 @@ void updateSelectedSpeed(int selectedSpeedIndex, String selectedSpeedText) {
17011713
}
17021714

17031715
void updateReplayButton(boolean toBeShown) {
1704-
ImageButton playPauseButton = findControlButton(mSizeType, R.id.pause);
17051716
ImageButton ffwdButton = findControlButton(mSizeType, R.id.ffwd);
17061717
if (toBeShown) {
17071718
mIsShowingReplayButton = true;
1708-
if (playPauseButton != null) {
1709-
playPauseButton.setImageDrawable(
1710-
mResources.getDrawable(R.drawable.ic_replay_circle_filled));
1711-
playPauseButton.setContentDescription(
1712-
mResources.getString(R.string.mcv2_replay_button_desc));
1713-
}
1719+
updatePlayButton(PLAY_BUTTON_REPLAY);
17141720
if (ffwdButton != null) {
17151721
ffwdButton.setAlpha(0.5f);
17161722
ffwdButton.setEnabled(false);
17171723
}
17181724
} else {
17191725
mIsShowingReplayButton = false;
1720-
if (playPauseButton != null) {
1721-
if (mPlayer != null && mPlayer.isPlaying()) {
1722-
playPauseButton.setImageDrawable(
1723-
mResources.getDrawable(R.drawable.ic_pause_circle_filled));
1724-
playPauseButton.setContentDescription(
1725-
mResources.getString(R.string.mcv2_pause_button_desc));
1726-
} else {
1727-
playPauseButton.setImageDrawable(
1728-
mResources.getDrawable(R.drawable.ic_play_circle_filled));
1729-
playPauseButton.setContentDescription(
1730-
mResources.getString(R.string.mcv2_play_button_desc));
1731-
}
1726+
if (mPlayer != null && mPlayer.isPlaying()) {
1727+
updatePlayButton(PLAY_BUTTON_PAUSE);
1728+
} else {
1729+
updatePlayButton(PLAY_BUTTON_PLAY);
17321730
}
17331731
if (ffwdButton != null) {
17341732
ffwdButton.setAlpha(1.0f);
@@ -1737,6 +1735,29 @@ void updateReplayButton(boolean toBeShown) {
17371735
}
17381736
}
17391737

1738+
void updatePlayButton(int type) {
1739+
ImageButton playButton = findControlButton(mSizeType, R.id.pause);
1740+
if (playButton == null) {
1741+
return;
1742+
}
1743+
Drawable drawable;
1744+
String description;
1745+
if (type == PLAY_BUTTON_PAUSE) {
1746+
drawable = mResources.getDrawable(R.drawable.ic_pause_circle_filled);
1747+
description = mResources.getString(R.string.mcv2_pause_button_desc);
1748+
} else if (type == PLAY_BUTTON_PLAY) {
1749+
drawable = mResources.getDrawable(R.drawable.ic_play_circle_filled);
1750+
description = mResources.getString(R.string.mcv2_play_button_desc);
1751+
} else if (type == PLAY_BUTTON_REPLAY) {
1752+
drawable = mResources.getDrawable(R.drawable.ic_replay_circle_filled);
1753+
description = mResources.getString(R.string.mcv2_replay_button_desc);
1754+
} else {
1755+
throw new IllegalArgumentException("unknown type " + type);
1756+
}
1757+
playButton.setImageDrawable(drawable);
1758+
playButton.setContentDescription(description);
1759+
}
1760+
17401761
void postDelayedRunnable(Runnable runnable, long interval) {
17411762
if (interval != DISABLE_DELAYED_ANIMATION) {
17421763
postDelayed(runnable, interval);
@@ -1979,7 +2000,6 @@ public void onPlayerStateChanged(@NonNull PlayerWrapper player, int state) {
19792000
// 1) Need to handle case where app customizes playback state behavior when app
19802001
// activity is resumed.
19812002
// 2) Need to handle case where the media file reaches end of duration.
1982-
ImageButton playPauseButton = findControlButton(mSizeType, R.id.pause);
19832003
switch (state) {
19842004
case SessionPlayer.PLAYER_STATE_PLAYING:
19852005
removeCallbacks(mUpdateProgress);
@@ -1988,20 +2008,14 @@ public void onPlayerStateChanged(@NonNull PlayerWrapper player, int state) {
19882008
updateReplayButton(false);
19892009
break;
19902010
case SessionPlayer.PLAYER_STATE_PAUSED:
1991-
playPauseButton.setImageDrawable(
1992-
mResources.getDrawable(R.drawable.ic_play_circle_filled));
1993-
playPauseButton.setContentDescription(
1994-
mResources.getString(R.string.mcv2_play_button_desc));
2011+
updatePlayButton(PLAY_BUTTON_PLAY);
19952012
removeCallbacks(mUpdateProgress);
19962013
removeCallbacks(mHideMainBars);
19972014
removeCallbacks(mHideProgressBar);
19982015
post(mShowAllBars);
19992016
break;
20002017
case SessionPlayer.PLAYER_STATE_ERROR:
2001-
playPauseButton.setImageDrawable(
2002-
mResources.getDrawable(R.drawable.ic_play_circle_filled));
2003-
playPauseButton.setContentDescription(
2004-
mResources.getString(R.string.mcv2_play_button_desc));
2018+
updatePlayButton(PLAY_BUTTON_PLAY);
20052019
removeCallbacks(mUpdateProgress);
20062020
if (getWindowToken() != null) {
20072021
new AlertDialog.Builder(getContext())

0 commit comments

Comments
 (0)