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 17 package com.android.incallui.video.impl; 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.VisibleForTesting; 27 import android.support.v4.app.DialogFragment; 28 import android.support.v4.os.UserManagerCompat; 29 import android.telecom.Call.Details; 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 import com.android.incallui.call.CallList; 35 import com.android.incallui.call.DialerCall; 36 37 /** Alert dialog for video charges. */ 38 public class VideoChargesAlertDialogFragment extends DialogFragment { 39 40 /** Preference key for whether to show the alert dialog for video charges next time. */ 41 @VisibleForTesting 42 static final String KEY_DO_NOT_SHOW_VIDEO_CHARGES_ALERT = "key_do_not_show_video_charges_alert"; 43 44 /** Key in the arguments bundle for call id. */ 45 private static final String ARG_CALL_ID = "call_id"; 46 47 /** 48 * Returns {@code true} if an {@link VideoChargesAlertDialogFragment} should be shown. 49 * 50 * <p>Attempting to show an VideoChargesAlertDialogFragment when this method returns {@code false} 51 * will result in an {@link IllegalStateException}. 52 */ shouldShow(@onNull Context context, String callId)53 public static boolean shouldShow(@NonNull Context context, String callId) { 54 DialerCall call = CallList.getInstance().getCallById(callId); 55 if (call == null) { 56 LogUtil.i("VideoChargesAlertDialogFragment.shouldShow", "null call"); 57 return false; 58 } 59 60 if (call.hasProperty(Details.PROPERTY_WIFI)) { 61 return false; 62 } 63 64 if (call.didDismissVideoChargesAlertDialog()) { 65 LogUtil.i( 66 "VideoChargesAlertDialogFragment.shouldShow", "The dialog has been dismissed by user"); 67 return false; 68 } 69 70 if (!call.showVideoChargesAlertDialog()) { 71 return false; 72 } 73 74 if (!UserManagerCompat.isUserUnlocked(context)) { 75 LogUtil.i("VideoChargesAlertDialogFragment.shouldShow", "user locked, returning false"); 76 return false; 77 } 78 79 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context); 80 if (preferences.getBoolean(KEY_DO_NOT_SHOW_VIDEO_CHARGES_ALERT, false)) { 81 LogUtil.i( 82 "VideoChargesAlertDialogFragment.shouldShow", 83 "Video charges alert has been disabled by user, returning false"); 84 return false; 85 } 86 87 return true; 88 } 89 90 /** 91 * Returns a new instance of {@link VideoChargesAlertDialogFragment} 92 * 93 * <p>Prefer this method over the default constructor. 94 */ newInstance(@onNull String callId)95 public static VideoChargesAlertDialogFragment newInstance(@NonNull String callId) { 96 VideoChargesAlertDialogFragment fragment = new VideoChargesAlertDialogFragment(); 97 Bundle args = new Bundle(); 98 args.putString(ARG_CALL_ID, Assert.isNotNull(callId)); 99 fragment.setArguments(args); 100 return fragment; 101 } 102 103 @NonNull 104 @Override onCreateDialog(Bundle bundle)105 public Dialog onCreateDialog(Bundle bundle) { 106 super.onCreateDialog(bundle); 107 108 if (!VideoChargesAlertDialogFragment.shouldShow( 109 getActivity(), getArguments().getString(ARG_CALL_ID))) { 110 throw new IllegalStateException( 111 "shouldShow indicated VideoChargesAlertDialogFragment should not have showed"); 112 } 113 114 View dialogView = View.inflate(getActivity(), R.layout.frag_video_charges_alert_dialog, null); 115 116 CheckBox alertCheckBox = dialogView.findViewById(R.id.do_not_show); 117 118 SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity()); 119 AlertDialog alertDialog = 120 new AlertDialog.Builder(getActivity()) 121 .setView(dialogView) 122 .setPositiveButton( 123 android.R.string.ok, 124 (dialog, which) -> onPositiveButtonClicked(preferences, alertCheckBox.isChecked())) 125 .create(); 126 this.setCancelable(false); 127 return alertDialog; 128 } 129 onPositiveButtonClicked(@onNull SharedPreferences preferences, boolean isChecked)130 private void onPositiveButtonClicked(@NonNull SharedPreferences preferences, boolean isChecked) { 131 LogUtil.i( 132 "VideoChargesAlertDialogFragment.onPositiveButtonClicked", "isChecked: %b", isChecked); 133 preferences.edit().putBoolean(KEY_DO_NOT_SHOW_VIDEO_CHARGES_ALERT, isChecked).apply(); 134 135 DialerCall dialerCall = 136 CallList.getInstance().getCallById(getArguments().getString(ARG_CALL_ID)); 137 if (dialerCall != null) { 138 dialerCall.setDidDismissVideoChargesAlertDialog(true); 139 } 140 } 141 } 142