Home | History | Annotate | Download | only in phone
      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 import com.android.internal.telephony.Phone;
     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 DialogFragment implements OnClickListener {
     37 
     38     /**
     39      * The interface we expect a host activity to implement.
     40      */
     41     public interface RoamingDialogListener {
     42         void onPositiveButtonClick(DialogFragment dialog);
     43     }
     44 
     45     // the host activity which implements the listening interface
     46     private RoamingDialogListener mListener;
     47 
     48     private Phone mPhone;
     49 
     50     public void setPhone(Phone phone) {
     51         mPhone = phone;
     52     }
     53 
     54     @Override
     55     public void onAttach(Context context) {
     56         super.onAttach(context);
     57         // Verify host activity implemented callback interface
     58         FragmentManager fragmentManager = getFragmentManager();
     59         Fragment fragment = fragmentManager.findFragmentById(R.id.network_setting_content);
     60         try {
     61             mListener = (RoamingDialogListener) fragment;
     62         } catch (ClassCastException e) {
     63             throw new ClassCastException(fragment.toString() +
     64                     "must implement RoamingDialogListener");
     65         }
     66     }
     67 
     68     @Override
     69     public Dialog onCreateDialog(Bundle savedInstanceState) {
     70         AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
     71         int title = R.string.roaming_alert_title;
     72         if (mPhone != null) {
     73             PersistableBundle carrierConfig =
     74                     PhoneGlobals.getInstance().getCarrierConfigForSubId(mPhone.getSubId());
     75             if (carrierConfig != null && carrierConfig.getBoolean(
     76                     CarrierConfigManager.KEY_CHECK_PRICING_WITH_CARRIER_FOR_DATA_ROAMING_BOOL)) {
     77                 title = R.string.roaming_check_price_warning;
     78             }
     79         }
     80         builder.setMessage(getResources().getString(R.string.roaming_warning))
     81                 .setTitle(title)
     82                 .setIconAttribute(android.R.attr.alertDialogIcon)
     83                 .setPositiveButton(android.R.string.yes, this)
     84                 .setNegativeButton(android.R.string.no, this);
     85         return builder.create();
     86     }
     87 
     88     @Override
     89     public void onClick(DialogInterface dialog, int which) {
     90         // let the host know that the positive button has been clicked
     91         if (which == dialog.BUTTON_POSITIVE) {
     92             mListener.onPositiveButtonClick(this);
     93         }
     94     }
     95 }
     96