OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / packages / apps / Music / src / com / android / music / CreatePlaylist.java
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.music;
18
19 import android.app.Activity;
20 import android.content.ContentResolver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Intent;
24 import android.database.Cursor;
25 import android.media.AudioManager;
26 import android.net.Uri;
27 import android.os.Bundle;
28 import android.provider.MediaStore;
29 import android.text.Editable;
30 import android.text.TextWatcher;
31 import android.view.View;
32 import android.view.Window;
33 import android.view.WindowManager;
34 import android.widget.Button;
35 import android.widget.EditText;
36 import android.widget.TextView;
37
38 public class CreatePlaylist extends Activity
39 {
40     private EditText mPlaylist;
41     private TextView mPrompt;
42     private Button mSaveButton;
43
44     @Override
45     public void onCreate(Bundle icicle) {
46         super.onCreate(icicle);
47         setVolumeControlStream(AudioManager.STREAM_MUSIC);
48
49         requestWindowFeature(Window.FEATURE_NO_TITLE);
50         setContentView(R.layout.create_playlist);
51         getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
52                                     WindowManager.LayoutParams.WRAP_CONTENT);
53
54         mPrompt = (TextView)findViewById(R.id.prompt);
55         mPlaylist = (EditText)findViewById(R.id.playlist);
56         mSaveButton = (Button) findViewById(R.id.create);
57         mSaveButton.setOnClickListener(mOpenClicked);
58
59         ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {
60             public void onClick(View v) {
61                 finish();
62             }
63         });
64         
65         String defaultname = icicle != null ? icicle.getString("defaultname") : makePlaylistName();
66         if (defaultname == null) {
67             finish();
68             return;
69         }
70         String promptformat = getString(R.string.create_playlist_create_text_prompt);
71         String prompt = String.format(promptformat, defaultname);
72         mPrompt.setText(prompt);
73         mPlaylist.setText(defaultname);
74         mPlaylist.setSelection(defaultname.length());
75         mPlaylist.addTextChangedListener(mTextWatcher);
76     }
77     
78     TextWatcher mTextWatcher = new TextWatcher() {
79         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
80             // don't care about this one
81         }
82         public void onTextChanged(CharSequence s, int start, int before, int count) {
83             String newText = mPlaylist.getText().toString();
84             if (newText.trim().length() == 0) {
85                 mSaveButton.setEnabled(false);
86             } else {
87                 mSaveButton.setEnabled(true);
88                 // check if playlist with current name exists already, and warn the user if so.
89                 if (idForplaylist(newText) >= 0) {
90                     mSaveButton.setText(R.string.create_playlist_overwrite_text);
91                 } else {
92                     mSaveButton.setText(R.string.create_playlist_create_text);
93                 }
94             }
95         };
96         public void afterTextChanged(Editable s) {
97             // don't care about this one
98         }
99     };
100     
101     private int idForplaylist(String name) {
102         Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
103                 new String[] { MediaStore.Audio.Playlists._ID },
104                 MediaStore.Audio.Playlists.NAME + "=?",
105                 new String[] { name },
106                 MediaStore.Audio.Playlists.NAME);
107         int id = -1;
108         if (c != null) {
109             c.moveToFirst();
110             if (!c.isAfterLast()) {
111                 id = c.getInt(0);
112             }
113             c.close();
114         }
115         return id;
116     }
117     
118     @Override
119     public void onSaveInstanceState(Bundle outcicle) {
120         outcicle.putString("defaultname", mPlaylist.getText().toString());
121     }
122     
123     @Override
124     public void onResume() {
125         super.onResume();
126     }
127
128     private String makePlaylistName() {
129
130         String template = getString(R.string.new_playlist_name_template);
131         int num = 1;
132
133         String[] cols = new String[] {
134                 MediaStore.Audio.Playlists.NAME
135         };
136         ContentResolver resolver = getContentResolver();
137         String whereclause = MediaStore.Audio.Playlists.NAME + " != ''";
138         Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
139             cols, whereclause, null,
140             MediaStore.Audio.Playlists.NAME);
141
142         if (c == null) {
143             return null;
144         }
145         
146         String suggestedname;
147         suggestedname = String.format(template, num++);
148         
149         // Need to loop until we've made 1 full pass through without finding a match.
150         // Looping more than once shouldn't happen very often, but will happen if
151         // you have playlists named "New Playlist 1"/10/2/3/4/5/6/7/8/9, where
152         // making only one pass would result in "New Playlist 10" being erroneously
153         // picked for the new name.
154         boolean done = false;
155         while (!done) {
156             done = true;
157             c.moveToFirst();
158             while (! c.isAfterLast()) {
159                 String playlistname = c.getString(0);
160                 if (playlistname.compareToIgnoreCase(suggestedname) == 0) {
161                     suggestedname = String.format(template, num++);
162                     done = false;
163                 }
164                 c.moveToNext();
165             }
166         }
167         c.close();
168         return suggestedname;
169     }
170     
171     private View.OnClickListener mOpenClicked = new View.OnClickListener() {
172         public void onClick(View v) {
173             String name = mPlaylist.getText().toString();
174             if (name != null && name.length() > 0) {
175                 ContentResolver resolver = getContentResolver();
176                 int id = idForplaylist(name);
177                 Uri uri;
178                 if (id >= 0) {
179                     uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id);
180                     MusicUtils.clearPlaylist(CreatePlaylist.this, id);
181                 } else {
182                     ContentValues values = new ContentValues(1);
183                     values.put(MediaStore.Audio.Playlists.NAME, name);
184                     uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values);
185                 }
186                 setResult(RESULT_OK, (new Intent()).setData(uri));
187                 finish();
188             }
189         }
190     };
191 }