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; |
pkanwar | 7d69db0 | 2018-01-01 17:03:42 -0800 | [diff] [blame] | 27 | import android.os.PersistableBundle; |
| 28 | import android.telephony.CarrierConfigManager; |
| 29 | |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 30 | /** |
| 31 | * A dialog fragment that asks the user if they are sure they want to turn on data roaming |
| 32 | * to avoid accidental charges. |
| 33 | */ |
| 34 | public class RoamingDialogFragment extends DialogFragment implements OnClickListener { |
| 35 | |
Pengquan Meng | 7ca05bc | 2018-08-28 17:20:19 -0700 | [diff] [blame^] | 36 | public static final String SUB_ID_KEY = "sub_id_key"; |
| 37 | |
| 38 | private int mSubId; |
| 39 | |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 40 | /** |
| 41 | * The interface we expect a host activity to implement. |
| 42 | */ |
| 43 | public interface RoamingDialogListener { |
| 44 | void onPositiveButtonClick(DialogFragment dialog); |
| 45 | } |
| 46 | |
| 47 | // the host activity which implements the listening interface |
| 48 | private RoamingDialogListener mListener; |
| 49 | |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 50 | @Override |
| 51 | public void onAttach(Context context) { |
| 52 | super.onAttach(context); |
Pengquan Meng | 7ca05bc | 2018-08-28 17:20:19 -0700 | [diff] [blame^] | 53 | Bundle args = getArguments(); |
| 54 | mSubId = args.getInt(SUB_ID_KEY); |
| 55 | |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 56 | // Verify host activity implemented callback interface |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 57 | FragmentManager fragmentManager = getFragmentManager(); |
| 58 | Fragment fragment = fragmentManager.findFragmentById(R.id.network_setting_content); |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 59 | try { |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 60 | mListener = (RoamingDialogListener) fragment; |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 61 | } catch (ClassCastException e) { |
pkanwar | d702e54 | 2017-02-08 15:44:54 -0800 | [diff] [blame] | 62 | throw new ClassCastException(fragment.toString() + |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 63 | "must implement RoamingDialogListener"); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public Dialog onCreateDialog(Bundle savedInstanceState) { |
| 69 | AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); |
pkanwar | 7d69db0 | 2018-01-01 17:03:42 -0800 | [diff] [blame] | 70 | int title = R.string.roaming_alert_title; |
Pengquan Meng | 7ca05bc | 2018-08-28 17:20:19 -0700 | [diff] [blame^] | 71 | PersistableBundle carrierConfig = |
| 72 | PhoneGlobals.getInstance().getCarrierConfigForSubId(mSubId); |
| 73 | if (carrierConfig != null && carrierConfig.getBoolean( |
| 74 | CarrierConfigManager.KEY_CHECK_PRICING_WITH_CARRIER_FOR_DATA_ROAMING_BOOL)) { |
| 75 | title = R.string.roaming_check_price_warning; |
pkanwar | 7d69db0 | 2018-01-01 17:03:42 -0800 | [diff] [blame] | 76 | } |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 77 | builder.setMessage(getResources().getString(R.string.roaming_warning)) |
pkanwar | 7d69db0 | 2018-01-01 17:03:42 -0800 | [diff] [blame] | 78 | .setTitle(title) |
Salvador Martinez | 4642497 | 2016-10-28 13:28:06 -0700 | [diff] [blame] | 79 | .setIconAttribute(android.R.attr.alertDialogIcon) |
| 80 | .setPositiveButton(android.R.string.yes, this) |
| 81 | .setNegativeButton(android.R.string.no, this); |
| 82 | return builder.create(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onClick(DialogInterface dialog, int which) { |
| 87 | // let the host know that the positive button has been clicked |
| 88 | if (which == dialog.BUTTON_POSITIVE) { |
| 89 | mListener.onPositiveButtonClick(this); |
| 90 | } |
| 91 | } |
| 92 | } |