Allow TrackSelectionDialog to indicate whether it has any content

PiperOrigin-RevId: 241523313
This commit is contained in:
olly 2019-04-02 15:23:38 +01:00 committed by Oliver Woodman
parent 42b641221c
commit 9aacef47ad
3 changed files with 131 additions and 71 deletions

View File

@ -197,7 +197,6 @@ public class DownloadTracker implements DownloadManager.Listener {
if (trackSelectionDialog != null) { if (trackSelectionDialog != null) {
trackSelectionDialog.dismiss(); trackSelectionDialog.dismiss();
} }
startDownloadDialogHelper = null;
} }
// DownloadHelper.Callback implementation. // DownloadHelper.Callback implementation.
@ -206,21 +205,26 @@ public class DownloadTracker implements DownloadManager.Listener {
public void onPrepared(DownloadHelper helper) { public void onPrepared(DownloadHelper helper) {
if (helper.getPeriodCount() == 0) { if (helper.getPeriodCount() == 0) {
Log.d(TAG, "No periods found. Downloading entire stream."); Log.d(TAG, "No periods found. Downloading entire stream.");
DownloadAction downloadAction = downloadHelper.getDownloadAction(Util.getUtf8Bytes(name)); startDownload();
startServiceWithAction(downloadAction);
downloadHelper.release(); downloadHelper.release();
return; return;
} }
mappedTrackInfo = downloadHelper.getMappedTrackInfo(/* periodIndex= */ 0); mappedTrackInfo = downloadHelper.getMappedTrackInfo(/* periodIndex= */ 0);
trackSelectionDialog = new TrackSelectionDialog(); if (!TrackSelectionDialog.willHaveContent(mappedTrackInfo)) {
trackSelectionDialog.init( Log.d(TAG, "No dialog content. Downloading entire stream.");
/* titleId= */ R.string.exo_download_description, startDownload();
mappedTrackInfo, downloadHelper.release();
/* initialSelection= */ DownloadHelper.DEFAULT_TRACK_SELECTOR_PARAMETERS, return;
/* allowAdaptiveSelections =*/ false, }
/* allowMultipleOverrides= */ true, trackSelectionDialog =
/* onClickListener= */ this, TrackSelectionDialog.createForMappedTrackInfoAndParameters(
/* onDismissListener= */ this); /* titleId= */ R.string.exo_download_description,
mappedTrackInfo,
/* initialParameters= */ DownloadHelper.DEFAULT_TRACK_SELECTOR_PARAMETERS,
/* allowAdaptiveSelections =*/ false,
/* allowMultipleOverrides= */ true,
/* onClickListener= */ this,
/* onDismissListener= */ this);
trackSelectionDialog.show(fragmentManager, /* tag= */ null); trackSelectionDialog.show(fragmentManager, /* tag= */ null);
} }
@ -248,8 +252,7 @@ public class DownloadTracker implements DownloadManager.Listener {
} }
} }
} }
DownloadAction downloadAction = downloadHelper.getDownloadAction(Util.getUtf8Bytes(name)); startDownload();
startServiceWithAction(downloadAction);
} }
// DialogInterface.OnDismissListener implementation. // DialogInterface.OnDismissListener implementation.
@ -259,5 +262,12 @@ public class DownloadTracker implements DownloadManager.Listener {
trackSelectionDialog = null; trackSelectionDialog = null;
downloadHelper.release(); downloadHelper.release();
} }
// Internal methods.
private void startDownload() {
DownloadAction downloadAction = downloadHelper.getDownloadAction(Util.getUtf8Bytes(name));
startServiceWithAction(downloadAction);
}
} }
} }

View File

