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