Home | History | Annotate | Download | only in telecomeventui
      1 /*
      2  * Copyright (C) 2017 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 
     17 package com.android.incallui.telecomeventui;
     18 
     19 import android.app.AlertDialog;
     20 import android.app.Dialog;
     21 import android.content.Context;
     22 import android.content.SharedPreferences;
     23 import android.os.Bundle;
     24 import android.preference.PreferenceManager;
     25 import android.support.annotation.NonNull;
     26 import android.support.annotation.Nullable;
     27 import android.support.annotation.VisibleForTesting;
     28 import android.support.v4.app.DialogFragment;
     29 import android.support.v4.os.UserManagerCompat;
     30 import android.view.View;
     31 import android.widget.CheckBox;
     32 import com.android.dialer.common.Assert;
     33 import com.android.dialer.common.LogUtil;
     34 
     35 /**
     36  * Dialog that may be shown when users place an outgoing call to an international number while on
     37  * Wifi.
     38  *
     39  * <p>The android.telephony.event.EVENT_NOTIFY_INTERNATIONAL_CALL_ON_WFC event is sent when users
     40  * attempt to place a call under these circumstances.
     41  */
     42 public class InternationalCallOnWifiDialogFragment extends DialogFragment {
     43 
     44   /**
     45    * Returns {@code true} if an {@link InternationalCallOnWifiDialogFragment} should be shown.
     46    *
     47    * <p>Attempting to show an InternationalCallOnWifiDialogFragment when this method returns {@code
     48    * false} will result in an {@link IllegalStateException}.
     49    */
     50   public static boolean shouldShow(@NonNull Context context) {
     51     if (!UserManagerCompat.isUserUnlocked(context)) {
     52       LogUtil.i("InternationalCallOnWifiDialogFragment.shouldShow", "user locked, returning false");
     53       return false;
     54     }
     55 
     56     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
     57     boolean shouldShow = preferences.getBoolean(ALWAYS_SHOW_WARNING_PREFERENCE_KEY, true);
     58 
     59     LogUtil.i("InternationalCallOnWifiDialogFragment.shouldShow", "result: %b", shouldShow);
     60     return shouldShow;
     61   }
     62 
     63   /**
     64    * Called in response to user interaction with the {@link InternationalCallOnWifiDialogFragment}.
     65    */
     66   public interface Callback {
     67 
     68     /** Indicates that the user wishes to proceed with the call represented by the given call id. */
     69     void continueCall(@NonNull String callId);
     70 
     71     /** Indicates that the user wishes to cancel the call represented by the given call id. */
     72     void cancelCall(@NonNull String callId);
     73   }
     74 
     75   /**
     76    * Returns a new instance of {@link InternationalCallOnWifiDialogFragment} with the given
     77    * callback.
     78    *
     79    * <p>Prefer this method over the default constructor.
     80    */
     81   public static InternationalCallOnWifiDialogFragment newInstance(
     82       @NonNull String callId, @NonNull Callback callback) {
     83     InternationalCallOnWifiDialogFragment fragment = new InternationalCallOnWifiDialogFragment();
     84     fragment.setCallback(callback);
     85     Bundle args = new Bundle();
     86     args.putString(ARG_CALL_ID, Assert.isNotNull(callId));
     87     fragment.setArguments(args);
     88     return fragment;
     89   }
     90 
     91   /**
     92    * Key to the preference used to determine if the user wants to see {@link
     93    * InternationalCallOnWifiDialogFragment InternationalCallOnWifiDialogFragments}.
     94    */
     95   @VisibleForTesting
     96   static final String ALWAYS_SHOW_WARNING_PREFERENCE_KEY =
     97       "ALWAYS_SHOW_INTERNATIONAL_CALL_ON_WIFI_WARNING";
     98 
     99   /** Key in the arguments bundle for call id. */
    100   private static final String ARG_CALL_ID = "call_id";
    101 
    102   /**
    103    * Callback which will receive information about user interactions with this dialog.
    104    *
    105    * <p>This is Nullable in the event that the dialog is destroyed by the framework, but doesn't
    106    * have a callback reattached. Ideally, the InCallActivity would implement the callback and we
    107    * would use FragmentUtils.getParentUnsafe instead of holding onto the callback here, but that's
    108    * not possible with the existing InCallActivity/InCallActivityCommon implementation.
    109    */
    110   @Nullable private Callback callback;
    111 
    112   /**
    113    * Sets the callback for this dialog.
    114    *
    115    * <p>Used to reset the callback after state changes.
    116    */
    117   public void setCallback(@NonNull Callback callback) {
    118     this.callback = Assert.isNotNull(callback);
    119   }
    120 
    121   @NonNull
    122   @Override
    123   public Dialog onCreateDialog(Bundle bundle) {
    124     super.onCreateDialog(bundle);
    125     LogUtil.enterBlock("InternationalCallOnWifiDialogFragment.onCreateDialog");
    126 
    127     if (!InternationalCallOnWifiDialogFragment.shouldShow(getActivity())) {
    128       throw new IllegalStateException(
    129           "shouldShow indicated InternationalCallOnWifiDialogFragment should not have showed");
    130     }
    131 
    132     View dialogView =
    133         View.inflate(getActivity(), R.layout.frag_international_call_on_wifi_dialog, null);
    134 
    135     CheckBox alwaysWarn = dialogView.findViewById(R.id.always_warn);
    136 
    137     SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
    138     // The default is set to false in this case to ensure that the first time the dialog opens,
    139     // the checkbox is unchecked.
    140     alwaysWarn.setChecked(preferences.getBoolean(ALWAYS_SHOW_WARNING_PREFERENCE_KEY, false));
    141 
    142     AlertDialog alertDialog =
    143         new AlertDialog.Builder(getActivity(), R.style.AlertDialogTheme)
    144             .setCancelable(false)
    145             .setView(dialogView)
    146             .setPositiveButton(
    147                 android.R.string.ok,
    148                 (dialog, which) -> onPositiveButtonClick(preferences, alwaysWarn.isChecked()))
    149             .setNegativeButton(
    150                 android.R.string.cancel,
    151                 (dialog, which) -> onNegativeButtonClick(preferences, alwaysWarn.isChecked()))
    152             .create();
    153 
    154     alertDialog.setCanceledOnTouchOutside(false);
    155     return alertDialog;
    156   }
    157 
    158   private void onPositiveButtonClick(@NonNull SharedPreferences preferences, boolean alwaysWarn) {
    159     LogUtil.i(
    160         "InternationalCallOnWifiDialogFragment.onPositiveButtonClick",
    161         "alwaysWarn: %b",
    162         alwaysWarn);
    163     preferences.edit().putBoolean(ALWAYS_SHOW_WARNING_PREFERENCE_KEY, alwaysWarn).apply();
    164 
    165     // Neither callback nor callId are null in normal circumstances. See comments on callback
    166     callback.continueCall(getArguments().getString(ARG_CALL_ID));
    167   }
    168 
    169   private void onNegativeButtonClick(@NonNull SharedPreferences preferences, boolean alwaysWarn) {
    170     LogUtil.i(
    171         "InternationalCallOnWifiDialogFragment.onNegativeButtonClick",
    172         "alwaysWarn: %b",
    173         alwaysWarn);
    174     preferences.edit().putBoolean(ALWAYS_SHOW_WARNING_PREFERENCE_KEY, alwaysWarn).apply();
    175 
    176     // Neither callback nor callId are null in normal circumstances. See comments on callback
    177     callback.cancelCall(getArguments().getString(ARG_CALL_ID));
    178   }
    179 }
    180