blob: 32dbfde0af5501efb523baa1eb3132f649831b5e [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 }
Amit Mahajand4766222016-01-28 15:28:28 -0800742 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
743 if (request.result == null) {
744 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
745 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700746 synchronized (request) {
747 request.notifyAll();
748 }
749 break;
750
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700751 default:
752 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
753 break;
754 }
755 }
Jake Hambye994d462014-02-03 13:10:13 -0800756
757 private void handleNullReturnEvent(Message msg, String command) {
758 AsyncResult ar = (AsyncResult) msg.obj;
759 MainThreadRequest request = (MainThreadRequest) ar.userObj;
760 if (ar.exception == null) {
761 request.result = true;
762 } else {
763 request.result = false;
764 if (ar.exception instanceof CommandException) {
765 loge(command + ": CommandException: " + ar.exception);
766 } else {
767 loge(command + ": Unknown exception");
768 }
769 }
770 synchronized (request) {
771 request.notifyAll();
772 }
773 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700774 }
775
776 /**
777 * Posts the specified command to be executed on the main thread,
778 * waits for the request to complete, and returns the result.
779 * @see #sendRequestAsync
780 */
781 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700782 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700783 }
784
785 /**
786 * Posts the specified command to be executed on the main thread,
787 * waits for the request to complete, and returns the result.
788 * @see #sendRequestAsync
789 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800790 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700791 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
792 throw new RuntimeException("This method will deadlock if called from the main thread.");
793 }
794
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800795 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 Message msg = mMainThreadHandler.obtainMessage(command, request);
797 msg.sendToTarget();
798
799 // Wait for the request to complete
800 synchronized (request) {
801 while (request.result == null) {
802 try {
803 request.wait();
804 } catch (InterruptedException e) {
805 // Do nothing, go back and wait until the request is complete
806 }
807 }
808 }
809 return request.result;
810 }
811
812 /**
813 * Asynchronous ("fire and forget") version of sendRequest():
814 * Posts the specified command to be executed on the main thread, and
815 * returns immediately.
816 * @see #sendRequest
817 */
818 private void sendRequestAsync(int command) {
819 mMainThreadHandler.sendEmptyMessage(command);
820 }
821
822 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700823 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
824 * @see {@link #sendRequest(int,Object)}
825 */
826 private void sendRequestAsync(int command, Object argument) {
827 MainThreadRequest request = new MainThreadRequest(argument);
828 Message msg = mMainThreadHandler.obtainMessage(command, request);
829 msg.sendToTarget();
830 }
831
832 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700833 * Initialize the singleton PhoneInterfaceManager instance.
834 * This is only done once, at startup, from PhoneApp.onCreate().
835 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700836 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 synchronized (PhoneInterfaceManager.class) {
838 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700839 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700840 } else {
841 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
842 }
843 return sInstance;
844 }
845 }
846
847 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700848 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 mApp = app;
850 mPhone = phone;
851 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700852 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
854 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700855 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700856 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800857 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800858
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700859 publish();
860 }
861
862 private void publish() {
863 if (DBG) log("publish: " + this);
864
865 ServiceManager.addService("phone", this);
866 }
867
Stuart Scott584921c2015-01-15 17:10:34 -0800868 private Phone getPhoneFromRequest(MainThreadRequest request) {
869 return (request.subId == null) ? mPhone : getPhone(request.subId);
870 }
871
Wink Saville36469e72014-06-11 15:17:00 -0700872 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700873 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800874 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700875 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700876 //
877 // Implementation of the ITelephony interface.
878 //
879
880 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700881 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700882 }
883
Wink Savilleb564aae2014-10-23 10:18:09 -0700884 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700885 if (DBG) log("dial: " + number);
886 // No permission check needed here: This is just a wrapper around the
887 // ACTION_DIAL intent, which is available to any app since it puts up
888 // the UI before it does anything.
889
890 String url = createTelUrl(number);
891 if (url == null) {
892 return;
893 }
894
895 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700896 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700897 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
898 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
899 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
900 mApp.startActivity(intent);
901 }
902 }
903
904 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700905 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700906 }
907
Wink Savilleb564aae2014-10-23 10:18:09 -0700908 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700909 if (DBG) log("call: " + number);
910
911 // This is just a wrapper around the ACTION_CALL intent, but we still
912 // need to do a permission check since we're calling startActivity()
913 // from the context of the phone app.
914 enforceCallPermission();
915
916 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
917 != AppOpsManager.MODE_ALLOWED) {
918 return;
919 }
920
921 String url = createTelUrl(number);
922 if (url == null) {
923 return;
924 }
925
Wink Saville08874612014-08-31 19:19:58 -0700926 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100927 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800928 if (slist != null) {
929 for (SubscriptionInfo subInfoRecord : slist) {
930 if (subInfoRecord.getSubscriptionId() == subId) {
931 isValid = true;
932 break;
933 }
Wink Saville08874612014-08-31 19:19:58 -0700934 }
935 }
936 if (isValid == false) {
937 return;
938 }
939
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700940 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700941 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700942 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
943 mApp.startActivity(intent);
944 }
945
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 /**
947 * End a call based on call state
948 * @return true is a call was ended
949 */
950 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700951 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700952 }
953
954 /**
955 * End a call based on the call state of the subId
956 * @return true is a call was ended
957 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700958 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700959 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800960 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700961 }
962
963 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700964 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700965 }
966
Wink Savilleb564aae2014-10-23 10:18:09 -0700967 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700968 if (DBG) log("answerRingingCall...");
969 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
970 // but that can probably wait till the big TelephonyManager API overhaul.
971 // For now, protect this call with the MODIFY_PHONE_STATE permission.
972 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800973 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 }
975
976 /**
977 * Make the actual telephony calls to implement answerRingingCall().
978 * This should only be called from the main thread of the Phone app.
979 * @see #answerRingingCall
980 *
981 * TODO: it would be nice to return true if we answered the call, or
982 * false if there wasn't actually a ringing incoming call, or some
983 * other error occurred. (In other words, pass back the return value
984 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
985 * But that would require calling this method via sendRequest() rather
986 * than sendRequestAsync(), and right now we don't actually *need* that
987 * return value, so let's just return void for now.
988 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700989 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700990 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700992 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
993 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700994 if (hasActiveCall && hasHoldingCall) {
995 // Both lines are in use!
996 // TODO: provide a flag to let the caller specify what
997 // policy to use if both lines are in use. (The current
998 // behavior is hardwired to "answer incoming, end ongoing",
999 // which is how the CALL button is specced to behave.)
1000 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1001 return;
1002 } else {
1003 // answerCall() will automatically hold the current active
1004 // call, if there is one.
1005 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1006 return;
1007 }
1008 } else {
1009 // No call was ringing.
1010 return;
1011 }
1012 }
1013
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001015 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001017 public void silenceRinger() {
1018 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001019 }
1020
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001021 @Override
1022 public boolean isOffhook(String callingPackage) {
1023 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001024 }
1025
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001026 @Override
1027 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1028 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1029 return false;
1030 }
1031
Sanket Padawe356d7632015-06-22 14:03:32 -07001032 final Phone phone = getPhone(subId);
1033 if (phone != null) {
1034 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1035 } else {
1036 return false;
1037 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001038 }
1039
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001040 @Override
1041 public boolean isRinging(String callingPackage) {
1042 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001043 }
1044
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001045 @Override
1046 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1047 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1048 return false;
1049 }
1050
Sanket Padawe356d7632015-06-22 14:03:32 -07001051 final Phone phone = getPhone(subId);
1052 if (phone != null) {
1053 return (phone.getState() == PhoneConstants.State.RINGING);
1054 } else {
1055 return false;
1056 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001057 }
1058
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001059 @Override
1060 public boolean isIdle(String callingPackage) {
1061 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001062 }
1063
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001064 @Override
1065 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1066 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1067 return false;
1068 }
1069
Sanket Padawe356d7632015-06-22 14:03:32 -07001070 final Phone phone = getPhone(subId);
1071 if (phone != null) {
1072 return (phone.getState() == PhoneConstants.State.IDLE);
1073 } else {
1074 return false;
1075 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001076 }
1077
Svet Ganovb320e182015-04-16 12:30:10 -07001078 public boolean isSimPinEnabled(String callingPackage) {
1079 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1080 return false;
1081 }
1082
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001083 return (PhoneGlobals.getInstance().isSimPinEnabled());
1084 }
1085
1086 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001087 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001088 }
1089
Wink Savilleb564aae2014-10-23 10:18:09 -07001090 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001091 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001092 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1093 }
1094
1095 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001096 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001097 }
1098
Wink Savilleb564aae2014-10-23 10:18:09 -07001099 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001100 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001101 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1102 }
1103
1104 /** {@hide} */
1105 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001107 }
1108
Wink Savilleb564aae2014-10-23 10:18:09 -07001109 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001111 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001112 checkSimPin.start();
1113 return checkSimPin.unlockSim(null, pin);
1114 }
1115
Wink Saville9de0f752013-10-22 19:04:03 -07001116 /** {@hide} */
1117 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001118 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001119 }
1120
Wink Savilleb564aae2014-10-23 10:18:09 -07001121 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001123 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001124 checkSimPuk.start();
1125 return checkSimPuk.unlockSim(puk, pin);
1126 }
1127
1128 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001129 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001130 * a synchronous one.
1131 */
1132 private static class UnlockSim extends Thread {
1133
1134 private final IccCard mSimCard;
1135
1136 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001137 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1138 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139
1140 // For replies from SimCard interface
1141 private Handler mHandler;
1142
1143 // For async handler to identify request type
1144 private static final int SUPPLY_PIN_COMPLETE = 100;
1145
1146 public UnlockSim(IccCard simCard) {
1147 mSimCard = simCard;
1148 }
1149
1150 @Override
1151 public void run() {
1152 Looper.prepare();
1153 synchronized (UnlockSim.this) {
1154 mHandler = new Handler() {
1155 @Override
1156 public void handleMessage(Message msg) {
1157 AsyncResult ar = (AsyncResult) msg.obj;
1158 switch (msg.what) {
1159 case SUPPLY_PIN_COMPLETE:
1160 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1161 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001162 mRetryCount = msg.arg1;
1163 if (ar.exception != null) {
1164 if (ar.exception instanceof CommandException &&
1165 ((CommandException)(ar.exception)).getCommandError()
1166 == CommandException.Error.PASSWORD_INCORRECT) {
1167 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1168 } else {
1169 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1170 }
1171 } else {
1172 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1173 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001174 mDone = true;
1175 UnlockSim.this.notifyAll();
1176 }
1177 break;
1178 }
1179 }
1180 };
1181 UnlockSim.this.notifyAll();
1182 }
1183 Looper.loop();
1184 }
1185
1186 /*
1187 * Use PIN or PUK to unlock SIM card
1188 *
1189 * If PUK is null, unlock SIM card with PIN
1190 *
1191 * If PUK is not null, unlock SIM card with PUK and set PIN code
1192 */
Wink Saville9de0f752013-10-22 19:04:03 -07001193 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001194
1195 while (mHandler == null) {
1196 try {
1197 wait();
1198 } catch (InterruptedException e) {
1199 Thread.currentThread().interrupt();
1200 }
1201 }
1202 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1203
1204 if (puk == null) {
1205 mSimCard.supplyPin(pin, callback);
1206 } else {
1207 mSimCard.supplyPuk(puk, pin, callback);
1208 }
1209
1210 while (!mDone) {
1211 try {
1212 Log.d(LOG_TAG, "wait for done");
1213 wait();
1214 } catch (InterruptedException e) {
1215 // Restore the interrupted status
1216 Thread.currentThread().interrupt();
1217 }
1218 }
1219 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001220 int[] resultArray = new int[2];
1221 resultArray[0] = mResult;
1222 resultArray[1] = mRetryCount;
1223 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001224 }
1225 }
1226
1227 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001228 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001229
1230 }
1231
Wink Savilleb564aae2014-10-23 10:18:09 -07001232 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001233 // No permission check needed here: this call is harmless, and it's
1234 // needed for the ServiceState.requestStateUpdate() call (which is
1235 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001236 final Phone phone = getPhone(subId);
1237 if (phone != null) {
1238 phone.updateServiceLocation();
1239 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001240 }
1241
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001242 @Override
1243 public boolean isRadioOn(String callingPackage) {
1244 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001245 }
1246
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001247 @Override
1248 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1249 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1250 return false;
1251 }
1252 return isRadioOnForSubscriber(subId);
1253 }
1254
1255 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001256 final Phone phone = getPhone(subId);
1257 if (phone != null) {
1258 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1259 } else {
1260 return false;
1261 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001262 }
1263
1264 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001265 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001266
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001267 }
Wink Saville36469e72014-06-11 15:17:00 -07001268
Wink Savilleb564aae2014-10-23 10:18:09 -07001269 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001270 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001271 final Phone phone = getPhone(subId);
1272 if (phone != null) {
1273 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1274 }
Wink Saville36469e72014-06-11 15:17:00 -07001275 }
1276
1277 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001278 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001279 }
1280
Wink Savilleb564aae2014-10-23 10:18:09 -07001281 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001282 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001283 final Phone phone = getPhone(subId);
1284 if (phone == null) {
1285 return false;
1286 }
1287 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001288 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001289 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001290 }
1291 return true;
1292 }
Wink Saville36469e72014-06-11 15:17:00 -07001293
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001294 public boolean needMobileRadioShutdown() {
1295 /*
1296 * If any of the Radios are available, it will need to be
1297 * shutdown. So return true if any Radio is available.
1298 */
1299 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1300 Phone phone = PhoneFactory.getPhone(i);
1301 if (phone != null && phone.isRadioAvailable()) return true;
1302 }
1303 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1304 return false;
1305 }
1306
1307 public void shutdownMobileRadios() {
1308 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1309 logv("Shutting down Phone " + i);
1310 shutdownRadioUsingPhoneId(i);
1311 }
1312 }
1313
1314 private void shutdownRadioUsingPhoneId(int phoneId) {
1315 enforceModifyPermission();
1316 Phone phone = PhoneFactory.getPhone(phoneId);
1317 if (phone != null && phone.isRadioAvailable()) {
1318 phone.shutdownRadio();
1319 }
1320 }
1321
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001322 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001323 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001324 }
1325
Wink Savilleb564aae2014-10-23 10:18:09 -07001326 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001327 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001328 final Phone phone = getPhone(subId);
1329 if (phone != null) {
1330 phone.setRadioPower(turnOn);
1331 return true;
1332 } else {
1333 return false;
1334 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001335 }
1336
Wink Saville36469e72014-06-11 15:17:00 -07001337 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001338 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001339 public boolean enableDataConnectivity() {
1340 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001341 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001342 final Phone phone = getPhone(subId);
1343 if (phone != null) {
1344 phone.setDataEnabled(true);
1345 return true;
1346 } else {
1347 return false;
1348 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001349 }
1350
Wink Saville36469e72014-06-11 15:17:00 -07001351 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001352 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001353 public boolean disableDataConnectivity() {
1354 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001355 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001356 final Phone phone = getPhone(subId);
1357 if (phone != null) {
1358 phone.setDataEnabled(false);
1359 return true;
1360 } else {
1361 return false;
1362 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001363 }
1364
Wink Saville36469e72014-06-11 15:17:00 -07001365 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001367 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001368 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001369 final Phone phone = getPhone(subId);
1370 if (phone != null) {
1371 return phone.isDataConnectivityPossible();
1372 } else {
1373 return false;
1374 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001375 }
1376
1377 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001378 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001379 }
1380
Wink Savilleb564aae2014-10-23 10:18:09 -07001381 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001383 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1384 return false;
1385 }
Wink Saville36469e72014-06-11 15:17:00 -07001386 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 }
1388
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001389 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001390 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001391 }
1392
Wink Savilleb564aae2014-10-23 10:18:09 -07001393 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001394 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 }
1396
Sanket Padawe356d7632015-06-22 14:03:32 -07001397 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001398 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001399 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001400 if (phone != null) {
1401 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1402 } else {
1403 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1404 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 }
1406
Sanket Padawe356d7632015-06-22 14:03:32 -07001407 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001408 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001409 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001410 if (phone != null) {
1411 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1412 } else {
1413 return TelephonyManager.DATA_ACTIVITY_NONE;
1414 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 }
1416
1417 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001418 public Bundle getCellLocation(String callingPackage) {
1419 enforceFineOrCoarseLocationPermission("getCellLocation");
1420
1421 // OP_COARSE_LOCATION controls both fine and coarse location.
1422 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1423 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001424 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001425 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 }
1427
Jake Hambye994d462014-02-03 13:10:13 -08001428 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001429 if (DBG_LOC) log("getCellLocation: is active user");
1430 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001431 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001432 if (phone == null) {
1433 return null;
1434 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001435 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 return data;
1437 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001438 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001439 return null;
1440 }
1441 }
1442
Svetoslav64fad262015-04-14 14:35:21 -07001443 private void enforceFineOrCoarseLocationPermission(String message) {
1444 try {
1445 mApp.enforceCallingOrSelfPermission(
1446 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1447 } catch (SecurityException e) {
1448 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1449 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1450 // is the weaker precondition
1451 mApp.enforceCallingOrSelfPermission(
1452 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1453 }
1454 }
1455
1456
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 @Override
1458 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001459 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001460 }
1461
Sanket Padawe356d7632015-06-22 14:03:32 -07001462 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001463 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001464 mApp.enforceCallingOrSelfPermission(
1465 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001466 final Phone phone = getPhone(subId);
1467 if (phone != null) {
1468 phone.enableLocationUpdates();
1469 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001470 }
1471
1472 @Override
1473 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001474 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001475 }
1476
Sanket Padawe356d7632015-06-22 14:03:32 -07001477 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001478 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001479 mApp.enforceCallingOrSelfPermission(
1480 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001481 final Phone phone = getPhone(subId);
1482 if (phone != null) {
1483 phone.disableLocationUpdates();
1484 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001485 }
1486
1487 @Override
1488 @SuppressWarnings("unchecked")
1489 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001490 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1491
1492 // OP_COARSE_LOCATION controls both fine and coarse location.
1493 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1494 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1495 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001496 }
1497
1498 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1499 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1500 return null;
1501 }
Svetoslav64fad262015-04-14 14:35:21 -07001502
Jake Hambye994d462014-02-03 13:10:13 -08001503 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001504 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1505
1506 ArrayList<NeighboringCellInfo> cells = null;
1507
1508 try {
1509 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001510 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001511 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001512 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001513 }
1514 return cells;
1515 } else {
1516 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1517 return null;
1518 }
1519 }
1520
1521
1522 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001523 public List<CellInfo> getAllCellInfo(String callingPackage) {
1524 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1525
1526 // OP_COARSE_LOCATION controls both fine and coarse location.
1527 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1528 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1529 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 }
1531
Jake Hambye994d462014-02-03 13:10:13 -08001532 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001534 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1535 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001536 final List<CellInfo> info = phone.getAllCellInfo();
1537 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001538 }
1539 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001540 } else {
1541 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1542 return null;
1543 }
1544 }
1545
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001546 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001547 public void setCellInfoListRate(int rateInMillis) {
1548 mPhone.setCellInfoListRate(rateInMillis);
1549 }
1550
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001551 //
1552 // Internal helper methods.
1553 //
1554
Jake Hambye994d462014-02-03 13:10:13 -08001555 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001556 boolean ok;
1557
1558 boolean self = Binder.getCallingUid() == Process.myUid();
1559 if (!self) {
1560 // Get the caller's user id then clear the calling identity
1561 // which will be restored in the finally clause.
1562 int callingUser = UserHandle.getCallingUserId();
1563 long ident = Binder.clearCallingIdentity();
1564
1565 try {
1566 // With calling identity cleared the current user is the foreground user.
1567 int foregroundUser = ActivityManager.getCurrentUser();
1568 ok = (foregroundUser == callingUser);
1569 if (DBG_LOC) {
1570 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1571 + " callingUser=" + callingUser + " ok=" + ok);
1572 }
1573 } catch (Exception ex) {
1574 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1575 ok = false;
1576 } finally {
1577 Binder.restoreCallingIdentity(ident);
1578 }
1579 } else {
1580 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1581 ok = true;
1582 }
1583 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1584 return ok;
1585 }
1586
1587 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001588 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1589 *
1590 * @throws SecurityException if the caller does not have the required permission
1591 */
1592 private void enforceModifyPermission() {
1593 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1594 }
1595
1596 /**
Junda Liua2e36012014-07-09 18:30:01 -07001597 * Make sure either system app or the caller has carrier privilege.
1598 *
1599 * @throws SecurityException if the caller does not have the required permission/privilege
1600 */
1601 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001602 int permission = mApp.checkCallingOrSelfPermission(
1603 android.Manifest.permission.MODIFY_PHONE_STATE);
1604 if (permission == PackageManager.PERMISSION_GRANTED) {
1605 return;
1606 }
1607
1608 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001609 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001610 loge("No Carrier Privilege.");
1611 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001612 }
1613 }
1614
1615 /**
1616 * Make sure the caller has carrier privilege.
1617 *
1618 * @throws SecurityException if the caller does not have the required permission
1619 */
1620 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001621 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001622 loge("No Carrier Privilege.");
1623 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001624 }
1625 }
1626
1627 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001628 * Make sure the caller has the CALL_PHONE permission.
1629 *
1630 * @throws SecurityException if the caller does not have the required permission
1631 */
1632 private void enforceCallPermission() {
1633 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1634 }
1635
Stuart Scott8eef64f2015-04-08 15:13:54 -07001636 private void enforceConnectivityInternalPermission() {
1637 mApp.enforceCallingOrSelfPermission(
1638 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1639 "ConnectivityService");
1640 }
1641
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001642 private String createTelUrl(String number) {
1643 if (TextUtils.isEmpty(number)) {
1644 return null;
1645 }
1646
Jake Hambye994d462014-02-03 13:10:13 -08001647 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 }
1649
Ihab Awadf9e92732013-12-05 18:02:52 -08001650 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001651 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1652 }
1653
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001654 private static void logv(String msg) {
1655 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1656 }
1657
Ihab Awadf9e92732013-12-05 18:02:52 -08001658 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001659 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1660 }
1661
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001662 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001663 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001664 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001665 }
1666
Sanket Padawe356d7632015-06-22 14:03:32 -07001667 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001668 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001669 final Phone phone = getPhone(subId);
1670 if (phone == null) {
1671 return PhoneConstants.PHONE_TYPE_NONE;
1672 } else {
1673 return getPhone(subId).getPhoneType();
1674 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001675 }
1676
1677 /**
1678 * Returns the CDMA ERI icon index to display
1679 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001680 @Override
1681 public int getCdmaEriIconIndex(String callingPackage) {
1682 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001683 }
1684
Sanket Padawe356d7632015-06-22 14:03:32 -07001685 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001686 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1687 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1688 return -1;
1689 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001690 final Phone phone = getPhone(subId);
1691 if (phone != null) {
1692 return phone.getCdmaEriIconIndex();
1693 } else {
1694 return -1;
1695 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001696 }
1697
1698 /**
1699 * Returns the CDMA ERI icon mode,
1700 * 0 - ON
1701 * 1 - FLASHING
1702 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001703 @Override
1704 public int getCdmaEriIconMode(String callingPackage) {
1705 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001706 }
1707
Sanket Padawe356d7632015-06-22 14:03:32 -07001708 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001709 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1710 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1711 return -1;
1712 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001713 final Phone phone = getPhone(subId);
1714 if (phone != null) {
1715 return phone.getCdmaEriIconMode();
1716 } else {
1717 return -1;
1718 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001719 }
1720
1721 /**
1722 * Returns the CDMA ERI text,
1723 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001724 @Override
1725 public String getCdmaEriText(String callingPackage) {
1726 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001727 }
1728
Sanket Padawe356d7632015-06-22 14:03:32 -07001729 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001730 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1731 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1732 return null;
1733 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001734 final Phone phone = getPhone(subId);
1735 if (phone != null) {
1736 return phone.getCdmaEriText();
1737 } else {
1738 return null;
1739 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001740 }
1741
1742 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001743 * Returns the CDMA MDN.
1744 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001745 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001746 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001747 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001748 final Phone phone = getPhone(subId);
1749 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1750 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001751 } else {
1752 return null;
1753 }
1754 }
1755
1756 /**
1757 * Returns the CDMA MIN.
1758 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001759 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001760 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001761 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001762 final Phone phone = getPhone(subId);
1763 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1764 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001765 } else {
1766 return null;
1767 }
1768 }
1769
1770 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001771 * Returns true if CDMA provisioning needs to run.
1772 */
1773 public boolean needsOtaServiceProvisioning() {
1774 return mPhone.needsOtaServiceProvisioning();
1775 }
1776
1777 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001778 * Sets the voice mail number of a given subId.
1779 */
1780 @Override
1781 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001782 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001783 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1784 new Pair<String, String>(alphaTag, number), new Integer(subId));
1785 return success;
1786 }
1787
1788 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001789 * Returns the unread count of voicemails
1790 */
1791 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001792 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001793 }
1794
1795 /**
1796 * Returns the unread count of voicemails for a subId
1797 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001798 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001799 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001800 final Phone phone = getPhone(subId);
1801 if (phone != null) {
1802 return phone.getVoiceMessageCount();
1803 } else {
1804 return 0;
1805 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001806 }
1807
1808 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001809 * Returns the data network type.
1810 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001811 *
1812 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1813 */
1814 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001815 public int getNetworkType() {
1816 final Phone phone = getPhone(getDefaultSubscription());
1817 if (phone != null) {
1818 return phone.getServiceState().getDataNetworkType();
1819 } else {
1820 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1821 }
Wink Saville36469e72014-06-11 15:17:00 -07001822 }
1823
1824 /**
1825 * Returns the network type for a subId
1826 */
1827 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001828 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1829 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1830 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1831 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001832
Sanket Padawe356d7632015-06-22 14:03:32 -07001833 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001834 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001835 return phone.getServiceState().getDataNetworkType();
1836 } else {
1837 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1838 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001839 }
1840
1841 /**
1842 * Returns the data network type
1843 */
1844 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001845 public int getDataNetworkType(String callingPackage) {
1846 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001847 }
1848
1849 /**
1850 * Returns the data network type for a subId
1851 */
1852 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001853 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1854 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1855 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1856 }
1857
Sanket Padawe356d7632015-06-22 14:03:32 -07001858 final Phone phone = getPhone(subId);
1859 if (phone != null) {
1860 return phone.getServiceState().getDataNetworkType();
1861 } else {
1862 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1863 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001864 }
1865
1866 /**
Wink Saville36469e72014-06-11 15:17:00 -07001867 * Returns the Voice network type for a subId
1868 */
1869 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001870 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1871 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1872 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1873 }
1874
Sanket Padawe356d7632015-06-22 14:03:32 -07001875 final Phone phone = getPhone(subId);
1876 if (phone != null) {
1877 return phone.getServiceState().getVoiceNetworkType();
1878 } else {
1879 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1880 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001881 }
1882
1883 /**
1884 * @return true if a ICC card is present
1885 */
1886 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001887 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001888 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001889 }
1890
1891 /**
1892 * @return true if a ICC card is present for a slotId
1893 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001894 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001895 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001896 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001897 final Phone phone = getPhone(subId[0]);
1898 if (subId != null && phone != null) {
1899 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001900 } else {
1901 return false;
1902 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001903 }
1904
1905 /**
1906 * Return if the current radio is LTE on CDMA. This
1907 * is a tri-state return value as for a period of time
1908 * the mode may be unknown.
1909 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001910 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001911 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001912 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001913 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001914 @Override
1915 public int getLteOnCdmaMode(String callingPackage) {
1916 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001917 }
1918
Sanket Padawe356d7632015-06-22 14:03:32 -07001919 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001920 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1921 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1922 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1923 }
1924
Sanket Padawe356d7632015-06-22 14:03:32 -07001925 final Phone phone = getPhone(subId);
1926 if (phone == null) {
1927 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1928 } else {
1929 return phone.getLteOnCdmaMode();
1930 }
Wink Saville36469e72014-06-11 15:17:00 -07001931 }
1932
1933 public void setPhone(Phone phone) {
1934 mPhone = phone;
1935 }
1936
1937 /**
1938 * {@hide}
1939 * Returns Default subId, 0 in the case of single standby.
1940 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001941 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001942 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001943 }
1944
Wink Savilleb564aae2014-10-23 10:18:09 -07001945 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001946 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001947 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001948
1949 /**
1950 * @see android.telephony.TelephonyManager.WifiCallingChoices
1951 */
1952 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001953 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1954 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001955 }
1956
1957 /**
1958 * @see android.telephony.TelephonyManager.WifiCallingChoices
1959 */
1960 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001961 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1962 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1963 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001964 }
1965
Sailesh Nepald1e68152013-12-12 19:08:02 -08001966 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001967 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001968 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001969 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001970
Shishir Agrawal566b7612013-10-28 14:41:00 -07001971 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001972 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001973 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001974
1975 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001976 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1977 CMD_OPEN_CHANNEL, AID);
1978 if (DBG) log("iccOpenLogicalChannel: " + response);
1979 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001980 }
1981
1982 @Override
1983 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001984 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001985
1986 if (DBG) log("iccCloseLogicalChannel: " + channel);
1987 if (channel < 0) {
1988 return false;
1989 }
Jake Hambye994d462014-02-03 13:10:13 -08001990 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001991 if (DBG) log("iccCloseLogicalChannel: " + success);
1992 return success;
1993 }
1994
1995 @Override
1996 public String iccTransmitApduLogicalChannel(int channel, int cla,
1997 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001998 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001999
2000 if (DBG) {
2001 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
2002 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2003 " data=" + data);
2004 }
2005
2006 if (channel < 0) {
2007 return "";
2008 }
2009
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002010 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002011 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2012 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2013
Shishir Agrawal566b7612013-10-28 14:41:00 -07002014 // Append the returned status code to the end of the response payload.
2015 String s = Integer.toHexString(
2016 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002017 if (response.payload != null) {
2018 s = IccUtils.bytesToHexString(response.payload) + s;
2019 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002020 return s;
2021 }
Jake Hambye994d462014-02-03 13:10:13 -08002022
Evan Charltonc66da362014-05-16 14:06:40 -07002023 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002024 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2025 int p3, String data) {
2026 enforceModifyPermissionOrCarrierPrivilege();
2027
2028 if (DBG) {
2029 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2030 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2031 }
2032
2033 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2034 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2035 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2036
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002037 // Append the returned status code to the end of the response payload.
2038 String s = Integer.toHexString(
2039 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002040 if (response.payload != null) {
2041 s = IccUtils.bytesToHexString(response.payload) + s;
2042 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002043 return s;
2044 }
2045
2046 @Override
2047 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2048 String filePath) {
2049 enforceModifyPermissionOrCarrierPrivilege();
2050
2051 if (DBG) {
2052 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2053 p1 + " " + p2 + " " + p3 + ":" + filePath);
2054 }
2055
2056 IccIoResult response =
2057 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002058 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002059
2060 if (DBG) {
2061 log("Exchange SIM_IO [R]" + response);
2062 }
2063
2064 byte[] result = null;
2065 int length = 2;
2066 if (response.payload != null) {
2067 length = 2 + response.payload.length;
2068 result = new byte[length];
2069 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2070 } else {
2071 result = new byte[length];
2072 }
2073
2074 result[length - 1] = (byte) response.sw2;
2075 result[length - 2] = (byte) response.sw1;
2076 return result;
2077 }
2078
2079 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002080 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002081 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002082
2083 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2084 if (response.payload == null) {
2085 return "";
2086 }
2087
2088 // Append the returned status code to the end of the response payload.
2089 String s = Integer.toHexString(
2090 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2091 s = IccUtils.bytesToHexString(response.payload) + s;
2092 return s;
2093 }
2094
Jake Hambye994d462014-02-03 13:10:13 -08002095 /**
2096 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2097 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2098 *
2099 * @param itemID the ID of the item to read
2100 * @return the NV item as a String, or null on error.
2101 */
2102 @Override
2103 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002104 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002105 if (DBG) log("nvReadItem: item " + itemID);
2106 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2107 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2108 return value;
2109 }
2110
2111 /**
2112 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2113 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2114 *
2115 * @param itemID the ID of the item to read
2116 * @param itemValue the value to write, as a String
2117 * @return true on success; false on any failure
2118 */
2119 @Override
2120 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002121 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002122 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2123 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2124 new Pair<Integer, String>(itemID, itemValue));
2125 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2126 return success;
2127 }
2128
2129 /**
2130 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2131 * Used for device configuration by some CDMA operators.
2132 *
2133 * @param preferredRoamingList byte array containing the new PRL
2134 * @return true on success; false on any failure
2135 */
2136 @Override
2137 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002138 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002139 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2140 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2141 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2142 return success;
2143 }
2144
2145 /**
2146 * Perform the specified type of NV config reset.
2147 * Used for device configuration by some CDMA operators.
2148 *
2149 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2150 * @return true on success; false on any failure
2151 */
2152 @Override
2153 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002154 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002155 if (DBG) log("nvResetConfig: type " + resetType);
2156 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2157 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2158 return success;
2159 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002160
2161 /**
Wink Saville36469e72014-06-11 15:17:00 -07002162 * {@hide}
2163 * Returns Default sim, 0 in the case of single standby.
2164 */
2165 public int getDefaultSim() {
2166 //TODO Need to get it from Telephony Devcontroller
2167 return 0;
2168 }
2169
Svet Ganovb320e182015-04-16 12:30:10 -07002170 public String[] getPcscfAddress(String apnType, String callingPackage) {
2171 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2172 return new String[0];
2173 }
2174
2175
ram87fca6f2014-07-18 18:58:44 +05302176 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002177 }
2178
2179 public void setImsRegistrationState(boolean registered) {
2180 enforceModifyPermission();
2181 mPhone.setImsRegistrationState(registered);
2182 }
2183
2184 /**
Stuart Scott54788802015-03-30 13:18:01 -07002185 * Set the network selection mode to automatic.
2186 *
2187 */
2188 @Override
2189 public void setNetworkSelectionModeAutomatic(int subId) {
2190 enforceModifyPermissionOrCarrierPrivilege();
2191 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2192 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2193 }
2194
2195 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002196 * Set the network selection mode to manual with the selected carrier.
2197 */
2198 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002199 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2200 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002201 enforceModifyPermissionOrCarrierPrivilege();
2202 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002203 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2204 persistSelection);
2205 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002206 }
2207
2208 /**
2209 * Scans for available networks.
2210 */
2211 @Override
2212 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2213 enforceModifyPermissionOrCarrierPrivilege();
2214 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2215 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2216 CMD_PERFORM_NETWORK_SCAN, null, subId);
2217 return result;
2218 }
2219
2220 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002221 * Get the calculated preferred network type.
2222 * Used for debugging incorrect network type.
2223 *
2224 * @return the preferred network type, defined in RILConstants.java.
2225 */
2226 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002227 public int getCalculatedPreferredNetworkType(String callingPackage) {
2228 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2229 return RILConstants.PREFERRED_NETWORK_MODE;
2230 }
2231
Amit Mahajan43330e02014-11-18 11:54:45 -08002232 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002233 }
2234
2235 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002236 * Get the preferred network type.
2237 * Used for device configuration by some CDMA operators.
2238 *
2239 * @return the preferred network type, defined in RILConstants.java.
2240 */
2241 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002242 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002243 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002244 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002245 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002246 int networkType = (result != null ? result[0] : -1);
2247 if (DBG) log("getPreferredNetworkType: " + networkType);
2248 return networkType;
2249 }
2250
2251 /**
2252 * Set the preferred network type.
2253 * Used for device configuration by some CDMA operators.
2254 *
2255 * @param networkType the preferred network type, defined in RILConstants.java.
2256 * @return true on success; false on any failure.
2257 */
2258 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002259 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002260 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002261 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2262 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002263 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002264 if (success) {
2265 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002266 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002267 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002268 return success;
2269 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002270
2271 /**
Junda Liu475951f2014-11-07 16:45:03 -08002272 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2273 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2274 * tethering.
2275 *
2276 * @return 0: Not required. 1: required. 2: Not set.
2277 * @hide
2278 */
2279 @Override
2280 public int getTetherApnRequired() {
2281 enforceModifyPermissionOrCarrierPrivilege();
2282 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2283 Settings.Global.TETHER_DUN_REQUIRED, 2);
2284 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2285 // config_tether_apndata.
2286 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2287 dunRequired = 1;
2288 }
2289 return dunRequired;
2290 }
2291
2292 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002293 * Set mobile data enabled
2294 * Used by the user through settings etc to turn on/off mobile data
2295 *
2296 * @param enable {@code true} turn turn data on, else {@code false}
2297 */
2298 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002299 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002300 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002301 int phoneId = mSubscriptionController.getPhoneId(subId);
2302 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2303 Phone phone = PhoneFactory.getPhone(phoneId);
2304 if (phone != null) {
2305 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2306 phone.setDataEnabled(enable);
2307 } else {
2308 loge("setDataEnabled: no phone for subId=" + subId);
2309 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002310 }
2311
2312 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002313 * Get whether mobile data is enabled.
2314 *
2315 * Note that this used to be available from ConnectivityService, gated by
2316 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2317 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002318 *
2319 * @return {@code true} if data is enabled else {@code false}
2320 */
2321 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002322 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002323 try {
2324 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2325 null);
2326 } catch (Exception e) {
2327 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2328 null);
2329 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002330 int phoneId = mSubscriptionController.getPhoneId(subId);
2331 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2332 Phone phone = PhoneFactory.getPhone(phoneId);
2333 if (phone != null) {
2334 boolean retVal = phone.getDataEnabled();
2335 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2336 return retVal;
2337 } else {
2338 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2339 return false;
2340 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002341 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002342
2343 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002344 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002345 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002346 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002347 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002348 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2349 }
2350 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002351 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002352 }
Junda Liu29340342014-07-10 15:23:27 -07002353
2354 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002355 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002356 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002357 if (card == null) {
2358 loge("checkCarrierPrivilegesForPackage: No UICC");
2359 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2360 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002361 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2362 }
2363
2364 @Override
2365 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2366 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2367 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2368 UiccCard card = UiccController.getInstance().getUiccCard(i);
2369 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002370 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002371 continue;
2372 }
2373
2374 result = card.getCarrierPrivilegeStatus(
2375 mPhone.getContext().getPackageManager(), pkgName);
2376 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2377 break;
2378 }
2379 }
2380
2381 return result;
Junda Liu29340342014-07-10 15:23:27 -07002382 }
Derek Tan89e89d42014-07-08 17:00:10 -07002383
2384 @Override
Junda Liue64de782015-04-16 17:19:16 -07002385 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2386 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2387 loge("phoneId " + phoneId + " is not valid.");
2388 return null;
2389 }
2390 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002391 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002392 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002393 return null ;
2394 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002395 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002396 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002397 }
2398
Wink Savilleb564aae2014-10-23 10:18:09 -07002399 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002400 final Phone phone = getPhone(subId);
2401 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002402 if (card == null) {
2403 loge("getIccId: No UICC");
2404 return null;
2405 }
2406 String iccId = card.getIccId();
2407 if (TextUtils.isEmpty(iccId)) {
2408 loge("getIccId: ICC ID is null or empty.");
2409 return null;
2410 }
2411 return iccId;
2412 }
2413
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002414 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002415 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2416 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002417 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002418
Jeff Sharkey85190e62014-12-05 09:40:12 -08002419 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002420 final Phone phone = getPhone(subId);
2421 if (phone == null) {
2422 return false;
2423 }
2424 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002425
2426 if (DBG_MERGE) {
2427 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2428 + subscriberId + " to " + number);
2429 }
2430
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002431 if (TextUtils.isEmpty(iccId)) {
2432 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002433 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002434
Jeff Sharkey85190e62014-12-05 09:40:12 -08002435 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2436
2437 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002438 if (alphaTag == null) {
2439 editor.remove(alphaTagPrefKey);
2440 } else {
2441 editor.putString(alphaTagPrefKey, alphaTag);
2442 }
2443
Jeff Sharkey85190e62014-12-05 09:40:12 -08002444 // Record both the line number and IMSI for this ICCID, since we need to
2445 // track all merged IMSIs based on line number
2446 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2447 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002448 if (number == null) {
2449 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002450 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002451 } else {
2452 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002453 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002454 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002455
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002456 editor.commit();
2457 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002458 }
2459
2460 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002461 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002462 // This is open to apps with WRITE_SMS.
2463 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Jack Yue7939d82015-12-10 16:04:02 -08002464 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002465 return null;
2466 }
Derek Tan97ebb422014-09-05 16:55:38 -07002467
2468 String iccId = getIccId(subId);
2469 if (iccId != null) {
2470 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Jack Yue7939d82015-12-10 16:04:02 -08002471 if (DBG_MERGE) {
2472 log("getLine1NumberForDisplay returning " +
2473 mTelephonySharedPreferences.getString(numberPrefKey, null));
2474 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002475 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002476 }
Jack Yue7939d82015-12-10 16:04:02 -08002477 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002478 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002479 }
2480
2481 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002482 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2483 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2484 return null;
2485 }
Derek Tan97ebb422014-09-05 16:55:38 -07002486
2487 String iccId = getIccId(subId);
2488 if (iccId != null) {
2489 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002490 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002491 }
Derek Tan97ebb422014-09-05 16:55:38 -07002492 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002493 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002494
2495 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002496 public String[] getMergedSubscriberIds(String callingPackage) {
2497 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2498 return null;
2499 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002500 final Context context = mPhone.getContext();
2501 final TelephonyManager tele = TelephonyManager.from(context);
2502 final SubscriptionManager sub = SubscriptionManager.from(context);
2503
2504 // Figure out what subscribers are currently active
2505 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002506 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2507 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2508 final long identity = Binder.clearCallingIdentity();
2509 try {
2510 final int[] subIds = sub.getActiveSubscriptionIdList();
2511 for (int subId : subIds) {
2512 activeSubscriberIds.add(tele.getSubscriberId(subId));
2513 }
2514 } finally {
2515 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002516 }
2517
2518 // First pass, find a number override for an active subscriber
2519 String mergeNumber = null;
2520 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2521 for (String key : prefs.keySet()) {
2522 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2523 final String subscriberId = (String) prefs.get(key);
2524 if (activeSubscriberIds.contains(subscriberId)) {
2525 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2526 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2527 mergeNumber = (String) prefs.get(numberKey);
2528 if (DBG_MERGE) {
2529 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2530 + " for active subscriber " + subscriberId);
2531 }
2532 if (!TextUtils.isEmpty(mergeNumber)) {
2533 break;
2534 }
2535 }
2536 }
2537 }
2538
2539 // Shortcut when no active merged subscribers
2540 if (TextUtils.isEmpty(mergeNumber)) {
2541 return null;
2542 }
2543
2544 // Second pass, find all subscribers under that line override
2545 final ArraySet<String> result = new ArraySet<>();
2546 for (String key : prefs.keySet()) {
2547 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2548 final String number = (String) prefs.get(key);
2549 if (mergeNumber.equals(number)) {
2550 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2551 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2552 final String subscriberId = (String) prefs.get(subscriberKey);
2553 if (!TextUtils.isEmpty(subscriberId)) {
2554 result.add(subscriberId);
2555 }
2556 }
2557 }
2558 }
2559
2560 final String[] resultArray = result.toArray(new String[result.size()]);
2561 Arrays.sort(resultArray);
2562 if (DBG_MERGE) {
2563 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2564 }
2565 return resultArray;
2566 }
2567
2568 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002569 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002570 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002571 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002572 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002573
2574 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002575 public boolean setRoamingOverride(List<String> gsmRoamingList,
2576 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2577 List<String> cdmaNonRoamingList) {
2578 enforceCarrierPrivilege();
2579 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2580 cdmaNonRoamingList);
2581 }
2582
2583 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002584 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2585 enforceModifyPermission();
2586
2587 int returnValue = 0;
2588 try {
2589 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2590 if(result.exception == null) {
2591 if (result.result != null) {
2592 byte[] responseData = (byte[])(result.result);
2593 if(responseData.length > oemResp.length) {
2594 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2595 responseData.length + "bytes. Buffer Size is " +
2596 oemResp.length + "bytes.");
2597 }
2598 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2599 returnValue = responseData.length;
2600 }
2601 } else {
2602 CommandException ex = (CommandException) result.exception;
2603 returnValue = ex.getCommandError().ordinal();
2604 if(returnValue > 0) returnValue *= -1;
2605 }
2606 } catch (RuntimeException e) {
2607 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2608 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2609 if(returnValue > 0) returnValue *= -1;
2610 }
2611
2612 return returnValue;
2613 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002614
2615 @Override
2616 public void setRadioCapability(RadioAccessFamily[] rafs) {
2617 try {
2618 ProxyController.getInstance().setRadioCapability(rafs);
2619 } catch (RuntimeException e) {
2620 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2621 }
2622 }
2623
2624 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002625 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2626 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2627 return RadioAccessFamily.RAF_UNKNOWN;
2628 }
2629
Wink Saville5d475dd2014-10-17 15:00:58 -07002630 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2631 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002632
2633 @Override
2634 public void enableVideoCalling(boolean enable) {
2635 enforceModifyPermission();
2636 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2637 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2638 editor.commit();
2639 }
2640
2641 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002642 public boolean isVideoCallingEnabled(String callingPackage) {
2643 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2644 return false;
2645 }
2646
Andrew Lee77527ac2014-10-21 16:57:39 -07002647 // Check the user preference and the system-level IMS setting. Even if the user has
2648 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2649 // In the long run, we may instead need to check if there exists a connection service
2650 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002651 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2652 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2653 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002654 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002655
Andrew Leea1239f22015-03-02 17:44:07 -08002656 @Override
2657 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002658 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002659 }
2660
2661 @Override
2662 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002663 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002664 }
2665
Andrew Lee9431b832015-03-09 18:46:45 -07002666 @Override
2667 public boolean isTtyModeSupported() {
2668 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2669 TelephonyManager telephonyManager =
2670 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2671 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2672 }
2673
2674 @Override
2675 public boolean isHearingAidCompatibilitySupported() {
2676 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2677 }
2678
Sanket Padawe7310cc72015-01-14 09:53:20 -08002679 /**
2680 * Returns the unique device ID of phone, for example, the IMEI for
2681 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2682 *
2683 * <p>Requires Permission:
2684 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2685 */
2686 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002687 public String getDeviceId(String callingPackage) {
2688 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2689 return null;
2690 }
2691
Sanket Padawe7310cc72015-01-14 09:53:20 -08002692 final Phone phone = PhoneFactory.getPhone(0);
2693 if (phone != null) {
2694 return phone.getDeviceId();
2695 } else {
2696 return null;
2697 }
2698 }
2699
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002700 /*
2701 * {@hide}
2702 * Returns the IMS Registration Status
2703 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002704 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002705 public boolean isImsRegistered() {
2706 return mPhone.isImsRegistered();
2707 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002708
2709 @Override
2710 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2711 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2712 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002713
Nathan Haroldc55097a2015-03-11 18:14:50 -07002714 /*
2715 * {@hide}
2716 * Returns the IMS Registration Status
2717 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002718 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002719 return mPhone.isWifiCallingEnabled();
2720 }
2721
2722 /*
2723 * {@hide}
2724 * Returns the IMS Registration Status
2725 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002726 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002727 return mPhone.isVolteEnabled();
2728 }
Svet Ganovb320e182015-04-16 12:30:10 -07002729
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002730 /*
2731 * {@hide} Returns the IMS Registration Status
2732 */
2733 public boolean isVideoTelephonyAvailable() {
2734 return mPhone.isVideoEnabled();
2735 }
2736
Svet Ganovb320e182015-04-16 12:30:10 -07002737 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002738 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002739 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002740 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2741
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002742 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002743 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002744 } catch (SecurityException e) {
2745 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2746 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002747 }
Svet Ganovb320e182015-04-16 12:30:10 -07002748
2749 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2750 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2751 return false;
2752 }
2753
2754 return true;
2755 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002756
Makoto Onukifee69342015-06-29 14:44:50 -07002757 /**
Jack Yue7939d82015-12-10 16:04:02 -08002758 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002759 */
2760 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002761 // Default SMS app can always read it.
2762 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2763 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2764 return true;
2765 }
2766 try {
2767 return canReadPhoneState(callingPackage, message);
Jack Yue7939d82015-12-10 16:04:02 -08002768 } catch (SecurityException readPhoneStateSecurityException) {
2769 try {
2770 // Can be read with READ_SMS too.
2771 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2772 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2773 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2774 } catch (SecurityException readSmsSecurityException) {
2775 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2776 // permissions
2777 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2778 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2779 " or " + android.Manifest.permission.READ_SMS + ".");
2780 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002781 }
Makoto Onukifee69342015-06-29 14:44:50 -07002782 }
2783
Stuart Scott8eef64f2015-04-08 15:13:54 -07002784 @Override
2785 public void factoryReset(int subId) {
2786 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002787 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2788 return;
2789 }
2790
Svet Ganovcc087f82015-05-12 20:35:54 -07002791 final long identity = Binder.clearCallingIdentity();
2792 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002793 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2794 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002795 // Enable data
2796 setDataEnabled(subId, true);
2797 // Set network selection mode to automatic
2798 setNetworkSelectionModeAutomatic(subId);
2799 // Set preferred mobile network type to the best available
2800 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2801 // Turn off roaming
2802 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2803 }
2804 } finally {
2805 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002806 }
2807 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002808
2809 @Override
2810 public String getLocaleFromDefaultSim() {
2811 // We query all subscriptions instead of just the active ones, because
2812 // this might be called early on in the provisioning flow when the
2813 // subscriptions potentially aren't active yet.
2814 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2815 if (slist == null || slist.isEmpty()) {
2816 return null;
2817 }
2818
2819 // This function may be called very early, say, from the setup wizard, at
2820 // which point we won't have a default subscription set. If that's the case
2821 // we just choose the first, which will be valid in "most cases".
2822 final int defaultSubId = getDefaultSubscription();
2823 SubscriptionInfo info = null;
2824 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2825 info = slist.get(0);
2826 } else {
2827 for (SubscriptionInfo item : slist) {
2828 if (item.getSubscriptionId() == defaultSubId) {
2829 info = item;
2830 break;
2831 }
2832 }
2833
2834 if (info == null) {
2835 return null;
2836 }
2837 }
2838
2839 // Try and fetch the locale from the carrier properties or from the SIM language
2840 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002841 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002842 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002843 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002844 if (defaultPhone != null) {
2845 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2846 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002847 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002848 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2849 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002850 } else {
2851 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002852 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002853 }
2854 }
2855
Narayan Kamath011676f2015-07-29 12:04:08 +01002856 // The SIM language preferences only store a language (e.g. fr = French), not an
2857 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2858 // the SIM and carrier preferences does not include a country we add the country
2859 // determined from the SIM MCC to provide an exact locale.
2860 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002861 if (mccLocale != null) {
2862 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2863 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002864 }
2865
Tony Hill183b2de2015-06-24 14:53:58 +01002866 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002867 return null;
2868 }
2869
2870 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2871 final long identity = Binder.clearCallingIdentity();
2872 try {
2873 return mSubscriptionController.getAllSubInfoList(
2874 mPhone.getContext().getOpPackageName());
2875 } finally {
2876 Binder.restoreCallingIdentity(identity);
2877 }
2878 }
2879
2880 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2881 final long identity = Binder.clearCallingIdentity();
2882 try {
2883 return mSubscriptionController.getActiveSubscriptionInfoList(
2884 mPhone.getContext().getOpPackageName());
2885 } finally {
2886 Binder.restoreCallingIdentity(identity);
2887 }
2888 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002889
2890 /**
2891 * {@hide}
2892 * Returns the modem stats
2893 */
2894 @Override
2895 public ModemActivityInfo getModemActivityInfo() {
2896 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2897 }
Jack Yue7939d82015-12-10 16:04:02 -08002898
2899 /**
2900 * {@hide}
2901 * Returns the service state information on specified subscription.
2902 */
2903 @Override
2904 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2905
2906 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2907 return null;
2908 }
2909
2910 final Phone phone = getPhone(subId);
2911 if (phone == null) {
2912 return null;
2913 }
2914
2915 return phone.getServiceState();
2916 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002917}