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     public static final String TAG = "SplitConfirmation";
38 
39     /**
40      * Callbacks for the dialog host.
41      */
42     public interface Listener {
43 
44         /**
45          * Invoked after the user has confirmed that they want to proceed with the split.
46          *
47          * @param hasPendingChanges whether there are unsaved changes in the underlying contact
48          *         that should be saved before the split.
49          */
onSplitContactConfirmed(boolean hasPendingChanges)50         void onSplitContactConfirmed(boolean hasPendingChanges);
51 
52         /**
53          * Invoked if the user has canceled or dismissed the dialog without making a choice.
54          */
onSplitContactCanceled()55         void onSplitContactCanceled();
56     }
57 
show(ContactEditorFragment fragment, boolean hasPendingChanges)58     public static void show(ContactEditorFragment fragment, boolean hasPendingChanges) {
59         final Bundle args = new Bundle();
60         args.putBoolean(ARG_HAS_PENDING_CHANGES, hasPendingChanges);
61 
62         final SplitContactConfirmationDialogFragment dialog = new
63                 SplitContactConfirmationDialogFragment();
64         dialog.setTargetFragment(fragment, 0);
65         dialog.setArguments(args);
66         dialog.show(fragment.getFragmentManager(), "splitContact");
67     }
68 
69     private boolean mHasPendingChanges;
70 
71     @Override
onCreate(Bundle savedInstanceState)72     public void onCreate(Bundle savedInstanceState) {
73         super.onCreate(savedInstanceState);
74         mHasPendingChanges = getArguments() != null
75                 && getArguments().getBoolean(ARG_HAS_PENDING_CHANGES);
76     }
77 
78     @Override
onCreateDialog(Bundle savedInstanceState)79     public Dialog onCreateDialog(Bundle savedInstanceState) {
80         final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
81         builder.setMessage(mHasPendingChanges
82                 ? R.string.splitConfirmationWithPendingChanges
83                 : R.string.splitConfirmation);
84         builder.setPositiveButton(mHasPendingChanges
85                 ? R.string.splitConfirmationWithPendingChanges_positive_button
86                 : R.string.splitConfirmation_positive_button,
87                 new DialogInterface.OnClickListener() {
88                     @Override
89                     public void onClick(DialogInterface dialog, int which) {
90                         getListener().onSplitContactConfirmed(mHasPendingChanges);
91                     }
92                 });
93         builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
94             @Override
95             public void onClick(DialogInterface dialog, int which) {
96                 onCancel(dialog);
97             }
98         });
99         builder.setCancelable(false);
100         return builder.create();
101     }
102 
getListener()103     private Listener getListener() {
104         return getTargetFragment() == null
105                 ? (Listener) getActivity()
106                 : (Listener) getTargetFragment();
107     }
108 
109     @Override
onCancel(DialogInterface dialog)110     public void onCancel(DialogInterface dialog) {
111         super.onCancel(dialog);
112         getListener().onSplitContactCanceled();
113     }
114 }
115