CreatePlaylist.java revision 792a2206a4f05f6bd13fce902d3663892d2947af
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 if (defaultname == null) { 69 finish(); 70 return; 71 } 72 String promptformat = getString(R.string.create_playlist_create_text_prompt); 73 String prompt = String.format(promptformat, defaultname); 74 mPrompt.setText(prompt); 75 mPlaylist.setText(defaultname); 76 mPlaylist.setSelection(defaultname.length()); 77 mPlaylist.addTextChangedListener(mTextWatcher); 78 } 79 80 TextWatcher mTextWatcher = new TextWatcher() { 81 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 82 // don't care about this one 83 } 84 public void onTextChanged(CharSequence s, int start, int before, int count) { 85 // check if playlist with current name exists already, and warn the user if so. 86 if (idForplaylist(mPlaylist.getText().toString()) >= 0) { 87 mSaveButton.setText(R.string.create_playlist_overwrite_text); 88 } else { 89 mSaveButton.setText(R.string.create_playlist_create_text); 90 } 91 }; 92 public void afterTextChanged(Editable s) { 93 // don't care about this one 94 } 95 }; 96 97 private int idForplaylist(String name) { 98 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 99 new String[] { MediaStore.Audio.Playlists._ID }, 100 MediaStore.Audio.Playlists.NAME + "=?", 101 new String[] { name }, 102 MediaStore.Audio.Playlists.NAME); 103 int id = -1; 104 if (c != null) { 105 c.moveToFirst(); 106 if (!c.isAfterLast()) { 107 id = c.getInt(0); 108 } 109 } 110 c.close(); 111 return id; 112 } 113 114 @Override 115 public void onSaveInstanceState(Bundle outcicle) { 116 outcicle.putString("defaultname", mPlaylist.getText().toString()); 117 } 118 119 @Override 120 public void onResume() { 121 super.onResume(); 122 } 123 124 private String makePlaylistName() { 125 126 String template = getString(R.string.new_playlist_name_template); 127 int num = 1; 128 129 String[] cols = new String[] { 130 MediaStore.Audio.Playlists.NAME 131 }; 132 ContentResolver resolver = getContentResolver(); 133 String whereclause = MediaStore.Audio.Playlists.NAME + " != ''"; 134 Cursor c = resolver.query(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 135 cols, whereclause, null, 136 MediaStore.Audio.Playlists.NAME); 137 138 if (c == null) { 139 return null; 140 } 141 142 String suggestedname; 143 suggestedname = String.format(template, num++); 144 145 // Need to loop until we've made 1 full pass through without finding a match. 146 // Looping more than once shouldn't happen very often, but will happen if 147 // you have playlists named "New Playlist 1"/10/2/3/4/5/6/7/8/9, where 148 // making only one pass would result in "New Playlist 10" being erroneously 149 // picked for the new name. 150 boolean done = false; 151 while (!done) { 152 done = true; 153 c.moveToFirst(); 154 while (! c.isAfterLast()) { 155 String playlistname = c.getString(0); 156 if (playlistname.compareToIgnoreCase(suggestedname) == 0) { 157 suggestedname = String.format(template, num++); 158 done = false; 159 } 160 c.moveToNext(); 161 } 162 } 163 c.close(); 164 return suggestedname; 165 } 166 167 private View.OnClickListener mOpenClicked = new View.OnClickListener() { 168 public void onClick(View v) { 169 String name = mPlaylist.getText().toString(); 170 if (name != null && name.length() > 0) { 171 ContentResolver resolver = getContentResolver(); 172 int id = idForplaylist(name); 173 Uri uri; 174 if (id >= 0) { 175 uri = ContentUris.withAppendedId(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, id); 176 MusicUtils.clearPlaylist(CreatePlaylist.this, id); 177 } else { 178 ContentValues values = new ContentValues(1); 179 values.put(MediaStore.Audio.Playlists.NAME, name); 180 uri = resolver.insert(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values); 181 } 182 setResult(RESULT_OK, (new Intent()).setData(uri)); 183 finish(); 184 } 185 } 186 }; 187} 188