mirror of
https://github.com/zoriya/Opus.git
synced 2025-12-06 06:26:15 +00:00
Reworking suggestion to solve a bug. Making app work when the user declined the read permission.
This commit is contained in:
@@ -770,15 +770,20 @@ namespace MusicApp
|
||||
FindViewById<NestedScrollView>(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<bool> 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)
|
||||
|
||||
@@ -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<Song> allSongs = new List<Song>();
|
||||
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<Song> allSongs = new List<Song>();
|
||||
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<Song> songList = allSongs.OrderBy(x => r.Next()).ToList();
|
||||
Random r = new Random();
|
||||
List<Song> 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<HomeItem> 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);
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace MusicApp.Resources.Portable_Class
|
||||
private List<Suggestion> History = new List<Suggestion>();
|
||||
private List<Suggestion> suggestions = new List<Suggestion>();
|
||||
|
||||
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<Toolbar>(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<Suggestion>();
|
||||
@@ -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<SearchView>();
|
||||
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) =>
|
||||
|
||||
@@ -14,14 +14,12 @@ namespace MusicApp.Resources.Portable_Class
|
||||
private List<Suggestion> objects;
|
||||
private LayoutInflater inflater;
|
||||
private Context context;
|
||||
private int resource;
|
||||
|
||||
public override int Count => objects.Count;
|
||||
|
||||
public SuggestionAdapter(Context context, int resource, List<Suggestion> 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<ImageView>(Resource.Id.icon1).SetX((int)(18 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f));
|
||||
convertView.FindViewById<TextView>(Resource.Id.text).SetX(SearchableActivity.instance.searchView.GetX());
|
||||
convertView.FindViewById<TextView>(Resource.Id.text).SetPadding((int)(16 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f), 0, 0, 0);
|
||||
convertView.FindViewById<ImageView>(Resource.Id.refine).SetPadding(0, 0, (int)(6 * SearchableActivity.instance.Resources.DisplayMetrics.Density + 0.5f), 0);
|
||||
|
||||
convertView.FindViewById<ImageView>(Resource.Id.icon1).SetImageResource(objects[position].Icon);
|
||||
convertView.FindViewById<TextView>(Resource.Id.text).Text = objects[position].Text;
|
||||
if (!convertView.FindViewById<ImageView>(Resource.Id.refine).HasOnClickListeners)
|
||||
|
||||
@@ -5,26 +5,30 @@
|
||||
android:layout_height="wrap_content" >
|
||||
<ImageView
|
||||
android:id="@+id/icon1"
|
||||
android:layout_width="24dip"
|
||||
android:layout_height="wrap_content"
|
||||
android:scaleType="centerInside"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_marginLeft="18dp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:layout_marginBottom="12dp"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_centerVertical="true" />
|
||||
<ImageView
|
||||
android:id="@+id/refine"
|
||||
android:layout_width="48dip"
|
||||
android:layout_height="48dip"
|
||||
android:scaleType="centerInside"
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_marginRight="5dp"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_centerVertical="true"
|
||||
android:focusable="false"
|
||||
android:clickable="false"
|
||||
android:src="@drawable/Refine"/>
|
||||
<TextView
|
||||
android:id="@+id/text"
|
||||
android:textAppearance="?attr/textAppearanceSearchResultTitle"
|
||||
android:singleLine="true"
|
||||
android:layout_width="wrap_content"
|
||||
android:maxLines="2"
|
||||
android:ellipsize="end"
|
||||
android:layout_marginLeft="26dp"
|
||||
android:layout_toLeftOf="@id/refine"
|
||||
android:layout_toRightOf="@id/icon1"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerVertical="true" />
|
||||
</RelativeLayout>
|
||||
Reference in New Issue
Block a user