blob: 6ee70999007e5e26021447f72e8863af0e78898f [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
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070036import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080038import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080039import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070040import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070043import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080044import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070045import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080047import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080048import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080049import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070050import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080052import android.util.ArrayMap;
53import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080056import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070060import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070061import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080064import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010065import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070068import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070069import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070071import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080072import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080077import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Wink Saville36469e72014-06-11 15:17:00 -070079import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080082import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080083import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010084import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080085import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070086
87/**
88 * Implementation of the ITelephony interface.
89 */
Santos Cordon117fee72014-05-16 17:56:12 -070090public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070091 private static final String LOG_TAG = "PhoneInterfaceManager";
92 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
93 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080094 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070095
96 // Message codes used with mMainThreadHandler
97 private static final int CMD_HANDLE_PIN_MMI = 1;
98 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
99 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
100 private static final int CMD_ANSWER_RINGING_CALL = 4;
101 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700102 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
103 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700104 private static final int CMD_OPEN_CHANNEL = 9;
105 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
106 private static final int CMD_CLOSE_CHANNEL = 11;
107 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800108 private static final int CMD_NV_READ_ITEM = 13;
109 private static final int EVENT_NV_READ_ITEM_DONE = 14;
110 private static final int CMD_NV_WRITE_ITEM = 15;
111 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
112 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
113 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
114 private static final int CMD_NV_RESET_CONFIG = 19;
115 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800116 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
117 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
118 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
119 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800120 private static final int CMD_SEND_ENVELOPE = 25;
121 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700122 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
123 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
124 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
125 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
126 private static final int CMD_EXCHANGE_SIM_IO = 31;
127 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800128 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
129 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700130 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
131 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700132 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
133 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700134 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
135 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
136 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
137 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700138
139 /** The singleton instance. */
140 private static PhoneInterfaceManager sInstance;
141
Wink Saville3ab207e2014-11-20 13:07:20 -0800142 private PhoneGlobals mApp;
143 private Phone mPhone;
144 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700145 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800146 private AppOpsManager mAppOps;
147 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800148 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800149 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700150
Derek Tan97ebb422014-09-05 16:55:38 -0700151 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
152 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800153 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700154 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700155
156 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700157 * A request object to use for transmitting data to an ICC.
158 */
159 private static final class IccAPDUArgument {
160 public int channel, cla, command, p1, p2, p3;
161 public String data;
162
163 public IccAPDUArgument(int channel, int cla, int command,
164 int p1, int p2, int p3, String data) {
165 this.channel = channel;
166 this.cla = cla;
167 this.command = command;
168 this.p1 = p1;
169 this.p2 = p2;
170 this.p3 = p3;
171 this.data = data;
172 }
173 }
174
175 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700176 * A request object to use for transmitting data to an ICC.
177 */
178 private static final class ManualNetworkSelectionArgument {
179 public OperatorInfo operatorInfo;
180 public boolean persistSelection;
181
182 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
183 this.operatorInfo = operatorInfo;
184 this.persistSelection = persistSelection;
185 }
186 }
187
188 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700189 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
190 * request after sending. The main thread will notify the request when it is complete.
191 */
192 private static final class MainThreadRequest {
193 /** The argument to use for the request */
194 public Object argument;
195 /** The result of the request that is run on the main thread */
196 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800197 // The subscriber id that this request applies to. Defaults to
198 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
199 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
201 public MainThreadRequest(Object argument) {
202 this.argument = argument;
203 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800204
205 public MainThreadRequest(Object argument, Integer subId) {
206 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800207 if (subId != null) {
208 this.subId = subId;
209 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
212
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800213 private static final class IncomingThirdPartyCallArgs {
214 public final ComponentName component;
215 public final String callId;
216 public final String callerDisplayName;
217
218 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
219 String callerDisplayName) {
220 this.component = component;
221 this.callId = callId;
222 this.callerDisplayName = callerDisplayName;
223 }
224 }
225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 /**
227 * A handler that processes messages on the main thread in the phone process. Since many
228 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
229 * inbound binder threads to the main thread in the phone process. The Binder thread
230 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
231 * on, which will be notified when the operation completes and will contain the result of the
232 * request.
233 *
234 * <p>If a MainThreadRequest object is provided in the msg.obj field,
235 * note that request.result must be set to something non-null for the calling thread to
236 * unblock.
237 */
238 private final class MainThreadHandler extends Handler {
239 @Override
240 public void handleMessage(Message msg) {
241 MainThreadRequest request;
242 Message onCompleted;
243 AsyncResult ar;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800244 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700245 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246
247 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700248 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700250 final Phone phone = getPhoneFromRequest(request);
251 request.result = phone != null ?
252 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
253 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 // Wake up the requesting thread
255 synchronized (request) {
256 request.notifyAll();
257 }
258 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260
261 case CMD_HANDLE_NEIGHBORING_CELL:
262 request = (MainThreadRequest) msg.obj;
263 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
264 request);
265 mPhone.getNeighboringCids(onCompleted);
266 break;
267
268 case EVENT_NEIGHBORING_CELL_DONE:
269 ar = (AsyncResult) msg.obj;
270 request = (MainThreadRequest) ar.userObj;
271 if (ar.exception == null && ar.result != null) {
272 request.result = ar.result;
273 } else {
274 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800275 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 }
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282
283 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700284 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800285 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700286 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 break;
288
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 case CMD_END_CALL:
290 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800291 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700292 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700293 Phone phone = getPhone(end_subId);
294 if (phone == null) {
295 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
296 break;
297 }
298 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
300 // CDMA: If the user presses the Power button we treat it as
301 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700302 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
304 // GSM: End the call as per the Phone state
305 hungUp = PhoneUtils.hangup(mCM);
306 } else {
307 throw new IllegalStateException("Unexpected phone type: " + phoneType);
308 }
309 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
310 request.result = hungUp;
311 // Wake up the requesting thread
312 synchronized (request) {
313 request.notifyAll();
314 }
315 break;
316
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700317 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700319 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800320 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700321 if (uiccCard == null) {
322 loge("iccTransmitApduLogicalChannel: No UICC");
323 request.result = new IccIoResult(0x6F, 0, (byte[])null);
324 synchronized (request) {
325 request.notifyAll();
326 }
327 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
329 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700330 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700331 iccArgument.channel, iccArgument.cla, iccArgument.command,
332 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700333 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700334 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700335 break;
336
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700337 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700338 ar = (AsyncResult) msg.obj;
339 request = (MainThreadRequest) ar.userObj;
340 if (ar.exception == null && ar.result != null) {
341 request.result = ar.result;
342 } else {
343 request.result = new IccIoResult(0x6F, 0, (byte[])null);
344 if (ar.result == null) {
345 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800346 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800348 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700349 } else {
350 loge("iccTransmitApduLogicalChannel: Unknown exception");
351 }
352 }
353 synchronized (request) {
354 request.notifyAll();
355 }
356 break;
357
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700358 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
359 request = (MainThreadRequest) msg.obj;
360 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800361 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700362 if (uiccCard == null) {
363 loge("iccTransmitApduBasicChannel: No UICC");
364 request.result = new IccIoResult(0x6F, 0, (byte[])null);
365 synchronized (request) {
366 request.notifyAll();
367 }
368 } else {
369 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
370 request);
371 uiccCard.iccTransmitApduBasicChannel(
372 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
373 iccArgument.p3, iccArgument.data, onCompleted);
374 }
375 break;
376
377 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
378 ar = (AsyncResult) msg.obj;
379 request = (MainThreadRequest) ar.userObj;
380 if (ar.exception == null && ar.result != null) {
381 request.result = ar.result;
382 } else {
383 request.result = new IccIoResult(0x6F, 0, (byte[])null);
384 if (ar.result == null) {
385 loge("iccTransmitApduBasicChannel: Empty response");
386 } else if (ar.exception instanceof CommandException) {
387 loge("iccTransmitApduBasicChannel: CommandException: " +
388 ar.exception);
389 } else {
390 loge("iccTransmitApduBasicChannel: Unknown exception");
391 }
392 }
393 synchronized (request) {
394 request.notifyAll();
395 }
396 break;
397
398 case CMD_EXCHANGE_SIM_IO:
399 request = (MainThreadRequest) msg.obj;
400 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800401 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700402 if (uiccCard == null) {
403 loge("iccExchangeSimIO: No UICC");
404 request.result = new IccIoResult(0x6F, 0, (byte[])null);
405 synchronized (request) {
406 request.notifyAll();
407 }
408 } else {
409 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
410 request);
411 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
412 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
413 iccArgument.data, onCompleted);
414 }
415 break;
416
417 case EVENT_EXCHANGE_SIM_IO_DONE:
418 ar = (AsyncResult) msg.obj;
419 request = (MainThreadRequest) ar.userObj;
420 if (ar.exception == null && ar.result != null) {
421 request.result = ar.result;
422 } else {
423 request.result = new IccIoResult(0x6f, 0, (byte[])null);
424 }
425 synchronized (request) {
426 request.notifyAll();
427 }
428 break;
429
Derek Tan4d5e5c12014-02-04 11:54:58 -0800430 case CMD_SEND_ENVELOPE:
431 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800432 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700433 if (uiccCard == null) {
434 loge("sendEnvelopeWithStatus: No UICC");
435 request.result = new IccIoResult(0x6F, 0, (byte[])null);
436 synchronized (request) {
437 request.notifyAll();
438 }
439 } else {
440 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
441 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
442 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800443 break;
444
445 case EVENT_SEND_ENVELOPE_DONE:
446 ar = (AsyncResult) msg.obj;
447 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700448 if (ar.exception == null && ar.result != null) {
449 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800450 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700451 request.result = new IccIoResult(0x6F, 0, (byte[])null);
452 if (ar.result == null) {
453 loge("sendEnvelopeWithStatus: Empty response");
454 } else if (ar.exception instanceof CommandException) {
455 loge("sendEnvelopeWithStatus: CommandException: " +
456 ar.exception);
457 } else {
458 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
459 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800460 }
461 synchronized (request) {
462 request.notifyAll();
463 }
464 break;
465
Shishir Agrawal566b7612013-10-28 14:41:00 -0700466 case CMD_OPEN_CHANNEL:
467 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800468 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700469 if (uiccCard == null) {
470 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800471 request.result = new IccOpenLogicalChannelResponse(-1,
472 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700473 synchronized (request) {
474 request.notifyAll();
475 }
476 } else {
477 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
478 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
479 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700480 break;
481
482 case EVENT_OPEN_CHANNEL_DONE:
483 ar = (AsyncResult) msg.obj;
484 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700485 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700486 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700487 int[] result = (int[]) ar.result;
488 int channelId = result[0];
489 byte[] selectResponse = null;
490 if (result.length > 1) {
491 selectResponse = new byte[result.length - 1];
492 for (int i = 1; i < result.length; ++i) {
493 selectResponse[i - 1] = (byte) result[i];
494 }
495 }
496 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700497 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700498 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700499 if (ar.result == null) {
500 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700501 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700502 if (ar.exception != null) {
503 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
504 }
505
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700506 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700507 if (ar.exception instanceof CommandException) {
508 CommandException.Error error =
509 ((CommandException) (ar.exception)).getCommandError();
510 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700511 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700512 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700513 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700514 }
515 }
516 openChannelResp = new IccOpenLogicalChannelResponse(
517 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700518 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700519 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700520 synchronized (request) {
521 request.notifyAll();
522 }
523 break;
524
525 case CMD_CLOSE_CHANNEL:
526 request = (MainThreadRequest) msg.obj;
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800527 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700528 if (uiccCard == null) {
529 loge("iccCloseLogicalChannel: No UICC");
530 request.result = new IccIoResult(0x6F, 0, (byte[])null);
531 synchronized (request) {
532 request.notifyAll();
533 }
534 } else {
535 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
536 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
537 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700538 break;
539
540 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800541 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
542 break;
543
544 case CMD_NV_READ_ITEM:
545 request = (MainThreadRequest) msg.obj;
546 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
547 mPhone.nvReadItem((Integer) request.argument, onCompleted);
548 break;
549
550 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700551 ar = (AsyncResult) msg.obj;
552 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800553 if (ar.exception == null && ar.result != null) {
554 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800556 request.result = "";
557 if (ar.result == null) {
558 loge("nvReadItem: Empty response");
559 } else if (ar.exception instanceof CommandException) {
560 loge("nvReadItem: CommandException: " +
561 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700562 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800563 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700564 }
565 }
566 synchronized (request) {
567 request.notifyAll();
568 }
569 break;
570
Jake Hambye994d462014-02-03 13:10:13 -0800571 case CMD_NV_WRITE_ITEM:
572 request = (MainThreadRequest) msg.obj;
573 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
574 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
575 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
576 break;
577
578 case EVENT_NV_WRITE_ITEM_DONE:
579 handleNullReturnEvent(msg, "nvWriteItem");
580 break;
581
582 case CMD_NV_WRITE_CDMA_PRL:
583 request = (MainThreadRequest) msg.obj;
584 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
585 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
586 break;
587
588 case EVENT_NV_WRITE_CDMA_PRL_DONE:
589 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
590 break;
591
592 case CMD_NV_RESET_CONFIG:
593 request = (MainThreadRequest) msg.obj;
594 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
595 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
596 break;
597
598 case EVENT_NV_RESET_CONFIG_DONE:
599 handleNullReturnEvent(msg, "nvResetConfig");
600 break;
601
Jake Hamby7c27be32014-03-03 13:25:59 -0800602 case CMD_GET_PREFERRED_NETWORK_TYPE:
603 request = (MainThreadRequest) msg.obj;
604 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700605 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800606 break;
607
608 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
609 ar = (AsyncResult) msg.obj;
610 request = (MainThreadRequest) ar.userObj;
611 if (ar.exception == null && ar.result != null) {
612 request.result = ar.result; // Integer
613 } else {
614 request.result = -1;
615 if (ar.result == null) {
616 loge("getPreferredNetworkType: Empty response");
617 } else if (ar.exception instanceof CommandException) {
618 loge("getPreferredNetworkType: CommandException: " +
619 ar.exception);
620 } else {
621 loge("getPreferredNetworkType: Unknown exception");
622 }
623 }
624 synchronized (request) {
625 request.notifyAll();
626 }
627 break;
628
629 case CMD_SET_PREFERRED_NETWORK_TYPE:
630 request = (MainThreadRequest) msg.obj;
631 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
632 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700633 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800634 break;
635
636 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
637 handleNullReturnEvent(msg, "setPreferredNetworkType");
638 break;
639
Steven Liu4bf01bc2014-07-17 11:05:29 -0500640 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
641 request = (MainThreadRequest)msg.obj;
642 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
643 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
644 break;
645
646 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
647 ar = (AsyncResult)msg.obj;
648 request = (MainThreadRequest)ar.userObj;
649 request.result = ar;
650 synchronized (request) {
651 request.notifyAll();
652 }
653 break;
654
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800655 case CMD_SET_VOICEMAIL_NUMBER:
656 request = (MainThreadRequest) msg.obj;
657 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
658 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800659 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
660 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800661 break;
662
663 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
664 handleNullReturnEvent(msg, "setVoicemailNumber");
665 break;
666
Stuart Scott54788802015-03-30 13:18:01 -0700667 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
668 request = (MainThreadRequest) msg.obj;
669 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
670 request);
671 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
672 break;
673
674 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
675 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
676 break;
677
Shishir Agrawal302c8692015-06-19 13:49:39 -0700678 case CMD_PERFORM_NETWORK_SCAN:
679 request = (MainThreadRequest) msg.obj;
680 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
681 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
682 break;
683
684 case EVENT_PERFORM_NETWORK_SCAN_DONE:
685 ar = (AsyncResult) msg.obj;
686 request = (MainThreadRequest) ar.userObj;
687 CellNetworkScanResult cellScanResult;
688 if (ar.exception == null && ar.result != null) {
689 cellScanResult = new CellNetworkScanResult(
690 CellNetworkScanResult.STATUS_SUCCESS,
691 (List<OperatorInfo>) ar.result);
692 } else {
693 if (ar.result == null) {
694 loge("getCellNetworkScanResults: Empty response");
695 }
696 if (ar.exception != null) {
697 loge("getCellNetworkScanResults: Exception: " + ar.exception);
698 }
699 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
700 if (ar.exception instanceof CommandException) {
701 CommandException.Error error =
702 ((CommandException) (ar.exception)).getCommandError();
703 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
704 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
705 } else if (error == CommandException.Error.GENERIC_FAILURE) {
706 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
707 }
708 }
709 cellScanResult = new CellNetworkScanResult(errorCode, null);
710 }
711 request.result = cellScanResult;
712 synchronized (request) {
713 request.notifyAll();
714 }
715 break;
716
717 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
718 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700719 ManualNetworkSelectionArgument selArg =
720 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700721 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
722 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700723 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
724 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700725 break;
726
727 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
728 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
729 break;
730
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700731 case CMD_GET_MODEM_ACTIVITY_INFO:
732 request = (MainThreadRequest) msg.obj;
733 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700734 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700735 break;
736
737 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
738 ar = (AsyncResult) msg.obj;
739 request = (MainThreadRequest) ar.userObj;
740 if (ar.exception == null && ar.result != null) {
741 request.result = ar.result;
742 } else {
743 if (ar.result == null) {
744 loge("queryModemActivityInfo: Empty response");
745 } else if (ar.exception instanceof CommandException) {
746 loge("queryModemActivityInfo: CommandException: " +
747 ar.exception);
748 } else {
749 loge("queryModemActivityInfo: Unknown exception");
750 }
751 }
Amit Mahajand4766222016-01-28 15:28:28 -0800752 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
753 if (request.result == null) {
754 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
755 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700756 synchronized (request) {
757 request.notifyAll();
758 }
759 break;
760
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700761 default:
762 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
763 break;
764 }
765 }
Jake Hambye994d462014-02-03 13:10:13 -0800766
767 private void handleNullReturnEvent(Message msg, String command) {
768 AsyncResult ar = (AsyncResult) msg.obj;
769 MainThreadRequest request = (MainThreadRequest) ar.userObj;
770 if (ar.exception == null) {
771 request.result = true;
772 } else {
773 request.result = false;
774 if (ar.exception instanceof CommandException) {
775 loge(command + ": CommandException: " + ar.exception);
776 } else {
777 loge(command + ": Unknown exception");
778 }
779 }
780 synchronized (request) {
781 request.notifyAll();
782 }
783 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700784 }
785
786 /**
787 * Posts the specified command to be executed on the main thread,
788 * waits for the request to complete, and returns the result.
789 * @see #sendRequestAsync
790 */
791 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800792 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700793 }
794
795 /**
796 * Posts the specified command to be executed on the main thread,
797 * waits for the request to complete, and returns the result.
798 * @see #sendRequestAsync
799 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800800 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
802 throw new RuntimeException("This method will deadlock if called from the main thread.");
803 }
804
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800805 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700806 Message msg = mMainThreadHandler.obtainMessage(command, request);
807 msg.sendToTarget();
808
809 // Wait for the request to complete
810 synchronized (request) {
811 while (request.result == null) {
812 try {
813 request.wait();
814 } catch (InterruptedException e) {
815 // Do nothing, go back and wait until the request is complete
816 }
817 }
818 }
819 return request.result;
820 }
821
822 /**
823 * Asynchronous ("fire and forget") version of sendRequest():
824 * Posts the specified command to be executed on the main thread, and
825 * returns immediately.
826 * @see #sendRequest
827 */
828 private void sendRequestAsync(int command) {
829 mMainThreadHandler.sendEmptyMessage(command);
830 }
831
832 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700833 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
834 * @see {@link #sendRequest(int,Object)}
835 */
836 private void sendRequestAsync(int command, Object argument) {
837 MainThreadRequest request = new MainThreadRequest(argument);
838 Message msg = mMainThreadHandler.obtainMessage(command, request);
839 msg.sendToTarget();
840 }
841
842 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 * Initialize the singleton PhoneInterfaceManager instance.
844 * This is only done once, at startup, from PhoneApp.onCreate().
845 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700846 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700847 synchronized (PhoneInterfaceManager.class) {
848 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700849 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850 } else {
851 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
852 }
853 return sInstance;
854 }
855 }
856
857 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700858 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 mApp = app;
860 mPhone = phone;
861 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700862 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
864 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700865 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700866 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800867 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800868
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 publish();
870 }
871
872 private void publish() {
873 if (DBG) log("publish: " + this);
874
875 ServiceManager.addService("phone", this);
876 }
877
Stuart Scott584921c2015-01-15 17:10:34 -0800878 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800879 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
880 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800881 }
882
Shishir Agrawal61ff83b2016-02-19 10:41:00 -0800883 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
884 Phone phone = getPhoneFromRequest(request);
885 return phone == null ? null :
886 UiccController.getInstance().getUiccCard(phone.getPhoneId());
887 }
888
Wink Saville36469e72014-06-11 15:17:00 -0700889 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700890 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800891 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700892 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 //
894 // Implementation of the ITelephony interface.
895 //
896
897 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700898 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700899 }
900
Wink Savilleb564aae2014-10-23 10:18:09 -0700901 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 if (DBG) log("dial: " + number);
903 // No permission check needed here: This is just a wrapper around the
904 // ACTION_DIAL intent, which is available to any app since it puts up
905 // the UI before it does anything.
906
907 String url = createTelUrl(number);
908 if (url == null) {
909 return;
910 }
911
912 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700913 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
915 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
916 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
917 mApp.startActivity(intent);
918 }
919 }
920
921 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700922 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700923 }
924
Wink Savilleb564aae2014-10-23 10:18:09 -0700925 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700926 if (DBG) log("call: " + number);
927
928 // This is just a wrapper around the ACTION_CALL intent, but we still
929 // need to do a permission check since we're calling startActivity()
930 // from the context of the phone app.
931 enforceCallPermission();
932
933 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
934 != AppOpsManager.MODE_ALLOWED) {
935 return;
936 }
937
938 String url = createTelUrl(number);
939 if (url == null) {
940 return;
941 }
942
Wink Saville08874612014-08-31 19:19:58 -0700943 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100944 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800945 if (slist != null) {
946 for (SubscriptionInfo subInfoRecord : slist) {
947 if (subInfoRecord.getSubscriptionId() == subId) {
948 isValid = true;
949 break;
950 }
Wink Saville08874612014-08-31 19:19:58 -0700951 }
952 }
953 if (isValid == false) {
954 return;
955 }
956
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700958 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700959 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
960 mApp.startActivity(intent);
961 }
962
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 /**
964 * End a call based on call state
965 * @return true is a call was ended
966 */
967 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700968 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700969 }
970
971 /**
972 * End a call based on the call state of the subId
973 * @return true is a call was ended
974 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700975 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700976 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800977 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 }
979
980 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700981 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700982 }
983
Wink Savilleb564aae2014-10-23 10:18:09 -0700984 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700985 if (DBG) log("answerRingingCall...");
986 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
987 // but that can probably wait till the big TelephonyManager API overhaul.
988 // For now, protect this call with the MODIFY_PHONE_STATE permission.
989 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800990 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 }
992
993 /**
994 * Make the actual telephony calls to implement answerRingingCall().
995 * This should only be called from the main thread of the Phone app.
996 * @see #answerRingingCall
997 *
998 * TODO: it would be nice to return true if we answered the call, or
999 * false if there wasn't actually a ringing incoming call, or some
1000 * other error occurred. (In other words, pass back the return value
1001 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1002 * But that would require calling this method via sendRequest() rather
1003 * than sendRequestAsync(), and right now we don't actually *need* that
1004 * return value, so let's just return void for now.
1005 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001006 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001007 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001008 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001009 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1010 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 if (hasActiveCall && hasHoldingCall) {
1012 // Both lines are in use!
1013 // TODO: provide a flag to let the caller specify what
1014 // policy to use if both lines are in use. (The current
1015 // behavior is hardwired to "answer incoming, end ongoing",
1016 // which is how the CALL button is specced to behave.)
1017 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1018 return;
1019 } else {
1020 // answerCall() will automatically hold the current active
1021 // call, if there is one.
1022 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1023 return;
1024 }
1025 } else {
1026 // No call was ringing.
1027 return;
1028 }
1029 }
1030
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001031 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001032 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001033 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001034 public void silenceRinger() {
1035 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001036 }
1037
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001038 @Override
1039 public boolean isOffhook(String callingPackage) {
1040 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001041 }
1042
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001043 @Override
1044 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1045 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1046 return false;
1047 }
1048
Sanket Padawe356d7632015-06-22 14:03:32 -07001049 final Phone phone = getPhone(subId);
1050 if (phone != null) {
1051 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1052 } else {
1053 return false;
1054 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001055 }
1056
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001057 @Override
1058 public boolean isRinging(String callingPackage) {
1059 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001060 }
1061
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001062 @Override
1063 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1064 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1065 return false;
1066 }
1067
Sanket Padawe356d7632015-06-22 14:03:32 -07001068 final Phone phone = getPhone(subId);
1069 if (phone != null) {
1070 return (phone.getState() == PhoneConstants.State.RINGING);
1071 } else {
1072 return false;
1073 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001074 }
1075
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001076 @Override
1077 public boolean isIdle(String callingPackage) {
1078 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001079 }
1080
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001081 @Override
1082 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1083 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1084 return false;
1085 }
1086
Sanket Padawe356d7632015-06-22 14:03:32 -07001087 final Phone phone = getPhone(subId);
1088 if (phone != null) {
1089 return (phone.getState() == PhoneConstants.State.IDLE);
1090 } else {
1091 return false;
1092 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001093 }
1094
Svet Ganovb320e182015-04-16 12:30:10 -07001095 public boolean isSimPinEnabled(String callingPackage) {
1096 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1097 return false;
1098 }
1099
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001100 return (PhoneGlobals.getInstance().isSimPinEnabled());
1101 }
1102
1103 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001104 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001105 }
1106
Wink Savilleb564aae2014-10-23 10:18:09 -07001107 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001108 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001109 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1110 }
1111
1112 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001113 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001114 }
1115
Wink Savilleb564aae2014-10-23 10:18:09 -07001116 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001117 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001118 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1119 }
1120
1121 /** {@hide} */
1122 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001123 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001124 }
1125
Wink Savilleb564aae2014-10-23 10:18:09 -07001126 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001128 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 checkSimPin.start();
1130 return checkSimPin.unlockSim(null, pin);
1131 }
1132
Wink Saville9de0f752013-10-22 19:04:03 -07001133 /** {@hide} */
1134 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001135 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001136 }
1137
Wink Savilleb564aae2014-10-23 10:18:09 -07001138 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001140 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001141 checkSimPuk.start();
1142 return checkSimPuk.unlockSim(puk, pin);
1143 }
1144
1145 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001146 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001147 * a synchronous one.
1148 */
1149 private static class UnlockSim extends Thread {
1150
1151 private final IccCard mSimCard;
1152
1153 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001154 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1155 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001156
1157 // For replies from SimCard interface
1158 private Handler mHandler;
1159
1160 // For async handler to identify request type
1161 private static final int SUPPLY_PIN_COMPLETE = 100;
1162
1163 public UnlockSim(IccCard simCard) {
1164 mSimCard = simCard;
1165 }
1166
1167 @Override
1168 public void run() {
1169 Looper.prepare();
1170 synchronized (UnlockSim.this) {
1171 mHandler = new Handler() {
1172 @Override
1173 public void handleMessage(Message msg) {
1174 AsyncResult ar = (AsyncResult) msg.obj;
1175 switch (msg.what) {
1176 case SUPPLY_PIN_COMPLETE:
1177 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1178 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001179 mRetryCount = msg.arg1;
1180 if (ar.exception != null) {
1181 if (ar.exception instanceof CommandException &&
1182 ((CommandException)(ar.exception)).getCommandError()
1183 == CommandException.Error.PASSWORD_INCORRECT) {
1184 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1185 } else {
1186 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1187 }
1188 } else {
1189 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1190 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191 mDone = true;
1192 UnlockSim.this.notifyAll();
1193 }
1194 break;
1195 }
1196 }
1197 };
1198 UnlockSim.this.notifyAll();
1199 }
1200 Looper.loop();
1201 }
1202
1203 /*
1204 * Use PIN or PUK to unlock SIM card
1205 *
1206 * If PUK is null, unlock SIM card with PIN
1207 *
1208 * If PUK is not null, unlock SIM card with PUK and set PIN code
1209 */
Wink Saville9de0f752013-10-22 19:04:03 -07001210 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001211
1212 while (mHandler == null) {
1213 try {
1214 wait();
1215 } catch (InterruptedException e) {
1216 Thread.currentThread().interrupt();
1217 }
1218 }
1219 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1220
1221 if (puk == null) {
1222 mSimCard.supplyPin(pin, callback);
1223 } else {
1224 mSimCard.supplyPuk(puk, pin, callback);
1225 }
1226
1227 while (!mDone) {
1228 try {
1229 Log.d(LOG_TAG, "wait for done");
1230 wait();
1231 } catch (InterruptedException e) {
1232 // Restore the interrupted status
1233 Thread.currentThread().interrupt();
1234 }
1235 }
1236 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001237 int[] resultArray = new int[2];
1238 resultArray[0] = mResult;
1239 resultArray[1] = mRetryCount;
1240 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001241 }
1242 }
1243
1244 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001245 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001246
1247 }
1248
Wink Savilleb564aae2014-10-23 10:18:09 -07001249 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001250 // No permission check needed here: this call is harmless, and it's
1251 // needed for the ServiceState.requestStateUpdate() call (which is
1252 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001253 final Phone phone = getPhone(subId);
1254 if (phone != null) {
1255 phone.updateServiceLocation();
1256 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001257 }
1258
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001259 @Override
1260 public boolean isRadioOn(String callingPackage) {
1261 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001262 }
1263
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001264 @Override
1265 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1266 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1267 return false;
1268 }
1269 return isRadioOnForSubscriber(subId);
1270 }
1271
1272 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001273 final Phone phone = getPhone(subId);
1274 if (phone != null) {
1275 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1276 } else {
1277 return false;
1278 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001279 }
1280
1281 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001282 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001283
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001284 }
Wink Saville36469e72014-06-11 15:17:00 -07001285
Wink Savilleb564aae2014-10-23 10:18:09 -07001286 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001288 final Phone phone = getPhone(subId);
1289 if (phone != null) {
1290 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1291 }
Wink Saville36469e72014-06-11 15:17:00 -07001292 }
1293
1294 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001295 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001296 }
1297
Wink Savilleb564aae2014-10-23 10:18:09 -07001298 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001299 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001300 final Phone phone = getPhone(subId);
1301 if (phone == null) {
1302 return false;
1303 }
1304 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001305 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001306 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001307 }
1308 return true;
1309 }
Wink Saville36469e72014-06-11 15:17:00 -07001310
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001311 public boolean needMobileRadioShutdown() {
1312 /*
1313 * If any of the Radios are available, it will need to be
1314 * shutdown. So return true if any Radio is available.
1315 */
1316 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1317 Phone phone = PhoneFactory.getPhone(i);
1318 if (phone != null && phone.isRadioAvailable()) return true;
1319 }
1320 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1321 return false;
1322 }
1323
1324 public void shutdownMobileRadios() {
1325 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1326 logv("Shutting down Phone " + i);
1327 shutdownRadioUsingPhoneId(i);
1328 }
1329 }
1330
1331 private void shutdownRadioUsingPhoneId(int phoneId) {
1332 enforceModifyPermission();
1333 Phone phone = PhoneFactory.getPhone(phoneId);
1334 if (phone != null && phone.isRadioAvailable()) {
1335 phone.shutdownRadio();
1336 }
1337 }
1338
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001340 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001341 }
1342
Wink Savilleb564aae2014-10-23 10:18:09 -07001343 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001345 final Phone phone = getPhone(subId);
1346 if (phone != null) {
1347 phone.setRadioPower(turnOn);
1348 return true;
1349 } else {
1350 return false;
1351 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 }
1353
Wink Saville36469e72014-06-11 15:17:00 -07001354 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001355 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001356 public boolean enableDataConnectivity() {
1357 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001358 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001359 final Phone phone = getPhone(subId);
1360 if (phone != null) {
1361 phone.setDataEnabled(true);
1362 return true;
1363 } else {
1364 return false;
1365 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 }
1367
Wink Saville36469e72014-06-11 15:17:00 -07001368 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001369 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001370 public boolean disableDataConnectivity() {
1371 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001372 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001373 final Phone phone = getPhone(subId);
1374 if (phone != null) {
1375 phone.setDataEnabled(false);
1376 return true;
1377 } else {
1378 return false;
1379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 }
1381
Wink Saville36469e72014-06-11 15:17:00 -07001382 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001383 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001384 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001385 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001386 final Phone phone = getPhone(subId);
1387 if (phone != null) {
1388 return phone.isDataConnectivityPossible();
1389 } else {
1390 return false;
1391 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 }
1393
1394 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001395 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001396 }
1397
Wink Savilleb564aae2014-10-23 10:18:09 -07001398 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001400 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1401 return false;
1402 }
Wink Saville36469e72014-06-11 15:17:00 -07001403 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 }
1405
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001407 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001408 }
1409
Wink Savilleb564aae2014-10-23 10:18:09 -07001410 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001411 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
Sanket Padawe356d7632015-06-22 14:03:32 -07001414 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001416 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001417 if (phone != null) {
1418 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1419 } else {
1420 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1421 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 }
1423
Sanket Padawe356d7632015-06-22 14:03:32 -07001424 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001426 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001427 if (phone != null) {
1428 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1429 } else {
1430 return TelephonyManager.DATA_ACTIVITY_NONE;
1431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433
1434 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001435 public Bundle getCellLocation(String callingPackage) {
1436 enforceFineOrCoarseLocationPermission("getCellLocation");
1437
1438 // OP_COARSE_LOCATION controls both fine and coarse location.
1439 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1440 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001441 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001442 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001445 if (checkIfCallerIsSelfOrForegroundUser() ||
1446 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 if (DBG_LOC) log("getCellLocation: is active user");
1448 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001449 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 if (phone == null) {
1451 return null;
1452 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001453 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 return data;
1455 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001456 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 return null;
1458 }
1459 }
1460
Svetoslav64fad262015-04-14 14:35:21 -07001461 private void enforceFineOrCoarseLocationPermission(String message) {
1462 try {
1463 mApp.enforceCallingOrSelfPermission(
1464 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1465 } catch (SecurityException e) {
1466 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1467 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1468 // is the weaker precondition
1469 mApp.enforceCallingOrSelfPermission(
1470 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1471 }
1472 }
1473
1474
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 @Override
1476 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001477 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001478 }
1479
Sanket Padawe356d7632015-06-22 14:03:32 -07001480 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001481 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 mApp.enforceCallingOrSelfPermission(
1483 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 final Phone phone = getPhone(subId);
1485 if (phone != null) {
1486 phone.enableLocationUpdates();
1487 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
1490 @Override
1491 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001492 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001493 }
1494
Sanket Padawe356d7632015-06-22 14:03:32 -07001495 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001496 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 mApp.enforceCallingOrSelfPermission(
1498 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001499 final Phone phone = getPhone(subId);
1500 if (phone != null) {
1501 phone.disableLocationUpdates();
1502 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 }
1504
1505 @Override
1506 @SuppressWarnings("unchecked")
1507 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001508 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1509
1510 // OP_COARSE_LOCATION controls both fine and coarse location.
1511 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1512 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1513 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 }
1515
1516 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1517 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1518 return null;
1519 }
Svetoslav64fad262015-04-14 14:35:21 -07001520
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001521 if (checkIfCallerIsSelfOrForegroundUser() ||
1522 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1524
1525 ArrayList<NeighboringCellInfo> cells = null;
1526
1527 try {
1528 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001529 CMD_HANDLE_NEIGHBORING_CELL, null,
1530 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001531 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001532 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 }
1534 return cells;
1535 } else {
1536 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1537 return null;
1538 }
1539 }
1540
1541
1542 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001543 public List<CellInfo> getAllCellInfo(String callingPackage) {
1544 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1545
1546 // OP_COARSE_LOCATION controls both fine and coarse location.
1547 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1548 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1549 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001552 if (checkIfCallerIsSelfOrForegroundUser() ||
1553 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001555 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1556 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001557 final List<CellInfo> info = phone.getAllCellInfo();
1558 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001559 }
1560 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 } else {
1562 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1563 return null;
1564 }
1565 }
1566
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001567 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 public void setCellInfoListRate(int rateInMillis) {
1569 mPhone.setCellInfoListRate(rateInMillis);
1570 }
1571
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001572 //
1573 // Internal helper methods.
1574 //
1575
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001576 /**
1577 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1578 */
1579 private boolean checkCallerInteractAcrossUsersFull() {
1580 return mPhone.getContext().checkCallingOrSelfPermission(
1581 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1582 == PackageManager.PERMISSION_GRANTED;
1583 }
1584
Jake Hambye994d462014-02-03 13:10:13 -08001585 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001586 boolean ok;
1587
1588 boolean self = Binder.getCallingUid() == Process.myUid();
1589 if (!self) {
1590 // Get the caller's user id then clear the calling identity
1591 // which will be restored in the finally clause.
1592 int callingUser = UserHandle.getCallingUserId();
1593 long ident = Binder.clearCallingIdentity();
1594
1595 try {
1596 // With calling identity cleared the current user is the foreground user.
1597 int foregroundUser = ActivityManager.getCurrentUser();
1598 ok = (foregroundUser == callingUser);
1599 if (DBG_LOC) {
1600 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1601 + " callingUser=" + callingUser + " ok=" + ok);
1602 }
1603 } catch (Exception ex) {
1604 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1605 ok = false;
1606 } finally {
1607 Binder.restoreCallingIdentity(ident);
1608 }
1609 } else {
1610 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1611 ok = true;
1612 }
1613 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1614 return ok;
1615 }
1616
1617 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001618 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1619 *
1620 * @throws SecurityException if the caller does not have the required permission
1621 */
1622 private void enforceModifyPermission() {
1623 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1624 }
1625
1626 /**
Junda Liua2e36012014-07-09 18:30:01 -07001627 * Make sure either system app or the caller has carrier privilege.
1628 *
1629 * @throws SecurityException if the caller does not have the required permission/privilege
1630 */
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001631 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001632 int permission = mApp.checkCallingOrSelfPermission(
1633 android.Manifest.permission.MODIFY_PHONE_STATE);
1634 if (permission == PackageManager.PERMISSION_GRANTED) {
1635 return;
1636 }
1637
1638 log("No modify permission, check carrier privilege next.");
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001639 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001640 }
1641
1642 /**
1643 * Make sure the caller has carrier privilege.
1644 *
1645 * @throws SecurityException if the caller does not have the required permission
1646 */
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001647 private void enforceCarrierPrivilege(int subId) {
1648 if (getCarrierPrivilegeStatus(subId) !=
1649 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001650 loge("No Carrier Privilege.");
1651 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001652 }
1653 }
1654
1655 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 * Make sure the caller has the CALL_PHONE permission.
1657 *
1658 * @throws SecurityException if the caller does not have the required permission
1659 */
1660 private void enforceCallPermission() {
1661 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1662 }
1663
Stuart Scott8eef64f2015-04-08 15:13:54 -07001664 private void enforceConnectivityInternalPermission() {
1665 mApp.enforceCallingOrSelfPermission(
1666 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1667 "ConnectivityService");
1668 }
1669
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001670 private String createTelUrl(String number) {
1671 if (TextUtils.isEmpty(number)) {
1672 return null;
1673 }
1674
Jake Hambye994d462014-02-03 13:10:13 -08001675 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001676 }
1677
Ihab Awadf9e92732013-12-05 18:02:52 -08001678 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001679 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1680 }
1681
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001682 private static void logv(String msg) {
1683 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1684 }
1685
Ihab Awadf9e92732013-12-05 18:02:52 -08001686 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001687 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1688 }
1689
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001690 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001691 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001692 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001693 }
1694
Sanket Padawe356d7632015-06-22 14:03:32 -07001695 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001696 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001697 final Phone phone = getPhone(subId);
1698 if (phone == null) {
1699 return PhoneConstants.PHONE_TYPE_NONE;
1700 } else {
1701 return getPhone(subId).getPhoneType();
1702 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001703 }
1704
1705 /**
1706 * Returns the CDMA ERI icon index to display
1707 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001708 @Override
1709 public int getCdmaEriIconIndex(String callingPackage) {
1710 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001711 }
1712
Sanket Padawe356d7632015-06-22 14:03:32 -07001713 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001714 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1715 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1716 return -1;
1717 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001718 final Phone phone = getPhone(subId);
1719 if (phone != null) {
1720 return phone.getCdmaEriIconIndex();
1721 } else {
1722 return -1;
1723 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 }
1725
1726 /**
1727 * Returns the CDMA ERI icon mode,
1728 * 0 - ON
1729 * 1 - FLASHING
1730 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001731 @Override
1732 public int getCdmaEriIconMode(String callingPackage) {
1733 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001734 }
1735
Sanket Padawe356d7632015-06-22 14:03:32 -07001736 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001737 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1738 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1739 return -1;
1740 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001741 final Phone phone = getPhone(subId);
1742 if (phone != null) {
1743 return phone.getCdmaEriIconMode();
1744 } else {
1745 return -1;
1746 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001747 }
1748
1749 /**
1750 * Returns the CDMA ERI text,
1751 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001752 @Override
1753 public String getCdmaEriText(String callingPackage) {
1754 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001755 }
1756
Sanket Padawe356d7632015-06-22 14:03:32 -07001757 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001758 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1759 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1760 return null;
1761 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001762 final Phone phone = getPhone(subId);
1763 if (phone != null) {
1764 return phone.getCdmaEriText();
1765 } else {
1766 return null;
1767 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001768 }
1769
1770 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001771 * Returns the CDMA MDN.
1772 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001773 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001774 public String getCdmaMdn(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001775 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001776 final Phone phone = getPhone(subId);
1777 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1778 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001779 } else {
1780 return null;
1781 }
1782 }
1783
1784 /**
1785 * Returns the CDMA MIN.
1786 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001787 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001788 public String getCdmaMin(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001789 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001790 final Phone phone = getPhone(subId);
1791 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1792 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001793 } else {
1794 return null;
1795 }
1796 }
1797
1798 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001799 * Returns true if CDMA provisioning needs to run.
1800 */
1801 public boolean needsOtaServiceProvisioning() {
1802 return mPhone.needsOtaServiceProvisioning();
1803 }
1804
1805 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001806 * Sets the voice mail number of a given subId.
1807 */
1808 @Override
1809 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08001810 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001811 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1812 new Pair<String, String>(alphaTag, number), new Integer(subId));
1813 return success;
1814 }
1815
1816 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 * Returns the unread count of voicemails
1818 */
1819 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001820 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001821 }
1822
1823 /**
1824 * Returns the unread count of voicemails for a subId
1825 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001826 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001827 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001828 final Phone phone = getPhone(subId);
1829 if (phone != null) {
1830 return phone.getVoiceMessageCount();
1831 } else {
1832 return 0;
1833 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001834 }
1835
1836 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001837 * Returns the data network type.
1838 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 *
1840 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1841 */
1842 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001843 public int getNetworkType() {
1844 final Phone phone = getPhone(getDefaultSubscription());
1845 if (phone != null) {
1846 return phone.getServiceState().getDataNetworkType();
1847 } else {
1848 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1849 }
Wink Saville36469e72014-06-11 15:17:00 -07001850 }
1851
1852 /**
1853 * Returns the network type for a subId
1854 */
1855 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001856 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1857 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1858 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1859 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001860
Sanket Padawe356d7632015-06-22 14:03:32 -07001861 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001862 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 return phone.getServiceState().getDataNetworkType();
1864 } else {
1865 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1866 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001867 }
1868
1869 /**
1870 * Returns the data network type
1871 */
1872 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001873 public int getDataNetworkType(String callingPackage) {
1874 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001875 }
1876
1877 /**
1878 * Returns the data network type for a subId
1879 */
1880 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001881 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1882 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1883 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1884 }
1885
Sanket Padawe356d7632015-06-22 14:03:32 -07001886 final Phone phone = getPhone(subId);
1887 if (phone != null) {
1888 return phone.getServiceState().getDataNetworkType();
1889 } else {
1890 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1891 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001892 }
1893
1894 /**
Wink Saville36469e72014-06-11 15:17:00 -07001895 * Returns the Voice network type for a subId
1896 */
1897 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001898 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1899 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1900 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1901 }
1902
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 final Phone phone = getPhone(subId);
1904 if (phone != null) {
1905 return phone.getServiceState().getVoiceNetworkType();
1906 } else {
1907 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1908 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 }
1910
1911 /**
1912 * @return true if a ICC card is present
1913 */
1914 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001915 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001916 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001917 }
1918
1919 /**
1920 * @return true if a ICC card is present for a slotId
1921 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001923 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001924 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 final Phone phone = getPhone(subId[0]);
1926 if (subId != null && phone != null) {
1927 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001928 } else {
1929 return false;
1930 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001931 }
1932
1933 /**
1934 * Return if the current radio is LTE on CDMA. This
1935 * is a tri-state return value as for a period of time
1936 * the mode may be unknown.
1937 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001938 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001939 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001940 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001941 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001942 @Override
1943 public int getLteOnCdmaMode(String callingPackage) {
1944 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001945 }
1946
Sanket Padawe356d7632015-06-22 14:03:32 -07001947 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001948 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1949 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1950 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1951 }
1952
Sanket Padawe356d7632015-06-22 14:03:32 -07001953 final Phone phone = getPhone(subId);
1954 if (phone == null) {
1955 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1956 } else {
1957 return phone.getLteOnCdmaMode();
1958 }
Wink Saville36469e72014-06-11 15:17:00 -07001959 }
1960
1961 public void setPhone(Phone phone) {
1962 mPhone = phone;
1963 }
1964
1965 /**
1966 * {@hide}
1967 * Returns Default subId, 0 in the case of single standby.
1968 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001969 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001970 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001971 }
1972
Wink Savilleb564aae2014-10-23 10:18:09 -07001973 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001974 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001975 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001976
1977 /**
1978 * @see android.telephony.TelephonyManager.WifiCallingChoices
1979 */
1980 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001981 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1982 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001983 }
1984
1985 /**
1986 * @see android.telephony.TelephonyManager.WifiCallingChoices
1987 */
1988 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001989 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1990 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1991 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001992 }
1993
Sailesh Nepald1e68152013-12-12 19:08:02 -08001994 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001995 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001996 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001997 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001998
Shishir Agrawal566b7612013-10-28 14:41:00 -07001999 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002000 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2001 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002002
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002003 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002004 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002005 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002006 if (DBG) log("iccOpenLogicalChannel: " + response);
2007 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002008 }
2009
2010 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002011 public boolean iccCloseLogicalChannel(int subId, int channel) {
2012 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002013
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002014 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002015 if (channel < 0) {
2016 return false;
2017 }
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002018 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002019 if (DBG) log("iccCloseLogicalChannel: " + success);
2020 return success;
2021 }
2022
2023 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002024 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002025 int command, int p1, int p2, int p3, String data) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002026 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002027
2028 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002029 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2030 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002031 " data=" + data);
2032 }
2033
2034 if (channel < 0) {
2035 return "";
2036 }
2037
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002038 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002039 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002040 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2041
Shishir Agrawal566b7612013-10-28 14:41:00 -07002042 // Append the returned status code to the end of the response payload.
2043 String s = Integer.toHexString(
2044 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002045 if (response.payload != null) {
2046 s = IccUtils.bytesToHexString(response.payload) + s;
2047 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002048 return s;
2049 }
Jake Hambye994d462014-02-03 13:10:13 -08002050
Evan Charltonc66da362014-05-16 14:06:40 -07002051 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002052 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002053 int p3, String data) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002054 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002055
2056 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002057 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2058 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002059 }
2060
2061 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002062 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002063 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2064
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002065 // Append the returned status code to the end of the response payload.
2066 String s = Integer.toHexString(
2067 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002068 if (response.payload != null) {
2069 s = IccUtils.bytesToHexString(response.payload) + s;
2070 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002071 return s;
2072 }
2073
2074 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002075 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002076 String filePath) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002077 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002078
2079 if (DBG) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002080 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002081 p1 + " " + p2 + " " + p3 + ":" + filePath);
2082 }
2083
2084 IccIoResult response =
2085 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002086 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2087 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002088
2089 if (DBG) {
2090 log("Exchange SIM_IO [R]" + response);
2091 }
2092
2093 byte[] result = null;
2094 int length = 2;
2095 if (response.payload != null) {
2096 length = 2 + response.payload.length;
2097 result = new byte[length];
2098 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2099 } else {
2100 result = new byte[length];
2101 }
2102
2103 result[length - 1] = (byte) response.sw2;
2104 result[length - 2] = (byte) response.sw1;
2105 return result;
2106 }
2107
2108 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002109 public String sendEnvelopeWithStatus(int subId, String content) {
2110 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002111
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002112 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002113 if (response.payload == null) {
2114 return "";
2115 }
2116
2117 // Append the returned status code to the end of the response payload.
2118 String s = Integer.toHexString(
2119 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2120 s = IccUtils.bytesToHexString(response.payload) + s;
2121 return s;
2122 }
2123
Jake Hambye994d462014-02-03 13:10:13 -08002124 /**
2125 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2126 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2127 *
2128 * @param itemID the ID of the item to read
2129 * @return the NV item as a String, or null on error.
2130 */
2131 @Override
2132 public String nvReadItem(int itemID) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002133 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002134 if (DBG) log("nvReadItem: item " + itemID);
2135 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2136 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2137 return value;
2138 }
2139
2140 /**
2141 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2142 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2143 *
2144 * @param itemID the ID of the item to read
2145 * @param itemValue the value to write, as a String
2146 * @return true on success; false on any failure
2147 */
2148 @Override
2149 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002150 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002151 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2152 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2153 new Pair<Integer, String>(itemID, itemValue));
2154 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2155 return success;
2156 }
2157
2158 /**
2159 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2160 * Used for device configuration by some CDMA operators.
2161 *
2162 * @param preferredRoamingList byte array containing the new PRL
2163 * @return true on success; false on any failure
2164 */
2165 @Override
2166 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002167 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002168 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2169 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2170 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2171 return success;
2172 }
2173
2174 /**
2175 * Perform the specified type of NV config reset.
2176 * Used for device configuration by some CDMA operators.
2177 *
2178 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2179 * @return true on success; false on any failure
2180 */
2181 @Override
2182 public boolean nvResetConfig(int resetType) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002183 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002184 if (DBG) log("nvResetConfig: type " + resetType);
2185 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2186 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2187 return success;
2188 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002189
2190 /**
Wink Saville36469e72014-06-11 15:17:00 -07002191 * {@hide}
2192 * Returns Default sim, 0 in the case of single standby.
2193 */
2194 public int getDefaultSim() {
2195 //TODO Need to get it from Telephony Devcontroller
2196 return 0;
2197 }
2198
Svet Ganovb320e182015-04-16 12:30:10 -07002199 public String[] getPcscfAddress(String apnType, String callingPackage) {
2200 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2201 return new String[0];
2202 }
2203
2204
ram87fca6f2014-07-18 18:58:44 +05302205 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002206 }
2207
2208 public void setImsRegistrationState(boolean registered) {
2209 enforceModifyPermission();
2210 mPhone.setImsRegistrationState(registered);
2211 }
2212
2213 /**
Stuart Scott54788802015-03-30 13:18:01 -07002214 * Set the network selection mode to automatic.
2215 *
2216 */
2217 @Override
2218 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002219 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002220 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2221 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2222 }
2223
2224 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002225 * Set the network selection mode to manual with the selected carrier.
2226 */
2227 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002228 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2229 boolean persistSelection) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002230 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002231 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002232 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2233 persistSelection);
2234 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002235 }
2236
2237 /**
2238 * Scans for available networks.
2239 */
2240 @Override
2241 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002242 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002243 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2244 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2245 CMD_PERFORM_NETWORK_SCAN, null, subId);
2246 return result;
2247 }
2248
2249 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002250 * Get the calculated preferred network type.
2251 * Used for debugging incorrect network type.
2252 *
2253 * @return the preferred network type, defined in RILConstants.java.
2254 */
2255 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002256 public int getCalculatedPreferredNetworkType(String callingPackage) {
2257 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2258 return RILConstants.PREFERRED_NETWORK_MODE;
2259 }
2260
Amit Mahajan43330e02014-11-18 11:54:45 -08002261 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002262 }
2263
2264 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002265 * Get the preferred network type.
2266 * Used for device configuration by some CDMA operators.
2267 *
2268 * @return the preferred network type, defined in RILConstants.java.
2269 */
2270 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002271 public int getPreferredNetworkType(int subId) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002272 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002273 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002274 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002275 int networkType = (result != null ? result[0] : -1);
2276 if (DBG) log("getPreferredNetworkType: " + networkType);
2277 return networkType;
2278 }
2279
2280 /**
2281 * Set the preferred network type.
2282 * Used for device configuration by some CDMA operators.
2283 *
2284 * @param networkType the preferred network type, defined in RILConstants.java.
2285 * @return true on success; false on any failure.
2286 */
2287 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002288 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002289 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002290 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2291 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002292 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002293 if (success) {
2294 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002295 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002296 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002297 return success;
2298 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002299
2300 /**
Junda Liu475951f2014-11-07 16:45:03 -08002301 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2302 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2303 * tethering.
2304 *
2305 * @return 0: Not required. 1: required. 2: Not set.
2306 * @hide
2307 */
2308 @Override
2309 public int getTetherApnRequired() {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002310 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002311 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2312 Settings.Global.TETHER_DUN_REQUIRED, 2);
2313 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2314 // config_tether_apndata.
2315 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2316 dunRequired = 1;
2317 }
2318 return dunRequired;
2319 }
2320
2321 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002322 * Set mobile data enabled
2323 * Used by the user through settings etc to turn on/off mobile data
2324 *
2325 * @param enable {@code true} turn turn data on, else {@code false}
2326 */
2327 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002328 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002329 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002330 int phoneId = mSubscriptionController.getPhoneId(subId);
2331 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2332 Phone phone = PhoneFactory.getPhone(phoneId);
2333 if (phone != null) {
2334 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2335 phone.setDataEnabled(enable);
2336 } else {
2337 loge("setDataEnabled: no phone for subId=" + subId);
2338 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002339 }
2340
2341 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002342 * Get whether mobile data is enabled.
2343 *
2344 * Note that this used to be available from ConnectivityService, gated by
2345 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2346 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002347 *
2348 * @return {@code true} if data is enabled else {@code false}
2349 */
2350 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002351 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002352 try {
2353 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2354 null);
2355 } catch (Exception e) {
2356 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2357 null);
2358 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002359 int phoneId = mSubscriptionController.getPhoneId(subId);
2360 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2361 Phone phone = PhoneFactory.getPhone(phoneId);
2362 if (phone != null) {
2363 boolean retVal = phone.getDataEnabled();
2364 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2365 return retVal;
2366 } else {
2367 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2368 return false;
2369 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002370 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002371
2372 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002373 public int getCarrierPrivilegeStatus(int subId) {
2374 final Phone phone = getPhone(subId);
2375 if (phone == null) {
2376 loge("getCarrierPrivilegeStatus: Invalid subId");
2377 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2378 }
2379 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002380 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002381 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002382 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2383 }
2384 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002385 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002386 }
Junda Liu29340342014-07-10 15:23:27 -07002387
2388 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002389 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002390 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002391 if (card == null) {
2392 loge("checkCarrierPrivilegesForPackage: No UICC");
2393 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2394 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002395 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2396 }
2397
2398 @Override
2399 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2400 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2401 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2402 UiccCard card = UiccController.getInstance().getUiccCard(i);
2403 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002404 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002405 continue;
2406 }
2407
2408 result = card.getCarrierPrivilegeStatus(
2409 mPhone.getContext().getPackageManager(), pkgName);
2410 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2411 break;
2412 }
2413 }
2414
2415 return result;
Junda Liu29340342014-07-10 15:23:27 -07002416 }
Derek Tan89e89d42014-07-08 17:00:10 -07002417
2418 @Override
Junda Liue64de782015-04-16 17:19:16 -07002419 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2420 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2421 loge("phoneId " + phoneId + " is not valid.");
2422 return null;
2423 }
2424 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002425 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002426 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002427 return null ;
2428 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002429 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002430 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002431 }
2432
Wink Savilleb564aae2014-10-23 10:18:09 -07002433 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002434 final Phone phone = getPhone(subId);
2435 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002436 if (card == null) {
2437 loge("getIccId: No UICC");
2438 return null;
2439 }
2440 String iccId = card.getIccId();
2441 if (TextUtils.isEmpty(iccId)) {
2442 loge("getIccId: ICC ID is null or empty.");
2443 return null;
2444 }
2445 return iccId;
2446 }
2447
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002448 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002449 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2450 String number) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002451 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002452
Jeff Sharkey85190e62014-12-05 09:40:12 -08002453 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002454 final Phone phone = getPhone(subId);
2455 if (phone == null) {
2456 return false;
2457 }
2458 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002459
2460 if (DBG_MERGE) {
2461 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2462 + subscriberId + " to " + number);
2463 }
2464
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002465 if (TextUtils.isEmpty(iccId)) {
2466 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002467 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002468
Jeff Sharkey85190e62014-12-05 09:40:12 -08002469 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2470
2471 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002472 if (alphaTag == null) {
2473 editor.remove(alphaTagPrefKey);
2474 } else {
2475 editor.putString(alphaTagPrefKey, alphaTag);
2476 }
2477
Jeff Sharkey85190e62014-12-05 09:40:12 -08002478 // Record both the line number and IMSI for this ICCID, since we need to
2479 // track all merged IMSIs based on line number
2480 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2481 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002482 if (number == null) {
2483 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002484 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002485 } else {
2486 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002487 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002488 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002489
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002490 editor.commit();
2491 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002492 }
2493
2494 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002495 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002496 // This is open to apps with WRITE_SMS.
2497 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002498 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002499 return null;
2500 }
Derek Tan97ebb422014-09-05 16:55:38 -07002501
2502 String iccId = getIccId(subId);
2503 if (iccId != null) {
2504 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002505 if (DBG_MERGE) {
2506 log("getLine1NumberForDisplay returning " +
2507 mTelephonySharedPreferences.getString(numberPrefKey, null));
2508 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002509 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002510 }
Jack Yue7939d82015-12-10 16:04:02 -08002511 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002512 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002513 }
2514
2515 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002516 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2517 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2518 return null;
2519 }
Derek Tan97ebb422014-09-05 16:55:38 -07002520
2521 String iccId = getIccId(subId);
2522 if (iccId != null) {
2523 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002524 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002525 }
Derek Tan97ebb422014-09-05 16:55:38 -07002526 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002527 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002528
2529 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002530 public String[] getMergedSubscriberIds(String callingPackage) {
2531 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2532 return null;
2533 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002534 final Context context = mPhone.getContext();
2535 final TelephonyManager tele = TelephonyManager.from(context);
2536 final SubscriptionManager sub = SubscriptionManager.from(context);
2537
2538 // Figure out what subscribers are currently active
2539 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002540 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2541 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2542 final long identity = Binder.clearCallingIdentity();
2543 try {
2544 final int[] subIds = sub.getActiveSubscriptionIdList();
2545 for (int subId : subIds) {
2546 activeSubscriberIds.add(tele.getSubscriberId(subId));
2547 }
2548 } finally {
2549 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002550 }
2551
2552 // First pass, find a number override for an active subscriber
2553 String mergeNumber = null;
2554 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2555 for (String key : prefs.keySet()) {
2556 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2557 final String subscriberId = (String) prefs.get(key);
2558 if (activeSubscriberIds.contains(subscriberId)) {
2559 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2560 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2561 mergeNumber = (String) prefs.get(numberKey);
2562 if (DBG_MERGE) {
2563 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2564 + " for active subscriber " + subscriberId);
2565 }
2566 if (!TextUtils.isEmpty(mergeNumber)) {
2567 break;
2568 }
2569 }
2570 }
2571 }
2572
2573 // Shortcut when no active merged subscribers
2574 if (TextUtils.isEmpty(mergeNumber)) {
2575 return null;
2576 }
2577
2578 // Second pass, find all subscribers under that line override
2579 final ArraySet<String> result = new ArraySet<>();
2580 for (String key : prefs.keySet()) {
2581 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2582 final String number = (String) prefs.get(key);
2583 if (mergeNumber.equals(number)) {
2584 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2585 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2586 final String subscriberId = (String) prefs.get(subscriberKey);
2587 if (!TextUtils.isEmpty(subscriberId)) {
2588 result.add(subscriberId);
2589 }
2590 }
2591 }
2592 }
2593
2594 final String[] resultArray = result.toArray(new String[result.size()]);
2595 Arrays.sort(resultArray);
2596 if (DBG_MERGE) {
2597 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2598 }
2599 return resultArray;
2600 }
2601
2602 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002603 public boolean setOperatorBrandOverride(int subId, String brand) {
2604 enforceCarrierPrivilege(subId);
2605 final Phone phone = getPhone(subId);
2606 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002607 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002608
2609 @Override
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002610 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002611 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2612 List<String> cdmaNonRoamingList) {
Shishir Agrawal61ff83b2016-02-19 10:41:00 -08002613 enforceCarrierPrivilege(subId);
2614 final Phone phone = getPhone(subId);
2615 if (phone == null) {
2616 return false;
2617 }
2618 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002619 cdmaNonRoamingList);
2620 }
2621
2622 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002623 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2624 enforceModifyPermission();
2625
2626 int returnValue = 0;
2627 try {
2628 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2629 if(result.exception == null) {
2630 if (result.result != null) {
2631 byte[] responseData = (byte[])(result.result);
2632 if(responseData.length > oemResp.length) {
2633 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2634 responseData.length + "bytes. Buffer Size is " +
2635 oemResp.length + "bytes.");
2636 }
2637 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2638 returnValue = responseData.length;
2639 }
2640 } else {
2641 CommandException ex = (CommandException) result.exception;
2642 returnValue = ex.getCommandError().ordinal();
2643 if(returnValue > 0) returnValue *= -1;
2644 }
2645 } catch (RuntimeException e) {
2646 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2647 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2648 if(returnValue > 0) returnValue *= -1;
2649 }
2650
2651 return returnValue;
2652 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002653
2654 @Override
2655 public void setRadioCapability(RadioAccessFamily[] rafs) {
2656 try {
2657 ProxyController.getInstance().setRadioCapability(rafs);
2658 } catch (RuntimeException e) {
2659 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2660 }
2661 }
2662
2663 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002664 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2665 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2666 return RadioAccessFamily.RAF_UNKNOWN;
2667 }
2668
Wink Saville5d475dd2014-10-17 15:00:58 -07002669 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2670 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002671
2672 @Override
2673 public void enableVideoCalling(boolean enable) {
2674 enforceModifyPermission();
2675 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2676 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2677 editor.commit();
2678 }
2679
2680 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002681 public boolean isVideoCallingEnabled(String callingPackage) {
2682 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2683 return false;
2684 }
2685
Andrew Lee77527ac2014-10-21 16:57:39 -07002686 // Check the user preference and the system-level IMS setting. Even if the user has
2687 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2688 // In the long run, we may instead need to check if there exists a connection service
2689 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002690 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2691 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2692 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002693 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002694
Andrew Leea1239f22015-03-02 17:44:07 -08002695 @Override
2696 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002697 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002698 }
2699
2700 @Override
2701 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002702 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002703 }
2704
Andrew Lee9431b832015-03-09 18:46:45 -07002705 @Override
2706 public boolean isTtyModeSupported() {
2707 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2708 TelephonyManager telephonyManager =
2709 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2710 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2711 }
2712
2713 @Override
2714 public boolean isHearingAidCompatibilitySupported() {
2715 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2716 }
2717
Sanket Padawe7310cc72015-01-14 09:53:20 -08002718 /**
2719 * Returns the unique device ID of phone, for example, the IMEI for
2720 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2721 *
2722 * <p>Requires Permission:
2723 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2724 */
2725 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002726 public String getDeviceId(String callingPackage) {
2727 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2728 return null;
2729 }
2730
Sanket Padawe7310cc72015-01-14 09:53:20 -08002731 final Phone phone = PhoneFactory.getPhone(0);
2732 if (phone != null) {
2733 return phone.getDeviceId();
2734 } else {
2735 return null;
2736 }
2737 }
2738
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002739 /*
2740 * {@hide}
2741 * Returns the IMS Registration Status
2742 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002743 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002744 public boolean isImsRegistered() {
2745 return mPhone.isImsRegistered();
2746 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002747
2748 @Override
2749 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2750 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2751 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002752
Nathan Haroldc55097a2015-03-11 18:14:50 -07002753 /*
2754 * {@hide}
2755 * Returns the IMS Registration Status
2756 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002757 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002758 return mPhone.isWifiCallingEnabled();
2759 }
2760
2761 /*
2762 * {@hide}
2763 * Returns the IMS Registration Status
2764 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002765 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002766 return mPhone.isVolteEnabled();
2767 }
Svet Ganovb320e182015-04-16 12:30:10 -07002768
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002769 /*
2770 * {@hide} Returns the IMS Registration Status
2771 */
2772 public boolean isVideoTelephonyAvailable() {
2773 return mPhone.isVideoEnabled();
2774 }
2775
Svet Ganovb320e182015-04-16 12:30:10 -07002776 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002777 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002778 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002779 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2780
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002781 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002782 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002783 } catch (SecurityException e) {
2784 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2785 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002786 }
Svet Ganovb320e182015-04-16 12:30:10 -07002787
2788 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2789 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2790 return false;
2791 }
2792
2793 return true;
2794 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002795
Makoto Onukifee69342015-06-29 14:44:50 -07002796 /**
Jack Yue7939d82015-12-10 16:04:02 -08002797 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002798 */
2799 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002800 // Default SMS app can always read it.
2801 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2802 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2803 return true;
2804 }
2805 try {
2806 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002807 } catch (SecurityException readPhoneStateSecurityException) {
2808 try {
2809 // Can be read with READ_SMS too.
2810 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2811 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2812 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2813 } catch (SecurityException readSmsSecurityException) {
2814 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2815 // permissions
2816 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2817 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2818 " or " + android.Manifest.permission.READ_SMS + ".");
2819 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002820 }
Makoto Onukifee69342015-06-29 14:44:50 -07002821 }
2822
Stuart Scott8eef64f2015-04-08 15:13:54 -07002823 @Override
2824 public void factoryReset(int subId) {
2825 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002826 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2827 return;
2828 }
2829
Svet Ganovcc087f82015-05-12 20:35:54 -07002830 final long identity = Binder.clearCallingIdentity();
2831 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002832 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2833 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002834 // Enable data
2835 setDataEnabled(subId, true);
2836 // Set network selection mode to automatic
2837 setNetworkSelectionModeAutomatic(subId);
2838 // Set preferred mobile network type to the best available
2839 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2840 // Turn off roaming
2841 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2842 }
2843 } finally {
2844 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002845 }
2846 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002847
2848 @Override
2849 public String getLocaleFromDefaultSim() {
2850 // We query all subscriptions instead of just the active ones, because
2851 // this might be called early on in the provisioning flow when the
2852 // subscriptions potentially aren't active yet.
2853 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2854 if (slist == null || slist.isEmpty()) {
2855 return null;
2856 }
2857
2858 // This function may be called very early, say, from the setup wizard, at
2859 // which point we won't have a default subscription set. If that's the case
2860 // we just choose the first, which will be valid in "most cases".
2861 final int defaultSubId = getDefaultSubscription();
2862 SubscriptionInfo info = null;
2863 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2864 info = slist.get(0);
2865 } else {
2866 for (SubscriptionInfo item : slist) {
2867 if (item.getSubscriptionId() == defaultSubId) {
2868 info = item;
2869 break;
2870 }
2871 }
2872
2873 if (info == null) {
2874 return null;
2875 }
2876 }
2877
2878 // Try and fetch the locale from the carrier properties or from the SIM language
2879 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002880 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002881 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002882 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002883 if (defaultPhone != null) {
2884 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2885 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002886 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002887 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2888 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002889 } else {
2890 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002891 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002892 }
2893 }
2894
Narayan Kamath011676f2015-07-29 12:04:08 +01002895 // The SIM language preferences only store a language (e.g. fr = French), not an
2896 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2897 // the SIM and carrier preferences does not include a country we add the country
2898 // determined from the SIM MCC to provide an exact locale.
2899 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002900 if (mccLocale != null) {
2901 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2902 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002903 }
2904
Tony Hill183b2de2015-06-24 14:53:58 +01002905 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002906 return null;
2907 }
2908
2909 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2910 final long identity = Binder.clearCallingIdentity();
2911 try {
2912 return mSubscriptionController.getAllSubInfoList(
2913 mPhone.getContext().getOpPackageName());
2914 } finally {
2915 Binder.restoreCallingIdentity(identity);
2916 }
2917 }
2918
2919 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2920 final long identity = Binder.clearCallingIdentity();
2921 try {
2922 return mSubscriptionController.getActiveSubscriptionInfoList(
2923 mPhone.getContext().getOpPackageName());
2924 } finally {
2925 Binder.restoreCallingIdentity(identity);
2926 }
2927 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002928
2929 /**
2930 * {@hide}
2931 * Returns the modem stats
2932 */
2933 @Override
2934 public ModemActivityInfo getModemActivityInfo() {
2935 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2936 }
Jack Yue7939d82015-12-10 16:04:02 -08002937
2938 /**
2939 * {@hide}
2940 * Returns the service state information on specified subscription.
2941 */
2942 @Override
2943 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2944
2945 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2946 return null;
2947 }
2948
2949 final Phone phone = getPhone(subId);
2950 if (phone == null) {
2951 return null;
2952 }
2953
2954 return phone.getServiceState();
2955 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002956}