OSDN Git Service

84a72c4941abcd3040f750314b82866cae234583
[android-x86/packages-apps-Eleven.git] / src / com / cyanogenmod / eleven / menu / CreateNewPlaylist.java
1 /*
2  * Copyright (C) 2012 Andrew Neal
3  * Copyright (C) 2014 The CyanogenMod Project
4  * Licensed under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
8  * or agreed to in writing, software distributed under the License is
9  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
10  * KIND, either express or implied. See the License for the specific language
11  * governing permissions and limitations under the License.
12  */
13
14 package com.cyanogenmod.eleven.menu;
15
16 import android.app.Dialog;
17 import android.content.ContentResolver;
18 import android.database.Cursor;
19 import android.os.Bundle;
20 import android.provider.MediaStore;
21
22 import com.cyanogenmod.eleven.R;
23 import com.cyanogenmod.eleven.format.Capitalize;
24 import com.cyanogenmod.eleven.utils.MusicUtils;
25
26 /**
27  * @author Andrew Neal (andrewdneal@gmail.com) TODO - The playlist names are
28  *         automatically capitalized to help when you want to play one via voice
29  *         actions, but it really needs to work either way. As in, capitalized
30  *         or not.
31  */
32 public class CreateNewPlaylist extends BasePlaylistDialog {
33
34     // The playlist list
35     private long[] mPlaylistList = new long[] {};
36
37     /**
38      * @param list The list of tracks to add to the playlist
39      * @return A new instance of this dialog.
40      */
41     public static CreateNewPlaylist getInstance(final long[] list) {
42         final CreateNewPlaylist frag = new CreateNewPlaylist();
43         final Bundle args = new Bundle();
44         args.putLongArray("playlist_list", list);
45         frag.setArguments(args);
46         return frag;
47     }
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public void onSaveInstanceState(final Bundle outcicle) {
54         outcicle.putString("defaultname", mPlaylist.getText().toString());
55     }
56
57     /**
58      * {@inheritDoc}
59      */
60     @Override
61     public void initObjects(final Bundle savedInstanceState) {
62         mPlaylistList = getArguments().getLongArray("playlist_list");
63         mDefaultname = savedInstanceState != null ? savedInstanceState.getString("defaultname")
64                 : makePlaylistName();
65         if (mDefaultname == null) {
66             getDialog().dismiss();
67             return;
68         }
69         final String prromptformat = getString(R.string.create_playlist_prompt);
70         mPrompt = String.format(prromptformat, mDefaultname);
71     }
72
73     @Override
74     public void onSaveClick() {
75         final String playlistName = mPlaylist.getText().toString();
76         if (playlistName != null && playlistName.length() > 0) {
77             final int playlistId = (int)MusicUtils.getIdForPlaylist(getActivity(),
78                     playlistName);
79             if (playlistId >= 0) {
80                 MusicUtils.clearPlaylist(getActivity(), playlistId);
81                 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, playlistId);
82             } else {
83                 final long newId = MusicUtils.createPlaylist(getActivity(),
84                         Capitalize.capitalize(playlistName));
85                 MusicUtils.addToPlaylist(getActivity(), mPlaylistList, newId);
86             }
87             getDialog().dismiss();
88         }
89     }
90
91     /**
92      * {@inheritDoc}
93      */
94     @Override
95     public void onTextChangedListener() {
96         final String playlistName = mPlaylist.getText().toString();
97         mSaveButton = mPlaylistDialog.getButton(Dialog.BUTTON_POSITIVE);
98         if (mSaveButton == null) {
99             return;
100         }
101         if (playlistName.trim().length() == 0) {
102             mSaveButton.setEnabled(false);
103         } else {
104             mSaveButton.setEnabled(true);
105             if (MusicUtils.getIdForPlaylist(getActivity(), playlistName) >= 0) {
106                 mSaveButton.setText(R.string.overwrite);
107             } else {
108                 mSaveButton.setText(R.string.save);
109             }
110         }
111     }
112
113     private String makePlaylistName() {
114         final String template = getString(R.string.new_playlist_name_template);
115         int num = 1;
116         final String[] projection = new String[] {
117             MediaStore.Audio.Playlists.NAME
118         };
119         final ContentResolver resolver = getActivity().getContentResolver();
120         final String selection = MediaStore.Audio.Playlists.NAME + " != ''";
121         Cursor cursor = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, projection,
122                 selection, null, MediaStore.Audio.Playlists.NAME);
123         if (cursor == null) {
124             return null;
125         }
126
127         String suggestedname;
128         suggestedname = String.format(template, num++);
129         boolean done = false;
130         while (!done) {
131             done = true;
132             cursor.moveToFirst();
133             while (!cursor.isAfterLast()) {
134                 final String playlistname = cursor.getString(0);
135                 if (playlistname.compareToIgnoreCase(suggestedname) == 0) {
136                     suggestedname = String.format(template, num++);
137                     done = false;
138                 }
139                 cursor.moveToNext();
140             }
141         }
142         cursor.close();
143         cursor = null;
144         return suggestedname;
145     }
146 }