CreatePlaylist.java revision 6cb8bc92e0ca524a76a6fa3f6814b43ea9a3b30d
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 17package com.android.music; 18 19import android.app.Activity; 20import android.content.ContentResolver; 21import android.content.ContentUris; 22import android.content.ContentValues; 23import android.content.Intent; 24import android.database.Cursor; 25import android.media.AudioManager; 26import android.net.Uri; 27import android.os.Bundle; 28import android.provider.MediaStore; 29import android.text.Editable; 30import android.text.TextWatcher; 31import android.util.Log; 32import android.view.KeyEvent; 33import android.view.View; 34import android.view.Window; 35import android.view.WindowManager; 36import android.widget.Button; 37import android.widget.EditText; 38import android.widget.TextView; 39 40public class CreatePlaylist extends Activity 41{ 42 private EditText mPlaylist; 43 private TextView mPrompt; 44 private Button mSaveButton; 45 46 @Override 47 public void onCreate(Bundle icicle) { 48 super.onCreate(icicle); 49 setVolumeControlStream(AudioManager.STREAM_MUSIC); 50 51 requestWindowFeature(Window.FEATURE_NO_TITLE); 52 setContentView(R.layout.create_playlist); 53 getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT, 54 WindowManager.LayoutParams.WRAP_CONTENT); 55 56 mPrompt = (TextView)findViewById(R.id.prompt); 57 mPlaylist = (EditText)findViewById(R.id.playlist); 58 mSaveButton = (Button) findViewById(R.id.create); 59 mSaveButton.setOnClickListener(mOpenClicked); 60 61 ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() { 62 public void onClick(View v) { 63 finish(); 64 } 65 }); 66 67 String defaultname = icicle != null ? icicle.getString("defaultname") : makePlaylistName(); 68 String promptformat = getString(R.string.create_playlist_create_text_prompt); 69 String prompt = String.format(promptformat, defaultname); 70 mPrompt.setText(prompt); 71 mPlaylist.setText(defaultname); 72 mPlaylist.setSelection(defaultname.length()); 73 mPlaylist.addTextChangedListener(mTextWatcher); 74 } 75 76 TextWatcher mTextWatcher = new TextWatcher() { 77 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 78 // don't care about this one 79 } 80 public void onTextChanged(CharSequence s, int start, int before, int count) { 81 // check if playlist with current name exists already, and warn the user if so. 82 if (idForplaylist(mPlaylist.getText().toString()) >= 0) { 83 mSaveButton.setText(R.string.create_playlist_overwrite_text); 84 } else { 85 mSaveButton.setText(R.string.create_playlist_create_text); 86 } 87 }; 88 public void afterTextChanged(Editable s) { 89 // don't care about this one 90 } 91 }; 92 93 private int idForplaylist(String name) { 94 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 95 new String[] { MediaStore.Audio.Playlists._ID }, 96 MediaStore.Audio.Playlists.NAME + "=?", 97 new String[] { name }, 98 MediaStore.Audio.Playlists.NAME); 99 int id = -1; 100 if (c != null) { 101 c.moveToFirst(); 102 if (!c.isAfterLast()) { 103 id = c.getInt(0); 104 } 105 } 106 c.close(); 107 return id; 108 } 109 110 @Override 111 public void onSaveInstanceState(Bundle outcicle) { 112 outcicle.putString("defaultname", mPlaylist.getText().toString()); 113 } 114 115 @Override 116 public void onResume() { 117 super.onResume(); 118 } 119 120 private String makePlaylistName() { 121 122 String template = getString(R.string.new_playlist_name_template); 123 int num = 1; 124 125 String[] cols = new String[] { 126 MediaStore.Audio.Playlists.NAME 127 }; 128 ContentResolver resolver = getContentResolver(); 129 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''"; 130 Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 131 cols, whereclause, null, 132 MediaStore.Audio.Playlists.NAME); 133 134 String suggestedname; 135 suggestedname = String.format(template, num++); 136 137 // Need to loop until we've made 1 full pass through without finding a match. 138 // Looping more than once shouldn't happen very often, but will happen if 139 // you have playlists named "New Playlist 1"/10/2/3/4/5/6/7/8/9, where 140 // making only one pass would result in "New Playlist 10" being erroneously 141 // picked for the new name. 142 boolean done = false; 143 while (!done) { 144 done = true; 145 c.moveToFirst(); 146 while (! c.isAfterLast()) { 147 String playlistname = c.getString(0); 148 if (playlistname.compareToIgnoreCase(suggestedname) == 0) { 149 suggestedname = String.format(template, num++); 150 done = false; 151 } 152 c.moveToNext(); 153 } 154 } 155 c.close(); 156 return suggestedname; 157 } 158 159 private View.OnClickListener mOpenClicked = new View.OnClickListener() { 160 public void onClick(View v) { 161 String name = mPlaylist.getText().toString(); 162 if (name != null && name.length() > 0) { 163 ContentResolver resolver = getContentResolver(); 164 int id = idForplaylist(name); 165 Uri uri; 166 if (id >= 0) { 167 uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id); 168 MusicUtils.clearPlaylist(CreatePlaylist.this, id); 169 } else { 170 ContentValues values = new ContentValues(1); 171 values.put(MediaStore.Audio.Playlists.NAME, name); 172 uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values); 173 } 174 setResult(RESULT_OK, (new Intent()).setAction(uri.toString())); 175 finish(); 176 } 177 } 178 }; 179} 180