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.phone.settings; 18 19 import android.app.AlertDialog; 20 import android.app.Dialog; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.content.DialogInterface; 24 import android.content.Intent; 25 import android.os.UserHandle; 26 import android.telephony.TelephonyManager; 27 import android.text.TextUtils; 28 import android.util.Log; 29 import android.view.WindowManager; 30 31 import com.android.internal.telephony.MmiCode; 32 import com.android.internal.telephony.Phone; 33 import com.android.phone.CarrierXmlParser; 34 import com.android.phone.GsmUmtsAdditionalCallOptions; 35 import com.android.phone.GsmUmtsCallOptions; 36 import com.android.phone.PhoneGlobals; 37 import com.android.phone.PhoneUtils; 38 import com.android.phone.R; 39 40 import java.util.HashMap; 41 42 /** 43 * Utility class to help supplementary service functions and UI. 44 */ 45 public class SuppServicesUiUtil { 46 static final String LOG_TAG = "SuppServicesUiUtil"; 47 48 private static final String CLIR_ACTIVATE = "#31#"; 49 private static final String CLIR_DEACTIVATE = "*31#"; 50 51 /** 52 * show dialog for supplementary services over ut precaution. 53 * 54 * @param context The context. 55 * @param phone The Phone object. 56 * @param preferenceKey The preference's key. 57 */ showBlockingSuppServicesDialog(Context context, Phone phone, String preferenceKey)58 public static Dialog showBlockingSuppServicesDialog(Context context, Phone phone, 59 String preferenceKey) { 60 if (context == null || phone == null) { 61 return null; 62 } 63 64 String message = makeMessage(context, preferenceKey, phone); 65 66 AlertDialog.Builder builder = new AlertDialog.Builder(context); 67 DialogInterface.OnClickListener networkSettingsClickListener = 68 new Dialog.OnClickListener() { 69 @Override 70 public void onClick(DialogInterface dialog, int which) { 71 Intent intent = new Intent(Intent.ACTION_MAIN); 72 ComponentName mobileNetworkSettingsComponent = new ComponentName( 73 context.getString(R.string.mobile_network_settings_package), 74 context.getString(R.string.sims_settings_class)); 75 intent.setComponent(mobileNetworkSettingsComponent); 76 context.startActivityAsUser(intent, UserHandle.CURRENT); 77 } 78 }; 79 return builder.setMessage(message) 80 .setNeutralButton(context.getResources().getString( 81 R.string.settings_label), 82 networkSettingsClickListener) 83 .setPositiveButton(context.getResources().getString( 84 R.string.supp_service_over_ut_precautions_dialog_dismiss), null) 85 .create(); 86 } 87 makeMessage(Context context, String preferenceKey, Phone phone)88 public static String makeMessage(Context context, String preferenceKey, Phone phone) { 89 String message = ""; 90 int simSlot = (phone.getPhoneId() == 0) ? 1 : 2; 91 String suppServiceName = getSuppServiceName(context, preferenceKey); 92 93 TelephonyManager telephonyManager = 94 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 95 boolean isRoaming = telephonyManager.isNetworkRoaming(phone.getSubId()); 96 boolean isMultiSim = (telephonyManager.getSimCount() > 1); 97 98 if (!isMultiSim) { 99 if (isRoaming) { 100 message = context.getResources().getString( 101 R.string.supp_service_over_ut_precautions_roaming, suppServiceName); 102 } else { 103 message = context.getResources().getString( 104 R.string.supp_service_over_ut_precautions, suppServiceName); 105 } 106 } else { 107 if (isRoaming) { 108 message = context.getResources().getString( 109 R.string.supp_service_over_ut_precautions_roaming_dual_sim, suppServiceName, 110 simSlot); 111 } else { 112 message = context.getResources().getString( 113 R.string.supp_service_over_ut_precautions_dual_sim, suppServiceName, 114 simSlot); 115 } 116 } 117 return message; 118 } 119 getSuppServiceName(Context context, String preferenceKey)120 private static String getSuppServiceName(Context context, String preferenceKey) { 121 String suppServiceName = ""; 122 if (preferenceKey.equals(GsmUmtsCallOptions.CALL_FORWARDING_KEY)) { 123 suppServiceName = context.getResources().getString(R.string.labelCF); 124 } else if (preferenceKey.equals(GsmUmtsCallOptions.CALL_BARRING_KEY)) { 125 suppServiceName = context.getResources().getString(R.string.labelCallBarring); 126 } else if (preferenceKey.equals(GsmUmtsAdditionalCallOptions.BUTTON_CLIR_KEY)) { 127 suppServiceName = context.getResources().getString(R.string.labelCallerId); 128 } else if (preferenceKey.equals(GsmUmtsAdditionalCallOptions.BUTTON_CW_KEY)) { 129 suppServiceName = context.getResources().getString(R.string.labelCW); 130 } 131 return suppServiceName; 132 } 133 134 /** 135 * Check SS over Ut precautions in condition which is 136 * "mobile data button is off" or "Roaming button is off during roaming". 137 * 138 * @param context The context. 139 * @param phone The Phone object. 140 * @return "mobile data button is off" or "Roaming button is off during roaming", return true. 141 */ isSsOverUtPrecautions(Context context, Phone phone)142 public static boolean isSsOverUtPrecautions(Context context, Phone phone) { 143 if (phone == null || context == null) { 144 return false; 145 } 146 return isMobileDataOff(context, phone) || isDataRoamingOffUnderRoaming(context, phone); 147 } 148 isMobileDataOff(Context context, Phone phone)149 private static boolean isMobileDataOff(Context context, Phone phone) { 150 TelephonyManager telephonyManager = 151 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 152 return !telephonyManager.getDataEnabled(phone.getSubId()); 153 } 154 isDataRoamingOffUnderRoaming(Context context, Phone phone)155 private static boolean isDataRoamingOffUnderRoaming(Context context, Phone phone) { 156 TelephonyManager telephonyManager = 157 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 158 return telephonyManager.isNetworkRoaming(phone.getSubId()) 159 && !phone.getDataRoamingEnabled(); 160 } 161 162 /** 163 * To handle caller id's ussd response message which sets caller id activate or deactivate, 164 * and then sync caller id's ussd value to ss value if this command successful. 165 * 166 * @param context context to get strings. 167 * @param mmiCode MMI result. 168 * @return Text from response message is displayed on dialog . 169 * @hide 170 */ handleCallerIdUssdResponse(PhoneGlobals app, Context context, Phone phone, MmiCode mmiCode)171 public static CharSequence handleCallerIdUssdResponse(PhoneGlobals app, Context context, 172 Phone phone, MmiCode mmiCode) { 173 if (TextUtils.isEmpty(mmiCode.getDialString())) { 174 return mmiCode.getMessage(); 175 } 176 177 TelephonyManager telephonyManager = new TelephonyManager(context, phone.getSubId()); 178 int carrierId = telephonyManager.getSimCarrierId(); 179 if (carrierId == TelephonyManager.UNKNOWN_CARRIER_ID) { 180 return mmiCode.getMessage(); 181 } 182 183 CarrierXmlParser carrierXmlParser = new CarrierXmlParser(context, carrierId); 184 CarrierXmlParser.SsEntry.SSAction ssAction = carrierXmlParser.getCallerIdUssdCommandAction( 185 mmiCode.getDialString()); 186 Log.d(LOG_TAG, "handleCallerIdUssdResponse: ssAction =" + ssAction); 187 188 if (ssAction == CarrierXmlParser.SsEntry.SSAction.UNKNOWN) { 189 return mmiCode.getMessage(); 190 } 191 192 HashMap<String, String> analysisResult = carrierXmlParser.getFeature( 193 CarrierXmlParser.FEATURE_CALLER_ID) 194 .getResponseSet(ssAction, 195 mmiCode.getMessage().toString()); 196 Log.d(LOG_TAG, "handleCallerIdUssdResponse: analysisResult =" + analysisResult); 197 if (analysisResult.get(CarrierXmlParser.TAG_RESPONSE_STATUS).equals( 198 CarrierXmlParser.TAG_COMMAND_RESULT_DEFINITION_OK)) { 199 200 new Thread(new Runnable() { 201 @Override 202 public void run() { 203 TelephonyManager.UssdResponseCallback ussdCallback = 204 new TelephonyManager.UssdResponseCallback() { 205 @Override 206 public void onReceiveUssdResponse( 207 final TelephonyManager telephonyManager, 208 String request, CharSequence response) { 209 Log.d(LOG_TAG, "handleCallerIdUssdResponse: response =" 210 + response.toString()); 211 PhoneUtils.createUssdDialog(app, context, response.toString(), 212 phone, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 213 } 214 215 @Override 216 public void onReceiveUssdResponseFailed( 217 final TelephonyManager telephonyManager, 218 String request, int failureCode) { 219 Log.d(LOG_TAG, "handleCallerIdUssdResponse: failureCode =" 220 + failureCode); 221 PhoneUtils.createUssdDialog(app, context, 222 context.getText(R.string.response_error), 223 phone, WindowManager.LayoutParams.TYPE_SYSTEM_ALERT); 224 } 225 }; 226 227 String clir = ""; 228 if (ssAction == CarrierXmlParser.SsEntry.SSAction.UPDATE_ACTIVATE) { 229 clir = CLIR_ACTIVATE; 230 } else { 231 clir = CLIR_DEACTIVATE; 232 } 233 TelephonyManager telephonyManager = 234 (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 235 telephonyManager.sendUssdRequest(clir, ussdCallback, null); 236 } 237 }).start(); 238 239 return ""; 240 } else { 241 return context.getText( 242 com.android.internal.R.string.mmiError); 243 } 244 } 245 } 246