blob: 68b68234b3d5f086e2c28874bf6f04a27664b046 [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");
466 request.result = new IccIoResult(0x6F, 0, (byte[])null);
467 synchronized (request) {
468 request.notifyAll();
469 }
470 } else {
471 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
472 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
473 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 break;
475
476 case EVENT_OPEN_CHANNEL_DONE:
477 ar = (AsyncResult) msg.obj;
478 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700479 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700480 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700481 int[] result = (int[]) ar.result;
482 int channelId = result[0];
483 byte[] selectResponse = null;
484 if (result.length > 1) {
485 selectResponse = new byte[result.length - 1];
486 for (int i = 1; i < result.length; ++i) {
487 selectResponse[i - 1] = (byte) result[i];
488 }
489 }
490 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700491 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 if (ar.result == null) {
494 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700496 if (ar.exception != null) {
497 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
498 }
499
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700500 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700501 if (ar.exception instanceof CommandException) {
502 CommandException.Error error =
503 ((CommandException) (ar.exception)).getCommandError();
504 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700505 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700506 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700507 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700508 }
509 }
510 openChannelResp = new IccOpenLogicalChannelResponse(
511 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700513 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 synchronized (request) {
515 request.notifyAll();
516 }
517 break;
518
519 case CMD_CLOSE_CHANNEL:
520 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700521 if (uiccCard == null) {
522 loge("iccCloseLogicalChannel: No UICC");
523 request.result = new IccIoResult(0x6F, 0, (byte[])null);
524 synchronized (request) {
525 request.notifyAll();
526 }
527 } else {
528 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
529 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
530 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 break;
532
533 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800534 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
535 break;
536
537 case CMD_NV_READ_ITEM:
538 request = (MainThreadRequest) msg.obj;
539 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
540 mPhone.nvReadItem((Integer) request.argument, onCompleted);
541 break;
542
543 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 ar = (AsyncResult) msg.obj;
545 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800546 if (ar.exception == null && ar.result != null) {
547 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800549 request.result = "";
550 if (ar.result == null) {
551 loge("nvReadItem: Empty response");
552 } else if (ar.exception instanceof CommandException) {
553 loge("nvReadItem: CommandException: " +
554 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800556 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 }
558 }
559 synchronized (request) {
560 request.notifyAll();
561 }
562 break;
563
Jake Hambye994d462014-02-03 13:10:13 -0800564 case CMD_NV_WRITE_ITEM:
565 request = (MainThreadRequest) msg.obj;
566 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
567 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
568 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
569 break;
570
571 case EVENT_NV_WRITE_ITEM_DONE:
572 handleNullReturnEvent(msg, "nvWriteItem");
573 break;
574
575 case CMD_NV_WRITE_CDMA_PRL:
576 request = (MainThreadRequest) msg.obj;
577 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
578 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
579 break;
580
581 case EVENT_NV_WRITE_CDMA_PRL_DONE:
582 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
583 break;
584
585 case CMD_NV_RESET_CONFIG:
586 request = (MainThreadRequest) msg.obj;
587 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
588 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
589 break;
590
591 case EVENT_NV_RESET_CONFIG_DONE:
592 handleNullReturnEvent(msg, "nvResetConfig");
593 break;
594
Jake Hamby7c27be32014-03-03 13:25:59 -0800595 case CMD_GET_PREFERRED_NETWORK_TYPE:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700598 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800599 break;
600
601 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
602 ar = (AsyncResult) msg.obj;
603 request = (MainThreadRequest) ar.userObj;
604 if (ar.exception == null && ar.result != null) {
605 request.result = ar.result; // Integer
606 } else {
607 request.result = -1;
608 if (ar.result == null) {
609 loge("getPreferredNetworkType: Empty response");
610 } else if (ar.exception instanceof CommandException) {
611 loge("getPreferredNetworkType: CommandException: " +
612 ar.exception);
613 } else {
614 loge("getPreferredNetworkType: Unknown exception");
615 }
616 }
617 synchronized (request) {
618 request.notifyAll();
619 }
620 break;
621
622 case CMD_SET_PREFERRED_NETWORK_TYPE:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
625 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700626 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800627 break;
628
629 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
630 handleNullReturnEvent(msg, "setPreferredNetworkType");
631 break;
632
Steven Liu4bf01bc2014-07-17 11:05:29 -0500633 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
634 request = (MainThreadRequest)msg.obj;
635 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
636 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
637 break;
638
639 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
640 ar = (AsyncResult)msg.obj;
641 request = (MainThreadRequest)ar.userObj;
642 request.result = ar;
643 synchronized (request) {
644 request.notifyAll();
645 }
646 break;
647
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800648 case CMD_SET_VOICEMAIL_NUMBER:
649 request = (MainThreadRequest) msg.obj;
650 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
651 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800652 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
653 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800654 break;
655
656 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
657 handleNullReturnEvent(msg, "setVoicemailNumber");
658 break;
659
Stuart Scott54788802015-03-30 13:18:01 -0700660 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
661 request = (MainThreadRequest) msg.obj;
662 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
663 request);
664 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
665 break;
666
667 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
668 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
669 break;
670
Shishir Agrawal302c8692015-06-19 13:49:39 -0700671 case CMD_PERFORM_NETWORK_SCAN:
672 request = (MainThreadRequest) msg.obj;
673 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
674 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
675 break;
676
677 case EVENT_PERFORM_NETWORK_SCAN_DONE:
678 ar = (AsyncResult) msg.obj;
679 request = (MainThreadRequest) ar.userObj;
680 CellNetworkScanResult cellScanResult;
681 if (ar.exception == null && ar.result != null) {
682 cellScanResult = new CellNetworkScanResult(
683 CellNetworkScanResult.STATUS_SUCCESS,
684 (List<OperatorInfo>) ar.result);
685 } else {
686 if (ar.result == null) {
687 loge("getCellNetworkScanResults: Empty response");
688 }
689 if (ar.exception != null) {
690 loge("getCellNetworkScanResults: Exception: " + ar.exception);
691 }
692 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
693 if (ar.exception instanceof CommandException) {
694 CommandException.Error error =
695 ((CommandException) (ar.exception)).getCommandError();
696 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
697 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
698 } else if (error == CommandException.Error.GENERIC_FAILURE) {
699 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
700 }
701 }
702 cellScanResult = new CellNetworkScanResult(errorCode, null);
703 }
704 request.result = cellScanResult;
705 synchronized (request) {
706 request.notifyAll();
707 }
708 break;
709
710 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
711 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700712 ManualNetworkSelectionArgument selArg =
713 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700714 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
715 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700716 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
717 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700718 break;
719
720 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
721 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
722 break;
723
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700724 case CMD_GET_MODEM_ACTIVITY_INFO:
725 request = (MainThreadRequest) msg.obj;
726 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700727 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700728 break;
729
730 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
731 ar = (AsyncResult) msg.obj;
732 request = (MainThreadRequest) ar.userObj;
733 if (ar.exception == null && ar.result != null) {
734 request.result = ar.result;
735 } else {
736 if (ar.result == null) {
737 loge("queryModemActivityInfo: Empty response");
738 } else if (ar.exception instanceof CommandException) {
739 loge("queryModemActivityInfo: CommandException: " +
740 ar.exception);
741 } else {
742 loge("queryModemActivityInfo: Unknown exception");
743 }
744 }
Amit Mahajand4766222016-01-28 15:28:28 -0800745 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
746 if (request.result == null) {
747 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
748 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700749 synchronized (request) {
750 request.notifyAll();
751 }
752 break;
753
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700754 default:
755 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
756 break;
757 }
758 }
Jake Hambye994d462014-02-03 13:10:13 -0800759
760 private void handleNullReturnEvent(Message msg, String command) {
761 AsyncResult ar = (AsyncResult) msg.obj;
762 MainThreadRequest request = (MainThreadRequest) ar.userObj;
763 if (ar.exception == null) {
764 request.result = true;
765 } else {
766 request.result = false;
767 if (ar.exception instanceof CommandException) {
768 loge(command + ": CommandException: " + ar.exception);
769 } else {
770 loge(command + ": Unknown exception");
771 }
772 }
773 synchronized (request) {
774 request.notifyAll();
775 }
776 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700777 }
778
779 /**
780 * Posts the specified command to be executed on the main thread,
781 * waits for the request to complete, and returns the result.
782 * @see #sendRequestAsync
783 */
784 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800785 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700786 }
787
788 /**
789 * Posts the specified command to be executed on the main thread,
790 * waits for the request to complete, and returns the result.
791 * @see #sendRequestAsync
792 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800793 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700794 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
795 throw new RuntimeException("This method will deadlock if called from the main thread.");
796 }
797
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800798 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700799 Message msg = mMainThreadHandler.obtainMessage(command, request);
800 msg.sendToTarget();
801
802 // Wait for the request to complete
803 synchronized (request) {
804 while (request.result == null) {
805 try {
806 request.wait();
807 } catch (InterruptedException e) {
808 // Do nothing, go back and wait until the request is complete
809 }
810 }
811 }
812 return request.result;
813 }
814
815 /**
816 * Asynchronous ("fire and forget") version of sendRequest():
817 * Posts the specified command to be executed on the main thread, and
818 * returns immediately.
819 * @see #sendRequest
820 */
821 private void sendRequestAsync(int command) {
822 mMainThreadHandler.sendEmptyMessage(command);
823 }
824
825 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700826 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
827 * @see {@link #sendRequest(int,Object)}
828 */
829 private void sendRequestAsync(int command, Object argument) {
830 MainThreadRequest request = new MainThreadRequest(argument);
831 Message msg = mMainThreadHandler.obtainMessage(command, request);
832 msg.sendToTarget();
833 }
834
835 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 * Initialize the singleton PhoneInterfaceManager instance.
837 * This is only done once, at startup, from PhoneApp.onCreate().
838 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700839 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 synchronized (PhoneInterfaceManager.class) {
841 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700842 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700843 } else {
844 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
845 }
846 return sInstance;
847 }
848 }
849
850 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700851 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 mApp = app;
853 mPhone = phone;
854 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700855 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
857 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700858 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700859 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800860 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800861
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700862 publish();
863 }
864
865 private void publish() {
866 if (DBG) log("publish: " + this);
867
868 ServiceManager.addService("phone", this);
869 }
870
Stuart Scott584921c2015-01-15 17:10:34 -0800871 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800872 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
873 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800874 }
875
Wink Saville36469e72014-06-11 15:17:00 -0700876 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700877 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800878 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700879 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700880 //
881 // Implementation of the ITelephony interface.
882 //
883
884 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700885 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700886 }
887
Wink Savilleb564aae2014-10-23 10:18:09 -0700888 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700889 if (DBG) log("dial: " + number);
890 // No permission check needed here: This is just a wrapper around the
891 // ACTION_DIAL intent, which is available to any app since it puts up
892 // the UI before it does anything.
893
894 String url = createTelUrl(number);
895 if (url == null) {
896 return;
897 }
898
899 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700900 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700901 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
902 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
903 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
904 mApp.startActivity(intent);
905 }
906 }
907
908 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700909 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700910 }
911
Wink Savilleb564aae2014-10-23 10:18:09 -0700912 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700913 if (DBG) log("call: " + number);
914
915 // This is just a wrapper around the ACTION_CALL intent, but we still
916 // need to do a permission check since we're calling startActivity()
917 // from the context of the phone app.
918 enforceCallPermission();
919
920 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
921 != AppOpsManager.MODE_ALLOWED) {
922 return;
923 }
924
925 String url = createTelUrl(number);
926 if (url == null) {
927 return;
928 }
929
Wink Saville08874612014-08-31 19:19:58 -0700930 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100931 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800932 if (slist != null) {
933 for (SubscriptionInfo subInfoRecord : slist) {
934 if (subInfoRecord.getSubscriptionId() == subId) {
935 isValid = true;
936 break;
937 }
Wink Saville08874612014-08-31 19:19:58 -0700938 }
939 }
940 if (isValid == false) {
941 return;
942 }
943
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700944 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700945 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
947 mApp.startActivity(intent);
948 }
949
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700950 /**
951 * End a call based on call state
952 * @return true is a call was ended
953 */
954 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700955 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700956 }
957
958 /**
959 * End a call based on the call state of the subId
960 * @return true is a call was ended
961 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700962 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800964 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 }
966
967 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700968 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700969 }
970
Wink Savilleb564aae2014-10-23 10:18:09 -0700971 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700972 if (DBG) log("answerRingingCall...");
973 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
974 // but that can probably wait till the big TelephonyManager API overhaul.
975 // For now, protect this call with the MODIFY_PHONE_STATE permission.
976 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800977 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700978 }
979
980 /**
981 * Make the actual telephony calls to implement answerRingingCall().
982 * This should only be called from the main thread of the Phone app.
983 * @see #answerRingingCall
984 *
985 * TODO: it would be nice to return true if we answered the call, or
986 * false if there wasn't actually a ringing incoming call, or some
987 * other error occurred. (In other words, pass back the return value
988 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
989 * But that would require calling this method via sendRequest() rather
990 * than sendRequestAsync(), and right now we don't actually *need* that
991 * return value, so let's just return void for now.
992 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700993 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700994 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700995 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700996 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
997 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700998 if (hasActiveCall && hasHoldingCall) {
999 // Both lines are in use!
1000 // TODO: provide a flag to let the caller specify what
1001 // policy to use if both lines are in use. (The current
1002 // behavior is hardwired to "answer incoming, end ongoing",
1003 // which is how the CALL button is specced to behave.)
1004 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1005 return;
1006 } else {
1007 // answerCall() will automatically hold the current active
1008 // call, if there is one.
1009 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1010 return;
1011 }
1012 } else {
1013 // No call was ringing.
1014 return;
1015 }
1016 }
1017
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001019 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001021 public void silenceRinger() {
1022 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001023 }
1024
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001025 @Override
1026 public boolean isOffhook(String callingPackage) {
1027 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001028 }
1029
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001030 @Override
1031 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1032 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1033 return false;
1034 }
1035
Sanket Padawe356d7632015-06-22 14:03:32 -07001036 final Phone phone = getPhone(subId);
1037 if (phone != null) {
1038 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1039 } else {
1040 return false;
1041 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001042 }
1043
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001044 @Override
1045 public boolean isRinging(String callingPackage) {
1046 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001047 }
1048
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001049 @Override
1050 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1051 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1052 return false;
1053 }
1054
Sanket Padawe356d7632015-06-22 14:03:32 -07001055 final Phone phone = getPhone(subId);
1056 if (phone != null) {
1057 return (phone.getState() == PhoneConstants.State.RINGING);
1058 } else {
1059 return false;
1060 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 }
1062
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001063 @Override
1064 public boolean isIdle(String callingPackage) {
1065 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001066 }
1067
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001068 @Override
1069 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1070 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1071 return false;
1072 }
1073
Sanket Padawe356d7632015-06-22 14:03:32 -07001074 final Phone phone = getPhone(subId);
1075 if (phone != null) {
1076 return (phone.getState() == PhoneConstants.State.IDLE);
1077 } else {
1078 return false;
1079 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 }
1081
Svet Ganovb320e182015-04-16 12:30:10 -07001082 public boolean isSimPinEnabled(String callingPackage) {
1083 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1084 return false;
1085 }
1086
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001087 return (PhoneGlobals.getInstance().isSimPinEnabled());
1088 }
1089
1090 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001091 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001092 }
1093
Wink Savilleb564aae2014-10-23 10:18:09 -07001094 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001095 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001096 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1097 }
1098
1099 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001100 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001101 }
1102
Wink Savilleb564aae2014-10-23 10:18:09 -07001103 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001104 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001105 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1106 }
1107
1108 /** {@hide} */
1109 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001110 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001111 }
1112
Wink Savilleb564aae2014-10-23 10:18:09 -07001113 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001114 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001115 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001116 checkSimPin.start();
1117 return checkSimPin.unlockSim(null, pin);
1118 }
1119
Wink Saville9de0f752013-10-22 19:04:03 -07001120 /** {@hide} */
1121 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001122 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001123 }
1124
Wink Savilleb564aae2014-10-23 10:18:09 -07001125 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001127 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 checkSimPuk.start();
1129 return checkSimPuk.unlockSim(puk, pin);
1130 }
1131
1132 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001133 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001134 * a synchronous one.
1135 */
1136 private static class UnlockSim extends Thread {
1137
1138 private final IccCard mSimCard;
1139
1140 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001141 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1142 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001143
1144 // For replies from SimCard interface
1145 private Handler mHandler;
1146
1147 // For async handler to identify request type
1148 private static final int SUPPLY_PIN_COMPLETE = 100;
1149
1150 public UnlockSim(IccCard simCard) {
1151 mSimCard = simCard;
1152 }
1153
1154 @Override
1155 public void run() {
1156 Looper.prepare();
1157 synchronized (UnlockSim.this) {
1158 mHandler = new Handler() {
1159 @Override
1160 public void handleMessage(Message msg) {
1161 AsyncResult ar = (AsyncResult) msg.obj;
1162 switch (msg.what) {
1163 case SUPPLY_PIN_COMPLETE:
1164 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1165 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001166 mRetryCount = msg.arg1;
1167 if (ar.exception != null) {
1168 if (ar.exception instanceof CommandException &&
1169 ((CommandException)(ar.exception)).getCommandError()
1170 == CommandException.Error.PASSWORD_INCORRECT) {
1171 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1172 } else {
1173 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1174 }
1175 } else {
1176 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1177 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001178 mDone = true;
1179 UnlockSim.this.notifyAll();
1180 }
1181 break;
1182 }
1183 }
1184 };
1185 UnlockSim.this.notifyAll();
1186 }
1187 Looper.loop();
1188 }
1189
1190 /*
1191 * Use PIN or PUK to unlock SIM card
1192 *
1193 * If PUK is null, unlock SIM card with PIN
1194 *
1195 * If PUK is not null, unlock SIM card with PUK and set PIN code
1196 */
Wink Saville9de0f752013-10-22 19:04:03 -07001197 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001198
1199 while (mHandler == null) {
1200 try {
1201 wait();
1202 } catch (InterruptedException e) {
1203 Thread.currentThread().interrupt();
1204 }
1205 }
1206 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1207
1208 if (puk == null) {
1209 mSimCard.supplyPin(pin, callback);
1210 } else {
1211 mSimCard.supplyPuk(puk, pin, callback);
1212 }
1213
1214 while (!mDone) {
1215 try {
1216 Log.d(LOG_TAG, "wait for done");
1217 wait();
1218 } catch (InterruptedException e) {
1219 // Restore the interrupted status
1220 Thread.currentThread().interrupt();
1221 }
1222 }
1223 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001224 int[] resultArray = new int[2];
1225 resultArray[0] = mResult;
1226 resultArray[1] = mRetryCount;
1227 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001228 }
1229 }
1230
1231 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001232 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001233
1234 }
1235
Wink Savilleb564aae2014-10-23 10:18:09 -07001236 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 // No permission check needed here: this call is harmless, and it's
1238 // needed for the ServiceState.requestStateUpdate() call (which is
1239 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001240 final Phone phone = getPhone(subId);
1241 if (phone != null) {
1242 phone.updateServiceLocation();
1243 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001244 }
1245
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001246 @Override
1247 public boolean isRadioOn(String callingPackage) {
1248 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001249 }
1250
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001251 @Override
1252 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1253 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1254 return false;
1255 }
1256 return isRadioOnForSubscriber(subId);
1257 }
1258
1259 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001260 final Phone phone = getPhone(subId);
1261 if (phone != null) {
1262 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1263 } else {
1264 return false;
1265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
1267
1268 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001269 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001270
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001271 }
Wink Saville36469e72014-06-11 15:17:00 -07001272
Wink Savilleb564aae2014-10-23 10:18:09 -07001273 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001274 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001275 final Phone phone = getPhone(subId);
1276 if (phone != null) {
1277 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1278 }
Wink Saville36469e72014-06-11 15:17:00 -07001279 }
1280
1281 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001282 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001283 }
1284
Wink Savilleb564aae2014-10-23 10:18:09 -07001285 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001286 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001287 final Phone phone = getPhone(subId);
1288 if (phone == null) {
1289 return false;
1290 }
1291 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001292 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001293 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001294 }
1295 return true;
1296 }
Wink Saville36469e72014-06-11 15:17:00 -07001297
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001298 public boolean needMobileRadioShutdown() {
1299 /*
1300 * If any of the Radios are available, it will need to be
1301 * shutdown. So return true if any Radio is available.
1302 */
1303 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1304 Phone phone = PhoneFactory.getPhone(i);
1305 if (phone != null && phone.isRadioAvailable()) return true;
1306 }
1307 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1308 return false;
1309 }
1310
1311 public void shutdownMobileRadios() {
1312 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1313 logv("Shutting down Phone " + i);
1314 shutdownRadioUsingPhoneId(i);
1315 }
1316 }
1317
1318 private void shutdownRadioUsingPhoneId(int phoneId) {
1319 enforceModifyPermission();
1320 Phone phone = PhoneFactory.getPhone(phoneId);
1321 if (phone != null && phone.isRadioAvailable()) {
1322 phone.shutdownRadio();
1323 }
1324 }
1325
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001327 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001328 }
1329
Wink Savilleb564aae2014-10-23 10:18:09 -07001330 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001332 final Phone phone = getPhone(subId);
1333 if (phone != null) {
1334 phone.setRadioPower(turnOn);
1335 return true;
1336 } else {
1337 return false;
1338 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 }
1340
Wink Saville36469e72014-06-11 15:17:00 -07001341 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001342 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001343 public boolean enableDataConnectivity() {
1344 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001345 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001346 final Phone phone = getPhone(subId);
1347 if (phone != null) {
1348 phone.setDataEnabled(true);
1349 return true;
1350 } else {
1351 return false;
1352 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 }
1354
Wink Saville36469e72014-06-11 15:17:00 -07001355 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001356 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001357 public boolean disableDataConnectivity() {
1358 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001359 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001360 final Phone phone = getPhone(subId);
1361 if (phone != null) {
1362 phone.setDataEnabled(false);
1363 return true;
1364 } else {
1365 return false;
1366 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001367 }
1368
Wink Saville36469e72014-06-11 15:17:00 -07001369 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001370 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001372 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001373 final Phone phone = getPhone(subId);
1374 if (phone != null) {
1375 return phone.isDataConnectivityPossible();
1376 } else {
1377 return false;
1378 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 }
1380
1381 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001382 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001383 }
1384
Wink Savilleb564aae2014-10-23 10:18:09 -07001385 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001387 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1388 return false;
1389 }
Wink Saville36469e72014-06-11 15:17:00 -07001390 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 }
1392
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001393 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001394 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001395 }
1396
Wink Savilleb564aae2014-10-23 10:18:09 -07001397 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001398 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001399 }
1400
Sanket Padawe356d7632015-06-22 14:03:32 -07001401 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001403 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001404 if (phone != null) {
1405 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1406 } else {
1407 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1408 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001409 }
1410
Sanket Padawe356d7632015-06-22 14:03:32 -07001411 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001413 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001414 if (phone != null) {
1415 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1416 } else {
1417 return TelephonyManager.DATA_ACTIVITY_NONE;
1418 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001419 }
1420
1421 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001422 public Bundle getCellLocation(String callingPackage) {
1423 enforceFineOrCoarseLocationPermission("getCellLocation");
1424
1425 // OP_COARSE_LOCATION controls both fine and coarse location.
1426 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1427 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001428 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001429 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001430 }
1431
Jake Hambye994d462014-02-03 13:10:13 -08001432 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 if (DBG_LOC) log("getCellLocation: is active user");
1434 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001435 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001436 if (phone == null) {
1437 return null;
1438 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001439 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001440 return data;
1441 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001442 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 return null;
1444 }
1445 }
1446
Svetoslav64fad262015-04-14 14:35:21 -07001447 private void enforceFineOrCoarseLocationPermission(String message) {
1448 try {
1449 mApp.enforceCallingOrSelfPermission(
1450 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1451 } catch (SecurityException e) {
1452 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1453 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1454 // is the weaker precondition
1455 mApp.enforceCallingOrSelfPermission(
1456 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1457 }
1458 }
1459
1460
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 @Override
1462 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001463 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001464 }
1465
Sanket Padawe356d7632015-06-22 14:03:32 -07001466 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001467 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 mApp.enforceCallingOrSelfPermission(
1469 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001470 final Phone phone = getPhone(subId);
1471 if (phone != null) {
1472 phone.enableLocationUpdates();
1473 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001474 }
1475
1476 @Override
1477 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001478 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001479 }
1480
Sanket Padawe356d7632015-06-22 14:03:32 -07001481 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001482 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 mApp.enforceCallingOrSelfPermission(
1484 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001485 final Phone phone = getPhone(subId);
1486 if (phone != null) {
1487 phone.disableLocationUpdates();
1488 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001489 }
1490
1491 @Override
1492 @SuppressWarnings("unchecked")
1493 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001494 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1495
1496 // OP_COARSE_LOCATION controls both fine and coarse location.
1497 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1498 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1499 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 }
1501
1502 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1503 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1504 return null;
1505 }
Svetoslav64fad262015-04-14 14:35:21 -07001506
Jake Hambye994d462014-02-03 13:10:13 -08001507 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1509
1510 ArrayList<NeighboringCellInfo> cells = null;
1511
1512 try {
1513 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001514 CMD_HANDLE_NEIGHBORING_CELL, null,
1515 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001516 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001517 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001518 }
1519 return cells;
1520 } else {
1521 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1522 return null;
1523 }
1524 }
1525
1526
1527 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001528 public List<CellInfo> getAllCellInfo(String callingPackage) {
1529 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1530
1531 // OP_COARSE_LOCATION controls both fine and coarse location.
1532 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1533 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1534 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001535 }
1536
Jake Hambye994d462014-02-03 13:10:13 -08001537 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001538 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001539 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1540 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001541 final List<CellInfo> info = phone.getAllCellInfo();
1542 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001543 }
1544 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001545 } else {
1546 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1547 return null;
1548 }
1549 }
1550
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001551 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 public void setCellInfoListRate(int rateInMillis) {
1553 mPhone.setCellInfoListRate(rateInMillis);
1554 }
1555
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 //
1557 // Internal helper methods.
1558 //
1559
Jake Hambye994d462014-02-03 13:10:13 -08001560 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 boolean ok;
1562
1563 boolean self = Binder.getCallingUid() == Process.myUid();
1564 if (!self) {
1565 // Get the caller's user id then clear the calling identity
1566 // which will be restored in the finally clause.
1567 int callingUser = UserHandle.getCallingUserId();
1568 long ident = Binder.clearCallingIdentity();
1569
1570 try {
1571 // With calling identity cleared the current user is the foreground user.
1572 int foregroundUser = ActivityManager.getCurrentUser();
1573 ok = (foregroundUser == callingUser);
1574 if (DBG_LOC) {
1575 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1576 + " callingUser=" + callingUser + " ok=" + ok);
1577 }
1578 } catch (Exception ex) {
1579 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1580 ok = false;
1581 } finally {
1582 Binder.restoreCallingIdentity(ident);
1583 }
1584 } else {
1585 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1586 ok = true;
1587 }
1588 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1589 return ok;
1590 }
1591
1592 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001593 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1594 *
1595 * @throws SecurityException if the caller does not have the required permission
1596 */
1597 private void enforceModifyPermission() {
1598 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1599 }
1600
1601 /**
Junda Liua2e36012014-07-09 18:30:01 -07001602 * Make sure either system app or the caller has carrier privilege.
1603 *
1604 * @throws SecurityException if the caller does not have the required permission/privilege
1605 */
1606 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001607 int permission = mApp.checkCallingOrSelfPermission(
1608 android.Manifest.permission.MODIFY_PHONE_STATE);
1609 if (permission == PackageManager.PERMISSION_GRANTED) {
1610 return;
1611 }
1612
1613 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001614 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001615 loge("No Carrier Privilege.");
1616 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001617 }
1618 }
1619
1620 /**
1621 * Make sure the caller has carrier privilege.
1622 *
1623 * @throws SecurityException if the caller does not have the required permission
1624 */
1625 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001626 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001627 loge("No Carrier Privilege.");
1628 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001629 }
1630 }
1631
1632 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001633 * Make sure the caller has the CALL_PHONE permission.
1634 *
1635 * @throws SecurityException if the caller does not have the required permission
1636 */
1637 private void enforceCallPermission() {
1638 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1639 }
1640
Stuart Scott8eef64f2015-04-08 15:13:54 -07001641 private void enforceConnectivityInternalPermission() {
1642 mApp.enforceCallingOrSelfPermission(
1643 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1644 "ConnectivityService");
1645 }
1646
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001647 private String createTelUrl(String number) {
1648 if (TextUtils.isEmpty(number)) {
1649 return null;
1650 }
1651
Jake Hambye994d462014-02-03 13:10:13 -08001652 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001653 }
1654
Ihab Awadf9e92732013-12-05 18:02:52 -08001655 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1657 }
1658
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001659 private static void logv(String msg) {
1660 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1661 }
1662
Ihab Awadf9e92732013-12-05 18:02:52 -08001663 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001664 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1665 }
1666
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001667 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001668 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001669 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001670 }
1671
Sanket Padawe356d7632015-06-22 14:03:32 -07001672 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001673 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001674 final Phone phone = getPhone(subId);
1675 if (phone == null) {
1676 return PhoneConstants.PHONE_TYPE_NONE;
1677 } else {
1678 return getPhone(subId).getPhoneType();
1679 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001680 }
1681
1682 /**
1683 * Returns the CDMA ERI icon index to display
1684 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001685 @Override
1686 public int getCdmaEriIconIndex(String callingPackage) {
1687 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001688 }
1689
Sanket Padawe356d7632015-06-22 14:03:32 -07001690 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001691 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1692 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1693 return -1;
1694 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001695 final Phone phone = getPhone(subId);
1696 if (phone != null) {
1697 return phone.getCdmaEriIconIndex();
1698 } else {
1699 return -1;
1700 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001701 }
1702
1703 /**
1704 * Returns the CDMA ERI icon mode,
1705 * 0 - ON
1706 * 1 - FLASHING
1707 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001708 @Override
1709 public int getCdmaEriIconMode(String callingPackage) {
1710 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001711 }
1712
Sanket Padawe356d7632015-06-22 14:03:32 -07001713 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001714 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1715 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1716 return -1;
1717 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001718 final Phone phone = getPhone(subId);
1719 if (phone != null) {
1720 return phone.getCdmaEriIconMode();
1721 } else {
1722 return -1;
1723 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001724 }
1725
1726 /**
1727 * Returns the CDMA ERI text,
1728 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001729 @Override
1730 public String getCdmaEriText(String callingPackage) {
1731 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001732 }
1733
Sanket Padawe356d7632015-06-22 14:03:32 -07001734 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001735 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1736 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1737 return null;
1738 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001739 final Phone phone = getPhone(subId);
1740 if (phone != null) {
1741 return phone.getCdmaEriText();
1742 } else {
1743 return null;
1744 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001745 }
1746
1747 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001748 * Returns the CDMA MDN.
1749 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001750 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001751 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001752 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001753 final Phone phone = getPhone(subId);
1754 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1755 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001756 } else {
1757 return null;
1758 }
1759 }
1760
1761 /**
1762 * Returns the CDMA MIN.
1763 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001764 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001765 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001766 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001767 final Phone phone = getPhone(subId);
1768 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1769 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001770 } else {
1771 return null;
1772 }
1773 }
1774
1775 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001776 * Returns true if CDMA provisioning needs to run.
1777 */
1778 public boolean needsOtaServiceProvisioning() {
1779 return mPhone.needsOtaServiceProvisioning();
1780 }
1781
1782 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001783 * Sets the voice mail number of a given subId.
1784 */
1785 @Override
1786 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001787 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001788 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1789 new Pair<String, String>(alphaTag, number), new Integer(subId));
1790 return success;
1791 }
1792
1793 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001794 * Returns the unread count of voicemails
1795 */
1796 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001797 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001798 }
1799
1800 /**
1801 * Returns the unread count of voicemails for a subId
1802 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001803 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001804 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001805 final Phone phone = getPhone(subId);
1806 if (phone != null) {
1807 return phone.getVoiceMessageCount();
1808 } else {
1809 return 0;
1810 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001811 }
1812
1813 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001814 * Returns the data network type.
1815 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001816 *
1817 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1818 */
1819 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001820 public int getNetworkType() {
1821 final Phone phone = getPhone(getDefaultSubscription());
1822 if (phone != null) {
1823 return phone.getServiceState().getDataNetworkType();
1824 } else {
1825 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1826 }
Wink Saville36469e72014-06-11 15:17:00 -07001827 }
1828
1829 /**
1830 * Returns the network type for a subId
1831 */
1832 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001833 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1834 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1835 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1836 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001837
Sanket Padawe356d7632015-06-22 14:03:32 -07001838 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001839 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001840 return phone.getServiceState().getDataNetworkType();
1841 } else {
1842 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1843 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001844 }
1845
1846 /**
1847 * Returns the data network type
1848 */
1849 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001850 public int getDataNetworkType(String callingPackage) {
1851 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001852 }
1853
1854 /**
1855 * Returns the data network type for a subId
1856 */
1857 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001858 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1859 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1860 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1861 }
1862
Sanket Padawe356d7632015-06-22 14:03:32 -07001863 final Phone phone = getPhone(subId);
1864 if (phone != null) {
1865 return phone.getServiceState().getDataNetworkType();
1866 } else {
1867 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1868 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001869 }
1870
1871 /**
Wink Saville36469e72014-06-11 15:17:00 -07001872 * Returns the Voice network type for a subId
1873 */
1874 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001875 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1876 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1877 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1878 }
1879
Sanket Padawe356d7632015-06-22 14:03:32 -07001880 final Phone phone = getPhone(subId);
1881 if (phone != null) {
1882 return phone.getServiceState().getVoiceNetworkType();
1883 } else {
1884 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1885 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001886 }
1887
1888 /**
1889 * @return true if a ICC card is present
1890 */
1891 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001892 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001893 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001894 }
1895
1896 /**
1897 * @return true if a ICC card is present for a slotId
1898 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001899 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001900 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001901 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001902 final Phone phone = getPhone(subId[0]);
1903 if (subId != null && phone != null) {
1904 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001905 } else {
1906 return false;
1907 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001908 }
1909
1910 /**
1911 * Return if the current radio is LTE on CDMA. This
1912 * is a tri-state return value as for a period of time
1913 * the mode may be unknown.
1914 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001915 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001916 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001917 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001918 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001919 @Override
1920 public int getLteOnCdmaMode(String callingPackage) {
1921 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001922 }
1923
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001925 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1926 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1927 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1928 }
1929
Sanket Padawe356d7632015-06-22 14:03:32 -07001930 final Phone phone = getPhone(subId);
1931 if (phone == null) {
1932 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1933 } else {
1934 return phone.getLteOnCdmaMode();
1935 }
Wink Saville36469e72014-06-11 15:17:00 -07001936 }
1937
1938 public void setPhone(Phone phone) {
1939 mPhone = phone;
1940 }
1941
1942 /**
1943 * {@hide}
1944 * Returns Default subId, 0 in the case of single standby.
1945 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001946 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001947 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001948 }
1949
Wink Savilleb564aae2014-10-23 10:18:09 -07001950 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001951 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001952 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001953
1954 /**
1955 * @see android.telephony.TelephonyManager.WifiCallingChoices
1956 */
1957 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001958 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1959 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001960 }
1961
1962 /**
1963 * @see android.telephony.TelephonyManager.WifiCallingChoices
1964 */
1965 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001966 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1967 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1968 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001969 }
1970
Sailesh Nepald1e68152013-12-12 19:08:02 -08001971 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001972 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001973 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001974 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001975
Shishir Agrawal566b7612013-10-28 14:41:00 -07001976 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001977 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001978 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001979
1980 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001981 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1982 CMD_OPEN_CHANNEL, AID);
1983 if (DBG) log("iccOpenLogicalChannel: " + response);
1984 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001985 }
1986
1987 @Override
1988 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001989 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001990
1991 if (DBG) log("iccCloseLogicalChannel: " + channel);
1992 if (channel < 0) {
1993 return false;
1994 }
Jake Hambye994d462014-02-03 13:10:13 -08001995 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001996 if (DBG) log("iccCloseLogicalChannel: " + success);
1997 return success;
1998 }
1999
2000 @Override
2001 public String iccTransmitApduLogicalChannel(int channel, int cla,
2002 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07002003 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07002004
2005 if (DBG) {
2006 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
2007 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2008 " data=" + data);
2009 }
2010
2011 if (channel < 0) {
2012 return "";
2013 }
2014
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002015 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002016 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2017 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2018
Shishir Agrawal566b7612013-10-28 14:41:00 -07002019 // Append the returned status code to the end of the response payload.
2020 String s = Integer.toHexString(
2021 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002022 if (response.payload != null) {
2023 s = IccUtils.bytesToHexString(response.payload) + s;
2024 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002025 return s;
2026 }
Jake Hambye994d462014-02-03 13:10:13 -08002027
Evan Charltonc66da362014-05-16 14:06:40 -07002028 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002029 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2030 int p3, String data) {
2031 enforceModifyPermissionOrCarrierPrivilege();
2032
2033 if (DBG) {
2034 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2035 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2036 }
2037
2038 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2039 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2040 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2041
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002042 // Append the returned status code to the end of the response payload.
2043 String s = Integer.toHexString(
2044 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002045 if (response.payload != null) {
2046 s = IccUtils.bytesToHexString(response.payload) + s;
2047 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002048 return s;
2049 }
2050
2051 @Override
2052 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2053 String filePath) {
2054 enforceModifyPermissionOrCarrierPrivilege();
2055
2056 if (DBG) {
2057 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2058 p1 + " " + p2 + " " + p3 + ":" + filePath);
2059 }
2060
2061 IccIoResult response =
2062 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002063 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002064
2065 if (DBG) {
2066 log("Exchange SIM_IO [R]" + response);
2067 }
2068
2069 byte[] result = null;
2070 int length = 2;
2071 if (response.payload != null) {
2072 length = 2 + response.payload.length;
2073 result = new byte[length];
2074 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2075 } else {
2076 result = new byte[length];
2077 }
2078
2079 result[length - 1] = (byte) response.sw2;
2080 result[length - 2] = (byte) response.sw1;
2081 return result;
2082 }
2083
2084 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002085 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002086 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002087
2088 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2089 if (response.payload == null) {
2090 return "";
2091 }
2092
2093 // Append the returned status code to the end of the response payload.
2094 String s = Integer.toHexString(
2095 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2096 s = IccUtils.bytesToHexString(response.payload) + s;
2097 return s;
2098 }
2099
Jake Hambye994d462014-02-03 13:10:13 -08002100 /**
2101 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2102 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2103 *
2104 * @param itemID the ID of the item to read
2105 * @return the NV item as a String, or null on error.
2106 */
2107 @Override
2108 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002109 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002110 if (DBG) log("nvReadItem: item " + itemID);
2111 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2112 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2113 return value;
2114 }
2115
2116 /**
2117 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2118 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2119 *
2120 * @param itemID the ID of the item to read
2121 * @param itemValue the value to write, as a String
2122 * @return true on success; false on any failure
2123 */
2124 @Override
2125 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002126 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002127 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2128 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2129 new Pair<Integer, String>(itemID, itemValue));
2130 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2131 return success;
2132 }
2133
2134 /**
2135 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2136 * Used for device configuration by some CDMA operators.
2137 *
2138 * @param preferredRoamingList byte array containing the new PRL
2139 * @return true on success; false on any failure
2140 */
2141 @Override
2142 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002143 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002144 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2145 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2146 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2147 return success;
2148 }
2149
2150 /**
2151 * Perform the specified type of NV config reset.
2152 * Used for device configuration by some CDMA operators.
2153 *
2154 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2155 * @return true on success; false on any failure
2156 */
2157 @Override
2158 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002159 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002160 if (DBG) log("nvResetConfig: type " + resetType);
2161 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2162 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2163 return success;
2164 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002165
2166 /**
Wink Saville36469e72014-06-11 15:17:00 -07002167 * {@hide}
2168 * Returns Default sim, 0 in the case of single standby.
2169 */
2170 public int getDefaultSim() {
2171 //TODO Need to get it from Telephony Devcontroller
2172 return 0;
2173 }
2174
Svet Ganovb320e182015-04-16 12:30:10 -07002175 public String[] getPcscfAddress(String apnType, String callingPackage) {
2176 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2177 return new String[0];
2178 }
2179
2180
ram87fca6f2014-07-18 18:58:44 +05302181 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002182 }
2183
2184 public void setImsRegistrationState(boolean registered) {
2185 enforceModifyPermission();
2186 mPhone.setImsRegistrationState(registered);
2187 }
2188
2189 /**
Stuart Scott54788802015-03-30 13:18:01 -07002190 * Set the network selection mode to automatic.
2191 *
2192 */
2193 @Override
2194 public void setNetworkSelectionModeAutomatic(int subId) {
2195 enforceModifyPermissionOrCarrierPrivilege();
2196 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2197 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2198 }
2199
2200 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002201 * Set the network selection mode to manual with the selected carrier.
2202 */
2203 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002204 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2205 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002206 enforceModifyPermissionOrCarrierPrivilege();
2207 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002208 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2209 persistSelection);
2210 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002211 }
2212
2213 /**
2214 * Scans for available networks.
2215 */
2216 @Override
2217 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2218 enforceModifyPermissionOrCarrierPrivilege();
2219 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2220 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2221 CMD_PERFORM_NETWORK_SCAN, null, subId);
2222 return result;
2223 }
2224
2225 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002226 * Get the calculated preferred network type.
2227 * Used for debugging incorrect network type.
2228 *
2229 * @return the preferred network type, defined in RILConstants.java.
2230 */
2231 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002232 public int getCalculatedPreferredNetworkType(String callingPackage) {
2233 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2234 return RILConstants.PREFERRED_NETWORK_MODE;
2235 }
2236
Amit Mahajan43330e02014-11-18 11:54:45 -08002237 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002238 }
2239
2240 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002241 * Get the preferred network type.
2242 * Used for device configuration by some CDMA operators.
2243 *
2244 * @return the preferred network type, defined in RILConstants.java.
2245 */
2246 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002247 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002248 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002249 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002250 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002251 int networkType = (result != null ? result[0] : -1);
2252 if (DBG) log("getPreferredNetworkType: " + networkType);
2253 return networkType;
2254 }
2255
2256 /**
2257 * Set the preferred network type.
2258 * Used for device configuration by some CDMA operators.
2259 *
2260 * @param networkType the preferred network type, defined in RILConstants.java.
2261 * @return true on success; false on any failure.
2262 */
2263 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002264 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002265 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002266 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2267 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002268 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002269 if (success) {
2270 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002271 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002272 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002273 return success;
2274 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002275
2276 /**
Junda Liu475951f2014-11-07 16:45:03 -08002277 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2278 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2279 * tethering.
2280 *
2281 * @return 0: Not required. 1: required. 2: Not set.
2282 * @hide
2283 */
2284 @Override
2285 public int getTetherApnRequired() {
2286 enforceModifyPermissionOrCarrierPrivilege();
2287 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2288 Settings.Global.TETHER_DUN_REQUIRED, 2);
2289 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2290 // config_tether_apndata.
2291 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2292 dunRequired = 1;
2293 }
2294 return dunRequired;
2295 }
2296
2297 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002298 * Set mobile data enabled
2299 * Used by the user through settings etc to turn on/off mobile data
2300 *
2301 * @param enable {@code true} turn turn data on, else {@code false}
2302 */
2303 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002304 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002305 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002306 int phoneId = mSubscriptionController.getPhoneId(subId);
2307 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2308 Phone phone = PhoneFactory.getPhone(phoneId);
2309 if (phone != null) {
2310 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2311 phone.setDataEnabled(enable);
2312 } else {
2313 loge("setDataEnabled: no phone for subId=" + subId);
2314 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002315 }
2316
2317 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002318 * Get whether mobile data is enabled.
2319 *
2320 * Note that this used to be available from ConnectivityService, gated by
2321 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2322 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002323 *
2324 * @return {@code true} if data is enabled else {@code false}
2325 */
2326 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002327 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002328 try {
2329 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2330 null);
2331 } catch (Exception e) {
2332 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2333 null);
2334 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002335 int phoneId = mSubscriptionController.getPhoneId(subId);
2336 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2337 Phone phone = PhoneFactory.getPhone(phoneId);
2338 if (phone != null) {
2339 boolean retVal = phone.getDataEnabled();
2340 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2341 return retVal;
2342 } else {
2343 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2344 return false;
2345 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002346 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002347
2348 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002349 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002350 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002351 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002352 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002353 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2354 }
2355 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002356 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002357 }
Junda Liu29340342014-07-10 15:23:27 -07002358
2359 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002360 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002361 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002362 if (card == null) {
2363 loge("checkCarrierPrivilegesForPackage: No UICC");
2364 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2365 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002366 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2367 }
2368
2369 @Override
2370 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2371 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2372 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2373 UiccCard card = UiccController.getInstance().getUiccCard(i);
2374 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002375 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002376 continue;
2377 }
2378
2379 result = card.getCarrierPrivilegeStatus(
2380 mPhone.getContext().getPackageManager(), pkgName);
2381 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2382 break;
2383 }
2384 }
2385
2386 return result;
Junda Liu29340342014-07-10 15:23:27 -07002387 }
Derek Tan89e89d42014-07-08 17:00:10 -07002388
2389 @Override
Junda Liue64de782015-04-16 17:19:16 -07002390 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2391 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2392 loge("phoneId " + phoneId + " is not valid.");
2393 return null;
2394 }
2395 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002396 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002397 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002398 return null ;
2399 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002400 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002401 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002402 }
2403
Wink Savilleb564aae2014-10-23 10:18:09 -07002404 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002405 final Phone phone = getPhone(subId);
2406 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002407 if (card == null) {
2408 loge("getIccId: No UICC");
2409 return null;
2410 }
2411 String iccId = card.getIccId();
2412 if (TextUtils.isEmpty(iccId)) {
2413 loge("getIccId: ICC ID is null or empty.");
2414 return null;
2415 }
2416 return iccId;
2417 }
2418
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002419 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002420 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2421 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002422 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002423
Jeff Sharkey85190e62014-12-05 09:40:12 -08002424 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002425 final Phone phone = getPhone(subId);
2426 if (phone == null) {
2427 return false;
2428 }
2429 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002430
2431 if (DBG_MERGE) {
2432 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2433 + subscriberId + " to " + number);
2434 }
2435
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002436 if (TextUtils.isEmpty(iccId)) {
2437 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002438 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002439
Jeff Sharkey85190e62014-12-05 09:40:12 -08002440 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2441
2442 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002443 if (alphaTag == null) {
2444 editor.remove(alphaTagPrefKey);
2445 } else {
2446 editor.putString(alphaTagPrefKey, alphaTag);
2447 }
2448
Jeff Sharkey85190e62014-12-05 09:40:12 -08002449 // Record both the line number and IMSI for this ICCID, since we need to
2450 // track all merged IMSIs based on line number
2451 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2452 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002453 if (number == null) {
2454 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002455 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002456 } else {
2457 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002458 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002459 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002460
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002461 editor.commit();
2462 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002463 }
2464
2465 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002466 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002467 // This is open to apps with WRITE_SMS.
2468 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002469 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002470 return null;
2471 }
Derek Tan97ebb422014-09-05 16:55:38 -07002472
2473 String iccId = getIccId(subId);
2474 if (iccId != null) {
2475 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002476 if (DBG_MERGE) {
2477 log("getLine1NumberForDisplay returning " +
2478 mTelephonySharedPreferences.getString(numberPrefKey, null));
2479 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002480 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002481 }
Jack Yue7939d82015-12-10 16:04:02 -08002482 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002483 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002484 }
2485
2486 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002487 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2488 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2489 return null;
2490 }
Derek Tan97ebb422014-09-05 16:55:38 -07002491
2492 String iccId = getIccId(subId);
2493 if (iccId != null) {
2494 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002495 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002496 }
Derek Tan97ebb422014-09-05 16:55:38 -07002497 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002498 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002499
2500 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002501 public String[] getMergedSubscriberIds(String callingPackage) {
2502 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2503 return null;
2504 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002505 final Context context = mPhone.getContext();
2506 final TelephonyManager tele = TelephonyManager.from(context);
2507 final SubscriptionManager sub = SubscriptionManager.from(context);
2508
2509 // Figure out what subscribers are currently active
2510 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002511 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2512 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2513 final long identity = Binder.clearCallingIdentity();
2514 try {
2515 final int[] subIds = sub.getActiveSubscriptionIdList();
2516 for (int subId : subIds) {
2517 activeSubscriberIds.add(tele.getSubscriberId(subId));
2518 }
2519 } finally {
2520 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002521 }
2522
2523 // First pass, find a number override for an active subscriber
2524 String mergeNumber = null;
2525 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2526 for (String key : prefs.keySet()) {
2527 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2528 final String subscriberId = (String) prefs.get(key);
2529 if (activeSubscriberIds.contains(subscriberId)) {
2530 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2531 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2532 mergeNumber = (String) prefs.get(numberKey);
2533 if (DBG_MERGE) {
2534 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2535 + " for active subscriber " + subscriberId);
2536 }
2537 if (!TextUtils.isEmpty(mergeNumber)) {
2538 break;
2539 }
2540 }
2541 }
2542 }
2543
2544 // Shortcut when no active merged subscribers
2545 if (TextUtils.isEmpty(mergeNumber)) {
2546 return null;
2547 }
2548
2549 // Second pass, find all subscribers under that line override
2550 final ArraySet<String> result = new ArraySet<>();
2551 for (String key : prefs.keySet()) {
2552 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2553 final String number = (String) prefs.get(key);
2554 if (mergeNumber.equals(number)) {
2555 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2556 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2557 final String subscriberId = (String) prefs.get(subscriberKey);
2558 if (!TextUtils.isEmpty(subscriberId)) {
2559 result.add(subscriberId);
2560 }
2561 }
2562 }
2563 }
2564
2565 final String[] resultArray = result.toArray(new String[result.size()]);
2566 Arrays.sort(resultArray);
2567 if (DBG_MERGE) {
2568 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2569 }
2570 return resultArray;
2571 }
2572
2573 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002574 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002575 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002576 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002577 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002578
2579 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002580 public boolean setRoamingOverride(List<String> gsmRoamingList,
2581 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2582 List<String> cdmaNonRoamingList) {
2583 enforceCarrierPrivilege();
2584 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2585 cdmaNonRoamingList);
2586 }
2587
2588 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002589 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2590 enforceModifyPermission();
2591
2592 int returnValue = 0;
2593 try {
2594 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2595 if(result.exception == null) {
2596 if (result.result != null) {
2597 byte[] responseData = (byte[])(result.result);
2598 if(responseData.length > oemResp.length) {
2599 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2600 responseData.length + "bytes. Buffer Size is " +
2601 oemResp.length + "bytes.");
2602 }
2603 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2604 returnValue = responseData.length;
2605 }
2606 } else {
2607 CommandException ex = (CommandException) result.exception;
2608 returnValue = ex.getCommandError().ordinal();
2609 if(returnValue > 0) returnValue *= -1;
2610 }
2611 } catch (RuntimeException e) {
2612 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2613 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2614 if(returnValue > 0) returnValue *= -1;
2615 }
2616
2617 return returnValue;
2618 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002619
2620 @Override
2621 public void setRadioCapability(RadioAccessFamily[] rafs) {
2622 try {
2623 ProxyController.getInstance().setRadioCapability(rafs);
2624 } catch (RuntimeException e) {
2625 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2626 }
2627 }
2628
2629 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002630 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2631 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2632 return RadioAccessFamily.RAF_UNKNOWN;
2633 }
2634
Wink Saville5d475dd2014-10-17 15:00:58 -07002635 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2636 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002637
2638 @Override
2639 public void enableVideoCalling(boolean enable) {
2640 enforceModifyPermission();
2641 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2642 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2643 editor.commit();
2644 }
2645
2646 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002647 public boolean isVideoCallingEnabled(String callingPackage) {
2648 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2649 return false;
2650 }
2651
Andrew Lee77527ac2014-10-21 16:57:39 -07002652 // Check the user preference and the system-level IMS setting. Even if the user has
2653 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2654 // In the long run, we may instead need to check if there exists a connection service
2655 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002656 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2657 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2658 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002659 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002660
Andrew Leea1239f22015-03-02 17:44:07 -08002661 @Override
2662 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002663 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002664 }
2665
2666 @Override
2667 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002668 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002669 }
2670
Andrew Lee9431b832015-03-09 18:46:45 -07002671 @Override
2672 public boolean isTtyModeSupported() {
2673 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2674 TelephonyManager telephonyManager =
2675 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2676 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2677 }
2678
2679 @Override
2680 public boolean isHearingAidCompatibilitySupported() {
2681 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2682 }
2683
Sanket Padawe7310cc72015-01-14 09:53:20 -08002684 /**
2685 * Returns the unique device ID of phone, for example, the IMEI for
2686 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2687 *
2688 * <p>Requires Permission:
2689 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2690 */
2691 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002692 public String getDeviceId(String callingPackage) {
2693 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2694 return null;
2695 }
2696
Sanket Padawe7310cc72015-01-14 09:53:20 -08002697 final Phone phone = PhoneFactory.getPhone(0);
2698 if (phone != null) {
2699 return phone.getDeviceId();
2700 } else {
2701 return null;
2702 }
2703 }
2704
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002705 /*
2706 * {@hide}
2707 * Returns the IMS Registration Status
2708 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002709 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002710 public boolean isImsRegistered() {
2711 return mPhone.isImsRegistered();
2712 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002713
2714 @Override
2715 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2716 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2717 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002718
Nathan Haroldc55097a2015-03-11 18:14:50 -07002719 /*
2720 * {@hide}
2721 * Returns the IMS Registration Status
2722 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002723 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002724 return mPhone.isWifiCallingEnabled();
2725 }
2726
2727 /*
2728 * {@hide}
2729 * Returns the IMS Registration Status
2730 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002731 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002732 return mPhone.isVolteEnabled();
2733 }
Svet Ganovb320e182015-04-16 12:30:10 -07002734
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002735 /*
2736 * {@hide} Returns the IMS Registration Status
2737 */
2738 public boolean isVideoTelephonyAvailable() {
2739 return mPhone.isVideoEnabled();
2740 }
2741
Svet Ganovb320e182015-04-16 12:30:10 -07002742 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002743 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002744 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002745 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2746
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002747 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002748 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002749 } catch (SecurityException e) {
2750 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2751 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002752 }
Svet Ganovb320e182015-04-16 12:30:10 -07002753
2754 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2755 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2756 return false;
2757 }
2758
2759 return true;
2760 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002761
Makoto Onukifee69342015-06-29 14:44:50 -07002762 /**
Jack Yue7939d82015-12-10 16:04:02 -08002763 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002764 */
2765 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002766 // Default SMS app can always read it.
2767 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2768 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2769 return true;
2770 }
2771 try {
2772 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002773 } catch (SecurityException readPhoneStateSecurityException) {
2774 try {
2775 // Can be read with READ_SMS too.
2776 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2777 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2778 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2779 } catch (SecurityException readSmsSecurityException) {
2780 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2781 // permissions
2782 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2783 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2784 " or " + android.Manifest.permission.READ_SMS + ".");
2785 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002786 }
Makoto Onukifee69342015-06-29 14:44:50 -07002787 }
2788
Stuart Scott8eef64f2015-04-08 15:13:54 -07002789 @Override
2790 public void factoryReset(int subId) {
2791 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002792 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2793 return;
2794 }
2795
Svet Ganovcc087f82015-05-12 20:35:54 -07002796 final long identity = Binder.clearCallingIdentity();
2797 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002798 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2799 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002800 // Enable data
2801 setDataEnabled(subId, true);
2802 // Set network selection mode to automatic
2803 setNetworkSelectionModeAutomatic(subId);
2804 // Set preferred mobile network type to the best available
2805 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2806 // Turn off roaming
2807 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2808 }
2809 } finally {
2810 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002811 }
2812 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002813
2814 @Override
2815 public String getLocaleFromDefaultSim() {
2816 // We query all subscriptions instead of just the active ones, because
2817 // this might be called early on in the provisioning flow when the
2818 // subscriptions potentially aren't active yet.
2819 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2820 if (slist == null || slist.isEmpty()) {
2821 return null;
2822 }
2823
2824 // This function may be called very early, say, from the setup wizard, at
2825 // which point we won't have a default subscription set. If that's the case
2826 // we just choose the first, which will be valid in "most cases".
2827 final int defaultSubId = getDefaultSubscription();
2828 SubscriptionInfo info = null;
2829 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2830 info = slist.get(0);
2831 } else {
2832 for (SubscriptionInfo item : slist) {
2833 if (item.getSubscriptionId() == defaultSubId) {
2834 info = item;
2835 break;
2836 }
2837 }
2838
2839 if (info == null) {
2840 return null;
2841 }
2842 }
2843
2844 // Try and fetch the locale from the carrier properties or from the SIM language
2845 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002846 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002847 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002848 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002849 if (defaultPhone != null) {
2850 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2851 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002852 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002853 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2854 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002855 } else {
2856 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002857 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002858 }
2859 }
2860
Narayan Kamath011676f2015-07-29 12:04:08 +01002861 // The SIM language preferences only store a language (e.g. fr = French), not an
2862 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2863 // the SIM and carrier preferences does not include a country we add the country
2864 // determined from the SIM MCC to provide an exact locale.
2865 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002866 if (mccLocale != null) {
2867 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2868 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002869 }
2870
Tony Hill183b2de2015-06-24 14:53:58 +01002871 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002872 return null;
2873 }
2874
2875 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2876 final long identity = Binder.clearCallingIdentity();
2877 try {
2878 return mSubscriptionController.getAllSubInfoList(
2879 mPhone.getContext().getOpPackageName());
2880 } finally {
2881 Binder.restoreCallingIdentity(identity);
2882 }
2883 }
2884
2885 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2886 final long identity = Binder.clearCallingIdentity();
2887 try {
2888 return mSubscriptionController.getActiveSubscriptionInfoList(
2889 mPhone.getContext().getOpPackageName());
2890 } finally {
2891 Binder.restoreCallingIdentity(identity);
2892 }
2893 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002894
2895 /**
2896 * {@hide}
2897 * Returns the modem stats
2898 */
2899 @Override
2900 public ModemActivityInfo getModemActivityInfo() {
2901 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2902 }
Jack Yue7939d82015-12-10 16:04:02 -08002903
2904 /**
2905 * {@hide}
2906 * Returns the service state information on specified subscription.
2907 */
2908 @Override
2909 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2910
2911 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2912 return null;
2913 }
2914
2915 final Phone phone = getPhone(subId);
2916 if (phone == null) {
2917 return null;
2918 }
2919
2920 return phone.getServiceState();
2921 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002922}