-
Notifications
You must be signed in to change notification settings - Fork 135
Add a save button to the media controller tester app, #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
import android.os.RemoteException; | ||
import android.support.v4.media.MediaBrowserCompat; | ||
import android.support.v4.media.MediaBrowserCompat.MediaItem; | ||
import android.support.v4.media.MediaDescriptionCompat; | ||
import android.support.v4.media.MediaMetadataCompat; | ||
import android.support.v4.media.RatingCompat; | ||
import android.support.v4.media.session.MediaControllerCompat; | ||
|
@@ -73,6 +74,11 @@ | |
|
||
import com.google.android.material.tabs.TabLayout; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
|
@@ -248,20 +254,20 @@ public Object instantiateItem(@NonNull ViewGroup container, int position) { | |
browseTreeList.setHasFixedSize(true); | ||
browseTreeList.setAdapter(mBrowseMediaItemsAdapter); | ||
mBrowseMediaItemsAdapter.init(findViewById(R.id.media_browse_tree_top), | ||
findViewById(R.id.media_browse_tree_up)); | ||
findViewById(R.id.media_browse_tree_up), findViewById(R.id.media_browse_tree_save)); | ||
|
||
final RecyclerView browseTreeListExtraSuggested = findViewById(R.id.media_items_list_extra_suggested); | ||
browseTreeListExtraSuggested.setLayoutManager(new LinearLayoutManager(this)); | ||
browseTreeListExtraSuggested.setHasFixedSize(true); | ||
browseTreeListExtraSuggested.setAdapter(mBrowseMediaItemsExtraSuggestedAdapter); | ||
mBrowseMediaItemsExtraSuggestedAdapter.init(findViewById(R.id.media_browse_tree_top_extra_suggested), | ||
findViewById(R.id.media_browse_tree_up_extra_suggested)); | ||
findViewById(R.id.media_browse_tree_up_extra_suggested), findViewById(R.id.media_browse_tree_save_extra_suggested)); | ||
|
||
final RecyclerView searchItemsList = findViewById(R.id.search_items_list); | ||
searchItemsList.setLayoutManager(new LinearLayoutManager(this)); | ||
searchItemsList.setHasFixedSize(true); | ||
searchItemsList.setAdapter(mSearchMediaItemsAdapter); | ||
mSearchMediaItemsAdapter.init(null, null); | ||
mSearchMediaItemsAdapter.init(null, null, null); | ||
|
||
findViewById(R.id.search_button).setOnClickListener(v -> { | ||
CharSequence queryText = ((TextView) findViewById(R.id.search_query)).getText(); | ||
|
@@ -1206,7 +1212,7 @@ void updateItems(List<MediaBrowserCompat.MediaItem> items) { | |
* Assigns click handlers to the buttons if provided for moving to the top of the tree or | ||
* for moving up one level in the tree. | ||
*/ | ||
void init(View topButtonView, View upButtonView) { | ||
void init(View topButtonView, View upButtonView, View saveButtonView) { | ||
if (topButtonView != null) { | ||
topButtonView.setOnClickListener(v -> { | ||
if (mNodes.size() > 1) { | ||
|
@@ -1228,6 +1234,75 @@ void init(View topButtonView, View upButtonView) { | |
} | ||
}); | ||
} | ||
|
||
if (saveButtonView != null) { | ||
saveButtonView.setOnClickListener(v -> { | ||
if (mNodes.isEmpty()) { | ||
Toast toast = | ||
Toast.makeText( | ||
getApplicationContext(), "List Empty, nothing saved! ", Toast.LENGTH_LONG); | ||
toast.setMargin(50, 50); | ||
toast.show(); | ||
return; | ||
} | ||
File root = android.os.Environment.getExternalStorageDirectory(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This bit of code won't work on Android 11 and above because of changes for scoped storage. If possible, I think it would be best to change this to use the Storage Access Framework with the intent action The intent returns a URI in the form of |
||
File dir = new File(root.getAbsolutePath() + "/Temp"); | ||
dir.mkdirs(); | ||
File file = new File(dir, "_BrowseTreeContent.txt"); | ||
if (file.exists()) { | ||
file.delete(); | ||
} | ||
try { | ||
FileOutputStream f = new FileOutputStream(file); | ||
PrintWriter pw = new PrintWriter(f); | ||
// We print the file path at the beginning of the file so that we can use it | ||
// to pull the file from android to local computer in case user have forgotten. | ||
pw.println(file.toString()); | ||
|
||
for (MediaBrowserCompat.MediaItem item : mItems) { | ||
MediaDescriptionCompat descriptionCompat = item.getDescription(); | ||
if (descriptionCompat != null) { | ||
String infoStr = | ||
"Title:" + descriptionCompat.getTitle() != null | ||
? descriptionCompat.getTitle().toString() | ||
: "NAN"; | ||
infoStr += | ||
",Subtitle:" + descriptionCompat.getSubtitle() != null | ||
? descriptionCompat.getSubtitle().toString() | ||
: "NAN"; | ||
infoStr += | ||
",MediaId:" + descriptionCompat.getMediaId() != null | ||
? descriptionCompat.getMediaId().toString() | ||
: "NAN"; | ||
infoStr += | ||
",Uri:" + descriptionCompat.getMediaUri() != null | ||
? descriptionCompat.getMediaUri().toString() | ||
: "NAN"; | ||
infoStr += | ||
",Description:" + descriptionCompat.getDescription() != null | ||
? descriptionCompat.getDescription().toString() | ||
: "NAN"; | ||
pw.println(infoStr); | ||
} | ||
} | ||
|
||
pw.flush(); | ||
pw.close(); | ||
f.close(); | ||
Toast toast = | ||
Toast.makeText( | ||
getApplicationContext(), | ||
"MediaItems saved to " + file.getAbsolutePath(), | ||
Toast.LENGTH_LONG); | ||
toast.setMargin(50, 50); | ||
toast.show(); | ||
} catch (FileNotFoundException e) { | ||
e.printStackTrace(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
protected void subscribe() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This actually doesn't work when targeting Android Q+ because of scoped storage changes. It could work on Android Q, but I think it would be better to use the Storage Access Framework for this, which wouldn't require any permissions and would let the user pick where to put the file (I explained more below, but just adding this here so I don't miss this permission being removed if you go that route.