Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | package com.android.phone; |
| 17 | |
| 18 | import android.app.AlertDialog; |
| 19 | import android.app.Dialog; |
| 20 | import android.app.DialogFragment; |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 21 | import android.app.Fragment; |
| 22 | import android.app.FragmentManager; |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.DialogInterface; |
| 25 | import android.content.DialogInterface.OnClickListener; |
| 26 | import android.os.Bundle; |
| 27 | |
| 28 | /** |
| 29 | * A dialog fragment that asks the user if they are sure they want to turn on data roaming |
| 30 | * to avoid accidental charges. |
| 31 | */ |
| 32 | public class RoamingDialogFragment extends DialogFragment implements OnClickListener { |
| 33 | |
| 34 | /** |
| 35 | * The interface we expect a host activity to implement. |
| 36 | */ |
| 37 | public interface RoamingDialogListener { |
| 38 | void onPositiveButtonClick(DialogFragment dialog); |
| 39 | } |
| 40 | |
| 41 | // the host activity which implements the listening interface |
| 42 | private RoamingDialogListener mListener; |
| 43 | |
| 44 | |
| 45 | @Override |
| 46 | public void onAttach(Context context) { |
| 47 | super.onAttach(context); |
| 48 | // Verify host activity implemented callback interface |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 49 | FragmentManager fragmentManager = getFragmentManager(); |
| 50 | Fragment fragment = fragmentManager.findFragmentById(R.id.network_setting_content); |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 51 | try { |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 52 | mListener = (RoamingDialogListener) fragment; |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 53 | } catch (ClassCastException e) { |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 54 | throw new ClassCastException(fragment.toString() + |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 55 | "must implement RoamingDialogListener"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 61 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
| 62 | builder.setMessage(getResources().getString(R.string.roaming_warning)) |
| 63 | .setTitle(R.string.roaming_alert_title) |
| 64 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 65 | .setPositiveButton(android.R.string.yes, this) |
| 66 | .setNegativeButton(android.R.string.no, this); |
| 67 | return builder.create(); |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void onClick(DialogInterface dialog, int which) { |
| 72 | // let the host know that the positive button has been clicked |
| 73 | if (which == dialog.BUTTON_POSITIVE) { |
| 74 | mListener.onPositiveButtonClick(this); |
| 75 | } |
| 76 | } |
| 77 | } |