1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.contacts.interactions;
17
18import android.app.AlertDialog;
19import android.app.Dialog;
20import android.app.DialogFragment;
21import android.app.FragmentManager;
22import android.content.DialogInterface;
23import android.os.Bundle;
24
25import com.android.contacts.ContactSaveService;
26import com.android.contacts.R;
27
28/**
29 * A dialog for deleting a group.
30 */
31public class GroupDeletionDialogFragment extends DialogFragment {
32
33    private static final String ARG_GROUP_ID = "groupId";
34    private static final String ARG_LABEL = "label";
35    private static final String ARG_SHOULD_END_ACTIVITY = "endActivity";
36
37    public static void show(FragmentManager fragmentManager, long groupId, String label,
38            boolean endActivity) {
39        GroupDeletionDialogFragment dialog = new GroupDeletionDialogFragment();
40        Bundle args = new Bundle();
41        args.putLong(ARG_GROUP_ID, groupId);
42        args.putString(ARG_LABEL, label);
43        args.putBoolean(ARG_SHOULD_END_ACTIVITY, endActivity);
44        dialog.setArguments(args);
45        dialog.show(fragmentManager, "deleteGroup");
46    }
47
48    @Override
49    public Dialog onCreateDialog(Bundle savedInstanceState) {
50        String label = getArguments().getString(ARG_LABEL);
51        String message = getActivity().getString(R.string.delete_group_dialog_message, label);
52
53        return new AlertDialog.Builder(getActivity())
54                .setIconAttribute(android.R.attr.alertDialogIcon)
55                .setMessage(message)
56                .setPositiveButton(android.R.string.ok,
57                    new DialogInterface.OnClickListener() {
58                        @Override
59                        public void onClick(DialogInterface dialog, int whichButton) {
60                            deleteGroup();
61                        }
62                    }
63                )
64                .setNegativeButton(android.R.string.cancel, null)
65                .create();
66    }
67
68    protected void deleteGroup() {
69        Bundle arguments = getArguments();
70        long groupId = arguments.getLong(ARG_GROUP_ID);
71
72        getActivity().startService(ContactSaveService.createGroupDeletionIntent(
73                getActivity(), groupId));
74        if (shouldEndActivity()) {
75            getActivity().finish();
76        }
77    }
78
79    private boolean shouldEndActivity() {
80        return getArguments().getBoolean(ARG_SHOULD_END_ACTIVITY);
81    }
82}
83