OSDN Git Service

Automatic translation import
[android-x86/packages-apps-Eleven.git] / src / com / cyngn / eleven / menu / RenamePlaylist.java
1 /*
2  * Copyright (C) 2012 Andrew Neal Licensed under the Apache License, Version 2.0
3  * (the "License"); you may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
6  * or agreed to in writing, software distributed under the License is
7  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
8  * KIND, either express or implied. See the License for the specific language
9  * governing permissions and limitations under the License.
10  */
11
12 package com.cyngn.eleven.menu;
13
14 import android.app.Dialog;
15 import android.content.ContentResolver;
16 import android.content.ContentValues;
17 import android.database.Cursor;
18 import android.os.Bundle;
19 import android.provider.MediaStore;
20 import android.provider.MediaStore.Audio;
21
22 import com.cyngn.eleven.R;
23 import com.cyngn.eleven.format.Capitalize;
24 import com.cyngn.eleven.utils.MusicUtils;
25
26 /**
27  * Alert dialog used to rename playlits.
28  * 
29  * @author Andrew Neal (andrewdneal@gmail.com)
30  */
31 public class RenamePlaylist extends BasePlaylistDialog {
32
33     private String mOriginalName;
34
35     private long mRenameId;
36
37     /**
38      * @param id The Id of the playlist to rename
39      * @return A new instance of this dialog.
40      */
41     public static RenamePlaylist getInstance(final Long id) {
42         final RenamePlaylist frag = new RenamePlaylist();
43         final Bundle args = new Bundle();
44         args.putLong("rename", id);
45         frag.setArguments(args);
46         return frag;
47     }
48
49     /**
50      * {@inheritDoc}
51      */
52     @Override
53     public void onSaveInstanceState(final Bundle outcicle) {
54         outcicle.putString("defaultname", mPlaylist.getText().toString());
55         outcicle.putLong("rename", mRenameId);
56     }
57
58     /**
59      * {@inheritDoc}
60      */
61     @Override
62     public void initObjects(final Bundle savedInstanceState) {
63         mRenameId = savedInstanceState != null ? savedInstanceState.getLong("rename")
64                 : getArguments().getLong("rename", -1);
65         mOriginalName = getPlaylistNameFromId(mRenameId);
66         mDefaultname = savedInstanceState != null ? savedInstanceState.getString("defaultname")
67                 : mOriginalName;
68         if (mRenameId < 0 || mOriginalName == null || mDefaultname == null) {
69             getDialog().dismiss();
70             return;
71         }
72         final String promptformat = getString(R.string.create_playlist_prompt);
73         mPrompt = String.format(promptformat, mOriginalName, mDefaultname);
74     }
75
76     /**
77      * {@inheritDoc}
78      */
79     @Override
80     public void onSaveClick() {
81         final String playlistName = mPlaylist.getText().toString();
82         if (playlistName != null && playlistName.length() > 0) {
83             final ContentResolver resolver = getActivity().getContentResolver();
84             final ContentValues values = new ContentValues(1);
85             values.put(Audio.Playlists.NAME, Capitalize.capitalize(playlistName));
86             resolver.update(Audio.Playlists.EXTERNAL_CONTENT_URI, values,
87                     MediaStore.Audio.Playlists._ID + "=?", new String[] {
88                         String.valueOf(mRenameId)
89                     });
90             closeKeyboard();
91             getDialog().dismiss();
92         }
93     }
94
95     @Override
96     public void onTextChangedListener() {
97         final String playlistName = mPlaylist.getText().toString();
98         mSaveButton = mPlaylistDialog.getButton(Dialog.BUTTON_POSITIVE);
99         if (mSaveButton == null) {
100             return;
101         }
102         if (playlistName.trim().length() == 0) {
103             mSaveButton.setEnabled(false);
104         } else {
105             mSaveButton.setEnabled(true);
106             if (MusicUtils.getIdForPlaylist(getActivity(), playlistName) >= 0) {
107                 mSaveButton.setText(R.string.overwrite);
108             } else {
109                 mSaveButton.setText(R.string.save);
110             }
111         }
112     }
113
114     /**
115      * @param id The Id of the playlist
116      * @return The name of the playlist
117      */
118     private String getPlaylistNameFromId(final long id) {
119         Cursor cursor = getActivity().getContentResolver().query(
120                 MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI, new String[] {
121                     MediaStore.Audio.Playlists.NAME
122                 }, MediaStore.Audio.Playlists._ID + "=?", new String[] {
123                     String.valueOf(id)
124                 }, MediaStore.Audio.Playlists.NAME);
125         String playlistName = null;
126         if (cursor != null) {
127             cursor.moveToFirst();
128             if (!cursor.isAfterLast()) {
129                 playlistName = cursor.getString(0);
130             }
131         }
132         cursor.close();
133         cursor = null;
134         return playlistName;
135     }
136
137 }