2 * Copyright (C) 2007 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.music;
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;
38 public class CreatePlaylist extends Activity
40 private EditText mPlaylist;
41 private TextView mPrompt;
42 private Button mSaveButton;
45 public void onCreate(Bundle icicle) {
46 super.onCreate(icicle);
47 setVolumeControlStream(AudioManager.STREAM_MUSIC);
49 requestWindowFeature(Window.FEATURE_NO_TITLE);
50 setContentView(R.layout.create_playlist);
51 getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
52 WindowManager.LayoutParams.WRAP_CONTENT);
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);
59 ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {
60 public void onClick(View v) {
65 String defaultname = icicle != null ? icicle.getString("defaultname") : makePlaylistName();
66 if (defaultname == null) {
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);
78 TextWatcher mTextWatcher = new TextWatcher() {
79 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
80 // don't care about this one
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);
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);
92 mSaveButton.setText(R.string.create_playlist_create_text);
96 public void afterTextChanged(Editable s) {
97 // don't care about this one
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);
110 if (!c.isAfterLast()) {
119 public void onSaveInstanceState(Bundle outcicle) {
120 outcicle.putString("defaultname", mPlaylist.getText().toString());
124 public void onResume() {
128 private String makePlaylistName() {
130 String template = getString(R.string.new_playlist_name_template);
133 String[] cols = new String[] {
134 MediaStore.Audio.Playlists.NAME
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);
146 String suggestedname;
147 suggestedname = String.format(template, num++);
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;
158 while (! c.isAfterLast()) {
159 String playlistname = c.getString(0);
160 if (playlistname.compareToIgnoreCase(suggestedname) == 0) {
161 suggestedname = String.format(template, num++);
168 return suggestedname;
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);
179 uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id);
180 MusicUtils.clearPlaylist(CreatePlaylist.this, id);
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);
186 setResult(RESULT_OK, (new Intent()).setData(uri));