Walter Jang | ed8f6c9 | 2015-01-30 16:07:47 -0800 | [diff] [blame] | 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 | |
| 17 | package com.android.contacts.editor; |
| 18 | |
| 19 | import com.android.contacts.R; |
| 20 | |
| 21 | import android.app.AlertDialog; |
| 22 | import android.app.Dialog; |
| 23 | import android.app.DialogFragment; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.os.Bundle; |
| 26 | |
| 27 | /** |
| 28 | * Asks the user whether to cancel editing the contact. |
| 29 | */ |
| 30 | public class CancelEditDialogFragment extends DialogFragment { |
| 31 | |
| 32 | private static final String TAG = "cancelEditor"; |
| 33 | |
| 34 | /** |
| 35 | * Shows a {@link CancelEditDialogFragment} after setting the given Fragment as the |
| 36 | * target of the dialog. |
| 37 | */ |
Gary Mai | 363af60 | 2016-09-28 10:01:23 -0700 | [diff] [blame] | 38 | public static void show(ContactEditorFragment fragment) { |
Walter Jang | ed8f6c9 | 2015-01-30 16:07:47 -0800 | [diff] [blame] | 39 | final CancelEditDialogFragment dialog = new CancelEditDialogFragment(); |
| 40 | dialog.setTargetFragment(fragment, 0); |
| 41 | dialog.show(fragment.getFragmentManager(), TAG); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 46 | return new AlertDialog.Builder(getActivity()) |
| 47 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 48 | .setMessage(R.string.cancel_confirmation_dialog_message) |
Walter Jang | d1cc5fe | 2016-01-25 15:44:01 -0800 | [diff] [blame] | 49 | .setPositiveButton(R.string.cancel_confirmation_dialog_cancel_editing_button, |
Tingting Wang | 168331d | 2015-10-30 12:00:57 -0700 | [diff] [blame] | 50 | new DialogInterface.OnClickListener() { |
Walter Jang | ed8f6c9 | 2015-01-30 16:07:47 -0800 | [diff] [blame] | 51 | @Override |
| 52 | public void onClick(DialogInterface dialogInterface, int which) { |
| 53 | final Listener targetListener = (Listener) getTargetFragment(); |
| 54 | targetListener.onCancelEditConfirmed(); |
| 55 | } |
| 56 | } |
| 57 | ) |
Walter Jang | d1cc5fe | 2016-01-25 15:44:01 -0800 | [diff] [blame] | 58 | .setNegativeButton(R.string.cancel_confirmation_dialog_keep_editing_button, null) |
Walter Jang | ed8f6c9 | 2015-01-30 16:07:47 -0800 | [diff] [blame] | 59 | .create(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Callbacks for {@link CancelEditDialogFragment} hosts. |
| 64 | */ |
| 65 | public interface Listener { |
| 66 | |
| 67 | /** |
| 68 | * Invoked when the user confirms that they want to cancel editing the contact. |
| 69 | */ |
| 70 | void onCancelEditConfirmed(); |
| 71 | } |
Walter Jang | 7b0970f | 2016-09-01 10:40:19 -0700 | [diff] [blame] | 72 | } |