OSDN Git Service

8f61026f241b904f7f146998b9ae86814a74581d
[android-x86/packages-apps-Eleven.git] / src / com / andrew / apollo / menu / CreateNewPlaylist.java
1 /*
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.
10  */
11
12 package com.andrew.apollo.menu;
13
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;
19
20 import com.andrew.apollo.R;
21 import com.andrew.apollo.format.Capitalize;
22 import com.andrew.apollo.utils.MusicUtils;
23
24 /**
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
28  *         or not.
29  */
30 public class CreateNewPlaylist extends BasePlaylistDialog {
31
32     // The playlist list
33     private long[] mPlaylistList = new long[] {};
34
35     /**
36      * @param list The list of tracks to add to the playlist
37      * @return A new instance of this dialog.
38      */
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);
44         return frag;
45     }
46
47     /**
48      * {@inheritDoc}
49      */
50     @Override
51     public void onSaveInstanceState(final Bundle outcicle) {
52         outcicle.putString("defaultname", mPlaylist.getText().toString());
53     }
54
55     /**
56      * {@inheritDoc}
57      */
58     @Override
59     public void initObjects(final Bundle savedInstanceState) {
60         mPlaylistList = getArguments().getLongArray("playlist_list");
61         mDefaultname = savedInstanceState != null ? savedInstanceState.getString("defaultname")
62                 : makePlaylistName();
63         if (mDefaultname == null) {
64             getDialog().dismiss();
65             return;
66         }
67         final String prromptformat = getString(R.string.create_playlist_prompt);
68         mPrompt = String.format(prromptformat, mDefaultname);
69     }
70
71     @Override
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(),
76                     playlistName);
77             if (playlistId >= 0) {
78                 MusicUtils.clearPlaylist(getActivity(), playlistId);
79                 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, playlistId);
80             } else {
81                 final long newId = MusicUtils.createPlaylist(getActivity(),
82                         Capitalize.capitalize(playlistName));
83                 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, newId);
84             }
85             closeKeyboard();
86             getDialog().dismiss();
87         }
88     }
89
90     /**
91      * {@inheritDoc}
92      */
93     @Override
94     public void onTextChangedListener() {
95         final String playlistName = mPlaylist.getText().toString();
96         mSaveButton = mPlaylistDialog.getButton(Dialog.BUTTON_POSITIVE);
97         if (mSaveButton == null) {
98             return;
99         }
100         if (playlistName.trim().length() == 0) {
101             mSaveButton.setEnabled(false);
102         } else {
103             mSaveButton.setEnabled(true);
104             if (MusicUtils.getIdForPlaylist(getActivity(), playlistName) >= 0) {
105                 mSaveButton.setText(R.string.overwrite);
106             } else {
107                 mSaveButton.setText(R.string.save);
108             }
109         }
110     }
111
112     private String makePlaylistName() {
113         final String template = getString(R.string.new_playlist_name_template);
114         int num = 1;
115         final String[] projection = new String[] {
116             MediaStore.Audio.Playlists.NAME
117         };
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) {
123             return null;
124         }
125
126         String suggestedname;
127         suggestedname = String.format(template, num++);
128         boolean done = false;
129         while (!done) {
130             done = true;
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++);
136                     done = false;
137                 }
138                 cursor.moveToNext();
139             }
140         }
141         cursor.close();
142         cursor = null;
143         return suggestedname;
144     }
145 }