From 1936e42d3660c2db0f517aac56c55ef8c3e11819 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Sat, 25 Jun 2016 20:48:24 +0200 Subject: [PATCH] Give the playlist-dialog its own class so it does not need to abuse the SubMenu class --- .../android/vanilla/PlaylistDialog.java | 111 ++++++++++++++++++ .../vanilla/SlidingPlaybackActivity.java | 66 +++++------ 2 files changed, 143 insertions(+), 34 deletions(-) create mode 100644 src/ch/blinkenlights/android/vanilla/PlaylistDialog.java diff --git a/src/ch/blinkenlights/android/vanilla/PlaylistDialog.java b/src/ch/blinkenlights/android/vanilla/PlaylistDialog.java new file mode 100644 index 00000000..27509e65 --- /dev/null +++ b/src/ch/blinkenlights/android/vanilla/PlaylistDialog.java @@ -0,0 +1,111 @@ +/* + * Copyright (C) 2016 Adrian Ulrich + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package ch.blinkenlights.android.vanilla; + +import android.app.AlertDialog; +import android.app.Dialog; +import android.app.DialogFragment; +import android.content.Context; +import android.content.DialogInterface; +import android.content.Intent; +import android.database.Cursor; +import android.os.Bundle; + + +public class PlaylistDialog extends DialogFragment + implements DialogInterface.OnClickListener +{ + /** + * A class implementing our callback interface + */ + private Callback mCallback; + /** + * The intent to act on + */ + private Intent mIntent; + /** + * Array of all found playlist names + */ + private String[] mItemName; + /** + * Array of all found playlist values + */ + private long[] mItemValue; + /** + * Magic value, indicating that a new + * playlist shall be created + */ + private final int VALUE_CREATE_PLAYLIST = -1; + /** + * Our callback interface + */ + public interface Callback { + void appendToPlaylistFromIntent(Intent intent); + void createNewPlaylistFromIntent(Intent intent); + } + + + PlaylistDialog(Callback callback, Intent intent) { + mCallback = callback; + mIntent = intent; + } + + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + Cursor cursor = Playlist.queryPlaylists(getActivity().getContentResolver()); + if (cursor == null) + return null; + + int count = cursor.getCount(); + mItemName = new String[1+count]; + mItemValue = new long[1+count]; + + // Index 0 is always 'New Playlist...' + mItemName[0] = getResources().getString(R.string.new_playlist); + mItemValue[0] = VALUE_CREATE_PLAYLIST; + + for (int i = 0 ; i < count; i++) { + cursor.moveToPosition(i); + mItemValue[1+i] = cursor.getLong(0); + mItemName[1+i] = cursor.getString(1); + } + + // All names are now known: we can show the dialog + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.add_to_playlist) + .setItems(mItemName, this); + return builder.create(); + } + + @Override + public void onClick(DialogInterface dialog, int which) { + if (mItemValue[which] == VALUE_CREATE_PLAYLIST) { + mCallback.createNewPlaylistFromIntent(mIntent); + } else { + Intent copy = new Intent(mIntent); + copy.putExtra("playlist", mItemValue[which]); + copy.putExtra("playlistName", mItemName[which]); + mCallback.appendToPlaylistFromIntent(copy); + } + } +} diff --git a/src/ch/blinkenlights/android/vanilla/SlidingPlaybackActivity.java b/src/ch/blinkenlights/android/vanilla/SlidingPlaybackActivity.java index b390d753..855831c1 100644 --- a/src/ch/blinkenlights/android/vanilla/SlidingPlaybackActivity.java +++ b/src/ch/blinkenlights/android/vanilla/SlidingPlaybackActivity.java @@ -33,7 +33,8 @@ import java.net.InetAddress; public class SlidingPlaybackActivity extends PlaybackActivity implements SlidingView.Callback, - SeekBar.OnSeekBarChangeListener + SeekBar.OnSeekBarChangeListener, + PlaylistDialog.Callback { /** * Reference to the inflated menu @@ -146,8 +147,6 @@ public class SlidingPlaybackActivity extends PlaybackActivity } public static final int CTX_MENU_ADD_TO_PLAYLIST = 300; - private static final int CTX_MENU_NEW_PLAYLIST = 301; - private static final int CTX_MENU_SELECT_PLAYLIST = 302; @Override public boolean onContextItemSelected(MenuItem item) { @@ -157,37 +156,8 @@ public class SlidingPlaybackActivity extends PlaybackActivity final Intent intent = item.getIntent(); switch (item.getItemId()) { case CTX_MENU_ADD_TO_PLAYLIST: { - SubMenu playlistMenu = item.getSubMenu(); - playlistMenu.add(0, CTX_MENU_NEW_PLAYLIST, 0, R.string.new_playlist).setIntent(intent); - Cursor cursor = Playlist.queryPlaylists(getContentResolver()); - if (cursor != null) { - for (int i = 0, count = cursor.getCount(); i != count; ++i) { - cursor.moveToPosition(i); - long id = cursor.getLong(0); - String name = cursor.getString(1); - Intent copy = new Intent(intent); - copy.putExtra("playlist", id); - copy.putExtra("playlistName", name); - playlistMenu.add(0, CTX_MENU_SELECT_PLAYLIST, 0, name).setIntent(copy); - } - cursor.close(); - } - break; - } - case CTX_MENU_NEW_PLAYLIST: { - PlaylistTask playlistTask = new PlaylistTask(-1, null); - playlistTask.query = buildQueryFromIntent(intent, true, null); - NewPlaylistDialog dialog = new NewPlaylistDialog(this, null, R.string.create, playlistTask); - dialog.setDismissMessage(mHandler.obtainMessage(MSG_NEW_PLAYLIST, dialog)); - dialog.show(); - break; - } - case CTX_MENU_SELECT_PLAYLIST: { - long playlistId = intent.getLongExtra("playlist", -1); - String playlistName = intent.getStringExtra("playlistName"); - PlaylistTask playlistTask = new PlaylistTask(playlistId, playlistName); - playlistTask.query = buildQueryFromIntent(intent, true, null); - mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_TO_PLAYLIST, playlistTask)); + PlaylistDialog dialog = new PlaylistDialog(this, intent); + dialog.show(getFragmentManager(), "PlaylistDialog"); break; } default: @@ -196,6 +166,34 @@ public class SlidingPlaybackActivity extends PlaybackActivity return true; } + /** + * Called by PlaylistDialog.Callback to prompt for the new + * playlist name + * + * @param intent The intent holding the selected data + */ + public void createNewPlaylistFromIntent(Intent intent) { + PlaylistTask playlistTask = new PlaylistTask(-1, null); + playlistTask.query = buildQueryFromIntent(intent, true, null); + NewPlaylistDialog dialog = new NewPlaylistDialog(this, null, R.string.create, playlistTask); + dialog.setDismissMessage(mHandler.obtainMessage(MSG_NEW_PLAYLIST, dialog)); + dialog.show(); + } + + /** + * Called by PlaylistDialog.Callback to append data to + * a playlist + * + * @param intent The intent holding the selected data + */ + public void appendToPlaylistFromIntent(Intent intent) { + long playlistId = intent.getLongExtra("playlist", -1); + String playlistName = intent.getStringExtra("playlistName"); + PlaylistTask playlistTask = new PlaylistTask(playlistId, playlistName); + playlistTask.query = buildQueryFromIntent(intent, true, null); + mHandler.sendMessage(mHandler.obtainMessage(MSG_ADD_TO_PLAYLIST, playlistTask)); + } + /** * Update the seekbar progress with the current song progress. This must be * called on the UI Handler.