Fix favorite toggling
This commit is contained in:
parent
c77e6f132a
commit
1f37982536
4 changed files with 57 additions and 40 deletions
|
|
@ -6,6 +6,7 @@ import android.support.annotation.NonNull;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.v4.app.DialogFragment;
|
import android.support.v4.app.DialogFragment;
|
||||||
import android.text.InputType;
|
import android.text.InputType;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import com.afollestad.materialdialogs.MaterialDialog;
|
import com.afollestad.materialdialogs.MaterialDialog;
|
||||||
import com.kabouzeid.gramophone.R;
|
import com.kabouzeid.gramophone.R;
|
||||||
|
|
@ -19,6 +20,8 @@ import java.util.ArrayList;
|
||||||
*/
|
*/
|
||||||
public class CreatePlaylistDialog extends DialogFragment {
|
public class CreatePlaylistDialog extends DialogFragment {
|
||||||
|
|
||||||
|
private static final String SONGS = "songs";
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static CreatePlaylistDialog create() {
|
public static CreatePlaylistDialog create() {
|
||||||
return create((Song) null);
|
return create((Song) null);
|
||||||
|
|
@ -36,7 +39,7 @@ public class CreatePlaylistDialog extends DialogFragment {
|
||||||
public static CreatePlaylistDialog create(ArrayList<Song> songs) {
|
public static CreatePlaylistDialog create(ArrayList<Song> songs) {
|
||||||
CreatePlaylistDialog dialog = new CreatePlaylistDialog();
|
CreatePlaylistDialog dialog = new CreatePlaylistDialog();
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putParcelableArrayList("songs", songs);
|
args.putParcelableArrayList(SONGS, songs);
|
||||||
dialog.setArguments(args);
|
dialog.setArguments(args);
|
||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
@ -53,20 +56,27 @@ public class CreatePlaylistDialog extends DialogFragment {
|
||||||
InputType.TYPE_TEXT_FLAG_CAP_WORDS)
|
InputType.TYPE_TEXT_FLAG_CAP_WORDS)
|
||||||
.input(R.string.playlist_name_empty, 0, false, new MaterialDialog.InputCallback() {
|
.input(R.string.playlist_name_empty, 0, false, new MaterialDialog.InputCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onInput(MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
public void onInput(@NonNull MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||||
if (getActivity() == null)
|
if (getActivity() == null)
|
||||||
return;
|
return;
|
||||||
if (!charSequence.toString().trim().isEmpty()) {
|
final String name = charSequence.toString().trim();
|
||||||
final int playlistId = PlaylistsUtil.createPlaylist(getActivity(), charSequence.toString());
|
if (!name.isEmpty()) {
|
||||||
if (playlistId != -1 && getActivity() != null) {
|
if (!PlaylistsUtil.doesPlaylistExist(getActivity(), name)) {
|
||||||
//noinspection unchecked
|
final int playlistId = PlaylistsUtil.createPlaylist(getActivity(), name);
|
||||||
ArrayList<Song> songs = getArguments().getParcelableArrayList("songs");
|
if (getActivity() != null) {
|
||||||
if (songs != null) {
|
//noinspection unchecked
|
||||||
PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true);
|
ArrayList<Song> songs = getArguments().getParcelableArrayList(SONGS);
|
||||||
|
if (songs != null && !songs.isEmpty()) {
|
||||||
|
PlaylistsUtil.addToPlaylist(getActivity(), songs, playlistId, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
Toast.makeText(getActivity(), getActivity().getResources().getString(
|
||||||
|
R.string.playlist_exists, name), Toast.LENGTH_SHORT).show();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}).build();
|
})
|
||||||
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -15,11 +15,13 @@ import com.kabouzeid.gramophone.util.PlaylistsUtil;
|
||||||
*/
|
*/
|
||||||
public class RenamePlaylistDialog extends DialogFragment {
|
public class RenamePlaylistDialog extends DialogFragment {
|
||||||
|
|
||||||
|
private static final String PLAYLIST_ID = "playlist_id";
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public static RenamePlaylistDialog create(long playlistId) {
|
public static RenamePlaylistDialog create(long playlistId) {
|
||||||
RenamePlaylistDialog dialog = new RenamePlaylistDialog();
|
RenamePlaylistDialog dialog = new RenamePlaylistDialog();
|
||||||
Bundle args = new Bundle();
|
Bundle args = new Bundle();
|
||||||
args.putLong("playlist_id", playlistId);
|
args.putLong(PLAYLIST_ID, playlistId);
|
||||||
dialog.setArguments(args);
|
dialog.setArguments(args);
|
||||||
return dialog;
|
return dialog;
|
||||||
}
|
}
|
||||||
|
|
@ -27,7 +29,7 @@ public class RenamePlaylistDialog extends DialogFragment {
|
||||||
@NonNull
|
@NonNull
|
||||||
@Override
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
long playlistId = getArguments().getLong("playlist_id");
|
long playlistId = getArguments().getLong(PLAYLIST_ID);
|
||||||
return new MaterialDialog.Builder(getActivity())
|
return new MaterialDialog.Builder(getActivity())
|
||||||
.title(R.string.rename_playlist_title)
|
.title(R.string.rename_playlist_title)
|
||||||
.positiveText(R.string.rename_action)
|
.positiveText(R.string.rename_action)
|
||||||
|
|
@ -38,10 +40,11 @@ public class RenamePlaylistDialog extends DialogFragment {
|
||||||
.input(getString(R.string.playlist_name_empty), PlaylistsUtil.getNameForPlaylist(getActivity(), playlistId), false,
|
.input(getString(R.string.playlist_name_empty), PlaylistsUtil.getNameForPlaylist(getActivity(), playlistId), false,
|
||||||
new MaterialDialog.InputCallback() {
|
new MaterialDialog.InputCallback() {
|
||||||
@Override
|
@Override
|
||||||
public void onInput(MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
public void onInput(@NonNull MaterialDialog materialDialog, @NonNull CharSequence charSequence) {
|
||||||
if (!charSequence.toString().trim().equals("")) {
|
final String name = charSequence.toString().trim();
|
||||||
long playlistId = getArguments().getLong("playlist_id");
|
if (!name.isEmpty()) {
|
||||||
PlaylistsUtil.renamePlaylist(getActivity(), playlistId, charSequence.toString());
|
long playlistId = getArguments().getLong(PLAYLIST_ID);
|
||||||
|
PlaylistsUtil.renamePlaylist(getActivity(), playlistId, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,6 @@ import java.util.ArrayList;
|
||||||
* @author Karim Abou Zeid (kabouzeid)
|
* @author Karim Abou Zeid (kabouzeid)
|
||||||
*/
|
*/
|
||||||
public class PlaylistMenuHelper {
|
public class PlaylistMenuHelper {
|
||||||
public static final int MENU_RES = R.menu.menu_item_playlist;
|
|
||||||
|
|
||||||
public static boolean handleMenuClick(@NonNull AppCompatActivity activity, @NonNull final Playlist playlist, @NonNull MenuItem item) {
|
public static boolean handleMenuClick(@NonNull AppCompatActivity activity, @NonNull final Playlist playlist, @NonNull MenuItem item) {
|
||||||
switch (item.getItemId()) {
|
switch (item.getItemId()) {
|
||||||
case R.id.action_play:
|
case R.id.action_play:
|
||||||
|
|
@ -50,7 +48,8 @@ public class PlaylistMenuHelper {
|
||||||
DeletePlaylistDialog.create(playlist).show(activity.getSupportFragmentManager(), "DELETE_PLAYLIST");
|
DeletePlaylistDialog.create(playlist).show(activity.getSupportFragmentManager(), "DELETE_PLAYLIST");
|
||||||
return true;
|
return true;
|
||||||
case R.id.action_save_playlist:
|
case R.id.action_save_playlist:
|
||||||
@SuppressLint("ShowToast") final Toast toast = Toast.makeText(activity, R.string.saving_to_file, Toast.LENGTH_SHORT);
|
@SuppressLint("ShowToast")
|
||||||
|
final Toast toast = Toast.makeText(activity, R.string.saving_to_file, Toast.LENGTH_SHORT);
|
||||||
new AsyncTask<Context, Void, String>() {
|
new AsyncTask<Context, Void, String>() {
|
||||||
@Override
|
@Override
|
||||||
protected void onPreExecute() {
|
protected void onPreExecute() {
|
||||||
|
|
|
||||||
|
|
@ -31,20 +31,15 @@ import static android.provider.MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
|
||||||
public class PlaylistsUtil {
|
public class PlaylistsUtil {
|
||||||
|
|
||||||
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
|
public static boolean doesPlaylistExist(@NonNull final Context context, final int playlistId) {
|
||||||
if (playlistId == -1) {
|
return playlistId != -1 && doesPlaylistExist(context,
|
||||||
return false;
|
MediaStore.Audio.Playlists._ID + "=?",
|
||||||
}
|
new String[]{String.valueOf(playlistId)});
|
||||||
|
}
|
||||||
|
|
||||||
Cursor cursor = context.getContentResolver().query(
|
public static boolean doesPlaylistExist(@NonNull final Context context, final String name) {
|
||||||
MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
|
return doesPlaylistExist(context,
|
||||||
new String[]{}, null, null, null);
|
MediaStore.Audio.PlaylistsColumns.NAME + "=?",
|
||||||
|
new String[]{name});
|
||||||
if (cursor == null || cursor.getCount() == 0) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
cursor.close();
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int createPlaylist(@NonNull final Context context, @Nullable final String name) {
|
public static int createPlaylist(@NonNull final Context context, @Nullable final String name) {
|
||||||
|
|
@ -52,7 +47,9 @@ public class PlaylistsUtil {
|
||||||
if (name != null && name.length() > 0) {
|
if (name != null && name.length() > 0) {
|
||||||
try {
|
try {
|
||||||
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||||
new String[]{MediaStore.Audio.Playlists._ID}, MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name}, null);
|
new String[]{MediaStore.Audio.Playlists._ID},
|
||||||
|
MediaStore.Audio.PlaylistsColumns.NAME + "=?", new String[]{name},
|
||||||
|
null);
|
||||||
if (cursor == null || cursor.getCount() < 1) {
|
if (cursor == null || cursor.getCount() < 1) {
|
||||||
final ContentValues values = new ContentValues(1);
|
final ContentValues values = new ContentValues(1);
|
||||||
values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
|
values.put(MediaStore.Audio.PlaylistsColumns.NAME, name);
|
||||||
|
|
@ -60,7 +57,7 @@ public class PlaylistsUtil {
|
||||||
EXTERNAL_CONTENT_URI,
|
EXTERNAL_CONTENT_URI,
|
||||||
values);
|
values);
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
// necessary because somehow the MediaStoreObserver is not notified when adding a playlist
|
// Necessary because somehow the MediaStoreObserver is not notified when adding a playlist
|
||||||
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
|
context.getContentResolver().notifyChange(Uri.parse("content://media"), null);
|
||||||
Toast.makeText(context, context.getResources().getString(
|
Toast.makeText(context, context.getResources().getString(
|
||||||
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
|
R.string.created_playlist_x, name), Toast.LENGTH_SHORT).show();
|
||||||
|
|
@ -69,10 +66,7 @@ public class PlaylistsUtil {
|
||||||
} else {
|
} else {
|
||||||
// Playlist exists
|
// Playlist exists
|
||||||
if (cursor.moveToFirst()) {
|
if (cursor.moveToFirst()) {
|
||||||
Toast.makeText(context, context.getResources().getString(
|
id = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Playlists._ID));
|
||||||
R.string.playlist_exists, name), Toast.LENGTH_SHORT).show();
|
|
||||||
cursor.close();
|
|
||||||
return -1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
|
|
@ -231,8 +225,7 @@ public class PlaylistsUtil {
|
||||||
|
|
||||||
public static String getNameForPlaylist(@NonNull final Context context, final long id) {
|
public static String getNameForPlaylist(@NonNull final Context context, final long id) {
|
||||||
try {
|
try {
|
||||||
Cursor cursor = context.getContentResolver().query(
|
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||||
EXTERNAL_CONTENT_URI,
|
|
||||||
new String[]{MediaStore.Audio.PlaylistsColumns.NAME},
|
new String[]{MediaStore.Audio.PlaylistsColumns.NAME},
|
||||||
BaseColumns._ID + "=?",
|
BaseColumns._ID + "=?",
|
||||||
new String[]{String.valueOf(id)},
|
new String[]{String.valueOf(id)},
|
||||||
|
|
@ -254,4 +247,16 @@ public class PlaylistsUtil {
|
||||||
public static File savePlaylist(Context context, Playlist playlist) throws IOException {
|
public static File savePlaylist(Context context, Playlist playlist) throws IOException {
|
||||||
return M3UWriter.write(context, new File(Environment.getExternalStorageDirectory(), "Playlists"), playlist);
|
return M3UWriter.write(context, new File(Environment.getExternalStorageDirectory(), "Playlists"), playlist);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean doesPlaylistExist(@NonNull Context context, @NonNull final String selection, @NonNull final String[] values) {
|
||||||
|
Cursor cursor = context.getContentResolver().query(EXTERNAL_CONTENT_URI,
|
||||||
|
new String[]{}, selection, values, null);
|
||||||
|
|
||||||
|
boolean exists = false;
|
||||||
|
if (cursor != null) {
|
||||||
|
exists = cursor.getCount() != 0;
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
return exists;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue