2 * Copyright (C) 2008 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.database.Cursor;
24 import android.media.AudioManager;
25 import android.net.Uri;
26 import android.os.Bundle;
27 import android.provider.MediaStore;
28 import android.text.Editable;
29 import android.text.TextWatcher;
30 import android.util.Log;
31 import android.view.KeyEvent;
32 import android.view.View;
33 import android.view.Window;
34 import android.view.WindowManager;
35 import android.widget.Button;
36 import android.widget.EditText;
37 import android.widget.TextView;
38 import android.widget.Toast;
40 public class RenamePlaylist extends Activity
42 private EditText mPlaylist;
43 private TextView mPrompt;
44 private Button mSaveButton;
45 private long mRenameId;
46 private String mOriginalName;
49 public void onCreate(Bundle icicle) {
50 super.onCreate(icicle);
51 setVolumeControlStream(AudioManager.STREAM_MUSIC);
53 requestWindowFeature(Window.FEATURE_NO_TITLE);
54 setContentView(R.layout.create_playlist);
55 getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT,
56 WindowManager.LayoutParams.WRAP_CONTENT);
58 mPrompt = (TextView)findViewById(R.id.prompt);
59 mPlaylist = (EditText)findViewById(R.id.playlist);
60 mSaveButton = (Button) findViewById(R.id.create);
61 mSaveButton.setOnClickListener(mOpenClicked);
63 ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {
64 public void onClick(View v) {
69 mRenameId = icicle != null ? icicle.getLong("rename")
70 : getIntent().getLongExtra("rename", -1);
71 mOriginalName = nameForId(mRenameId);
72 String defaultname = icicle != null ? icicle.getString("defaultname") : mOriginalName;
74 if (mRenameId < 0 || mOriginalName == null || defaultname == null) {
75 Log.i("@@@@", "Rename failed: " + mRenameId + "/" + defaultname);
81 if (mOriginalName.equals(defaultname)) {
82 promptformat = getString(R.string.rename_playlist_same_prompt);
84 promptformat = getString(R.string.rename_playlist_diff_prompt);
87 String prompt = String.format(promptformat, mOriginalName, defaultname);
88 mPrompt.setText(prompt);
89 mPlaylist.setText(defaultname);
90 mPlaylist.setSelection(defaultname.length());
91 mPlaylist.addTextChangedListener(mTextWatcher);
95 TextWatcher mTextWatcher = new TextWatcher() {
96 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
97 // don't care about this one
99 public void onTextChanged(CharSequence s, int start, int before, int count) {
100 // check if playlist with current name exists already, and warn the user if so.
103 public void afterTextChanged(Editable s) {
104 // don't care about this one
108 private void setSaveButton() {
109 String typedname = mPlaylist.getText().toString();
110 if (idForplaylist(typedname) >= 0
111 && ! mOriginalName.equals(typedname)) {
112 mSaveButton.setText(R.string.create_playlist_overwrite_text);
114 mSaveButton.setText(R.string.create_playlist_create_text);
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 + "=?",
122 new String[] { name },
123 MediaStore.Audio.Playlists.NAME);
127 if (!c.isAfterLast()) {
135 private String nameForId(long id) {
136 Cursor c = MusicUtils.query(this, MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
137 new String[] { MediaStore.Audio.Playlists.NAME },
138 MediaStore.Audio.Playlists._ID + "=?",
139 new String[] { Long.valueOf(id).toString() },
140 MediaStore.Audio.Playlists.NAME);
144 if (!c.isAfterLast()) {
145 name = c.getString(0);
154 public void onSaveInstanceState(Bundle outcicle) {
155 outcicle.putString("defaultname", mPlaylist.getText().toString());
156 outcicle.putLong("rename", mRenameId);
160 public void onResume() {
164 private View.OnClickListener mOpenClicked = new View.OnClickListener() {
165 public void onClick(View v) {
166 String name = mPlaylist.getText().toString();
167 if (name != null && name.length() > 0) {
168 ContentResolver resolver = getContentResolver();
169 ContentValues values = new ContentValues(1);
170 values.put(MediaStore.Audio.Playlists.NAME, name);
171 resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,
173 MediaStore.Audio.Playlists._ID + "=?",
174 new String[] { Long.valueOf(mRenameId).toString()});
176 setResult(RESULT_OK);
177 Toast.makeText(RenamePlaylist.this, R.string.playlist_renamed_message, Toast.LENGTH_SHORT).show();