1/* 2 * Copyright (C) 2008 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.ContentValues; 22import android.database.Cursor; 23import android.media.AudioManager; 24import android.os.Bundle; 25import android.provider.MediaStore; 26import android.text.Editable; 27import android.text.TextWatcher; 28import android.util.Log; 29import android.view.View; 30import android.view.Window; 31import android.view.WindowManager; 32import android.widget.Button; 33import android.widget.EditText; 34import android.widget.TextView; 35import android.widget.Toast; 36 37public class RenamePlaylist extends Activity { 38 private EditText mPlaylist; 39 private TextView mPrompt; 40 private Button mSaveButton; 41 private long mRenameId; 42 private String mOriginalName; 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( 52 WindowManager.LayoutParams.MATCH_PARENT, 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 mRenameId = 66 icicle != null ? icicle.getLong("rename") : getIntent().getLongExtra("rename", -1); 67 mOriginalName = nameForId(mRenameId); 68 String defaultname = icicle != null ? icicle.getString("defaultname") : mOriginalName; 69 70 if (mRenameId < 0 || mOriginalName == null || defaultname == null) { 71 Log.i("@@@@", "Rename failed: " + mRenameId + "/" + defaultname); 72 finish(); 73 return; 74 } 75 76 String promptformat; 77 if (mOriginalName.equals(defaultname)) { 78 promptformat = getString(R.string.rename_playlist_same_prompt); 79 } else { 80 promptformat = getString(R.string.rename_playlist_diff_prompt); 81 } 82 83 String prompt = String.format(promptformat, mOriginalName, defaultname); 84 mPrompt.setText(prompt); 85 mPlaylist.setText(defaultname); 86 mPlaylist.setSelection(defaultname.length()); 87 mPlaylist.addTextChangedListener(mTextWatcher); 88 setSaveButton(); 89 } 90 91 TextWatcher mTextWatcher = new TextWatcher() { 92 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 93 // don't care about this one 94 } 95 public void onTextChanged(CharSequence s, int start, int before, int count) { 96 // check if playlist with current name exists already, and warn the user if so. 97 setSaveButton(); 98 }; 99 public void afterTextChanged(Editable s) { 100 // don't care about this one 101 } 102 }; 103 104 private void setSaveButton() { 105 String typedname = mPlaylist.getText().toString(); 106 if (typedname.trim().length() == 0) { 107 mSaveButton.setEnabled(false); 108 } else { 109 mSaveButton.setEnabled(true); 110 if (idForplaylist(typedname) >= 0 && !mOriginalName.equals(typedname)) { 111 mSaveButton.setText(R.string.create_playlist_overwrite_text); 112 } else { 113 mSaveButton.setText(R.string.create_playlist_create_text); 114 } 115 } 116 } 117 118 private int idForplaylist(String name) { 119 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 120 new String[] {MediaStore.Audio.Playlists._ID}, 121 MediaStore.Audio.Playlists.NAME + "=?", new String[] {name}, 122 MediaStore.Audio.Playlists.NAME); 123 int id = -1; 124 if (c != null) { 125 c.moveToFirst(); 126 if (!c.isAfterLast()) { 127 id = c.getInt(0); 128 } 129 } 130 c.close(); 131 return id; 132 } 133 134 private String nameForId(long id) { 135 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, 136 new String[] {MediaStore.Audio.Playlists.NAME}, 137 MediaStore.Audio.Playlists._ID + "=?", new String[] {Long.valueOf(id).toString()}, 138 MediaStore.Audio.Playlists.NAME); 139 String name = null; 140 if (c != null) { 141 c.moveToFirst(); 142 if (!c.isAfterLast()) { 143 name = c.getString(0); 144 } 145 } 146 c.close(); 147 return name; 148 } 149 150 @Override 151 public void onSaveInstanceState(Bundle outcicle) { 152 outcicle.putString("defaultname", mPlaylist.getText().toString()); 153 outcicle.putLong("rename", mRenameId); 154 } 155 156 @Override 157 public void onResume() { 158 super.onResume(); 159 } 160 161 private View.OnClickListener mOpenClicked = new View.OnClickListener() { 162 public void onClick(View v) { 163 String name = mPlaylist.getText().toString(); 164 if (name != null && name.length() > 0) { 165 ContentResolver resolver = getContentResolver(); 166 ContentValues values = new ContentValues(1); 167 values.put(MediaStore.Audio.Playlists.NAME, name); 168 resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, values, 169 MediaStore.Audio.Playlists._ID + "=?", 170 new String[] {Long.valueOf(mRenameId).toString()}); 171 172 setResult(RESULT_OK); 173 Toast.makeText(RenamePlaylist.this, R.string.playlist_renamed_message, 174 Toast.LENGTH_SHORT) 175 .show(); 176 finish(); 177 } 178 } 179 }; 180} 181