blob: 6f828dbb07ded23a0616688faed62de83684a0b7 [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;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800197 /** The subscriber id that this request applies to. Null if default. */
198 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700199
200 public MainThreadRequest(Object argument) {
201 this.argument = argument;
202 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800203
204 public MainThreadRequest(Object argument, Integer subId) {
205 this.argument = argument;
206 this.subId = subId;
207 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700208 }
209
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800210 private static final class IncomingThirdPartyCallArgs {
211 public final ComponentName component;
212 public final String callId;
213 public final String callerDisplayName;
214
215 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
216 String callerDisplayName) {
217 this.component = component;
218 this.callId = callId;
219 this.callerDisplayName = callerDisplayName;
220 }
221 }
222
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700223 /**
224 * A handler that processes messages on the main thread in the phone process. Since many
225 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
226 * inbound binder threads to the main thread in the phone process. The Binder thread
227 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
228 * on, which will be notified when the operation completes and will contain the result of the
229 * request.
230 *
231 * <p>If a MainThreadRequest object is provided in the msg.obj field,
232 * note that request.result must be set to something non-null for the calling thread to
233 * unblock.
234 */
235 private final class MainThreadHandler extends Handler {
236 @Override
237 public void handleMessage(Message msg) {
238 MainThreadRequest request;
239 Message onCompleted;
240 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800241 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700242 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700243
244 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700245 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700247 final Phone phone = getPhoneFromRequest(request);
248 request.result = phone != null ?
249 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
250 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700251 // Wake up the requesting thread
252 synchronized (request) {
253 request.notifyAll();
254 }
255 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700256 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700257
258 case CMD_HANDLE_NEIGHBORING_CELL:
259 request = (MainThreadRequest) msg.obj;
260 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
261 request);
262 mPhone.getNeighboringCids(onCompleted);
263 break;
264
265 case EVENT_NEIGHBORING_CELL_DONE:
266 ar = (AsyncResult) msg.obj;
267 request = (MainThreadRequest) ar.userObj;
268 if (ar.exception == null && ar.result != null) {
269 request.result = ar.result;
270 } else {
271 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800272 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700273 }
274 // Wake up the requesting thread
275 synchronized (request) {
276 request.notifyAll();
277 }
278 break;
279
280 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700281 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800282 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700283 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700284 break;
285
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700286 case CMD_END_CALL:
287 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800288 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700289 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700290 Phone phone = getPhone(end_subId);
291 if (phone == null) {
292 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
293 break;
294 }
295 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700296 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
297 // CDMA: If the user presses the Power button we treat it as
298 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700299 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
301 // GSM: End the call as per the Phone state
302 hungUp = PhoneUtils.hangup(mCM);
303 } else {
304 throw new IllegalStateException("Unexpected phone type: " + phoneType);
305 }
306 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
307 request.result = hungUp;
308 // Wake up the requesting thread
309 synchronized (request) {
310 request.notifyAll();
311 }
312 break;
313
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700314 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700315 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700316 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700317 if (uiccCard == null) {
318 loge("iccTransmitApduLogicalChannel: No UICC");
319 request.result = new IccIoResult(0x6F, 0, (byte[])null);
320 synchronized (request) {
321 request.notifyAll();
322 }
323 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700324 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
325 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700326 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700327 iccArgument.channel, iccArgument.cla, iccArgument.command,
328 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700329 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700330 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700331 break;
332
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700333 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700334 ar = (AsyncResult) msg.obj;
335 request = (MainThreadRequest) ar.userObj;
336 if (ar.exception == null && ar.result != null) {
337 request.result = ar.result;
338 } else {
339 request.result = new IccIoResult(0x6F, 0, (byte[])null);
340 if (ar.result == null) {
341 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800342 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700343 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800344 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700345 } else {
346 loge("iccTransmitApduLogicalChannel: Unknown exception");
347 }
348 }
349 synchronized (request) {
350 request.notifyAll();
351 }
352 break;
353
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700354 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
355 request = (MainThreadRequest) msg.obj;
356 iccArgument = (IccAPDUArgument) request.argument;
357 if (uiccCard == null) {
358 loge("iccTransmitApduBasicChannel: No UICC");
359 request.result = new IccIoResult(0x6F, 0, (byte[])null);
360 synchronized (request) {
361 request.notifyAll();
362 }
363 } else {
364 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
365 request);
366 uiccCard.iccTransmitApduBasicChannel(
367 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
368 iccArgument.p3, iccArgument.data, onCompleted);
369 }
370 break;
371
372 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
373 ar = (AsyncResult) msg.obj;
374 request = (MainThreadRequest) ar.userObj;
375 if (ar.exception == null && ar.result != null) {
376 request.result = ar.result;
377 } else {
378 request.result = new IccIoResult(0x6F, 0, (byte[])null);
379 if (ar.result == null) {
380 loge("iccTransmitApduBasicChannel: Empty response");
381 } else if (ar.exception instanceof CommandException) {
382 loge("iccTransmitApduBasicChannel: CommandException: " +
383 ar.exception);
384 } else {
385 loge("iccTransmitApduBasicChannel: Unknown exception");
386 }
387 }
388 synchronized (request) {
389 request.notifyAll();
390 }
391 break;
392
393 case CMD_EXCHANGE_SIM_IO:
394 request = (MainThreadRequest) msg.obj;
395 iccArgument = (IccAPDUArgument) request.argument;
396 if (uiccCard == null) {
397 loge("iccExchangeSimIO: No UICC");
398 request.result = new IccIoResult(0x6F, 0, (byte[])null);
399 synchronized (request) {
400 request.notifyAll();
401 }
402 } else {
403 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
404 request);
405 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
406 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
407 iccArgument.data, onCompleted);
408 }
409 break;
410
411 case EVENT_EXCHANGE_SIM_IO_DONE:
412 ar = (AsyncResult) msg.obj;
413 request = (MainThreadRequest) ar.userObj;
414 if (ar.exception == null && ar.result != null) {
415 request.result = ar.result;
416 } else {
417 request.result = new IccIoResult(0x6f, 0, (byte[])null);
418 }
419 synchronized (request) {
420 request.notifyAll();
421 }
422 break;
423
Derek Tan4d5e5c12014-02-04 11:54:58 -0800424 case CMD_SEND_ENVELOPE:
425 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700426 if (uiccCard == null) {
427 loge("sendEnvelopeWithStatus: No UICC");
428 request.result = new IccIoResult(0x6F, 0, (byte[])null);
429 synchronized (request) {
430 request.notifyAll();
431 }
432 } else {
433 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
434 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
435 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800436 break;
437
438 case EVENT_SEND_ENVELOPE_DONE:
439 ar = (AsyncResult) msg.obj;
440 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700441 if (ar.exception == null && ar.result != null) {
442 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800443 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700444 request.result = new IccIoResult(0x6F, 0, (byte[])null);
445 if (ar.result == null) {
446 loge("sendEnvelopeWithStatus: Empty response");
447 } else if (ar.exception instanceof CommandException) {
448 loge("sendEnvelopeWithStatus: CommandException: " +
449 ar.exception);
450 } else {
451 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
452 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800453 }
454 synchronized (request) {
455 request.notifyAll();
456 }
457 break;
458
Shishir Agrawal566b7612013-10-28 14:41:00 -0700459 case CMD_OPEN_CHANNEL:
460 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700461 if (uiccCard == null) {
462 loge("iccOpenLogicalChannel: No UICC");
463 request.result = new IccIoResult(0x6F, 0, (byte[])null);
464 synchronized (request) {
465 request.notifyAll();
466 }
467 } else {
468 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
469 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
470 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700471 break;
472
473 case EVENT_OPEN_CHANNEL_DONE:
474 ar = (AsyncResult) msg.obj;
475 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700476 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700477 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700478 int[] result = (int[]) ar.result;
479 int channelId = result[0];
480 byte[] selectResponse = null;
481 if (result.length > 1) {
482 selectResponse = new byte[result.length - 1];
483 for (int i = 1; i < result.length; ++i) {
484 selectResponse[i - 1] = (byte) result[i];
485 }
486 }
487 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700488 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700489 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 if (ar.result == null) {
491 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700493 if (ar.exception != null) {
494 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
495 }
496
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700497 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700498 if (ar.exception instanceof CommandException) {
499 CommandException.Error error =
500 ((CommandException) (ar.exception)).getCommandError();
501 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700502 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700503 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700504 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700505 }
506 }
507 openChannelResp = new IccOpenLogicalChannelResponse(
508 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700509 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700510 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700511 synchronized (request) {
512 request.notifyAll();
513 }
514 break;
515
516 case CMD_CLOSE_CHANNEL:
517 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700518 if (uiccCard == null) {
519 loge("iccCloseLogicalChannel: No UICC");
520 request.result = new IccIoResult(0x6F, 0, (byte[])null);
521 synchronized (request) {
522 request.notifyAll();
523 }
524 } else {
525 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
526 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
527 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700528 break;
529
530 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800531 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
532 break;
533
534 case CMD_NV_READ_ITEM:
535 request = (MainThreadRequest) msg.obj;
536 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
537 mPhone.nvReadItem((Integer) request.argument, onCompleted);
538 break;
539
540 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700541 ar = (AsyncResult) msg.obj;
542 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800543 if (ar.exception == null && ar.result != null) {
544 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700545 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800546 request.result = "";
547 if (ar.result == null) {
548 loge("nvReadItem: Empty response");
549 } else if (ar.exception instanceof CommandException) {
550 loge("nvReadItem: CommandException: " +
551 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700552 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800553 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700554 }
555 }
556 synchronized (request) {
557 request.notifyAll();
558 }
559 break;
560
Jake Hambye994d462014-02-03 13:10:13 -0800561 case CMD_NV_WRITE_ITEM:
562 request = (MainThreadRequest) msg.obj;
563 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
564 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
565 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
566 break;
567
568 case EVENT_NV_WRITE_ITEM_DONE:
569 handleNullReturnEvent(msg, "nvWriteItem");
570 break;
571
572 case CMD_NV_WRITE_CDMA_PRL:
573 request = (MainThreadRequest) msg.obj;
574 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
575 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
576 break;
577
578 case EVENT_NV_WRITE_CDMA_PRL_DONE:
579 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
580 break;
581
582 case CMD_NV_RESET_CONFIG:
583 request = (MainThreadRequest) msg.obj;
584 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
585 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
586 break;
587
588 case EVENT_NV_RESET_CONFIG_DONE:
589 handleNullReturnEvent(msg, "nvResetConfig");
590 break;
591
Jake Hamby7c27be32014-03-03 13:25:59 -0800592 case CMD_GET_PREFERRED_NETWORK_TYPE:
593 request = (MainThreadRequest) msg.obj;
594 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700595 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800596 break;
597
598 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
599 ar = (AsyncResult) msg.obj;
600 request = (MainThreadRequest) ar.userObj;
601 if (ar.exception == null && ar.result != null) {
602 request.result = ar.result; // Integer
603 } else {
604 request.result = -1;
605 if (ar.result == null) {
606 loge("getPreferredNetworkType: Empty response");
607 } else if (ar.exception instanceof CommandException) {
608 loge("getPreferredNetworkType: CommandException: " +
609 ar.exception);
610 } else {
611 loge("getPreferredNetworkType: Unknown exception");
612 }
613 }
614 synchronized (request) {
615 request.notifyAll();
616 }
617 break;
618
619 case CMD_SET_PREFERRED_NETWORK_TYPE:
620 request = (MainThreadRequest) msg.obj;
621 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
622 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700623 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800624 break;
625
626 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
627 handleNullReturnEvent(msg, "setPreferredNetworkType");
628 break;
629
Steven Liu4bf01bc2014-07-17 11:05:29 -0500630 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
631 request = (MainThreadRequest)msg.obj;
632 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
633 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
634 break;
635
636 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
637 ar = (AsyncResult)msg.obj;
638 request = (MainThreadRequest)ar.userObj;
639 request.result = ar;
640 synchronized (request) {
641 request.notifyAll();
642 }
643 break;
644
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800645 case CMD_SET_VOICEMAIL_NUMBER:
646 request = (MainThreadRequest) msg.obj;
647 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
648 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800649 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
650 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800651 break;
652
653 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
654 handleNullReturnEvent(msg, "setVoicemailNumber");
655 break;
656
Stuart Scott54788802015-03-30 13:18:01 -0700657 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
658 request = (MainThreadRequest) msg.obj;
659 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
660 request);
661 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
662 break;
663
664 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
665 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
666 break;
667
Shishir Agrawal302c8692015-06-19 13:49:39 -0700668 case CMD_PERFORM_NETWORK_SCAN:
669 request = (MainThreadRequest) msg.obj;
670 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
671 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
672 break;
673
674 case EVENT_PERFORM_NETWORK_SCAN_DONE:
675 ar = (AsyncResult) msg.obj;
676 request = (MainThreadRequest) ar.userObj;
677 CellNetworkScanResult cellScanResult;
678 if (ar.exception == null && ar.result != null) {
679 cellScanResult = new CellNetworkScanResult(
680 CellNetworkScanResult.STATUS_SUCCESS,
681 (List<OperatorInfo>) ar.result);
682 } else {
683 if (ar.result == null) {
684 loge("getCellNetworkScanResults: Empty response");
685 }
686 if (ar.exception != null) {
687 loge("getCellNetworkScanResults: Exception: " + ar.exception);
688 }
689 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
690 if (ar.exception instanceof CommandException) {
691 CommandException.Error error =
692 ((CommandException) (ar.exception)).getCommandError();
693 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
694 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
695 } else if (error == CommandException.Error.GENERIC_FAILURE) {
696 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
697 }
698 }
699 cellScanResult = new CellNetworkScanResult(errorCode, null);
700 }
701 request.result = cellScanResult;
702 synchronized (request) {
703 request.notifyAll();
704 }
705 break;
706
707 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
708 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700709 ManualNetworkSelectionArgument selArg =
710 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700711 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
712 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700713 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
714 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700715 break;
716
717 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
718 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
719 break;
720
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700721 case CMD_GET_MODEM_ACTIVITY_INFO:
722 request = (MainThreadRequest) msg.obj;
723 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700724 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700725 break;
726
727 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
728 ar = (AsyncResult) msg.obj;
729 request = (MainThreadRequest) ar.userObj;
730 if (ar.exception == null && ar.result != null) {
731 request.result = ar.result;
732 } else {
733 if (ar.result == null) {
734 loge("queryModemActivityInfo: Empty response");
735 } else if (ar.exception instanceof CommandException) {
736 loge("queryModemActivityInfo: CommandException: " +
737 ar.exception);
738 } else {
739 loge("queryModemActivityInfo: Unknown exception");
740 }
741 }
742 synchronized (request) {
743 request.notifyAll();
744 }
745 break;
746
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700747 default:
748 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
749 break;
750 }
751 }
Jake Hambye994d462014-02-03 13:10:13 -0800752
753 private void handleNullReturnEvent(Message msg, String command) {
754 AsyncResult ar = (AsyncResult) msg.obj;
755 MainThreadRequest request = (MainThreadRequest) ar.userObj;
756 if (ar.exception == null) {
757 request.result = true;
758 } else {
759 request.result = false;
760 if (ar.exception instanceof CommandException) {
761 loge(command + ": CommandException: " + ar.exception);
762 } else {
763 loge(command + ": Unknown exception");
764 }
765 }
766 synchronized (request) {
767 request.notifyAll();
768 }
769 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700770 }
771
772 /**
773 * Posts the specified command to be executed on the main thread,
774 * waits for the request to complete, and returns the result.
775 * @see #sendRequestAsync
776 */
777 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700778 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700779 }
780
781 /**
782 * Posts the specified command to be executed on the main thread,
783 * waits for the request to complete, and returns the result.
784 * @see #sendRequestAsync
785 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800786 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700787 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
788 throw new RuntimeException("This method will deadlock if called from the main thread.");
789 }
790
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800791 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700792 Message msg = mMainThreadHandler.obtainMessage(command, request);
793 msg.sendToTarget();
794
795 // Wait for the request to complete
796 synchronized (request) {
797 while (request.result == null) {
798 try {
799 request.wait();
800 } catch (InterruptedException e) {
801 // Do nothing, go back and wait until the request is complete
802 }
803 }
804 }
805 return request.result;
806 }
807
808 /**
809 * Asynchronous ("fire and forget") version of sendRequest():
810 * Posts the specified command to be executed on the main thread, and
811 * returns immediately.
812 * @see #sendRequest
813 */
814 private void sendRequestAsync(int command) {
815 mMainThreadHandler.sendEmptyMessage(command);
816 }
817
818 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700819 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
820 * @see {@link #sendRequest(int,Object)}
821 */
822 private void sendRequestAsync(int command, Object argument) {
823 MainThreadRequest request = new MainThreadRequest(argument);
824 Message msg = mMainThreadHandler.obtainMessage(command, request);
825 msg.sendToTarget();
826 }
827
828 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700829 * Initialize the singleton PhoneInterfaceManager instance.
830 * This is only done once, at startup, from PhoneApp.onCreate().
831 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700832 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 synchronized (PhoneInterfaceManager.class) {
834 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700835 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 } else {
837 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
838 }
839 return sInstance;
840 }
841 }
842
843 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700844 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 mApp = app;
846 mPhone = phone;
847 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700848 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
850 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700851 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700852 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800853 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800854
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700855 publish();
856 }
857
858 private void publish() {
859 if (DBG) log("publish: " + this);
860
861 ServiceManager.addService("phone", this);
862 }
863
Stuart Scott584921c2015-01-15 17:10:34 -0800864 private Phone getPhoneFromRequest(MainThreadRequest request) {
865 return (request.subId == null) ? mPhone : getPhone(request.subId);
866 }
867
Wink Saville36469e72014-06-11 15:17:00 -0700868 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700869 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800870 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700871 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700872 //
873 // Implementation of the ITelephony interface.
874 //
875
876 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700877 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700878 }
879
Wink Savilleb564aae2014-10-23 10:18:09 -0700880 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700881 if (DBG) log("dial: " + number);
882 // No permission check needed here: This is just a wrapper around the
883 // ACTION_DIAL intent, which is available to any app since it puts up
884 // the UI before it does anything.
885
886 String url = createTelUrl(number);
887 if (url == null) {
888 return;
889 }
890
891 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700892 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700893 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
894 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
895 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
896 mApp.startActivity(intent);
897 }
898 }
899
900 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700901 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700902 }
903
Wink Savilleb564aae2014-10-23 10:18:09 -0700904 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700905 if (DBG) log("call: " + number);
906
907 // This is just a wrapper around the ACTION_CALL intent, but we still
908 // need to do a permission check since we're calling startActivity()
909 // from the context of the phone app.
910 enforceCallPermission();
911
912 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
913 != AppOpsManager.MODE_ALLOWED) {
914 return;
915 }
916
917 String url = createTelUrl(number);
918 if (url == null) {
919 return;
920 }
921
Wink Saville08874612014-08-31 19:19:58 -0700922 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100923 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800924 if (slist != null) {
925 for (SubscriptionInfo subInfoRecord : slist) {
926 if (subInfoRecord.getSubscriptionId() == subId) {
927 isValid = true;
928 break;
929 }
Wink Saville08874612014-08-31 19:19:58 -0700930 }
931 }
932 if (isValid == false) {
933 return;
934 }
935
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700936 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700937 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700938 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
939 mApp.startActivity(intent);
940 }
941
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 /**
943 * End a call based on call state
944 * @return true is a call was ended
945 */
946 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700947 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700948 }
949
950 /**
951 * End a call based on the call state of the subId
952 * @return true is a call was ended
953 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700954 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700955 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800956 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700957 }
958
959 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700960 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700961 }
962
Wink Savilleb564aae2014-10-23 10:18:09 -0700963 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700964 if (DBG) log("answerRingingCall...");
965 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
966 // but that can probably wait till the big TelephonyManager API overhaul.
967 // For now, protect this call with the MODIFY_PHONE_STATE permission.
968 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800969 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700970 }
971
972 /**
973 * Make the actual telephony calls to implement answerRingingCall().
974 * This should only be called from the main thread of the Phone app.
975 * @see #answerRingingCall
976 *
977 * TODO: it would be nice to return true if we answered the call, or
978 * false if there wasn't actually a ringing incoming call, or some
979 * other error occurred. (In other words, pass back the return value
980 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
981 * But that would require calling this method via sendRequest() rather
982 * than sendRequestAsync(), and right now we don't actually *need* that
983 * return value, so let's just return void for now.
984 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700985 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700986 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700987 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700988 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
989 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 if (hasActiveCall && hasHoldingCall) {
991 // Both lines are in use!
992 // TODO: provide a flag to let the caller specify what
993 // policy to use if both lines are in use. (The current
994 // behavior is hardwired to "answer incoming, end ongoing",
995 // which is how the CALL button is specced to behave.)
996 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
997 return;
998 } else {
999 // answerCall() will automatically hold the current active
1000 // call, if there is one.
1001 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1002 return;
1003 }
1004 } else {
1005 // No call was ringing.
1006 return;
1007 }
1008 }
1009
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001010 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001011 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001012 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001013 public void silenceRinger() {
1014 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 }
1016
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001017 @Override
1018 public boolean isOffhook(String callingPackage) {
1019 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001020 }
1021
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001022 @Override
1023 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1024 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1025 return false;
1026 }
1027
Sanket Padawe356d7632015-06-22 14:03:32 -07001028 final Phone phone = getPhone(subId);
1029 if (phone != null) {
1030 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1031 } else {
1032 return false;
1033 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001034 }
1035
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001036 @Override
1037 public boolean isRinging(String callingPackage) {
1038 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001039 }
1040
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001041 @Override
1042 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1043 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1044 return false;
1045 }
1046
Sanket Padawe356d7632015-06-22 14:03:32 -07001047 final Phone phone = getPhone(subId);
1048 if (phone != null) {
1049 return (phone.getState() == PhoneConstants.State.RINGING);
1050 } else {
1051 return false;
1052 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001053 }
1054
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001055 @Override
1056 public boolean isIdle(String callingPackage) {
1057 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001058 }
1059
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001060 @Override
1061 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1062 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1063 return false;
1064 }
1065
Sanket Padawe356d7632015-06-22 14:03:32 -07001066 final Phone phone = getPhone(subId);
1067 if (phone != null) {
1068 return (phone.getState() == PhoneConstants.State.IDLE);
1069 } else {
1070 return false;
1071 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001072 }
1073
Svet Ganovb320e182015-04-16 12:30:10 -07001074 public boolean isSimPinEnabled(String callingPackage) {
1075 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1076 return false;
1077 }
1078
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001079 return (PhoneGlobals.getInstance().isSimPinEnabled());
1080 }
1081
1082 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001083 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001084 }
1085
Wink Savilleb564aae2014-10-23 10:18:09 -07001086 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001087 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001088 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1089 }
1090
1091 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001092 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001093 }
1094
Wink Savilleb564aae2014-10-23 10:18:09 -07001095 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001096 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001097 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1098 }
1099
1100 /** {@hide} */
1101 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001102 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001103 }
1104
Wink Savilleb564aae2014-10-23 10:18:09 -07001105 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001106 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001107 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 checkSimPin.start();
1109 return checkSimPin.unlockSim(null, pin);
1110 }
1111
Wink Saville9de0f752013-10-22 19:04:03 -07001112 /** {@hide} */
1113 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001114 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001115 }
1116
Wink Savilleb564aae2014-10-23 10:18:09 -07001117 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001118 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001119 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 checkSimPuk.start();
1121 return checkSimPuk.unlockSim(puk, pin);
1122 }
1123
1124 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001125 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001126 * a synchronous one.
1127 */
1128 private static class UnlockSim extends Thread {
1129
1130 private final IccCard mSimCard;
1131
1132 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001133 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1134 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001135
1136 // For replies from SimCard interface
1137 private Handler mHandler;
1138
1139 // For async handler to identify request type
1140 private static final int SUPPLY_PIN_COMPLETE = 100;
1141
1142 public UnlockSim(IccCard simCard) {
1143 mSimCard = simCard;
1144 }
1145
1146 @Override
1147 public void run() {
1148 Looper.prepare();
1149 synchronized (UnlockSim.this) {
1150 mHandler = new Handler() {
1151 @Override
1152 public void handleMessage(Message msg) {
1153 AsyncResult ar = (AsyncResult) msg.obj;
1154 switch (msg.what) {
1155 case SUPPLY_PIN_COMPLETE:
1156 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1157 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001158 mRetryCount = msg.arg1;
1159 if (ar.exception != null) {
1160 if (ar.exception instanceof CommandException &&
1161 ((CommandException)(ar.exception)).getCommandError()
1162 == CommandException.Error.PASSWORD_INCORRECT) {
1163 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1164 } else {
1165 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1166 }
1167 } else {
1168 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1169 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001170 mDone = true;
1171 UnlockSim.this.notifyAll();
1172 }
1173 break;
1174 }
1175 }
1176 };
1177 UnlockSim.this.notifyAll();
1178 }
1179 Looper.loop();
1180 }
1181
1182 /*
1183 * Use PIN or PUK to unlock SIM card
1184 *
1185 * If PUK is null, unlock SIM card with PIN
1186 *
1187 * If PUK is not null, unlock SIM card with PUK and set PIN code
1188 */
Wink Saville9de0f752013-10-22 19:04:03 -07001189 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001190
1191 while (mHandler == null) {
1192 try {
1193 wait();
1194 } catch (InterruptedException e) {
1195 Thread.currentThread().interrupt();
1196 }
1197 }
1198 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1199
1200 if (puk == null) {
1201 mSimCard.supplyPin(pin, callback);
1202 } else {
1203 mSimCard.supplyPuk(puk, pin, callback);
1204 }
1205
1206 while (!mDone) {
1207 try {
1208 Log.d(LOG_TAG, "wait for done");
1209 wait();
1210 } catch (InterruptedException e) {
1211 // Restore the interrupted status
1212 Thread.currentThread().interrupt();
1213 }
1214 }
1215 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001216 int[] resultArray = new int[2];
1217 resultArray[0] = mResult;
1218 resultArray[1] = mRetryCount;
1219 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001220 }
1221 }
1222
1223 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001224 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001225
1226 }
1227
Wink Savilleb564aae2014-10-23 10:18:09 -07001228 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001229 // No permission check needed here: this call is harmless, and it's
1230 // needed for the ServiceState.requestStateUpdate() call (which is
1231 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001232 final Phone phone = getPhone(subId);
1233 if (phone != null) {
1234 phone.updateServiceLocation();
1235 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001236 }
1237
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001238 @Override
1239 public boolean isRadioOn(String callingPackage) {
1240 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001241 }
1242
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001243 @Override
1244 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1245 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1246 return false;
1247 }
1248 return isRadioOnForSubscriber(subId);
1249 }
1250
1251 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001252 final Phone phone = getPhone(subId);
1253 if (phone != null) {
1254 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1255 } else {
1256 return false;
1257 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001258 }
1259
1260 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001261 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001262
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001263 }
Wink Saville36469e72014-06-11 15:17:00 -07001264
Wink Savilleb564aae2014-10-23 10:18:09 -07001265 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001267 final Phone phone = getPhone(subId);
1268 if (phone != null) {
1269 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1270 }
Wink Saville36469e72014-06-11 15:17:00 -07001271 }
1272
1273 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001274 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001275 }
1276
Wink Savilleb564aae2014-10-23 10:18:09 -07001277 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001278 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001279 final Phone phone = getPhone(subId);
1280 if (phone == null) {
1281 return false;
1282 }
1283 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001284 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001285 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001286 }
1287 return true;
1288 }
Wink Saville36469e72014-06-11 15:17:00 -07001289
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001290 public boolean needMobileRadioShutdown() {
1291 /*
1292 * If any of the Radios are available, it will need to be
1293 * shutdown. So return true if any Radio is available.
1294 */
1295 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1296 Phone phone = PhoneFactory.getPhone(i);
1297 if (phone != null && phone.isRadioAvailable()) return true;
1298 }
1299 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1300 return false;
1301 }
1302
1303 public void shutdownMobileRadios() {
1304 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1305 logv("Shutting down Phone " + i);
1306 shutdownRadioUsingPhoneId(i);
1307 }
1308 }
1309
1310 private void shutdownRadioUsingPhoneId(int phoneId) {
1311 enforceModifyPermission();
1312 Phone phone = PhoneFactory.getPhone(phoneId);
1313 if (phone != null && phone.isRadioAvailable()) {
1314 phone.shutdownRadio();
1315 }
1316 }
1317
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001318 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001319 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001320 }
1321
Wink Savilleb564aae2014-10-23 10:18:09 -07001322 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001323 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001324 final Phone phone = getPhone(subId);
1325 if (phone != null) {
1326 phone.setRadioPower(turnOn);
1327 return true;
1328 } else {
1329 return false;
1330 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001331 }
1332
Wink Saville36469e72014-06-11 15:17:00 -07001333 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001334 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 public boolean enableDataConnectivity() {
1336 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001337 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001338 final Phone phone = getPhone(subId);
1339 if (phone != null) {
1340 phone.setDataEnabled(true);
1341 return true;
1342 } else {
1343 return false;
1344 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001345 }
1346
Wink Saville36469e72014-06-11 15:17:00 -07001347 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001348 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 public boolean disableDataConnectivity() {
1350 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001351 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001352 final Phone phone = getPhone(subId);
1353 if (phone != null) {
1354 phone.setDataEnabled(false);
1355 return true;
1356 } else {
1357 return false;
1358 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001359 }
1360
Wink Saville36469e72014-06-11 15:17:00 -07001361 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001362 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001364 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001365 final Phone phone = getPhone(subId);
1366 if (phone != null) {
1367 return phone.isDataConnectivityPossible();
1368 } else {
1369 return false;
1370 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001371 }
1372
1373 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001374 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001375 }
1376
Wink Savilleb564aae2014-10-23 10:18:09 -07001377 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001379 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1380 return false;
1381 }
Wink Saville36469e72014-06-11 15:17:00 -07001382 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001383 }
1384
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001386 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001387 }
1388
Wink Savilleb564aae2014-10-23 10:18:09 -07001389 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001390 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001391 }
1392
Sanket Padawe356d7632015-06-22 14:03:32 -07001393 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001395 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001396 if (phone != null) {
1397 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1398 } else {
1399 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1400 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001401 }
1402
Sanket Padawe356d7632015-06-22 14:03:32 -07001403 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001405 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001406 if (phone != null) {
1407 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1408 } else {
1409 return TelephonyManager.DATA_ACTIVITY_NONE;
1410 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001411 }
1412
1413 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001414 public Bundle getCellLocation(String callingPackage) {
1415 enforceFineOrCoarseLocationPermission("getCellLocation");
1416
1417 // OP_COARSE_LOCATION controls both fine and coarse location.
1418 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1419 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001420 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001421 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 }
1423
Jake Hambye994d462014-02-03 13:10:13 -08001424 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 if (DBG_LOC) log("getCellLocation: is active user");
1426 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001427 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001428 if (phone == null) {
1429 return null;
1430 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001431 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 return data;
1433 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001434 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 return null;
1436 }
1437 }
1438
Svetoslav64fad262015-04-14 14:35:21 -07001439 private void enforceFineOrCoarseLocationPermission(String message) {
1440 try {
1441 mApp.enforceCallingOrSelfPermission(
1442 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1443 } catch (SecurityException e) {
1444 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1445 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1446 // is the weaker precondition
1447 mApp.enforceCallingOrSelfPermission(
1448 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1449 }
1450 }
1451
1452
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001453 @Override
1454 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001455 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001456 }
1457
Sanket Padawe356d7632015-06-22 14:03:32 -07001458 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001459 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001460 mApp.enforceCallingOrSelfPermission(
1461 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001462 final Phone phone = getPhone(subId);
1463 if (phone != null) {
1464 phone.enableLocationUpdates();
1465 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001466 }
1467
1468 @Override
1469 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001470 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001471 }
1472
Sanket Padawe356d7632015-06-22 14:03:32 -07001473 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001474 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 mApp.enforceCallingOrSelfPermission(
1476 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001477 final Phone phone = getPhone(subId);
1478 if (phone != null) {
1479 phone.disableLocationUpdates();
1480 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001481 }
1482
1483 @Override
1484 @SuppressWarnings("unchecked")
1485 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001486 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1487
1488 // OP_COARSE_LOCATION controls both fine and coarse location.
1489 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1490 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1491 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001492 }
1493
1494 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1495 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1496 return null;
1497 }
Svetoslav64fad262015-04-14 14:35:21 -07001498
Jake Hambye994d462014-02-03 13:10:13 -08001499 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001500 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1501
1502 ArrayList<NeighboringCellInfo> cells = null;
1503
1504 try {
1505 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001506 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001507 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001508 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001509 }
1510 return cells;
1511 } else {
1512 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1513 return null;
1514 }
1515 }
1516
1517
1518 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001519 public List<CellInfo> getAllCellInfo(String callingPackage) {
1520 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1521
1522 // OP_COARSE_LOCATION controls both fine and coarse location.
1523 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1524 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1525 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001526 }
1527
Jake Hambye994d462014-02-03 13:10:13 -08001528 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001530 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1531 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001532 final List<CellInfo> info = phone.getAllCellInfo();
1533 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001534 }
1535 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001536 } else {
1537 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1538 return null;
1539 }
1540 }
1541
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001542 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001543 public void setCellInfoListRate(int rateInMillis) {
1544 mPhone.setCellInfoListRate(rateInMillis);
1545 }
1546
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 //
1548 // Internal helper methods.
1549 //
1550
Jake Hambye994d462014-02-03 13:10:13 -08001551 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001552 boolean ok;
1553
1554 boolean self = Binder.getCallingUid() == Process.myUid();
1555 if (!self) {
1556 // Get the caller's user id then clear the calling identity
1557 // which will be restored in the finally clause.
1558 int callingUser = UserHandle.getCallingUserId();
1559 long ident = Binder.clearCallingIdentity();
1560
1561 try {
1562 // With calling identity cleared the current user is the foreground user.
1563 int foregroundUser = ActivityManager.getCurrentUser();
1564 ok = (foregroundUser == callingUser);
1565 if (DBG_LOC) {
1566 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1567 + " callingUser=" + callingUser + " ok=" + ok);
1568 }
1569 } catch (Exception ex) {
1570 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1571 ok = false;
1572 } finally {
1573 Binder.restoreCallingIdentity(ident);
1574 }
1575 } else {
1576 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1577 ok = true;
1578 }
1579 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1580 return ok;
1581 }
1582
1583 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001584 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1585 *
1586 * @throws SecurityException if the caller does not have the required permission
1587 */
1588 private void enforceModifyPermission() {
1589 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1590 }
1591
1592 /**
Junda Liua2e36012014-07-09 18:30:01 -07001593 * Make sure either system app or the caller has carrier privilege.
1594 *
1595 * @throws SecurityException if the caller does not have the required permission/privilege
1596 */
1597 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001598 int permission = mApp.checkCallingOrSelfPermission(
1599 android.Manifest.permission.MODIFY_PHONE_STATE);
1600 if (permission == PackageManager.PERMISSION_GRANTED) {
1601 return;
1602 }
1603
1604 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001605 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001606 loge("No Carrier Privilege.");
1607 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001608 }
1609 }
1610
1611 /**
1612 * Make sure the caller has carrier privilege.
1613 *
1614 * @throws SecurityException if the caller does not have the required permission
1615 */
1616 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001617 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001618 loge("No Carrier Privilege.");
1619 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001620 }
1621 }
1622
1623 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001624 * Make sure the caller has the CALL_PHONE permission.
1625 *
1626 * @throws SecurityException if the caller does not have the required permission
1627 */
1628 private void enforceCallPermission() {
1629 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1630 }
1631
Stuart Scott8eef64f2015-04-08 15:13:54 -07001632 private void enforceConnectivityInternalPermission() {
1633 mApp.enforceCallingOrSelfPermission(
1634 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1635 "ConnectivityService");
1636 }
1637
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001638 private String createTelUrl(String number) {
1639 if (TextUtils.isEmpty(number)) {
1640 return null;
1641 }
1642
Jake Hambye994d462014-02-03 13:10:13 -08001643 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001644 }
1645
Ihab Awadf9e92732013-12-05 18:02:52 -08001646 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001647 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1648 }
1649
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001650 private static void logv(String msg) {
1651 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1652 }
1653
Ihab Awadf9e92732013-12-05 18:02:52 -08001654 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001655 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1656 }
1657
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001658 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001660 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001661 }
1662
Sanket Padawe356d7632015-06-22 14:03:32 -07001663 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001664 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001665 final Phone phone = getPhone(subId);
1666 if (phone == null) {
1667 return PhoneConstants.PHONE_TYPE_NONE;
1668 } else {
1669 return getPhone(subId).getPhoneType();
1670 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001671 }
1672
1673 /**
1674 * Returns the CDMA ERI icon index to display
1675 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001676 @Override
1677 public int getCdmaEriIconIndex(String callingPackage) {
1678 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001679 }
1680
Sanket Padawe356d7632015-06-22 14:03:32 -07001681 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001682 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1683 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1684 return -1;
1685 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001686 final Phone phone = getPhone(subId);
1687 if (phone != null) {
1688 return phone.getCdmaEriIconIndex();
1689 } else {
1690 return -1;
1691 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001692 }
1693
1694 /**
1695 * Returns the CDMA ERI icon mode,
1696 * 0 - ON
1697 * 1 - FLASHING
1698 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001699 @Override
1700 public int getCdmaEriIconMode(String callingPackage) {
1701 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001702 }
1703
Sanket Padawe356d7632015-06-22 14:03:32 -07001704 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001705 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1706 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1707 return -1;
1708 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001709 final Phone phone = getPhone(subId);
1710 if (phone != null) {
1711 return phone.getCdmaEriIconMode();
1712 } else {
1713 return -1;
1714 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001715 }
1716
1717 /**
1718 * Returns the CDMA ERI text,
1719 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001720 @Override
1721 public String getCdmaEriText(String callingPackage) {
1722 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001723 }
1724
Sanket Padawe356d7632015-06-22 14:03:32 -07001725 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001726 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1727 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1728 return null;
1729 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001730 final Phone phone = getPhone(subId);
1731 if (phone != null) {
1732 return phone.getCdmaEriText();
1733 } else {
1734 return null;
1735 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001736 }
1737
1738 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001739 * Returns the CDMA MDN.
1740 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001741 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001742 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001743 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001744 final Phone phone = getPhone(subId);
1745 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1746 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001747 } else {
1748 return null;
1749 }
1750 }
1751
1752 /**
1753 * Returns the CDMA MIN.
1754 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001755 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001756 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001757 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001758 final Phone phone = getPhone(subId);
1759 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1760 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001761 } else {
1762 return null;
1763 }
1764 }
1765
1766 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001767 * Returns true if CDMA provisioning needs to run.
1768 */
1769 public boolean needsOtaServiceProvisioning() {
1770 return mPhone.needsOtaServiceProvisioning();
1771 }
1772
1773 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001774 * Sets the voice mail number of a given subId.
1775 */
1776 @Override
1777 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001778 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001779 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1780 new Pair<String, String>(alphaTag, number), new Integer(subId));
1781 return success;
1782 }
1783
1784 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001785 * Returns the unread count of voicemails
1786 */
1787 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001788 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001789 }
1790
1791 /**
1792 * Returns the unread count of voicemails for a subId
1793 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001794 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001795 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001796 final Phone phone = getPhone(subId);
1797 if (phone != null) {
1798 return phone.getVoiceMessageCount();
1799 } else {
1800 return 0;
1801 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001802 }
1803
1804 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001805 * Returns the data network type.
1806 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001807 *
1808 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1809 */
1810 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001811 public int getNetworkType() {
1812 final Phone phone = getPhone(getDefaultSubscription());
1813 if (phone != null) {
1814 return phone.getServiceState().getDataNetworkType();
1815 } else {
1816 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1817 }
Wink Saville36469e72014-06-11 15:17:00 -07001818 }
1819
1820 /**
1821 * Returns the network type for a subId
1822 */
1823 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001824 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1825 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1826 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1827 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001828
Sanket Padawe356d7632015-06-22 14:03:32 -07001829 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001830 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001831 return phone.getServiceState().getDataNetworkType();
1832 } else {
1833 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1834 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001835 }
1836
1837 /**
1838 * Returns the data network type
1839 */
1840 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001841 public int getDataNetworkType(String callingPackage) {
1842 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001843 }
1844
1845 /**
1846 * Returns the data network type for a subId
1847 */
1848 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001849 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1850 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1851 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1852 }
1853
Sanket Padawe356d7632015-06-22 14:03:32 -07001854 final Phone phone = getPhone(subId);
1855 if (phone != null) {
1856 return phone.getServiceState().getDataNetworkType();
1857 } else {
1858 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1859 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001860 }
1861
1862 /**
Wink Saville36469e72014-06-11 15:17:00 -07001863 * Returns the Voice network type for a subId
1864 */
1865 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001866 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1867 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1868 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1869 }
1870
Sanket Padawe356d7632015-06-22 14:03:32 -07001871 final Phone phone = getPhone(subId);
1872 if (phone != null) {
1873 return phone.getServiceState().getVoiceNetworkType();
1874 } else {
1875 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1876 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001877 }
1878
1879 /**
1880 * @return true if a ICC card is present
1881 */
1882 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001883 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001884 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001885 }
1886
1887 /**
1888 * @return true if a ICC card is present for a slotId
1889 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001890 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001891 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001892 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001893 final Phone phone = getPhone(subId[0]);
1894 if (subId != null && phone != null) {
1895 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001896 } else {
1897 return false;
1898 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001899 }
1900
1901 /**
1902 * Return if the current radio is LTE on CDMA. This
1903 * is a tri-state return value as for a period of time
1904 * the mode may be unknown.
1905 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001906 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001907 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001908 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001909 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001910 @Override
1911 public int getLteOnCdmaMode(String callingPackage) {
1912 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001913 }
1914
Sanket Padawe356d7632015-06-22 14:03:32 -07001915 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001916 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1917 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1918 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1919 }
1920
Sanket Padawe356d7632015-06-22 14:03:32 -07001921 final Phone phone = getPhone(subId);
1922 if (phone == null) {
1923 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1924 } else {
1925 return phone.getLteOnCdmaMode();
1926 }
Wink Saville36469e72014-06-11 15:17:00 -07001927 }
1928
1929 public void setPhone(Phone phone) {
1930 mPhone = phone;
1931 }
1932
1933 /**
1934 * {@hide}
1935 * Returns Default subId, 0 in the case of single standby.
1936 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001937 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001938 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001939 }
1940
Wink Savilleb564aae2014-10-23 10:18:09 -07001941 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001942 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001943 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001944
1945 /**
1946 * @see android.telephony.TelephonyManager.WifiCallingChoices
1947 */
1948 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001949 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1950 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001951 }
1952
1953 /**
1954 * @see android.telephony.TelephonyManager.WifiCallingChoices
1955 */
1956 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001957 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1958 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1959 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001960 }
1961
Sailesh Nepald1e68152013-12-12 19:08:02 -08001962 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001963 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001964 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001965 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001966
Shishir Agrawal566b7612013-10-28 14:41:00 -07001967 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001968 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001969 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001970
1971 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001972 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1973 CMD_OPEN_CHANNEL, AID);
1974 if (DBG) log("iccOpenLogicalChannel: " + response);
1975 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001976 }
1977
1978 @Override
1979 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001980 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001981
1982 if (DBG) log("iccCloseLogicalChannel: " + channel);
1983 if (channel < 0) {
1984 return false;
1985 }
Jake Hambye994d462014-02-03 13:10:13 -08001986 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001987 if (DBG) log("iccCloseLogicalChannel: " + success);
1988 return success;
1989 }
1990
1991 @Override
1992 public String iccTransmitApduLogicalChannel(int channel, int cla,
1993 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001994 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001995
1996 if (DBG) {
1997 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1998 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
1999 " data=" + data);
2000 }
2001
2002 if (channel < 0) {
2003 return "";
2004 }
2005
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002006 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002007 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2008 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2009
Shishir Agrawal566b7612013-10-28 14:41:00 -07002010 // Append the returned status code to the end of the response payload.
2011 String s = Integer.toHexString(
2012 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002013 if (response.payload != null) {
2014 s = IccUtils.bytesToHexString(response.payload) + s;
2015 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002016 return s;
2017 }
Jake Hambye994d462014-02-03 13:10:13 -08002018
Evan Charltonc66da362014-05-16 14:06:40 -07002019 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002020 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2021 int p3, String data) {
2022 enforceModifyPermissionOrCarrierPrivilege();
2023
2024 if (DBG) {
2025 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2026 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2027 }
2028
2029 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2030 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2031 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2032
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002033 // Append the returned status code to the end of the response payload.
2034 String s = Integer.toHexString(
2035 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002036 if (response.payload != null) {
2037 s = IccUtils.bytesToHexString(response.payload) + s;
2038 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002039 return s;
2040 }
2041
2042 @Override
2043 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2044 String filePath) {
2045 enforceModifyPermissionOrCarrierPrivilege();
2046
2047 if (DBG) {
2048 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2049 p1 + " " + p2 + " " + p3 + ":" + filePath);
2050 }
2051
2052 IccIoResult response =
2053 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002054 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002055
2056 if (DBG) {
2057 log("Exchange SIM_IO [R]" + response);
2058 }
2059
2060 byte[] result = null;
2061 int length = 2;
2062 if (response.payload != null) {
2063 length = 2 + response.payload.length;
2064 result = new byte[length];
2065 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2066 } else {
2067 result = new byte[length];
2068 }
2069
2070 result[length - 1] = (byte) response.sw2;
2071 result[length - 2] = (byte) response.sw1;
2072 return result;
2073 }
2074
2075 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002076 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002077 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002078
2079 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2080 if (response.payload == null) {
2081 return "";
2082 }
2083
2084 // Append the returned status code to the end of the response payload.
2085 String s = Integer.toHexString(
2086 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2087 s = IccUtils.bytesToHexString(response.payload) + s;
2088 return s;
2089 }
2090
Jake Hambye994d462014-02-03 13:10:13 -08002091 /**
2092 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2093 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2094 *
2095 * @param itemID the ID of the item to read
2096 * @return the NV item as a String, or null on error.
2097 */
2098 @Override
2099 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002100 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002101 if (DBG) log("nvReadItem: item " + itemID);
2102 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2103 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2104 return value;
2105 }
2106
2107 /**
2108 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2109 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2110 *
2111 * @param itemID the ID of the item to read
2112 * @param itemValue the value to write, as a String
2113 * @return true on success; false on any failure
2114 */
2115 @Override
2116 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002117 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002118 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2119 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2120 new Pair<Integer, String>(itemID, itemValue));
2121 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2122 return success;
2123 }
2124
2125 /**
2126 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2127 * Used for device configuration by some CDMA operators.
2128 *
2129 * @param preferredRoamingList byte array containing the new PRL
2130 * @return true on success; false on any failure
2131 */
2132 @Override
2133 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002134 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002135 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2136 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2137 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2138 return success;
2139 }
2140
2141 /**
2142 * Perform the specified type of NV config reset.
2143 * Used for device configuration by some CDMA operators.
2144 *
2145 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2146 * @return true on success; false on any failure
2147 */
2148 @Override
2149 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002150 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002151 if (DBG) log("nvResetConfig: type " + resetType);
2152 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2153 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2154 return success;
2155 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002156
2157 /**
Wink Saville36469e72014-06-11 15:17:00 -07002158 * {@hide}
2159 * Returns Default sim, 0 in the case of single standby.
2160 */
2161 public int getDefaultSim() {
2162 //TODO Need to get it from Telephony Devcontroller
2163 return 0;
2164 }
2165
Svet Ganovb320e182015-04-16 12:30:10 -07002166 public String[] getPcscfAddress(String apnType, String callingPackage) {
2167 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2168 return new String[0];
2169 }
2170
2171
ram87fca6f2014-07-18 18:58:44 +05302172 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002173 }
2174
2175 public void setImsRegistrationState(boolean registered) {
2176 enforceModifyPermission();
2177 mPhone.setImsRegistrationState(registered);
2178 }
2179
2180 /**
Stuart Scott54788802015-03-30 13:18:01 -07002181 * Set the network selection mode to automatic.
2182 *
2183 */
2184 @Override
2185 public void setNetworkSelectionModeAutomatic(int subId) {
2186 enforceModifyPermissionOrCarrierPrivilege();
2187 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2188 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2189 }
2190
2191 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002192 * Set the network selection mode to manual with the selected carrier.
2193 */
2194 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002195 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2196 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002197 enforceModifyPermissionOrCarrierPrivilege();
2198 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002199 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2200 persistSelection);
2201 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002202 }
2203
2204 /**
2205 * Scans for available networks.
2206 */
2207 @Override
2208 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2209 enforceModifyPermissionOrCarrierPrivilege();
2210 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2211 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2212 CMD_PERFORM_NETWORK_SCAN, null, subId);
2213 return result;
2214 }
2215
2216 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002217 * Get the calculated preferred network type.
2218 * Used for debugging incorrect network type.
2219 *
2220 * @return the preferred network type, defined in RILConstants.java.
2221 */
2222 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002223 public int getCalculatedPreferredNetworkType(String callingPackage) {
2224 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2225 return RILConstants.PREFERRED_NETWORK_MODE;
2226 }
2227
Amit Mahajan43330e02014-11-18 11:54:45 -08002228 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002229 }
2230
2231 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002232 * Get the preferred network type.
2233 * Used for device configuration by some CDMA operators.
2234 *
2235 * @return the preferred network type, defined in RILConstants.java.
2236 */
2237 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002238 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002239 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002240 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002241 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002242 int networkType = (result != null ? result[0] : -1);
2243 if (DBG) log("getPreferredNetworkType: " + networkType);
2244 return networkType;
2245 }
2246
2247 /**
2248 * Set the preferred network type.
2249 * Used for device configuration by some CDMA operators.
2250 *
2251 * @param networkType the preferred network type, defined in RILConstants.java.
2252 * @return true on success; false on any failure.
2253 */
2254 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002255 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002256 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002257 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2258 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002259 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002260 if (success) {
2261 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002262 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002263 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002264 return success;
2265 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002266
2267 /**
Junda Liu475951f2014-11-07 16:45:03 -08002268 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2269 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2270 * tethering.
2271 *
2272 * @return 0: Not required. 1: required. 2: Not set.
2273 * @hide
2274 */
2275 @Override
2276 public int getTetherApnRequired() {
2277 enforceModifyPermissionOrCarrierPrivilege();
2278 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2279 Settings.Global.TETHER_DUN_REQUIRED, 2);
2280 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2281 // config_tether_apndata.
2282 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2283 dunRequired = 1;
2284 }
2285 return dunRequired;
2286 }
2287
2288 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002289 * Set mobile data enabled
2290 * Used by the user through settings etc to turn on/off mobile data
2291 *
2292 * @param enable {@code true} turn turn data on, else {@code false}
2293 */
2294 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002295 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002296 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002297 int phoneId = mSubscriptionController.getPhoneId(subId);
2298 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2299 Phone phone = PhoneFactory.getPhone(phoneId);
2300 if (phone != null) {
2301 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2302 phone.setDataEnabled(enable);
2303 } else {
2304 loge("setDataEnabled: no phone for subId=" + subId);
2305 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002306 }
2307
2308 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002309 * Get whether mobile data is enabled.
2310 *
2311 * Note that this used to be available from ConnectivityService, gated by
2312 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2313 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002314 *
2315 * @return {@code true} if data is enabled else {@code false}
2316 */
2317 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002318 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002319 try {
2320 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2321 null);
2322 } catch (Exception e) {
2323 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2324 null);
2325 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002326 int phoneId = mSubscriptionController.getPhoneId(subId);
2327 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2328 Phone phone = PhoneFactory.getPhone(phoneId);
2329 if (phone != null) {
2330 boolean retVal = phone.getDataEnabled();
2331 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2332 return retVal;
2333 } else {
2334 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2335 return false;
2336 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002337 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002338
2339 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002340 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002341 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002342 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002343 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002344 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2345 }
2346 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002347 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002348 }
Junda Liu29340342014-07-10 15:23:27 -07002349
2350 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002351 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002352 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002353 if (card == null) {
2354 loge("checkCarrierPrivilegesForPackage: No UICC");
2355 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2356 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002357 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2358 }
2359
2360 @Override
2361 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2362 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2363 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2364 UiccCard card = UiccController.getInstance().getUiccCard(i);
2365 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002366 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002367 continue;
2368 }
2369
2370 result = card.getCarrierPrivilegeStatus(
2371 mPhone.getContext().getPackageManager(), pkgName);
2372 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2373 break;
2374 }
2375 }
2376
2377 return result;
Junda Liu29340342014-07-10 15:23:27 -07002378 }
Derek Tan89e89d42014-07-08 17:00:10 -07002379
2380 @Override
Junda Liue64de782015-04-16 17:19:16 -07002381 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2382 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2383 loge("phoneId " + phoneId + " is not valid.");
2384 return null;
2385 }
2386 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002387 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002388 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002389 return null ;
2390 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002391 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002392 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002393 }
2394
Wink Savilleb564aae2014-10-23 10:18:09 -07002395 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002396 final Phone phone = getPhone(subId);
2397 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002398 if (card == null) {
2399 loge("getIccId: No UICC");
2400 return null;
2401 }
2402 String iccId = card.getIccId();
2403 if (TextUtils.isEmpty(iccId)) {
2404 loge("getIccId: ICC ID is null or empty.");
2405 return null;
2406 }
2407 return iccId;
2408 }
2409
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002410 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002411 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2412 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002413 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002414
Jeff Sharkey85190e62014-12-05 09:40:12 -08002415 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002416 final Phone phone = getPhone(subId);
2417 if (phone == null) {
2418 return false;
2419 }
2420 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002421
2422 if (DBG_MERGE) {
2423 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2424 + subscriberId + " to " + number);
2425 }
2426
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002427 if (TextUtils.isEmpty(iccId)) {
2428 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002429 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002430
Jeff Sharkey85190e62014-12-05 09:40:12 -08002431 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2432
2433 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002434 if (alphaTag == null) {
2435 editor.remove(alphaTagPrefKey);
2436 } else {
2437 editor.putString(alphaTagPrefKey, alphaTag);
2438 }
2439
Jeff Sharkey85190e62014-12-05 09:40:12 -08002440 // Record both the line number and IMSI for this ICCID, since we need to
2441 // track all merged IMSIs based on line number
2442 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2443 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002444 if (number == null) {
2445 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002446 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002447 } else {
2448 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002449 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002450 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002451
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002452 editor.commit();
2453 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002454 }
2455
2456 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002457 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002458 // This is open to apps with WRITE_SMS.
2459 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002460 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002461 return null;
2462 }
Derek Tan97ebb422014-09-05 16:55:38 -07002463
2464 String iccId = getIccId(subId);
2465 if (iccId != null) {
2466 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002467 if (DBG_MERGE) {
2468 log("getLine1NumberForDisplay returning " +
2469 mTelephonySharedPreferences.getString(numberPrefKey, null));
2470 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002471 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002472 }
Jack Yue7939d82015-12-10 16:04:02 -08002473 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002474 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002475 }
2476
2477 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002478 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2479 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2480 return null;
2481 }
Derek Tan97ebb422014-09-05 16:55:38 -07002482
2483 String iccId = getIccId(subId);
2484 if (iccId != null) {
2485 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002486 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002487 }
Derek Tan97ebb422014-09-05 16:55:38 -07002488 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002489 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002490
2491 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002492 public String[] getMergedSubscriberIds(String callingPackage) {
2493 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2494 return null;
2495 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002496 final Context context = mPhone.getContext();
2497 final TelephonyManager tele = TelephonyManager.from(context);
2498 final SubscriptionManager sub = SubscriptionManager.from(context);
2499
2500 // Figure out what subscribers are currently active
2501 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002502 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2503 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2504 final long identity = Binder.clearCallingIdentity();
2505 try {
2506 final int[] subIds = sub.getActiveSubscriptionIdList();
2507 for (int subId : subIds) {
2508 activeSubscriberIds.add(tele.getSubscriberId(subId));
2509 }
2510 } finally {
2511 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002512 }
2513
2514 // First pass, find a number override for an active subscriber
2515 String mergeNumber = null;
2516 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2517 for (String key : prefs.keySet()) {
2518 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2519 final String subscriberId = (String) prefs.get(key);
2520 if (activeSubscriberIds.contains(subscriberId)) {
2521 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2522 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2523 mergeNumber = (String) prefs.get(numberKey);
2524 if (DBG_MERGE) {
2525 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2526 + " for active subscriber " + subscriberId);
2527 }
2528 if (!TextUtils.isEmpty(mergeNumber)) {
2529 break;
2530 }
2531 }
2532 }
2533 }
2534
2535 // Shortcut when no active merged subscribers
2536 if (TextUtils.isEmpty(mergeNumber)) {
2537 return null;
2538 }
2539
2540 // Second pass, find all subscribers under that line override
2541 final ArraySet<String> result = new ArraySet<>();
2542 for (String key : prefs.keySet()) {
2543 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2544 final String number = (String) prefs.get(key);
2545 if (mergeNumber.equals(number)) {
2546 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2547 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2548 final String subscriberId = (String) prefs.get(subscriberKey);
2549 if (!TextUtils.isEmpty(subscriberId)) {
2550 result.add(subscriberId);
2551 }
2552 }
2553 }
2554 }
2555
2556 final String[] resultArray = result.toArray(new String[result.size()]);
2557 Arrays.sort(resultArray);
2558 if (DBG_MERGE) {
2559 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2560 }
2561 return resultArray;
2562 }
2563
2564 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002565 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002566 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002567 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002568 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002569
2570 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002571 public boolean setRoamingOverride(List<String> gsmRoamingList,
2572 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2573 List<String> cdmaNonRoamingList) {
2574 enforceCarrierPrivilege();
2575 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2576 cdmaNonRoamingList);
2577 }
2578
2579 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002580 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2581 enforceModifyPermission();
2582
2583 int returnValue = 0;
2584 try {
2585 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2586 if(result.exception == null) {
2587 if (result.result != null) {
2588 byte[] responseData = (byte[])(result.result);
2589 if(responseData.length > oemResp.length) {
2590 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2591 responseData.length + "bytes. Buffer Size is " +
2592 oemResp.length + "bytes.");
2593 }
2594 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2595 returnValue = responseData.length;
2596 }
2597 } else {
2598 CommandException ex = (CommandException) result.exception;
2599 returnValue = ex.getCommandError().ordinal();
2600 if(returnValue > 0) returnValue *= -1;
2601 }
2602 } catch (RuntimeException e) {
2603 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2604 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2605 if(returnValue > 0) returnValue *= -1;
2606 }
2607
2608 return returnValue;
2609 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002610
2611 @Override
2612 public void setRadioCapability(RadioAccessFamily[] rafs) {
2613 try {
2614 ProxyController.getInstance().setRadioCapability(rafs);
2615 } catch (RuntimeException e) {
2616 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2617 }
2618 }
2619
2620 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002621 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2622 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2623 return RadioAccessFamily.RAF_UNKNOWN;
2624 }
2625
Wink Saville5d475dd2014-10-17 15:00:58 -07002626 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2627 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002628
2629 @Override
2630 public void enableVideoCalling(boolean enable) {
2631 enforceModifyPermission();
2632 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2633 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2634 editor.commit();
2635 }
2636
2637 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002638 public boolean isVideoCallingEnabled(String callingPackage) {
2639 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2640 return false;
2641 }
2642
Andrew Lee77527ac2014-10-21 16:57:39 -07002643 // Check the user preference and the system-level IMS setting. Even if the user has
2644 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2645 // In the long run, we may instead need to check if there exists a connection service
2646 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002647 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2648 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2649 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002650 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002651
Andrew Leea1239f22015-03-02 17:44:07 -08002652 @Override
2653 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002654 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002655 }
2656
2657 @Override
2658 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002659 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002660 }
2661
Andrew Lee9431b832015-03-09 18:46:45 -07002662 @Override
2663 public boolean isTtyModeSupported() {
2664 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2665 TelephonyManager telephonyManager =
2666 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2667 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2668 }
2669
2670 @Override
2671 public boolean isHearingAidCompatibilitySupported() {
2672 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2673 }
2674
Sanket Padawe7310cc72015-01-14 09:53:20 -08002675 /**
2676 * Returns the unique device ID of phone, for example, the IMEI for
2677 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2678 *
2679 * <p>Requires Permission:
2680 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2681 */
2682 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002683 public String getDeviceId(String callingPackage) {
2684 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2685 return null;
2686 }
2687
Sanket Padawe7310cc72015-01-14 09:53:20 -08002688 final Phone phone = PhoneFactory.getPhone(0);
2689 if (phone != null) {
2690 return phone.getDeviceId();
2691 } else {
2692 return null;
2693 }
2694 }
2695
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002696 /*
2697 * {@hide}
2698 * Returns the IMS Registration Status
2699 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002700 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002701 public boolean isImsRegistered() {
2702 return mPhone.isImsRegistered();
2703 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002704
2705 @Override
2706 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2707 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2708 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002709
Nathan Haroldc55097a2015-03-11 18:14:50 -07002710 /*
2711 * {@hide}
2712 * Returns the IMS Registration Status
2713 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002714 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002715 return mPhone.isWifiCallingEnabled();
2716 }
2717
2718 /*
2719 * {@hide}
2720 * Returns the IMS Registration Status
2721 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002722 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002723 return mPhone.isVolteEnabled();
2724 }
Svet Ganovb320e182015-04-16 12:30:10 -07002725
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002726 /*
2727 * {@hide} Returns the IMS Registration Status
2728 */
2729 public boolean isVideoTelephonyAvailable() {
2730 return mPhone.isVideoEnabled();
2731 }
2732
Svet Ganovb320e182015-04-16 12:30:10 -07002733 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002734 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002735 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002736 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2737
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002738 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002739 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002740 } catch (SecurityException e) {
2741 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2742 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002743 }
Svet Ganovb320e182015-04-16 12:30:10 -07002744
2745 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2746 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2747 return false;
2748 }
2749
2750 return true;
2751 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002752
Makoto Onukifee69342015-06-29 14:44:50 -07002753 /**
Jack Yue7939d82015-12-10 16:04:02 -08002754 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002755 */
2756 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002757 // Default SMS app can always read it.
2758 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2759 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2760 return true;
2761 }
2762 try {
2763 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002764 } catch (SecurityException readPhoneStateSecurityException) {
2765 try {
2766 // Can be read with READ_SMS too.
2767 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2768 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2769 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2770 } catch (SecurityException readSmsSecurityException) {
2771 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2772 // permissions
2773 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2774 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2775 " or " + android.Manifest.permission.READ_SMS + ".");
2776 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002777 }
Makoto Onukifee69342015-06-29 14:44:50 -07002778 }
2779
Stuart Scott8eef64f2015-04-08 15:13:54 -07002780 @Override
2781 public void factoryReset(int subId) {
2782 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002783 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2784 return;
2785 }
2786
Svet Ganovcc087f82015-05-12 20:35:54 -07002787 final long identity = Binder.clearCallingIdentity();
2788 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002789 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2790 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002791 // Enable data
2792 setDataEnabled(subId, true);
2793 // Set network selection mode to automatic
2794 setNetworkSelectionModeAutomatic(subId);
2795 // Set preferred mobile network type to the best available
2796 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2797 // Turn off roaming
2798 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2799 }
2800 } finally {
2801 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002802 }
2803 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002804
2805 @Override
2806 public String getLocaleFromDefaultSim() {
2807 // We query all subscriptions instead of just the active ones, because
2808 // this might be called early on in the provisioning flow when the
2809 // subscriptions potentially aren't active yet.
2810 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2811 if (slist == null || slist.isEmpty()) {
2812 return null;
2813 }
2814
2815 // This function may be called very early, say, from the setup wizard, at
2816 // which point we won't have a default subscription set. If that's the case
2817 // we just choose the first, which will be valid in "most cases".
2818 final int defaultSubId = getDefaultSubscription();
2819 SubscriptionInfo info = null;
2820 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2821 info = slist.get(0);
2822 } else {
2823 for (SubscriptionInfo item : slist) {
2824 if (item.getSubscriptionId() == defaultSubId) {
2825 info = item;
2826 break;
2827 }
2828 }
2829
2830 if (info == null) {
2831 return null;
2832 }
2833 }
2834
2835 // Try and fetch the locale from the carrier properties or from the SIM language
2836 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002837 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002838 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002839 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002840 if (defaultPhone != null) {
2841 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2842 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002843 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002844 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2845 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002846 } else {
2847 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002848 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002849 }
2850 }
2851
Narayan Kamath011676f2015-07-29 12:04:08 +01002852 // The SIM language preferences only store a language (e.g. fr = French), not an
2853 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2854 // the SIM and carrier preferences does not include a country we add the country
2855 // determined from the SIM MCC to provide an exact locale.
2856 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002857 if (mccLocale != null) {
2858 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2859 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002860 }
2861
Tony Hill183b2de2015-06-24 14:53:58 +01002862 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002863 return null;
2864 }
2865
2866 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2867 final long identity = Binder.clearCallingIdentity();
2868 try {
2869 return mSubscriptionController.getAllSubInfoList(
2870 mPhone.getContext().getOpPackageName());
2871 } finally {
2872 Binder.restoreCallingIdentity(identity);
2873 }
2874 }
2875
2876 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2877 final long identity = Binder.clearCallingIdentity();
2878 try {
2879 return mSubscriptionController.getActiveSubscriptionInfoList(
2880 mPhone.getContext().getOpPackageName());
2881 } finally {
2882 Binder.restoreCallingIdentity(identity);
2883 }
2884 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002885
2886 /**
2887 * {@hide}
2888 * Returns the modem stats
2889 */
2890 @Override
2891 public ModemActivityInfo getModemActivityInfo() {
2892 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2893 }
Jack Yue7939d82015-12-10 16:04:02 -08002894
2895 /**
2896 * {@hide}
2897 * Returns the service state information on specified subscription.
2898 */
2899 @Override
2900 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2901
2902 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2903 return null;
2904 }
2905
2906 final Phone phone = getPhone(subId);
2907 if (phone == null) {
2908 return null;
2909 }
2910
2911 return phone.getServiceState();
2912 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002913}