XML String sort

This commit is contained in:
h4h13 2019-11-04 21:56:18 +05:30
parent 6c27767930
commit f926cdeaf6
37 changed files with 9794 additions and 2558 deletions

View file

@ -14,7 +14,6 @@
package code.name.monkey.retromusic.util;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
@ -41,7 +40,6 @@ import code.name.monkey.retromusic.model.Song;
import static android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
public class PlaylistsUtil {
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
return playlistId != -1 && doesPlaylistExist(context,
@ -72,8 +70,7 @@ public class PlaylistsUtil {
if (uri != null) {
// Necessary because somehow the MediaStoreObserver is not notified when adding a playlist
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
Toast.makeText(context, context.getResources().getString(
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
Toast.makeText(context, context.getResources().getString(R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
id = Integer.parseInt(uri.getLastPathSegment());
}
} else {
@ -112,10 +109,10 @@ public class PlaylistsUtil {
}
}
public static void addToPlaylist(@NonNull Context context,
@NonNull Song song,
int playlistId,
boolean showToastOnFinish) {
static void addToPlaylist(@NonNull Context context,
@NonNull Song song,
int playlistId,
boolean showToastOnFinish) {
List<Song> helperList = new ArrayList<>();
helperList.add(song);
addToPlaylist(context, helperList, playlistId, showToastOnFinish);
@ -125,36 +122,38 @@ public class PlaylistsUtil {
@NonNull List<Song> songs,
int playlistId,
boolean showToastOnFinish) {
final int size = songs.size();
ArrayList<Song> noSongs = new ArrayList<Song>();
for (Song song : songs) {
if (!doPlaylistContains(context, playlistId, song.getId())) {
noSongs.add(song);
}
}
final int size = noSongs.size();
final ContentResolver resolver = context.getContentResolver();
final String[] projection = new String[]{"max(" + MediaStore.Audio.Playlists.Members.PLAY_ORDER + ")",};
final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
Cursor cursor = null;
int base = 0;
try (Cursor cursor = resolver.query(uri, projection, null, null, null)) {
try {
try {
cursor = resolver.query(uri, projection, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
base = cursor.getInt(0) + 1;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
int numInserted = 0;
for (int offSet = 0; offSet < size; offSet += 1000)
numInserted += resolver.bulkInsert(uri, makeInsertItems(songs, offSet, 1000, base));
if (showToastOnFinish) {
Toast.makeText(context, context.getResources().getString(
R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId)), Toast.LENGTH_SHORT).show();
if (cursor != null && cursor.moveToFirst()) {
base = cursor.getInt(0) + 1;
}
} catch (SecurityException ignored) {
}
int numInserted = 0;
for (int offSet = 0; offSet < size; offSet += 1000)
numInserted += resolver.bulkInsert(uri, makeInsertItems(noSongs, offSet, 1000, base));
if (showToastOnFinish) {
Toast.makeText(context, context.getResources().getString(
R.string.inserted_x_songs_into_playlist_x, numInserted, getNameForPlaylist(context, playlistId)), Toast.LENGTH_SHORT).show();
}
}
@NonNull
@ -278,5 +277,4 @@ public class PlaylistsUtil {
}
return exists;
}
}