Allow playlists to be renamed

This commit is contained in:
Christopher Eby 2010-05-23 11:02:52 -05:00
parent ec017c8d46
commit 0e8fb9cb29
5 changed files with 93 additions and 17 deletions

View File

@ -27,9 +27,7 @@
android:singleLine="true"
android:layout_marginBottom="8dip"
android:layout_marginLeft="8dip"
android:layout_marginRight="8dip">
<requestFocus />
</EditText>
android:layout_marginRight="8dip" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"

View File

@ -47,6 +47,7 @@
<string name="enqueue">Enqueue</string>
<string name="play">Play</string>
<string name="edit">Edit</string>
<string name="rename">Rename</string>
<string name="add_to_playlist">Add to Playlist...</string>
<string name="new_playlist">New Playlist...</string>
<string name="expand">Expand</string>

View File

@ -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);
}
}
/**

View File

@ -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);
}
}

View File

@ -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);
}