Exemple #1
0
        public async Task PopulateList()
        {
            if (await MainActivity.instance.GetReadPermission(false) == false)
            {
                MainActivity.instance.FindViewById(Resource.Id.loading).Visibility = ViewStates.Gone;
                EmptyView.Visibility = ViewStates.Visible;
                EmptyView.Text       = GetString(Resource.String.no_permission);
                return;
            }

            pathDisplay.Clear();
            paths.Clear();
            pathUse.Clear();

            await Task.Run(() =>
            {
                if (Looper.MyLooper() == null)
                {
                    Looper.Prepare();
                }

                Uri musicUri = MediaStore.Audio.Media.ExternalContentUri;
                CursorLoader cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null);
                ICursor musicCursor       = (ICursor)cursorLoader.LoadInBackground();

                if (musicCursor != null && musicCursor.MoveToFirst())
                {
                    int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
                    do
                    {
                        string path        = musicCursor.GetString(pathID);
                        path               = path.Substring(0, path.LastIndexOf("/"));
                        string displayPath = path.Substring(path.LastIndexOf("/") + 1, path.Length - (path.LastIndexOf("/") + 1));

                        if (!paths.Contains(path))
                        {
                            pathDisplay.Add(displayPath);
                            paths.Add(path);
                            pathUse.Add(1);
                        }
                        else
                        {
                            pathUse[paths.IndexOf(path)] += 1;
                        }
                    }while (musicCursor.MoveToNext());
                    musicCursor.Close();
                }
            });

            adapter                = new TwoLineAdapter(pathDisplay, pathUse);
            adapter.ItemClick     += ListView_ItemClick;
            adapter.ItemLongClick += ListView_ItemLongClick;
            ListView.SetAdapter(adapter);

            if (adapter.ItemCount == 0)
            {
                EmptyView.Visibility = ViewStates.Visible;
                EmptyView.Text       = MainActivity.instance.Resources.GetString(Resource.String.no_song);
            }
        }
Exemple #2
0
        public async void CreatePlaylist(string name, string path)
        {
            await MainActivity.instance.GetWritePermission();

            ContentResolver resolver = MainActivity.instance.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);
        }
Exemple #3
0
        public void GetPlaylist(string path)
        {
            List <string> playList   = new List <string>();
            List <long>   playListId = new List <long>();

            playList.Add(GetString(Resource.String.create_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(MainActivity.instance, MainActivity.dialogTheme);
            builder.SetTitle(Resource.String.save_folder_playlist);
            builder.SetItems(playList.ToArray(), (senderAlert, args) =>
            {
                AddToPlaylist(path, playList[args.Which], playListId[args.Which]);
            });
            builder.Show();
        }
Exemple #4
0
        public static ICursor CreateCursor(Context ctx)
        {
            string[] columns = { MediaStore.Images.Media.InterfaceConsts.Data, MediaStore.Images.Media.InterfaceConsts.Id };

            string orderBy = MediaStore.Images.Media.InterfaceConsts.Id;

            var loader      = new Android.Support.V4.Content.CursorLoader(ctx, MediaStore.Images.Media.ExternalContentUri, columns, null, null, orderBy + " DESC");
            var imagecursor = (ICursor)loader.LoadInBackground();

            return(imagecursor);
        }
Exemple #5
0
        public static ICursor CreateCursor(Context ctx)
        {
            string[] columns = { MediaStore.Images.Media.InterfaceConsts.Data, MediaStore.Images.Media.InterfaceConsts.Id };

            string orderBy = MediaStore.Images.Media.InterfaceConsts.Id;

            var loader = new Android.Support.V4.Content.CursorLoader(ctx, MediaStore.Images.Media.ExternalContentUri, columns, null, null, orderBy + " DESC");
            var imagecursor = (ICursor)loader.LoadInBackground();

            return imagecursor;
        }
Exemple #6
0
        public async void AddToPlaylist(string path, string playList, long playlistID)
        {
            if (playList == GetString(Resource.String.create_playlist))
            {
                CreatePlalistDialog(path);
            }

            else
            {
                await MainActivity.instance.GetWritePermission();

                int playSlot = await PlaylistManager.GetLastPlayCount(playlistID);

                List <ContentValues> values   = new List <ContentValues>();
                ContentResolver      resolver = MainActivity.instance.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);
                    int pathID = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
                    do
                    {
                        string songPath = musicCursor.GetString(pathID);

                        if (!songPath.Contains(path))
                        {
                            continue;
                        }

                        long id = musicCursor.GetLong(thisID);

                        ContentValues value = new ContentValues();
                        value.Put(MediaStore.Audio.Playlists.Members.AudioId, id);
                        value.Put(MediaStore.Audio.Playlists.Members.PlayOrder, playSlot + 1);
                        values.Add(value);

                        playSlot++;
                    }while (musicCursor.MoveToNext());
                    musicCursor.Close();
                }
                resolver.BulkInsert(MediaStore.Audio.Playlists.Members.GetContentUri("external", playlistID), values.ToArray());
            }
        }
