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  */
16 
17 package com.android.contacts.editor;
18 
19 import android.app.AlertDialog;
20 import android.app.Dialog;
21 import android.app.DialogFragment;
22 import android.app.Fragment;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 
26 import com.android.contacts.R;
27 
28 /**
29  * Shows a dialog asking the user whether to split the contact. The result is passed back
30  * to the Fragment that is configured by {@link Fragment#setTargetFragment(Fragment, int)}, which
31  * has to implement {@link SplitContactConfirmationDialogFragment.Listener}.
32  * Does not split the contact itself.
33  */
34 public class SplitContactConfirmationDialogFragment extends DialogFragment {
35 
36     private static final String ARG_HAS_PENDING_CHANGES = "hasPendingChanges";
37 
38     /**
39      * Callbacks for the dialog host.
40      */
41     public interface Listener {
42 
43         /**
44          * Invoked after the user has confirmed that they want to proceed with the split.
45          *
46          * @param hasPendingChanges whether there are unsaved changes in the underlying contact
47          *         that should be saved before the split.
48          */
onSplitContactConfirmed(boolean hasPendingChanges)49         void onSplitContactConfirmed(boolean hasPendingChanges);
50     }
51 
show(ContactEditorBaseFragment fragment, boolean hasPendingChanges)52     public static void show(ContactEditorBaseFragment fragment, boolean hasPendingChanges) {
53         final Bundle args = new Bundle();
54         args.putBoolean(ARG_HAS_PENDING_CHANGES, hasPendingChanges);
55 
56         final SplitContactConfirmationDialogFragment dialog = new
57                 SplitContactConfirmationDialogFragment();
58         dialog.setTargetFragment(fragment, 0);
59         dialog.setArguments(args);
60         dialog.show(fragment.getFragmentManager(), "splitContact");
61     }
62 
63     private boolean mHasPendingChanges;
64 
65     @Override
onCreate(Bundle savedInstanceState)66     public void onCreate(Bundle savedInstanceState) {
67         super.onCreate(savedInstanceState);
68         mHasPendingChanges = getArguments().getBoolean(ARG_HAS_PENDING_CHANGES);
69     }
70 
71     @Override
onCreateDialog(Bundle savedInstanceState)72     public Dialog onCreateDialog(Bundle savedInstanceState) {
73         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
74         builder.setMessage(mHasPendingChanges
75                 ? R.string.splitConfirmationWithPendingChanges
76                 : R.string.splitConfirmation);
77         builder.setPositiveButton(mHasPendingChanges
78                 ? R.string.splitConfirmationWithPendingChanges_positive_button
79                 : R.string.splitConfirmation_positive_button,
80                 new DialogInterface.OnClickListener() {
81                     @Override
82                     public void onClick(DialogInterface dialog, int which) {
83                         final Listener targetListener = (Listener) getTargetFragment();
84                         targetListener.onSplitContactConfirmed(mHasPendingChanges);
85                     }
86                 });
87         builder.setNegativeButton(android.R.string.cancel, null);
88         builder.setCancelable(false);
89         return builder.create();
90     }
91 }
92