blob: 762897e1f0ef369a2eff63e1ee41388136336de2 [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 Agrawal21409252015-01-15 23:33:50 -0800244 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
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 Agrawaleb8771e2014-07-22 11:24:08 -0700320 if (uiccCard == null) {
321 loge("iccTransmitApduLogicalChannel: No UICC");
322 request.result = new IccIoResult(0x6F, 0, (byte[])null);
323 synchronized (request) {
324 request.notifyAll();
325 }
326 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700327 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
328 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700329 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700330 iccArgument.channel, iccArgument.cla, iccArgument.command,
331 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700332 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700333 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700334 break;
335
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700336 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700337 ar = (AsyncResult) msg.obj;
338 request = (MainThreadRequest) ar.userObj;
339 if (ar.exception == null && ar.result != null) {
340 request.result = ar.result;
341 } else {
342 request.result = new IccIoResult(0x6F, 0, (byte[])null);
343 if (ar.result == null) {
344 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800345 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700346 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800347 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 } else {
349 loge("iccTransmitApduLogicalChannel: Unknown exception");
350 }
351 }
352 synchronized (request) {
353 request.notifyAll();
354 }
355 break;
356
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700357 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
358 request = (MainThreadRequest) msg.obj;
359 iccArgument = (IccAPDUArgument) request.argument;
360 if (uiccCard == null) {
361 loge("iccTransmitApduBasicChannel: No UICC");
362 request.result = new IccIoResult(0x6F, 0, (byte[])null);
363 synchronized (request) {
364 request.notifyAll();
365 }
366 } else {
367 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
368 request);
369 uiccCard.iccTransmitApduBasicChannel(
370 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
371 iccArgument.p3, iccArgument.data, onCompleted);
372 }
373 break;
374
375 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
376 ar = (AsyncResult) msg.obj;
377 request = (MainThreadRequest) ar.userObj;
378 if (ar.exception == null && ar.result != null) {
379 request.result = ar.result;
380 } else {
381 request.result = new IccIoResult(0x6F, 0, (byte[])null);
382 if (ar.result == null) {
383 loge("iccTransmitApduBasicChannel: Empty response");
384 } else if (ar.exception instanceof CommandException) {
385 loge("iccTransmitApduBasicChannel: CommandException: " +
386 ar.exception);
387 } else {
388 loge("iccTransmitApduBasicChannel: Unknown exception");
389 }
390 }
391 synchronized (request) {
392 request.notifyAll();
393 }
394 break;
395
396 case CMD_EXCHANGE_SIM_IO:
397 request = (MainThreadRequest) msg.obj;
398 iccArgument = (IccAPDUArgument) request.argument;
399 if (uiccCard == null) {
400 loge("iccExchangeSimIO: No UICC");
401 request.result = new IccIoResult(0x6F, 0, (byte[])null);
402 synchronized (request) {
403 request.notifyAll();
404 }
405 } else {
406 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
407 request);
408 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
409 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
410 iccArgument.data, onCompleted);
411 }
412 break;
413
414 case EVENT_EXCHANGE_SIM_IO_DONE:
415 ar = (AsyncResult) msg.obj;
416 request = (MainThreadRequest) ar.userObj;
417 if (ar.exception == null && ar.result != null) {
418 request.result = ar.result;
419 } else {
420 request.result = new IccIoResult(0x6f, 0, (byte[])null);
421 }
422 synchronized (request) {
423 request.notifyAll();
424 }
425 break;
426
Derek Tan4d5e5c12014-02-04 11:54:58 -0800427 case CMD_SEND_ENVELOPE:
428 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700429 if (uiccCard == null) {
430 loge("sendEnvelopeWithStatus: No UICC");
431 request.result = new IccIoResult(0x6F, 0, (byte[])null);
432 synchronized (request) {
433 request.notifyAll();
434 }
435 } else {
436 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
437 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
438 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800439 break;
440
441 case EVENT_SEND_ENVELOPE_DONE:
442 ar = (AsyncResult) msg.obj;
443 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700444 if (ar.exception == null && ar.result != null) {
445 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800446 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700447 request.result = new IccIoResult(0x6F, 0, (byte[])null);
448 if (ar.result == null) {
449 loge("sendEnvelopeWithStatus: Empty response");
450 } else if (ar.exception instanceof CommandException) {
451 loge("sendEnvelopeWithStatus: CommandException: " +
452 ar.exception);
453 } else {
454 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
455 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800456 }
457 synchronized (request) {
458 request.notifyAll();
459 }
460 break;
461
Shishir Agrawal566b7612013-10-28 14:41:00 -0700462 case CMD_OPEN_CHANNEL:
463 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700464 if (uiccCard == null) {
465 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800466 request.result = new IccOpenLogicalChannelResponse(-1,
467 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700468 synchronized (request) {
469 request.notifyAll();
470 }
471 } else {
472 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
473 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
474 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700475 break;
476
477 case EVENT_OPEN_CHANNEL_DONE:
478 ar = (AsyncResult) msg.obj;
479 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700480 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700481 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700482 int[] result = (int[]) ar.result;
483 int channelId = result[0];
484 byte[] selectResponse = null;
485 if (result.length > 1) {
486 selectResponse = new byte[result.length - 1];
487 for (int i = 1; i < result.length; ++i) {
488 selectResponse[i - 1] = (byte) result[i];
489 }
490 }
491 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700492 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700494 if (ar.result == null) {
495 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700496 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700497 if (ar.exception != null) {
498 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
499 }
500
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700501 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700502 if (ar.exception instanceof CommandException) {
503 CommandException.Error error =
504 ((CommandException) (ar.exception)).getCommandError();
505 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700506 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700507 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700508 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700509 }
510 }
511 openChannelResp = new IccOpenLogicalChannelResponse(
512 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700513 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700514 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700515 synchronized (request) {
516 request.notifyAll();
517 }
518 break;
519
520 case CMD_CLOSE_CHANNEL:
521 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700522 if (uiccCard == null) {
523 loge("iccCloseLogicalChannel: No UICC");
524 request.result = new IccIoResult(0x6F, 0, (byte[])null);
525 synchronized (request) {
526 request.notifyAll();
527 }
528 } else {
529 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
530 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
531 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700532 break;
533
534 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800535 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
536 break;
537
538 case CMD_NV_READ_ITEM:
539 request = (MainThreadRequest) msg.obj;
540 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
541 mPhone.nvReadItem((Integer) request.argument, onCompleted);
542 break;
543
544 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700545 ar = (AsyncResult) msg.obj;
546 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800547 if (ar.exception == null && ar.result != null) {
548 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700549 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800550 request.result = "";
551 if (ar.result == null) {
552 loge("nvReadItem: Empty response");
553 } else if (ar.exception instanceof CommandException) {
554 loge("nvReadItem: CommandException: " +
555 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700556 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800557 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700558 }
559 }
560 synchronized (request) {
561 request.notifyAll();
562 }
563 break;
564
Jake Hambye994d462014-02-03 13:10:13 -0800565 case CMD_NV_WRITE_ITEM:
566 request = (MainThreadRequest) msg.obj;
567 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
568 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
569 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
570 break;
571
572 case EVENT_NV_WRITE_ITEM_DONE:
573 handleNullReturnEvent(msg, "nvWriteItem");
574 break;
575
576 case CMD_NV_WRITE_CDMA_PRL:
577 request = (MainThreadRequest) msg.obj;
578 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
579 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
580 break;
581
582 case EVENT_NV_WRITE_CDMA_PRL_DONE:
583 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
584 break;
585
586 case CMD_NV_RESET_CONFIG:
587 request = (MainThreadRequest) msg.obj;
588 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
589 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
590 break;
591
592 case EVENT_NV_RESET_CONFIG_DONE:
593 handleNullReturnEvent(msg, "nvResetConfig");
594 break;
595
Jake Hamby7c27be32014-03-03 13:25:59 -0800596 case CMD_GET_PREFERRED_NETWORK_TYPE:
597 request = (MainThreadRequest) msg.obj;
598 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700599 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800600 break;
601
602 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
603 ar = (AsyncResult) msg.obj;
604 request = (MainThreadRequest) ar.userObj;
605 if (ar.exception == null && ar.result != null) {
606 request.result = ar.result; // Integer
607 } else {
608 request.result = -1;
609 if (ar.result == null) {
610 loge("getPreferredNetworkType: Empty response");
611 } else if (ar.exception instanceof CommandException) {
612 loge("getPreferredNetworkType: CommandException: " +
613 ar.exception);
614 } else {
615 loge("getPreferredNetworkType: Unknown exception");
616 }
617 }
618 synchronized (request) {
619 request.notifyAll();
620 }
621 break;
622
623 case CMD_SET_PREFERRED_NETWORK_TYPE:
624 request = (MainThreadRequest) msg.obj;
625 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
626 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700627 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800628 break;
629
630 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
631 handleNullReturnEvent(msg, "setPreferredNetworkType");
632 break;
633
Steven Liu4bf01bc2014-07-17 11:05:29 -0500634 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
635 request = (MainThreadRequest)msg.obj;
636 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
637 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
638 break;
639
640 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
641 ar = (AsyncResult)msg.obj;
642 request = (MainThreadRequest)ar.userObj;
643 request.result = ar;
644 synchronized (request) {
645 request.notifyAll();
646 }
647 break;
648
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800649 case CMD_SET_VOICEMAIL_NUMBER:
650 request = (MainThreadRequest) msg.obj;
651 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
652 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800653 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
654 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800655 break;
656
657 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
658 handleNullReturnEvent(msg, "setVoicemailNumber");
659 break;
660
Stuart Scott54788802015-03-30 13:18:01 -0700661 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
662 request = (MainThreadRequest) msg.obj;
663 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
664 request);
665 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
666 break;
667
668 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
669 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
670 break;
671
Shishir Agrawal302c8692015-06-19 13:49:39 -0700672 case CMD_PERFORM_NETWORK_SCAN:
673 request = (MainThreadRequest) msg.obj;
674 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
675 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
676 break;
677
678 case EVENT_PERFORM_NETWORK_SCAN_DONE:
679 ar = (AsyncResult) msg.obj;
680 request = (MainThreadRequest) ar.userObj;
681 CellNetworkScanResult cellScanResult;
682 if (ar.exception == null && ar.result != null) {
683 cellScanResult = new CellNetworkScanResult(
684 CellNetworkScanResult.STATUS_SUCCESS,
685 (List<OperatorInfo>) ar.result);
686 } else {
687 if (ar.result == null) {
688 loge("getCellNetworkScanResults: Empty response");
689 }
690 if (ar.exception != null) {
691 loge("getCellNetworkScanResults: Exception: " + ar.exception);
692 }
693 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
694 if (ar.exception instanceof CommandException) {
695 CommandException.Error error =
696 ((CommandException) (ar.exception)).getCommandError();
697 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
698 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
699 } else if (error == CommandException.Error.GENERIC_FAILURE) {
700 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
701 }
702 }
703 cellScanResult = new CellNetworkScanResult(errorCode, null);
704 }
705 request.result = cellScanResult;
706 synchronized (request) {
707 request.notifyAll();
708 }
709 break;
710
711 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
712 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700713 ManualNetworkSelectionArgument selArg =
714 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700715 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
716 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700717 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
718 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700719 break;
720
721 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
722 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
723 break;
724
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700725 case CMD_GET_MODEM_ACTIVITY_INFO:
726 request = (MainThreadRequest) msg.obj;
727 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700728 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700729 break;
730
731 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
732 ar = (AsyncResult) msg.obj;
733 request = (MainThreadRequest) ar.userObj;
734 if (ar.exception == null && ar.result != null) {
735 request.result = ar.result;
736 } else {
737 if (ar.result == null) {
738 loge("queryModemActivityInfo: Empty response");
739 } else if (ar.exception instanceof CommandException) {
740 loge("queryModemActivityInfo: CommandException: " +
741 ar.exception);
742 } else {
743 loge("queryModemActivityInfo: Unknown exception");
744 }
745 }
Amit Mahajand4766222016-01-28 15:28:28 -0800746 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
747 if (request.result == null) {
748 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
749 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700750 synchronized (request) {
751 request.notifyAll();
752 }
753 break;
754
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700755 default:
756 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
757 break;
758 }
759 }
Jake Hambye994d462014-02-03 13:10:13 -0800760
761 private void handleNullReturnEvent(Message msg, String command) {
762 AsyncResult ar = (AsyncResult) msg.obj;
763 MainThreadRequest request = (MainThreadRequest) ar.userObj;
764 if (ar.exception == null) {
765 request.result = true;
766 } else {
767 request.result = false;
768 if (ar.exception instanceof CommandException) {
769 loge(command + ": CommandException: " + ar.exception);
770 } else {
771 loge(command + ": Unknown exception");
772 }
773 }
774 synchronized (request) {
775 request.notifyAll();
776 }
777 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700778 }
779
780 /**
781 * Posts the specified command to be executed on the main thread,
782 * waits for the request to complete, and returns the result.
783 * @see #sendRequestAsync
784 */
785 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800786 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700787 }
788
789 /**
790 * Posts the specified command to be executed on the main thread,
791 * waits for the request to complete, and returns the result.
792 * @see #sendRequestAsync
793 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800794 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
796 throw new RuntimeException("This method will deadlock if called from the main thread.");
797 }
798
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800799 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700800 Message msg = mMainThreadHandler.obtainMessage(command, request);
801 msg.sendToTarget();
802
803 // Wait for the request to complete
804 synchronized (request) {
805 while (request.result == null) {
806 try {
807 request.wait();
808 } catch (InterruptedException e) {
809 // Do nothing, go back and wait until the request is complete
810 }
811 }
812 }
813 return request.result;
814 }
815
816 /**
817 * Asynchronous ("fire and forget") version of sendRequest():
818 * Posts the specified command to be executed on the main thread, and
819 * returns immediately.
820 * @see #sendRequest
821 */
822 private void sendRequestAsync(int command) {
823 mMainThreadHandler.sendEmptyMessage(command);
824 }
825
826 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700827 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
828 * @see {@link #sendRequest(int,Object)}
829 */
830 private void sendRequestAsync(int command, Object argument) {
831 MainThreadRequest request = new MainThreadRequest(argument);
832 Message msg = mMainThreadHandler.obtainMessage(command, request);
833 msg.sendToTarget();
834 }
835
836 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 * Initialize the singleton PhoneInterfaceManager instance.
838 * This is only done once, at startup, from PhoneApp.onCreate().
839 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700840 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700841 synchronized (PhoneInterfaceManager.class) {
842 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700843 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700844 } else {
845 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
846 }
847 return sInstance;
848 }
849 }
850
851 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700852 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 mApp = app;
854 mPhone = phone;
855 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700856 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700857 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
858 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700859 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700860 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800861 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800862
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700863 publish();
864 }
865
866 private void publish() {
867 if (DBG) log("publish: " + this);
868
869 ServiceManager.addService("phone", this);
870 }
871
Stuart Scott584921c2015-01-15 17:10:34 -0800872 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800873 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
874 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800875 }
876
Wink Saville36469e72014-06-11 15:17:00 -0700877 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700878 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800879 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 //
882 // Implementation of the ITelephony interface.
883 //
884
885 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700886 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700887 }
888
Wink Savilleb564aae2014-10-23 10:18:09 -0700889 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700890 if (DBG) log("dial: " + number);
891 // No permission check needed here: This is just a wrapper around the
892 // ACTION_DIAL intent, which is available to any app since it puts up
893 // the UI before it does anything.
894
895 String url = createTelUrl(number);
896 if (url == null) {
897 return;
898 }
899
900 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700901 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700902 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
903 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
904 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
905 mApp.startActivity(intent);
906 }
907 }
908
909 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700910 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700911 }
912
Wink Savilleb564aae2014-10-23 10:18:09 -0700913 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700914 if (DBG) log("call: " + number);
915
916 // This is just a wrapper around the ACTION_CALL intent, but we still
917 // need to do a permission check since we're calling startActivity()
918 // from the context of the phone app.
919 enforceCallPermission();
920
921 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
922 != AppOpsManager.MODE_ALLOWED) {
923 return;
924 }
925
926 String url = createTelUrl(number);
927 if (url == null) {
928 return;
929 }
930
Wink Saville08874612014-08-31 19:19:58 -0700931 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100932 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800933 if (slist != null) {
934 for (SubscriptionInfo subInfoRecord : slist) {
935 if (subInfoRecord.getSubscriptionId() == subId) {
936 isValid = true;
937 break;
938 }
Wink Saville08874612014-08-31 19:19:58 -0700939 }
940 }
941 if (isValid == false) {
942 return;
943 }
944
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700946 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700947 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
948 mApp.startActivity(intent);
949 }
950
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700951 /**
952 * End a call based on call state
953 * @return true is a call was ended
954 */
955 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700956 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700957 }
958
959 /**
960 * End a call based on the call state of the subId
961 * @return true is a call was ended
962 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700963 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800965 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700966 }
967
968 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700969 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700970 }
971
Wink Savilleb564aae2014-10-23 10:18:09 -0700972 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 if (DBG) log("answerRingingCall...");
974 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
975 // but that can probably wait till the big TelephonyManager API overhaul.
976 // For now, protect this call with the MODIFY_PHONE_STATE permission.
977 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800978 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700979 }
980
981 /**
982 * Make the actual telephony calls to implement answerRingingCall().
983 * This should only be called from the main thread of the Phone app.
984 * @see #answerRingingCall
985 *
986 * TODO: it would be nice to return true if we answered the call, or
987 * false if there wasn't actually a ringing incoming call, or some
988 * other error occurred. (In other words, pass back the return value
989 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
990 * But that would require calling this method via sendRequest() rather
991 * than sendRequestAsync(), and right now we don't actually *need* that
992 * return value, so let's just return void for now.
993 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700994 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700995 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700996 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700997 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
998 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700999 if (hasActiveCall && hasHoldingCall) {
1000 // Both lines are in use!
1001 // TODO: provide a flag to let the caller specify what
1002 // policy to use if both lines are in use. (The current
1003 // behavior is hardwired to "answer incoming, end ongoing",
1004 // which is how the CALL button is specced to behave.)
1005 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1006 return;
1007 } else {
1008 // answerCall() will automatically hold the current active
1009 // call, if there is one.
1010 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1011 return;
1012 }
1013 } else {
1014 // No call was ringing.
1015 return;
1016 }
1017 }
1018
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001020 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001021 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001022 public void silenceRinger() {
1023 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001024 }
1025
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001026 @Override
1027 public boolean isOffhook(String callingPackage) {
1028 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001029 }
1030
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001031 @Override
1032 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1033 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1034 return false;
1035 }
1036
Sanket Padawe356d7632015-06-22 14:03:32 -07001037 final Phone phone = getPhone(subId);
1038 if (phone != null) {
1039 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1040 } else {
1041 return false;
1042 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001043 }
1044
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001045 @Override
1046 public boolean isRinging(String callingPackage) {
1047 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001048 }
1049
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001050 @Override
1051 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1052 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1053 return false;
1054 }
1055
Sanket Padawe356d7632015-06-22 14:03:32 -07001056 final Phone phone = getPhone(subId);
1057 if (phone != null) {
1058 return (phone.getState() == PhoneConstants.State.RINGING);
1059 } else {
1060 return false;
1061 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001062 }
1063
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001064 @Override
1065 public boolean isIdle(String callingPackage) {
1066 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001067 }
1068
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001069 @Override
1070 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1071 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1072 return false;
1073 }
1074
Sanket Padawe356d7632015-06-22 14:03:32 -07001075 final Phone phone = getPhone(subId);
1076 if (phone != null) {
1077 return (phone.getState() == PhoneConstants.State.IDLE);
1078 } else {
1079 return false;
1080 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001081 }
1082
Svet Ganovb320e182015-04-16 12:30:10 -07001083 public boolean isSimPinEnabled(String callingPackage) {
1084 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1085 return false;
1086 }
1087
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001088 return (PhoneGlobals.getInstance().isSimPinEnabled());
1089 }
1090
1091 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001092 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001093 }
1094
Wink Savilleb564aae2014-10-23 10:18:09 -07001095 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001096 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001097 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1098 }
1099
1100 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001101 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001102 }
1103
Wink Savilleb564aae2014-10-23 10:18:09 -07001104 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001105 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001106 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1107 }
1108
1109 /** {@hide} */
1110 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001111 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001112 }
1113
Wink Savilleb564aae2014-10-23 10:18:09 -07001114 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001115 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001116 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001117 checkSimPin.start();
1118 return checkSimPin.unlockSim(null, pin);
1119 }
1120
Wink Saville9de0f752013-10-22 19:04:03 -07001121 /** {@hide} */
1122 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001123 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001124 }
1125
Wink Savilleb564aae2014-10-23 10:18:09 -07001126 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001128 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 checkSimPuk.start();
1130 return checkSimPuk.unlockSim(puk, pin);
1131 }
1132
1133 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001134 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135 * a synchronous one.
1136 */
1137 private static class UnlockSim extends Thread {
1138
1139 private final IccCard mSimCard;
1140
1141 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001142 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1143 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001144
1145 // For replies from SimCard interface
1146 private Handler mHandler;
1147
1148 // For async handler to identify request type
1149 private static final int SUPPLY_PIN_COMPLETE = 100;
1150
1151 public UnlockSim(IccCard simCard) {
1152 mSimCard = simCard;
1153 }
1154
1155 @Override
1156 public void run() {
1157 Looper.prepare();
1158 synchronized (UnlockSim.this) {
1159 mHandler = new Handler() {
1160 @Override
1161 public void handleMessage(Message msg) {
1162 AsyncResult ar = (AsyncResult) msg.obj;
1163 switch (msg.what) {
1164 case SUPPLY_PIN_COMPLETE:
1165 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1166 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001167 mRetryCount = msg.arg1;
1168 if (ar.exception != null) {
1169 if (ar.exception instanceof CommandException &&
1170 ((CommandException)(ar.exception)).getCommandError()
1171 == CommandException.Error.PASSWORD_INCORRECT) {
1172 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1173 } else {
1174 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1175 }
1176 } else {
1177 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1178 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001179 mDone = true;
1180 UnlockSim.this.notifyAll();
1181 }
1182 break;
1183 }
1184 }
1185 };
1186 UnlockSim.this.notifyAll();
1187 }
1188 Looper.loop();
1189 }
1190
1191 /*
1192 * Use PIN or PUK to unlock SIM card
1193 *
1194 * If PUK is null, unlock SIM card with PIN
1195 *
1196 * If PUK is not null, unlock SIM card with PUK and set PIN code
1197 */
Wink Saville9de0f752013-10-22 19:04:03 -07001198 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001199
1200 while (mHandler == null) {
1201 try {
1202 wait();
1203 } catch (InterruptedException e) {
1204 Thread.currentThread().interrupt();
1205 }
1206 }
1207 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1208
1209 if (puk == null) {
1210 mSimCard.supplyPin(pin, callback);
1211 } else {
1212 mSimCard.supplyPuk(puk, pin, callback);
1213 }
1214
1215 while (!mDone) {
1216 try {
1217 Log.d(LOG_TAG, "wait for done");
1218 wait();
1219 } catch (InterruptedException e) {
1220 // Restore the interrupted status
1221 Thread.currentThread().interrupt();
1222 }
1223 }
1224 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001225 int[] resultArray = new int[2];
1226 resultArray[0] = mResult;
1227 resultArray[1] = mRetryCount;
1228 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 }
1230 }
1231
1232 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001233 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001234
1235 }
1236
Wink Savilleb564aae2014-10-23 10:18:09 -07001237 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 // No permission check needed here: this call is harmless, and it's
1239 // needed for the ServiceState.requestStateUpdate() call (which is
1240 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001241 final Phone phone = getPhone(subId);
1242 if (phone != null) {
1243 phone.updateServiceLocation();
1244 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001245 }
1246
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001247 @Override
1248 public boolean isRadioOn(String callingPackage) {
1249 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001250 }
1251
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001252 @Override
1253 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1254 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1255 return false;
1256 }
1257 return isRadioOnForSubscriber(subId);
1258 }
1259
1260 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001261 final Phone phone = getPhone(subId);
1262 if (phone != null) {
1263 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1264 } else {
1265 return false;
1266 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001267 }
1268
1269 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001270 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001271
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001272 }
Wink Saville36469e72014-06-11 15:17:00 -07001273
Wink Savilleb564aae2014-10-23 10:18:09 -07001274 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001275 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001276 final Phone phone = getPhone(subId);
1277 if (phone != null) {
1278 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1279 }
Wink Saville36469e72014-06-11 15:17:00 -07001280 }
1281
1282 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001283 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001284 }
1285
Wink Savilleb564aae2014-10-23 10:18:09 -07001286 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001287 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001288 final Phone phone = getPhone(subId);
1289 if (phone == null) {
1290 return false;
1291 }
1292 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001293 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001294 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001295 }
1296 return true;
1297 }
Wink Saville36469e72014-06-11 15:17:00 -07001298
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001299 public boolean needMobileRadioShutdown() {
1300 /*
1301 * If any of the Radios are available, it will need to be
1302 * shutdown. So return true if any Radio is available.
1303 */
1304 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1305 Phone phone = PhoneFactory.getPhone(i);
1306 if (phone != null && phone.isRadioAvailable()) return true;
1307 }
1308 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1309 return false;
1310 }
1311
1312 public void shutdownMobileRadios() {
1313 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1314 logv("Shutting down Phone " + i);
1315 shutdownRadioUsingPhoneId(i);
1316 }
1317 }
1318
1319 private void shutdownRadioUsingPhoneId(int phoneId) {
1320 enforceModifyPermission();
1321 Phone phone = PhoneFactory.getPhone(phoneId);
1322 if (phone != null && phone.isRadioAvailable()) {
1323 phone.shutdownRadio();
1324 }
1325 }
1326
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001328 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001329 }
1330
Wink Savilleb564aae2014-10-23 10:18:09 -07001331 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001333 final Phone phone = getPhone(subId);
1334 if (phone != null) {
1335 phone.setRadioPower(turnOn);
1336 return true;
1337 } else {
1338 return false;
1339 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001340 }
1341
Wink Saville36469e72014-06-11 15:17:00 -07001342 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001343 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001344 public boolean enableDataConnectivity() {
1345 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001346 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001347 final Phone phone = getPhone(subId);
1348 if (phone != null) {
1349 phone.setDataEnabled(true);
1350 return true;
1351 } else {
1352 return false;
1353 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 }
1355
Wink Saville36469e72014-06-11 15:17:00 -07001356 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001357 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001358 public boolean disableDataConnectivity() {
1359 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001360 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001361 final Phone phone = getPhone(subId);
1362 if (phone != null) {
1363 phone.setDataEnabled(false);
1364 return true;
1365 } else {
1366 return false;
1367 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 }
1369
Wink Saville36469e72014-06-11 15:17:00 -07001370 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001371 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001372 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001373 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001374 final Phone phone = getPhone(subId);
1375 if (phone != null) {
1376 return phone.isDataConnectivityPossible();
1377 } else {
1378 return false;
1379 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 }
1381
1382 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001383 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001384 }
1385
Wink Savilleb564aae2014-10-23 10:18:09 -07001386 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001388 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1389 return false;
1390 }
Wink Saville36469e72014-06-11 15:17:00 -07001391 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 }
1393
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001395 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001396 }
1397
Wink Savilleb564aae2014-10-23 10:18:09 -07001398 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001399 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001400 }
1401
Sanket Padawe356d7632015-06-22 14:03:32 -07001402 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001404 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001405 if (phone != null) {
1406 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1407 } else {
1408 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1409 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001410 }
1411
Sanket Padawe356d7632015-06-22 14:03:32 -07001412 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001413 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001414 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001415 if (phone != null) {
1416 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1417 } else {
1418 return TelephonyManager.DATA_ACTIVITY_NONE;
1419 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001420 }
1421
1422 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001423 public Bundle getCellLocation(String callingPackage) {
1424 enforceFineOrCoarseLocationPermission("getCellLocation");
1425
1426 // OP_COARSE_LOCATION controls both fine and coarse location.
1427 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1428 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001429 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001430 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001431 }
1432
Jake Hambye994d462014-02-03 13:10:13 -08001433 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 if (DBG_LOC) log("getCellLocation: is active user");
1435 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001436 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001437 if (phone == null) {
1438 return null;
1439 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001440 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001441 return data;
1442 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001443 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001444 return null;
1445 }
1446 }
1447
Svetoslav64fad262015-04-14 14:35:21 -07001448 private void enforceFineOrCoarseLocationPermission(String message) {
1449 try {
1450 mApp.enforceCallingOrSelfPermission(
1451 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1452 } catch (SecurityException e) {
1453 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1454 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1455 // is the weaker precondition
1456 mApp.enforceCallingOrSelfPermission(
1457 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1458 }
1459 }
1460
1461
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 @Override
1463 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001464 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001465 }
1466
Sanket Padawe356d7632015-06-22 14:03:32 -07001467 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001468 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 mApp.enforceCallingOrSelfPermission(
1470 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001471 final Phone phone = getPhone(subId);
1472 if (phone != null) {
1473 phone.enableLocationUpdates();
1474 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 }
1476
1477 @Override
1478 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001479 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001480 }
1481
Sanket Padawe356d7632015-06-22 14:03:32 -07001482 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001483 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 mApp.enforceCallingOrSelfPermission(
1485 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001486 final Phone phone = getPhone(subId);
1487 if (phone != null) {
1488 phone.disableLocationUpdates();
1489 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001490 }
1491
1492 @Override
1493 @SuppressWarnings("unchecked")
1494 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001495 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1496
1497 // OP_COARSE_LOCATION controls both fine and coarse location.
1498 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1499 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1500 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 }
1502
1503 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1504 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1505 return null;
1506 }
Svetoslav64fad262015-04-14 14:35:21 -07001507
Jake Hambye994d462014-02-03 13:10:13 -08001508 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1510
1511 ArrayList<NeighboringCellInfo> cells = null;
1512
1513 try {
1514 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001515 CMD_HANDLE_NEIGHBORING_CELL, null,
1516 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001517 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001518 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001519 }
1520 return cells;
1521 } else {
1522 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1523 return null;
1524 }
1525 }
1526
1527
1528 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001529 public List<CellInfo> getAllCellInfo(String callingPackage) {
1530 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1531
1532 // OP_COARSE_LOCATION controls both fine and coarse location.
1533 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1534 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1535 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 }
1537
Jake Hambye994d462014-02-03 13:10:13 -08001538 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001540 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1541 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001542 final List<CellInfo> info = phone.getAllCellInfo();
1543 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001544 }
1545 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 } else {
1547 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1548 return null;
1549 }
1550 }
1551
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001552 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 public void setCellInfoListRate(int rateInMillis) {
1554 mPhone.setCellInfoListRate(rateInMillis);
1555 }
1556
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001557 //
1558 // Internal helper methods.
1559 //
1560
Jake Hambye994d462014-02-03 13:10:13 -08001561 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001562 boolean ok;
1563
1564 boolean self = Binder.getCallingUid() == Process.myUid();
1565 if (!self) {
1566 // Get the caller's user id then clear the calling identity
1567 // which will be restored in the finally clause.
1568 int callingUser = UserHandle.getCallingUserId();
1569 long ident = Binder.clearCallingIdentity();
1570
1571 try {
1572 // With calling identity cleared the current user is the foreground user.
1573 int foregroundUser = ActivityManager.getCurrentUser();
1574 ok = (foregroundUser == callingUser);
1575 if (DBG_LOC) {
1576 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1577 + " callingUser=" + callingUser + " ok=" + ok);
1578 }
1579 } catch (Exception ex) {
1580 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1581 ok = false;
1582 } finally {
1583 Binder.restoreCallingIdentity(ident);
1584 }
1585 } else {
1586 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1587 ok = true;
1588 }
1589 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1590 return ok;
1591 }
1592
1593 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001594 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1595 *
1596 * @throws SecurityException if the caller does not have the required permission
1597 */
1598 private void enforceModifyPermission() {
1599 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1600 }
1601
1602 /**
Junda Liua2e36012014-07-09 18:30:01 -07001603 * Make sure either system app or the caller has carrier privilege.
1604 *
1605 * @throws SecurityException if the caller does not have the required permission/privilege
1606 */
1607 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001608 int permission = mApp.checkCallingOrSelfPermission(
1609 android.Manifest.permission.MODIFY_PHONE_STATE);
1610 if (permission == PackageManager.PERMISSION_GRANTED) {
1611 return;
1612 }
1613
1614 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001615 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001616 loge("No Carrier Privilege.");
1617 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001618 }
1619 }
1620
1621 /**
1622 * Make sure the caller has carrier privilege.
1623 *
1624 * @throws SecurityException if the caller does not have the required permission
1625 */
1626 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001627 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001628 loge("No Carrier Privilege.");
1629 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001630 }
1631 }
1632
1633 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001634 * Make sure the caller has the CALL_PHONE permission.
1635 *
1636 * @throws SecurityException if the caller does not have the required permission
1637 */
1638 private void enforceCallPermission() {
1639 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1640 }
1641
Stuart Scott8eef64f2015-04-08 15:13:54 -07001642 private void enforceConnectivityInternalPermission() {
1643 mApp.enforceCallingOrSelfPermission(
1644 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1645 "ConnectivityService");
1646 }
1647
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 private String createTelUrl(String number) {
1649 if (TextUtils.isEmpty(number)) {
1650 return null;
1651 }
1652
Jake Hambye994d462014-02-03 13:10:13 -08001653 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001654 }
1655
Ihab Awadf9e92732013-12-05 18:02:52 -08001656 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001657 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1658 }
1659
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001660 private static void logv(String msg) {
1661 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1662 }
1663
Ihab Awadf9e92732013-12-05 18:02:52 -08001664 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001665 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1666 }
1667
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001668 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001669 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001670 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001671 }
1672
Sanket Padawe356d7632015-06-22 14:03:32 -07001673 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001674 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001675 final Phone phone = getPhone(subId);
1676 if (phone == null) {
1677 return PhoneConstants.PHONE_TYPE_NONE;
1678 } else {
1679 return getPhone(subId).getPhoneType();
1680 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001681 }
1682
1683 /**
1684 * Returns the CDMA ERI icon index to display
1685 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001686 @Override
1687 public int getCdmaEriIconIndex(String callingPackage) {
1688 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001689 }
1690
Sanket Padawe356d7632015-06-22 14:03:32 -07001691 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001692 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1693 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1694 return -1;
1695 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001696 final Phone phone = getPhone(subId);
1697 if (phone != null) {
1698 return phone.getCdmaEriIconIndex();
1699 } else {
1700 return -1;
1701 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001702 }
1703
1704 /**
1705 * Returns the CDMA ERI icon mode,
1706 * 0 - ON
1707 * 1 - FLASHING
1708 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001709 @Override
1710 public int getCdmaEriIconMode(String callingPackage) {
1711 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001712 }
1713
Sanket Padawe356d7632015-06-22 14:03:32 -07001714 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001715 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1716 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1717 return -1;
1718 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001719 final Phone phone = getPhone(subId);
1720 if (phone != null) {
1721 return phone.getCdmaEriIconMode();
1722 } else {
1723 return -1;
1724 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001725 }
1726
1727 /**
1728 * Returns the CDMA ERI text,
1729 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001730 @Override
1731 public String getCdmaEriText(String callingPackage) {
1732 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001733 }
1734
Sanket Padawe356d7632015-06-22 14:03:32 -07001735 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001736 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1737 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1738 return null;
1739 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001740 final Phone phone = getPhone(subId);
1741 if (phone != null) {
1742 return phone.getCdmaEriText();
1743 } else {
1744 return null;
1745 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001746 }
1747
1748 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001749 * Returns the CDMA MDN.
1750 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001751 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001752 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001753 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001754 final Phone phone = getPhone(subId);
1755 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1756 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001757 } else {
1758 return null;
1759 }
1760 }
1761
1762 /**
1763 * Returns the CDMA MIN.
1764 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001765 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001766 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001767 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001768 final Phone phone = getPhone(subId);
1769 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1770 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001771 } else {
1772 return null;
1773 }
1774 }
1775
1776 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001777 * Returns true if CDMA provisioning needs to run.
1778 */
1779 public boolean needsOtaServiceProvisioning() {
1780 return mPhone.needsOtaServiceProvisioning();
1781 }
1782
1783 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001784 * Sets the voice mail number of a given subId.
1785 */
1786 @Override
1787 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001788 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001789 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1790 new Pair<String, String>(alphaTag, number), new Integer(subId));
1791 return success;
1792 }
1793
1794 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001795 * Returns the unread count of voicemails
1796 */
1797 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001798 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001799 }
1800
1801 /**
1802 * Returns the unread count of voicemails for a subId
1803 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001804 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001805 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001806 final Phone phone = getPhone(subId);
1807 if (phone != null) {
1808 return phone.getVoiceMessageCount();
1809 } else {
1810 return 0;
1811 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001812 }
1813
1814 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001815 * Returns the data network type.
1816 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 *
1818 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1819 */
1820 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001821 public int getNetworkType() {
1822 final Phone phone = getPhone(getDefaultSubscription());
1823 if (phone != null) {
1824 return phone.getServiceState().getDataNetworkType();
1825 } else {
1826 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1827 }
Wink Saville36469e72014-06-11 15:17:00 -07001828 }
1829
1830 /**
1831 * Returns the network type for a subId
1832 */
1833 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001834 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1835 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1836 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1837 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001838
Sanket Padawe356d7632015-06-22 14:03:32 -07001839 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001840 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001841 return phone.getServiceState().getDataNetworkType();
1842 } else {
1843 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1844 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001845 }
1846
1847 /**
1848 * Returns the data network type
1849 */
1850 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001851 public int getDataNetworkType(String callingPackage) {
1852 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001853 }
1854
1855 /**
1856 * Returns the data network type for a subId
1857 */
1858 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001859 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1860 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1861 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1862 }
1863
Sanket Padawe356d7632015-06-22 14:03:32 -07001864 final Phone phone = getPhone(subId);
1865 if (phone != null) {
1866 return phone.getServiceState().getDataNetworkType();
1867 } else {
1868 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1869 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001870 }
1871
1872 /**
Wink Saville36469e72014-06-11 15:17:00 -07001873 * Returns the Voice network type for a subId
1874 */
1875 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001876 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1877 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1878 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1879 }
1880
Sanket Padawe356d7632015-06-22 14:03:32 -07001881 final Phone phone = getPhone(subId);
1882 if (phone != null) {
1883 return phone.getServiceState().getVoiceNetworkType();
1884 } else {
1885 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1886 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001887 }
1888
1889 /**
1890 * @return true if a ICC card is present
1891 */
1892 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001893 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001894 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001895 }
1896
1897 /**
1898 * @return true if a ICC card is present for a slotId
1899 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001900 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001901 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001902 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001903 final Phone phone = getPhone(subId[0]);
1904 if (subId != null && phone != null) {
1905 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001906 } else {
1907 return false;
1908 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 }
1910
1911 /**
1912 * Return if the current radio is LTE on CDMA. This
1913 * is a tri-state return value as for a period of time
1914 * the mode may be unknown.
1915 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001916 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001917 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001918 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001919 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 @Override
1921 public int getLteOnCdmaMode(String callingPackage) {
1922 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001923 }
1924
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001926 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1927 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1928 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1929 }
1930
Sanket Padawe356d7632015-06-22 14:03:32 -07001931 final Phone phone = getPhone(subId);
1932 if (phone == null) {
1933 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1934 } else {
1935 return phone.getLteOnCdmaMode();
1936 }
Wink Saville36469e72014-06-11 15:17:00 -07001937 }
1938
1939 public void setPhone(Phone phone) {
1940 mPhone = phone;
1941 }
1942
1943 /**
1944 * {@hide}
1945 * Returns Default subId, 0 in the case of single standby.
1946 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001947 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001948 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001949 }
1950
Wink Savilleb564aae2014-10-23 10:18:09 -07001951 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001952 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001953 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001954
1955 /**
1956 * @see android.telephony.TelephonyManager.WifiCallingChoices
1957 */
1958 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001959 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1960 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001961 }
1962
1963 /**
1964 * @see android.telephony.TelephonyManager.WifiCallingChoices
1965 */
1966 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001967 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1968 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1969 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001970 }
1971
Sailesh Nepald1e68152013-12-12 19:08:02 -08001972 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001973 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001974 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001975 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001976
Shishir Agrawal566b7612013-10-28 14:41:00 -07001977 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001978 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001979 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001980
1981 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001982 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1983 CMD_OPEN_CHANNEL, AID);
1984 if (DBG) log("iccOpenLogicalChannel: " + response);
1985 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001986 }
1987
1988 @Override
1989 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001990 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001991
1992 if (DBG) log("iccCloseLogicalChannel: " + channel);
1993 if (channel < 0) {
1994 return false;
1995 }
Jake Hambye994d462014-02-03 13:10:13 -08001996 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001997 if (DBG) log("iccCloseLogicalChannel: " + success);
1998 return success;
1999 }
2000
2001 @Override
2002 public String iccTransmitApduLogicalChannel(int channel, int cla,
2003 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07002004 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07002005
2006 if (DBG) {
2007 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
2008 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2009 " data=" + data);
2010 }
2011
2012 if (channel < 0) {
2013 return "";
2014 }
2015
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002016 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002017 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2018 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2019
Shishir Agrawal566b7612013-10-28 14:41:00 -07002020 // Append the returned status code to the end of the response payload.
2021 String s = Integer.toHexString(
2022 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002023 if (response.payload != null) {
2024 s = IccUtils.bytesToHexString(response.payload) + s;
2025 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002026 return s;
2027 }
Jake Hambye994d462014-02-03 13:10:13 -08002028
Evan Charltonc66da362014-05-16 14:06:40 -07002029 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002030 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2031 int p3, String data) {
2032 enforceModifyPermissionOrCarrierPrivilege();
2033
2034 if (DBG) {
2035 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2036 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2037 }
2038
2039 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2040 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2041 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2042
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002043 // Append the returned status code to the end of the response payload.
2044 String s = Integer.toHexString(
2045 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002046 if (response.payload != null) {
2047 s = IccUtils.bytesToHexString(response.payload) + s;
2048 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002049 return s;
2050 }
2051
2052 @Override
2053 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2054 String filePath) {
2055 enforceModifyPermissionOrCarrierPrivilege();
2056
2057 if (DBG) {
2058 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2059 p1 + " " + p2 + " " + p3 + ":" + filePath);
2060 }
2061
2062 IccIoResult response =
2063 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002064 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002065
2066 if (DBG) {
2067 log("Exchange SIM_IO [R]" + response);
2068 }
2069
2070 byte[] result = null;
2071 int length = 2;
2072 if (response.payload != null) {
2073 length = 2 + response.payload.length;
2074 result = new byte[length];
2075 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2076 } else {
2077 result = new byte[length];
2078 }
2079
2080 result[length - 1] = (byte) response.sw2;
2081 result[length - 2] = (byte) response.sw1;
2082 return result;
2083 }
2084
2085 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002086 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002087 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002088
2089 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2090 if (response.payload == null) {
2091 return "";
2092 }
2093
2094 // Append the returned status code to the end of the response payload.
2095 String s = Integer.toHexString(
2096 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2097 s = IccUtils.bytesToHexString(response.payload) + s;
2098 return s;
2099 }
2100
Jake Hambye994d462014-02-03 13:10:13 -08002101 /**
2102 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2103 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2104 *
2105 * @param itemID the ID of the item to read
2106 * @return the NV item as a String, or null on error.
2107 */
2108 @Override
2109 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002110 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002111 if (DBG) log("nvReadItem: item " + itemID);
2112 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2113 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2114 return value;
2115 }
2116
2117 /**
2118 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2119 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2120 *
2121 * @param itemID the ID of the item to read
2122 * @param itemValue the value to write, as a String
2123 * @return true on success; false on any failure
2124 */
2125 @Override
2126 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002127 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002128 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2129 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2130 new Pair<Integer, String>(itemID, itemValue));
2131 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2132 return success;
2133 }
2134
2135 /**
2136 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2137 * Used for device configuration by some CDMA operators.
2138 *
2139 * @param preferredRoamingList byte array containing the new PRL
2140 * @return true on success; false on any failure
2141 */
2142 @Override
2143 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002144 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002145 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2146 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2147 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2148 return success;
2149 }
2150
2151 /**
2152 * Perform the specified type of NV config reset.
2153 * Used for device configuration by some CDMA operators.
2154 *
2155 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2156 * @return true on success; false on any failure
2157 */
2158 @Override
2159 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002160 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002161 if (DBG) log("nvResetConfig: type " + resetType);
2162 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2163 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2164 return success;
2165 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002166
2167 /**
Wink Saville36469e72014-06-11 15:17:00 -07002168 * {@hide}
2169 * Returns Default sim, 0 in the case of single standby.
2170 */
2171 public int getDefaultSim() {
2172 //TODO Need to get it from Telephony Devcontroller
2173 return 0;
2174 }
2175
Svet Ganovb320e182015-04-16 12:30:10 -07002176 public String[] getPcscfAddress(String apnType, String callingPackage) {
2177 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2178 return new String[0];
2179 }
2180
2181
ram87fca6f2014-07-18 18:58:44 +05302182 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002183 }
2184
2185 public void setImsRegistrationState(boolean registered) {
2186 enforceModifyPermission();
2187 mPhone.setImsRegistrationState(registered);
2188 }
2189
2190 /**
Stuart Scott54788802015-03-30 13:18:01 -07002191 * Set the network selection mode to automatic.
2192 *
2193 */
2194 @Override
2195 public void setNetworkSelectionModeAutomatic(int subId) {
2196 enforceModifyPermissionOrCarrierPrivilege();
2197 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2198 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2199 }
2200
2201 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002202 * Set the network selection mode to manual with the selected carrier.
2203 */
2204 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002205 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2206 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002207 enforceModifyPermissionOrCarrierPrivilege();
2208 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002209 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2210 persistSelection);
2211 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002212 }
2213
2214 /**
2215 * Scans for available networks.
2216 */
2217 @Override
2218 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2219 enforceModifyPermissionOrCarrierPrivilege();
2220 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2221 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2222 CMD_PERFORM_NETWORK_SCAN, null, subId);
2223 return result;
2224 }
2225
2226 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002227 * Get the calculated preferred network type.
2228 * Used for debugging incorrect network type.
2229 *
2230 * @return the preferred network type, defined in RILConstants.java.
2231 */
2232 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002233 public int getCalculatedPreferredNetworkType(String callingPackage) {
2234 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2235 return RILConstants.PREFERRED_NETWORK_MODE;
2236 }
2237
Amit Mahajan43330e02014-11-18 11:54:45 -08002238 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002239 }
2240
2241 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002242 * Get the preferred network type.
2243 * Used for device configuration by some CDMA operators.
2244 *
2245 * @return the preferred network type, defined in RILConstants.java.
2246 */
2247 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002248 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002249 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002250 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002251 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002252 int networkType = (result != null ? result[0] : -1);
2253 if (DBG) log("getPreferredNetworkType: " + networkType);
2254 return networkType;
2255 }
2256
2257 /**
2258 * Set the preferred network type.
2259 * Used for device configuration by some CDMA operators.
2260 *
2261 * @param networkType the preferred network type, defined in RILConstants.java.
2262 * @return true on success; false on any failure.
2263 */
2264 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002265 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002266 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002267 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2268 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002269 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002270 if (success) {
2271 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002272 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002273 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002274 return success;
2275 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002276
2277 /**
Junda Liu475951f2014-11-07 16:45:03 -08002278 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2279 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2280 * tethering.
2281 *
2282 * @return 0: Not required. 1: required. 2: Not set.
2283 * @hide
2284 */
2285 @Override
2286 public int getTetherApnRequired() {
2287 enforceModifyPermissionOrCarrierPrivilege();
2288 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2289 Settings.Global.TETHER_DUN_REQUIRED, 2);
2290 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2291 // config_tether_apndata.
2292 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2293 dunRequired = 1;
2294 }
2295 return dunRequired;
2296 }
2297
2298 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002299 * Set mobile data enabled
2300 * Used by the user through settings etc to turn on/off mobile data
2301 *
2302 * @param enable {@code true} turn turn data on, else {@code false}
2303 */
2304 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002305 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002306 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002307 int phoneId = mSubscriptionController.getPhoneId(subId);
2308 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2309 Phone phone = PhoneFactory.getPhone(phoneId);
2310 if (phone != null) {
2311 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2312 phone.setDataEnabled(enable);
2313 } else {
2314 loge("setDataEnabled: no phone for subId=" + subId);
2315 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002316 }
2317
2318 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002319 * Get whether mobile data is enabled.
2320 *
2321 * Note that this used to be available from ConnectivityService, gated by
2322 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2323 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002324 *
2325 * @return {@code true} if data is enabled else {@code false}
2326 */
2327 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002328 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002329 try {
2330 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2331 null);
2332 } catch (Exception e) {
2333 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2334 null);
2335 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002336 int phoneId = mSubscriptionController.getPhoneId(subId);
2337 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2338 Phone phone = PhoneFactory.getPhone(phoneId);
2339 if (phone != null) {
2340 boolean retVal = phone.getDataEnabled();
2341 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2342 return retVal;
2343 } else {
2344 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2345 return false;
2346 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002347 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002348
2349 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002350 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002351 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002352 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002353 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002354 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2355 }
2356 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002357 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002358 }
Junda Liu29340342014-07-10 15:23:27 -07002359
2360 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002361 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002362 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002363 if (card == null) {
2364 loge("checkCarrierPrivilegesForPackage: No UICC");
2365 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2366 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002367 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2368 }
2369
2370 @Override
2371 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2372 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2373 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2374 UiccCard card = UiccController.getInstance().getUiccCard(i);
2375 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002376 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002377 continue;
2378 }
2379
2380 result = card.getCarrierPrivilegeStatus(
2381 mPhone.getContext().getPackageManager(), pkgName);
2382 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2383 break;
2384 }
2385 }
2386
2387 return result;
Junda Liu29340342014-07-10 15:23:27 -07002388 }
Derek Tan89e89d42014-07-08 17:00:10 -07002389
2390 @Override
Junda Liue64de782015-04-16 17:19:16 -07002391 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2392 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2393 loge("phoneId " + phoneId + " is not valid.");
2394 return null;
2395 }
2396 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002397 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002398 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002399 return null ;
2400 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002401 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002402 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002403 }
2404
Wink Savilleb564aae2014-10-23 10:18:09 -07002405 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002406 final Phone phone = getPhone(subId);
2407 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002408 if (card == null) {
2409 loge("getIccId: No UICC");
2410 return null;
2411 }
2412 String iccId = card.getIccId();
2413 if (TextUtils.isEmpty(iccId)) {
2414 loge("getIccId: ICC ID is null or empty.");
2415 return null;
2416 }
2417 return iccId;
2418 }
2419
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002420 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002421 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2422 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002423 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002424
Jeff Sharkey85190e62014-12-05 09:40:12 -08002425 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002426 final Phone phone = getPhone(subId);
2427 if (phone == null) {
2428 return false;
2429 }
2430 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002431
2432 if (DBG_MERGE) {
2433 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2434 + subscriberId + " to " + number);
2435 }
2436
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002437 if (TextUtils.isEmpty(iccId)) {
2438 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002439 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002440
Jeff Sharkey85190e62014-12-05 09:40:12 -08002441 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2442
2443 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002444 if (alphaTag == null) {
2445 editor.remove(alphaTagPrefKey);
2446 } else {
2447 editor.putString(alphaTagPrefKey, alphaTag);
2448 }
2449
Jeff Sharkey85190e62014-12-05 09:40:12 -08002450 // Record both the line number and IMSI for this ICCID, since we need to
2451 // track all merged IMSIs based on line number
2452 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2453 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002454 if (number == null) {
2455 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002456 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002457 } else {
2458 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002459 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002460 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002461
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002462 editor.commit();
2463 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002464 }
2465
2466 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002467 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002468 // This is open to apps with WRITE_SMS.
2469 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002470 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002471 return null;
2472 }
Derek Tan97ebb422014-09-05 16:55:38 -07002473
2474 String iccId = getIccId(subId);
2475 if (iccId != null) {
2476 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002477 if (DBG_MERGE) {
2478 log("getLine1NumberForDisplay returning " +
2479 mTelephonySharedPreferences.getString(numberPrefKey, null));
2480 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002481 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002482 }
Jack Yue7939d82015-12-10 16:04:02 -08002483 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002484 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002485 }
2486
2487 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002488 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2489 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2490 return null;
2491 }
Derek Tan97ebb422014-09-05 16:55:38 -07002492
2493 String iccId = getIccId(subId);
2494 if (iccId != null) {
2495 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002496 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002497 }
Derek Tan97ebb422014-09-05 16:55:38 -07002498 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002499 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002500
2501 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002502 public String[] getMergedSubscriberIds(String callingPackage) {
2503 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2504 return null;
2505 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002506 final Context context = mPhone.getContext();
2507 final TelephonyManager tele = TelephonyManager.from(context);
2508 final SubscriptionManager sub = SubscriptionManager.from(context);
2509
2510 // Figure out what subscribers are currently active
2511 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002512 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2513 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2514 final long identity = Binder.clearCallingIdentity();
2515 try {
2516 final int[] subIds = sub.getActiveSubscriptionIdList();
2517 for (int subId : subIds) {
2518 activeSubscriberIds.add(tele.getSubscriberId(subId));
2519 }
2520 } finally {
2521 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002522 }
2523
2524 // First pass, find a number override for an active subscriber
2525 String mergeNumber = null;
2526 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2527 for (String key : prefs.keySet()) {
2528 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2529 final String subscriberId = (String) prefs.get(key);
2530 if (activeSubscriberIds.contains(subscriberId)) {
2531 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2532 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2533 mergeNumber = (String) prefs.get(numberKey);
2534 if (DBG_MERGE) {
2535 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2536 + " for active subscriber " + subscriberId);
2537 }
2538 if (!TextUtils.isEmpty(mergeNumber)) {
2539 break;
2540 }
2541 }
2542 }
2543 }
2544
2545 // Shortcut when no active merged subscribers
2546 if (TextUtils.isEmpty(mergeNumber)) {
2547 return null;
2548 }
2549
2550 // Second pass, find all subscribers under that line override
2551 final ArraySet<String> result = new ArraySet<>();
2552 for (String key : prefs.keySet()) {
2553 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2554 final String number = (String) prefs.get(key);
2555 if (mergeNumber.equals(number)) {
2556 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2557 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2558 final String subscriberId = (String) prefs.get(subscriberKey);
2559 if (!TextUtils.isEmpty(subscriberId)) {
2560 result.add(subscriberId);
2561 }
2562 }
2563 }
2564 }
2565
2566 final String[] resultArray = result.toArray(new String[result.size()]);
2567 Arrays.sort(resultArray);
2568 if (DBG_MERGE) {
2569 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2570 }
2571 return resultArray;
2572 }
2573
2574 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002575 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002576 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002577 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002578 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002579
2580 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002581 public boolean setRoamingOverride(List<String> gsmRoamingList,
2582 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2583 List<String> cdmaNonRoamingList) {
2584 enforceCarrierPrivilege();
2585 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2586 cdmaNonRoamingList);
2587 }
2588
2589 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002590 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2591 enforceModifyPermission();
2592
2593 int returnValue = 0;
2594 try {
2595 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2596 if(result.exception == null) {
2597 if (result.result != null) {
2598 byte[] responseData = (byte[])(result.result);
2599 if(responseData.length > oemResp.length) {
2600 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2601 responseData.length + "bytes. Buffer Size is " +
2602 oemResp.length + "bytes.");
2603 }
2604 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2605 returnValue = responseData.length;
2606 }
2607 } else {
2608 CommandException ex = (CommandException) result.exception;
2609 returnValue = ex.getCommandError().ordinal();
2610 if(returnValue > 0) returnValue *= -1;
2611 }
2612 } catch (RuntimeException e) {
2613 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2614 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2615 if(returnValue > 0) returnValue *= -1;
2616 }
2617
2618 return returnValue;
2619 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002620
2621 @Override
2622 public void setRadioCapability(RadioAccessFamily[] rafs) {
2623 try {
2624 ProxyController.getInstance().setRadioCapability(rafs);
2625 } catch (RuntimeException e) {
2626 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2627 }
2628 }
2629
2630 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002631 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2632 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2633 return RadioAccessFamily.RAF_UNKNOWN;
2634 }
2635
Wink Saville5d475dd2014-10-17 15:00:58 -07002636 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2637 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002638
2639 @Override
2640 public void enableVideoCalling(boolean enable) {
2641 enforceModifyPermission();
2642 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2643 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2644 editor.commit();
2645 }
2646
2647 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002648 public boolean isVideoCallingEnabled(String callingPackage) {
2649 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2650 return false;
2651 }
2652
Andrew Lee77527ac2014-10-21 16:57:39 -07002653 // Check the user preference and the system-level IMS setting. Even if the user has
2654 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2655 // In the long run, we may instead need to check if there exists a connection service
2656 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002657 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2658 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2659 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002660 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002661
Andrew Leea1239f22015-03-02 17:44:07 -08002662 @Override
2663 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002664 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002665 }
2666
2667 @Override
2668 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002669 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002670 }
2671
Andrew Lee9431b832015-03-09 18:46:45 -07002672 @Override
2673 public boolean isTtyModeSupported() {
2674 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2675 TelephonyManager telephonyManager =
2676 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2677 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2678 }
2679
2680 @Override
2681 public boolean isHearingAidCompatibilitySupported() {
2682 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2683 }
2684
Sanket Padawe7310cc72015-01-14 09:53:20 -08002685 /**
2686 * Returns the unique device ID of phone, for example, the IMEI for
2687 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2688 *
2689 * <p>Requires Permission:
2690 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2691 */
2692 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002693 public String getDeviceId(String callingPackage) {
2694 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2695 return null;
2696 }
2697
Sanket Padawe7310cc72015-01-14 09:53:20 -08002698 final Phone phone = PhoneFactory.getPhone(0);
2699 if (phone != null) {
2700 return phone.getDeviceId();
2701 } else {
2702 return null;
2703 }
2704 }
2705
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002706 /*
2707 * {@hide}
2708 * Returns the IMS Registration Status
2709 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002710 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002711 public boolean isImsRegistered() {
2712 return mPhone.isImsRegistered();
2713 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002714
2715 @Override
2716 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2717 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2718 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002719
Nathan Haroldc55097a2015-03-11 18:14:50 -07002720 /*
2721 * {@hide}
2722 * Returns the IMS Registration Status
2723 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002724 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002725 return mPhone.isWifiCallingEnabled();
2726 }
2727
2728 /*
2729 * {@hide}
2730 * Returns the IMS Registration Status
2731 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002732 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002733 return mPhone.isVolteEnabled();
2734 }
Svet Ganovb320e182015-04-16 12:30:10 -07002735
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002736 /*
2737 * {@hide} Returns the IMS Registration Status
2738 */
2739 public boolean isVideoTelephonyAvailable() {
2740 return mPhone.isVideoEnabled();
2741 }
2742
Svet Ganovb320e182015-04-16 12:30:10 -07002743 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002744 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002745 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002746 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2747
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002748 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002749 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002750 } catch (SecurityException e) {
2751 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2752 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002753 }
Svet Ganovb320e182015-04-16 12:30:10 -07002754
2755 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2756 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2757 return false;
2758 }
2759
2760 return true;
2761 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002762
Makoto Onukifee69342015-06-29 14:44:50 -07002763 /**
Jack Yue7939d82015-12-10 16:04:02 -08002764 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002765 */
2766 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002767 // Default SMS app can always read it.
2768 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2769 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2770 return true;
2771 }
2772 try {
2773 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002774 } catch (SecurityException readPhoneStateSecurityException) {
2775 try {
2776 // Can be read with READ_SMS too.
2777 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2778 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2779 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2780 } catch (SecurityException readSmsSecurityException) {
2781 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2782 // permissions
2783 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2784 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2785 " or " + android.Manifest.permission.READ_SMS + ".");
2786 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002787 }
Makoto Onukifee69342015-06-29 14:44:50 -07002788 }
2789
Stuart Scott8eef64f2015-04-08 15:13:54 -07002790 @Override
2791 public void factoryReset(int subId) {
2792 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002793 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2794 return;
2795 }
2796
Svet Ganovcc087f82015-05-12 20:35:54 -07002797 final long identity = Binder.clearCallingIdentity();
2798 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002799 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2800 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002801 // Enable data
2802 setDataEnabled(subId, true);
2803 // Set network selection mode to automatic
2804 setNetworkSelectionModeAutomatic(subId);
2805 // Set preferred mobile network type to the best available
2806 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2807 // Turn off roaming
2808 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2809 }
2810 } finally {
2811 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002812 }
2813 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002814
2815 @Override
2816 public String getLocaleFromDefaultSim() {
2817 // We query all subscriptions instead of just the active ones, because
2818 // this might be called early on in the provisioning flow when the
2819 // subscriptions potentially aren't active yet.
2820 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2821 if (slist == null || slist.isEmpty()) {
2822 return null;
2823 }
2824
2825 // This function may be called very early, say, from the setup wizard, at
2826 // which point we won't have a default subscription set. If that's the case
2827 // we just choose the first, which will be valid in "most cases".
2828 final int defaultSubId = getDefaultSubscription();
2829 SubscriptionInfo info = null;
2830 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2831 info = slist.get(0);
2832 } else {
2833 for (SubscriptionInfo item : slist) {
2834 if (item.getSubscriptionId() == defaultSubId) {
2835 info = item;
2836 break;
2837 }
2838 }
2839
2840 if (info == null) {
2841 return null;
2842 }
2843 }
2844
2845 // Try and fetch the locale from the carrier properties or from the SIM language
2846 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002847 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002848 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002849 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002850 if (defaultPhone != null) {
2851 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2852 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002853 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002854 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2855 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002856 } else {
2857 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002858 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002859 }
2860 }
2861
Narayan Kamath011676f2015-07-29 12:04:08 +01002862 // The SIM language preferences only store a language (e.g. fr = French), not an
2863 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2864 // the SIM and carrier preferences does not include a country we add the country
2865 // determined from the SIM MCC to provide an exact locale.
2866 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002867 if (mccLocale != null) {
2868 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2869 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002870 }
2871
Tony Hill183b2de2015-06-24 14:53:58 +01002872 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002873 return null;
2874 }
2875
2876 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2877 final long identity = Binder.clearCallingIdentity();
2878 try {
2879 return mSubscriptionController.getAllSubInfoList(
2880 mPhone.getContext().getOpPackageName());
2881 } finally {
2882 Binder.restoreCallingIdentity(identity);
2883 }
2884 }
2885
2886 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2887 final long identity = Binder.clearCallingIdentity();
2888 try {
2889 return mSubscriptionController.getActiveSubscriptionInfoList(
2890 mPhone.getContext().getOpPackageName());
2891 } finally {
2892 Binder.restoreCallingIdentity(identity);
2893 }
2894 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002895
2896 /**
2897 * {@hide}
2898 * Returns the modem stats
2899 */
2900 @Override
2901 public ModemActivityInfo getModemActivityInfo() {
2902 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2903 }
Jack Yue7939d82015-12-10 16:04:02 -08002904
2905 /**
2906 * {@hide}
2907 * Returns the service state information on specified subscription.
2908 */
2909 @Override
2910 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2911
2912 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2913 return null;
2914 }
2915
2916 final Phone phone = getPhone(subId);
2917 if (phone == null) {
2918 return null;
2919 }
2920
2921 return phone.getServiceState();
2922 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002923}