OSDN Git Service

Eleven: Complete Apollo rebrand.
[android-x86/packages-apps-Eleven.git] / src / org / lineageos / eleven / menu / DeleteDialog.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 org.lineageos.eleven.menu;
15
16 import android.app.AlertDialog;
17 import android.app.Dialog;
18 import android.content.DialogInterface;
19 import android.content.DialogInterface.OnClickListener;
20 import android.os.Bundle;
21 import android.support.v4.app.DialogFragment;
22
23 import org.lineageos.eleven.Config;
24 import org.lineageos.eleven.R;
25 import org.lineageos.eleven.cache.ImageFetcher;
26 import org.lineageos.eleven.utils.ElevenUtils;
27 import org.lineageos.eleven.utils.MusicUtils;
28
29 /**
30  * Alert dialog used to delete tracks.
31  * <p>
32  * TODO: Remove albums from the recents list upon deletion.
33  *
34  * @author Andrew Neal (andrewdneal@gmail.com)
35  */
36 public class DeleteDialog extends DialogFragment {
37
38     public interface DeleteDialogCallback {
39         public void onDelete(long[] id);
40     }
41
42     /**
43      * The item(s) to delete
44      */
45     private long[] mItemList;
46
47     /**
48      * The image cache
49      */
50     private ImageFetcher mFetcher;
51
52     /**
53      * Empty constructor as per the {@link Fragment} documentation
54      */
55     public DeleteDialog() {
56     }
57
58     /**
59      * @param title The title of the artist, album, or song to delete
60      * @param items The item(s) to delete
61      * @param key The key used to remove items from the cache.
62      * @return A new instance of the dialog
63      */
64     public static DeleteDialog newInstance(final String title, final long[] items, final String key) {
65         final DeleteDialog frag = new DeleteDialog();
66         final Bundle args = new Bundle();
67         args.putString(Config.NAME, title);
68         args.putLongArray("items", items);
69         args.putString("cachekey", key);
70         frag.setArguments(args);
71         return frag;
72     }
73
74     /**
75      * {@inheritDoc}
76      */
77     @Override
78     public Dialog onCreateDialog(final Bundle savedInstanceState) {
79         final String delete = getString(R.string.context_menu_delete);
80         final Bundle arguments = getArguments();
81         // Get the image cache key
82         final String key = arguments.getString("cachekey");
83         // Get the track(s) to delete
84         mItemList = arguments.getLongArray("items");
85         // Get the dialog title
86         final String title = arguments.getString(Config.NAME);
87         final String dialogTitle = getString(R.string.delete_dialog_title, title);
88         // Initialize the image cache
89         mFetcher = ElevenUtils.getImageFetcher(getActivity());
90         // Build the dialog
91         return new AlertDialog.Builder(getActivity()).setTitle(dialogTitle)
92                 .setMessage(R.string.cannot_be_undone)
93                 .setPositiveButton(delete, new OnClickListener() {
94
95                     @Override
96                     public void onClick(final DialogInterface dialog, final int which) {
97                         // Remove the items from the image cache
98                         mFetcher.removeFromCache(key);
99                         // Delete the selected item(s)
100                         MusicUtils.deleteTracks(getActivity(), mItemList);
101                         if (getActivity() instanceof DeleteDialogCallback) {
102                             ((DeleteDialogCallback)getActivity()).onDelete(mItemList);
103                         }
104                         dialog.dismiss();
105                     }
106                 }).setNegativeButton(R.string.cancel, new OnClickListener() {
107
108                     @Override
109                     public void onClick(final DialogInterface dialog, final int which) {
110                         dialog.dismiss();
111                     }
112                 }).create();
113     }
114 }