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