OSDN Git Service

Initial Contribution
[android-x86/packages-apps-Music.git] / src / com / android / music / RenamePlaylist.java
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
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.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;
39
40 public class RenamePlaylist extends Activity
41 {
42     private EditText mPlaylist;
43     private TextView mPrompt;
44     private Button mSaveButton;
45     private long mRenameId;
46     private String mOriginalName;
47
48     @Override
49     public void onCreate(Bundle icicle) {
50         super.onCreate(icicle);
51         setVolumeControlStream(AudioManager.STREAM_MUSIC);
52
53         requestWindowFeature(Window.FEATURE_NO_TITLE);
54         setContentView(R.layout.create_playlist);
55         getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT,
56                                     WindowManager.LayoutParams.WRAP_CONTENT);
57
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);
62
63         ((Button)findViewById(R.id.cancel)).setOnClickListener(new View.OnClickListener() {
64             public void onClick(View v) {
65                 finish();
66             }
67         });
68
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;
73         
74         if (mRenameId < 0 || mOriginalName == null || defaultname == null) {
75             Log.i("@@@@", "Rename failed: " + mRenameId + "/" + defaultname);
76             finish();
77             return;
78         }
79         
80         String promptformat;
81         if (mOriginalName.equals(defaultname)) {
82             promptformat = getString(R.string.rename_playlist_same_prompt);
83         } else {
84             promptformat = getString(R.string.rename_playlist_diff_prompt);
85         }
86                 
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);
92         setSaveButton();
93     }
94     
95     TextWatcher mTextWatcher = new TextWatcher() {
96         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
97             // don't care about this one
98         }
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.
101             setSaveButton();
102         };
103         public void afterTextChanged(Editable s) {
104             // don't care about this one
105         }
106     };
107     
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);
113         } else {
114             mSaveButton.setText(R.string.create_playlist_create_text);
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 + "=?",
122                 new String[] { name },
123                 MediaStore.Audio.Playlists.NAME);
124         int id = -1;
125         if (c != null) {
126             c.moveToFirst();
127             if (!c.isAfterLast()) {
128                 id = c.getInt(0);
129             }
130         }
131         c.close();
132         return id;
133     }
134     
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);
141         String name = null;
142         if (c != null) {
143             c.moveToFirst();
144             if (!c.isAfterLast()) {
145                 name = c.getString(0);
146             }
147         }
148         c.close();
149         return name;
150     }
151     
152     
153     @Override
154     public void onSaveInstanceState(Bundle outcicle) {
155         outcicle.putString("defaultname", mPlaylist.getText().toString());
156         outcicle.putLong("rename", mRenameId);
157     }
158     
159     @Override
160     public void onResume() {
161         super.onResume();
162     }
163
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,
172                         values,
173                         MediaStore.Audio.Playlists._ID + "=?",
174                         new String[] { Long.valueOf(mRenameId).toString()});
175                 
176                 setResult(RESULT_OK);
177                 Toast.makeText(RenamePlaylist.this, R.string.playlist_renamed_message, Toast.LENGTH_SHORT).show();
178                 finish();
179             }
180         }
181     };
182 }