Exemple #7
0
        /// <summary>
        /// Return the local path of a youtube video (if downloaded). If the video is not downloaded, return null.
        /// </summary>
        /// <param name="videoID"></param>
        /// <returns></returns>
        public static string GetLocalPathFromYTID(string videoID)
        {
            Android.Net.Uri musicUri     = MediaStore.Audio.Media.ExternalContentUri;
            CursorLoader    cursorLoader = new CursorLoader(Android.App.Application.Context, musicUri, null, null, null, null);
            ICursor         musicCursor  = (ICursor)cursorLoader.LoadInBackground();

            if (musicCursor != null && musicCursor.MoveToFirst())
            {
                int pathKey = musicCursor.GetColumnIndex(MediaStore.Audio.Media.InterfaceConsts.Data);
                do
                {
                    string path = musicCursor.GetString(pathKey);

                    try
                    {
                        Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read);
                        var    meta   = TagLib.File.Create(new StreamFileAbstraction(path, stream, stream));
                        string ytID   = meta.Tag.Comment;
                        stream.Dispose();

                        if (ytID == videoID)
                        {
                            musicCursor.Close();
                            return(path);
                        }
                    }
                    catch (CorruptFileException)
                    {
                        continue;
                    }
                }while (musicCursor.MoveToNext());
                musicCursor.Close();
            }

            return(null);
        }
Exemple #8
0
        private async Task PopulateView()
        {
            if (!populating)
            {
                populating = true;
                sections   = new List <Section>();

                if (MusicPlayer.UseCastPlayer || (MusicPlayer.queue != null && MusicPlayer.queue?.Count > 0))
                {
                    Section queue = new Section("Queue", SectionType.SinglePlaylist, MusicPlayer.queue);
                    sections.Add(queue);
                }

                Section shuffle = new Section(Resources.GetString(Resource.String.shuffle), SectionType.Shuffle);
                sections.Add(shuffle);

                await Task.Run(() =>
                {
                    if (MainActivity.instance.HasReadPermission())
                    {
                        if (Looper.MyLooper() == null)
                        {
                            Looper.Prepare();
                        }

                        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())
                        {
                            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";
                                }

                                allSongs.Add(new Song(Title, Artist, Album, null, AlbumArt, id, path));
                            }while (musicCursor.MoveToNext());
                            musicCursor.Close();
                        }
                        Random r             = new Random();
                        List <Song> songList = allSongs.OrderBy(x => r.Next()).ToList();

                        if (songList.Count > 0)
                        {
                            Section featured = new Section(Resources.GetString(Resource.String.featured), SectionType.SinglePlaylist, songList.GetRange(0, songList.Count > 50 ? 50 : songList.Count));
                            sections.Add(featured);
                        }
                    }
                });

                List <Song> favorites = await SongManager.GetFavorites();

                if (favorites.Count > 0)
                {
                    sections.Add(new Section("Fav", SectionType.SinglePlaylist, favorites));
                }

                view.FindViewById(Resource.Id.loading).Visibility = ViewStates.Gone;
                adapter = new SectionAdapter(sections);
                ListView.SetAdapter(adapter);
                adapter.ItemClick += ListView_ItemClick;
                ListView.SetItemAnimator(new DefaultItemAnimator());

                (List <PlaylistItem> playlists, string error) = await PlaylistManager.GetLocalPlaylists(false);

                if (playlists != null)
                {
                    (List <PlaylistItem> pl, List <PlaylistItem> sp) = await PlaylistManager.ProcessSyncedPlaylists(playlists);

                    List <PlaylistItem> saved = await PlaylistManager.GetSavedYoutubePlaylists(sp, null);

                    sp.AddRange(saved);
                    sp.AddRange(pl);

                    sections.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, sp));
                    adapter.NotifyItemInserted(sections.Count - 1);
                }
                else
                {
                    List <PlaylistItem> saved = await PlaylistManager.GetSavedYoutubePlaylists(null, null);

                    if (saved != null && saved.Count > 0)
                    {
                        sections.Add(new Section(GetString(Resource.String.playlists), SectionType.PlaylistList, saved));
                        adapter.NotifyItemInserted(sections.Count - 1);
                    }
                }

                populating = false;
            }
        }