1 /*
2  * Copyright (C) 2018 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.settings.network.telephony;
17 
18 import android.app.AlertDialog;
19 import android.app.Dialog;
20 import android.app.settings.SettingsEnums;
21 import android.content.Context;
22 import android.content.DialogInterface;
23 import android.content.DialogInterface.OnClickListener;
24 import android.os.Bundle;
25 import android.os.PersistableBundle;
26 import android.telephony.CarrierConfigManager;
27 import android.telephony.TelephonyManager;
28 
29 import com.android.settings.R;
30 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
31 
32 /**
33  * A dialog fragment that asks the user if they are sure they want to turn on data roaming
34  * to avoid accidental charges.
35  */
36 public class RoamingDialogFragment extends InstrumentedDialogFragment implements OnClickListener {
37 
38     public static final String SUB_ID_KEY = "sub_id_key";
39 
40     private CarrierConfigManager mCarrierConfigManager;
41     private int mSubId;
42 
newInstance(int subId)43     public static RoamingDialogFragment newInstance(int subId) {
44         final RoamingDialogFragment dialogFragment = new RoamingDialogFragment();
45         final Bundle args = new Bundle();
46         args.putInt(SUB_ID_KEY, subId);
47         dialogFragment.setArguments(args);
48 
49         return dialogFragment;
50     }
51 
52     @Override
onAttach(Context context)53     public void onAttach(Context context) {
54         super.onAttach(context);
55         final Bundle args = getArguments();
56         mSubId = args.getInt(SUB_ID_KEY);
57         mCarrierConfigManager = context.getSystemService(CarrierConfigManager.class);
58     }
59 
60     @Override
onCreateDialog(Bundle savedInstanceState)61     public Dialog onCreateDialog(Bundle savedInstanceState) {
62         final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
63         final int title = R.string.roaming_alert_title;
64         int message = R.string.roaming_warning;
65         final PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
66         if (carrierConfig != null && carrierConfig.getBoolean(
67                 CarrierConfigManager.KEY_CHECK_PRICING_WITH_CARRIER_FOR_DATA_ROAMING_BOOL)) {
68             message = R.string.roaming_check_price_warning;
69         }
70         builder.setMessage(getResources().getString(message))
71                 .setTitle(title)
72                 .setIconAttribute(android.R.attr.alertDialogIcon)
73                 .setPositiveButton(android.R.string.yes, this)
74                 .setNegativeButton(android.R.string.no, this);
75         return builder.create();
76     }
77 
78     @Override
getMetricsCategory()79     public int getMetricsCategory() {
80         return SettingsEnums.MOBILE_ROAMING_DIALOG;
81     }
82 
83     @Override
onClick(DialogInterface dialog, int which)84     public void onClick(DialogInterface dialog, int which) {
85         // let the host know that the positive button has been clicked
86         if (which == dialog.BUTTON_POSITIVE) {
87             final TelephonyManager telephonyManager =
88                     getContext().getSystemService(TelephonyManager.class)
89                     .createForSubscriptionId(mSubId);
90             if (telephonyManager == null) {
91                 return;
92             }
93             telephonyManager.setDataRoamingEnabled(true);
94         }
95     }
96 }
97