OSDN Git Service

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