mirror of
https://github.com/androidx/media.git
synced 2025-05-18 13:09:56 +08:00
Handle recreation of TrackSelectionView instances.
TrackSelectionView requires an initialization with a call to init(...). That's why we shouldn't let the view retain its view hierarchy automatically as the views won't be backed by data if restored. Instead add a listener which lets the containing activity/fragment save and restore the state if needed. PiperOrigin-RevId: 234152491
This commit is contained in:
parent
f9f55ae4d5
commit
c6195dbbaf
@ -261,15 +261,17 @@ public final class TrackSelectionDialog extends DialogFragment {
|
||||
}
|
||||
|
||||
/** Fragment to show a track seleciton in tab of the track selection dialog. */
|
||||
public static final class TrackSelectionViewFragment extends Fragment {
|
||||
public static final class TrackSelectionViewFragment extends Fragment
|
||||
implements TrackSelectionView.TrackSelectionListener {
|
||||
|
||||
private MappedTrackInfo mappedTrackInfo;
|
||||
private int rendererIndex;
|
||||
private boolean initialIsDisabled;
|
||||
@Nullable private SelectionOverride initialOverride;
|
||||
private boolean allowAdaptiveSelections;
|
||||
private boolean allowMultipleOverrides;
|
||||
|
||||
private boolean isDisabled;
|
||||
private List<SelectionOverride> overrides;
|
||||
|
||||
/* package */ TrackSelectionView trackSelectionView;
|
||||
|
||||
public void init(
|
||||
@ -281,8 +283,11 @@ public final class TrackSelectionDialog extends DialogFragment {
|
||||
boolean allowMultipleOverrides) {
|
||||
this.mappedTrackInfo = mappedTrackInfo;
|
||||
this.rendererIndex = rendererIndex;
|
||||
this.initialIsDisabled = initialIsDisabled;
|
||||
this.initialOverride = initialOverride;
|
||||
this.isDisabled = initialIsDisabled;
|
||||
this.overrides =
|
||||
initialOverride == null
|
||||
? Collections.emptyList()
|
||||
: Collections.singletonList(initialOverride);
|
||||
this.allowAdaptiveSelections = allowAdaptiveSelections;
|
||||
this.allowMultipleOverrides = allowMultipleOverrides;
|
||||
}
|
||||
@ -301,13 +306,14 @@ public final class TrackSelectionDialog extends DialogFragment {
|
||||
trackSelectionView.setAllowMultipleOverrides(allowMultipleOverrides);
|
||||
trackSelectionView.setAllowAdaptiveSelections(allowAdaptiveSelections);
|
||||
trackSelectionView.init(
|
||||
mappedTrackInfo,
|
||||
rendererIndex,
|
||||
initialIsDisabled,
|
||||
initialOverride == null
|
||||
? Collections.emptyList()
|
||||
: Collections.singletonList(initialOverride));
|
||||
mappedTrackInfo, rendererIndex, isDisabled, overrides, /* listener= */ this);
|
||||
return rootView;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackSelectionChanged(boolean isDisabled, List<SelectionOverride> overrides) {
|
||||
this.isDisabled = isDisabled;
|
||||
this.overrides = overrides;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -219,7 +219,7 @@ public final class TrackSelectionDialogBuilder {
|
||||
if (trackNameProvider != null) {
|
||||
selectionView.setTrackNameProvider(trackNameProvider);
|
||||
}
|
||||
selectionView.init(mappedTrackInfo, rendererIndex, isDisabled, overrides);
|
||||
selectionView.init(mappedTrackInfo, rendererIndex, isDisabled, overrides, /* listener= */ null);
|
||||
Dialog.OnClickListener okClickListener =
|
||||
(dialog, which) ->
|
||||
callback.onTracksSelected(selectionView.getIsDisabled(), selectionView.getOverrides());
|
||||
|
@ -41,6 +41,18 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
||||
/** A view for making track selections. */
|
||||
public class TrackSelectionView extends LinearLayout {
|
||||
|
||||
/** Listener for changes to the selected tracks. */
|
||||
public interface TrackSelectionListener {
|
||||
|
||||
/**
|
||||
* Called when the selected tracks changed.
|
||||
*
|
||||
* @param isDisabled Whether the renderer is disabled.
|
||||
* @param overrides List of selected track selection overrides for the renderer.
|
||||
*/
|
||||
void onTrackSelectionChanged(boolean isDisabled, List<SelectionOverride> overrides);
|
||||
}
|
||||
|
||||
private final int selectableItemBackgroundResourceId;
|
||||
private final LayoutInflater inflater;
|
||||
private final CheckedTextView disableView;
|
||||
@ -54,10 +66,11 @@ public class TrackSelectionView extends LinearLayout {
|
||||
private TrackNameProvider trackNameProvider;
|
||||
private CheckedTextView[][] trackViews;
|
||||
|
||||
private @MonotonicNonNull MappedTrackInfo mappedTrackInfo;
|
||||
@MonotonicNonNull private MappedTrackInfo mappedTrackInfo;
|
||||
private int rendererIndex;
|
||||
private TrackGroupArray trackGroups;
|
||||
private boolean isDisabled;
|
||||
@Nullable private TrackSelectionListener listener;
|
||||
|
||||
/** Creates a track selection view. */
|
||||
public TrackSelectionView(Context context) {
|
||||
@ -76,6 +89,9 @@ public class TrackSelectionView extends LinearLayout {
|
||||
super(context, attrs, defStyleAttr);
|
||||
overrides = new SparseArray<>();
|
||||
|
||||
// Don't save view hierarchy as it needs to be reinitialized with a call to init.
|
||||
setSaveFromParentEnabled(false);
|
||||
|
||||
TypedArray attributeArray =
|
||||
context
|
||||
.getTheme()
|
||||
@ -177,15 +193,18 @@ public class TrackSelectionView extends LinearLayout {
|
||||
* @param overrides List of initial overrides to be shown for this renderer. There must be at most
|
||||
* one override for each track group. If {@link #setAllowMultipleOverrides(boolean)} hasn't
|
||||
* been set to {@code true}, only the first override is used.
|
||||
* @param listener An optional listener for track selection updates.
|
||||
*/
|
||||
public void init(
|
||||
MappedTrackInfo mappedTrackInfo,
|
||||
int rendererIndex,
|
||||
boolean isDisabled,
|
||||
List<SelectionOverride> overrides) {
|
||||
List<SelectionOverride> overrides,
|
||||
@Nullable TrackSelectionListener listener) {
|
||||
this.mappedTrackInfo = mappedTrackInfo;
|
||||
this.rendererIndex = rendererIndex;
|
||||
this.isDisabled = isDisabled;
|
||||
this.listener = listener;
|
||||
int maxOverrides = allowMultipleOverrides ? overrides.size() : Math.min(overrides.size(), 1);
|
||||
for (int i = 0; i < maxOverrides; i++) {
|
||||
SelectionOverride override = overrides.get(i);
|
||||
@ -291,6 +310,9 @@ public class TrackSelectionView extends LinearLayout {
|
||||
onTrackViewClicked(view);
|
||||
}
|
||||
updateViewStates();
|
||||
if (listener != null) {
|
||||
listener.onTrackSelectionChanged(getIsDisabled(), getOverrides());
|
||||
}
|
||||
}
|
||||
|
||||
private void onDisableViewClicked() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user