• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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;
21 import android.app.Fragment;
22 import android.app.FragmentManager;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.content.DialogInterface.OnClickListener;
26 import android.os.Bundle;
27 import android.os.PersistableBundle;
28 import android.telephony.CarrierConfigManager;
29 
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 
36     public static final String SUB_ID_KEY = "sub_id_key";
37 
38     private CarrierConfigManager mCarrierConfigManager;
39     private int mSubId;
40 
41     /**
42      * The interface we expect a host activity to implement.
43      */
44     public interface RoamingDialogListener {
onPositiveButtonClick(DialogFragment dialog)45         void onPositiveButtonClick(DialogFragment dialog);
46     }
47 
48     // the host activity which implements the listening interface
49     private RoamingDialogListener mListener;
50 
51     @Override
onAttach(Context context)52     public void onAttach(Context context) {
53         super.onAttach(context);
54         Bundle args = getArguments();
55         mSubId = args.getInt(SUB_ID_KEY);
56         mCarrierConfigManager = new CarrierConfigManager(context);
57 
58         // Verify host activity implemented callback interface
59         FragmentManager fragmentManager = getFragmentManager();
60         Fragment fragment = fragmentManager.findFragmentById(R.id.network_setting_content);
61         try {
62             mListener = (RoamingDialogListener) fragment;
63         } catch (ClassCastException e) {
64             throw new ClassCastException(fragment.toString() +
65                     "must implement RoamingDialogListener");
66         }
67     }
68 
69     @Override
onCreateDialog(Bundle savedInstanceState)70     public Dialog onCreateDialog(Bundle savedInstanceState) {
71         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
72         int title = R.string.roaming_alert_title;
73         PersistableBundle carrierConfig = mCarrierConfigManager.getConfigForSubId(mSubId);
74         if (carrierConfig != null && carrierConfig.getBoolean(
75                 CarrierConfigManager.KEY_CHECK_PRICING_WITH_CARRIER_FOR_DATA_ROAMING_BOOL)) {
76             title = R.string.roaming_check_price_warning;
77         }
78         builder.setMessage(getResources().getString(R.string.roaming_warning))
79                 .setTitle(title)
80                 .setIconAttribute(android.R.attr.alertDialogIcon)
81                 .setPositiveButton(android.R.string.yes, this)
82                 .setNegativeButton(android.R.string.no, this);
83         return builder.create();
84     }
85 
86     @Override
onClick(DialogInterface dialog, int which)87     public void onClick(DialogInterface dialog, int which) {
88         // let the host know that the positive button has been clicked
89         if (which == dialog.BUTTON_POSITIVE) {
90             mListener.onPositiveButtonClick(this);
91         }
92     }
93 }
94