@ -199,6 +199,7 @@ public class PlayerActivity extends AppCompatActivity
@Override @Override
public void onNewIntent(Intent intent) { public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
releasePlayer(); releasePlayer();
releaseAdsLoader(); releaseAdsLoader();
clearStartPosition(); clearStartPosition();
@ -294,13 +295,15 @@ public class PlayerActivity extends AppCompatActivity
@Override @Override
public void onClick(View view) { public void onClick(View view) {
if (view == selectTracksButton && !isShowingTrackSelectionDialog) { if (view == selectTracksButton
&& !isShowingTrackSelectionDialog
&& TrackSelectionDialog.willHaveContent(trackSelector)) {
isShowingTrackSelectionDialog = true; isShowingTrackSelectionDialog = true;
TrackSelectionDialog newDialog = TrackSelectionDialog trackSelectionDialog =
TrackSelectionDialog.createForTrackSelector( TrackSelectionDialog.createForTrackSelector(
trackSelector, trackSelector,
/* onDismissListener= */ dialog -> isShowingTrackSelectionDialog = false); /* onDismissListener= */ dismissedDialog -> isShowingTrackSelectionDialog = false);
newDialog.show(getSupportFragmentManager(), /* tag= */ null); trackSelectionDialog.show(getSupportFragmentManager(), /* tag= */ null);
} }
} }
@ -599,7 +602,7 @@ public class PlayerActivity extends AppCompatActivity
private void updateButtonVisibility() { private void updateButtonVisibility() {
selectTracksButton.setEnabled( selectTracksButton.setEnabled(
player != null && trackSelector.getCurrentMappedTrackInfo() != null); player != null && TrackSelectionDialog.willHaveContent(trackSelector));
} }
private void showControls() { private void showControls() {

View File

@ -47,22 +47,40 @@ import java.util.List;
public final class TrackSelectionDialog extends DialogFragment { public final class TrackSelectionDialog extends DialogFragment {
private final SparseArray<TrackSelectionViewFragment> tabFragments; private final SparseArray<TrackSelectionViewFragment> tabFragments;
private final List<CharSequence> tabTitles; private final ArrayList<Integer> tabTrackTypes;
private int titleId; private int titleId;
private MappedTrackInfo mappedTrackInfo;
private DefaultTrackSelector.Parameters initialSelection;
private boolean allowAdaptiveSelections;
private boolean allowMultipleOverrides;
private DialogInterface.OnClickListener onClickListener; private DialogInterface.OnClickListener onClickListener;
private DialogInterface.OnDismissListener onDismissListener; private DialogInterface.OnDismissListener onDismissListener;
/** /**
* Creates and initializes a dialog with a {@link DefaultTrackSelector} and automatically updates * Returns whether a track selection dialog will have content to display if initialized with the
* the track selector's parameters when tracks are selected. * specified {@link DefaultTrackSelector} in its current state.
*/
public static boolean willHaveContent(DefaultTrackSelector trackSelector) {
MappedTrackInfo mappedTrackInfo = trackSelector.getCurrentMappedTrackInfo();
return mappedTrackInfo != null && willHaveContent(mappedTrackInfo);
}
/**
* Returns whether a track selection dialog will have content to display if initialized with the
* specified {@link MappedTrackInfo}.
*/
public static boolean willHaveContent(MappedTrackInfo mappedTrackInfo) {
for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
if (showTabForRenderer(mappedTrackInfo, i)) {
return true;
}
}
return false;
}
/**
* Creates a dialog for a given {@link DefaultTrackSelector}, whose parameters will be
* automatically updated when tracks are selected.
* *
* @param trackSelector A {@link DefaultTrackSelector}. * @param trackSelector The {@link DefaultTrackSelector}.
* @param onDismissListener {@link DialogInterface.OnDismissListener} called when the dialog is * @param onDismissListener A {@link DialogInterface.OnDismissListener} to call when the dialog is
* dismissed. * dismissed.
*/ */
public static TrackSelectionDialog createForTrackSelector( public static TrackSelectionDialog createForTrackSelector(
@ -74,7 +92,7 @@ public final class TrackSelectionDialog extends DialogFragment {
trackSelectionDialog.init( trackSelectionDialog.init(
/* titleId= */ R.string.track_selection_title, /* titleId= */ R.string.track_selection_title,
mappedTrackInfo, mappedTrackInfo,
/* initialSelection = */ parameters, /* initialParameters = */ parameters,
/* allowAdaptiveSelections =*/ true, /* allowAdaptiveSelections =*/ true,
/* allowMultipleOverrides= */ false, /* allowMultipleOverrides= */ false,
/* onClickListener= */ (dialog, which) -> { /* onClickListener= */ (dialog, which) -> {
@ -100,20 +118,12 @@ public final class TrackSelectionDialog extends DialogFragment {
return trackSelectionDialog; return trackSelectionDialog;
} }
/** Creates the dialog. */
public TrackSelectionDialog() {
tabFragments = new SparseArray<>();
tabTitles = new ArrayList<>();
// Retain instance across activity re-creation to prevent losing access to init data.
setRetainInstance(true);
}
/** /**
* Initializes the dialog. * Creates a dialog for given {@link MappedTrackInfo} and {@link DefaultTrackSelector.Parameters}.
* *
* @param titleId The resource id of the dialog title. * @param titleId The resource id of the dialog title.
* @param mappedTrackInfo The {@link MappedTrackInfo} to display. * @param mappedTrackInfo The {@link MappedTrackInfo} to display.
* @param initialSelection The {@link DefaultTrackSelector.Parameters} describing the initial * @param initialParameters The {@link DefaultTrackSelector.Parameters} describing the initial
* track selection. * track selection.
* @param allowAdaptiveSelections Whether adaptive selections (consisting of more than one track) * @param allowAdaptiveSelections Whether adaptive selections (consisting of more than one track)
* can be made. * can be made.
@ -122,21 +132,60 @@ public final class TrackSelectionDialog extends DialogFragment {
* @param onDismissListener {@link DialogInterface.OnDismissListener} called when the dialog is * @param onDismissListener {@link DialogInterface.OnDismissListener} called when the dialog is
* dismissed. * dismissed.
*/ */
public void init( public static TrackSelectionDialog createForMappedTrackInfoAndParameters(
int titleId, int titleId,
MappedTrackInfo mappedTrackInfo, MappedTrackInfo mappedTrackInfo,
DefaultTrackSelector.Parameters initialSelection, DefaultTrackSelector.Parameters initialParameters,
boolean allowAdaptiveSelections,
boolean allowMultipleOverrides,
DialogInterface.OnClickListener onClickListener,
DialogInterface.OnDismissListener onDismissListener) {
TrackSelectionDialog trackSelectionDialog = new TrackSelectionDialog();
trackSelectionDialog.init(
titleId,
mappedTrackInfo,
initialParameters,
allowAdaptiveSelections,
allowMultipleOverrides,
onClickListener,
onDismissListener);
return trackSelectionDialog;
}
public TrackSelectionDialog() {
tabFragments = new SparseArray<>();
tabTrackTypes = new ArrayList<>();
// Retain instance across activity re-creation to prevent losing access to init data.
setRetainInstance(true);
}
private void init(
int titleId,
MappedTrackInfo mappedTrackInfo,
DefaultTrackSelector.Parameters initialParameters,
boolean allowAdaptiveSelections, boolean allowAdaptiveSelections,
boolean allowMultipleOverrides, boolean allowMultipleOverrides,
DialogInterface.OnClickListener onClickListener, DialogInterface.OnClickListener onClickListener,
DialogInterface.OnDismissListener onDismissListener) { DialogInterface.OnDismissListener onDismissListener) {
this.titleId = titleId; this.titleId = titleId;
this.mappedTrackInfo = mappedTrackInfo;
this.initialSelection = initialSelection;
this.allowAdaptiveSelections = allowAdaptiveSelections;
this.allowMultipleOverrides = allowMultipleOverrides;
this.onClickListener = onClickListener; this.onClickListener = onClickListener;
this.onDismissListener = onDismissListener; this.onDismissListener = onDismissListener;
for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
if (showTabForRenderer(mappedTrackInfo, i)) {
int trackType = mappedTrackInfo.getRendererType(/* rendererIndex= */ i);
TrackGroupArray trackGroupArray = mappedTrackInfo.getTrackGroups(i);
TrackSelectionViewFragment tabFragment = new TrackSelectionViewFragment();
tabFragment.init(
mappedTrackInfo,
/* rendererIndex= */ i,
initialParameters.getRendererDisabled(/* rendererIndex= */ i),
initialParameters.getSelectionOverride(/* rendererIndex= */ i, trackGroupArray),
allowAdaptiveSelections,
allowMultipleOverrides);
tabFragments.put(i, tabFragment);
tabTrackTypes.add(trackType);
}
}
} }
/** /**
@ -183,27 +232,7 @@ public final class TrackSelectionDialog extends DialogFragment {
@Override @Override
public View onCreateView( public View onCreateView(
LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
for (int i = 0; i < mappedTrackInfo.getRendererCount(); i++) {
TrackGroupArray trackGroupArray = mappedTrackInfo.getTrackGroups(i);
if (trackGroupArray.length == 0) {
continue;
}
String trackTypeString =
getTrackTypeString(mappedTrackInfo.getRendererType(/* rendererIndex= */ i));
if (trackTypeString == null) {
continue;
}
TrackSelectionViewFragment tabFragment = new TrackSelectionViewFragment();
tabFragment.init(
mappedTrackInfo,
/* rendererIndex= */ i,
initialSelection.getRendererDisabled(/* rendererIndex= */ i),
initialSelection.getSelectionOverride(/* rendererIndex= */ i, trackGroupArray),
allowAdaptiveSelections,
allowMultipleOverrides);
tabFragments.put(i, tabFragment);
tabTitles.add(trackTypeString);
}
View dialogView = inflater.inflate(R.layout.track_selection_dialog, container, false); View dialogView = inflater.inflate(R.layout.track_selection_dialog, container, false);
TabLayout tabLayout = dialogView.findViewById(R.id.track_selection_dialog_tab_layout); TabLayout tabLayout = dialogView.findViewById(R.id.track_selection_dialog_tab_layout);
ViewPager viewPager = dialogView.findViewById(R.id.track_selection_dialog_view_pager); ViewPager viewPager = dialogView.findViewById(R.id.track_selection_dialog_view_pager);
@ -221,9 +250,27 @@ public final class TrackSelectionDialog extends DialogFragment {
return dialogView; return dialogView;
} }
@Nullable private static boolean showTabForRenderer(MappedTrackInfo mappedTrackInfo, int rendererIndex) {
private String getTrackTypeString(int trackType) { TrackGroupArray trackGroupArray = mappedTrackInfo.getTrackGroups(rendererIndex);
Resources resources = getResources(); if (trackGroupArray.length == 0) {
return false;
}
int trackType = mappedTrackInfo.getRendererType(rendererIndex);
return isSupportedTrackType(trackType);
}
private static boolean isSupportedTrackType(int trackType) {
switch (trackType) {
case C.TRACK_TYPE_VIDEO:
case C.TRACK_TYPE_AUDIO:
case C.TRACK_TYPE_TEXT:
return true;
default:
return false;
}
}
private static String getTrackTypeString(Resources resources, int trackType) {
switch (trackType) { switch (trackType) {
case C.TRACK_TYPE_VIDEO: case C.TRACK_TYPE_VIDEO:
return resources.getString(R.string.exo_track_selection_title_video); return resources.getString(R.string.exo_track_selection_title_video);
@ -232,7 +279,7 @@ public final class TrackSelectionDialog extends DialogFragment {
case C.TRACK_TYPE_TEXT: case C.TRACK_TYPE_TEXT:
return resources.getString(R.string.exo_track_selection_title_text); return resources.getString(R.string.exo_track_selection_title_text);
default: default:
return null; throw new IllegalArgumentException();
} }
} }
@ -255,7 +302,7 @@ public final class TrackSelectionDialog extends DialogFragment {
@Nullable @Nullable
@Override @Override
public CharSequence getPageTitle(int position) { public CharSequence getPageTitle(int position) {
return tabTitles.get(position); return getTrackTypeString(getResources(), tabTrackTypes.get(position));
} }
} }