blob: c47a37df4e0edaec2411ce54d36e7ae8361bfb26 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 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
17package com.android.phone;
18
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Ta-wei Yen1b455992016-12-27 14:52:32 -080021import android.Manifest.permission;
Ta-wei Yen243b6372017-01-10 16:17:08 -080022import android.annotation.Nullable;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070023import android.app.ActivityManager;
24import android.app.AppOpsManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -080025import android.app.PendingIntent;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070026import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070027import android.content.Context;
28import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070029import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080030import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070031import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070032import android.net.Uri;
33import android.os.AsyncResult;
34import android.os.Binder;
35import android.os.Bundle;
36import android.os.Handler;
yinxu34eed212017-04-12 16:03:22 -070037import android.os.IBinder;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.Looper;
39import android.os.Message;
yinxu34eed212017-04-12 16:03:22 -070040import android.os.Messenger;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070041import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070042import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.os.ServiceManager;
44import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070045import android.os.UserManager;
Sooraj Sasindran22882212016-07-18 11:57:25 -070046import android.os.WorkSource;
Derek Tan97ebb422014-09-05 16:55:38 -070047import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080048import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070049import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080050import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080051import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070052import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070053import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.telephony.CellInfo;
Sooraj Sasindran22882212016-07-18 11:57:25 -070055import android.telephony.ClientRequestStats;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070056import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070057import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080058import android.telephony.NeighboringCellInfo;
yinxu34eed212017-04-12 16:03:22 -070059import android.telephony.NetworkScanRequest;
Wink Saville5d475dd2014-10-17 15:00:58 -070060import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.telephony.ServiceState;
Ta-wei Yen1b455992016-12-27 14:52:32 -080062import android.telephony.SmsManager;
Nathan Harold46b42aa2017-03-10 19:38:22 -080063import android.telephony.SignalStrength;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080064import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080065import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070066import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070067import android.telephony.TelephonyManager;
68import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080070import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080072import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080073import android.util.Slog;
Ta-wei Yen1b455992016-12-27 14:52:32 -080074
Andrew Lee312e8172014-10-23 17:01:36 -070075import com.android.ims.ImsManager;
Brad Ebinger76681002017-01-23 13:50:20 -080076import com.android.ims.internal.IImsServiceController;
77import com.android.ims.internal.IImsServiceFeatureListener;
Shishir Agrawal566b7612013-10-28 14:41:00 -070078import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070079import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070080import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070082import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080083import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070085import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086import com.android.internal.telephony.Phone;
Nathan Harold8f68faa2016-12-14 11:27:20 -080087import com.android.internal.telephony.PhoneConstantConversions;
Ta-wei Yen87c49842016-05-13 21:19:52 -070088import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070089import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070090import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070091import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070092import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080093import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070094import com.android.internal.telephony.uicc.IccIoResult;
95import com.android.internal.telephony.uicc.IccUtils;
Nathan Haroldb3014052017-01-25 15:57:32 -080096import com.android.internal.telephony.uicc.SIMRecords;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070097import com.android.internal.telephony.uicc.UiccCard;
Nathan Haroldb3014052017-01-25 15:57:32 -080098import com.android.internal.telephony.uicc.UiccCardApplication;
Shishir Agrawal566b7612013-10-28 14:41:00 -070099import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -0800100import com.android.internal.util.HexDump;
Ta-wei Yenb0f695b2016-08-08 17:33:11 -0700101import com.android.phone.settings.VisualVoicemailSettingsUtil;
Nancy Chen31f9ba12016-01-06 11:42:12 -0800102import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Ta-wei Yen463efd12017-01-06 15:29:25 -0800103import com.android.phone.vvm.RemoteVvmTaskManager;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800104
Ta-wei Yenc236d6b2016-06-21 13:33:12 -0700105import java.io.FileDescriptor;
106import java.io.PrintWriter;
Ta-wei Yen1b455992016-12-27 14:52:32 -0800107import java.nio.charset.StandardCharsets;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700108import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800109import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -0800110import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100111import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800112import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700113
114/**
115 * Implementation of the ITelephony interface.
116 */
Santos Cordon117fee72014-05-16 17:56:12 -0700117public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700118 private static final String LOG_TAG = "PhoneInterfaceManager";
119 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
120 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800121 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700122
123 // Message codes used with mMainThreadHandler
124 private static final int CMD_HANDLE_PIN_MMI = 1;
125 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
126 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
127 private static final int CMD_ANSWER_RINGING_CALL = 4;
128 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700129 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
130 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700131 private static final int CMD_OPEN_CHANNEL = 9;
132 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
133 private static final int CMD_CLOSE_CHANNEL = 11;
134 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800135 private static final int CMD_NV_READ_ITEM = 13;
136 private static final int EVENT_NV_READ_ITEM_DONE = 14;
137 private static final int CMD_NV_WRITE_ITEM = 15;
138 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
139 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
140 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
141 private static final int CMD_NV_RESET_CONFIG = 19;
142 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800143 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
144 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
145 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
146 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800147 private static final int CMD_SEND_ENVELOPE = 25;
148 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700149 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
150 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
151 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
152 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
153 private static final int CMD_EXCHANGE_SIM_IO = 31;
154 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800155 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
156 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700157 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
158 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700159 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
160 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700161 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
162 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
163 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
164 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700165 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
166 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
167 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
168 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
pkanwar7e856482016-10-14 19:37:38 -0700169 private static final int CMD_HANDLE_USSD_REQUEST = 47;
Nathan Haroldb3014052017-01-25 15:57:32 -0800170 private static final int CMD_GET_FORBIDDEN_PLMNS = 48;
171 private static final int EVENT_GET_FORBIDDEN_PLMNS_DONE = 49;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700172
173 /** The singleton instance. */
174 private static PhoneInterfaceManager sInstance;
175
Wink Saville3ab207e2014-11-20 13:07:20 -0800176 private PhoneGlobals mApp;
177 private Phone mPhone;
178 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700179 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800180 private AppOpsManager mAppOps;
181 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800182 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800183 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700184
Derek Tan97ebb422014-09-05 16:55:38 -0700185 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
186 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800187 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700188
189 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700190 * A request object to use for transmitting data to an ICC.
191 */
192 private static final class IccAPDUArgument {
193 public int channel, cla, command, p1, p2, p3;
194 public String data;
195
196 public IccAPDUArgument(int channel, int cla, int command,
197 int p1, int p2, int p3, String data) {
198 this.channel = channel;
199 this.cla = cla;
200 this.command = command;
201 this.p1 = p1;
202 this.p2 = p2;
203 this.p3 = p3;
204 this.data = data;
205 }
206 }
207
208 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700209 * A request object to use for transmitting data to an ICC.
210 */
211 private static final class ManualNetworkSelectionArgument {
212 public OperatorInfo operatorInfo;
213 public boolean persistSelection;
214
215 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
216 this.operatorInfo = operatorInfo;
217 this.persistSelection = persistSelection;
218 }
219 }
220
221 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700222 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
223 * request after sending. The main thread will notify the request when it is complete.
224 */
225 private static final class MainThreadRequest {
226 /** The argument to use for the request */
227 public Object argument;
228 /** The result of the request that is run on the main thread */
229 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800230 // The subscriber id that this request applies to. Defaults to
231 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
232 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700233
234 public MainThreadRequest(Object argument) {
235 this.argument = argument;
236 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800237
238 public MainThreadRequest(Object argument, Integer subId) {
239 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800240 if (subId != null) {
241 this.subId = subId;
242 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800243 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244 }
245
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800246 private static final class IncomingThirdPartyCallArgs {
247 public final ComponentName component;
248 public final String callId;
249 public final String callerDisplayName;
250
251 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
252 String callerDisplayName) {
253 this.component = component;
254 this.callId = callId;
255 this.callerDisplayName = callerDisplayName;
256 }
257 }
258
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700259 /**
260 * A handler that processes messages on the main thread in the phone process. Since many
261 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
262 * inbound binder threads to the main thread in the phone process. The Binder thread
263 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
264 * on, which will be notified when the operation completes and will contain the result of the
265 * request.
266 *
267 * <p>If a MainThreadRequest object is provided in the msg.obj field,
268 * note that request.result must be set to something non-null for the calling thread to
269 * unblock.
270 */
271 private final class MainThreadHandler extends Handler {
272 @Override
273 public void handleMessage(Message msg) {
274 MainThreadRequest request;
275 Message onCompleted;
276 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800277 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700278 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700279
280 switch (msg.what) {
pkanwar7e856482016-10-14 19:37:38 -0700281 case CMD_HANDLE_USSD_REQUEST: {
282 request = (MainThreadRequest) msg.obj;
283 final Phone phone = getPhoneFromRequest(request);
284 Pair<String, ResultReceiver> ussdObject = (Pair) request.argument;
285 String ussdRequest = ussdObject.first;
286 ResultReceiver wrappedCallback = ussdObject.second;
287 request.result = phone != null ?
288 phone.handleUssdRequest(ussdRequest, wrappedCallback)
289 :false;
290 // Wake up the requesting thread
291 synchronized (request) {
292 request.notifyAll();
293 }
294 break;
295 }
296
Yorke Lee716f67e2015-06-17 15:39:16 -0700297 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700298 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700299 final Phone phone = getPhoneFromRequest(request);
300 request.result = phone != null ?
301 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
302 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 // Wake up the requesting thread
304 synchronized (request) {
305 request.notifyAll();
306 }
307 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700308 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309
310 case CMD_HANDLE_NEIGHBORING_CELL:
311 request = (MainThreadRequest) msg.obj;
312 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
313 request);
Sooraj Sasindran22882212016-07-18 11:57:25 -0700314 mPhone.getNeighboringCids(onCompleted, (WorkSource)request.argument);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 break;
316
317 case EVENT_NEIGHBORING_CELL_DONE:
318 ar = (AsyncResult) msg.obj;
319 request = (MainThreadRequest) ar.userObj;
320 if (ar.exception == null && ar.result != null) {
321 request.result = ar.result;
322 } else {
323 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800324 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700325 }
326 // Wake up the requesting thread
327 synchronized (request) {
328 request.notifyAll();
329 }
330 break;
331
332 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700333 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800334 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700335 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700336 break;
337
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700338 case CMD_END_CALL:
339 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800340 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700341 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700342 Phone phone = getPhone(end_subId);
343 if (phone == null) {
344 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
345 break;
346 }
347 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700348 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
349 // CDMA: If the user presses the Power button we treat it as
350 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700351 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700352 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
353 // GSM: End the call as per the Phone state
354 hungUp = PhoneUtils.hangup(mCM);
355 } else {
356 throw new IllegalStateException("Unexpected phone type: " + phoneType);
357 }
358 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
359 request.result = hungUp;
360 // Wake up the requesting thread
361 synchronized (request) {
362 request.notifyAll();
363 }
364 break;
365
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700366 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700367 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700368 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800369 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700370 if (uiccCard == null) {
371 loge("iccTransmitApduLogicalChannel: No UICC");
372 request.result = new IccIoResult(0x6F, 0, (byte[])null);
373 synchronized (request) {
374 request.notifyAll();
375 }
376 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700377 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
378 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700379 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700380 iccArgument.channel, iccArgument.cla, iccArgument.command,
381 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700382 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700383 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700384 break;
385
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700386 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700387 ar = (AsyncResult) msg.obj;
388 request = (MainThreadRequest) ar.userObj;
389 if (ar.exception == null && ar.result != null) {
390 request.result = ar.result;
391 } else {
392 request.result = new IccIoResult(0x6F, 0, (byte[])null);
393 if (ar.result == null) {
394 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800395 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700396 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800397 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700398 } else {
399 loge("iccTransmitApduLogicalChannel: Unknown exception");
400 }
401 }
402 synchronized (request) {
403 request.notifyAll();
404 }
405 break;
406
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700407 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
408 request = (MainThreadRequest) msg.obj;
409 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800410 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700411 if (uiccCard == null) {
412 loge("iccTransmitApduBasicChannel: No UICC");
413 request.result = new IccIoResult(0x6F, 0, (byte[])null);
414 synchronized (request) {
415 request.notifyAll();
416 }
417 } else {
418 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
419 request);
420 uiccCard.iccTransmitApduBasicChannel(
421 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
422 iccArgument.p3, iccArgument.data, onCompleted);
423 }
424 break;
425
426 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
427 ar = (AsyncResult) msg.obj;
428 request = (MainThreadRequest) ar.userObj;
429 if (ar.exception == null && ar.result != null) {
430 request.result = ar.result;
431 } else {
432 request.result = new IccIoResult(0x6F, 0, (byte[])null);
433 if (ar.result == null) {
434 loge("iccTransmitApduBasicChannel: Empty response");
435 } else if (ar.exception instanceof CommandException) {
436 loge("iccTransmitApduBasicChannel: CommandException: " +
437 ar.exception);
438 } else {
439 loge("iccTransmitApduBasicChannel: Unknown exception");
440 }
441 }
442 synchronized (request) {
443 request.notifyAll();
444 }
445 break;
446
447 case CMD_EXCHANGE_SIM_IO:
448 request = (MainThreadRequest) msg.obj;
449 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800450 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700451 if (uiccCard == null) {
452 loge("iccExchangeSimIO: No UICC");
453 request.result = new IccIoResult(0x6F, 0, (byte[])null);
454 synchronized (request) {
455 request.notifyAll();
456 }
457 } else {
458 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
459 request);
460 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
461 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
462 iccArgument.data, onCompleted);
463 }
464 break;
465
466 case EVENT_EXCHANGE_SIM_IO_DONE:
467 ar = (AsyncResult) msg.obj;
468 request = (MainThreadRequest) ar.userObj;
469 if (ar.exception == null && ar.result != null) {
470 request.result = ar.result;
471 } else {
472 request.result = new IccIoResult(0x6f, 0, (byte[])null);
473 }
474 synchronized (request) {
475 request.notifyAll();
476 }
477 break;
478
Derek Tan4d5e5c12014-02-04 11:54:58 -0800479 case CMD_SEND_ENVELOPE:
480 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800481 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700482 if (uiccCard == null) {
483 loge("sendEnvelopeWithStatus: No UICC");
484 request.result = new IccIoResult(0x6F, 0, (byte[])null);
485 synchronized (request) {
486 request.notifyAll();
487 }
488 } else {
489 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
490 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
491 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800492 break;
493
494 case EVENT_SEND_ENVELOPE_DONE:
495 ar = (AsyncResult) msg.obj;
496 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700497 if (ar.exception == null && ar.result != null) {
498 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800499 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700500 request.result = new IccIoResult(0x6F, 0, (byte[])null);
501 if (ar.result == null) {
502 loge("sendEnvelopeWithStatus: Empty response");
503 } else if (ar.exception instanceof CommandException) {
504 loge("sendEnvelopeWithStatus: CommandException: " +
505 ar.exception);
506 } else {
507 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
508 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800509 }
510 synchronized (request) {
511 request.notifyAll();
512 }
513 break;
514
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 case CMD_OPEN_CHANNEL:
516 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800517 uiccCard = getUiccCardFromRequest(request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800518 Pair<String, Integer> openChannelArgs = (Pair<String, Integer>) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700519 if (uiccCard == null) {
520 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800521 request.result = new IccOpenLogicalChannelResponse(-1,
522 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700523 synchronized (request) {
524 request.notifyAll();
525 }
526 } else {
527 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
Ajay Nambiabd73502015-12-03 13:50:00 -0800528 uiccCard.iccOpenLogicalChannel(openChannelArgs.first,
529 openChannelArgs.second, onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700530 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 break;
532
533 case EVENT_OPEN_CHANNEL_DONE:
534 ar = (AsyncResult) msg.obj;
535 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700536 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700537 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700538 int[] result = (int[]) ar.result;
539 int channelId = result[0];
540 byte[] selectResponse = null;
541 if (result.length > 1) {
542 selectResponse = new byte[result.length - 1];
543 for (int i = 1; i < result.length; ++i) {
544 selectResponse[i - 1] = (byte) result[i];
545 }
546 }
547 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700548 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700549 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 if (ar.result == null) {
551 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700553 if (ar.exception != null) {
554 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
555 }
556
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700557 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700558 if (ar.exception instanceof CommandException) {
559 CommandException.Error error =
560 ((CommandException) (ar.exception)).getCommandError();
561 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700562 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700563 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700564 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700565 }
566 }
567 openChannelResp = new IccOpenLogicalChannelResponse(
568 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700569 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700570 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700571 synchronized (request) {
572 request.notifyAll();
573 }
574 break;
575
576 case CMD_CLOSE_CHANNEL:
577 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800578 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700579 if (uiccCard == null) {
580 loge("iccCloseLogicalChannel: No UICC");
581 request.result = new IccIoResult(0x6F, 0, (byte[])null);
582 synchronized (request) {
583 request.notifyAll();
584 }
585 } else {
586 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
587 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
588 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700589 break;
590
591 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800592 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
593 break;
594
595 case CMD_NV_READ_ITEM:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
598 mPhone.nvReadItem((Integer) request.argument, onCompleted);
599 break;
600
601 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700602 ar = (AsyncResult) msg.obj;
603 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800604 if (ar.exception == null && ar.result != null) {
605 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700606 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800607 request.result = "";
608 if (ar.result == null) {
609 loge("nvReadItem: Empty response");
610 } else if (ar.exception instanceof CommandException) {
611 loge("nvReadItem: CommandException: " +
612 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700613 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800614 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700615 }
616 }
617 synchronized (request) {
618 request.notifyAll();
619 }
620 break;
621
Jake Hambye994d462014-02-03 13:10:13 -0800622 case CMD_NV_WRITE_ITEM:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
625 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
626 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
627 break;
628
629 case EVENT_NV_WRITE_ITEM_DONE:
630 handleNullReturnEvent(msg, "nvWriteItem");
631 break;
632
633 case CMD_NV_WRITE_CDMA_PRL:
634 request = (MainThreadRequest) msg.obj;
635 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
636 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
637 break;
638
639 case EVENT_NV_WRITE_CDMA_PRL_DONE:
640 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
641 break;
642
643 case CMD_NV_RESET_CONFIG:
644 request = (MainThreadRequest) msg.obj;
645 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
646 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
647 break;
648
649 case EVENT_NV_RESET_CONFIG_DONE:
650 handleNullReturnEvent(msg, "nvResetConfig");
651 break;
652
Jake Hamby7c27be32014-03-03 13:25:59 -0800653 case CMD_GET_PREFERRED_NETWORK_TYPE:
654 request = (MainThreadRequest) msg.obj;
655 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700656 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800657 break;
658
659 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
660 ar = (AsyncResult) msg.obj;
661 request = (MainThreadRequest) ar.userObj;
662 if (ar.exception == null && ar.result != null) {
663 request.result = ar.result; // Integer
664 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800665 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800666 if (ar.result == null) {
667 loge("getPreferredNetworkType: Empty response");
668 } else if (ar.exception instanceof CommandException) {
669 loge("getPreferredNetworkType: CommandException: " +
670 ar.exception);
671 } else {
672 loge("getPreferredNetworkType: Unknown exception");
673 }
674 }
675 synchronized (request) {
676 request.notifyAll();
677 }
678 break;
679
680 case CMD_SET_PREFERRED_NETWORK_TYPE:
681 request = (MainThreadRequest) msg.obj;
682 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
683 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700684 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800685 break;
686
687 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
688 handleNullReturnEvent(msg, "setPreferredNetworkType");
689 break;
690
Steven Liu4bf01bc2014-07-17 11:05:29 -0500691 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
692 request = (MainThreadRequest)msg.obj;
693 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
694 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
695 break;
696
697 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
698 ar = (AsyncResult)msg.obj;
699 request = (MainThreadRequest)ar.userObj;
700 request.result = ar;
701 synchronized (request) {
702 request.notifyAll();
703 }
704 break;
705
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800706 case CMD_SET_VOICEMAIL_NUMBER:
707 request = (MainThreadRequest) msg.obj;
708 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
709 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800710 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
711 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800712 break;
713
714 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
715 handleNullReturnEvent(msg, "setVoicemailNumber");
716 break;
717
Stuart Scott54788802015-03-30 13:18:01 -0700718 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
719 request = (MainThreadRequest) msg.obj;
720 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
721 request);
722 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
723 break;
724
725 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
726 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
727 break;
728
Shishir Agrawal302c8692015-06-19 13:49:39 -0700729 case CMD_PERFORM_NETWORK_SCAN:
730 request = (MainThreadRequest) msg.obj;
731 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
732 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
733 break;
734
735 case EVENT_PERFORM_NETWORK_SCAN_DONE:
736 ar = (AsyncResult) msg.obj;
737 request = (MainThreadRequest) ar.userObj;
738 CellNetworkScanResult cellScanResult;
739 if (ar.exception == null && ar.result != null) {
740 cellScanResult = new CellNetworkScanResult(
741 CellNetworkScanResult.STATUS_SUCCESS,
742 (List<OperatorInfo>) ar.result);
743 } else {
744 if (ar.result == null) {
745 loge("getCellNetworkScanResults: Empty response");
746 }
747 if (ar.exception != null) {
748 loge("getCellNetworkScanResults: Exception: " + ar.exception);
749 }
750 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
751 if (ar.exception instanceof CommandException) {
752 CommandException.Error error =
753 ((CommandException) (ar.exception)).getCommandError();
754 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
755 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
756 } else if (error == CommandException.Error.GENERIC_FAILURE) {
757 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
758 }
759 }
760 cellScanResult = new CellNetworkScanResult(errorCode, null);
761 }
762 request.result = cellScanResult;
763 synchronized (request) {
764 request.notifyAll();
765 }
766 break;
767
768 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
769 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700770 ManualNetworkSelectionArgument selArg =
771 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700772 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
773 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700774 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
775 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700776 break;
777
778 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
779 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
780 break;
781
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700782 case CMD_GET_MODEM_ACTIVITY_INFO:
783 request = (MainThreadRequest) msg.obj;
784 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700785 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700786 break;
787
788 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
789 ar = (AsyncResult) msg.obj;
790 request = (MainThreadRequest) ar.userObj;
791 if (ar.exception == null && ar.result != null) {
792 request.result = ar.result;
793 } else {
794 if (ar.result == null) {
795 loge("queryModemActivityInfo: Empty response");
796 } else if (ar.exception instanceof CommandException) {
797 loge("queryModemActivityInfo: CommandException: " +
798 ar.exception);
799 } else {
800 loge("queryModemActivityInfo: Unknown exception");
801 }
802 }
Amit Mahajand4766222016-01-28 15:28:28 -0800803 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
804 if (request.result == null) {
805 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
806 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700807 synchronized (request) {
808 request.notifyAll();
809 }
810 break;
811
Meng Wang1a7c35a2016-05-05 20:56:15 -0700812 case CMD_SET_ALLOWED_CARRIERS:
813 request = (MainThreadRequest) msg.obj;
814 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
815 mPhone.setAllowedCarriers(
816 (List<CarrierIdentifier>) request.argument,
817 onCompleted);
818 break;
819
820 case EVENT_SET_ALLOWED_CARRIERS_DONE:
821 ar = (AsyncResult) msg.obj;
822 request = (MainThreadRequest) ar.userObj;
823 if (ar.exception == null && ar.result != null) {
824 request.result = ar.result;
825 } else {
826 if (ar.result == null) {
827 loge("setAllowedCarriers: Empty response");
828 } else if (ar.exception instanceof CommandException) {
829 loge("setAllowedCarriers: CommandException: " +
830 ar.exception);
831 } else {
832 loge("setAllowedCarriers: Unknown exception");
833 }
834 }
835 // Result cannot be null. Return -1 on error.
836 if (request.result == null) {
837 request.result = new int[]{-1};
838 }
839 synchronized (request) {
840 request.notifyAll();
841 }
842 break;
843
844 case CMD_GET_ALLOWED_CARRIERS:
845 request = (MainThreadRequest) msg.obj;
846 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
847 mPhone.getAllowedCarriers(onCompleted);
848 break;
849
850 case EVENT_GET_ALLOWED_CARRIERS_DONE:
851 ar = (AsyncResult) msg.obj;
852 request = (MainThreadRequest) ar.userObj;
853 if (ar.exception == null && ar.result != null) {
854 request.result = ar.result;
855 } else {
856 if (ar.result == null) {
857 loge("getAllowedCarriers: Empty response");
858 } else if (ar.exception instanceof CommandException) {
859 loge("getAllowedCarriers: CommandException: " +
860 ar.exception);
861 } else {
862 loge("getAllowedCarriers: Unknown exception");
863 }
864 }
865 // Result cannot be null. Return empty list of CarrierIdentifier.
866 if (request.result == null) {
867 request.result = new ArrayList<CarrierIdentifier>(0);
868 }
869 synchronized (request) {
870 request.notifyAll();
871 }
872 break;
873
Nathan Haroldb3014052017-01-25 15:57:32 -0800874 case EVENT_GET_FORBIDDEN_PLMNS_DONE:
875 ar = (AsyncResult) msg.obj;
876 request = (MainThreadRequest) ar.userObj;
877 if (ar.exception == null && ar.result != null) {
878 request.result = ar.result;
879 } else {
880 request.result = new IllegalArgumentException(
881 "Failed to retrieve Forbidden Plmns");
882 if (ar.result == null) {
883 loge("getForbiddenPlmns: Empty response");
884 } else {
885 loge("getForbiddenPlmns: Unknown exception");
886 }
887 }
888 synchronized (request) {
889 request.notifyAll();
890 }
891 break;
892
893 case CMD_GET_FORBIDDEN_PLMNS:
894 request = (MainThreadRequest) msg.obj;
895 uiccCard = getUiccCardFromRequest(request);
896 if (uiccCard == null) {
897 loge("getForbiddenPlmns() UiccCard is null");
898 request.result = new IllegalArgumentException(
899 "getForbiddenPlmns() UiccCard is null");
900 synchronized (request) {
901 request.notifyAll();
902 }
903 break;
904 }
905 Integer appType = (Integer) request.argument;
906 UiccCardApplication uiccApp = uiccCard.getApplicationByType(appType);
907 if (uiccApp == null) {
908 loge("getForbiddenPlmns() no app with specified type -- "
909 + appType);
910 request.result = new IllegalArgumentException("Failed to get UICC App");
911 synchronized (request) {
912 request.notifyAll();
913 }
914 break;
915 } else {
916 if (DBG) logv("getForbiddenPlmns() found app " + uiccApp.getAid()
917 + " specified type -- " + appType);
918 }
919 onCompleted = obtainMessage(EVENT_GET_FORBIDDEN_PLMNS_DONE, request);
920 ((SIMRecords) uiccApp.getIccRecords()).getForbiddenPlmns(
921 onCompleted);
922 break;
923
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 default:
925 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
926 break;
927 }
928 }
Jake Hambye994d462014-02-03 13:10:13 -0800929
930 private void handleNullReturnEvent(Message msg, String command) {
931 AsyncResult ar = (AsyncResult) msg.obj;
932 MainThreadRequest request = (MainThreadRequest) ar.userObj;
933 if (ar.exception == null) {
934 request.result = true;
935 } else {
936 request.result = false;
937 if (ar.exception instanceof CommandException) {
938 loge(command + ": CommandException: " + ar.exception);
939 } else {
940 loge(command + ": Unknown exception");
941 }
942 }
943 synchronized (request) {
944 request.notifyAll();
945 }
946 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 }
948
949 /**
950 * Posts the specified command to be executed on the main thread,
951 * waits for the request to complete, and returns the result.
952 * @see #sendRequestAsync
953 */
954 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800955 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
958 /**
959 * Posts the specified command to be executed on the main thread,
960 * waits for the request to complete, and returns the result.
961 * @see #sendRequestAsync
962 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800963 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
965 throw new RuntimeException("This method will deadlock if called from the main thread.");
966 }
967
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800968 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 Message msg = mMainThreadHandler.obtainMessage(command, request);
970 msg.sendToTarget();
971
972 // Wait for the request to complete
973 synchronized (request) {
974 while (request.result == null) {
975 try {
976 request.wait();
977 } catch (InterruptedException e) {
978 // Do nothing, go back and wait until the request is complete
979 }
980 }
981 }
982 return request.result;
983 }
984
985 /**
986 * Asynchronous ("fire and forget") version of sendRequest():
987 * Posts the specified command to be executed on the main thread, and
988 * returns immediately.
989 * @see #sendRequest
990 */
991 private void sendRequestAsync(int command) {
992 mMainThreadHandler.sendEmptyMessage(command);
993 }
994
995 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700996 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
997 * @see {@link #sendRequest(int,Object)}
998 */
999 private void sendRequestAsync(int command, Object argument) {
1000 MainThreadRequest request = new MainThreadRequest(argument);
1001 Message msg = mMainThreadHandler.obtainMessage(command, request);
1002 msg.sendToTarget();
1003 }
1004
1005 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001006 * Initialize the singleton PhoneInterfaceManager instance.
1007 * This is only done once, at startup, from PhoneApp.onCreate().
1008 */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001009 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 synchronized (PhoneInterfaceManager.class) {
1011 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -07001012 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 } else {
1014 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
1015 }
1016 return sInstance;
1017 }
1018 }
1019
1020 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -07001021 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 mApp = app;
1023 mPhone = phone;
1024 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -07001025 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001026 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
1027 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -07001028 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -07001029 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001030 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -08001031
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001032 publish();
1033 }
1034
1035 private void publish() {
1036 if (DBG) log("publish: " + this);
1037
1038 ServiceManager.addService("phone", this);
1039 }
1040
Stuart Scott584921c2015-01-15 17:10:34 -08001041 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -08001042 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
1043 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -08001044 }
1045
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001046 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
1047 Phone phone = getPhoneFromRequest(request);
1048 return phone == null ? null :
1049 UiccController.getInstance().getUiccCard(phone.getPhoneId());
1050 }
1051
Wink Saville36469e72014-06-11 15:17:00 -07001052 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -07001053 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001054 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -07001055 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 //
1057 // Implementation of the ITelephony interface.
1058 //
1059
1060 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001061 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -07001062 }
1063
Wink Savilleb564aae2014-10-23 10:18:09 -07001064 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 if (DBG) log("dial: " + number);
1066 // No permission check needed here: This is just a wrapper around the
1067 // ACTION_DIAL intent, which is available to any app since it puts up
1068 // the UI before it does anything.
1069
1070 String url = createTelUrl(number);
1071 if (url == null) {
1072 return;
1073 }
1074
1075 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -07001076 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001077 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
1078 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
1079 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1080 mApp.startActivity(intent);
1081 }
1082 }
1083
1084 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001085 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -07001086 }
1087
Wink Savilleb564aae2014-10-23 10:18:09 -07001088 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001089 if (DBG) log("call: " + number);
1090
1091 // This is just a wrapper around the ACTION_CALL intent, but we still
1092 // need to do a permission check since we're calling startActivity()
1093 // from the context of the phone app.
1094 enforceCallPermission();
1095
1096 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1097 != AppOpsManager.MODE_ALLOWED) {
1098 return;
1099 }
1100
1101 String url = createTelUrl(number);
1102 if (url == null) {
1103 return;
1104 }
1105
Wink Saville08874612014-08-31 19:19:58 -07001106 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001107 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001108 if (slist != null) {
1109 for (SubscriptionInfo subInfoRecord : slist) {
1110 if (subInfoRecord.getSubscriptionId() == subId) {
1111 isValid = true;
1112 break;
1113 }
Wink Saville08874612014-08-31 19:19:58 -07001114 }
1115 }
1116 if (isValid == false) {
1117 return;
1118 }
1119
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001121 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1123 mApp.startActivity(intent);
1124 }
1125
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 /**
1127 * End a call based on call state
1128 * @return true is a call was ended
1129 */
1130 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001131 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001132 }
1133
1134 /**
1135 * End a call based on the call state of the subId
1136 * @return true is a call was ended
1137 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001138 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001140 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 }
1142
1143 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001144 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001145 }
1146
Wink Savilleb564aae2014-10-23 10:18:09 -07001147 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 if (DBG) log("answerRingingCall...");
1149 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1150 // but that can probably wait till the big TelephonyManager API overhaul.
1151 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1152 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001153 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154 }
1155
1156 /**
1157 * Make the actual telephony calls to implement answerRingingCall().
1158 * This should only be called from the main thread of the Phone app.
1159 * @see #answerRingingCall
1160 *
1161 * TODO: it would be nice to return true if we answered the call, or
1162 * false if there wasn't actually a ringing incoming call, or some
1163 * other error occurred. (In other words, pass back the return value
1164 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1165 * But that would require calling this method via sendRequest() rather
1166 * than sendRequestAsync(), and right now we don't actually *need* that
1167 * return value, so let's just return void for now.
1168 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001169 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001170 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001172 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1173 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 if (hasActiveCall && hasHoldingCall) {
1175 // Both lines are in use!
1176 // TODO: provide a flag to let the caller specify what
1177 // policy to use if both lines are in use. (The current
1178 // behavior is hardwired to "answer incoming, end ongoing",
1179 // which is how the CALL button is specced to behave.)
1180 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1181 return;
1182 } else {
1183 // answerCall() will automatically hold the current active
1184 // call, if there is one.
1185 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1186 return;
1187 }
1188 } else {
1189 // No call was ringing.
1190 return;
1191 }
1192 }
1193
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001195 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001196 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001197 public void silenceRinger() {
1198 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199 }
1200
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001201 @Override
1202 public boolean isOffhook(String callingPackage) {
1203 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001204 }
1205
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001206 @Override
1207 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1208 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1209 return false;
1210 }
1211
Sanket Padawe356d7632015-06-22 14:03:32 -07001212 final Phone phone = getPhone(subId);
1213 if (phone != null) {
1214 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1215 } else {
1216 return false;
1217 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001218 }
1219
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001220 @Override
1221 public boolean isRinging(String callingPackage) {
1222 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001223 }
1224
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001225 @Override
1226 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1227 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1228 return false;
1229 }
1230
Sanket Padawe356d7632015-06-22 14:03:32 -07001231 final Phone phone = getPhone(subId);
1232 if (phone != null) {
1233 return (phone.getState() == PhoneConstants.State.RINGING);
1234 } else {
1235 return false;
1236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 }
1238
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001239 @Override
1240 public boolean isIdle(String callingPackage) {
1241 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001242 }
1243
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001244 @Override
1245 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1246 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1247 return false;
1248 }
1249
Sanket Padawe356d7632015-06-22 14:03:32 -07001250 final Phone phone = getPhone(subId);
1251 if (phone != null) {
1252 return (phone.getState() == PhoneConstants.State.IDLE);
1253 } else {
1254 return false;
1255 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001256 }
1257
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001259 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001260 }
1261
Wink Savilleb564aae2014-10-23 10:18:09 -07001262 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001263 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001264 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1265 }
1266
1267 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001268 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001269 }
1270
Wink Savilleb564aae2014-10-23 10:18:09 -07001271 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001272 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001273 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1274 }
1275
1276 /** {@hide} */
1277 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001278 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001279 }
1280
Wink Savilleb564aae2014-10-23 10:18:09 -07001281 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001283 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 checkSimPin.start();
1285 return checkSimPin.unlockSim(null, pin);
1286 }
1287
Wink Saville9de0f752013-10-22 19:04:03 -07001288 /** {@hide} */
1289 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001290 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001291 }
1292
Wink Savilleb564aae2014-10-23 10:18:09 -07001293 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001295 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001296 checkSimPuk.start();
1297 return checkSimPuk.unlockSim(puk, pin);
1298 }
1299
1300 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001301 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001302 * a synchronous one.
1303 */
1304 private static class UnlockSim extends Thread {
1305
1306 private final IccCard mSimCard;
1307
1308 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001309 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1310 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001311
1312 // For replies from SimCard interface
1313 private Handler mHandler;
1314
1315 // For async handler to identify request type
1316 private static final int SUPPLY_PIN_COMPLETE = 100;
1317
1318 public UnlockSim(IccCard simCard) {
1319 mSimCard = simCard;
1320 }
1321
1322 @Override
1323 public void run() {
1324 Looper.prepare();
1325 synchronized (UnlockSim.this) {
1326 mHandler = new Handler() {
1327 @Override
1328 public void handleMessage(Message msg) {
1329 AsyncResult ar = (AsyncResult) msg.obj;
1330 switch (msg.what) {
1331 case SUPPLY_PIN_COMPLETE:
1332 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1333 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001334 mRetryCount = msg.arg1;
1335 if (ar.exception != null) {
1336 if (ar.exception instanceof CommandException &&
1337 ((CommandException)(ar.exception)).getCommandError()
1338 == CommandException.Error.PASSWORD_INCORRECT) {
1339 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1340 } else {
1341 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1342 }
1343 } else {
1344 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1345 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 mDone = true;
1347 UnlockSim.this.notifyAll();
1348 }
1349 break;
1350 }
1351 }
1352 };
1353 UnlockSim.this.notifyAll();
1354 }
1355 Looper.loop();
1356 }
1357
1358 /*
1359 * Use PIN or PUK to unlock SIM card
1360 *
1361 * If PUK is null, unlock SIM card with PIN
1362 *
1363 * If PUK is not null, unlock SIM card with PUK and set PIN code
1364 */
Wink Saville9de0f752013-10-22 19:04:03 -07001365 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366
1367 while (mHandler == null) {
1368 try {
1369 wait();
1370 } catch (InterruptedException e) {
1371 Thread.currentThread().interrupt();
1372 }
1373 }
1374 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1375
1376 if (puk == null) {
1377 mSimCard.supplyPin(pin, callback);
1378 } else {
1379 mSimCard.supplyPuk(puk, pin, callback);
1380 }
1381
1382 while (!mDone) {
1383 try {
1384 Log.d(LOG_TAG, "wait for done");
1385 wait();
1386 } catch (InterruptedException e) {
1387 // Restore the interrupted status
1388 Thread.currentThread().interrupt();
1389 }
1390 }
1391 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001392 int[] resultArray = new int[2];
1393 resultArray[0] = mResult;
1394 resultArray[1] = mRetryCount;
1395 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 }
1397 }
1398
1399 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001400 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001401
1402 }
1403
Wink Savilleb564aae2014-10-23 10:18:09 -07001404 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 // No permission check needed here: this call is harmless, and it's
1406 // needed for the ServiceState.requestStateUpdate() call (which is
1407 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001408 final Phone phone = getPhone(subId);
1409 if (phone != null) {
1410 phone.updateServiceLocation();
1411 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001414 @Override
1415 public boolean isRadioOn(String callingPackage) {
1416 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001417 }
1418
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001419 @Override
1420 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1421 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1422 return false;
1423 }
1424 return isRadioOnForSubscriber(subId);
1425 }
1426
1427 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001428 final Phone phone = getPhone(subId);
1429 if (phone != null) {
1430 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1431 } else {
1432 return false;
1433 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 }
1435
1436 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001437 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001438
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 }
Wink Saville36469e72014-06-11 15:17:00 -07001440
Wink Savilleb564aae2014-10-23 10:18:09 -07001441 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001442 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001443 final Phone phone = getPhone(subId);
1444 if (phone != null) {
1445 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1446 }
Wink Saville36469e72014-06-11 15:17:00 -07001447 }
1448
1449 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001450 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001451 }
1452
Wink Savilleb564aae2014-10-23 10:18:09 -07001453 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001454 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001455 final Phone phone = getPhone(subId);
1456 if (phone == null) {
1457 return false;
1458 }
1459 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001460 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001461 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 }
1463 return true;
1464 }
Wink Saville36469e72014-06-11 15:17:00 -07001465
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001466 public boolean needMobileRadioShutdown() {
1467 /*
1468 * If any of the Radios are available, it will need to be
1469 * shutdown. So return true if any Radio is available.
1470 */
1471 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1472 Phone phone = PhoneFactory.getPhone(i);
1473 if (phone != null && phone.isRadioAvailable()) return true;
1474 }
1475 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1476 return false;
1477 }
1478
1479 public void shutdownMobileRadios() {
1480 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1481 logv("Shutting down Phone " + i);
1482 shutdownRadioUsingPhoneId(i);
1483 }
1484 }
1485
1486 private void shutdownRadioUsingPhoneId(int phoneId) {
1487 enforceModifyPermission();
1488 Phone phone = PhoneFactory.getPhone(phoneId);
1489 if (phone != null && phone.isRadioAvailable()) {
1490 phone.shutdownRadio();
1491 }
1492 }
1493
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 public boolean setRadioPower(boolean turnOn) {
Wei Liu9ae2a062016-08-08 11:09:34 -07001495 final Phone defaultPhone = PhoneFactory.getDefaultPhone();
1496 if (defaultPhone != null) {
1497 defaultPhone.setRadioPower(turnOn);
1498 return true;
1499 } else {
1500 loge("There's no default phone.");
1501 return false;
1502 }
Wink Saville36469e72014-06-11 15:17:00 -07001503 }
1504
Wink Savilleb564aae2014-10-23 10:18:09 -07001505 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001506 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001507 final Phone phone = getPhone(subId);
1508 if (phone != null) {
1509 phone.setRadioPower(turnOn);
1510 return true;
1511 } else {
1512 return false;
1513 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 }
1515
Wink Saville36469e72014-06-11 15:17:00 -07001516 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001517 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 public boolean enableDataConnectivity() {
1519 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001520 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001521 final Phone phone = getPhone(subId);
1522 if (phone != null) {
1523 phone.setDataEnabled(true);
1524 return true;
1525 } else {
1526 return false;
1527 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001528 }
1529
Wink Saville36469e72014-06-11 15:17:00 -07001530 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001531 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 public boolean disableDataConnectivity() {
1533 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001534 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001535 final Phone phone = getPhone(subId);
1536 if (phone != null) {
1537 phone.setDataEnabled(false);
1538 return true;
1539 } else {
1540 return false;
1541 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001542 }
1543
Wink Saville36469e72014-06-11 15:17:00 -07001544 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001545 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001547 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001548 final Phone phone = getPhone(subId);
1549 if (phone != null) {
1550 return phone.isDataConnectivityPossible();
1551 } else {
1552 return false;
1553 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 }
1555
1556 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001557 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001558 }
1559
pkanwarf8520782016-11-06 20:37:09 -08001560 public void handleUssdRequest(int subId, String ussdRequest, ResultReceiver wrappedCallback) {
pkanwar7e856482016-10-14 19:37:38 -07001561 enforceCallPermission();
pkanwar7e856482016-10-14 19:37:38 -07001562 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1563 return;
1564 }
1565 Pair<String, ResultReceiver> ussdObject = new Pair(ussdRequest, wrappedCallback);
1566 sendRequest(CMD_HANDLE_USSD_REQUEST, ussdObject, subId);
1567 };
1568
Wink Savilleb564aae2014-10-23 10:18:09 -07001569 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001570 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001571 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1572 return false;
1573 }
Wink Saville36469e72014-06-11 15:17:00 -07001574 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001575 }
1576
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001577 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001578 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001579 }
1580
Sanket Padawe5780e442017-03-20 15:04:47 -07001581 public int getCallStateForSlot(int slotIndex) {
1582 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001583 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
Nathan Harold8f68faa2016-12-14 11:27:20 -08001584 PhoneConstantConversions.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001585 }
1586
Sanket Padawe356d7632015-06-22 14:03:32 -07001587 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001589 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001590 if (phone != null) {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001591 return PhoneConstantConversions.convertDataState(phone.getDataConnectionState());
Sanket Padawe356d7632015-06-22 14:03:32 -07001592 } else {
Nathan Harold8f68faa2016-12-14 11:27:20 -08001593 return PhoneConstantConversions.convertDataState(PhoneConstants.DataState.DISCONNECTED);
Sanket Padawe356d7632015-06-22 14:03:32 -07001594 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001595 }
1596
Sanket Padawe356d7632015-06-22 14:03:32 -07001597 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001598 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001599 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001600 if (phone != null) {
1601 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1602 } else {
1603 return TelephonyManager.DATA_ACTIVITY_NONE;
1604 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001605 }
1606
1607 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001608 public Bundle getCellLocation(String callingPackage) {
1609 enforceFineOrCoarseLocationPermission("getCellLocation");
1610
1611 // OP_COARSE_LOCATION controls both fine and coarse location.
1612 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1613 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001614 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001615 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001616 }
1617
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001618 if (checkIfCallerIsSelfOrForegroundUser() ||
1619 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001620 if (DBG_LOC) log("getCellLocation: is active user");
1621 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001622 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001623 if (phone == null) {
1624 return null;
1625 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07001626
1627 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1628 phone.getCellLocation(workSource).fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 return data;
1630 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001631 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001632 return null;
1633 }
1634 }
1635
Svetoslav64fad262015-04-14 14:35:21 -07001636 private void enforceFineOrCoarseLocationPermission(String message) {
1637 try {
1638 mApp.enforceCallingOrSelfPermission(
1639 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1640 } catch (SecurityException e) {
1641 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1642 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1643 // is the weaker precondition
1644 mApp.enforceCallingOrSelfPermission(
1645 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1646 }
1647 }
1648
1649
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001650 @Override
1651 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001652 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001653 }
1654
Sanket Padawe356d7632015-06-22 14:03:32 -07001655 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001656 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 mApp.enforceCallingOrSelfPermission(
1658 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001659 final Phone phone = getPhone(subId);
1660 if (phone != null) {
1661 phone.enableLocationUpdates();
1662 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 }
1664
1665 @Override
1666 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001667 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001668 }
1669
Sanket Padawe356d7632015-06-22 14:03:32 -07001670 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001671 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 mApp.enforceCallingOrSelfPermission(
1673 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001674 final Phone phone = getPhone(subId);
1675 if (phone != null) {
1676 phone.disableLocationUpdates();
1677 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001678 }
1679
1680 @Override
1681 @SuppressWarnings("unchecked")
1682 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001683 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1684
1685 // OP_COARSE_LOCATION controls both fine and coarse location.
1686 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1687 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1688 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001689 }
1690
1691 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1692 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1693 return null;
1694 }
Svetoslav64fad262015-04-14 14:35:21 -07001695
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001696 if (checkIfCallerIsSelfOrForegroundUser() ||
1697 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001698 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1699
1700 ArrayList<NeighboringCellInfo> cells = null;
1701
Sooraj Sasindran22882212016-07-18 11:57:25 -07001702 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 try {
1704 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sooraj Sasindran22882212016-07-18 11:57:25 -07001705 CMD_HANDLE_NEIGHBORING_CELL, workSource,
Sanket Padawe56e75a32016-02-08 12:18:19 -08001706 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001707 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001708 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 }
1710 return cells;
1711 } else {
1712 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1713 return null;
1714 }
1715 }
1716
1717
1718 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001719 public List<CellInfo> getAllCellInfo(String callingPackage) {
1720 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1721
1722 // OP_COARSE_LOCATION controls both fine and coarse location.
1723 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1724 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1725 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001726 }
1727
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001728 if (checkIfCallerIsSelfOrForegroundUser() ||
1729 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001730 if (DBG_LOC) log("getAllCellInfo: is active user");
Sooraj Sasindran22882212016-07-18 11:57:25 -07001731 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001732 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1733 for (Phone phone : PhoneFactory.getPhones()) {
Sooraj Sasindran22882212016-07-18 11:57:25 -07001734 final List<CellInfo> info = phone.getAllCellInfo(workSource);
1735 if (info != null) cellInfos.addAll(info);
Legler Wu2c01cdf2014-12-08 19:00:59 +08001736 }
1737 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001738 } else {
1739 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1740 return null;
1741 }
1742 }
1743
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001744 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 public void setCellInfoListRate(int rateInMillis) {
Jack Yu3128d9e2017-01-16 10:15:34 -08001746 enforceModifyPermission();
Sooraj Sasindran22882212016-07-18 11:57:25 -07001747 WorkSource workSource = getWorkSource(null, Binder.getCallingUid());
1748 mPhone.setCellInfoListRate(rateInMillis, workSource);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001749 }
1750
Shishir Agrawala9f32182016-04-12 12:00:16 -07001751 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001752 public String getImeiForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001753 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1754 return null;
1755 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001756 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001757 return phone == null ? null : phone.getImei();
1758 }
1759
1760 @Override
Jack Yuf7e8f152017-03-15 17:14:14 -07001761 public String getMeidForSlot(int slotIndex, String callingPackage) {
1762 if (!canReadPhoneState(callingPackage, "getMeidForSlot")) {
1763 return null;
1764 }
1765 Phone phone = PhoneFactory.getPhone(slotIndex);
1766 return phone == null ? null : phone.getMeid();
1767 }
1768
1769 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001770 public String getDeviceSoftwareVersionForSlot(int slotIndex, String callingPackage) {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001771 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1772 return null;
1773 }
Sanket Padawe5780e442017-03-20 15:04:47 -07001774 Phone phone = PhoneFactory.getPhone(slotIndex);
Shishir Agrawala9f32182016-04-12 12:00:16 -07001775 return phone == null ? null : phone.getDeviceSvn();
1776 }
1777
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001778 //
1779 // Internal helper methods.
1780 //
1781
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001782 /**
1783 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1784 */
1785 private boolean checkCallerInteractAcrossUsersFull() {
1786 return mPhone.getContext().checkCallingOrSelfPermission(
1787 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1788 == PackageManager.PERMISSION_GRANTED;
1789 }
1790
Jake Hambye994d462014-02-03 13:10:13 -08001791 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001792 boolean ok;
1793
1794 boolean self = Binder.getCallingUid() == Process.myUid();
1795 if (!self) {
1796 // Get the caller's user id then clear the calling identity
1797 // which will be restored in the finally clause.
1798 int callingUser = UserHandle.getCallingUserId();
1799 long ident = Binder.clearCallingIdentity();
1800
1801 try {
1802 // With calling identity cleared the current user is the foreground user.
1803 int foregroundUser = ActivityManager.getCurrentUser();
1804 ok = (foregroundUser == callingUser);
1805 if (DBG_LOC) {
1806 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1807 + " callingUser=" + callingUser + " ok=" + ok);
1808 }
1809 } catch (Exception ex) {
1810 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1811 ok = false;
1812 } finally {
1813 Binder.restoreCallingIdentity(ident);
1814 }
1815 } else {
1816 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1817 ok = true;
1818 }
1819 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1820 return ok;
1821 }
1822
1823 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001824 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1825 *
1826 * @throws SecurityException if the caller does not have the required permission
1827 */
1828 private void enforceModifyPermission() {
1829 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1830 }
1831
1832 /**
Junda Liua2e36012014-07-09 18:30:01 -07001833 * Make sure either system app or the caller has carrier privilege.
1834 *
1835 * @throws SecurityException if the caller does not have the required permission/privilege
1836 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001837 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001838 int permission = mApp.checkCallingOrSelfPermission(
1839 android.Manifest.permission.MODIFY_PHONE_STATE);
1840 if (permission == PackageManager.PERMISSION_GRANTED) {
1841 return;
1842 }
1843
1844 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001845 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001846 }
1847
1848 /**
1849 * Make sure the caller has carrier privilege.
1850 *
1851 * @throws SecurityException if the caller does not have the required permission
1852 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001853 private void enforceCarrierPrivilege(int subId) {
1854 if (getCarrierPrivilegeStatus(subId) !=
1855 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001856 loge("No Carrier Privilege.");
1857 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001858 }
1859 }
1860
1861 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001862 * Make sure the caller has the CALL_PHONE permission.
1863 *
1864 * @throws SecurityException if the caller does not have the required permission
1865 */
1866 private void enforceCallPermission() {
1867 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1868 }
1869
Stuart Scott8eef64f2015-04-08 15:13:54 -07001870 private void enforceConnectivityInternalPermission() {
1871 mApp.enforceCallingOrSelfPermission(
1872 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1873 "ConnectivityService");
1874 }
1875
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001876 private String createTelUrl(String number) {
1877 if (TextUtils.isEmpty(number)) {
1878 return null;
1879 }
1880
Jake Hambye994d462014-02-03 13:10:13 -08001881 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001882 }
1883
Ihab Awadf9e92732013-12-05 18:02:52 -08001884 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1886 }
1887
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001888 private static void logv(String msg) {
1889 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1890 }
1891
Ihab Awadf9e92732013-12-05 18:02:52 -08001892 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001893 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1894 }
1895
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001896 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001897 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001898 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001899 }
1900
Sanket Padawe356d7632015-06-22 14:03:32 -07001901 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07001902 public int getActivePhoneTypeForSlot(int slotIndex) {
1903 final Phone phone = PhoneFactory.getPhone(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07001904 if (phone == null) {
1905 return PhoneConstants.PHONE_TYPE_NONE;
1906 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001907 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001908 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 }
1910
1911 /**
1912 * Returns the CDMA ERI icon index to display
1913 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001914 @Override
1915 public int getCdmaEriIconIndex(String callingPackage) {
1916 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001917 }
1918
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1921 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1922 return -1;
1923 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 final Phone phone = getPhone(subId);
1925 if (phone != null) {
1926 return phone.getCdmaEriIconIndex();
1927 } else {
1928 return -1;
1929 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001930 }
1931
1932 /**
1933 * Returns the CDMA ERI icon mode,
1934 * 0 - ON
1935 * 1 - FLASHING
1936 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001937 @Override
1938 public int getCdmaEriIconMode(String callingPackage) {
1939 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001940 }
1941
Sanket Padawe356d7632015-06-22 14:03:32 -07001942 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001943 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1944 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1945 return -1;
1946 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001947 final Phone phone = getPhone(subId);
1948 if (phone != null) {
1949 return phone.getCdmaEriIconMode();
1950 } else {
1951 return -1;
1952 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001953 }
1954
1955 /**
1956 * Returns the CDMA ERI text,
1957 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001958 @Override
1959 public String getCdmaEriText(String callingPackage) {
1960 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001961 }
1962
Sanket Padawe356d7632015-06-22 14:03:32 -07001963 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001964 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1965 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1966 return null;
1967 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001968 final Phone phone = getPhone(subId);
1969 if (phone != null) {
1970 return phone.getCdmaEriText();
1971 } else {
1972 return null;
1973 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001974 }
1975
1976 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001977 * Returns the CDMA MDN.
1978 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001979 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001980 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001981 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001982 final Phone phone = getPhone(subId);
1983 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1984 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001985 } else {
1986 return null;
1987 }
1988 }
1989
1990 /**
1991 * Returns the CDMA MIN.
1992 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001993 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001994 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001995 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001996 final Phone phone = getPhone(subId);
1997 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1998 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001999 } else {
2000 return null;
2001 }
2002 }
2003
2004 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002005 * Returns true if CDMA provisioning needs to run.
2006 */
2007 public boolean needsOtaServiceProvisioning() {
2008 return mPhone.needsOtaServiceProvisioning();
2009 }
2010
2011 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002012 * Sets the voice mail number of a given subId.
2013 */
2014 @Override
2015 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002016 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002017 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
2018 new Pair<String, String>(alphaTag, number), new Integer(subId));
2019 return success;
2020 }
2021
Ta-wei Yen87c49842016-05-13 21:19:52 -07002022 @Override
Ta-wei Yenb0f695b2016-08-08 17:33:11 -07002023 public void setVisualVoicemailEnabled(String callingPackage,
2024 PhoneAccountHandle phoneAccountHandle, boolean enabled) {
2025 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2026 if (!TextUtils.equals(callingPackage,
2027 TelecomManager.from(mPhone.getContext()).getDefaultDialerPackage())) {
2028 enforceModifyPermissionOrCarrierPrivilege(
2029 PhoneUtils.getSubIdForPhoneAccountHandle(phoneAccountHandle));
2030 }
2031 VisualVoicemailSettingsUtil.setEnabled(mPhone.getContext(), phoneAccountHandle, enabled);
2032 }
2033
2034 @Override
2035 public boolean isVisualVoicemailEnabled(String callingPackage,
2036 PhoneAccountHandle phoneAccountHandle) {
2037 if (!canReadPhoneState(callingPackage, "isVisualVoicemailEnabled")) {
2038 return false;
2039 }
2040 return VisualVoicemailSettingsUtil.isEnabled(mPhone.getContext(), phoneAccountHandle);
2041 }
2042
2043 @Override
Ta-wei Yen12551102017-03-06 16:00:44 -08002044 public String getVisualVoicemailPackageName(String callingPackage, int subId) {
Ta-wei Yen243b6372017-01-10 16:17:08 -08002045 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
2046 if (!canReadPhoneState(callingPackage, "getVisualVoicemailPackageName")) {
2047 return null;
2048 }
Ta-wei Yen243b6372017-01-10 16:17:08 -08002049 return RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId).getPackageName();
2050 }
2051
2052 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002053 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
2054 VisualVoicemailSmsFilterSettings settings) {
2055 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002056 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002057 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
2058 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002059 }
2060
2061 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002062 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
2063 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002064 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002065 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002066 }
2067
2068 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07002069 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
2070 String callingPackage, int subId) {
2071 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002072 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07002073 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07002074 }
2075
2076 @Override
Ta-wei Yen1b455992016-12-27 14:52:32 -08002077 public VisualVoicemailSmsFilterSettings getActiveVisualVoicemailSmsFilterSettings(int subId) {
Ta-wei Yenb6929602016-05-24 15:48:27 -07002078 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07002079 return VisualVoicemailSmsFilterConfig
Ta-wei Yen1b455992016-12-27 14:52:32 -08002080 .getActiveVisualVoicemailSmsFilterSettings(mPhone.getContext(), subId);
2081 }
2082
2083 @Override
2084 public void sendVisualVoicemailSmsForSubscriber(String callingPackage, int subId,
2085 String number, int port, String text, PendingIntent sentIntent) {
2086 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen463efd12017-01-06 15:29:25 -08002087 enforceVisualVoicemailPackage(callingPackage, subId);
Ta-wei Yen1b455992016-12-27 14:52:32 -08002088 enforceSendSmsPermission();
2089 // Make the calls as the phone process.
2090 final long identity = Binder.clearCallingIdentity();
2091 try {
2092 SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
2093 if (port == 0) {
2094 smsManager.sendTextMessageWithSelfPermissions(number, null, text,
2095 sentIntent, null, false);
2096 } else {
2097 byte[] data = text.getBytes(StandardCharsets.UTF_8);
2098 smsManager.sendDataMessageWithSelfPermissions(number, null,
2099 (short) port, data, sentIntent, null);
2100 }
2101 } finally {
2102 Binder.restoreCallingIdentity(identity);
2103 }
Ta-wei Yen87c49842016-05-13 21:19:52 -07002104 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08002105 /**
fionaxuef039d42016-11-14 13:36:14 -08002106 * Sets the voice activation state of a given subId.
2107 */
2108 @Override
2109 public void setVoiceActivationState(int subId, int activationState) {
2110 enforceModifyPermissionOrCarrierPrivilege(subId);
2111 final Phone phone = getPhone(subId);
2112 if (phone != null) {
2113 phone.setVoiceActivationState(activationState);
2114 } else {
2115 loge("setVoiceActivationState fails with invalid subId: " + subId);
2116 }
2117 }
2118
2119 /**
2120 * Sets the data activation state of a given subId.
2121 */
2122 @Override
2123 public void setDataActivationState(int subId, int activationState) {
2124 enforceModifyPermissionOrCarrierPrivilege(subId);
2125 final Phone phone = getPhone(subId);
2126 if (phone != null) {
2127 phone.setDataActivationState(activationState);
2128 } else {
2129 loge("setVoiceActivationState fails with invalid subId: " + subId);
2130 }
2131 }
2132
2133 /**
2134 * Returns the voice activation state of a given subId.
2135 */
2136 @Override
2137 public int getVoiceActivationState(int subId, String callingPackage) {
2138 if (!canReadPhoneState(callingPackage, "getVoiceActivationStateForSubscriber")) {
2139 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2140 }
2141 final Phone phone = getPhone(subId);
2142 if (phone != null) {
2143 return phone.getVoiceActivationState();
2144 } else {
2145 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2146 }
2147 }
2148
2149 /**
2150 * Returns the data activation state of a given subId.
2151 */
2152 @Override
2153 public int getDataActivationState(int subId, String callingPackage) {
2154 if (!canReadPhoneState(callingPackage, "getDataActivationStateForSubscriber")) {
2155 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2156 }
2157 final Phone phone = getPhone(subId);
2158 if (phone != null) {
2159 return phone.getDataActivationState();
2160 } else {
2161 return TelephonyManager.SIM_ACTIVATION_STATE_UNKNOWN;
2162 }
2163 }
2164
2165 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002166 * Returns the unread count of voicemails
2167 */
2168 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07002169 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07002170 }
2171
2172 /**
2173 * Returns the unread count of voicemails for a subId
2174 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002175 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002176 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002177 final Phone phone = getPhone(subId);
2178 if (phone != null) {
2179 return phone.getVoiceMessageCount();
2180 } else {
2181 return 0;
2182 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002183 }
2184
2185 /**
pkanwar8efe6572017-01-19 13:43:16 -08002186 * returns true, if the device is in a state where both voice and data
2187 * are supported simultaneously. This can change based on location or network condition.
2188 */
2189 @Override
2190 public boolean isConcurrentVoiceAndDataAllowed(int subId) {
2191 final Phone phone = getPhone(subId);
2192 return (phone == null ? false : phone.isConcurrentVoiceAndDataAllowed());
2193 }
2194
2195 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002196 * Returns the data network type.
2197 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002198 *
2199 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
2200 */
2201 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002202 public int getNetworkType() {
2203 final Phone phone = getPhone(getDefaultSubscription());
2204 if (phone != null) {
2205 return phone.getServiceState().getDataNetworkType();
2206 } else {
2207 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2208 }
Wink Saville36469e72014-06-11 15:17:00 -07002209 }
2210
2211 /**
2212 * Returns the network type for a subId
2213 */
2214 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002215 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
2216 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
2217 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2218 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002219
Sanket Padawe356d7632015-06-22 14:03:32 -07002220 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002221 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002222 return phone.getServiceState().getDataNetworkType();
2223 } else {
2224 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2225 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002226 }
2227
2228 /**
2229 * Returns the data network type
2230 */
2231 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002232 public int getDataNetworkType(String callingPackage) {
2233 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002234 }
2235
2236 /**
2237 * Returns the data network type for a subId
2238 */
2239 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002240 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
2241 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2242 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2243 }
2244
Sanket Padawe356d7632015-06-22 14:03:32 -07002245 final Phone phone = getPhone(subId);
2246 if (phone != null) {
2247 return phone.getServiceState().getDataNetworkType();
2248 } else {
2249 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2250 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002251 }
2252
2253 /**
Wink Saville36469e72014-06-11 15:17:00 -07002254 * Returns the Voice network type for a subId
2255 */
2256 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002257 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2258 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2259 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2260 }
2261
Sanket Padawe356d7632015-06-22 14:03:32 -07002262 final Phone phone = getPhone(subId);
2263 if (phone != null) {
2264 return phone.getServiceState().getVoiceNetworkType();
2265 } else {
2266 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2267 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002268 }
2269
2270 /**
2271 * @return true if a ICC card is present
2272 */
2273 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002274 // FIXME Make changes to pass defaultSimId of type int
Sanket Padawe5780e442017-03-20 15:04:47 -07002275 return hasIccCardUsingSlotIndex(mSubscriptionController.getSlotIndex(
2276 getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002277 }
2278
2279 /**
Sanket Padawe5780e442017-03-20 15:04:47 -07002280 * @return true if a ICC card is present for a slotIndex
Wink Saville36469e72014-06-11 15:17:00 -07002281 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002282 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07002283 public boolean hasIccCardUsingSlotIndex(int slotIndex) {
2284 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Sanket Padawe356d7632015-06-22 14:03:32 -07002285 final Phone phone = getPhone(subId[0]);
2286 if (subId != null && phone != null) {
2287 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002288 } else {
2289 return false;
2290 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002291 }
2292
2293 /**
2294 * Return if the current radio is LTE on CDMA. This
2295 * is a tri-state return value as for a period of time
2296 * the mode may be unknown.
2297 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002298 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002299 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002300 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002301 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002302 @Override
2303 public int getLteOnCdmaMode(String callingPackage) {
2304 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002305 }
2306
Sanket Padawe356d7632015-06-22 14:03:32 -07002307 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002308 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2309 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2310 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2311 }
2312
Sanket Padawe356d7632015-06-22 14:03:32 -07002313 final Phone phone = getPhone(subId);
2314 if (phone == null) {
2315 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2316 } else {
2317 return phone.getLteOnCdmaMode();
2318 }
Wink Saville36469e72014-06-11 15:17:00 -07002319 }
2320
2321 public void setPhone(Phone phone) {
2322 mPhone = phone;
2323 }
2324
2325 /**
2326 * {@hide}
2327 * Returns Default subId, 0 in the case of single standby.
2328 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002329 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002330 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002331 }
2332
Shishir Agrawala9f32182016-04-12 12:00:16 -07002333 private int getSlotForDefaultSubscription() {
2334 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2335 }
2336
Wink Savilleb564aae2014-10-23 10:18:09 -07002337 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002338 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002339 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002340
2341 /**
2342 * @see android.telephony.TelephonyManager.WifiCallingChoices
2343 */
2344 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002345 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2346 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002347 }
2348
2349 /**
2350 * @see android.telephony.TelephonyManager.WifiCallingChoices
2351 */
2352 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002353 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2354 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2355 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002356 }
2357
Sailesh Nepald1e68152013-12-12 19:08:02 -08002358 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002359 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002360 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002361 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002362
Shishir Agrawal566b7612013-10-28 14:41:00 -07002363 @Override
Ajay Nambiabd73502015-12-03 13:50:00 -08002364 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID, int p2) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002365 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002366
Ajay Nambiabd73502015-12-03 13:50:00 -08002367 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID + " p2=" + p2);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002368 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Ajay Nambiabd73502015-12-03 13:50:00 -08002369 CMD_OPEN_CHANNEL, new Pair<String, Integer>(AID, p2), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002370 if (DBG) log("iccOpenLogicalChannel: " + response);
2371 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002372 }
2373
2374 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002375 public boolean iccCloseLogicalChannel(int subId, int channel) {
2376 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002377
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002378 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002379 if (channel < 0) {
2380 return false;
2381 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002382 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002383 if (DBG) log("iccCloseLogicalChannel: " + success);
2384 return success;
2385 }
2386
2387 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002388 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002389 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002390 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002391
2392 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002393 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2394 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002395 " data=" + data);
2396 }
2397
2398 if (channel < 0) {
2399 return "";
2400 }
2401
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002402 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002403 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002404 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2405
Shishir Agrawal566b7612013-10-28 14:41:00 -07002406 // Append the returned status code to the end of the response payload.
2407 String s = Integer.toHexString(
2408 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002409 if (response.payload != null) {
2410 s = IccUtils.bytesToHexString(response.payload) + s;
2411 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002412 return s;
2413 }
Jake Hambye994d462014-02-03 13:10:13 -08002414
Evan Charltonc66da362014-05-16 14:06:40 -07002415 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002416 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002417 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002418 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002419
2420 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002421 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2422 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002423 }
2424
2425 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002426 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002427 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2428
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002429 // Append the returned status code to the end of the response payload.
2430 String s = Integer.toHexString(
2431 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002432 if (response.payload != null) {
2433 s = IccUtils.bytesToHexString(response.payload) + s;
2434 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002435 return s;
2436 }
2437
2438 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002439 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002440 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002441 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002442
2443 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002444 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002445 p1 + " " + p2 + " " + p3 + ":" + filePath);
2446 }
2447
2448 IccIoResult response =
2449 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002450 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2451 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002452
2453 if (DBG) {
2454 log("Exchange SIM_IO [R]" + response);
2455 }
2456
2457 byte[] result = null;
2458 int length = 2;
2459 if (response.payload != null) {
2460 length = 2 + response.payload.length;
2461 result = new byte[length];
2462 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2463 } else {
2464 result = new byte[length];
2465 }
2466
2467 result[length - 1] = (byte) response.sw2;
2468 result[length - 2] = (byte) response.sw1;
2469 return result;
2470 }
2471
Nathan Haroldb3014052017-01-25 15:57:32 -08002472 /**
2473 * Get the forbidden PLMN List from the given app type (ex APPTYPE_USIM)
2474 * on a particular subscription
2475 */
2476 public String[] getForbiddenPlmns(int subId, int appType) {
Nathan Harold892104e2017-04-13 18:19:05 -07002477 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2478 "Requires READ_PHONE_STATE");
Nathan Haroldb3014052017-01-25 15:57:32 -08002479 if (appType != TelephonyManager.APPTYPE_USIM && appType != TelephonyManager.APPTYPE_SIM) {
2480 loge("getForbiddenPlmnList(): App Type must be USIM or SIM");
2481 return null;
2482 }
2483 Object response = sendRequest(
2484 CMD_GET_FORBIDDEN_PLMNS, new Integer(appType), subId);
2485 if (response instanceof String[]) {
2486 return (String[]) response;
2487 }
2488 // Response is an Exception of some kind, which is signalled to the user as a NULL retval
2489 return null;
2490 }
2491
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002492 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002493 public String sendEnvelopeWithStatus(int subId, String content) {
2494 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002495
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002496 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002497 if (response.payload == null) {
2498 return "";
2499 }
2500
2501 // Append the returned status code to the end of the response payload.
2502 String s = Integer.toHexString(
2503 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2504 s = IccUtils.bytesToHexString(response.payload) + s;
2505 return s;
2506 }
2507
Jake Hambye994d462014-02-03 13:10:13 -08002508 /**
2509 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2510 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2511 *
2512 * @param itemID the ID of the item to read
2513 * @return the NV item as a String, or null on error.
2514 */
2515 @Override
2516 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002517 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002518 if (DBG) log("nvReadItem: item " + itemID);
2519 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2520 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2521 return value;
2522 }
2523
2524 /**
2525 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2526 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2527 *
2528 * @param itemID the ID of the item to read
2529 * @param itemValue the value to write, as a String
2530 * @return true on success; false on any failure
2531 */
2532 @Override
2533 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002534 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002535 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2536 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2537 new Pair<Integer, String>(itemID, itemValue));
2538 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2539 return success;
2540 }
2541
2542 /**
2543 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2544 * Used for device configuration by some CDMA operators.
2545 *
2546 * @param preferredRoamingList byte array containing the new PRL
2547 * @return true on success; false on any failure
2548 */
2549 @Override
2550 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002551 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002552 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2553 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2554 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2555 return success;
2556 }
2557
2558 /**
2559 * Perform the specified type of NV config reset.
2560 * Used for device configuration by some CDMA operators.
2561 *
2562 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2563 * @return true on success; false on any failure
2564 */
2565 @Override
2566 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002567 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002568 if (DBG) log("nvResetConfig: type " + resetType);
2569 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2570 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2571 return success;
2572 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002573
2574 /**
Wink Saville36469e72014-06-11 15:17:00 -07002575 * {@hide}
2576 * Returns Default sim, 0 in the case of single standby.
2577 */
2578 public int getDefaultSim() {
2579 //TODO Need to get it from Telephony Devcontroller
2580 return 0;
2581 }
2582
Svet Ganovb320e182015-04-16 12:30:10 -07002583 public String[] getPcscfAddress(String apnType, String callingPackage) {
2584 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2585 return new String[0];
2586 }
2587
2588
ram87fca6f2014-07-18 18:58:44 +05302589 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002590 }
2591
Brad Ebinger76681002017-01-23 13:50:20 -08002592 /**
2593 * Returns the {@link IImsServiceController} that corresponds to the given slot Id and IMS
2594 * feature or {@link null} if the service is not available. If an ImsServiceController is
2595 * available, the {@link IImsServiceFeatureListener} callback is registered as a listener for
2596 * feature updates.
2597 */
Sanket Padawe5780e442017-03-20 15:04:47 -07002598 public IImsServiceController getImsServiceControllerAndListen(int slotIndex, int feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002599 IImsServiceFeatureListener callback) {
2600 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07002601 return PhoneFactory.getImsResolver().getImsServiceControllerAndListen(slotIndex, feature,
Brad Ebinger76681002017-01-23 13:50:20 -08002602 callback);
2603 }
2604
Wink Saville36469e72014-06-11 15:17:00 -07002605 public void setImsRegistrationState(boolean registered) {
2606 enforceModifyPermission();
2607 mPhone.setImsRegistrationState(registered);
2608 }
2609
2610 /**
Stuart Scott54788802015-03-30 13:18:01 -07002611 * Set the network selection mode to automatic.
2612 *
2613 */
2614 @Override
2615 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002616 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002617 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2618 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2619 }
2620
2621 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002622 * Set the network selection mode to manual with the selected carrier.
2623 */
2624 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002625 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2626 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002627 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002628 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002629 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2630 persistSelection);
2631 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002632 }
2633
2634 /**
2635 * Scans for available networks.
2636 */
2637 @Override
2638 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002639 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002640 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2641 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2642 CMD_PERFORM_NETWORK_SCAN, null, subId);
2643 return result;
2644 }
2645
2646 /**
yinxu34eed212017-04-12 16:03:22 -07002647 * Performs a new network scan and returns the id of this scan.
2648 *
2649 * @return the id of the requested scan which can be used to stop the scan.
2650 */
2651 @Override
2652 public int requestNetworkScan(int subId, NetworkScanRequest request, Messenger messenger,
2653 IBinder binder) {
2654 // TODO(yinxu): Implement this method.
2655 throw new UnsupportedOperationException("To be implemented...");
2656 }
2657
2658 /**
2659 * Stops an existing network scan with the given scanId.
2660 */
2661 @Override
2662 public void stopNetworkScan(int subId, int scanId) {
2663 // TODO(yinxu): Implement this method.
2664 throw new UnsupportedOperationException("To be implemented...");
2665 }
2666
2667 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002668 * Get the calculated preferred network type.
2669 * Used for debugging incorrect network type.
2670 *
2671 * @return the preferred network type, defined in RILConstants.java.
2672 */
2673 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002674 public int getCalculatedPreferredNetworkType(String callingPackage) {
2675 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2676 return RILConstants.PREFERRED_NETWORK_MODE;
2677 }
2678
Amit Mahajan43330e02014-11-18 11:54:45 -08002679 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002680 }
2681
2682 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002683 * Get the preferred network type.
2684 * Used for device configuration by some CDMA operators.
2685 *
2686 * @return the preferred network type, defined in RILConstants.java.
2687 */
2688 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002689 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002690 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002691 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002692 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002693 int networkType = (result != null ? result[0] : -1);
2694 if (DBG) log("getPreferredNetworkType: " + networkType);
2695 return networkType;
2696 }
2697
2698 /**
2699 * Set the preferred network type.
2700 * Used for device configuration by some CDMA operators.
2701 *
2702 * @param networkType the preferred network type, defined in RILConstants.java.
2703 * @return true on success; false on any failure.
2704 */
2705 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002706 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002707 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002708 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2709 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002710 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002711 if (success) {
2712 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002713 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002714 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002715 return success;
2716 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002717
2718 /**
Junda Liu475951f2014-11-07 16:45:03 -08002719 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2720 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2721 * tethering.
2722 *
2723 * @return 0: Not required. 1: required. 2: Not set.
2724 * @hide
2725 */
2726 @Override
2727 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002728 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002729 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2730 Settings.Global.TETHER_DUN_REQUIRED, 2);
2731 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2732 // config_tether_apndata.
2733 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2734 dunRequired = 1;
2735 }
2736 return dunRequired;
2737 }
2738
2739 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002740 * Set mobile data enabled
2741 * Used by the user through settings etc to turn on/off mobile data
2742 *
2743 * @param enable {@code true} turn turn data on, else {@code false}
2744 */
2745 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002746 public void setDataEnabled(int subId, boolean enable) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002747 enforceModifyPermissionOrCarrierPrivilege(subId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002748 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002749 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002750 Phone phone = PhoneFactory.getPhone(phoneId);
2751 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002752 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002753 phone.setDataEnabled(enable);
2754 } else {
2755 loge("setDataEnabled: no phone for subId=" + subId);
2756 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002757 }
2758
2759 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002760 * Get whether mobile data is enabled.
2761 *
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002762 * Accepts either ACCESS_NETWORK_STATE, MODIFY_PHONE_STATE or carrier privileges.
Robert Greenwalted86e582014-05-21 20:03:20 -07002763 *
2764 * @return {@code true} if data is enabled else {@code false}
2765 */
2766 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002767 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002768 try {
2769 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2770 null);
2771 } catch (Exception e) {
Jeff Davidsonf7eecf02016-11-18 17:05:56 -08002772 enforceModifyPermissionOrCarrierPrivilege(subId);
Robert Greenwalt646120a2014-05-23 11:54:03 -07002773 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002774 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002775 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002776 Phone phone = PhoneFactory.getPhone(phoneId);
2777 if (phone != null) {
2778 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002779 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002780 return retVal;
2781 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002782 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002783 return false;
2784 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002785 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002786
2787 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002788 public int getCarrierPrivilegeStatus(int subId) {
2789 final Phone phone = getPhone(subId);
2790 if (phone == null) {
2791 loge("getCarrierPrivilegeStatus: Invalid subId");
2792 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2793 }
2794 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002795 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002796 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002797 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2798 }
2799 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002800 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002801 }
Junda Liu29340342014-07-10 15:23:27 -07002802
2803 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002804 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002805 if (TextUtils.isEmpty(pkgName))
2806 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002807 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002808 if (card == null) {
2809 loge("checkCarrierPrivilegesForPackage: No UICC");
2810 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2811 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002812 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2813 }
2814
2815 @Override
2816 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002817 if (TextUtils.isEmpty(pkgName))
2818 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002819 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2820 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2821 UiccCard card = UiccController.getInstance().getUiccCard(i);
2822 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002823 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002824 continue;
2825 }
2826
2827 result = card.getCarrierPrivilegeStatus(
2828 mPhone.getContext().getPackageManager(), pkgName);
2829 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2830 break;
2831 }
2832 }
2833
2834 return result;
Junda Liu29340342014-07-10 15:23:27 -07002835 }
Derek Tan89e89d42014-07-08 17:00:10 -07002836
2837 @Override
Junda Liue64de782015-04-16 17:19:16 -07002838 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2839 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2840 loge("phoneId " + phoneId + " is not valid.");
2841 return null;
2842 }
2843 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002844 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002845 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002846 return null ;
2847 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002848 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002849 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002850 }
2851
Amith Yamasani6e118872016-02-19 12:53:51 -08002852 @Override
2853 public List<String> getPackagesWithCarrierPrivileges() {
2854 PackageManager pm = mPhone.getContext().getPackageManager();
2855 List<String> privilegedPackages = new ArrayList<>();
2856 List<PackageInfo> packages = null;
2857 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2858 UiccCard card = UiccController.getInstance().getUiccCard(i);
2859 if (card == null) {
2860 // No UICC in that slot.
2861 continue;
2862 }
2863 if (card.hasCarrierPrivilegeRules()) {
2864 if (packages == null) {
2865 // Only check packages in user 0 for now
2866 packages = pm.getInstalledPackagesAsUser(
2867 PackageManager.MATCH_DISABLED_COMPONENTS
2868 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2869 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2870 }
2871 for (int p = packages.size() - 1; p >= 0; p--) {
2872 PackageInfo pkgInfo = packages.get(p);
2873 if (pkgInfo != null && pkgInfo.packageName != null
2874 && card.getCarrierPrivilegeStatus(pkgInfo)
2875 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2876 privilegedPackages.add(pkgInfo.packageName);
2877 }
2878 }
2879 }
2880 }
2881 return privilegedPackages;
2882 }
2883
Wink Savilleb564aae2014-10-23 10:18:09 -07002884 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002885 final Phone phone = getPhone(subId);
2886 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002887 if (card == null) {
2888 loge("getIccId: No UICC");
2889 return null;
2890 }
2891 String iccId = card.getIccId();
2892 if (TextUtils.isEmpty(iccId)) {
2893 loge("getIccId: ICC ID is null or empty.");
2894 return null;
2895 }
2896 return iccId;
2897 }
2898
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002899 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002900 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2901 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002902 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002903
Jeff Sharkey85190e62014-12-05 09:40:12 -08002904 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002905 final Phone phone = getPhone(subId);
2906 if (phone == null) {
2907 return false;
2908 }
2909 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002910
2911 if (DBG_MERGE) {
2912 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2913 + subscriberId + " to " + number);
2914 }
2915
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002916 if (TextUtils.isEmpty(iccId)) {
2917 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002918 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002919
Jeff Sharkey85190e62014-12-05 09:40:12 -08002920 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2921
2922 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002923 if (alphaTag == null) {
2924 editor.remove(alphaTagPrefKey);
2925 } else {
2926 editor.putString(alphaTagPrefKey, alphaTag);
2927 }
2928
Jeff Sharkey85190e62014-12-05 09:40:12 -08002929 // Record both the line number and IMSI for this ICCID, since we need to
2930 // track all merged IMSIs based on line number
2931 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2932 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002933 if (number == null) {
2934 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002935 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002936 } else {
2937 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002938 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002939 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002940
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002941 editor.commit();
2942 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002943 }
2944
2945 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002946 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002947 // This is open to apps with WRITE_SMS.
2948 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002949 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002950 return null;
2951 }
Derek Tan97ebb422014-09-05 16:55:38 -07002952
2953 String iccId = getIccId(subId);
2954 if (iccId != null) {
2955 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002956 if (DBG_MERGE) {
2957 log("getLine1NumberForDisplay returning " +
2958 mTelephonySharedPreferences.getString(numberPrefKey, null));
2959 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002960 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002961 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002962 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002963 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002964 }
2965
2966 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002967 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2968 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2969 return null;
2970 }
Derek Tan97ebb422014-09-05 16:55:38 -07002971
2972 String iccId = getIccId(subId);
2973 if (iccId != null) {
2974 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002975 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002976 }
Derek Tan97ebb422014-09-05 16:55:38 -07002977 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002978 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002979
2980 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002981 public String[] getMergedSubscriberIds(String callingPackage) {
2982 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2983 return null;
2984 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002985 final Context context = mPhone.getContext();
2986 final TelephonyManager tele = TelephonyManager.from(context);
2987 final SubscriptionManager sub = SubscriptionManager.from(context);
2988
2989 // Figure out what subscribers are currently active
2990 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002991 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2992 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2993 final long identity = Binder.clearCallingIdentity();
2994 try {
2995 final int[] subIds = sub.getActiveSubscriptionIdList();
2996 for (int subId : subIds) {
2997 activeSubscriberIds.add(tele.getSubscriberId(subId));
2998 }
2999 } finally {
3000 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08003001 }
3002
3003 // First pass, find a number override for an active subscriber
3004 String mergeNumber = null;
3005 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
3006 for (String key : prefs.keySet()) {
3007 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
3008 final String subscriberId = (String) prefs.get(key);
3009 if (activeSubscriberIds.contains(subscriberId)) {
3010 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
3011 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
3012 mergeNumber = (String) prefs.get(numberKey);
3013 if (DBG_MERGE) {
3014 Slog.d(LOG_TAG, "Found line number " + mergeNumber
3015 + " for active subscriber " + subscriberId);
3016 }
3017 if (!TextUtils.isEmpty(mergeNumber)) {
3018 break;
3019 }
3020 }
3021 }
3022 }
3023
3024 // Shortcut when no active merged subscribers
3025 if (TextUtils.isEmpty(mergeNumber)) {
3026 return null;
3027 }
3028
3029 // Second pass, find all subscribers under that line override
3030 final ArraySet<String> result = new ArraySet<>();
3031 for (String key : prefs.keySet()) {
3032 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
3033 final String number = (String) prefs.get(key);
3034 if (mergeNumber.equals(number)) {
3035 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
3036 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
3037 final String subscriberId = (String) prefs.get(subscriberKey);
3038 if (!TextUtils.isEmpty(subscriberId)) {
3039 result.add(subscriberId);
3040 }
3041 }
3042 }
3043 }
3044
3045 final String[] resultArray = result.toArray(new String[result.size()]);
3046 Arrays.sort(resultArray);
3047 if (DBG_MERGE) {
3048 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
3049 }
3050 return resultArray;
3051 }
3052
3053 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003054 public boolean setOperatorBrandOverride(int subId, String brand) {
3055 enforceCarrierPrivilege(subId);
3056 final Phone phone = getPhone(subId);
3057 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07003058 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05003059
3060 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003061 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003062 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
3063 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08003064 enforceCarrierPrivilege(subId);
3065 final Phone phone = getPhone(subId);
3066 if (phone == null) {
3067 return false;
3068 }
3069 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08003070 cdmaNonRoamingList);
3071 }
3072
3073 @Override
Amit Mahajanf43fe462017-02-23 12:08:31 -08003074 @Deprecated
Steven Liu4bf01bc2014-07-17 11:05:29 -05003075 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
3076 enforceModifyPermission();
3077
3078 int returnValue = 0;
3079 try {
3080 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
3081 if(result.exception == null) {
3082 if (result.result != null) {
3083 byte[] responseData = (byte[])(result.result);
3084 if(responseData.length > oemResp.length) {
3085 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
3086 responseData.length + "bytes. Buffer Size is " +
3087 oemResp.length + "bytes.");
3088 }
3089 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
3090 returnValue = responseData.length;
3091 }
3092 } else {
3093 CommandException ex = (CommandException) result.exception;
3094 returnValue = ex.getCommandError().ordinal();
3095 if(returnValue > 0) returnValue *= -1;
3096 }
3097 } catch (RuntimeException e) {
3098 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
3099 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
3100 if(returnValue > 0) returnValue *= -1;
3101 }
3102
3103 return returnValue;
3104 }
Wink Saville5d475dd2014-10-17 15:00:58 -07003105
3106 @Override
3107 public void setRadioCapability(RadioAccessFamily[] rafs) {
3108 try {
3109 ProxyController.getInstance().setRadioCapability(rafs);
3110 } catch (RuntimeException e) {
3111 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
3112 }
3113 }
3114
3115 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07003116 public int getRadioAccessFamily(int phoneId, String callingPackage) {
3117 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
3118 return RadioAccessFamily.RAF_UNKNOWN;
3119 }
3120
Wink Saville5d475dd2014-10-17 15:00:58 -07003121 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
3122 }
Andrew Leedf14ead2014-10-17 14:22:52 -07003123
3124 @Override
3125 public void enableVideoCalling(boolean enable) {
3126 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003127 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07003128 }
3129
3130 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003131 public boolean isVideoCallingEnabled(String callingPackage) {
3132 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
3133 return false;
3134 }
3135
Andrew Lee77527ac2014-10-21 16:57:39 -07003136 // Check the user preference and the system-level IMS setting. Even if the user has
3137 // enabled video calling, if IMS is disabled we aren't able to support video calling.
3138 // In the long run, we may instead need to check if there exists a connection service
3139 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07003140 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
3141 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08003142 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07003143 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003144
Andrew Leea1239f22015-03-02 17:44:07 -08003145 @Override
3146 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003147 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003148 }
3149
3150 @Override
3151 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07003152 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08003153 }
3154
Andrew Lee9431b832015-03-09 18:46:45 -07003155 @Override
3156 public boolean isTtyModeSupported() {
3157 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
3158 TelephonyManager telephonyManager =
3159 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08003160 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07003161 }
3162
3163 @Override
3164 public boolean isHearingAidCompatibilitySupported() {
3165 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
3166 }
3167
Sanket Padawe7310cc72015-01-14 09:53:20 -08003168 /**
3169 * Returns the unique device ID of phone, for example, the IMEI for
3170 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
3171 *
3172 * <p>Requires Permission:
3173 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
3174 */
3175 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07003176 public String getDeviceId(String callingPackage) {
3177 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
3178 return null;
3179 }
3180
Sanket Padawe7310cc72015-01-14 09:53:20 -08003181 final Phone phone = PhoneFactory.getPhone(0);
3182 if (phone != null) {
3183 return phone.getDeviceId();
3184 } else {
3185 return null;
3186 }
3187 }
3188
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003189 /*
3190 * {@hide}
3191 * Returns the IMS Registration Status
3192 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08003193 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06003194 public boolean isImsRegistered() {
3195 return mPhone.isImsRegistered();
3196 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08003197
3198 @Override
3199 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
3200 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
3201 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07003202
Nathan Haroldc55097a2015-03-11 18:14:50 -07003203 /*
3204 * {@hide}
3205 * Returns the IMS Registration Status
3206 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003207 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003208 return mPhone.isWifiCallingEnabled();
3209 }
3210
3211 /*
3212 * {@hide}
3213 * Returns the IMS Registration Status
3214 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003215 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07003216 return mPhone.isVolteEnabled();
3217 }
Svet Ganovb320e182015-04-16 12:30:10 -07003218
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07003219 /*
3220 * {@hide} Returns the IMS Registration Status
3221 */
3222 public boolean isVideoTelephonyAvailable() {
3223 return mPhone.isVideoEnabled();
3224 }
3225
Svet Ganovb320e182015-04-16 12:30:10 -07003226 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07003227 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003228 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07003229 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
3230
Amit Mahajan83ea23b2015-07-30 16:05:11 -07003231 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07003232 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07003233 } catch (SecurityException e) {
3234 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
3235 message);
Etan Cohen921655c2015-06-24 13:54:50 -07003236 }
Svet Ganovb320e182015-04-16 12:30:10 -07003237
3238 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
3239 callingPackage) != AppOpsManager.MODE_ALLOWED) {
3240 return false;
3241 }
3242
3243 return true;
3244 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07003245
Makoto Onukifee69342015-06-29 14:44:50 -07003246 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07003247 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07003248 */
3249 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07003250 // Default SMS app can always read it.
3251 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
3252 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
3253 return true;
3254 }
3255 try {
3256 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07003257 } catch (SecurityException readPhoneStateSecurityException) {
3258 try {
3259 // Can be read with READ_SMS too.
3260 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
3261 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
3262 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
3263 } catch (SecurityException readSmsSecurityException) {
3264 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
3265 // permissions
3266 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
3267 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
3268 " or " + android.Manifest.permission.READ_SMS + ".");
3269 }
Makoto Onukie4072d12015-08-03 15:12:23 -07003270 }
Makoto Onukifee69342015-06-29 14:44:50 -07003271 }
3272
Stuart Scott8eef64f2015-04-08 15:13:54 -07003273 @Override
3274 public void factoryReset(int subId) {
3275 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07003276 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
3277 return;
3278 }
3279
Svet Ganovcc087f82015-05-12 20:35:54 -07003280 final long identity = Binder.clearCallingIdentity();
3281 try {
Stuart Scott981d8582015-04-21 14:09:50 -07003282 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
3283 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07003284 // Enable data
3285 setDataEnabled(subId, true);
3286 // Set network selection mode to automatic
3287 setNetworkSelectionModeAutomatic(subId);
3288 // Set preferred mobile network type to the best available
3289 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
3290 // Turn off roaming
3291 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
3292 }
3293 } finally {
3294 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07003295 }
3296 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003297
3298 @Override
3299 public String getLocaleFromDefaultSim() {
3300 // We query all subscriptions instead of just the active ones, because
3301 // this might be called early on in the provisioning flow when the
3302 // subscriptions potentially aren't active yet.
3303 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3304 if (slist == null || slist.isEmpty()) {
3305 return null;
3306 }
3307
3308 // This function may be called very early, say, from the setup wizard, at
3309 // which point we won't have a default subscription set. If that's the case
3310 // we just choose the first, which will be valid in "most cases".
3311 final int defaultSubId = getDefaultSubscription();
3312 SubscriptionInfo info = null;
3313 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3314 info = slist.get(0);
3315 } else {
3316 for (SubscriptionInfo item : slist) {
3317 if (item.getSubscriptionId() == defaultSubId) {
3318 info = item;
3319 break;
3320 }
3321 }
3322
3323 if (info == null) {
3324 return null;
3325 }
3326 }
3327
3328 // Try and fetch the locale from the carrier properties or from the SIM language
3329 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003330 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003331 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003332 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003333 if (defaultPhone != null) {
3334 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3335 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003336 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003337 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3338 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003339 } else {
3340 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003341 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003342 }
3343 }
3344
Narayan Kamath011676f2015-07-29 12:04:08 +01003345 // The SIM language preferences only store a language (e.g. fr = French), not an
3346 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3347 // the SIM and carrier preferences does not include a country we add the country
3348 // determined from the SIM MCC to provide an exact locale.
3349 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003350 if (mccLocale != null) {
3351 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3352 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003353 }
3354
Tony Hill183b2de2015-06-24 14:53:58 +01003355 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003356 return null;
3357 }
3358
3359 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3360 final long identity = Binder.clearCallingIdentity();
3361 try {
3362 return mSubscriptionController.getAllSubInfoList(
3363 mPhone.getContext().getOpPackageName());
3364 } finally {
3365 Binder.restoreCallingIdentity(identity);
3366 }
3367 }
3368
3369 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3370 final long identity = Binder.clearCallingIdentity();
3371 try {
3372 return mSubscriptionController.getActiveSubscriptionInfoList(
3373 mPhone.getContext().getOpPackageName());
3374 } finally {
3375 Binder.restoreCallingIdentity(identity);
3376 }
3377 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003378
3379 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003380 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3381 * representing the state of the modem.
3382 *
3383 * NOTE: This clears the modem state, so there should only every be one caller.
3384 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003385 */
3386 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003387 public void requestModemActivityInfo(ResultReceiver result) {
3388 enforceModifyPermission();
3389
3390 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3391 Bundle bundle = new Bundle();
3392 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3393 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003394 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003395
3396 /**
3397 * {@hide}
3398 * Returns the service state information on specified subscription.
3399 */
3400 @Override
3401 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3402
3403 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3404 return null;
3405 }
3406
3407 final Phone phone = getPhone(subId);
3408 if (phone == null) {
3409 return null;
3410 }
3411
3412 return phone.getServiceState();
3413 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003414
3415 /**
3416 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3417 *
3418 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3419 * voicemail ringtone.
3420 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3421 * PhoneAccount.
3422 */
3423 @Override
3424 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3425 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3426 if (phone == null) {
3427 return null;
3428 }
3429
3430 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3431 }
3432
3433 /**
3434 * Returns whether vibration is set for voicemail notification in Phone settings.
3435 *
3436 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3437 * voicemail vibration setting.
3438 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3439 */
3440 @Override
3441 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3442 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3443 if (phone == null) {
3444 return false;
3445 }
3446
3447 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3448 }
3449
Youhan Wange64578a2016-05-02 15:32:42 -07003450 /**
3451 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3452 *
3453 * @throws SecurityException if the caller does not have the required permission
3454 */
3455 private void enforceReadPrivilegedPermission() {
3456 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3457 null);
3458 }
3459
3460 /**
Ta-wei Yen1b455992016-12-27 14:52:32 -08003461 * Make sure either called from same process as self (phone) or IPC caller has send SMS
3462 * permission.
3463 *
3464 * @throws SecurityException if the caller does not have the required permission
3465 */
3466 private void enforceSendSmsPermission() {
3467 mApp.enforceCallingOrSelfPermission(permission.SEND_SMS, null);
3468 }
3469
3470 /**
Ta-wei Yen463efd12017-01-06 15:29:25 -08003471 * Make sure called from the package in charge of visual voicemail.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003472 *
Ta-wei Yen463efd12017-01-06 15:29:25 -08003473 * @throws SecurityException if the caller is not the visual voicemail package.
Ta-wei Yen1b455992016-12-27 14:52:32 -08003474 */
Ta-wei Yen463efd12017-01-06 15:29:25 -08003475 private void enforceVisualVoicemailPackage(String callingPackage, int subId) {
3476 String vvmPackage = RemoteVvmTaskManager.getRemotePackage(mPhone.getContext(), subId)
3477 .getPackageName();
3478 if (!callingPackage.equals(vvmPackage)) {
3479 throw new SecurityException("Caller not current active visual voicemail package[" +
3480 vvmPackage + "]");
Ta-wei Yen1b455992016-12-27 14:52:32 -08003481 }
3482 }
3483
3484 /**
Youhan Wange64578a2016-05-02 15:32:42 -07003485 * Return the application ID for the app type.
3486 *
3487 * @param subId the subscription ID that this request applies to.
3488 * @param appType the uicc app type.
3489 * @return Application ID for specificied app type, or null if no uicc.
3490 */
3491 @Override
3492 public String getAidForAppType(int subId, int appType) {
3493 enforceReadPrivilegedPermission();
3494 Phone phone = getPhone(subId);
3495 if (phone == null) {
3496 return null;
3497 }
3498 String aid = null;
3499 try {
3500 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3501 .getApplicationByType(appType).getAid();
3502 } catch (Exception e) {
3503 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3504 }
3505 return aid;
3506 }
3507
Youhan Wang4001d252016-05-11 10:29:41 -07003508 /**
3509 * Return the Electronic Serial Number.
3510 *
3511 * @param subId the subscription ID that this request applies to.
3512 * @return ESN or null if error.
3513 */
3514 @Override
3515 public String getEsn(int subId) {
3516 enforceReadPrivilegedPermission();
3517 Phone phone = getPhone(subId);
3518 if (phone == null) {
3519 return null;
3520 }
3521 String esn = null;
3522 try {
3523 esn = phone.getEsn();
3524 } catch (Exception e) {
3525 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3526 }
3527 return esn;
3528 }
3529
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003530 /**
Youhan Wang66ad5d72016-07-18 17:56:58 -07003531 * Return the Preferred Roaming List Version.
3532 *
3533 * @param subId the subscription ID that this request applies to.
3534 * @return PRLVersion or null if error.
3535 */
3536 @Override
3537 public String getCdmaPrlVersion(int subId) {
3538 enforceReadPrivilegedPermission();
3539 Phone phone = getPhone(subId);
3540 if (phone == null) {
3541 return null;
3542 }
3543 String cdmaPrlVersion = null;
3544 try {
3545 cdmaPrlVersion = phone.getCdmaPrlVersion();
3546 } catch (Exception e) {
3547 Log.e(LOG_TAG, "Not getting PRLVersion", e);
3548 }
3549 return cdmaPrlVersion;
3550 }
3551
3552 /**
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003553 * Get snapshot of Telephony histograms
3554 * @return List of Telephony histograms
3555 * @hide
3556 */
3557 @Override
3558 public List<TelephonyHistogram> getTelephonyHistograms() {
3559 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3560 return RIL.getTelephonyRILTimingHistograms();
3561 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003562
3563 /**
3564 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003565 * Set the allowed carrier list for slotIndex
Meng Wang1a7c35a2016-05-05 20:56:15 -07003566 * Require system privileges. In the future we may add this to carrier APIs.
3567 *
3568 * @return The number of carriers set successfully, should match length of carriers
3569 */
3570 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003571 public int setAllowedCarriers(int slotIndex, List<CarrierIdentifier> carriers) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003572 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003573 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003574 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3575 return retVal[0];
3576 }
3577
3578 /**
3579 * {@hide}
Sanket Padawe5780e442017-03-20 15:04:47 -07003580 * Get the allowed carrier list for slotIndex.
Meng Wang1a7c35a2016-05-05 20:56:15 -07003581 * Require system privileges. In the future we may add this to carrier APIs.
3582 *
3583 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3584 * means all carriers are allowed.
3585 */
3586 @Override
Sanket Padawe5780e442017-03-20 15:04:47 -07003587 public List<CarrierIdentifier> getAllowedCarriers(int slotIndex) {
Meng Wang1a7c35a2016-05-05 20:56:15 -07003588 enforceReadPrivilegedPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003589 int subId = SubscriptionManager.getSubId(slotIndex)[0];
Meng Wang1a7c35a2016-05-05 20:56:15 -07003590 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3591 }
3592
fionaxu59545b42016-05-25 15:53:37 -07003593 /**
3594 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3595 * @param subId the subscription ID that this action applies to.
3596 * @param enabled control enable or disable metered apns.
3597 * {@hide}
3598 */
3599 @Override
3600 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3601 enforceModifyPermission();
3602 final Phone phone = getPhone(subId);
3603 if (phone == null) {
3604 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3605 return;
3606 }
3607 try {
3608 phone.carrierActionSetMeteredApnsEnabled(enabled);
3609 } catch (Exception e) {
3610 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3611 }
3612 }
3613
3614 /**
3615 * Action set from carrier signalling broadcast receivers to enable/disable radio
3616 * @param subId the subscription ID that this action applies to.
3617 * @param enabled control enable or disable radio.
3618 * {@hide}
3619 */
3620 @Override
3621 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3622 enforceModifyPermission();
3623 final Phone phone = getPhone(subId);
3624 if (phone == null) {
3625 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3626 return;
3627 }
3628 try {
3629 phone.carrierActionSetRadioEnabled(enabled);
3630 } catch (Exception e) {
3631 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3632 }
3633 }
3634
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003635 /**
3636 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3637 * bug report is being generated.
3638 */
3639 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003640 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
dcashman22b950d2016-06-27 11:39:02 -07003641 if (mPhone.getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
3642 != PackageManager.PERMISSION_GRANTED) {
3643 writer.println("Permission Denial: can't dump Phone from pid="
3644 + Binder.getCallingPid()
3645 + ", uid=" + Binder.getCallingUid()
3646 + "without permission "
3647 + android.Manifest.permission.DUMP);
3648 return;
3649 }
Ta-wei Yen99282e02016-06-21 18:19:35 -07003650 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003651 }
Jack Yueb89b242016-06-22 13:27:47 -07003652
3653 /**
3654 * Get aggregated video call data usage from all subscriptions since boot.
3655 * @return total data usage in bytes
3656 * {@hide}
3657 */
3658 @Override
3659 public long getVtDataUsage() {
3660 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_NETWORK_USAGE_HISTORY,
3661 null);
3662
3663 // NetworkStatsService keeps tracking the active network interface and identity. It will
3664 // record the delta with the corresponding network identity. What we need to do here is
3665 // returning total video call data usage from all subscriptions since boot.
3666
3667 // TODO: Add sub id support in the future. We'll need it when we support DSDA and
3668 // simultaneous VT calls.
3669 final Phone[] phones = PhoneFactory.getPhones();
3670 long total = 0;
3671 for (Phone phone : phones) {
3672 total += phone.getVtDataUsage();
3673 }
3674 return total;
3675 }
Jack Yu75ab2952016-07-08 14:29:33 -07003676
3677 /**
3678 * Policy control of data connection. Usually used when data limit is passed.
3679 * @param enabled True if enabling the data, otherwise disabling.
3680 * @param subId Subscription index
3681 * {@hide}
3682 */
3683 @Override
3684 public void setPolicyDataEnabled(boolean enabled, int subId) {
3685 enforceModifyPermission();
3686 Phone phone = getPhone(subId);
3687 if (phone != null) {
3688 phone.setPolicyDataEnabled(enabled);
3689 }
3690 }
Sooraj Sasindran22882212016-07-18 11:57:25 -07003691
3692 /**
3693 * Get Client request stats
3694 * @return List of Client Request Stats
3695 * @hide
3696 */
3697 @Override
3698 public List<ClientRequestStats> getClientRequestStats(String callingPackage, int subId) {
3699 if (!canReadPhoneState(callingPackage, "getClientRequestStats")) {
3700 return null;
3701 }
3702
3703 Phone phone = getPhone(subId);
3704 if (phone != null) {
3705 return phone.getClientRequestStats();
3706 }
3707
3708 return null;
3709 }
3710
3711 private WorkSource getWorkSource(WorkSource workSource, int uid) {
3712 if (workSource != null) {
3713 return workSource;
3714 }
3715
3716 String packageName = mPhone.getContext().getPackageManager().getNameForUid(uid);
3717 workSource = new WorkSource(uid, packageName);
3718 return workSource;
3719 }
Jack Yu346de222017-02-16 15:32:43 -08003720
3721 /**
Grace Chenf4a72232017-03-24 17:21:30 -07003722 * Set SIM card power state.
Jack Yu346de222017-02-16 15:32:43 -08003723 *
Sanket Padawe5780e442017-03-20 15:04:47 -07003724 * @param slotIndex SIM slot id.
Grace Chenf4a72232017-03-24 17:21:30 -07003725 * @param state State of SIM (power down, power up, pass through)
3726 * - {@link android.telephony.TelephonyManager#CARD_POWER_DOWN}
3727 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP}
3728 * - {@link android.telephony.TelephonyManager#CARD_POWER_UP_PASS_THROUGH}
Jack Yu346de222017-02-16 15:32:43 -08003729 *
3730 **/
3731 @Override
Grace Chenf4a72232017-03-24 17:21:30 -07003732 public void setSimPowerStateForSlot(int slotIndex, int state) {
Jack Yu346de222017-02-16 15:32:43 -08003733 enforceModifyPermission();
Sanket Padawe5780e442017-03-20 15:04:47 -07003734 int subId[] = mSubscriptionController.getSubIdUsingSlotIndex(slotIndex);
Jack Yu346de222017-02-16 15:32:43 -08003735 if (subId == null || subId.length == 0) {
3736 return;
3737 }
3738
3739 final Phone phone = getPhone(subId[0]);
3740 if (phone != null) {
Grace Chenf4a72232017-03-24 17:21:30 -07003741 phone.setSimPowerState(state);
Jack Yu346de222017-02-16 15:32:43 -08003742 }
3743 }
shuoqfef36a32017-01-10 13:02:18 -08003744
3745 /**
3746 * Check if phone is in emergency callback mode
3747 * @return true if phone is in emergency callback mode
3748 * @param subId sub id
3749 */
3750 public boolean getEmergencyCallbackMode(int subId) {
3751 final Phone phone = getPhone(subId);
3752 if (phone != null) {
3753 return phone.isInEcm();
3754 } else {
3755 return false;
3756 }
3757 }
Nathan Harold46b42aa2017-03-10 19:38:22 -08003758
3759 /**
3760 * Get the current signal strength information for the given subscription.
3761 * Because this information is not updated when the device is in a low power state
3762 * it should not be relied-upon to be current.
3763 * @param subId Subscription index
3764 * @return the most recent cached signal strength info from the modem
3765 */
3766 @Override
3767 public SignalStrength getSignalStrength(int subId) {
3768 Phone p = getPhone(subId);
3769 if (p == null) {
3770 return null;
3771 }
3772
3773 return p.getSignalStrength();
3774 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003775}