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