From 1f4156ce6d9231a78b3d69b720ec6a7cf3c5820e Mon Sep 17 00:00:00 2001 From: krocard Date: Thu, 5 Mar 2020 16:30:16 +0000 Subject: [PATCH] Restore the demo list position on start It's quite anoying to have to search in the list the last item played everytime the demo is restarted. Fix that by saving and restoring it. PiperOrigin-RevId: 299109261 --- RELEASENOTES.md | 1 + .../demo/SampleChooserActivity.java | 28 ++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 4b0c2605a3..f26249b139 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -90,6 +90,7 @@ ([#6920](https://github.com/google/ExoPlayer/issues/6920)). * Metadata: Add minimal DVB Application Information Table (AIT) support ([#6922](https://github.com/google/ExoPlayer/pull/6922)). +* The demo app startup selected item is the last played one. ### 2.11.3 (2020-02-19) ### diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java index 66bf4bad5a..2aa134b114 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/SampleChooserActivity.java @@ -17,6 +17,7 @@ package com.google.android.exoplayer2.demo; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.content.res.AssetManager; import android.net.Uri; import android.os.AsyncTask; @@ -62,6 +63,8 @@ public class SampleChooserActivity extends AppCompatActivity implements DownloadTracker.Listener, OnChildClickListener { private static final String TAG = "SampleChooserActivity"; + private static final String GROUP_POSITION_PREFERENCE_KEY = "SAMPLE_CHOOSER_GROUP_POSITION"; + private static final String CHILD_POSITION_PREFERENCE_KEY = "SAMPLE_CHOOSER_CHILD_POSITION"; private boolean useExtensionRenderers; private DownloadTracker downloadTracker; @@ -69,13 +72,15 @@ public class SampleChooserActivity extends AppCompatActivity private MenuItem preferExtensionDecodersMenuItem; private MenuItem randomAbrMenuItem; private MenuItem tunnelingMenuItem; + private ExpandableListView sampleListView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sample_chooser_activity); sampleAdapter = new SampleAdapter(); - ExpandableListView sampleListView = findViewById(R.id.sample_list); + sampleListView = findViewById(R.id.sample_list); + sampleListView.setAdapter(sampleAdapter); sampleListView.setOnChildClickListener(this); @@ -163,11 +168,32 @@ public class SampleChooserActivity extends AppCompatActivity .show(); } sampleAdapter.setSampleGroups(groups); + + SharedPreferences preferences = getPreferences(MODE_PRIVATE); + + int groupPosition = -1; + int childPosition = -1; + try { + groupPosition = preferences.getInt(GROUP_POSITION_PREFERENCE_KEY, /* defValue= */ -1); + childPosition = preferences.getInt(CHILD_POSITION_PREFERENCE_KEY, /* defValue= */ -1); + } catch (ClassCastException e) { + android.util.Log.w(TAG, "Saved position is not an int. Will not restore position.", e); + } + if (groupPosition != -1 && childPosition != -1) { + sampleListView.expandGroup(groupPosition); // shouldExpandGroup does not work without this. + sampleListView.setSelectedChild(groupPosition, childPosition, /* shouldExpandGroup= */ true); + } } @Override public boolean onChildClick( ExpandableListView parent, View view, int groupPosition, int childPosition, long id) { + // Save the selected item first to be able to restore it if the tested code crashes. + SharedPreferences.Editor prefEditor = getPreferences(MODE_PRIVATE).edit(); + prefEditor.putInt(GROUP_POSITION_PREFERENCE_KEY, groupPosition); + prefEditor.putInt(CHILD_POSITION_PREFERENCE_KEY, childPosition); + prefEditor.apply(); + Sample sample = (Sample) view.getTag(); Intent intent = new Intent(this, PlayerActivity.class); intent.putExtra(