Commit defbd046 by tonihei Committed by Oliver Woodman

Update StartDownloadDialogHelper to use TrackSelectionView.

This is now possible as the download helper uses a track selector.

PiperOrigin-RevId: 225014517
parent 8d137c2e
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2018 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:orientation="horizontal"
android:gravity="center_vertical">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/track_title"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="4dp"/>
<TextView
android:id="@+id/track_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="4dp"/>
</LinearLayout>
<ImageButton
android:id="@+id/edit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:contentDescription="@string/download_edit_track"
android:src="@drawable/ic_edit"/>
</LinearLayout>
...@@ -13,7 +13,8 @@ ...@@ -13,7 +13,8 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<ListView xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/representation_list" android:id="@+id/selection_list"
android:orientation="vertical"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"/> android:layout_height="match_parent"/>
...@@ -51,6 +51,10 @@ ...@@ -51,6 +51,10 @@
<string name="ima_not_loaded">Playing sample without ads, as the IMA extension was not loaded</string> <string name="ima_not_loaded">Playing sample without ads, as the IMA extension was not loaded</string>
<string name="download_edit_track">Edit selection</string>
<string name="download_preparing">Preparing download…</string>
<string name="download_start_error">Failed to start download</string> <string name="download_start_error">Failed to start download</string>
<string name="download_playlist_unsupported">This demo app does not support downloading playlists</string> <string name="download_playlist_unsupported">This demo app does not support downloading playlists</string>
......
...@@ -15,7 +15,6 @@ ...@@ -15,7 +15,6 @@
*/ */
package com.google.android.exoplayer2.ui; package com.google.android.exoplayer2.ui;
import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.Dialog; import android.app.Dialog;
import android.content.Context; import android.content.Context;
...@@ -33,13 +32,25 @@ import com.google.android.exoplayer2.source.TrackGroup; ...@@ -33,13 +32,25 @@ import com.google.android.exoplayer2.source.TrackGroup;
import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.source.TrackGroupArray;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector;
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector.SelectionOverride; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector.SelectionOverride;
import com.google.android.exoplayer2.trackselection.MappingTrackSelector; import com.google.android.exoplayer2.trackselection.MappingTrackSelector.MappedTrackInfo;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import java.util.Arrays; import java.util.Arrays;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** A view for making track selections. */ /** A view for making track selections. */
public class TrackSelectionView extends LinearLayout { public class TrackSelectionView extends LinearLayout {
/** Callback which is invoked when a track selection has been made. */
public interface DialogCallback {
/**
* Called when track are selected.
*
* @param parameters The {@link DefaultTrackSelector.Parameters} for the selected tracks.
*/
void onTracksSelected(DefaultTrackSelector.Parameters parameters);
}
private final int selectableItemBackgroundResourceId; private final int selectableItemBackgroundResourceId;
private final LayoutInflater inflater; private final LayoutInflater inflater;
private final CheckedTextView disableView; private final CheckedTextView disableView;
...@@ -51,35 +62,64 @@ public class TrackSelectionView extends LinearLayout { ...@@ -51,35 +62,64 @@ public class TrackSelectionView extends LinearLayout {
private TrackNameProvider trackNameProvider; private TrackNameProvider trackNameProvider;
private CheckedTextView[][] trackViews; private CheckedTextView[][] trackViews;
private DefaultTrackSelector trackSelector; private @MonotonicNonNull MappedTrackInfo mappedTrackInfo;
private int rendererIndex; private int rendererIndex;
private DefaultTrackSelector.Parameters parameters;
private TrackGroupArray trackGroups; private TrackGroupArray trackGroups;
private boolean isDisabled; private boolean isDisabled;
private @Nullable SelectionOverride override; @Nullable private SelectionOverride override;
/** /**
* Gets a pair consisting of a dialog and the {@link TrackSelectionView} that will be shown by it. * Gets a pair consisting of a dialog and the {@link TrackSelectionView} that will be shown by it.
* *
* @param activity The parent activity. * <p>The dialog shows the current configuration of the provided {@code TrackSelector} and updates
* the parameters when closing the dialog.
*
* @param context The parent context.
* @param title The dialog's title. * @param title The dialog's title.
* @param trackSelector The track selector. * @param trackSelector The track selector.
* @param rendererIndex The index of the renderer. * @param rendererIndex The index of the renderer.
* @return The dialog and the {@link TrackSelectionView} that will be shown by it. * @return The dialog and the {@link TrackSelectionView} that will be shown by it.
*/ */
public static Pair<AlertDialog, TrackSelectionView> getDialog( public static Pair<AlertDialog, TrackSelectionView> getDialog(
Activity activity, Context context, CharSequence title, DefaultTrackSelector trackSelector, int rendererIndex) {
return getDialog(
context,
title,
Assertions.checkNotNull(trackSelector.getCurrentMappedTrackInfo()),
rendererIndex,
trackSelector.getParameters(),
trackSelector::setParameters);
}
/**
* Gets a pair consisting of a dialog and the {@link TrackSelectionView} that will be shown by it.
*
* @param context The parent context.
* @param title The dialog's title.
* @param mappedTrackInfo The {@link MappedTrackInfo}.
* @param rendererIndex The index of the renderer.
* @param parameters The {@link DefaultTrackSelector.Parameters}.
* @param callback The {@link DialogCallback} invoked when the dialog is closed successfully.
* @return The dialog and the {@link TrackSelectionView} that will be shown by it.
*/
public static Pair<AlertDialog, TrackSelectionView> getDialog(
Context context,
CharSequence title, CharSequence title,
DefaultTrackSelector trackSelector, MappedTrackInfo mappedTrackInfo,
int rendererIndex) { int rendererIndex,
AlertDialog.Builder builder = new AlertDialog.Builder(activity); DefaultTrackSelector.Parameters parameters,
DialogCallback callback) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Inflate with the builder's context to ensure the correct style is used. // Inflate with the builder's context to ensure the correct style is used.
LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext()); LayoutInflater dialogInflater = LayoutInflater.from(builder.getContext());
View dialogView = dialogInflater.inflate(R.layout.exo_track_selection_dialog, null); View dialogView = dialogInflater.inflate(R.layout.exo_track_selection_dialog, null);
final TrackSelectionView selectionView = dialogView.findViewById(R.id.exo_track_selection_view); TrackSelectionView selectionView = dialogView.findViewById(R.id.exo_track_selection_view);
selectionView.init(trackSelector, rendererIndex); selectionView.init(mappedTrackInfo, rendererIndex, parameters);
Dialog.OnClickListener okClickListener = (dialog, which) -> selectionView.applySelection(); Dialog.OnClickListener okClickListener =
(dialog, which) -> callback.onTracksSelected(selectionView.getSelectionParameters());
AlertDialog dialog = AlertDialog dialog =
builder builder
...@@ -113,6 +153,8 @@ public class TrackSelectionView extends LinearLayout { ...@@ -113,6 +153,8 @@ public class TrackSelectionView extends LinearLayout {
inflater = LayoutInflater.from(context); inflater = LayoutInflater.from(context);
componentListener = new ComponentListener(); componentListener = new ComponentListener();
trackNameProvider = new DefaultTrackNameProvider(getResources()); trackNameProvider = new DefaultTrackNameProvider(getResources());
parameters = DefaultTrackSelector.Parameters.DEFAULT;
trackGroups = TrackGroupArray.EMPTY;
// View for disabling the renderer. // View for disabling the renderer.
disableView = disableView =
...@@ -176,18 +218,35 @@ public class TrackSelectionView extends LinearLayout { ...@@ -176,18 +218,35 @@ public class TrackSelectionView extends LinearLayout {
} }
/** /**
* Initialize the view to select tracks for a specified renderer using a {@link * Initialize the view to select tracks for a specified renderer using {@link MappedTrackInfo} and
* DefaultTrackSelector}. * a set of {@link DefaultTrackSelector.Parameters}.
* *
* @param trackSelector The {@link DefaultTrackSelector}. * @param mappedTrackInfo The {@link MappedTrackInfo}.
* @param rendererIndex The index of the renderer. * @param rendererIndex The index of the renderer.
* @param parameters The {@link DefaultTrackSelector.Parameters}.
*/ */
public void init(DefaultTrackSelector trackSelector, int rendererIndex) { public void init(
this.trackSelector = trackSelector; MappedTrackInfo mappedTrackInfo,
int rendererIndex,
DefaultTrackSelector.Parameters parameters) {
this.mappedTrackInfo = mappedTrackInfo;
this.rendererIndex = rendererIndex; this.rendererIndex = rendererIndex;
this.parameters = parameters;
updateViews(); updateViews();
} }
/** Returns the {@link DefaultTrackSelector.Parameters} for the current selection. */
public DefaultTrackSelector.Parameters getSelectionParameters() {
DefaultTrackSelector.ParametersBuilder parametersBuilder = parameters.buildUpon();
parametersBuilder.setRendererDisabled(rendererIndex, isDisabled);
if (override != null) {
parametersBuilder.setSelectionOverride(rendererIndex, trackGroups, override);
} else {
parametersBuilder.clearSelectionOverrides(rendererIndex);
}
return parametersBuilder.build();
}
// Private methods. // Private methods.
private void updateViews() { private void updateViews() {
...@@ -196,9 +255,7 @@ public class TrackSelectionView extends LinearLayout { ...@@ -196,9 +255,7 @@ public class TrackSelectionView extends LinearLayout {
removeViewAt(i); removeViewAt(i);
} }
MappingTrackSelector.MappedTrackInfo trackInfo = if (mappedTrackInfo == null) {
trackSelector == null ? null : trackSelector.getCurrentMappedTrackInfo();
if (trackSelector == null || trackInfo == null) {
// The view is not initialized. // The view is not initialized.
disableView.setEnabled(false); disableView.setEnabled(false);
defaultView.setEnabled(false); defaultView.setEnabled(false);
...@@ -207,9 +264,8 @@ public class TrackSelectionView extends LinearLayout { ...@@ -207,9 +264,8 @@ public class TrackSelectionView extends LinearLayout {
disableView.setEnabled(true); disableView.setEnabled(true);
defaultView.setEnabled(true); defaultView.setEnabled(true);
trackGroups = trackInfo.getTrackGroups(rendererIndex); trackGroups = mappedTrackInfo.getTrackGroups(rendererIndex);
DefaultTrackSelector.Parameters parameters = trackSelector.getParameters();
isDisabled = parameters.getRendererDisabled(rendererIndex); isDisabled = parameters.getRendererDisabled(rendererIndex);
override = parameters.getSelectionOverride(rendererIndex, trackGroups); override = parameters.getSelectionOverride(rendererIndex, trackGroups);
...@@ -220,7 +276,7 @@ public class TrackSelectionView extends LinearLayout { ...@@ -220,7 +276,7 @@ public class TrackSelectionView extends LinearLayout {
boolean enableAdaptiveSelections = boolean enableAdaptiveSelections =
allowAdaptiveSelections allowAdaptiveSelections
&& trackGroups.get(groupIndex).length > 1 && trackGroups.get(groupIndex).length > 1
&& trackInfo.getAdaptiveSupport(rendererIndex, groupIndex, false) && mappedTrackInfo.getAdaptiveSupport(rendererIndex, groupIndex, false)
!= RendererCapabilities.ADAPTIVE_NOT_SUPPORTED; != RendererCapabilities.ADAPTIVE_NOT_SUPPORTED;
trackViews[groupIndex] = new CheckedTextView[group.length]; trackViews[groupIndex] = new CheckedTextView[group.length];
for (int trackIndex = 0; trackIndex < group.length; trackIndex++) { for (int trackIndex = 0; trackIndex < group.length; trackIndex++) {
...@@ -235,7 +291,7 @@ public class TrackSelectionView extends LinearLayout { ...@@ -235,7 +291,7 @@ public class TrackSelectionView extends LinearLayout {
(CheckedTextView) inflater.inflate(trackViewLayoutId, this, false); (CheckedTextView) inflater.inflate(trackViewLayoutId, this, false);
trackView.setBackgroundResource(selectableItemBackgroundResourceId); trackView.setBackgroundResource(selectableItemBackgroundResourceId);
trackView.setText(trackNameProvider.getTrackName(group.getFormat(trackIndex))); trackView.setText(trackNameProvider.getTrackName(group.getFormat(trackIndex)));
if (trackInfo.getTrackSupport(rendererIndex, groupIndex, trackIndex) if (mappedTrackInfo.getTrackSupport(rendererIndex, groupIndex, trackIndex)
== RendererCapabilities.FORMAT_HANDLED) { == RendererCapabilities.FORMAT_HANDLED) {
trackView.setFocusable(true); trackView.setFocusable(true);
trackView.setTag(Pair.create(groupIndex, trackIndex)); trackView.setTag(Pair.create(groupIndex, trackIndex));
...@@ -263,17 +319,6 @@ public class TrackSelectionView extends LinearLayout { ...@@ -263,17 +319,6 @@ public class TrackSelectionView extends LinearLayout {
} }
} }
private void applySelection() {
DefaultTrackSelector.ParametersBuilder parametersBuilder = trackSelector.buildUponParameters();
parametersBuilder.setRendererDisabled(rendererIndex, isDisabled);
if (override != null) {
parametersBuilder.setSelectionOverride(rendererIndex, trackGroups, override);
} else {
parametersBuilder.clearSelectionOverrides(rendererIndex);
}
trackSelector.setParameters(parametersBuilder);
}
private void onClick(View view) { private void onClick(View view) {
if (view == disableView) { if (view == disableView) {
onDisableViewClicked(); onDisableViewClicked();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment