From 0e8fb9cb294af20fe53cac2764bedc23637cd55d Mon Sep 17 00:00:00 2001 From: Christopher Eby Date: Sun, 23 May 2010 11:02:52 -0500 Subject: [PATCH] Allow playlists to be renamed --- res/layout/new_playlist_dialog.xml | 4 +- res/values/strings.xml | 1 + src/org/kreed/vanilla/NewPlaylistDialog.java | 52 +++++++++++++++----- src/org/kreed/vanilla/Playlist.java | 22 +++++++++ src/org/kreed/vanilla/SongSelector.java | 31 ++++++++++-- 5 files changed, 93 insertions(+), 17 deletions(-) diff --git a/res/layout/new_playlist_dialog.xml b/res/layout/new_playlist_dialog.xml index 0560bed2..737c40b8 100644 --- a/res/layout/new_playlist_dialog.xml +++ b/res/layout/new_playlist_dialog.xml @@ -27,9 +27,7 @@ android:singleLine="true" android:layout_marginBottom="8dip" android:layout_marginLeft="8dip" - android:layout_marginRight="8dip"> - - + android:layout_marginRight="8dip" /> Enqueue Play Edit + Rename Add to Playlist... New Playlist... Expand diff --git a/src/org/kreed/vanilla/NewPlaylistDialog.java b/src/org/kreed/vanilla/NewPlaylistDialog.java index 709bb57a..fe8a82c4 100644 --- a/src/org/kreed/vanilla/NewPlaylistDialog.java +++ b/src/org/kreed/vanilla/NewPlaylistDialog.java @@ -46,10 +46,31 @@ public class NewPlaylistDialog extends Dialog implements TextWatcher, View.OnCli * was clicked. */ private boolean mAccepted; + /** + * The text to display initially. When the EditText contains this text, the + * positive button will be disabled. + */ + private String mInitialText; + /** + * The resource containing the string describing the default positive + * action (e.g. "Create" or "Rename"). + */ + private int mActionRes; - public NewPlaylistDialog(Context context) + /** + * Create a NewPlaylistDialog. + * + * @param context A Context to use. + * @param initialText The text to show initially. The positive button is + * disabled when the EditText contains this text. + * @param actionText A string resource describing the default positive + * action (e.g. "Create"). + */ + public NewPlaylistDialog(Context context, String initialText, int actionText) { super(context); + mInitialText = initialText; + mActionRes = actionText; } @Override @@ -61,13 +82,16 @@ public class NewPlaylistDialog extends Dialog implements TextWatcher, View.OnCli setTitle(R.string.choose_playlist_name); + mPositiveButton = (Button)findViewById(R.id.create); + mPositiveButton.setOnClickListener(this); + mPositiveButton.setText(mActionRes); + View negativeButton = findViewById(R.id.cancel); + negativeButton.setOnClickListener(this); + mText = (EditText)findViewById(R.id.playlist_name); mText.addTextChangedListener(this); - - mPositiveButton = (Button)findViewById(R.id.create); - View negativeButton = findViewById(R.id.cancel); - mPositiveButton.setOnClickListener(this); - negativeButton.setOnClickListener(this); + mText.setText(mInitialText); + mText.requestFocus(); } /** @@ -88,12 +112,18 @@ public class NewPlaylistDialog extends Dialog implements TextWatcher, View.OnCli // do nothing } - public void onTextChanged(CharSequence s, int start, int before, int count) + public void onTextChanged(CharSequence text, int start, int before, int count) { - // Update the action button based on whether there is an - // existing playlist with the given name. - int res = Playlist.getPlaylist(s.toString()) == -1 ? R.string.create : R.string.overwrite; - mPositiveButton.setText(res); + String string = text.toString(); + if (string.equals(mInitialText)) { + mPositiveButton.setEnabled(false); + } else { + mPositiveButton.setEnabled(true); + // Update the action button based on whether there is an + // existing playlist with the given name. + int res = Playlist.getPlaylist(string) == -1 ? mActionRes : R.string.overwrite; + mPositiveButton.setText(res); + } } /** diff --git a/src/org/kreed/vanilla/Playlist.java b/src/org/kreed/vanilla/Playlist.java index 4fa2ae76..7fa01744 100644 --- a/src/org/kreed/vanilla/Playlist.java +++ b/src/org/kreed/vanilla/Playlist.java @@ -171,4 +171,26 @@ public class Playlist { Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id); ContextApplication.getContext().getContentResolver().delete(uri, null, null); } + + /** + * Rename the playlist with the given id. + * + * @param id The Media.Audio.Playlists id of the playlist. + * @param newName The new name for the playlist. + */ + public static void renamePlaylist(long id, String newName) + { + long existingId = getPlaylist(newName); + // We are already called the requested name; nothing to do. + if (existingId == id) + return; + // There is already a playlist with this name. Kill it. + if (existingId != -1) + deletePlaylist(existingId); + + ContentResolver resolver = ContextApplication.getContext().getContentResolver(); + ContentValues values = new ContentValues(1); + values.put(MediaStore.Audio.Playlists.NAME, newName); + resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values, MediaStore.Audio.Playlists._ID + "=" + id, null); + } } diff --git a/src/org/kreed/vanilla/SongSelector.java b/src/org/kreed/vanilla/SongSelector.java index ba62eaf5..93ee7532 100644 --- a/src/org/kreed/vanilla/SongSelector.java +++ b/src/org/kreed/vanilla/SongSelector.java @@ -342,6 +342,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem private static final int MENU_NEW_PLAYLIST = 4; private static final int MENU_DELETE = 5; private static final int MENU_EDIT = 6; + private static final int MENU_RENAME_PLAYLIST = 7; @Override public void onCreateContextMenu(ContextMenu menu, View listView, ContextMenu.ContextMenuInfo absInfo) @@ -353,8 +354,10 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem menu.setHeaderTitle(view.getTitle()); menu.add(0, MENU_PLAY, 0, R.string.play); menu.add(0, MENU_ENQUEUE, 0, R.string.enqueue); - if (view.getMediaType() == MediaUtils.TYPE_PLAYLIST) + if (view.getMediaType() == MediaUtils.TYPE_PLAYLIST) { + menu.add(0, MENU_RENAME_PLAYLIST, 0, R.string.rename); menu.add(0, MENU_EDIT, 0, R.string.edit); + } SubMenu playlistMenu = menu.addSubMenu(0, MENU_ADD_TO_PLAYLIST, 0, R.string.add_to_playlist); if (view.hasExpanders()) menu.add(0, MENU_EXPAND, 0, R.string.expand); @@ -433,13 +436,22 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem sendSongIntent((MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView, action); break; case MENU_NEW_PLAYLIST: { - NewPlaylistDialog dialog = new NewPlaylistDialog(this); + NewPlaylistDialog dialog = new NewPlaylistDialog(this, null, R.string.create); Message message = mHandler.obtainMessage(MSG_NEW_PLAYLIST, type, mediaId); message.obj = dialog; dialog.setDismissMessage(message); dialog.show(); break; } + case MENU_RENAME_PLAYLIST: { + MediaAdapter.MediaView view = (MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView; + NewPlaylistDialog dialog = new NewPlaylistDialog(this, view.getTitle(), R.string.rename); + Message message = mHandler.obtainMessage(MSG_RENAME_PLAYLIST, view.getMediaType(), (int)view.getMediaId()); + message.obj = dialog; + dialog.setDismissMessage(message); + dialog.show(); + break; + } case MENU_DELETE: { MediaAdapter.MediaView view = (MediaAdapter.MediaView)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).targetView; type = view.getMediaType(); @@ -531,6 +543,13 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem * playlist. */ private static final int MSG_DELETE = 12; + /** + * Rename the playlist with the parameters from the given message. The + * message must contain the type and id of the media to be added in + * arg1 and arg2, respectively. The obj field must be a NewPlaylistDialog + * that the name will be taken from. + */ + private static final int MSG_RENAME_PLAYLIST = 13; @Override public boolean handleMessage(Message message) @@ -541,7 +560,7 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem Observer observer = new Observer(mHandler); resolver.registerContentObserver(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, true, observer); break; - case MSG_NEW_PLAYLIST: + case MSG_NEW_PLAYLIST: { NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj; if (dialog.isAccepted()) { String name = dialog.getText(); @@ -549,9 +568,15 @@ public class SongSelector extends PlaybackActivity implements AdapterView.OnItem addToPlaylist(playlistId, message.arg1, message.arg2, name); } break; + } case MSG_DELETE: delete(message.arg1, message.arg2, (String)message.obj); break; + case MSG_RENAME_PLAYLIST: { + NewPlaylistDialog dialog = (NewPlaylistDialog)message.obj; + if (dialog.isAccepted()) + Playlist.renamePlaylist(message.arg2, dialog.getText()); + } default: return super.handleMessage(message); }