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.
14 package org.lineageos.eleven.menu;
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;
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;
30 * Alert dialog used to delete tracks.
32 * TODO: Remove albums from the recents list upon deletion.
34 * @author Andrew Neal (andrewdneal@gmail.com)
36 public class DeleteDialog extends DialogFragment {
38 public interface DeleteDialogCallback {
39 public void onDelete(long[] id);
43 * The item(s) to delete
45 private long[] mItemList;
50 private ImageFetcher mFetcher;
53 * Empty constructor as per the {@link Fragment} documentation
55 public DeleteDialog() {
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
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);
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());
91 return new AlertDialog.Builder(getActivity()).setTitle(dialogTitle)
92 .setMessage(R.string.cannot_be_undone)
93 .setPositiveButton(delete, new OnClickListener() {
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);
106 }).setNegativeButton(R.string.cancel, new OnClickListener() {
109 public void onClick(final DialogInterface dialog, final int which) {