2 * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3 * (the "License"); you may not use this file except in compliance with the
4 * License. You may obtain a copy of the License at
5 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6 * or agreed to in writing, software distributed under the License is
7 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8 * KIND, either express or implied. See the License for the specific language
9 * governing permissions and limitations under the License.
12 package com.andrew.apollo.menu;
14 import android.app.Dialog;
15 import android.content.ContentResolver;
16 import android.database.Cursor;
17 import android.os.Bundle;
18 import android.provider.MediaStore;
20 import com.andrew.apollo.R;
21 import com.andrew.apollo.format.Capitalize;
22 import com.andrew.apollo.utils.MusicUtils;
25 * @author Andrew Neal (andrewdneal@gmail.com) TODO - The playlist names are
26 * automatically capitalized to help when you want to play one via voice
27 * actions, but it really needs to work either way. As in, capitalized
30 public class CreateNewPlaylist extends BasePlaylistDialog {
33 private long[] mPlaylistList = new long[] {};
36 * @param list The list of tracks to add to the playlist
37 * @return A new instance of this dialog.
39 public static CreateNewPlaylist getInstance(final long[] list) {
40 final CreateNewPlaylist frag = new CreateNewPlaylist();
41 final Bundle args = new Bundle();
42 args.putLongArray("playlist_list", list);
43 frag.setArguments(args);
51 public void onSaveInstanceState(final Bundle outcicle) {
52 outcicle.putString("defaultname", mPlaylist.getText().toString());
59 public void initObjects(final Bundle savedInstanceState) {
60 mPlaylistList = getArguments().getLongArray("playlist_list");
61 mDefaultname = savedInstanceState != null ? savedInstanceState.getString("defaultname")
63 if (mDefaultname == null) {
64 getDialog().dismiss();
67 final String prromptformat = getString(R.string.create_playlist_prompt);
68 mPrompt = String.format(prromptformat, mDefaultname);
72 public void onSaveClick() {
73 final String playlistName = mPlaylist.getText().toString();
74 if (playlistName != null && playlistName.length() > 0) {
75 final int playlistId = (int)MusicUtils.getIdForPlaylist(getActivity(),
77 if (playlistId >= 0) {
78 MusicUtils.clearPlaylist(getActivity(), playlistId);
79 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, playlistId);
81 final long newId = MusicUtils.createPlaylist(getActivity(),
82 Capitalize.capitalize(playlistName));
83 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, newId);
86 getDialog().dismiss();
94 public void onTextChangedListener() {
95 final String playlistName = mPlaylist.getText().toString();
96 mSaveButton = mPlaylistDialog.getButton(Dialog.BUTTON_POSITIVE);
97 if (mSaveButton == null) {
100 if (playlistName.trim().length() == 0) {
101 mSaveButton.setEnabled(false);
103 mSaveButton.setEnabled(true);
104 if (MusicUtils.getIdForPlaylist(getActivity(), playlistName) >= 0) {
105 mSaveButton.setText(R.string.overwrite);
107 mSaveButton.setText(R.string.save);
112 private String makePlaylistName() {
113 final String template = getString(R.string.new_playlist_name_template);
115 final String[] projection = new String[] {
116 MediaStore.Audio.Playlists.NAME
118 final ContentResolver resolver = getActivity().getContentResolver();
119 final String selection = MediaStore.Audio.Playlists.NAME + " != ''";
120 Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection,
121 selection, null, MediaStore.Audio.Playlists.NAME);
122 if (cursor == null) {
126 String suggestedname;
127 suggestedname = String.format(template, num++);
128 boolean done = false;
131 cursor.moveToFirst();
132 while (!cursor.isAfterLast()) {
133 final String playlistname = cursor.getString(0);
134 if (playlistname.compareToIgnoreCase(suggestedname) == 0) {
135 suggestedname = String.format(template, num++);
143 return suggestedname;