blob: 2fd21712f0a8780dffab2dd851735650fd156f70 [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
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Meng Wang1a7c35a2016-05-05 20:56:15 -070043import android.service.carrier.CarrierIdentifier;
Santos Cordon7a1885b2015-02-03 11:15:19 -080044import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080045import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070046import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070047import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070048import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070049import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070050import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080051import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070052import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070053import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080054import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080055import android.telephony.SubscriptionManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070056import android.telephony.TelephonyHistogram;
Ta-wei Yenb6929602016-05-24 15:48:27 -070057import android.telephony.TelephonyManager;
58import android.telephony.VisualVoicemailSmsFilterSettings;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070059import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080060import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070061import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080062import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080063import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064
Andrew Lee312e8172014-10-23 17:01:36 -070065import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070066import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070068import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080071import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010072import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070073import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070074import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070075import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070076import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070077import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070078import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070079import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080080import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070081import com.android.internal.telephony.uicc.IccIoResult;
82import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070083import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080085import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080086import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
Ta-wei Yenc236d6b2016-06-21 13:33:12 -070088import java.io.FileDescriptor;
89import java.io.PrintWriter;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070090import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080091import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080092import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010093import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080094import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
96/**
97 * Implementation of the ITelephony interface.
98 */
Santos Cordon117fee72014-05-16 17:56:12 -070099public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700100 private static final String LOG_TAG = "PhoneInterfaceManager";
101 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
102 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800103 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700104
105 // Message codes used with mMainThreadHandler
106 private static final int CMD_HANDLE_PIN_MMI = 1;
107 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
108 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
109 private static final int CMD_ANSWER_RINGING_CALL = 4;
110 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700111 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
112 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700113 private static final int CMD_OPEN_CHANNEL = 9;
114 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
115 private static final int CMD_CLOSE_CHANNEL = 11;
116 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800117 private static final int CMD_NV_READ_ITEM = 13;
118 private static final int EVENT_NV_READ_ITEM_DONE = 14;
119 private static final int CMD_NV_WRITE_ITEM = 15;
120 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
121 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
122 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
123 private static final int CMD_NV_RESET_CONFIG = 19;
124 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800125 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
126 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
127 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
128 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800129 private static final int CMD_SEND_ENVELOPE = 25;
130 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700131 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
132 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
133 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
134 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
135 private static final int CMD_EXCHANGE_SIM_IO = 31;
136 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800137 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
138 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700139 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
140 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700141 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
142 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700143 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
144 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
145 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
146 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Meng Wang1a7c35a2016-05-05 20:56:15 -0700147 private static final int CMD_SET_ALLOWED_CARRIERS = 43;
148 private static final int EVENT_SET_ALLOWED_CARRIERS_DONE = 44;
149 private static final int CMD_GET_ALLOWED_CARRIERS = 45;
150 private static final int EVENT_GET_ALLOWED_CARRIERS_DONE = 46;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700151
152 /** The singleton instance. */
153 private static PhoneInterfaceManager sInstance;
154
Wink Saville3ab207e2014-11-20 13:07:20 -0800155 private PhoneGlobals mApp;
156 private Phone mPhone;
157 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700158 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800159 private AppOpsManager mAppOps;
160 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800161 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800162 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700163
Derek Tan97ebb422014-09-05 16:55:38 -0700164 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
165 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800166 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700167
168 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700169 * A request object to use for transmitting data to an ICC.
170 */
171 private static final class IccAPDUArgument {
172 public int channel, cla, command, p1, p2, p3;
173 public String data;
174
175 public IccAPDUArgument(int channel, int cla, int command,
176 int p1, int p2, int p3, String data) {
177 this.channel = channel;
178 this.cla = cla;
179 this.command = command;
180 this.p1 = p1;
181 this.p2 = p2;
182 this.p3 = p3;
183 this.data = data;
184 }
185 }
186
187 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700188 * A request object to use for transmitting data to an ICC.
189 */
190 private static final class ManualNetworkSelectionArgument {
191 public OperatorInfo operatorInfo;
192 public boolean persistSelection;
193
194 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
195 this.operatorInfo = operatorInfo;
196 this.persistSelection = persistSelection;
197 }
198 }
199
200 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
202 * request after sending. The main thread will notify the request when it is complete.
203 */
204 private static final class MainThreadRequest {
205 /** The argument to use for the request */
206 public Object argument;
207 /** The result of the request that is run on the main thread */
208 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800209 // The subscriber id that this request applies to. Defaults to
210 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
211 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212
213 public MainThreadRequest(Object argument) {
214 this.argument = argument;
215 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800216
217 public MainThreadRequest(Object argument, Integer subId) {
218 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800219 if (subId != null) {
220 this.subId = subId;
221 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800222 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 }
224
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800225 private static final class IncomingThirdPartyCallArgs {
226 public final ComponentName component;
227 public final String callId;
228 public final String callerDisplayName;
229
230 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
231 String callerDisplayName) {
232 this.component = component;
233 this.callId = callId;
234 this.callerDisplayName = callerDisplayName;
235 }
236 }
237
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700238 /**
239 * A handler that processes messages on the main thread in the phone process. Since many
240 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
241 * inbound binder threads to the main thread in the phone process. The Binder thread
242 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
243 * on, which will be notified when the operation completes and will contain the result of the
244 * request.
245 *
246 * <p>If a MainThreadRequest object is provided in the msg.obj field,
247 * note that request.result must be set to something non-null for the calling thread to
248 * unblock.
249 */
250 private final class MainThreadHandler extends Handler {
251 @Override
252 public void handleMessage(Message msg) {
253 MainThreadRequest request;
254 Message onCompleted;
255 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800256 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700257 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258
259 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700260 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700262 final Phone phone = getPhoneFromRequest(request);
263 request.result = phone != null ?
264 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
265 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266 // Wake up the requesting thread
267 synchronized (request) {
268 request.notifyAll();
269 }
270 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700271 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700272
273 case CMD_HANDLE_NEIGHBORING_CELL:
274 request = (MainThreadRequest) msg.obj;
275 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
276 request);
277 mPhone.getNeighboringCids(onCompleted);
278 break;
279
280 case EVENT_NEIGHBORING_CELL_DONE:
281 ar = (AsyncResult) msg.obj;
282 request = (MainThreadRequest) ar.userObj;
283 if (ar.exception == null && ar.result != null) {
284 request.result = ar.result;
285 } else {
286 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800287 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 }
289 // Wake up the requesting thread
290 synchronized (request) {
291 request.notifyAll();
292 }
293 break;
294
295 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700296 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800297 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700298 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 break;
300
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 case CMD_END_CALL:
302 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800303 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700304 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700305 Phone phone = getPhone(end_subId);
306 if (phone == null) {
307 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
308 break;
309 }
310 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700311 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
312 // CDMA: If the user presses the Power button we treat it as
313 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700314 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700315 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
316 // GSM: End the call as per the Phone state
317 hungUp = PhoneUtils.hangup(mCM);
318 } else {
319 throw new IllegalStateException("Unexpected phone type: " + phoneType);
320 }
321 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
322 request.result = hungUp;
323 // Wake up the requesting thread
324 synchronized (request) {
325 request.notifyAll();
326 }
327 break;
328
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700329 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700330 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700331 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800332 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700333 if (uiccCard == null) {
334 loge("iccTransmitApduLogicalChannel: No UICC");
335 request.result = new IccIoResult(0x6F, 0, (byte[])null);
336 synchronized (request) {
337 request.notifyAll();
338 }
339 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700340 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
341 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700342 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700343 iccArgument.channel, iccArgument.cla, iccArgument.command,
344 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700345 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700346 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 break;
348
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700349 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700350 ar = (AsyncResult) msg.obj;
351 request = (MainThreadRequest) ar.userObj;
352 if (ar.exception == null && ar.result != null) {
353 request.result = ar.result;
354 } else {
355 request.result = new IccIoResult(0x6F, 0, (byte[])null);
356 if (ar.result == null) {
357 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800358 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700359 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800360 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700361 } else {
362 loge("iccTransmitApduLogicalChannel: Unknown exception");
363 }
364 }
365 synchronized (request) {
366 request.notifyAll();
367 }
368 break;
369
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700370 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
371 request = (MainThreadRequest) msg.obj;
372 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800373 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700374 if (uiccCard == null) {
375 loge("iccTransmitApduBasicChannel: No UICC");
376 request.result = new IccIoResult(0x6F, 0, (byte[])null);
377 synchronized (request) {
378 request.notifyAll();
379 }
380 } else {
381 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
382 request);
383 uiccCard.iccTransmitApduBasicChannel(
384 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
385 iccArgument.p3, iccArgument.data, onCompleted);
386 }
387 break;
388
389 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
390 ar = (AsyncResult) msg.obj;
391 request = (MainThreadRequest) ar.userObj;
392 if (ar.exception == null && ar.result != null) {
393 request.result = ar.result;
394 } else {
395 request.result = new IccIoResult(0x6F, 0, (byte[])null);
396 if (ar.result == null) {
397 loge("iccTransmitApduBasicChannel: Empty response");
398 } else if (ar.exception instanceof CommandException) {
399 loge("iccTransmitApduBasicChannel: CommandException: " +
400 ar.exception);
401 } else {
402 loge("iccTransmitApduBasicChannel: Unknown exception");
403 }
404 }
405 synchronized (request) {
406 request.notifyAll();
407 }
408 break;
409
410 case CMD_EXCHANGE_SIM_IO:
411 request = (MainThreadRequest) msg.obj;
412 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800413 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700414 if (uiccCard == null) {
415 loge("iccExchangeSimIO: No UICC");
416 request.result = new IccIoResult(0x6F, 0, (byte[])null);
417 synchronized (request) {
418 request.notifyAll();
419 }
420 } else {
421 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
422 request);
423 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
424 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
425 iccArgument.data, onCompleted);
426 }
427 break;
428
429 case EVENT_EXCHANGE_SIM_IO_DONE:
430 ar = (AsyncResult) msg.obj;
431 request = (MainThreadRequest) ar.userObj;
432 if (ar.exception == null && ar.result != null) {
433 request.result = ar.result;
434 } else {
435 request.result = new IccIoResult(0x6f, 0, (byte[])null);
436 }
437 synchronized (request) {
438 request.notifyAll();
439 }
440 break;
441
Derek Tan4d5e5c12014-02-04 11:54:58 -0800442 case CMD_SEND_ENVELOPE:
443 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800444 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700445 if (uiccCard == null) {
446 loge("sendEnvelopeWithStatus: No UICC");
447 request.result = new IccIoResult(0x6F, 0, (byte[])null);
448 synchronized (request) {
449 request.notifyAll();
450 }
451 } else {
452 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
453 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
454 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800455 break;
456
457 case EVENT_SEND_ENVELOPE_DONE:
458 ar = (AsyncResult) msg.obj;
459 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700460 if (ar.exception == null && ar.result != null) {
461 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800462 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700463 request.result = new IccIoResult(0x6F, 0, (byte[])null);
464 if (ar.result == null) {
465 loge("sendEnvelopeWithStatus: Empty response");
466 } else if (ar.exception instanceof CommandException) {
467 loge("sendEnvelopeWithStatus: CommandException: " +
468 ar.exception);
469 } else {
470 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
471 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800472 }
473 synchronized (request) {
474 request.notifyAll();
475 }
476 break;
477
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 case CMD_OPEN_CHANNEL:
479 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800480 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700481 if (uiccCard == null) {
482 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800483 request.result = new IccOpenLogicalChannelResponse(-1,
484 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700485 synchronized (request) {
486 request.notifyAll();
487 }
488 } else {
489 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
490 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
491 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 break;
493
494 case EVENT_OPEN_CHANNEL_DONE:
495 ar = (AsyncResult) msg.obj;
496 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700499 int[] result = (int[]) ar.result;
500 int channelId = result[0];
501 byte[] selectResponse = null;
502 if (result.length > 1) {
503 selectResponse = new byte[result.length - 1];
504 for (int i = 1; i < result.length; ++i) {
505 selectResponse[i - 1] = (byte) result[i];
506 }
507 }
508 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700509 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 if (ar.result == null) {
512 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700514 if (ar.exception != null) {
515 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
516 }
517
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700518 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700519 if (ar.exception instanceof CommandException) {
520 CommandException.Error error =
521 ((CommandException) (ar.exception)).getCommandError();
522 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700523 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700524 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700525 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700526 }
527 }
528 openChannelResp = new IccOpenLogicalChannelResponse(
529 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700530 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700531 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700532 synchronized (request) {
533 request.notifyAll();
534 }
535 break;
536
537 case CMD_CLOSE_CHANNEL:
538 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800539 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700540 if (uiccCard == null) {
541 loge("iccCloseLogicalChannel: No UICC");
542 request.result = new IccIoResult(0x6F, 0, (byte[])null);
543 synchronized (request) {
544 request.notifyAll();
545 }
546 } else {
547 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
548 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
549 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 break;
551
552 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800553 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
554 break;
555
556 case CMD_NV_READ_ITEM:
557 request = (MainThreadRequest) msg.obj;
558 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
559 mPhone.nvReadItem((Integer) request.argument, onCompleted);
560 break;
561
562 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700563 ar = (AsyncResult) msg.obj;
564 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800565 if (ar.exception == null && ar.result != null) {
566 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700567 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800568 request.result = "";
569 if (ar.result == null) {
570 loge("nvReadItem: Empty response");
571 } else if (ar.exception instanceof CommandException) {
572 loge("nvReadItem: CommandException: " +
573 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700574 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800575 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700576 }
577 }
578 synchronized (request) {
579 request.notifyAll();
580 }
581 break;
582
Jake Hambye994d462014-02-03 13:10:13 -0800583 case CMD_NV_WRITE_ITEM:
584 request = (MainThreadRequest) msg.obj;
585 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
586 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
587 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
588 break;
589
590 case EVENT_NV_WRITE_ITEM_DONE:
591 handleNullReturnEvent(msg, "nvWriteItem");
592 break;
593
594 case CMD_NV_WRITE_CDMA_PRL:
595 request = (MainThreadRequest) msg.obj;
596 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
597 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
598 break;
599
600 case EVENT_NV_WRITE_CDMA_PRL_DONE:
601 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
602 break;
603
604 case CMD_NV_RESET_CONFIG:
605 request = (MainThreadRequest) msg.obj;
606 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
607 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
608 break;
609
610 case EVENT_NV_RESET_CONFIG_DONE:
611 handleNullReturnEvent(msg, "nvResetConfig");
612 break;
613
Jake Hamby7c27be32014-03-03 13:25:59 -0800614 case CMD_GET_PREFERRED_NETWORK_TYPE:
615 request = (MainThreadRequest) msg.obj;
616 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700617 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800618 break;
619
620 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
621 ar = (AsyncResult) msg.obj;
622 request = (MainThreadRequest) ar.userObj;
623 if (ar.exception == null && ar.result != null) {
624 request.result = ar.result; // Integer
625 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800626 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800627 if (ar.result == null) {
628 loge("getPreferredNetworkType: Empty response");
629 } else if (ar.exception instanceof CommandException) {
630 loge("getPreferredNetworkType: CommandException: " +
631 ar.exception);
632 } else {
633 loge("getPreferredNetworkType: Unknown exception");
634 }
635 }
636 synchronized (request) {
637 request.notifyAll();
638 }
639 break;
640
641 case CMD_SET_PREFERRED_NETWORK_TYPE:
642 request = (MainThreadRequest) msg.obj;
643 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
644 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700645 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800646 break;
647
648 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
649 handleNullReturnEvent(msg, "setPreferredNetworkType");
650 break;
651
Steven Liu4bf01bc2014-07-17 11:05:29 -0500652 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
653 request = (MainThreadRequest)msg.obj;
654 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
655 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
656 break;
657
658 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
659 ar = (AsyncResult)msg.obj;
660 request = (MainThreadRequest)ar.userObj;
661 request.result = ar;
662 synchronized (request) {
663 request.notifyAll();
664 }
665 break;
666
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800667 case CMD_SET_VOICEMAIL_NUMBER:
668 request = (MainThreadRequest) msg.obj;
669 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
670 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800671 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
672 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800673 break;
674
675 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
676 handleNullReturnEvent(msg, "setVoicemailNumber");
677 break;
678
Stuart Scott54788802015-03-30 13:18:01 -0700679 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
680 request = (MainThreadRequest) msg.obj;
681 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
682 request);
683 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
684 break;
685
686 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
687 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
688 break;
689
Shishir Agrawal302c8692015-06-19 13:49:39 -0700690 case CMD_PERFORM_NETWORK_SCAN:
691 request = (MainThreadRequest) msg.obj;
692 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
693 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
694 break;
695
696 case EVENT_PERFORM_NETWORK_SCAN_DONE:
697 ar = (AsyncResult) msg.obj;
698 request = (MainThreadRequest) ar.userObj;
699 CellNetworkScanResult cellScanResult;
700 if (ar.exception == null && ar.result != null) {
701 cellScanResult = new CellNetworkScanResult(
702 CellNetworkScanResult.STATUS_SUCCESS,
703 (List<OperatorInfo>) ar.result);
704 } else {
705 if (ar.result == null) {
706 loge("getCellNetworkScanResults: Empty response");
707 }
708 if (ar.exception != null) {
709 loge("getCellNetworkScanResults: Exception: " + ar.exception);
710 }
711 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
712 if (ar.exception instanceof CommandException) {
713 CommandException.Error error =
714 ((CommandException) (ar.exception)).getCommandError();
715 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
716 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
717 } else if (error == CommandException.Error.GENERIC_FAILURE) {
718 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
719 }
720 }
721 cellScanResult = new CellNetworkScanResult(errorCode, null);
722 }
723 request.result = cellScanResult;
724 synchronized (request) {
725 request.notifyAll();
726 }
727 break;
728
729 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
730 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700731 ManualNetworkSelectionArgument selArg =
732 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700733 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
734 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700735 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
736 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700737 break;
738
739 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
740 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
741 break;
742
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700743 case CMD_GET_MODEM_ACTIVITY_INFO:
744 request = (MainThreadRequest) msg.obj;
745 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700746 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700747 break;
748
749 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
750 ar = (AsyncResult) msg.obj;
751 request = (MainThreadRequest) ar.userObj;
752 if (ar.exception == null && ar.result != null) {
753 request.result = ar.result;
754 } else {
755 if (ar.result == null) {
756 loge("queryModemActivityInfo: Empty response");
757 } else if (ar.exception instanceof CommandException) {
758 loge("queryModemActivityInfo: CommandException: " +
759 ar.exception);
760 } else {
761 loge("queryModemActivityInfo: Unknown exception");
762 }
763 }
Amit Mahajand4766222016-01-28 15:28:28 -0800764 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
765 if (request.result == null) {
766 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
767 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700768 synchronized (request) {
769 request.notifyAll();
770 }
771 break;
772
Meng Wang1a7c35a2016-05-05 20:56:15 -0700773 case CMD_SET_ALLOWED_CARRIERS:
774 request = (MainThreadRequest) msg.obj;
775 onCompleted = obtainMessage(EVENT_SET_ALLOWED_CARRIERS_DONE, request);
776 mPhone.setAllowedCarriers(
777 (List<CarrierIdentifier>) request.argument,
778 onCompleted);
779 break;
780
781 case EVENT_SET_ALLOWED_CARRIERS_DONE:
782 ar = (AsyncResult) msg.obj;
783 request = (MainThreadRequest) ar.userObj;
784 if (ar.exception == null && ar.result != null) {
785 request.result = ar.result;
786 } else {
787 if (ar.result == null) {
788 loge("setAllowedCarriers: Empty response");
789 } else if (ar.exception instanceof CommandException) {
790 loge("setAllowedCarriers: CommandException: " +
791 ar.exception);
792 } else {
793 loge("setAllowedCarriers: Unknown exception");
794 }
795 }
796 // Result cannot be null. Return -1 on error.
797 if (request.result == null) {
798 request.result = new int[]{-1};
799 }
800 synchronized (request) {
801 request.notifyAll();
802 }
803 break;
804
805 case CMD_GET_ALLOWED_CARRIERS:
806 request = (MainThreadRequest) msg.obj;
807 onCompleted = obtainMessage(EVENT_GET_ALLOWED_CARRIERS_DONE, request);
808 mPhone.getAllowedCarriers(onCompleted);
809 break;
810
811 case EVENT_GET_ALLOWED_CARRIERS_DONE:
812 ar = (AsyncResult) msg.obj;
813 request = (MainThreadRequest) ar.userObj;
814 if (ar.exception == null && ar.result != null) {
815 request.result = ar.result;
816 } else {
817 if (ar.result == null) {
818 loge("getAllowedCarriers: Empty response");
819 } else if (ar.exception instanceof CommandException) {
820 loge("getAllowedCarriers: CommandException: " +
821 ar.exception);
822 } else {
823 loge("getAllowedCarriers: Unknown exception");
824 }
825 }
826 // Result cannot be null. Return empty list of CarrierIdentifier.
827 if (request.result == null) {
828 request.result = new ArrayList<CarrierIdentifier>(0);
829 }
830 synchronized (request) {
831 request.notifyAll();
832 }
833 break;
834
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700835 default:
836 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
837 break;
838 }
839 }
Jake Hambye994d462014-02-03 13:10:13 -0800840
841 private void handleNullReturnEvent(Message msg, String command) {
842 AsyncResult ar = (AsyncResult) msg.obj;
843 MainThreadRequest request = (MainThreadRequest) ar.userObj;
844 if (ar.exception == null) {
845 request.result = true;
846 } else {
847 request.result = false;
848 if (ar.exception instanceof CommandException) {
849 loge(command + ": CommandException: " + ar.exception);
850 } else {
851 loge(command + ": Unknown exception");
852 }
853 }
854 synchronized (request) {
855 request.notifyAll();
856 }
857 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 }
859
860 /**
861 * Posts the specified command to be executed on the main thread,
862 * waits for the request to complete, and returns the result.
863 * @see #sendRequestAsync
864 */
865 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800866 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700867 }
868
869 /**
870 * Posts the specified command to be executed on the main thread,
871 * waits for the request to complete, and returns the result.
872 * @see #sendRequestAsync
873 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800874 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
876 throw new RuntimeException("This method will deadlock if called from the main thread.");
877 }
878
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800879 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 Message msg = mMainThreadHandler.obtainMessage(command, request);
881 msg.sendToTarget();
882
883 // Wait for the request to complete
884 synchronized (request) {
885 while (request.result == null) {
886 try {
887 request.wait();
888 } catch (InterruptedException e) {
889 // Do nothing, go back and wait until the request is complete
890 }
891 }
892 }
893 return request.result;
894 }
895
896 /**
897 * Asynchronous ("fire and forget") version of sendRequest():
898 * Posts the specified command to be executed on the main thread, and
899 * returns immediately.
900 * @see #sendRequest
901 */
902 private void sendRequestAsync(int command) {
903 mMainThreadHandler.sendEmptyMessage(command);
904 }
905
906 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700907 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
908 * @see {@link #sendRequest(int,Object)}
909 */
910 private void sendRequestAsync(int command, Object argument) {
911 MainThreadRequest request = new MainThreadRequest(argument);
912 Message msg = mMainThreadHandler.obtainMessage(command, request);
913 msg.sendToTarget();
914 }
915
916 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700917 * Initialize the singleton PhoneInterfaceManager instance.
918 * This is only done once, at startup, from PhoneApp.onCreate().
919 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700920 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700921 synchronized (PhoneInterfaceManager.class) {
922 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700923 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700924 } else {
925 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
926 }
927 return sInstance;
928 }
929 }
930
931 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700932 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700933 mApp = app;
934 mPhone = phone;
935 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700936 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
938 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700939 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700940 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800941 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800942
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 publish();
944 }
945
946 private void publish() {
947 if (DBG) log("publish: " + this);
948
949 ServiceManager.addService("phone", this);
950 }
951
Stuart Scott584921c2015-01-15 17:10:34 -0800952 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800953 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
954 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800955 }
956
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800957 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
958 Phone phone = getPhoneFromRequest(request);
959 return phone == null ? null :
960 UiccController.getInstance().getUiccCard(phone.getPhoneId());
961 }
962
Wink Saville36469e72014-06-11 15:17:00 -0700963 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700964 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800965 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700966 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 //
968 // Implementation of the ITelephony interface.
969 //
970
971 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700972 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700973 }
974
Wink Savilleb564aae2014-10-23 10:18:09 -0700975 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 if (DBG) log("dial: " + number);
977 // No permission check needed here: This is just a wrapper around the
978 // ACTION_DIAL intent, which is available to any app since it puts up
979 // the UI before it does anything.
980
981 String url = createTelUrl(number);
982 if (url == null) {
983 return;
984 }
985
986 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700987 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
989 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
990 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
991 mApp.startActivity(intent);
992 }
993 }
994
995 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700996 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700997 }
998
Wink Savilleb564aae2014-10-23 10:18:09 -0700999 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 if (DBG) log("call: " + number);
1001
1002 // This is just a wrapper around the ACTION_CALL intent, but we still
1003 // need to do a permission check since we're calling startActivity()
1004 // from the context of the phone app.
1005 enforceCallPermission();
1006
1007 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
1008 != AppOpsManager.MODE_ALLOWED) {
1009 return;
1010 }
1011
1012 String url = createTelUrl(number);
1013 if (url == null) {
1014 return;
1015 }
1016
Wink Saville08874612014-08-31 19:19:58 -07001017 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +01001018 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -08001019 if (slist != null) {
1020 for (SubscriptionInfo subInfoRecord : slist) {
1021 if (subInfoRecord.getSubscriptionId() == subId) {
1022 isValid = true;
1023 break;
1024 }
Wink Saville08874612014-08-31 19:19:58 -07001025 }
1026 }
1027 if (isValid == false) {
1028 return;
1029 }
1030
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -07001032 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1034 mApp.startActivity(intent);
1035 }
1036
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 /**
1038 * End a call based on call state
1039 * @return true is a call was ended
1040 */
1041 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001042 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001043 }
1044
1045 /**
1046 * End a call based on the call state of the subId
1047 * @return true is a call was ended
1048 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001049 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001050 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001051 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001052 }
1053
1054 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001055 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001056 }
1057
Wink Savilleb564aae2014-10-23 10:18:09 -07001058 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001059 if (DBG) log("answerRingingCall...");
1060 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
1061 // but that can probably wait till the big TelephonyManager API overhaul.
1062 // For now, protect this call with the MODIFY_PHONE_STATE permission.
1063 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -08001064 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001065 }
1066
1067 /**
1068 * Make the actual telephony calls to implement answerRingingCall().
1069 * This should only be called from the main thread of the Phone app.
1070 * @see #answerRingingCall
1071 *
1072 * TODO: it would be nice to return true if we answered the call, or
1073 * false if there wasn't actually a ringing incoming call, or some
1074 * other error occurred. (In other words, pass back the return value
1075 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1076 * But that would require calling this method via sendRequest() rather
1077 * than sendRequestAsync(), and right now we don't actually *need* that
1078 * return value, so let's just return void for now.
1079 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001080 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001081 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001083 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1084 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001085 if (hasActiveCall && hasHoldingCall) {
1086 // Both lines are in use!
1087 // TODO: provide a flag to let the caller specify what
1088 // policy to use if both lines are in use. (The current
1089 // behavior is hardwired to "answer incoming, end ongoing",
1090 // which is how the CALL button is specced to behave.)
1091 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1092 return;
1093 } else {
1094 // answerCall() will automatically hold the current active
1095 // call, if there is one.
1096 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1097 return;
1098 }
1099 } else {
1100 // No call was ringing.
1101 return;
1102 }
1103 }
1104
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001105 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001106 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001108 public void silenceRinger() {
1109 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 }
1111
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001112 @Override
1113 public boolean isOffhook(String callingPackage) {
1114 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001115 }
1116
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001117 @Override
1118 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1119 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1120 return false;
1121 }
1122
Sanket Padawe356d7632015-06-22 14:03:32 -07001123 final Phone phone = getPhone(subId);
1124 if (phone != null) {
1125 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1126 } else {
1127 return false;
1128 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 }
1130
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001131 @Override
1132 public boolean isRinging(String callingPackage) {
1133 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001134 }
1135
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001136 @Override
1137 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1138 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1139 return false;
1140 }
1141
Sanket Padawe356d7632015-06-22 14:03:32 -07001142 final Phone phone = getPhone(subId);
1143 if (phone != null) {
1144 return (phone.getState() == PhoneConstants.State.RINGING);
1145 } else {
1146 return false;
1147 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001148 }
1149
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001150 @Override
1151 public boolean isIdle(String callingPackage) {
1152 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001153 }
1154
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001155 @Override
1156 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1157 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1158 return false;
1159 }
1160
Sanket Padawe356d7632015-06-22 14:03:32 -07001161 final Phone phone = getPhone(subId);
1162 if (phone != null) {
1163 return (phone.getState() == PhoneConstants.State.IDLE);
1164 } else {
1165 return false;
1166 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001167 }
1168
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001169 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001170 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001171 }
1172
Wink Savilleb564aae2014-10-23 10:18:09 -07001173 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001174 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001175 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1176 }
1177
1178 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001179 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001180 }
1181
Wink Savilleb564aae2014-10-23 10:18:09 -07001182 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001183 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001184 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1185 }
1186
1187 /** {@hide} */
1188 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001189 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001190 }
1191
Wink Savilleb564aae2014-10-23 10:18:09 -07001192 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001194 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001195 checkSimPin.start();
1196 return checkSimPin.unlockSim(null, pin);
1197 }
1198
Wink Saville9de0f752013-10-22 19:04:03 -07001199 /** {@hide} */
1200 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001201 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001202 }
1203
Wink Savilleb564aae2014-10-23 10:18:09 -07001204 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001205 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001206 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001207 checkSimPuk.start();
1208 return checkSimPuk.unlockSim(puk, pin);
1209 }
1210
1211 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001212 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001213 * a synchronous one.
1214 */
1215 private static class UnlockSim extends Thread {
1216
1217 private final IccCard mSimCard;
1218
1219 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001220 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1221 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222
1223 // For replies from SimCard interface
1224 private Handler mHandler;
1225
1226 // For async handler to identify request type
1227 private static final int SUPPLY_PIN_COMPLETE = 100;
1228
1229 public UnlockSim(IccCard simCard) {
1230 mSimCard = simCard;
1231 }
1232
1233 @Override
1234 public void run() {
1235 Looper.prepare();
1236 synchronized (UnlockSim.this) {
1237 mHandler = new Handler() {
1238 @Override
1239 public void handleMessage(Message msg) {
1240 AsyncResult ar = (AsyncResult) msg.obj;
1241 switch (msg.what) {
1242 case SUPPLY_PIN_COMPLETE:
1243 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1244 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001245 mRetryCount = msg.arg1;
1246 if (ar.exception != null) {
1247 if (ar.exception instanceof CommandException &&
1248 ((CommandException)(ar.exception)).getCommandError()
1249 == CommandException.Error.PASSWORD_INCORRECT) {
1250 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1251 } else {
1252 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1253 }
1254 } else {
1255 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1256 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 mDone = true;
1258 UnlockSim.this.notifyAll();
1259 }
1260 break;
1261 }
1262 }
1263 };
1264 UnlockSim.this.notifyAll();
1265 }
1266 Looper.loop();
1267 }
1268
1269 /*
1270 * Use PIN or PUK to unlock SIM card
1271 *
1272 * If PUK is null, unlock SIM card with PIN
1273 *
1274 * If PUK is not null, unlock SIM card with PUK and set PIN code
1275 */
Wink Saville9de0f752013-10-22 19:04:03 -07001276 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277
1278 while (mHandler == null) {
1279 try {
1280 wait();
1281 } catch (InterruptedException e) {
1282 Thread.currentThread().interrupt();
1283 }
1284 }
1285 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1286
1287 if (puk == null) {
1288 mSimCard.supplyPin(pin, callback);
1289 } else {
1290 mSimCard.supplyPuk(puk, pin, callback);
1291 }
1292
1293 while (!mDone) {
1294 try {
1295 Log.d(LOG_TAG, "wait for done");
1296 wait();
1297 } catch (InterruptedException e) {
1298 // Restore the interrupted status
1299 Thread.currentThread().interrupt();
1300 }
1301 }
1302 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001303 int[] resultArray = new int[2];
1304 resultArray[0] = mResult;
1305 resultArray[1] = mRetryCount;
1306 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 }
1308 }
1309
1310 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001311 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001312
1313 }
1314
Wink Savilleb564aae2014-10-23 10:18:09 -07001315 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001316 // No permission check needed here: this call is harmless, and it's
1317 // needed for the ServiceState.requestStateUpdate() call (which is
1318 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001319 final Phone phone = getPhone(subId);
1320 if (phone != null) {
1321 phone.updateServiceLocation();
1322 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 }
1324
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001325 @Override
1326 public boolean isRadioOn(String callingPackage) {
1327 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001328 }
1329
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001330 @Override
1331 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1332 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1333 return false;
1334 }
1335 return isRadioOnForSubscriber(subId);
1336 }
1337
1338 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001339 final Phone phone = getPhone(subId);
1340 if (phone != null) {
1341 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1342 } else {
1343 return false;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 }
1346
1347 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001348 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001349
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 }
Wink Saville36469e72014-06-11 15:17:00 -07001351
Wink Savilleb564aae2014-10-23 10:18:09 -07001352 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001354 final Phone phone = getPhone(subId);
1355 if (phone != null) {
1356 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1357 }
Wink Saville36469e72014-06-11 15:17:00 -07001358 }
1359
1360 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001361 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001362 }
1363
Wink Savilleb564aae2014-10-23 10:18:09 -07001364 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001365 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 final Phone phone = getPhone(subId);
1367 if (phone == null) {
1368 return false;
1369 }
1370 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001371 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001372 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374 return true;
1375 }
Wink Saville36469e72014-06-11 15:17:00 -07001376
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001377 public boolean needMobileRadioShutdown() {
1378 /*
1379 * If any of the Radios are available, it will need to be
1380 * shutdown. So return true if any Radio is available.
1381 */
1382 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1383 Phone phone = PhoneFactory.getPhone(i);
1384 if (phone != null && phone.isRadioAvailable()) return true;
1385 }
1386 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1387 return false;
1388 }
1389
1390 public void shutdownMobileRadios() {
1391 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1392 logv("Shutting down Phone " + i);
1393 shutdownRadioUsingPhoneId(i);
1394 }
1395 }
1396
1397 private void shutdownRadioUsingPhoneId(int phoneId) {
1398 enforceModifyPermission();
1399 Phone phone = PhoneFactory.getPhone(phoneId);
1400 if (phone != null && phone.isRadioAvailable()) {
1401 phone.shutdownRadio();
1402 }
1403 }
1404
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001406 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001407 }
1408
Wink Savilleb564aae2014-10-23 10:18:09 -07001409 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001411 final Phone phone = getPhone(subId);
1412 if (phone != null) {
1413 phone.setRadioPower(turnOn);
1414 return true;
1415 } else {
1416 return false;
1417 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001418 }
1419
Wink Saville36469e72014-06-11 15:17:00 -07001420 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001421 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 public boolean enableDataConnectivity() {
1423 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001424 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001425 final Phone phone = getPhone(subId);
1426 if (phone != null) {
1427 phone.setDataEnabled(true);
1428 return true;
1429 } else {
1430 return false;
1431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433
Wink Saville36469e72014-06-11 15:17:00 -07001434 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001435 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 public boolean disableDataConnectivity() {
1437 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001438 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001439 final Phone phone = getPhone(subId);
1440 if (phone != null) {
1441 phone.setDataEnabled(false);
1442 return true;
1443 } else {
1444 return false;
1445 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001446 }
1447
Wink Saville36469e72014-06-11 15:17:00 -07001448 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001449 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001450 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001451 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001452 final Phone phone = getPhone(subId);
1453 if (phone != null) {
1454 return phone.isDataConnectivityPossible();
1455 } else {
1456 return false;
1457 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001458 }
1459
1460 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001461 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001462 }
1463
Wink Savilleb564aae2014-10-23 10:18:09 -07001464 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001465 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001466 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1467 return false;
1468 }
Wink Saville36469e72014-06-11 15:17:00 -07001469 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 }
1471
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001472 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001473 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001474 }
1475
Shishir Agrawala9f32182016-04-12 12:00:16 -07001476 public int getCallStateForSlot(int slotId) {
1477 Phone phone = PhoneFactory.getPhone(slotId);
1478 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1479 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001480 }
1481
Sanket Padawe356d7632015-06-22 14:03:32 -07001482 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001484 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001485 if (phone != null) {
1486 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1487 } else {
1488 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 }
1491
Sanket Padawe356d7632015-06-22 14:03:32 -07001492 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001494 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001495 if (phone != null) {
1496 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1497 } else {
1498 return TelephonyManager.DATA_ACTIVITY_NONE;
1499 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 }
1501
1502 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001503 public Bundle getCellLocation(String callingPackage) {
1504 enforceFineOrCoarseLocationPermission("getCellLocation");
1505
1506 // OP_COARSE_LOCATION controls both fine and coarse location.
1507 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1508 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001509 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001510 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 }
1512
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001513 if (checkIfCallerIsSelfOrForegroundUser() ||
1514 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001515 if (DBG_LOC) log("getCellLocation: is active user");
1516 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001517 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001518 if (phone == null) {
1519 return null;
1520 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001521 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001522 return data;
1523 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001524 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001525 return null;
1526 }
1527 }
1528
Svetoslav64fad262015-04-14 14:35:21 -07001529 private void enforceFineOrCoarseLocationPermission(String message) {
1530 try {
1531 mApp.enforceCallingOrSelfPermission(
1532 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1533 } catch (SecurityException e) {
1534 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1535 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1536 // is the weaker precondition
1537 mApp.enforceCallingOrSelfPermission(
1538 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1539 }
1540 }
1541
1542
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001543 @Override
1544 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001545 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001546 }
1547
Sanket Padawe356d7632015-06-22 14:03:32 -07001548 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001549 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 mApp.enforceCallingOrSelfPermission(
1551 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001552 final Phone phone = getPhone(subId);
1553 if (phone != null) {
1554 phone.enableLocationUpdates();
1555 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 }
1557
1558 @Override
1559 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001560 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001561 }
1562
Sanket Padawe356d7632015-06-22 14:03:32 -07001563 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001564 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001565 mApp.enforceCallingOrSelfPermission(
1566 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001567 final Phone phone = getPhone(subId);
1568 if (phone != null) {
1569 phone.disableLocationUpdates();
1570 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001571 }
1572
1573 @Override
1574 @SuppressWarnings("unchecked")
1575 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001576 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1577
1578 // OP_COARSE_LOCATION controls both fine and coarse location.
1579 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1580 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1581 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001582 }
1583
1584 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1585 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1586 return null;
1587 }
Svetoslav64fad262015-04-14 14:35:21 -07001588
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001589 if (checkIfCallerIsSelfOrForegroundUser() ||
1590 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001591 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1592
1593 ArrayList<NeighboringCellInfo> cells = null;
1594
1595 try {
1596 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001597 CMD_HANDLE_NEIGHBORING_CELL, null,
1598 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001599 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001600 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001601 }
1602 return cells;
1603 } else {
1604 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1605 return null;
1606 }
1607 }
1608
1609
1610 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001611 public List<CellInfo> getAllCellInfo(String callingPackage) {
1612 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1613
1614 // OP_COARSE_LOCATION controls both fine and coarse location.
1615 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1616 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1617 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 }
1619
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001620 if (checkIfCallerIsSelfOrForegroundUser() ||
1621 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001622 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001623 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1624 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001625 final List<CellInfo> info = phone.getAllCellInfo();
1626 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001627 }
1628 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001629 } else {
1630 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1631 return null;
1632 }
1633 }
1634
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001635 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 public void setCellInfoListRate(int rateInMillis) {
1637 mPhone.setCellInfoListRate(rateInMillis);
1638 }
1639
Shishir Agrawala9f32182016-04-12 12:00:16 -07001640 @Override
1641 public String getImeiForSlot(int slotId, String callingPackage) {
1642 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1643 return null;
1644 }
1645 Phone phone = PhoneFactory.getPhone(slotId);
1646 return phone == null ? null : phone.getImei();
1647 }
1648
1649 @Override
1650 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1651 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1652 return null;
1653 }
1654 Phone phone = PhoneFactory.getPhone(slotId);
1655 return phone == null ? null : phone.getDeviceSvn();
1656 }
1657
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 //
1659 // Internal helper methods.
1660 //
1661
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001662 /**
1663 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1664 */
1665 private boolean checkCallerInteractAcrossUsersFull() {
1666 return mPhone.getContext().checkCallingOrSelfPermission(
1667 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1668 == PackageManager.PERMISSION_GRANTED;
1669 }
1670
Jake Hambye994d462014-02-03 13:10:13 -08001671 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 boolean ok;
1673
1674 boolean self = Binder.getCallingUid() == Process.myUid();
1675 if (!self) {
1676 // Get the caller's user id then clear the calling identity
1677 // which will be restored in the finally clause.
1678 int callingUser = UserHandle.getCallingUserId();
1679 long ident = Binder.clearCallingIdentity();
1680
1681 try {
1682 // With calling identity cleared the current user is the foreground user.
1683 int foregroundUser = ActivityManager.getCurrentUser();
1684 ok = (foregroundUser == callingUser);
1685 if (DBG_LOC) {
1686 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1687 + " callingUser=" + callingUser + " ok=" + ok);
1688 }
1689 } catch (Exception ex) {
1690 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1691 ok = false;
1692 } finally {
1693 Binder.restoreCallingIdentity(ident);
1694 }
1695 } else {
1696 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1697 ok = true;
1698 }
1699 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1700 return ok;
1701 }
1702
1703 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001704 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1705 *
1706 * @throws SecurityException if the caller does not have the required permission
1707 */
1708 private void enforceModifyPermission() {
1709 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1710 }
1711
1712 /**
Junda Liua2e36012014-07-09 18:30:01 -07001713 * Make sure either system app or the caller has carrier privilege.
1714 *
1715 * @throws SecurityException if the caller does not have the required permission/privilege
1716 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001717 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001718 int permission = mApp.checkCallingOrSelfPermission(
1719 android.Manifest.permission.MODIFY_PHONE_STATE);
1720 if (permission == PackageManager.PERMISSION_GRANTED) {
1721 return;
1722 }
1723
1724 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001725 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001726 }
1727
1728 /**
1729 * Make sure the caller has carrier privilege.
1730 *
1731 * @throws SecurityException if the caller does not have the required permission
1732 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001733 private void enforceCarrierPrivilege(int subId) {
1734 if (getCarrierPrivilegeStatus(subId) !=
1735 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001736 loge("No Carrier Privilege.");
1737 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001738 }
1739 }
1740
1741 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001742 * Make sure the caller has the CALL_PHONE permission.
1743 *
1744 * @throws SecurityException if the caller does not have the required permission
1745 */
1746 private void enforceCallPermission() {
1747 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1748 }
1749
Stuart Scott8eef64f2015-04-08 15:13:54 -07001750 private void enforceConnectivityInternalPermission() {
1751 mApp.enforceCallingOrSelfPermission(
1752 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1753 "ConnectivityService");
1754 }
1755
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001756 private String createTelUrl(String number) {
1757 if (TextUtils.isEmpty(number)) {
1758 return null;
1759 }
1760
Jake Hambye994d462014-02-03 13:10:13 -08001761 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001762 }
1763
Ihab Awadf9e92732013-12-05 18:02:52 -08001764 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001765 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1766 }
1767
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001768 private static void logv(String msg) {
1769 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1770 }
1771
Ihab Awadf9e92732013-12-05 18:02:52 -08001772 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001773 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1774 }
1775
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001776 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001777 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001778 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001779 }
1780
Sanket Padawe356d7632015-06-22 14:03:32 -07001781 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001782 public int getActivePhoneTypeForSlot(int slotId) {
1783 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001784 if (phone == null) {
1785 return PhoneConstants.PHONE_TYPE_NONE;
1786 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001787 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001788 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001789 }
1790
1791 /**
1792 * Returns the CDMA ERI icon index to display
1793 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001794 @Override
1795 public int getCdmaEriIconIndex(String callingPackage) {
1796 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001797 }
1798
Sanket Padawe356d7632015-06-22 14:03:32 -07001799 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001800 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1801 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1802 return -1;
1803 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001804 final Phone phone = getPhone(subId);
1805 if (phone != null) {
1806 return phone.getCdmaEriIconIndex();
1807 } else {
1808 return -1;
1809 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001810 }
1811
1812 /**
1813 * Returns the CDMA ERI icon mode,
1814 * 0 - ON
1815 * 1 - FLASHING
1816 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001817 @Override
1818 public int getCdmaEriIconMode(String callingPackage) {
1819 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001820 }
1821
Sanket Padawe356d7632015-06-22 14:03:32 -07001822 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001823 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1824 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1825 return -1;
1826 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001827 final Phone phone = getPhone(subId);
1828 if (phone != null) {
1829 return phone.getCdmaEriIconMode();
1830 } else {
1831 return -1;
1832 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001833 }
1834
1835 /**
1836 * Returns the CDMA ERI text,
1837 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001838 @Override
1839 public String getCdmaEriText(String callingPackage) {
1840 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001841 }
1842
Sanket Padawe356d7632015-06-22 14:03:32 -07001843 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001844 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1845 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1846 return null;
1847 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001848 final Phone phone = getPhone(subId);
1849 if (phone != null) {
1850 return phone.getCdmaEriText();
1851 } else {
1852 return null;
1853 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001854 }
1855
1856 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001857 * Returns the CDMA MDN.
1858 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001859 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001860 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001861 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001862 final Phone phone = getPhone(subId);
1863 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1864 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001865 } else {
1866 return null;
1867 }
1868 }
1869
1870 /**
1871 * Returns the CDMA MIN.
1872 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001873 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001874 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001875 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001876 final Phone phone = getPhone(subId);
1877 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1878 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001879 } else {
1880 return null;
1881 }
1882 }
1883
1884 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 * Returns true if CDMA provisioning needs to run.
1886 */
1887 public boolean needsOtaServiceProvisioning() {
1888 return mPhone.needsOtaServiceProvisioning();
1889 }
1890
1891 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001892 * Sets the voice mail number of a given subId.
1893 */
1894 @Override
1895 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001896 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001897 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1898 new Pair<String, String>(alphaTag, number), new Integer(subId));
1899 return success;
1900 }
1901
Ta-wei Yen87c49842016-05-13 21:19:52 -07001902 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001903 public void enableVisualVoicemailSmsFilter(String callingPackage, int subId,
1904 VisualVoicemailSmsFilterSettings settings) {
1905 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001906 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001907 .enableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId,
1908 settings);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001909 }
1910
1911 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001912 public void disableVisualVoicemailSmsFilter(String callingPackage, int subId) {
1913 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001914 VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001915 .disableVisualVoicemailSmsFilter(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001916 }
1917
1918 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001919 public VisualVoicemailSmsFilterSettings getVisualVoicemailSmsFilterSettings(
1920 String callingPackage, int subId) {
1921 mAppOps.checkPackage(Binder.getCallingUid(), callingPackage);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001922 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001923 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), callingPackage, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001924 }
1925
1926 @Override
Ta-wei Yenb6929602016-05-24 15:48:27 -07001927 public VisualVoicemailSmsFilterSettings getSystemVisualVoicemailSmsFilterSettings(
1928 String packageName, int subId) {
1929 enforceReadPrivilegedPermission();
Ta-wei Yen87c49842016-05-13 21:19:52 -07001930 return VisualVoicemailSmsFilterConfig
Ta-wei Yenb6929602016-05-24 15:48:27 -07001931 .getVisualVoicemailSmsFilterSettings(mPhone.getContext(), packageName, subId);
Ta-wei Yen87c49842016-05-13 21:19:52 -07001932 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001933 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001934 * Returns the unread count of voicemails
1935 */
1936 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001937 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001938 }
1939
1940 /**
1941 * Returns the unread count of voicemails for a subId
1942 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001943 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001944 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001945 final Phone phone = getPhone(subId);
1946 if (phone != null) {
1947 return phone.getVoiceMessageCount();
1948 } else {
1949 return 0;
1950 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001951 }
1952
1953 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001954 * Returns the data network type.
1955 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001956 *
1957 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1958 */
1959 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001960 public int getNetworkType() {
1961 final Phone phone = getPhone(getDefaultSubscription());
1962 if (phone != null) {
1963 return phone.getServiceState().getDataNetworkType();
1964 } else {
1965 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1966 }
Wink Saville36469e72014-06-11 15:17:00 -07001967 }
1968
1969 /**
1970 * Returns the network type for a subId
1971 */
1972 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001973 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1974 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1975 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1976 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001977
Sanket Padawe356d7632015-06-22 14:03:32 -07001978 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001979 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001980 return phone.getServiceState().getDataNetworkType();
1981 } else {
1982 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1983 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001984 }
1985
1986 /**
1987 * Returns the data network type
1988 */
1989 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001990 public int getDataNetworkType(String callingPackage) {
1991 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001992 }
1993
1994 /**
1995 * Returns the data network type for a subId
1996 */
1997 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001998 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1999 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2000 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2001 }
2002
Sanket Padawe356d7632015-06-22 14:03:32 -07002003 final Phone phone = getPhone(subId);
2004 if (phone != null) {
2005 return phone.getServiceState().getDataNetworkType();
2006 } else {
2007 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2008 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002009 }
2010
2011 /**
Wink Saville36469e72014-06-11 15:17:00 -07002012 * Returns the Voice network type for a subId
2013 */
2014 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07002015 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
2016 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
2017 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2018 }
2019
Sanket Padawe356d7632015-06-22 14:03:32 -07002020 final Phone phone = getPhone(subId);
2021 if (phone != null) {
2022 return phone.getServiceState().getVoiceNetworkType();
2023 } else {
2024 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
2025 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002026 }
2027
2028 /**
2029 * @return true if a ICC card is present
2030 */
2031 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07002032 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002033 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07002034 }
2035
2036 /**
2037 * @return true if a ICC card is present for a slotId
2038 */
Sanket Padawe356d7632015-06-22 14:03:32 -07002039 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07002040 public boolean hasIccCardUsingSlotId(int slotId) {
fionaxu6e6c68b2016-06-23 13:31:32 -07002041 final Phone phone = PhoneFactory.getPhone(slotId);
2042 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002043 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08002044 } else {
2045 return false;
2046 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002047 }
2048
2049 /**
2050 * Return if the current radio is LTE on CDMA. This
2051 * is a tri-state return value as for a period of time
2052 * the mode may be unknown.
2053 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002054 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002055 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002056 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002057 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002058 @Override
2059 public int getLteOnCdmaMode(String callingPackage) {
2060 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002061 }
2062
Sanket Padawe356d7632015-06-22 14:03:32 -07002063 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002064 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2065 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2066 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2067 }
2068
Sanket Padawe356d7632015-06-22 14:03:32 -07002069 final Phone phone = getPhone(subId);
2070 if (phone == null) {
2071 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2072 } else {
2073 return phone.getLteOnCdmaMode();
2074 }
Wink Saville36469e72014-06-11 15:17:00 -07002075 }
2076
2077 public void setPhone(Phone phone) {
2078 mPhone = phone;
2079 }
2080
2081 /**
2082 * {@hide}
2083 * Returns Default subId, 0 in the case of single standby.
2084 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002085 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002086 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002087 }
2088
Shishir Agrawala9f32182016-04-12 12:00:16 -07002089 private int getSlotForDefaultSubscription() {
2090 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2091 }
2092
Wink Savilleb564aae2014-10-23 10:18:09 -07002093 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002094 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002095 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002096
2097 /**
2098 * @see android.telephony.TelephonyManager.WifiCallingChoices
2099 */
2100 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002101 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2102 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002103 }
2104
2105 /**
2106 * @see android.telephony.TelephonyManager.WifiCallingChoices
2107 */
2108 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002109 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2110 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2111 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002112 }
2113
Sailesh Nepald1e68152013-12-12 19:08:02 -08002114 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002115 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002116 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002117 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002118
Shishir Agrawal566b7612013-10-28 14:41:00 -07002119 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002120 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2121 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002122
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002123 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002124 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002125 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002126 if (DBG) log("iccOpenLogicalChannel: " + response);
2127 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002128 }
2129
2130 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002131 public boolean iccCloseLogicalChannel(int subId, int channel) {
2132 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002133
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002134 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002135 if (channel < 0) {
2136 return false;
2137 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002138 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002139 if (DBG) log("iccCloseLogicalChannel: " + success);
2140 return success;
2141 }
2142
2143 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002144 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002145 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002146 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002147
2148 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002149 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2150 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002151 " data=" + data);
2152 }
2153
2154 if (channel < 0) {
2155 return "";
2156 }
2157
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002158 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002159 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002160 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2161
Shishir Agrawal566b7612013-10-28 14:41:00 -07002162 // Append the returned status code to the end of the response payload.
2163 String s = Integer.toHexString(
2164 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002165 if (response.payload != null) {
2166 s = IccUtils.bytesToHexString(response.payload) + s;
2167 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002168 return s;
2169 }
Jake Hambye994d462014-02-03 13:10:13 -08002170
Evan Charltonc66da362014-05-16 14:06:40 -07002171 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002172 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002173 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002174 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002175
2176 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002177 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2178 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002179 }
2180
2181 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002182 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002183 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2184
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002185 // Append the returned status code to the end of the response payload.
2186 String s = Integer.toHexString(
2187 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002188 if (response.payload != null) {
2189 s = IccUtils.bytesToHexString(response.payload) + s;
2190 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002191 return s;
2192 }
2193
2194 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002195 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002196 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002197 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002198
2199 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002200 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002201 p1 + " " + p2 + " " + p3 + ":" + filePath);
2202 }
2203
2204 IccIoResult response =
2205 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002206 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2207 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002208
2209 if (DBG) {
2210 log("Exchange SIM_IO [R]" + response);
2211 }
2212
2213 byte[] result = null;
2214 int length = 2;
2215 if (response.payload != null) {
2216 length = 2 + response.payload.length;
2217 result = new byte[length];
2218 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2219 } else {
2220 result = new byte[length];
2221 }
2222
2223 result[length - 1] = (byte) response.sw2;
2224 result[length - 2] = (byte) response.sw1;
2225 return result;
2226 }
2227
2228 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002229 public String sendEnvelopeWithStatus(int subId, String content) {
2230 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002231
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002232 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002233 if (response.payload == null) {
2234 return "";
2235 }
2236
2237 // Append the returned status code to the end of the response payload.
2238 String s = Integer.toHexString(
2239 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2240 s = IccUtils.bytesToHexString(response.payload) + s;
2241 return s;
2242 }
2243
Jake Hambye994d462014-02-03 13:10:13 -08002244 /**
2245 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2246 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2247 *
2248 * @param itemID the ID of the item to read
2249 * @return the NV item as a String, or null on error.
2250 */
2251 @Override
2252 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002253 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002254 if (DBG) log("nvReadItem: item " + itemID);
2255 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2256 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2257 return value;
2258 }
2259
2260 /**
2261 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2262 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2263 *
2264 * @param itemID the ID of the item to read
2265 * @param itemValue the value to write, as a String
2266 * @return true on success; false on any failure
2267 */
2268 @Override
2269 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002270 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002271 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2272 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2273 new Pair<Integer, String>(itemID, itemValue));
2274 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2275 return success;
2276 }
2277
2278 /**
2279 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2280 * Used for device configuration by some CDMA operators.
2281 *
2282 * @param preferredRoamingList byte array containing the new PRL
2283 * @return true on success; false on any failure
2284 */
2285 @Override
2286 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002287 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002288 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2289 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2290 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2291 return success;
2292 }
2293
2294 /**
2295 * Perform the specified type of NV config reset.
2296 * Used for device configuration by some CDMA operators.
2297 *
2298 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2299 * @return true on success; false on any failure
2300 */
2301 @Override
2302 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002303 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002304 if (DBG) log("nvResetConfig: type " + resetType);
2305 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2306 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2307 return success;
2308 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002309
2310 /**
Wink Saville36469e72014-06-11 15:17:00 -07002311 * {@hide}
2312 * Returns Default sim, 0 in the case of single standby.
2313 */
2314 public int getDefaultSim() {
2315 //TODO Need to get it from Telephony Devcontroller
2316 return 0;
2317 }
2318
Svet Ganovb320e182015-04-16 12:30:10 -07002319 public String[] getPcscfAddress(String apnType, String callingPackage) {
2320 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2321 return new String[0];
2322 }
2323
2324
ram87fca6f2014-07-18 18:58:44 +05302325 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002326 }
2327
2328 public void setImsRegistrationState(boolean registered) {
2329 enforceModifyPermission();
2330 mPhone.setImsRegistrationState(registered);
2331 }
2332
2333 /**
Stuart Scott54788802015-03-30 13:18:01 -07002334 * Set the network selection mode to automatic.
2335 *
2336 */
2337 @Override
2338 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002339 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002340 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2341 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2342 }
2343
2344 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002345 * Set the network selection mode to manual with the selected carrier.
2346 */
2347 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002348 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2349 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002350 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002351 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002352 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2353 persistSelection);
2354 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002355 }
2356
2357 /**
2358 * Scans for available networks.
2359 */
2360 @Override
2361 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002362 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002363 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2364 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2365 CMD_PERFORM_NETWORK_SCAN, null, subId);
2366 return result;
2367 }
2368
2369 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002370 * Get the calculated preferred network type.
2371 * Used for debugging incorrect network type.
2372 *
2373 * @return the preferred network type, defined in RILConstants.java.
2374 */
2375 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002376 public int getCalculatedPreferredNetworkType(String callingPackage) {
2377 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2378 return RILConstants.PREFERRED_NETWORK_MODE;
2379 }
2380
Amit Mahajan43330e02014-11-18 11:54:45 -08002381 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002382 }
2383
2384 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002385 * Get the preferred network type.
2386 * Used for device configuration by some CDMA operators.
2387 *
2388 * @return the preferred network type, defined in RILConstants.java.
2389 */
2390 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002391 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002392 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002393 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002394 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002395 int networkType = (result != null ? result[0] : -1);
2396 if (DBG) log("getPreferredNetworkType: " + networkType);
2397 return networkType;
2398 }
2399
2400 /**
2401 * Set the preferred network type.
2402 * Used for device configuration by some CDMA operators.
2403 *
2404 * @param networkType the preferred network type, defined in RILConstants.java.
2405 * @return true on success; false on any failure.
2406 */
2407 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002408 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002409 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002410 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2411 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002412 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002413 if (success) {
2414 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002415 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002416 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002417 return success;
2418 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002419
2420 /**
Junda Liu475951f2014-11-07 16:45:03 -08002421 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2422 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2423 * tethering.
2424 *
2425 * @return 0: Not required. 1: required. 2: Not set.
2426 * @hide
2427 */
2428 @Override
2429 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002430 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002431 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2432 Settings.Global.TETHER_DUN_REQUIRED, 2);
2433 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2434 // config_tether_apndata.
2435 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2436 dunRequired = 1;
2437 }
2438 return dunRequired;
2439 }
2440
2441 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002442 * Set mobile data enabled
2443 * Used by the user through settings etc to turn on/off mobile data
2444 *
2445 * @param enable {@code true} turn turn data on, else {@code false}
2446 */
2447 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002448 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002449 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002450 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002451 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002452 Phone phone = PhoneFactory.getPhone(phoneId);
2453 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002454 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002455 phone.setDataEnabled(enable);
2456 } else {
2457 loge("setDataEnabled: no phone for subId=" + subId);
2458 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002459 }
2460
2461 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002462 * Get whether mobile data is enabled.
2463 *
2464 * Note that this used to be available from ConnectivityService, gated by
2465 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2466 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002467 *
2468 * @return {@code true} if data is enabled else {@code false}
2469 */
2470 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002471 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002472 try {
2473 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2474 null);
2475 } catch (Exception e) {
2476 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2477 null);
2478 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002479 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002480 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002481 Phone phone = PhoneFactory.getPhone(phoneId);
2482 if (phone != null) {
2483 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002484 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002485 return retVal;
2486 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002487 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002488 return false;
2489 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002490 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002491
2492 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002493 public int getCarrierPrivilegeStatus(int subId) {
2494 final Phone phone = getPhone(subId);
2495 if (phone == null) {
2496 loge("getCarrierPrivilegeStatus: Invalid subId");
2497 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2498 }
2499 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002500 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002501 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002502 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2503 }
2504 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002505 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002506 }
Junda Liu29340342014-07-10 15:23:27 -07002507
2508 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002509 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002510 if (TextUtils.isEmpty(pkgName))
2511 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002512 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002513 if (card == null) {
2514 loge("checkCarrierPrivilegesForPackage: No UICC");
2515 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2516 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002517 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2518 }
2519
2520 @Override
2521 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002522 if (TextUtils.isEmpty(pkgName))
2523 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002524 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2525 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2526 UiccCard card = UiccController.getInstance().getUiccCard(i);
2527 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002528 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002529 continue;
2530 }
2531
2532 result = card.getCarrierPrivilegeStatus(
2533 mPhone.getContext().getPackageManager(), pkgName);
2534 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2535 break;
2536 }
2537 }
2538
2539 return result;
Junda Liu29340342014-07-10 15:23:27 -07002540 }
Derek Tan89e89d42014-07-08 17:00:10 -07002541
2542 @Override
Junda Liue64de782015-04-16 17:19:16 -07002543 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2544 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2545 loge("phoneId " + phoneId + " is not valid.");
2546 return null;
2547 }
2548 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002549 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002550 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002551 return null ;
2552 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002553 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002554 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002555 }
2556
Amith Yamasani6e118872016-02-19 12:53:51 -08002557 @Override
2558 public List<String> getPackagesWithCarrierPrivileges() {
2559 PackageManager pm = mPhone.getContext().getPackageManager();
2560 List<String> privilegedPackages = new ArrayList<>();
2561 List<PackageInfo> packages = null;
2562 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2563 UiccCard card = UiccController.getInstance().getUiccCard(i);
2564 if (card == null) {
2565 // No UICC in that slot.
2566 continue;
2567 }
2568 if (card.hasCarrierPrivilegeRules()) {
2569 if (packages == null) {
2570 // Only check packages in user 0 for now
2571 packages = pm.getInstalledPackagesAsUser(
2572 PackageManager.MATCH_DISABLED_COMPONENTS
2573 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2574 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2575 }
2576 for (int p = packages.size() - 1; p >= 0; p--) {
2577 PackageInfo pkgInfo = packages.get(p);
2578 if (pkgInfo != null && pkgInfo.packageName != null
2579 && card.getCarrierPrivilegeStatus(pkgInfo)
2580 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2581 privilegedPackages.add(pkgInfo.packageName);
2582 }
2583 }
2584 }
2585 }
2586 return privilegedPackages;
2587 }
2588
Wink Savilleb564aae2014-10-23 10:18:09 -07002589 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002590 final Phone phone = getPhone(subId);
2591 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002592 if (card == null) {
2593 loge("getIccId: No UICC");
2594 return null;
2595 }
2596 String iccId = card.getIccId();
2597 if (TextUtils.isEmpty(iccId)) {
2598 loge("getIccId: ICC ID is null or empty.");
2599 return null;
2600 }
2601 return iccId;
2602 }
2603
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002604 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002605 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2606 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002607 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002608
Jeff Sharkey85190e62014-12-05 09:40:12 -08002609 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002610 final Phone phone = getPhone(subId);
2611 if (phone == null) {
2612 return false;
2613 }
2614 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002615
2616 if (DBG_MERGE) {
2617 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2618 + subscriberId + " to " + number);
2619 }
2620
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002621 if (TextUtils.isEmpty(iccId)) {
2622 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002623 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002624
Jeff Sharkey85190e62014-12-05 09:40:12 -08002625 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2626
2627 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002628 if (alphaTag == null) {
2629 editor.remove(alphaTagPrefKey);
2630 } else {
2631 editor.putString(alphaTagPrefKey, alphaTag);
2632 }
2633
Jeff Sharkey85190e62014-12-05 09:40:12 -08002634 // Record both the line number and IMSI for this ICCID, since we need to
2635 // track all merged IMSIs based on line number
2636 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2637 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002638 if (number == null) {
2639 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002640 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002641 } else {
2642 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002643 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002644 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002645
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002646 editor.commit();
2647 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002648 }
2649
2650 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002651 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002652 // This is open to apps with WRITE_SMS.
2653 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002654 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002655 return null;
2656 }
Derek Tan97ebb422014-09-05 16:55:38 -07002657
2658 String iccId = getIccId(subId);
2659 if (iccId != null) {
2660 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002661 if (DBG_MERGE) {
2662 log("getLine1NumberForDisplay returning " +
2663 mTelephonySharedPreferences.getString(numberPrefKey, null));
2664 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002665 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002666 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002667 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002668 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002669 }
2670
2671 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002672 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2673 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2674 return null;
2675 }
Derek Tan97ebb422014-09-05 16:55:38 -07002676
2677 String iccId = getIccId(subId);
2678 if (iccId != null) {
2679 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002680 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002681 }
Derek Tan97ebb422014-09-05 16:55:38 -07002682 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002683 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002684
2685 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002686 public String[] getMergedSubscriberIds(String callingPackage) {
2687 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2688 return null;
2689 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002690 final Context context = mPhone.getContext();
2691 final TelephonyManager tele = TelephonyManager.from(context);
2692 final SubscriptionManager sub = SubscriptionManager.from(context);
2693
2694 // Figure out what subscribers are currently active
2695 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002696 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2697 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2698 final long identity = Binder.clearCallingIdentity();
2699 try {
2700 final int[] subIds = sub.getActiveSubscriptionIdList();
2701 for (int subId : subIds) {
2702 activeSubscriberIds.add(tele.getSubscriberId(subId));
2703 }
2704 } finally {
2705 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002706 }
2707
2708 // First pass, find a number override for an active subscriber
2709 String mergeNumber = null;
2710 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2711 for (String key : prefs.keySet()) {
2712 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2713 final String subscriberId = (String) prefs.get(key);
2714 if (activeSubscriberIds.contains(subscriberId)) {
2715 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2716 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2717 mergeNumber = (String) prefs.get(numberKey);
2718 if (DBG_MERGE) {
2719 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2720 + " for active subscriber " + subscriberId);
2721 }
2722 if (!TextUtils.isEmpty(mergeNumber)) {
2723 break;
2724 }
2725 }
2726 }
2727 }
2728
2729 // Shortcut when no active merged subscribers
2730 if (TextUtils.isEmpty(mergeNumber)) {
2731 return null;
2732 }
2733
2734 // Second pass, find all subscribers under that line override
2735 final ArraySet<String> result = new ArraySet<>();
2736 for (String key : prefs.keySet()) {
2737 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2738 final String number = (String) prefs.get(key);
2739 if (mergeNumber.equals(number)) {
2740 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2741 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2742 final String subscriberId = (String) prefs.get(subscriberKey);
2743 if (!TextUtils.isEmpty(subscriberId)) {
2744 result.add(subscriberId);
2745 }
2746 }
2747 }
2748 }
2749
2750 final String[] resultArray = result.toArray(new String[result.size()]);
2751 Arrays.sort(resultArray);
2752 if (DBG_MERGE) {
2753 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2754 }
2755 return resultArray;
2756 }
2757
2758 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002759 public boolean setOperatorBrandOverride(int subId, String brand) {
2760 enforceCarrierPrivilege(subId);
2761 final Phone phone = getPhone(subId);
2762 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002763 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002764
2765 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002766 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002767 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2768 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002769 enforceCarrierPrivilege(subId);
2770 final Phone phone = getPhone(subId);
2771 if (phone == null) {
2772 return false;
2773 }
2774 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002775 cdmaNonRoamingList);
2776 }
2777
2778 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002779 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2780 enforceModifyPermission();
2781
2782 int returnValue = 0;
2783 try {
2784 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2785 if(result.exception == null) {
2786 if (result.result != null) {
2787 byte[] responseData = (byte[])(result.result);
2788 if(responseData.length > oemResp.length) {
2789 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2790 responseData.length + "bytes. Buffer Size is " +
2791 oemResp.length + "bytes.");
2792 }
2793 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2794 returnValue = responseData.length;
2795 }
2796 } else {
2797 CommandException ex = (CommandException) result.exception;
2798 returnValue = ex.getCommandError().ordinal();
2799 if(returnValue > 0) returnValue *= -1;
2800 }
2801 } catch (RuntimeException e) {
2802 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2803 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2804 if(returnValue > 0) returnValue *= -1;
2805 }
2806
2807 return returnValue;
2808 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002809
2810 @Override
2811 public void setRadioCapability(RadioAccessFamily[] rafs) {
2812 try {
2813 ProxyController.getInstance().setRadioCapability(rafs);
2814 } catch (RuntimeException e) {
2815 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2816 }
2817 }
2818
2819 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002820 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2821 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2822 return RadioAccessFamily.RAF_UNKNOWN;
2823 }
2824
Wink Saville5d475dd2014-10-17 15:00:58 -07002825 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2826 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002827
2828 @Override
2829 public void enableVideoCalling(boolean enable) {
2830 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002831 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002832 }
2833
2834 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002835 public boolean isVideoCallingEnabled(String callingPackage) {
2836 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2837 return false;
2838 }
2839
Andrew Lee77527ac2014-10-21 16:57:39 -07002840 // Check the user preference and the system-level IMS setting. Even if the user has
2841 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2842 // In the long run, we may instead need to check if there exists a connection service
2843 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002844 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2845 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002846 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002847 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002848
Andrew Leea1239f22015-03-02 17:44:07 -08002849 @Override
2850 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002851 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002852 }
2853
2854 @Override
2855 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002856 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002857 }
2858
Andrew Lee9431b832015-03-09 18:46:45 -07002859 @Override
2860 public boolean isTtyModeSupported() {
2861 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2862 TelephonyManager telephonyManager =
2863 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
Wooki Wu1f82f7a2016-02-15 15:59:58 +08002864 return telecomManager.isTtySupported();
Andrew Lee9431b832015-03-09 18:46:45 -07002865 }
2866
2867 @Override
2868 public boolean isHearingAidCompatibilitySupported() {
2869 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2870 }
2871
Sanket Padawe7310cc72015-01-14 09:53:20 -08002872 /**
2873 * Returns the unique device ID of phone, for example, the IMEI for
2874 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2875 *
2876 * <p>Requires Permission:
2877 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2878 */
2879 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002880 public String getDeviceId(String callingPackage) {
2881 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2882 return null;
2883 }
2884
Sanket Padawe7310cc72015-01-14 09:53:20 -08002885 final Phone phone = PhoneFactory.getPhone(0);
2886 if (phone != null) {
2887 return phone.getDeviceId();
2888 } else {
2889 return null;
2890 }
2891 }
2892
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002893 /*
2894 * {@hide}
2895 * Returns the IMS Registration Status
2896 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002897 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002898 public boolean isImsRegistered() {
2899 return mPhone.isImsRegistered();
2900 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002901
2902 @Override
2903 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2904 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2905 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002906
Nathan Haroldc55097a2015-03-11 18:14:50 -07002907 /*
2908 * {@hide}
2909 * Returns the IMS Registration Status
2910 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002911 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002912 return mPhone.isWifiCallingEnabled();
2913 }
2914
2915 /*
2916 * {@hide}
2917 * Returns the IMS Registration Status
2918 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002919 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002920 return mPhone.isVolteEnabled();
2921 }
Svet Ganovb320e182015-04-16 12:30:10 -07002922
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002923 /*
2924 * {@hide} Returns the IMS Registration Status
2925 */
2926 public boolean isVideoTelephonyAvailable() {
2927 return mPhone.isVideoEnabled();
2928 }
2929
Svet Ganovb320e182015-04-16 12:30:10 -07002930 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002931 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002932 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002933 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2934
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002935 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002936 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002937 } catch (SecurityException e) {
2938 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2939 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002940 }
Svet Ganovb320e182015-04-16 12:30:10 -07002941
2942 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2943 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2944 return false;
2945 }
2946
2947 return true;
2948 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002949
Makoto Onukifee69342015-06-29 14:44:50 -07002950 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002951 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002952 */
2953 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002954 // Default SMS app can always read it.
2955 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2956 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2957 return true;
2958 }
2959 try {
2960 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002961 } catch (SecurityException readPhoneStateSecurityException) {
2962 try {
2963 // Can be read with READ_SMS too.
2964 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2965 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2966 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2967 } catch (SecurityException readSmsSecurityException) {
2968 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2969 // permissions
2970 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2971 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2972 " or " + android.Manifest.permission.READ_SMS + ".");
2973 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002974 }
Makoto Onukifee69342015-06-29 14:44:50 -07002975 }
2976
Stuart Scott8eef64f2015-04-08 15:13:54 -07002977 @Override
2978 public void factoryReset(int subId) {
2979 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002980 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2981 return;
2982 }
2983
Svet Ganovcc087f82015-05-12 20:35:54 -07002984 final long identity = Binder.clearCallingIdentity();
2985 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002986 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2987 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002988 // Enable data
2989 setDataEnabled(subId, true);
2990 // Set network selection mode to automatic
2991 setNetworkSelectionModeAutomatic(subId);
2992 // Set preferred mobile network type to the best available
2993 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2994 // Turn off roaming
2995 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2996 }
2997 } finally {
2998 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002999 }
3000 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003001
3002 @Override
3003 public String getLocaleFromDefaultSim() {
3004 // We query all subscriptions instead of just the active ones, because
3005 // this might be called early on in the provisioning flow when the
3006 // subscriptions potentially aren't active yet.
3007 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
3008 if (slist == null || slist.isEmpty()) {
3009 return null;
3010 }
3011
3012 // This function may be called very early, say, from the setup wizard, at
3013 // which point we won't have a default subscription set. If that's the case
3014 // we just choose the first, which will be valid in "most cases".
3015 final int defaultSubId = getDefaultSubscription();
3016 SubscriptionInfo info = null;
3017 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
3018 info = slist.get(0);
3019 } else {
3020 for (SubscriptionInfo item : slist) {
3021 if (item.getSubscriptionId() == defaultSubId) {
3022 info = item;
3023 break;
3024 }
3025 }
3026
3027 if (info == null) {
3028 return null;
3029 }
3030 }
3031
3032 // Try and fetch the locale from the carrier properties or from the SIM language
3033 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01003034 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003035 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01003036 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01003037 if (defaultPhone != null) {
3038 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
3039 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01003040 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01003041 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
3042 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01003043 } else {
3044 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01003045 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01003046 }
3047 }
3048
Narayan Kamath011676f2015-07-29 12:04:08 +01003049 // The SIM language preferences only store a language (e.g. fr = French), not an
3050 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3051 // the SIM and carrier preferences does not include a country we add the country
3052 // determined from the SIM MCC to provide an exact locale.
3053 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003054 if (mccLocale != null) {
3055 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3056 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003057 }
3058
Tony Hill183b2de2015-06-24 14:53:58 +01003059 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003060 return null;
3061 }
3062
3063 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3064 final long identity = Binder.clearCallingIdentity();
3065 try {
3066 return mSubscriptionController.getAllSubInfoList(
3067 mPhone.getContext().getOpPackageName());
3068 } finally {
3069 Binder.restoreCallingIdentity(identity);
3070 }
3071 }
3072
3073 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3074 final long identity = Binder.clearCallingIdentity();
3075 try {
3076 return mSubscriptionController.getActiveSubscriptionInfoList(
3077 mPhone.getContext().getOpPackageName());
3078 } finally {
3079 Binder.restoreCallingIdentity(identity);
3080 }
3081 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003082
3083 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003084 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3085 * representing the state of the modem.
3086 *
3087 * NOTE: This clears the modem state, so there should only every be one caller.
3088 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003089 */
3090 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003091 public void requestModemActivityInfo(ResultReceiver result) {
3092 enforceModifyPermission();
3093
3094 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3095 Bundle bundle = new Bundle();
3096 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3097 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003098 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003099
3100 /**
3101 * {@hide}
3102 * Returns the service state information on specified subscription.
3103 */
3104 @Override
3105 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3106
3107 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3108 return null;
3109 }
3110
3111 final Phone phone = getPhone(subId);
3112 if (phone == null) {
3113 return null;
3114 }
3115
3116 return phone.getServiceState();
3117 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003118
3119 /**
3120 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3121 *
3122 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3123 * voicemail ringtone.
3124 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3125 * PhoneAccount.
3126 */
3127 @Override
3128 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3129 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3130 if (phone == null) {
3131 return null;
3132 }
3133
3134 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3135 }
3136
3137 /**
3138 * Returns whether vibration is set for voicemail notification in Phone settings.
3139 *
3140 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3141 * voicemail vibration setting.
3142 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3143 */
3144 @Override
3145 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3146 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3147 if (phone == null) {
3148 return false;
3149 }
3150
3151 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3152 }
3153
Youhan Wange64578a2016-05-02 15:32:42 -07003154 /**
3155 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3156 *
3157 * @throws SecurityException if the caller does not have the required permission
3158 */
3159 private void enforceReadPrivilegedPermission() {
3160 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3161 null);
3162 }
3163
3164 /**
3165 * Return the application ID for the app type.
3166 *
3167 * @param subId the subscription ID that this request applies to.
3168 * @param appType the uicc app type.
3169 * @return Application ID for specificied app type, or null if no uicc.
3170 */
3171 @Override
3172 public String getAidForAppType(int subId, int appType) {
3173 enforceReadPrivilegedPermission();
3174 Phone phone = getPhone(subId);
3175 if (phone == null) {
3176 return null;
3177 }
3178 String aid = null;
3179 try {
3180 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3181 .getApplicationByType(appType).getAid();
3182 } catch (Exception e) {
3183 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3184 }
3185 return aid;
3186 }
3187
Youhan Wang4001d252016-05-11 10:29:41 -07003188 /**
3189 * Return the Electronic Serial Number.
3190 *
3191 * @param subId the subscription ID that this request applies to.
3192 * @return ESN or null if error.
3193 */
3194 @Override
3195 public String getEsn(int subId) {
3196 enforceReadPrivilegedPermission();
3197 Phone phone = getPhone(subId);
3198 if (phone == null) {
3199 return null;
3200 }
3201 String esn = null;
3202 try {
3203 esn = phone.getEsn();
3204 } catch (Exception e) {
3205 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3206 }
3207 return esn;
3208 }
3209
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003210 /**
3211 * Get snapshot of Telephony histograms
3212 * @return List of Telephony histograms
3213 * @hide
3214 */
3215 @Override
3216 public List<TelephonyHistogram> getTelephonyHistograms() {
3217 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3218 return RIL.getTelephonyRILTimingHistograms();
3219 }
Meng Wang1a7c35a2016-05-05 20:56:15 -07003220
3221 /**
3222 * {@hide}
3223 * Set the allowed carrier list for slotId
3224 * Require system privileges. In the future we may add this to carrier APIs.
3225 *
3226 * @return The number of carriers set successfully, should match length of carriers
3227 */
3228 @Override
3229 public int setAllowedCarriers(int slotId, List<CarrierIdentifier> carriers) {
3230 enforceModifyPermission();
3231 int subId = SubscriptionManager.getSubId(slotId)[0];
3232 int[] retVal = (int[]) sendRequest(CMD_SET_ALLOWED_CARRIERS, carriers, subId);
3233 return retVal[0];
3234 }
3235
3236 /**
3237 * {@hide}
3238 * Get the allowed carrier list for slotId.
3239 * Require system privileges. In the future we may add this to carrier APIs.
3240 *
3241 * @return List of {@link android.service.telephony.CarrierIdentifier}; empty list
3242 * means all carriers are allowed.
3243 */
3244 @Override
3245 public List<CarrierIdentifier> getAllowedCarriers(int slotId) {
3246 enforceReadPrivilegedPermission();
3247 int subId = SubscriptionManager.getSubId(slotId)[0];
3248 return (List<CarrierIdentifier>) sendRequest(CMD_GET_ALLOWED_CARRIERS, null, subId);
3249 }
3250
fionaxu59545b42016-05-25 15:53:37 -07003251 /**
3252 * Action set from carrier signalling broadcast receivers to enable/disable metered apns
3253 * @param subId the subscription ID that this action applies to.
3254 * @param enabled control enable or disable metered apns.
3255 * {@hide}
3256 */
3257 @Override
3258 public void carrierActionSetMeteredApnsEnabled(int subId, boolean enabled) {
3259 enforceModifyPermission();
3260 final Phone phone = getPhone(subId);
3261 if (phone == null) {
3262 loge("carrierAction: SetMeteredApnsEnabled fails with invalid subId: " + subId);
3263 return;
3264 }
3265 try {
3266 phone.carrierActionSetMeteredApnsEnabled(enabled);
3267 } catch (Exception e) {
3268 Log.e(LOG_TAG, "carrierAction: SetMeteredApnsEnabled fails. Exception ex=" + e);
3269 }
3270 }
3271
3272 /**
3273 * Action set from carrier signalling broadcast receivers to enable/disable radio
3274 * @param subId the subscription ID that this action applies to.
3275 * @param enabled control enable or disable radio.
3276 * {@hide}
3277 */
3278 @Override
3279 public void carrierActionSetRadioEnabled(int subId, boolean enabled) {
3280 enforceModifyPermission();
3281 final Phone phone = getPhone(subId);
3282 if (phone == null) {
3283 loge("carrierAction: SetRadioEnabled fails with invalid sibId: " + subId);
3284 return;
3285 }
3286 try {
3287 phone.carrierActionSetRadioEnabled(enabled);
3288 } catch (Exception e) {
3289 Log.e(LOG_TAG, "carrierAction: SetRadioEnabled fails. Exception ex=" + e);
3290 }
3291 }
3292
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003293 /**
3294 * Called when "adb shell dumpsys phone" is invoked. Dump is also automatically invoked when a
3295 * bug report is being generated.
3296 */
3297 @Override
Ta-wei Yen99282e02016-06-21 18:19:35 -07003298 protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) {
3299 DumpsysHandler.dump(mPhone.getContext(), fd, writer, args);
Ta-wei Yenc236d6b2016-06-21 13:33:12 -07003300 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003301}