From 12fee4cdf2b1edb63ea95b7a889fd6e805b167fd Mon Sep 17 00:00:00 2001 From: thecorkscrew <5bremer@informatik.uni-hamburg.de> Date: Fri, 15 Dec 2017 22:50:18 +0100 Subject: [PATCH 1/3] Checking for duplicates when inserting into a playlist and waiting for confirmation to add --- .../dialogs/AddDuplicateToPlaylistDialog.java | 88 +++++++++++++++++++ .../dialogs/AddToPlaylistDialog.java | 3 +- .../dialogs/CreatePlaylistDialog.java | 3 +- .../kabouzeid/gramophone/util/MusicUtil.java | 2 +- .../gramophone/util/PlaylistsUtil.java | 38 +++++++- app/src/main/res/values/strings.xml | 3 + 6 files changed, 130 insertions(+), 7 deletions(-) create mode 100755 app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java mode change 100644 => 100755 app/src/main/java/com/kabouzeid/gramophone/dialogs/AddToPlaylistDialog.java mode change 100644 => 100755 app/src/main/java/com/kabouzeid/gramophone/dialogs/CreatePlaylistDialog.java mode change 100644 => 100755 app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java mode change 100644 => 100755 app/src/main/res/values/strings.xml diff --git a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java new file mode 100755 index 000000000..b57a3ffcc --- /dev/null +++ b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java @@ -0,0 +1,88 @@ +package com.kabouzeid.gramophone.dialogs; + +import android.app.Dialog; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.v4.app.DialogFragment; +import android.text.Html; + +import com.afollestad.materialdialogs.DialogAction; +import com.afollestad.materialdialogs.MaterialDialog; +import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.model.Song; + +import java.util.ArrayList; + +/** + * @author Karim Abou Zeid (kabouzeid) + */ +public class AddDuplicateToPlaylistDialog extends DialogFragment { + + private int selection = 2; + + private SelectionListener selListener; + + public AddDuplicateToPlaylistDialog() { + selListener = null; + } + + public void setSelListener(SelectionListener SelListener) { + this.selListener = SelListener; + } + public interface SelectionListener { + public void selectedOption(int selection); + } + + @NonNull + public static AddDuplicateToPlaylistDialog create(Song song) { + ArrayList list = new ArrayList<>(); + list.add(song); + return create(list); + } + + @NonNull + public static AddDuplicateToPlaylistDialog create(ArrayList songs) { + AddDuplicateToPlaylistDialog dialog = new AddDuplicateToPlaylistDialog(); + Bundle args = new Bundle(); + args.putParcelableArrayList("songs", songs); + dialog.setArguments(args); + return dialog; + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + //noinspection unchecked + final ArrayList songs = getArguments().getParcelableArrayList("songs"); + int title; + CharSequence content; + title = R.string.add_duplicate_title; + content = Html.fromHtml(getString(R.string.add_duplicate_msg, songs.get(0).title)); + return new MaterialDialog.Builder(getActivity()) + .title(title) + .content(content) + .positiveText(R.string.add_action) + .negativeText(R.string.dont_add_duplicate) + .onNegative(new MaterialDialog.SingleButtonCallback() { + @Override + public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { + if (getActivity() == null) return; + selection = 0; + selListener.selectedOption(0); + } + }) + .onPositive(new MaterialDialog.SingleButtonCallback() { + @Override + public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { + if (getActivity() == null) return; + selection = 1; + selListener.selectedOption(1); + } + }) + .build(); + } + + public int getSelection() { + return selection; + } +} \ No newline at end of file diff --git a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddToPlaylistDialog.java b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddToPlaylistDialog.java old mode 100644 new mode 100755 index ac96afed8..820539674 --- a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddToPlaylistDialog.java +++ b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddToPlaylistDialog.java @@ -4,6 +4,7 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; +import android.support.v7.app.AppCompatActivity; import android.view.View; import com.afollestad.materialdialogs.MaterialDialog; @@ -60,7 +61,7 @@ public void onSelection(@NonNull MaterialDialog materialDialog, View view, int i CreatePlaylistDialog.create(songs).show(getActivity().getSupportFragmentManager(), "ADD_TO_PLAYLIST"); } else { materialDialog.dismiss(); - PlaylistsUtil.addToPlaylist(getActivity(), songs, playlists.get(i - 1).id, true); + PlaylistsUtil.addToPlaylist(getActivity(), songs, playlists.get(i - 1).id, true, (AppCompatActivity) getActivity()); } } }) diff --git a/app/src/main/java/com/kabouzeid/gramophone/dialogs/CreatePlaylistDialog.java b/app/src/main/java/com/kabouzeid/gramophone/dialogs/CreatePlaylistDialog.java old mode 100644 new mode 100755 index efd3261bb..e5e10dce0 --- a/app/src/main/java/com/kabouzeid/gramophone/dialogs/CreatePlaylistDialog.java +++ b/app/src/main/java/com/kabouzeid/gramophone/dialogs/CreatePlaylistDialog.java @@ -5,6 +5,7 @@ import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.DialogFragment; +import android.support.v7.app.AppCompatActivity; import android.text.InputType; import android.widget.Toast; @@ -67,7 +68,7 @@ public void onInput(@NonNull MaterialDialog materialDialog, @NonNull CharSequenc //noinspection unchecked ArrayList songs = getArguments().getParcelableArrayList(SONGS); if (songs != null && !songs.isEmpty()) { - PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true); + PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true, (AppCompatActivity) getActivity()); } } } else { diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java index 608b4486b..1329c2146 100644 --- a/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/MusicUtil.java @@ -263,7 +263,7 @@ public static void toggleFavorite(@NonNull final Context context, @NonNull final if (isFavorite(context, song)) { PlaylistsUtil.removeFromPlaylist(context, song, getFavoritesPlaylist(context).id); } else { - PlaylistsUtil.addToPlaylist(context, song, getOrCreateFavoritesPlaylist(context).id, false); + PlaylistsUtil.addToPlaylist(context, song, getOrCreateFavoritesPlaylist(context).id, false, null); } } diff --git a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java old mode 100644 new mode 100755 index 7f900cac3..7fdf44bbe --- a/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java +++ b/app/src/main/java/com/kabouzeid/gramophone/util/PlaylistsUtil.java @@ -10,9 +10,11 @@ import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import com.kabouzeid.gramophone.R; +import com.kabouzeid.gramophone.dialogs.AddDuplicateToPlaylistDialog; import com.kabouzeid.gramophone.helper.M3UWriter; import com.kabouzeid.gramophone.model.Playlist; import com.kabouzeid.gramophone.model.PlaylistSong; @@ -28,7 +30,7 @@ /** * @author Karim Abou Zeid (kabouzeid) */ -public class PlaylistsUtil { +public class PlaylistsUtil extends AddDuplicateToPlaylistDialog { public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) { return playlistId != -1 && doesPlaylistExist(context, @@ -99,13 +101,13 @@ public static void deletePlaylists(@NonNull final Context context, @NonNull fina } } - public static void addToPlaylist(@NonNull final Context context, final Song song, final int playlistId, final boolean showToastOnFinish) { + public static void addToPlaylist(@NonNull final Context context, final Song song, final int playlistId, final boolean showToastOnFinish, AppCompatActivity activity) { List helperList = new ArrayList<>(); helperList.add(song); - addToPlaylist(context, helperList, playlistId, showToastOnFinish); + addToPlaylist(context, helperList, playlistId, showToastOnFinish, activity); } - public static void addToPlaylist(@NonNull final Context context, @NonNull final List songs, final int playlistId, final boolean showToastOnFinish) { + public static void addToPlaylist(@NonNull final Context context, @NonNull final List songs, final int playlistId, final boolean showToastOnFinish, AppCompatActivity activity) { final int size = songs.size(); final ContentResolver resolver = context.getContentResolver(); final String[] projection = new String[]{ @@ -128,6 +130,23 @@ public static void addToPlaylist(@NonNull final Context context, @NonNull final } } + final int basecopy = base; + for(final Song song: songs) { + if (doPlaylistContains(context, playlistId, song.id)) { + //Toast.makeText(context, song.title+" already in Playlist!", Toast.LENGTH_SHORT).show(); + AddDuplicateToPlaylistDialog dialog = AddDuplicateToPlaylistDialog.create(song); + dialog.setSelListener(new AddDuplicateToPlaylistDialog.SelectionListener() { + @Override + public void selectedOption(int selection) { + if (selection == 0) { + removeFromPlaylist(context, song, playlistId, basecopy); + } + } + }); + dialog.show(activity.getSupportFragmentManager(), "DUPLICATE_ADD_PLAYLIST"); + } + } + int numInserted = 0; for (int offSet = 0; offSet < size; offSet += 1000) numInserted += resolver.bulkInsert(uri, makeInsertItems(songs, offSet, 1000, base)); @@ -168,6 +187,17 @@ public static void removeFromPlaylist(@NonNull final Context context, @NonNull f } } + public static void removeFromPlaylist(@NonNull final Context context, @NonNull final Song song, int playlistId, int startID) { + Uri uri = MediaStore.Audio.Playlists.Members.getContentUri( + "external", playlistId); + String selection = MediaStore.Audio.Playlists.Members.AUDIO_ID + " =? AND " + MediaStore.Audio.Playlists.Members.PLAY_ORDER + " >=?"; + String[] selectionArgs = new String[]{String.valueOf(song.id), String.valueOf(startID)}; + try { + context.getContentResolver().delete(uri, selection, selectionArgs); + } catch (SecurityException ignored) { + } + } + public static void removeFromPlaylist(@NonNull final Context context, @NonNull final List songs) { final int playlistId = songs.get(0).playlistId; Uri uri = MediaStore.Audio.Playlists.Members.getContentUri( diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml old mode 100644 new mode 100755 index 627d371b7..a7e57fe77 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -92,6 +92,9 @@ Clear playlist Save as file "Add to playlist" + "Duplicate in Playlist" + %1$s as a duplicate to the playlist?]]> + Do not add Shuffle all Shuffle album Shuffle artist From 197c41f3714211e7b62f488f5e07eac90f828a32 Mon Sep 17 00:00:00 2001 From: thecorkscrew <5bremer@informatik.uni-hamburg.de> Date: Fri, 15 Dec 2017 23:29:35 +0100 Subject: [PATCH 2/3] Handling cancellation of the dialog --- .../dialogs/AddDuplicateToPlaylistDialog.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java index b57a3ffcc..6c675634b 100755 --- a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java +++ b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java @@ -1,10 +1,12 @@ package com.kabouzeid.gramophone.dialogs; import android.app.Dialog; +import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.text.Html; +import android.util.Log; import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog; @@ -18,7 +20,7 @@ */ public class AddDuplicateToPlaylistDialog extends DialogFragment { - private int selection = 2; + private int selection = 0; private SelectionListener selListener; @@ -55,7 +57,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { //noinspection unchecked final ArrayList songs = getArguments().getParcelableArrayList("songs"); int title; - CharSequence content; + final CharSequence content; title = R.string.add_duplicate_title; content = Html.fromHtml(getString(R.string.add_duplicate_msg, songs.get(0).title)); return new MaterialDialog.Builder(getActivity()) @@ -68,7 +70,6 @@ public Dialog onCreateDialog(Bundle savedInstanceState) { public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { if (getActivity() == null) return; selection = 0; - selListener.selectedOption(0); } }) .onPositive(new MaterialDialog.SingleButtonCallback() { @@ -76,12 +77,17 @@ public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) { if (getActivity() == null) return; selection = 1; - selListener.selectedOption(1); } }) .build(); } + @Override + public void onDismiss(DialogInterface dialog) { + super.onDismiss(dialog); + selListener.selectedOption(selection); + } + public int getSelection() { return selection; } From caf94009d7591f6b55bd2c29fa9d3f4391437e19 Mon Sep 17 00:00:00 2001 From: thecorkscrew <5bremer@informatik.uni-hamburg.de> Date: Fri, 15 Dec 2017 23:39:12 +0100 Subject: [PATCH 3/3] Import-cleanup --- .../gramophone/dialogs/AddDuplicateToPlaylistDialog.java | 1 - 1 file changed, 1 deletion(-) diff --git a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java index 6c675634b..a613e48fd 100755 --- a/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java +++ b/app/src/main/java/com/kabouzeid/gramophone/dialogs/AddDuplicateToPlaylistDialog.java @@ -6,7 +6,6 @@ import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.text.Html; -import android.util.Log; import com.afollestad.materialdialogs.DialogAction; import com.afollestad.materialdialogs.MaterialDialog;