From b6224b63f87da2fca9d78ddc92198af0d40871d1 Mon Sep 17 00:00:00 2001
From: Tristan Roux
Date: Thu, 21 Feb 2019 23:28:31 +0100
Subject: [PATCH] Reworking suggestion to solve a bug. Making app work when the
user declined the read permission.
---
MusicApp/MainActivity.cs | 15 ++--
MusicApp/Resources/Portable Class/Home.cs | 75 ++++++++++---------
MusicApp/Resources/Portable Class/Playlist.cs | 67 +++++++++--------
.../Portable Class/PlaylistTracks.cs | 1 +
.../Portable Class/SearchableActivity.cs | 10 +--
.../Portable Class/SuggestionAdapter.cs | 9 +--
.../Resources/layout/SuggestionLayout.xml | 22 +++---
7 files changed, 106 insertions(+), 93 deletions(-)
diff --git a/MusicApp/MainActivity.cs b/MusicApp/MainActivity.cs
index 18952b0..dd01d48 100644
--- a/MusicApp/MainActivity.cs
+++ b/MusicApp/MainActivity.cs
@@ -770,15 +770,20 @@ namespace MusicApp
FindViewById(Resource.Id.playerSheet).Visibility = ViewStates.Gone;
}
+ public bool HasReadPermission()
+ {
+ if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage) == (int)Permission.Granted)
+ return true;
+ else
+ return false;
+ }
+
public async Task GetReadPermission()
{
- const string permission = Manifest.Permission.ReadExternalStorage;
- if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, permission) == (int)Permission.Granted)
- {
+ if (HasReadPermission())
return true;
- }
PermissionGot = null;
- string[] permissions = new string[] { permission };
+ string[] permissions = new string[] { Manifest.Permission.ReadExternalStorage };
RequestPermissions(permissions, RequestCode);
while (PermissionGot == null)
diff --git a/MusicApp/Resources/Portable Class/Home.cs b/MusicApp/Resources/Portable Class/Home.cs
index 7183462..20a03fc 100644
--- a/MusicApp/Resources/Portable Class/Home.cs
+++ b/MusicApp/Resources/Portable Class/Home.cs
@@ -78,47 +78,50 @@ namespace MusicApp.Resources.Portable_Class
HomeSection shuffle = new HomeSection(Resources.GetString(Resource.String.shuffle), SectionType.Shuffle, null);
adapterItems.Add(shuffle);
- Android.Net.Uri musicUri = MediaStore.Audio.Media.ExternalContentUri;
-
- List allSongs = new List();
- CursorLoader cursorLoader = new CursorLoader(MainActivity.instance, musicUri, null, null, null, null);
- ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground();
-
- if (musicCursor != null && musicCursor.MoveToFirst())
+ if (MainActivity.instance.HasReadPermission())
{
- int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title);
- int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist);
- int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album);
- int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id);
- int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
- do
+ Android.Net.Uri musicUri = MediaStore.Audio.Media.ExternalContentUri;
+
+ List allSongs = new List();
+ CursorLoader cursorLoader = new CursorLoader(MainActivity.instance, musicUri, null, null, null, null);
+ ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground();
+
+ if (musicCursor != null && musicCursor.MoveToFirst())
{
- string Artist = musicCursor.GetString(artistID);
- string Title = musicCursor.GetString(titleID);
- string Album = musicCursor.GetString(albumID);
- long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId));
- long id = musicCursor.GetLong(thisID);
- string path = musicCursor.GetString(pathID);
+ int titleID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Title);
+ int artistID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Artist);
+ int albumID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Album);
+ int thisID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Id);
+ int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
+ do
+ {
+ string Artist = musicCursor.GetString(artistID);
+ string Title = musicCursor.GetString(titleID);
+ string Album = musicCursor.GetString(albumID);
+ long AlbumArt = musicCursor.GetLong(musicCursor.GetColumnIndex(MediaStore.Audio.Albums.InterfaceConsts.AlbumId));
+ long id = musicCursor.GetLong(thisID);
+ string path = musicCursor.GetString(pathID);
- if (Title == null)
- Title = "Unknown Title";
- if (Artist == null)
- Artist = "Unknow Artist";
- if (Album == null)
- Album = "Unknow Album";
+ if (Title == null)
+ Title = "Unknown Title";
+ if (Artist == null)
+ Artist = "Unknow Artist";
+ if (Album == null)
+ Album = "Unknow Album";
- allSongs.Add(new Song(Title, Artist, Album, null, AlbumArt, id, path));
+ allSongs.Add(new Song(Title, Artist, Album, null, AlbumArt, id, path));
+ }
+ while (musicCursor.MoveToNext());
+ musicCursor.Close();
}
- while (musicCursor.MoveToNext());
- musicCursor.Close();
- }
- Random r = new Random();
- List songList = allSongs.OrderBy(x => r.Next()).ToList();
+ Random r = new Random();
+ List songList = allSongs.OrderBy(x => r.Next()).ToList();
- if (songList.Count > 0)
- {
- HomeSection featured = new HomeSection(Resources.GetString(Resource.String.featured), SectionType.SinglePlaylist, songList.GetRange(0, songList.Count > 50 ? 50 : songList.Count));
- adapterItems.Add(featured);
+ if (songList.Count > 0)
+ {
+ HomeSection featured = new HomeSection(Resources.GetString(Resource.String.featured), SectionType.SinglePlaylist, songList.GetRange(0, songList.Count > 50 ? 50 : songList.Count));
+ adapterItems.Add(featured);
+ }
}
adapter = new HomeAdapter(adapterItems);
@@ -208,7 +211,7 @@ namespace MusicApp.Resources.Portable_Class
}
List playlistList = Items.FindAll(x => x.contentType == SectionType.PlaylistList);
Items.RemoveAll(x => x.contentType == SectionType.PlaylistList);
- r = new Random();
+ Random r = new Random();
Items = Items.OrderBy(x => r.Next()).ToList();
Items.AddRange(playlistList);
diff --git a/MusicApp/Resources/Portable Class/Playlist.cs b/MusicApp/Resources/Portable Class/Playlist.cs
index 49cfbdc..a1a9ad8 100644
--- a/MusicApp/Resources/Portable Class/Playlist.cs
+++ b/MusicApp/Resources/Portable Class/Playlist.cs
@@ -81,45 +81,52 @@ namespace MusicApp.Resources.Portable_Class
YoutubePlaylists.Add(new PlaylistItem("Header", null));
PlaylistItem Loading = new PlaylistItem("Loading", null);
- //Local playlists
- Android.Net.Uri uri = 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())
+ if (await MainActivity.instance.GetReadPermission())
{
- int nameID = cursor.GetColumnIndex(Playlists.InterfaceConsts.Name);
- int listID = cursor.GetColumnIndex(Playlists.InterfaceConsts.Id);
- do
+ //Local playlists
+ Android.Net.Uri uri = 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())
{
- string name = cursor.GetString(nameID);
- long id = cursor.GetLong(listID);
-
- PlaylistItem ytPlaylist = SyncedPlaylists.Find(x => x.LocalID == id);
- if (ytPlaylist == null)
+ int nameID = cursor.GetColumnIndex(Playlists.InterfaceConsts.Name);
+ int listID = cursor.GetColumnIndex(Playlists.InterfaceConsts.Id);
+ do
{
- Android.Net.Uri musicUri = Playlists.Members.GetContentUri("external", id);
- CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null);
- ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground();
+ string name = cursor.GetString(nameID);
+ long id = cursor.GetLong(listID);
- LocalPlaylists.Add(new PlaylistItem(name, id, musicCursor.Count));
- }
- else
- {
- if (ytPlaylist.YoutubeID == null)
- ytPlaylist.SyncState = SyncState.Loading;
+ PlaylistItem ytPlaylist = SyncedPlaylists.Find(x => x.LocalID == id);
+ if (ytPlaylist == null)
+ {
+ Android.Net.Uri musicUri = Playlists.Members.GetContentUri("external", id);
+ CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null);
+ ICursor musicCursor = (ICursor)cursorLoader.LoadInBackground();
+
+ LocalPlaylists.Add(new PlaylistItem(name, id, musicCursor.Count));
+ }
else
- ytPlaylist.SyncState = SyncState.True;
+ {
+ if (ytPlaylist.YoutubeID == null)
+ ytPlaylist.SyncState = SyncState.Loading;
+ else
+ ytPlaylist.SyncState = SyncState.True;
- YoutubePlaylists.Add(ytPlaylist);
+ YoutubePlaylists.Add(ytPlaylist);
+ }
}
+ while (cursor.MoveToNext());
+ cursor.Close();
}
- while (cursor.MoveToNext());
- cursor.Close();
- }
- if (LocalPlaylists.Count == 1)
- LocalPlaylists.Add(new PlaylistItem("EMPTY", -1) { Owner = Resources.GetString(Resource.String.local_playlist_empty) });
+ if (LocalPlaylists.Count == 1)
+ LocalPlaylists.Add(new PlaylistItem("EMPTY", -1) { Owner = Resources.GetString(Resource.String.local_playlist_empty) });
+ }
+ else
+ {
+ LocalPlaylists.Add(new PlaylistItem("EMPTY", -1) { Owner = Resources.GetString(Resource.String.no_permission) });
+ }
YoutubePlaylists.Add(Loading);
adapter = new PlaylistAdapter(LocalPlaylists, YoutubePlaylists);
diff --git a/MusicApp/Resources/Portable Class/PlaylistTracks.cs b/MusicApp/Resources/Portable Class/PlaylistTracks.cs
index 7de0a59..c303b70 100644
--- a/MusicApp/Resources/Portable Class/PlaylistTracks.cs
+++ b/MusicApp/Resources/Portable Class/PlaylistTracks.cs
@@ -799,6 +799,7 @@ namespace MusicApp.Resources.Portable_Class
MusicPlayer.queue.InsertRange(0, preSongs);
MusicPlayer.currentID = preSongs.Count;
+ MusicPlayer.SaveQueueSlot();
Queue.instance?.Refresh();
while (MusicPlayer.instance == null)
diff --git a/MusicApp/Resources/Portable Class/SearchableActivity.cs b/MusicApp/Resources/Portable Class/SearchableActivity.cs
index 0f6b473..000ea1b 100644
--- a/MusicApp/Resources/Portable Class/SearchableActivity.cs
+++ b/MusicApp/Resources/Portable Class/SearchableActivity.cs
@@ -29,7 +29,7 @@ namespace MusicApp.Resources.Portable_Class
private List History = new List();
private List suggestions = new List();
- protected override void OnCreate(Bundle savedInstanceState)
+ protected async override void OnCreate(Bundle savedInstanceState)
{
instance = this;
base.OnCreate(savedInstanceState);
@@ -37,7 +37,6 @@ namespace MusicApp.Resources.Portable_Class
SetTheme(Resource.Style.DarkTheme);
SetContentView(Resource.Layout.SearchLayout);
- Window.SetStatusBarColor(Android.Graphics.Color.Argb(255, 33, 33, 33));
Toolbar ToolBar = FindViewById(Resource.Id.toolbar);
SetSupportActionBar(ToolBar);
@@ -75,7 +74,7 @@ namespace MusicApp.Resources.Portable_Class
}
};
- Task.Run(() =>
+ await Task.Run(() =>
{
SQLiteConnection db = new SQLiteConnection(System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "RecentSearch.sqlite"));
db.CreateTable();
@@ -84,6 +83,9 @@ namespace MusicApp.Resources.Portable_Class
History.Reverse();
suggestions = History;
});
+
+ adapter = new SuggestionAdapter(instance, Resource.Layout.SuggestionLayout, suggestions);
+ ListView.Adapter = adapter;
}
Suggestion HistoryItem(Suggestion suggestion)
@@ -97,8 +99,6 @@ namespace MusicApp.Resources.Portable_Class
IMenuItem searchItem = menu.FindItem(Resource.Id.search);
searchItem.ExpandActionView();
searchView = searchItem.ActionView.JavaCast();
- adapter = new SuggestionAdapter(instance, Resource.Layout.SuggestionLayout, suggestions);
- ListView.Adapter = adapter;
searchView.MaxWidth = int.MaxValue;
searchView.QueryHint = GetString(Resource.String.youtube_search);
searchView.QueryTextChange += (s, e) =>
diff --git a/MusicApp/Resources/Portable Class/SuggestionAdapter.cs b/MusicApp/Resources/Portable Class/SuggestionAdapter.cs
index 7cc4580..80872aa 100644
--- a/MusicApp/Resources/Portable Class/SuggestionAdapter.cs
+++ b/MusicApp/Resources/Portable Class/SuggestionAdapter.cs
@@ -14,14 +14,12 @@ namespace MusicApp.Resources.Portable_Class
private List objects;
private LayoutInflater inflater;
private Context context;
- private int resource;
public override int Count => objects.Count;
public SuggestionAdapter(Context context, int resource, List objects) : base(context, resource, objects)
{
this.context = context;
- this.resource = resource;
this.objects = objects;
}
@@ -39,14 +37,9 @@ namespace MusicApp.Resources.Portable_Class
}
if (convertView == null)
{
- convertView = inflater.Inflate(resource, parent, false);
+ convertView = inflater.Inflate(Resource.Layout.SuggestionLayout, parent, false);
}
- convertView.FindViewById(Resource.Id.icon1).SetX((int)(18 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f));
- convertView.FindViewById(Resource.Id.text).SetX(SearchableActivity.instance.searchView.GetX());
- convertView.FindViewById(Resource.Id.text).SetPadding((int)(16 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f), 0, 0, 0);
- convertView.FindViewById(Resource.Id.refine).SetPadding(0, 0, (int)(6 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f), 0);
-
convertView.FindViewById(Resource.Id.icon1).SetImageResource(objects[position].Icon);
convertView.FindViewById(Resource.Id.text).Text = objects[position].Text;
if (!convertView.FindViewById(Resource.Id.refine).HasOnClickListeners)
diff --git a/MusicApp/Resources/layout/SuggestionLayout.xml b/MusicApp/Resources/layout/SuggestionLayout.xml
index 94ab7c0..61657fc 100644
--- a/MusicApp/Resources/layout/SuggestionLayout.xml
+++ b/MusicApp/Resources/layout/SuggestionLayout.xml
@@ -5,26 +5,30 @@
android:layout_height="wrap_content" >
\ No newline at end of file