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.
12 package com.andrew.apollo.menu;
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;
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;
28 * Alert dialog used to delete tracks.
30 * TODO: Remove albums from the recents list upon deletion.
32 * @author Andrew Neal (andrewdneal@gmail.com)
34 public class DeleteDialog extends DialogFragment {
36 public interface DeleteDialogCallback {
37 public void onDelete(long[] id);
41 * The item(s) to delete
43 private long[] mItemList;
48 private ImageFetcher mFetcher;
51 * Empty constructor as per the {@link Fragment} documentation
53 public DeleteDialog() {
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
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);
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());
89 return new AlertDialog.Builder(getActivity()).setTitle(dialogTitle)
90 .setMessage(R.string.cannot_be_undone)
91 .setPositiveButton(delete, new OnClickListener() {
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);
104 }).setNegativeButton(R.string.cancel, new OnClickListener() {
107 public void onClick(final DialogInterface dialog, final int which) {