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 com.android.contacts.ContactSaveService;
19import com.android.contacts.R;
20
21import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.DialogFragment;
24import android.app.FragmentManager;
25import android.content.DialogInterface;
26import android.os.Bundle;
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                .setTitle(R.string.delete_group_dialog_title)
56                .setMessage(message)
57                .setPositiveButton(android.R.string.ok,
58                    new DialogInterface.OnClickListener() {
59                        @Override
60                        public void onClick(DialogInterface dialog, int whichButton) {
61                            deleteGroup();
62                        }
63                    }
64                )
65                .setNegativeButton(android.R.string.cancel, null)
66                .create();
67    }
68
69    protected void deleteGroup() {
70        Bundle arguments = getArguments();
71        long groupId = arguments.getLong(ARG_GROUP_ID);
72
73        getActivity().startService(ContactSaveService.createGroupDeletionIntent(
74                getActivity(), groupId));
75        if (shouldEndActivity()) {
76            getActivity().finish();
77        }
78    }
79
80    private boolean shouldEndActivity() {
81        return getArguments().getBoolean(ARG_SHOULD_END_ACTIVITY);
82    }
83}
84