1/*
2 * Copyright (C) 2015 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 */
16
17package com.android.contacts.editor;
18
19import android.app.AlertDialog;
20import android.app.Dialog;
21import android.app.DialogFragment;
22import android.content.DialogInterface;
23import android.os.Bundle;
24
25import com.android.contacts.R;
26
27/**
28 * Shows a dialog asking the user whether to apply pending changes before joining the contact.
29 */
30public class JoinContactConfirmationDialogFragment extends DialogFragment {
31
32    private static final String ARG_JOIN_CONTACT_ID = "joinContactId";
33
34    /**
35     * Callbacks for the host of this dialog fragment.
36     */
37    public interface Listener {
38
39        /**
40         * Invoked after the user confirms they want to save pending changes before
41         * joining the contact.
42         *
43         * @param joinContactId The raw contact ID of the contact to join to.
44         */
45        void onJoinContactConfirmed(long joinContactId);
46    }
47
48    /**
49     * @param joinContactId The raw contact ID of the contact to join to after confirmation.
50     */
51    public static void show(ContactEditorFragment fragment, long joinContactId) {
52        final Bundle args = new Bundle();
53        args.putLong(ARG_JOIN_CONTACT_ID, joinContactId);
54
55        final JoinContactConfirmationDialogFragment dialog = new
56                JoinContactConfirmationDialogFragment();
57        dialog.setTargetFragment(fragment, 0);
58        dialog.setArguments(args);
59        dialog.show(fragment.getFragmentManager(), "joinContactConfirmationDialog");
60    }
61
62    private long mContactId;
63
64    @Override
65    public void onCreate(Bundle savedInstanceState) {
66        super.onCreate(savedInstanceState);
67        mContactId = getArguments().getLong(ARG_JOIN_CONTACT_ID);
68    }
69
70    @Override
71    public Dialog onCreateDialog(Bundle savedInstanceState) {
72        final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
73        builder.setMessage(R.string.joinConfirmation);
74        builder.setPositiveButton(R.string.joinConfirmation_positive_button,
75                new DialogInterface.OnClickListener() {
76                    @Override
77                    public void onClick(DialogInterface dialog, int which) {
78                        final Listener targetListener = (Listener) getTargetFragment();
79                        targetListener.onJoinContactConfirmed(mContactId);
80                    }
81                });
82        builder.setNegativeButton(android.R.string.cancel, null);
83        builder.setCancelable(false);
84        return builder.create();
85    }
86}
87