OSDN Git Service

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