From 0ecf4085ff1827b237ea01ad0c873098fed87594 Mon Sep 17 00:00:00 2001 From: Gboy9155 <32224410+Gboy9155@users.noreply.github.com> Date: Sat, 21 Oct 2017 01:49:08 +0200 Subject: [PATCH] Folder list long click done --- MusicApp/MainActivity.cs | 4 + MusicApp/Resources/Portable Class/Browse.cs | 32 +++- .../Resources/Portable Class/FolderBrowse.cs | 171 ++++++++++++++---- .../Resources/Portable Class/FolderTracks.cs | 15 +- .../Portable Class/PlaylistTracks.cs | 2 +- 5 files changed, 183 insertions(+), 41 deletions(-) diff --git a/MusicApp/MainActivity.cs b/MusicApp/MainActivity.cs index d523383..81ba395 100644 --- a/MusicApp/MainActivity.cs +++ b/MusicApp/MainActivity.cs @@ -108,6 +108,10 @@ namespace MusicApp { if (Browse.instance != null) Browse.instance.result = null; + if (PlaylistTracks.instance != null) + PlaylistTracks.instance.result = null; + if (FolderTracks.instance != null) + FolderTracks.instance.result = null; } void HideSearch() diff --git a/MusicApp/Resources/Portable Class/Browse.cs b/MusicApp/Resources/Portable Class/Browse.cs index fb4a07f..418d425 100644 --- a/MusicApp/Resources/Portable Class/Browse.cs +++ b/MusicApp/Resources/Portable Class/Browse.cs @@ -265,7 +265,7 @@ namespace MusicApp.Resources.Portable_Class public static void AddToPlaylist(Song item, string playList, long playlistID) { if (playList == "Create a playlist") - CreatePlalistDialog(); + CreatePlalistDialog(item); else { @@ -278,7 +278,7 @@ namespace MusicApp.Resources.Portable_Class } } - public static void CreatePlalistDialog() + public static void CreatePlalistDialog(Song item) { AlertDialog.Builder builder = new AlertDialog.Builder(act, Resource.Style.AppCompatAlertDialogStyle); builder.SetTitle("Playlist name"); @@ -287,18 +287,42 @@ namespace MusicApp.Resources.Portable_Class builder.SetNegativeButton("Cancel", (senderAlert, args) => { }); builder.SetPositiveButton("Create", (senderAlert, args) => { - CreatePlaylist(view.FindViewById(Resource.Id.playlistName).Text); + CreatePlaylist(view.FindViewById(Resource.Id.playlistName).Text, item); }); builder.Show(); } - public static void CreatePlaylist(string name) + public static void CreatePlaylist(string name, Song item) { ContentResolver resolver = act.ContentResolver; Android.Net.Uri uri = MediaStore.Audio.Playlists.ExternalContentUri; ContentValues value = new ContentValues(); value.Put(MediaStore.Audio.Playlists.InterfaceConsts.Name, name); resolver.Insert(uri, value); + + string playList = "foo"; + long playlistID = 0; + + CursorLoader loader = new CursorLoader(Android.App.Application.Context, uri, null, null, null, null); + ICursor cursor = (ICursor)loader.LoadInBackground(); + + if (cursor != null && cursor.MoveToFirst()) + { + int nameID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Name); + int getplaylistID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Id); + do + { + string playlistName = cursor.GetString(nameID); + long id = cursor.GetLong(getplaylistID); + + if (playlistName == name) + playlistID = id; + } + while (cursor.MoveToNext()); + cursor.Close(); + } + + AddToPlaylist(item, playList, playlistID); } } diff --git a/MusicApp/Resources/Portable Class/FolderBrowse.cs b/MusicApp/Resources/Portable Class/FolderBrowse.cs index a06d9ac..57f76a1 100644 --- a/MusicApp/Resources/Portable Class/FolderBrowse.cs +++ b/MusicApp/Resources/Portable Class/FolderBrowse.cs @@ -26,7 +26,7 @@ namespace MusicApp.Resources.Portable_Class public View emptyView; private View view; - private string[] actions = new string[] { "Add To Playlist" }; + private string[] actions = new string[] { "List songs", "Add To Playlist", "Random Play" }; private bool isEmpty = false; @@ -39,7 +39,7 @@ namespace MusicApp.Resources.Portable_Class ListView.EmptyView = emptyView; if(ListView.Adapter == null) - GetStoragePermission(); + PopulateList(); } public override void OnDestroy() @@ -69,36 +69,6 @@ namespace MusicApp.Resources.Portable_Class return instance; } - void GetStoragePermission() - { - const string permission = Manifest.Permission.ReadExternalStorage; - if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(Android.App.Application.Context, permission) == (int)Permission.Granted) - { - PopulateList(); - return; - } - string[] permissions = new string[] { permission }; - RequestPermissions(permissions, 0); - } - - public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults) - { - switch (requestCode) - { - case 0: - if (grantResults != null) - return; - - if (grantResults[0] == Permission.Granted) - PopulateList(); - - else - Snackbar.Make(View, "Permission denied, can't list musics.", Snackbar.LengthShort).Show(); - - break; - } - } - void PopulateList() { Uri musicUri = MediaStore.Audio.Media.ExternalContentUri; @@ -148,7 +118,28 @@ namespace MusicApp.Resources.Portable_Class private void ListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e) { - + string path = paths[e.Position]; + + AlertDialog.Builder builder = new AlertDialog.Builder(Activity, Resource.Style.AppCompatAlertDialogStyle); + builder.SetTitle("Pick an action"); + builder.SetItems(actions, (senderAlert, args) => + { + switch (args.Which) + { + case 0: + ListSongs(path); + break; + case 1: + GetPlaylist(path); + break; + case 2: + //random play + break; + default: + break; + } + }); + builder.Show(); } void ListSongs(string path) @@ -163,5 +154,119 @@ namespace MusicApp.Resources.Portable_Class transaction.AddToBackStack(null); transaction.Commit(); } + + public void GetPlaylist(string item) + { + List playList = new List(); + List playListId = new List(); + playList.Add("Create a playlist"); + playListId.Add(0); + + Uri uri = MediaStore.Audio.Playlists.ExternalContentUri; + CursorLoader loader = new CursorLoader(Android.App.Application.Context, uri, null, null, null, null); + ICursor cursor = (ICursor)loader.LoadInBackground(); + + if (cursor != null && cursor.MoveToFirst()) + { + int nameID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Name); + int playlistID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Id); + do + { + string name = cursor.GetString(nameID); + long id = cursor.GetLong(playlistID); + playList.Add(name); + playListId.Add(id); + } + while (cursor.MoveToNext()); + cursor.Close(); + } + + AlertDialog.Builder builder = new AlertDialog.Builder(act, Resource.Style.AppCompatAlertDialogStyle); + builder.SetTitle("Add to a playlist"); + builder.SetItems(playList.ToArray(), (senderAlert, args) => + { + AddToPlaylist(item, playList[args.Which], playListId[args.Which]); + }); + builder.Show(); + } + + public void AddToPlaylist(string path, string playList, long playlistID) + { + if (playList == "Create a playlist") + CreatePlalistDialog(path); + + else + { + ContentResolver resolver = act.ContentResolver; + + Uri musicUri = MediaStore.Audio.Media.GetContentUriForPath(path); + + CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null); + ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground(); + + if (musicCursor != null && musicCursor.MoveToFirst()) + { + int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id); + do + { + long id = musicCursor.GetLong(thisID); + + ContentValues value = new ContentValues(); + value.Put(MediaStore.Audio.Playlists.Members.AudioId, id); + value.Put(MediaStore.Audio.Playlists.InterfaceConsts.Id, playlistID); + value.Put(MediaStore.Audio.Playlists.Members.PlayOrder, 0); + resolver.Insert(MediaStore.Audio.Playlists.Members.GetContentUri("external", playlistID), value); + } + while (musicCursor.MoveToNext()); + musicCursor.Close(); + } + } + } + + public void CreatePlalistDialog(string path) + { + AlertDialog.Builder builder = new AlertDialog.Builder(act, Resource.Style.AppCompatAlertDialogStyle); + builder.SetTitle("Playlist name"); + View view = inflater.Inflate(Resource.Layout.CreatePlaylistDialog, null); + builder.SetView(view); + builder.SetNegativeButton("Cancel", (senderAlert, args) => { }); + builder.SetPositiveButton("Create", (senderAlert, args) => + { + CreatePlaylist(view.FindViewById(Resource.Id.playlistName).Text, path); + }); + builder.Show(); + } + + public void CreatePlaylist(string name, string path) + { + ContentResolver resolver = act.ContentResolver; + Uri uri = MediaStore.Audio.Playlists.ExternalContentUri; + ContentValues value = new ContentValues(); + value.Put(MediaStore.Audio.Playlists.InterfaceConsts.Name, name); + resolver.Insert(uri, value); + + long playlistID = 0; + + CursorLoader loader = new CursorLoader(Android.App.Application.Context, uri, null, null, null, null); + ICursor cursor = (ICursor)loader.LoadInBackground(); + + if (cursor != null && cursor.MoveToFirst()) + { + int nameID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Name); + int getplaylistID = cursor.GetColumnIndex(MediaStore.Audio.Playlists.InterfaceConsts.Id); + do + { + string playlistName = cursor.GetString(nameID); + long id = cursor.GetLong(getplaylistID); + + if (playlistName == name) + playlistID = id; + } + while (cursor.MoveToNext()); + cursor.Close(); + } + + AddToPlaylist(path, "foo", playlistID); + } } } \ No newline at end of file diff --git a/MusicApp/Resources/Portable Class/FolderTracks.cs b/MusicApp/Resources/Portable Class/FolderTracks.cs index beac3f4..a515bfb 100644 --- a/MusicApp/Resources/Portable Class/FolderTracks.cs +++ b/MusicApp/Resources/Portable Class/FolderTracks.cs @@ -20,6 +20,7 @@ namespace MusicApp.Resources.Portable_Class public static FolderTracks instance; public Adapter adapter; public View emptyView; + public List result; public bool isEmpty = false; public string path; @@ -35,7 +36,7 @@ namespace MusicApp.Resources.Portable_Class ListAdapter = adapter; PopulateList(); - MainActivity.instance.DisplaySearch(); + MainActivity.instance.DisplaySearch(1); } public override void OnDestroy() @@ -110,7 +111,7 @@ namespace MusicApp.Resources.Portable_Class public void Search(string search) { - List result = new List(); + result = new List(); foreach (Song item in tracks) { if (item.GetName().ToLower().Contains(search.ToLower()) || item.GetArtist().ToLower().Contains(search.ToLower())) @@ -124,7 +125,12 @@ namespace MusicApp.Resources.Portable_Class private void ListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e) { Browse.act = Activity; - Browse.Play(tracks[e.Position]); + + Song item = tracks[e.Position]; + if (result != null) + item = result[e.Position]; + + Browse.Play(item); } private void ListView_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e) @@ -132,6 +138,9 @@ namespace MusicApp.Resources.Portable_Class Browse.act = Activity; Browse.inflater = LayoutInflater; Song item = tracks[e.Position]; + if (result != null) + item = result[e.Position]; + AlertDialog.Builder builder = new AlertDialog.Builder(Activity, Resource.Style.AppCompatAlertDialogStyle); builder.SetTitle("Pick an action"); builder.SetItems(actions, (senderAlert, args) => diff --git a/MusicApp/Resources/Portable Class/PlaylistTracks.cs b/MusicApp/Resources/Portable Class/PlaylistTracks.cs index c8cebc9..ce4b1ef 100644 --- a/MusicApp/Resources/Portable Class/PlaylistTracks.cs +++ b/MusicApp/Resources/Portable Class/PlaylistTracks.cs @@ -36,7 +36,7 @@ namespace MusicApp.Resources.Portable_Class ListAdapter = adapter; PopulateList(); - MainActivity.instance.DisplaySearch(3); + MainActivity.instance.DisplaySearch(1); } public override void OnDestroy()