blob: 1997063a1feb6e2e5111250f4b846c27edad5777 [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;
Nancy Chen31f9ba12016-01-06 11:42:12 -080040import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070041import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070042import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070043import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070044import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080045import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070046import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080048import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080049import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080050import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070051import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080053import android.util.ArrayMap;
54import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070055import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080056import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080057import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058
Andrew Lee312e8172014-10-23 17:01:36 -070059import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070060import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070061import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070062import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070064import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080065import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010066import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070067import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070069import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070070import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070071import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070072import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080073import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070074import com.android.internal.telephony.uicc.IccIoResult;
75import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070076import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070077import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080078import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080079import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070080
Wink Saville36469e72014-06-11 15:17:00 -070081import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
82
Santos Cordon7d4ddf62013-07-10 11:58:08 -070083import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080084import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080085import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010086import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080087import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088
89/**
90 * Implementation of the ITelephony interface.
91 */
Santos Cordon117fee72014-05-16 17:56:12 -070092public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093 private static final String LOG_TAG = "PhoneInterfaceManager";
94 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
95 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080096 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070097
98 // Message codes used with mMainThreadHandler
99 private static final int CMD_HANDLE_PIN_MMI = 1;
100 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
101 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
102 private static final int CMD_ANSWER_RINGING_CALL = 4;
103 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700104 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
105 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700106 private static final int CMD_OPEN_CHANNEL = 9;
107 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
108 private static final int CMD_CLOSE_CHANNEL = 11;
109 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800110 private static final int CMD_NV_READ_ITEM = 13;
111 private static final int EVENT_NV_READ_ITEM_DONE = 14;
112 private static final int CMD_NV_WRITE_ITEM = 15;
113 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
114 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
115 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
116 private static final int CMD_NV_RESET_CONFIG = 19;
117 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800118 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
119 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
120 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
121 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800122 private static final int CMD_SEND_ENVELOPE = 25;
123 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700124 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
125 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
126 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
127 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
128 private static final int CMD_EXCHANGE_SIM_IO = 31;
129 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800130 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
131 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700132 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
133 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700134 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
135 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700136 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
137 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
138 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
139 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700140
141 /** The singleton instance. */
142 private static PhoneInterfaceManager sInstance;
143
Wink Saville3ab207e2014-11-20 13:07:20 -0800144 private PhoneGlobals mApp;
145 private Phone mPhone;
146 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700147 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800148 private AppOpsManager mAppOps;
149 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800150 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800151 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700152
Derek Tan97ebb422014-09-05 16:55:38 -0700153 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
154 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800155 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700156
157 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700158 * A request object to use for transmitting data to an ICC.
159 */
160 private static final class IccAPDUArgument {
161 public int channel, cla, command, p1, p2, p3;
162 public String data;
163
164 public IccAPDUArgument(int channel, int cla, int command,
165 int p1, int p2, int p3, String data) {
166 this.channel = channel;
167 this.cla = cla;
168 this.command = command;
169 this.p1 = p1;
170 this.p2 = p2;
171 this.p3 = p3;
172 this.data = data;
173 }
174 }
175
176 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700177 * A request object to use for transmitting data to an ICC.
178 */
179 private static final class ManualNetworkSelectionArgument {
180 public OperatorInfo operatorInfo;
181 public boolean persistSelection;
182
183 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
184 this.operatorInfo = operatorInfo;
185 this.persistSelection = persistSelection;
186 }
187 }
188
189 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700190 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
191 * request after sending. The main thread will notify the request when it is complete.
192 */
193 private static final class MainThreadRequest {
194 /** The argument to use for the request */
195 public Object argument;
196 /** The result of the request that is run on the main thread */
197 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800198 /** The subscriber id that this request applies to. Null if default. */
199 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700200
201 public MainThreadRequest(Object argument) {
202 this.argument = argument;
203 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800204
205 public MainThreadRequest(Object argument, Integer subId) {
206 this.argument = argument;
207 this.subId = subId;
208 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700209 }
210
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800211 private static final class IncomingThirdPartyCallArgs {
212 public final ComponentName component;
213 public final String callId;
214 public final String callerDisplayName;
215
216 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
217 String callerDisplayName) {
218 this.component = component;
219 this.callId = callId;
220 this.callerDisplayName = callerDisplayName;
221 }
222 }
223
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700224 /**
225 * A handler that processes messages on the main thread in the phone process. Since many
226 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
227 * inbound binder threads to the main thread in the phone process. The Binder thread
228 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
229 * on, which will be notified when the operation completes and will contain the result of the
230 * request.
231 *
232 * <p>If a MainThreadRequest object is provided in the msg.obj field,
233 * note that request.result must be set to something non-null for the calling thread to
234 * unblock.
235 */
236 private final class MainThreadHandler extends Handler {
237 @Override
238 public void handleMessage(Message msg) {
239 MainThreadRequest request;
240 Message onCompleted;
241 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800242 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700243 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700244
245 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700246 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700248 final Phone phone = getPhoneFromRequest(request);
249 request.result = phone != null ?
250 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
251 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252 // Wake up the requesting thread
253 synchronized (request) {
254 request.notifyAll();
255 }
256 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700257 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700258
259 case CMD_HANDLE_NEIGHBORING_CELL:
260 request = (MainThreadRequest) msg.obj;
261 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
262 request);
263 mPhone.getNeighboringCids(onCompleted);
264 break;
265
266 case EVENT_NEIGHBORING_CELL_DONE:
267 ar = (AsyncResult) msg.obj;
268 request = (MainThreadRequest) ar.userObj;
269 if (ar.exception == null && ar.result != null) {
270 request.result = ar.result;
271 } else {
272 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800273 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700274 }
275 // Wake up the requesting thread
276 synchronized (request) {
277 request.notifyAll();
278 }
279 break;
280
281 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700282 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800283 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700284 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700285 break;
286
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 case CMD_END_CALL:
288 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800289 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700290 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700291 Phone phone = getPhone(end_subId);
292 if (phone == null) {
293 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
294 break;
295 }
296 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700297 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
298 // CDMA: If the user presses the Power button we treat it as
299 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700300 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700301 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
302 // GSM: End the call as per the Phone state
303 hungUp = PhoneUtils.hangup(mCM);
304 } else {
305 throw new IllegalStateException("Unexpected phone type: " + phoneType);
306 }
307 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
308 request.result = hungUp;
309 // Wake up the requesting thread
310 synchronized (request) {
311 request.notifyAll();
312 }
313 break;
314
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700315 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700316 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700317 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700318 if (uiccCard == null) {
319 loge("iccTransmitApduLogicalChannel: No UICC");
320 request.result = new IccIoResult(0x6F, 0, (byte[])null);
321 synchronized (request) {
322 request.notifyAll();
323 }
324 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700325 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
326 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700327 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 iccArgument.channel, iccArgument.cla, iccArgument.command,
329 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700330 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700331 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700332 break;
333
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700334 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700335 ar = (AsyncResult) msg.obj;
336 request = (MainThreadRequest) ar.userObj;
337 if (ar.exception == null && ar.result != null) {
338 request.result = ar.result;
339 } else {
340 request.result = new IccIoResult(0x6F, 0, (byte[])null);
341 if (ar.result == null) {
342 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800343 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700344 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800345 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700346 } else {
347 loge("iccTransmitApduLogicalChannel: Unknown exception");
348 }
349 }
350 synchronized (request) {
351 request.notifyAll();
352 }
353 break;
354
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700355 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
356 request = (MainThreadRequest) msg.obj;
357 iccArgument = (IccAPDUArgument) request.argument;
358 if (uiccCard == null) {
359 loge("iccTransmitApduBasicChannel: No UICC");
360 request.result = new IccIoResult(0x6F, 0, (byte[])null);
361 synchronized (request) {
362 request.notifyAll();
363 }
364 } else {
365 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
366 request);
367 uiccCard.iccTransmitApduBasicChannel(
368 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
369 iccArgument.p3, iccArgument.data, onCompleted);
370 }
371 break;
372
373 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
374 ar = (AsyncResult) msg.obj;
375 request = (MainThreadRequest) ar.userObj;
376 if (ar.exception == null && ar.result != null) {
377 request.result = ar.result;
378 } else {
379 request.result = new IccIoResult(0x6F, 0, (byte[])null);
380 if (ar.result == null) {
381 loge("iccTransmitApduBasicChannel: Empty response");
382 } else if (ar.exception instanceof CommandException) {
383 loge("iccTransmitApduBasicChannel: CommandException: " +
384 ar.exception);
385 } else {
386 loge("iccTransmitApduBasicChannel: Unknown exception");
387 }
388 }
389 synchronized (request) {
390 request.notifyAll();
391 }
392 break;
393
394 case CMD_EXCHANGE_SIM_IO:
395 request = (MainThreadRequest) msg.obj;
396 iccArgument = (IccAPDUArgument) request.argument;
397 if (uiccCard == null) {
398 loge("iccExchangeSimIO: No UICC");
399 request.result = new IccIoResult(0x6F, 0, (byte[])null);
400 synchronized (request) {
401 request.notifyAll();
402 }
403 } else {
404 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
405 request);
406 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
407 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
408 iccArgument.data, onCompleted);
409 }
410 break;
411
412 case EVENT_EXCHANGE_SIM_IO_DONE:
413 ar = (AsyncResult) msg.obj;
414 request = (MainThreadRequest) ar.userObj;
415 if (ar.exception == null && ar.result != null) {
416 request.result = ar.result;
417 } else {
418 request.result = new IccIoResult(0x6f, 0, (byte[])null);
419 }
420 synchronized (request) {
421 request.notifyAll();
422 }
423 break;
424
Derek Tan4d5e5c12014-02-04 11:54:58 -0800425 case CMD_SEND_ENVELOPE:
426 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700427 if (uiccCard == null) {
428 loge("sendEnvelopeWithStatus: No UICC");
429 request.result = new IccIoResult(0x6F, 0, (byte[])null);
430 synchronized (request) {
431 request.notifyAll();
432 }
433 } else {
434 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
435 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
436 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800437 break;
438
439 case EVENT_SEND_ENVELOPE_DONE:
440 ar = (AsyncResult) msg.obj;
441 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700442 if (ar.exception == null && ar.result != null) {
443 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800444 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700445 request.result = new IccIoResult(0x6F, 0, (byte[])null);
446 if (ar.result == null) {
447 loge("sendEnvelopeWithStatus: Empty response");
448 } else if (ar.exception instanceof CommandException) {
449 loge("sendEnvelopeWithStatus: CommandException: " +
450 ar.exception);
451 } else {
452 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
453 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800454 }
455 synchronized (request) {
456 request.notifyAll();
457 }
458 break;
459
Shishir Agrawal566b7612013-10-28 14:41:00 -0700460 case CMD_OPEN_CHANNEL:
461 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700462 if (uiccCard == null) {
463 loge("iccOpenLogicalChannel: No UICC");
464 request.result = new IccIoResult(0x6F, 0, (byte[])null);
465 synchronized (request) {
466 request.notifyAll();
467 }
468 } else {
469 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
470 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
471 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700472 break;
473
474 case EVENT_OPEN_CHANNEL_DONE:
475 ar = (AsyncResult) msg.obj;
476 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700477 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700478 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700479 int[] result = (int[]) ar.result;
480 int channelId = result[0];
481 byte[] selectResponse = null;
482 if (result.length > 1) {
483 selectResponse = new byte[result.length - 1];
484 for (int i = 1; i < result.length; ++i) {
485 selectResponse[i - 1] = (byte) result[i];
486 }
487 }
488 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700489 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700490 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700491 if (ar.result == null) {
492 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700494 if (ar.exception != null) {
495 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
496 }
497
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700498 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700499 if (ar.exception instanceof CommandException) {
500 CommandException.Error error =
501 ((CommandException) (ar.exception)).getCommandError();
502 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700503 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700504 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700505 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700506 }
507 }
508 openChannelResp = new IccOpenLogicalChannelResponse(
509 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700510 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700511 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 synchronized (request) {
513 request.notifyAll();
514 }
515 break;
516
517 case CMD_CLOSE_CHANNEL:
518 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700519 if (uiccCard == null) {
520 loge("iccCloseLogicalChannel: No UICC");
521 request.result = new IccIoResult(0x6F, 0, (byte[])null);
522 synchronized (request) {
523 request.notifyAll();
524 }
525 } else {
526 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
527 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
528 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700529 break;
530
531 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800532 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
533 break;
534
535 case CMD_NV_READ_ITEM:
536 request = (MainThreadRequest) msg.obj;
537 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
538 mPhone.nvReadItem((Integer) request.argument, onCompleted);
539 break;
540
541 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700542 ar = (AsyncResult) msg.obj;
543 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800544 if (ar.exception == null && ar.result != null) {
545 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800547 request.result = "";
548 if (ar.result == null) {
549 loge("nvReadItem: Empty response");
550 } else if (ar.exception instanceof CommandException) {
551 loge("nvReadItem: CommandException: " +
552 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700553 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800554 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 }
556 }
557 synchronized (request) {
558 request.notifyAll();
559 }
560 break;
561
Jake Hambye994d462014-02-03 13:10:13 -0800562 case CMD_NV_WRITE_ITEM:
563 request = (MainThreadRequest) msg.obj;
564 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
565 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
566 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
567 break;
568
569 case EVENT_NV_WRITE_ITEM_DONE:
570 handleNullReturnEvent(msg, "nvWriteItem");
571 break;
572
573 case CMD_NV_WRITE_CDMA_PRL:
574 request = (MainThreadRequest) msg.obj;
575 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
576 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
577 break;
578
579 case EVENT_NV_WRITE_CDMA_PRL_DONE:
580 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
581 break;
582
583 case CMD_NV_RESET_CONFIG:
584 request = (MainThreadRequest) msg.obj;
585 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
586 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
587 break;
588
589 case EVENT_NV_RESET_CONFIG_DONE:
590 handleNullReturnEvent(msg, "nvResetConfig");
591 break;
592
Jake Hamby7c27be32014-03-03 13:25:59 -0800593 case CMD_GET_PREFERRED_NETWORK_TYPE:
594 request = (MainThreadRequest) msg.obj;
595 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700596 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800597 break;
598
599 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
600 ar = (AsyncResult) msg.obj;
601 request = (MainThreadRequest) ar.userObj;
602 if (ar.exception == null && ar.result != null) {
603 request.result = ar.result; // Integer
604 } else {
605 request.result = -1;
606 if (ar.result == null) {
607 loge("getPreferredNetworkType: Empty response");
608 } else if (ar.exception instanceof CommandException) {
609 loge("getPreferredNetworkType: CommandException: " +
610 ar.exception);
611 } else {
612 loge("getPreferredNetworkType: Unknown exception");
613 }
614 }
615 synchronized (request) {
616 request.notifyAll();
617 }
618 break;
619
620 case CMD_SET_PREFERRED_NETWORK_TYPE:
621 request = (MainThreadRequest) msg.obj;
622 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
623 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700624 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800625 break;
626
627 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
628 handleNullReturnEvent(msg, "setPreferredNetworkType");
629 break;
630
Steven Liu4bf01bc2014-07-17 11:05:29 -0500631 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
632 request = (MainThreadRequest)msg.obj;
633 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
634 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
635 break;
636
637 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
638 ar = (AsyncResult)msg.obj;
639 request = (MainThreadRequest)ar.userObj;
640 request.result = ar;
641 synchronized (request) {
642 request.notifyAll();
643 }
644 break;
645
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800646 case CMD_SET_VOICEMAIL_NUMBER:
647 request = (MainThreadRequest) msg.obj;
648 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
649 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800650 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
651 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800652 break;
653
654 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
655 handleNullReturnEvent(msg, "setVoicemailNumber");
656 break;
657
Stuart Scott54788802015-03-30 13:18:01 -0700658 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
659 request = (MainThreadRequest) msg.obj;
660 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
661 request);
662 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
663 break;
664
665 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
666 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
667 break;
668
Shishir Agrawal302c8692015-06-19 13:49:39 -0700669 case CMD_PERFORM_NETWORK_SCAN:
670 request = (MainThreadRequest) msg.obj;
671 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
672 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
673 break;
674
675 case EVENT_PERFORM_NETWORK_SCAN_DONE:
676 ar = (AsyncResult) msg.obj;
677 request = (MainThreadRequest) ar.userObj;
678 CellNetworkScanResult cellScanResult;
679 if (ar.exception == null && ar.result != null) {
680 cellScanResult = new CellNetworkScanResult(
681 CellNetworkScanResult.STATUS_SUCCESS,
682 (List<OperatorInfo>) ar.result);
683 } else {
684 if (ar.result == null) {
685 loge("getCellNetworkScanResults: Empty response");
686 }
687 if (ar.exception != null) {
688 loge("getCellNetworkScanResults: Exception: " + ar.exception);
689 }
690 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
691 if (ar.exception instanceof CommandException) {
692 CommandException.Error error =
693 ((CommandException) (ar.exception)).getCommandError();
694 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
695 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
696 } else if (error == CommandException.Error.GENERIC_FAILURE) {
697 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
698 }
699 }
700 cellScanResult = new CellNetworkScanResult(errorCode, null);
701 }
702 request.result = cellScanResult;
703 synchronized (request) {
704 request.notifyAll();
705 }
706 break;
707
708 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
709 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700710 ManualNetworkSelectionArgument selArg =
711 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700712 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
713 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700714 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
715 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700716 break;
717
718 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
719 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
720 break;
721
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700722 case CMD_GET_MODEM_ACTIVITY_INFO:
723 request = (MainThreadRequest) msg.obj;
724 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700725 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700726 break;
727
728 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
729 ar = (AsyncResult) msg.obj;
730 request = (MainThreadRequest) ar.userObj;
731 if (ar.exception == null && ar.result != null) {
732 request.result = ar.result;
733 } else {
734 if (ar.result == null) {
735 loge("queryModemActivityInfo: Empty response");
736 } else if (ar.exception instanceof CommandException) {
737 loge("queryModemActivityInfo: CommandException: " +
738 ar.exception);
739 } else {
740 loge("queryModemActivityInfo: Unknown exception");
741 }
742 }
743 synchronized (request) {
744 request.notifyAll();
745 }
746 break;
747
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700748 default:
749 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
750 break;
751 }
752 }
Jake Hambye994d462014-02-03 13:10:13 -0800753
754 private void handleNullReturnEvent(Message msg, String command) {
755 AsyncResult ar = (AsyncResult) msg.obj;
756 MainThreadRequest request = (MainThreadRequest) ar.userObj;
757 if (ar.exception == null) {
758 request.result = true;
759 } else {
760 request.result = false;
761 if (ar.exception instanceof CommandException) {
762 loge(command + ": CommandException: " + ar.exception);
763 } else {
764 loge(command + ": Unknown exception");
765 }
766 }
767 synchronized (request) {
768 request.notifyAll();
769 }
770 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700771 }
772
773 /**
774 * Posts the specified command to be executed on the main thread,
775 * waits for the request to complete, and returns the result.
776 * @see #sendRequestAsync
777 */
778 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700779 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700780 }
781
782 /**
783 * Posts the specified command to be executed on the main thread,
784 * waits for the request to complete, and returns the result.
785 * @see #sendRequestAsync
786 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800787 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700788 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
789 throw new RuntimeException("This method will deadlock if called from the main thread.");
790 }
791
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800792 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700793 Message msg = mMainThreadHandler.obtainMessage(command, request);
794 msg.sendToTarget();
795
796 // Wait for the request to complete
797 synchronized (request) {
798 while (request.result == null) {
799 try {
800 request.wait();
801 } catch (InterruptedException e) {
802 // Do nothing, go back and wait until the request is complete
803 }
804 }
805 }
806 return request.result;
807 }
808
809 /**
810 * Asynchronous ("fire and forget") version of sendRequest():
811 * Posts the specified command to be executed on the main thread, and
812 * returns immediately.
813 * @see #sendRequest
814 */
815 private void sendRequestAsync(int command) {
816 mMainThreadHandler.sendEmptyMessage(command);
817 }
818
819 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700820 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
821 * @see {@link #sendRequest(int,Object)}
822 */
823 private void sendRequestAsync(int command, Object argument) {
824 MainThreadRequest request = new MainThreadRequest(argument);
825 Message msg = mMainThreadHandler.obtainMessage(command, request);
826 msg.sendToTarget();
827 }
828
829 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700830 * Initialize the singleton PhoneInterfaceManager instance.
831 * This is only done once, at startup, from PhoneApp.onCreate().
832 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700833 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700834 synchronized (PhoneInterfaceManager.class) {
835 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700836 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700837 } else {
838 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
839 }
840 return sInstance;
841 }
842 }
843
844 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700845 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700846 mApp = app;
847 mPhone = phone;
848 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700849 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700850 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
851 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700852 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700853 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800854 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800855
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 publish();
857 }
858
859 private void publish() {
860 if (DBG) log("publish: " + this);
861
862 ServiceManager.addService("phone", this);
863 }
864
Stuart Scott584921c2015-01-15 17:10:34 -0800865 private Phone getPhoneFromRequest(MainThreadRequest request) {
866 return (request.subId == null) ? mPhone : getPhone(request.subId);
867 }
868
Wink Saville36469e72014-06-11 15:17:00 -0700869 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700870 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800871 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700872 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700873 //
874 // Implementation of the ITelephony interface.
875 //
876
877 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700878 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700879 }
880
Wink Savilleb564aae2014-10-23 10:18:09 -0700881 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 if (DBG) log("dial: " + number);
883 // No permission check needed here: This is just a wrapper around the
884 // ACTION_DIAL intent, which is available to any app since it puts up
885 // the UI before it does anything.
886
887 String url = createTelUrl(number);
888 if (url == null) {
889 return;
890 }
891
892 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700893 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700894 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
895 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
896 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
897 mApp.startActivity(intent);
898 }
899 }
900
901 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700902 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700903 }
904
Wink Savilleb564aae2014-10-23 10:18:09 -0700905 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700906 if (DBG) log("call: " + number);
907
908 // This is just a wrapper around the ACTION_CALL intent, but we still
909 // need to do a permission check since we're calling startActivity()
910 // from the context of the phone app.
911 enforceCallPermission();
912
913 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
914 != AppOpsManager.MODE_ALLOWED) {
915 return;
916 }
917
918 String url = createTelUrl(number);
919 if (url == null) {
920 return;
921 }
922
Wink Saville08874612014-08-31 19:19:58 -0700923 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100924 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800925 if (slist != null) {
926 for (SubscriptionInfo subInfoRecord : slist) {
927 if (subInfoRecord.getSubscriptionId() == subId) {
928 isValid = true;
929 break;
930 }
Wink Saville08874612014-08-31 19:19:58 -0700931 }
932 }
933 if (isValid == false) {
934 return;
935 }
936
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700937 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700938 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
940 mApp.startActivity(intent);
941 }
942
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700943 /**
944 * End a call based on call state
945 * @return true is a call was ended
946 */
947 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700948 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700949 }
950
951 /**
952 * End a call based on the call state of the subId
953 * @return true is a call was ended
954 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700955 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700956 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800957 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 }
959
960 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700961 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700962 }
963
Wink Savilleb564aae2014-10-23 10:18:09 -0700964 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 if (DBG) log("answerRingingCall...");
966 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
967 // but that can probably wait till the big TelephonyManager API overhaul.
968 // For now, protect this call with the MODIFY_PHONE_STATE permission.
969 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800970 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700971 }
972
973 /**
974 * Make the actual telephony calls to implement answerRingingCall().
975 * This should only be called from the main thread of the Phone app.
976 * @see #answerRingingCall
977 *
978 * TODO: it would be nice to return true if we answered the call, or
979 * false if there wasn't actually a ringing incoming call, or some
980 * other error occurred. (In other words, pass back the return value
981 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
982 * But that would require calling this method via sendRequest() rather
983 * than sendRequestAsync(), and right now we don't actually *need* that
984 * return value, so let's just return void for now.
985 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700986 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700987 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700988 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700989 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
990 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 if (hasActiveCall && hasHoldingCall) {
992 // Both lines are in use!
993 // TODO: provide a flag to let the caller specify what
994 // policy to use if both lines are in use. (The current
995 // behavior is hardwired to "answer incoming, end ongoing",
996 // which is how the CALL button is specced to behave.)
997 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
998 return;
999 } else {
1000 // answerCall() will automatically hold the current active
1001 // call, if there is one.
1002 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1003 return;
1004 }
1005 } else {
1006 // No call was ringing.
1007 return;
1008 }
1009 }
1010
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001011 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001012 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001014 public void silenceRinger() {
1015 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001016 }
1017
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001018 @Override
1019 public boolean isOffhook(String callingPackage) {
1020 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001021 }
1022
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001023 @Override
1024 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1025 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1026 return false;
1027 }
1028
Sanket Padawe356d7632015-06-22 14:03:32 -07001029 final Phone phone = getPhone(subId);
1030 if (phone != null) {
1031 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1032 } else {
1033 return false;
1034 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001035 }
1036
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001037 @Override
1038 public boolean isRinging(String callingPackage) {
1039 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001040 }
1041
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001042 @Override
1043 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1044 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1045 return false;
1046 }
1047
Sanket Padawe356d7632015-06-22 14:03:32 -07001048 final Phone phone = getPhone(subId);
1049 if (phone != null) {
1050 return (phone.getState() == PhoneConstants.State.RINGING);
1051 } else {
1052 return false;
1053 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001054 }
1055
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001056 @Override
1057 public boolean isIdle(String callingPackage) {
1058 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001059 }
1060
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001061 @Override
1062 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1063 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1064 return false;
1065 }
1066
Sanket Padawe356d7632015-06-22 14:03:32 -07001067 final Phone phone = getPhone(subId);
1068 if (phone != null) {
1069 return (phone.getState() == PhoneConstants.State.IDLE);
1070 } else {
1071 return false;
1072 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001073 }
1074
Svet Ganovb320e182015-04-16 12:30:10 -07001075 public boolean isSimPinEnabled(String callingPackage) {
1076 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1077 return false;
1078 }
1079
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 return (PhoneGlobals.getInstance().isSimPinEnabled());
1081 }
1082
1083 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001084 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001085 }
1086
Wink Savilleb564aae2014-10-23 10:18:09 -07001087 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001088 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001089 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1090 }
1091
1092 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001093 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001094 }
1095
Wink Savilleb564aae2014-10-23 10:18:09 -07001096 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001097 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001098 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1099 }
1100
1101 /** {@hide} */
1102 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001103 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001104 }
1105
Wink Savilleb564aae2014-10-23 10:18:09 -07001106 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001107 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001108 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 checkSimPin.start();
1110 return checkSimPin.unlockSim(null, pin);
1111 }
1112
Wink Saville9de0f752013-10-22 19:04:03 -07001113 /** {@hide} */
1114 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001115 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001116 }
1117
Wink Savilleb564aae2014-10-23 10:18:09 -07001118 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001119 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001120 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 checkSimPuk.start();
1122 return checkSimPuk.unlockSim(puk, pin);
1123 }
1124
1125 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001126 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 * a synchronous one.
1128 */
1129 private static class UnlockSim extends Thread {
1130
1131 private final IccCard mSimCard;
1132
1133 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001134 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1135 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001136
1137 // For replies from SimCard interface
1138 private Handler mHandler;
1139
1140 // For async handler to identify request type
1141 private static final int SUPPLY_PIN_COMPLETE = 100;
1142
1143 public UnlockSim(IccCard simCard) {
1144 mSimCard = simCard;
1145 }
1146
1147 @Override
1148 public void run() {
1149 Looper.prepare();
1150 synchronized (UnlockSim.this) {
1151 mHandler = new Handler() {
1152 @Override
1153 public void handleMessage(Message msg) {
1154 AsyncResult ar = (AsyncResult) msg.obj;
1155 switch (msg.what) {
1156 case SUPPLY_PIN_COMPLETE:
1157 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1158 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001159 mRetryCount = msg.arg1;
1160 if (ar.exception != null) {
1161 if (ar.exception instanceof CommandException &&
1162 ((CommandException)(ar.exception)).getCommandError()
1163 == CommandException.Error.PASSWORD_INCORRECT) {
1164 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1165 } else {
1166 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1167 }
1168 } else {
1169 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1170 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001171 mDone = true;
1172 UnlockSim.this.notifyAll();
1173 }
1174 break;
1175 }
1176 }
1177 };
1178 UnlockSim.this.notifyAll();
1179 }
1180 Looper.loop();
1181 }
1182
1183 /*
1184 * Use PIN or PUK to unlock SIM card
1185 *
1186 * If PUK is null, unlock SIM card with PIN
1187 *
1188 * If PUK is not null, unlock SIM card with PUK and set PIN code
1189 */
Wink Saville9de0f752013-10-22 19:04:03 -07001190 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001191
1192 while (mHandler == null) {
1193 try {
1194 wait();
1195 } catch (InterruptedException e) {
1196 Thread.currentThread().interrupt();
1197 }
1198 }
1199 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1200
1201 if (puk == null) {
1202 mSimCard.supplyPin(pin, callback);
1203 } else {
1204 mSimCard.supplyPuk(puk, pin, callback);
1205 }
1206
1207 while (!mDone) {
1208 try {
1209 Log.d(LOG_TAG, "wait for done");
1210 wait();
1211 } catch (InterruptedException e) {
1212 // Restore the interrupted status
1213 Thread.currentThread().interrupt();
1214 }
1215 }
1216 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001217 int[] resultArray = new int[2];
1218 resultArray[0] = mResult;
1219 resultArray[1] = mRetryCount;
1220 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001221 }
1222 }
1223
1224 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001225 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001226
1227 }
1228
Wink Savilleb564aae2014-10-23 10:18:09 -07001229 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001230 // No permission check needed here: this call is harmless, and it's
1231 // needed for the ServiceState.requestStateUpdate() call (which is
1232 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001233 final Phone phone = getPhone(subId);
1234 if (phone != null) {
1235 phone.updateServiceLocation();
1236 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001237 }
1238
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001239 @Override
1240 public boolean isRadioOn(String callingPackage) {
1241 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001242 }
1243
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001244 @Override
1245 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1246 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1247 return false;
1248 }
1249 return isRadioOnForSubscriber(subId);
1250 }
1251
1252 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001253 final Phone phone = getPhone(subId);
1254 if (phone != null) {
1255 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1256 } else {
1257 return false;
1258 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001259 }
1260
1261 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001262 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001263
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001264 }
Wink Saville36469e72014-06-11 15:17:00 -07001265
Wink Savilleb564aae2014-10-23 10:18:09 -07001266 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001267 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001268 final Phone phone = getPhone(subId);
1269 if (phone != null) {
1270 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1271 }
Wink Saville36469e72014-06-11 15:17:00 -07001272 }
1273
1274 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001275 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001276 }
1277
Wink Savilleb564aae2014-10-23 10:18:09 -07001278 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001279 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001280 final Phone phone = getPhone(subId);
1281 if (phone == null) {
1282 return false;
1283 }
1284 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001285 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001286 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001287 }
1288 return true;
1289 }
Wink Saville36469e72014-06-11 15:17:00 -07001290
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001291 public boolean needMobileRadioShutdown() {
1292 /*
1293 * If any of the Radios are available, it will need to be
1294 * shutdown. So return true if any Radio is available.
1295 */
1296 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1297 Phone phone = PhoneFactory.getPhone(i);
1298 if (phone != null && phone.isRadioAvailable()) return true;
1299 }
1300 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1301 return false;
1302 }
1303
1304 public void shutdownMobileRadios() {
1305 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1306 logv("Shutting down Phone " + i);
1307 shutdownRadioUsingPhoneId(i);
1308 }
1309 }
1310
1311 private void shutdownRadioUsingPhoneId(int phoneId) {
1312 enforceModifyPermission();
1313 Phone phone = PhoneFactory.getPhone(phoneId);
1314 if (phone != null && phone.isRadioAvailable()) {
1315 phone.shutdownRadio();
1316 }
1317 }
1318
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001319 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001320 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001321 }
1322
Wink Savilleb564aae2014-10-23 10:18:09 -07001323 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001324 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001325 final Phone phone = getPhone(subId);
1326 if (phone != null) {
1327 phone.setRadioPower(turnOn);
1328 return true;
1329 } else {
1330 return false;
1331 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001332 }
1333
Wink Saville36469e72014-06-11 15:17:00 -07001334 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001335 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001336 public boolean enableDataConnectivity() {
1337 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001338 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001339 final Phone phone = getPhone(subId);
1340 if (phone != null) {
1341 phone.setDataEnabled(true);
1342 return true;
1343 } else {
1344 return false;
1345 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001346 }
1347
Wink Saville36469e72014-06-11 15:17:00 -07001348 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001349 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 public boolean disableDataConnectivity() {
1351 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001352 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001353 final Phone phone = getPhone(subId);
1354 if (phone != null) {
1355 phone.setDataEnabled(false);
1356 return true;
1357 } else {
1358 return false;
1359 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001360 }
1361
Wink Saville36469e72014-06-11 15:17:00 -07001362 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001363 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001364 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001365 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001366 final Phone phone = getPhone(subId);
1367 if (phone != null) {
1368 return phone.isDataConnectivityPossible();
1369 } else {
1370 return false;
1371 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001372 }
1373
1374 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001375 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001376 }
1377
Wink Savilleb564aae2014-10-23 10:18:09 -07001378 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001379 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001380 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1381 return false;
1382 }
Wink Saville36469e72014-06-11 15:17:00 -07001383 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001384 }
1385
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001387 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001388 }
1389
Wink Savilleb564aae2014-10-23 10:18:09 -07001390 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001391 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001392 }
1393
Sanket Padawe356d7632015-06-22 14:03:32 -07001394 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001395 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001396 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001397 if (phone != null) {
1398 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1399 } else {
1400 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1401 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
1403
Sanket Padawe356d7632015-06-22 14:03:32 -07001404 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001405 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001406 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001407 if (phone != null) {
1408 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1409 } else {
1410 return TelephonyManager.DATA_ACTIVITY_NONE;
1411 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
1414 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001415 public Bundle getCellLocation(String callingPackage) {
1416 enforceFineOrCoarseLocationPermission("getCellLocation");
1417
1418 // OP_COARSE_LOCATION controls both fine and coarse location.
1419 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1420 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001421 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001422 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001423 }
1424
Jake Hambye994d462014-02-03 13:10:13 -08001425 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001426 if (DBG_LOC) log("getCellLocation: is active user");
1427 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001428 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001429 if (phone == null) {
1430 return null;
1431 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001432 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001433 return data;
1434 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001435 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001436 return null;
1437 }
1438 }
1439
Svetoslav64fad262015-04-14 14:35:21 -07001440 private void enforceFineOrCoarseLocationPermission(String message) {
1441 try {
1442 mApp.enforceCallingOrSelfPermission(
1443 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1444 } catch (SecurityException e) {
1445 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1446 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1447 // is the weaker precondition
1448 mApp.enforceCallingOrSelfPermission(
1449 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1450 }
1451 }
1452
1453
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 @Override
1455 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001456 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001457 }
1458
Sanket Padawe356d7632015-06-22 14:03:32 -07001459 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001460 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001461 mApp.enforceCallingOrSelfPermission(
1462 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001463 final Phone phone = getPhone(subId);
1464 if (phone != null) {
1465 phone.enableLocationUpdates();
1466 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001467 }
1468
1469 @Override
1470 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001471 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001472 }
1473
Sanket Padawe356d7632015-06-22 14:03:32 -07001474 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001475 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001476 mApp.enforceCallingOrSelfPermission(
1477 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001478 final Phone phone = getPhone(subId);
1479 if (phone != null) {
1480 phone.disableLocationUpdates();
1481 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 }
1483
1484 @Override
1485 @SuppressWarnings("unchecked")
1486 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001487 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1488
1489 // OP_COARSE_LOCATION controls both fine and coarse location.
1490 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1491 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1492 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001493 }
1494
1495 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1496 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1497 return null;
1498 }
Svetoslav64fad262015-04-14 14:35:21 -07001499
Jake Hambye994d462014-02-03 13:10:13 -08001500 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001501 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1502
1503 ArrayList<NeighboringCellInfo> cells = null;
1504
1505 try {
1506 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001507 CMD_HANDLE_NEIGHBORING_CELL, null, null);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001508 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001509 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 }
1511 return cells;
1512 } else {
1513 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1514 return null;
1515 }
1516 }
1517
1518
1519 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001520 public List<CellInfo> getAllCellInfo(String callingPackage) {
1521 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1522
1523 // OP_COARSE_LOCATION controls both fine and coarse location.
1524 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1525 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1526 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001527 }
1528
Jake Hambye994d462014-02-03 13:10:13 -08001529 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001530 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001531 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1532 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001533 final List<CellInfo> info = phone.getAllCellInfo();
1534 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001535 }
1536 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001537 } else {
1538 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1539 return null;
1540 }
1541 }
1542
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001543 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001544 public void setCellInfoListRate(int rateInMillis) {
1545 mPhone.setCellInfoListRate(rateInMillis);
1546 }
1547
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001548 //
1549 // Internal helper methods.
1550 //
1551
Jake Hambye994d462014-02-03 13:10:13 -08001552 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001553 boolean ok;
1554
1555 boolean self = Binder.getCallingUid() == Process.myUid();
1556 if (!self) {
1557 // Get the caller's user id then clear the calling identity
1558 // which will be restored in the finally clause.
1559 int callingUser = UserHandle.getCallingUserId();
1560 long ident = Binder.clearCallingIdentity();
1561
1562 try {
1563 // With calling identity cleared the current user is the foreground user.
1564 int foregroundUser = ActivityManager.getCurrentUser();
1565 ok = (foregroundUser == callingUser);
1566 if (DBG_LOC) {
1567 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1568 + " callingUser=" + callingUser + " ok=" + ok);
1569 }
1570 } catch (Exception ex) {
1571 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1572 ok = false;
1573 } finally {
1574 Binder.restoreCallingIdentity(ident);
1575 }
1576 } else {
1577 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1578 ok = true;
1579 }
1580 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1581 return ok;
1582 }
1583
1584 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001585 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1586 *
1587 * @throws SecurityException if the caller does not have the required permission
1588 */
1589 private void enforceModifyPermission() {
1590 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1591 }
1592
1593 /**
Junda Liua2e36012014-07-09 18:30:01 -07001594 * Make sure either system app or the caller has carrier privilege.
1595 *
1596 * @throws SecurityException if the caller does not have the required permission/privilege
1597 */
1598 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001599 int permission = mApp.checkCallingOrSelfPermission(
1600 android.Manifest.permission.MODIFY_PHONE_STATE);
1601 if (permission == PackageManager.PERMISSION_GRANTED) {
1602 return;
1603 }
1604
1605 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001606 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001607 loge("No Carrier Privilege.");
1608 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001609 }
1610 }
1611
1612 /**
1613 * Make sure the caller has carrier privilege.
1614 *
1615 * @throws SecurityException if the caller does not have the required permission
1616 */
1617 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001618 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001619 loge("No Carrier Privilege.");
1620 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001621 }
1622 }
1623
1624 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001625 * Make sure the caller has the CALL_PHONE permission.
1626 *
1627 * @throws SecurityException if the caller does not have the required permission
1628 */
1629 private void enforceCallPermission() {
1630 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1631 }
1632
Stuart Scott8eef64f2015-04-08 15:13:54 -07001633 private void enforceConnectivityInternalPermission() {
1634 mApp.enforceCallingOrSelfPermission(
1635 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1636 "ConnectivityService");
1637 }
1638
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001639 private String createTelUrl(String number) {
1640 if (TextUtils.isEmpty(number)) {
1641 return null;
1642 }
1643
Jake Hambye994d462014-02-03 13:10:13 -08001644 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001645 }
1646
Ihab Awadf9e92732013-12-05 18:02:52 -08001647 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001648 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1649 }
1650
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001651 private static void logv(String msg) {
1652 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1653 }
1654
Ihab Awadf9e92732013-12-05 18:02:52 -08001655 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001656 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1657 }
1658
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001659 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001660 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001661 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001662 }
1663
Sanket Padawe356d7632015-06-22 14:03:32 -07001664 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001665 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001666 final Phone phone = getPhone(subId);
1667 if (phone == null) {
1668 return PhoneConstants.PHONE_TYPE_NONE;
1669 } else {
1670 return getPhone(subId).getPhoneType();
1671 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001672 }
1673
1674 /**
1675 * Returns the CDMA ERI icon index to display
1676 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001677 @Override
1678 public int getCdmaEriIconIndex(String callingPackage) {
1679 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001680 }
1681
Sanket Padawe356d7632015-06-22 14:03:32 -07001682 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001683 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1684 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1685 return -1;
1686 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001687 final Phone phone = getPhone(subId);
1688 if (phone != null) {
1689 return phone.getCdmaEriIconIndex();
1690 } else {
1691 return -1;
1692 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001693 }
1694
1695 /**
1696 * Returns the CDMA ERI icon mode,
1697 * 0 - ON
1698 * 1 - FLASHING
1699 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001700 @Override
1701 public int getCdmaEriIconMode(String callingPackage) {
1702 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001703 }
1704
Sanket Padawe356d7632015-06-22 14:03:32 -07001705 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001706 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1707 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1708 return -1;
1709 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001710 final Phone phone = getPhone(subId);
1711 if (phone != null) {
1712 return phone.getCdmaEriIconMode();
1713 } else {
1714 return -1;
1715 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001716 }
1717
1718 /**
1719 * Returns the CDMA ERI text,
1720 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001721 @Override
1722 public String getCdmaEriText(String callingPackage) {
1723 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001724 }
1725
Sanket Padawe356d7632015-06-22 14:03:32 -07001726 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001727 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1728 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1729 return null;
1730 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001731 final Phone phone = getPhone(subId);
1732 if (phone != null) {
1733 return phone.getCdmaEriText();
1734 } else {
1735 return null;
1736 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001737 }
1738
1739 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001740 * Returns the CDMA MDN.
1741 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001742 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001743 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001744 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001745 final Phone phone = getPhone(subId);
1746 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1747 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001748 } else {
1749 return null;
1750 }
1751 }
1752
1753 /**
1754 * Returns the CDMA MIN.
1755 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001756 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001757 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001758 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001759 final Phone phone = getPhone(subId);
1760 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1761 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001762 } else {
1763 return null;
1764 }
1765 }
1766
1767 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001768 * Returns true if CDMA provisioning needs to run.
1769 */
1770 public boolean needsOtaServiceProvisioning() {
1771 return mPhone.needsOtaServiceProvisioning();
1772 }
1773
1774 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001775 * Sets the voice mail number of a given subId.
1776 */
1777 @Override
1778 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001779 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001780 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1781 new Pair<String, String>(alphaTag, number), new Integer(subId));
1782 return success;
1783 }
1784
1785 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 * Returns the unread count of voicemails
1787 */
1788 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001789 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001790 }
1791
1792 /**
1793 * Returns the unread count of voicemails for a subId
1794 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001795 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001796 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001797 final Phone phone = getPhone(subId);
1798 if (phone != null) {
1799 return phone.getVoiceMessageCount();
1800 } else {
1801 return 0;
1802 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001803 }
1804
1805 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001806 * Returns the data network type.
1807 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001808 *
1809 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1810 */
1811 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001812 public int getNetworkType() {
1813 final Phone phone = getPhone(getDefaultSubscription());
1814 if (phone != null) {
1815 return phone.getServiceState().getDataNetworkType();
1816 } else {
1817 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1818 }
Wink Saville36469e72014-06-11 15:17:00 -07001819 }
1820
1821 /**
1822 * Returns the network type for a subId
1823 */
1824 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001825 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1826 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1827 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1828 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001829
Sanket Padawe356d7632015-06-22 14:03:32 -07001830 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001831 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001832 return phone.getServiceState().getDataNetworkType();
1833 } else {
1834 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1835 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001836 }
1837
1838 /**
1839 * Returns the data network type
1840 */
1841 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001842 public int getDataNetworkType(String callingPackage) {
1843 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001844 }
1845
1846 /**
1847 * Returns the data network type for a subId
1848 */
1849 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001850 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1851 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1852 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1853 }
1854
Sanket Padawe356d7632015-06-22 14:03:32 -07001855 final Phone phone = getPhone(subId);
1856 if (phone != null) {
1857 return phone.getServiceState().getDataNetworkType();
1858 } else {
1859 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1860 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001861 }
1862
1863 /**
Wink Saville36469e72014-06-11 15:17:00 -07001864 * Returns the Voice network type for a subId
1865 */
1866 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001867 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1868 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1869 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1870 }
1871
Sanket Padawe356d7632015-06-22 14:03:32 -07001872 final Phone phone = getPhone(subId);
1873 if (phone != null) {
1874 return phone.getServiceState().getVoiceNetworkType();
1875 } else {
1876 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1877 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001878 }
1879
1880 /**
1881 * @return true if a ICC card is present
1882 */
1883 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001884 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001885 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001886 }
1887
1888 /**
1889 * @return true if a ICC card is present for a slotId
1890 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001891 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001892 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001893 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001894 final Phone phone = getPhone(subId[0]);
1895 if (subId != null && phone != null) {
1896 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001897 } else {
1898 return false;
1899 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001900 }
1901
1902 /**
1903 * Return if the current radio is LTE on CDMA. This
1904 * is a tri-state return value as for a period of time
1905 * the mode may be unknown.
1906 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001907 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001908 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001909 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001910 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001911 @Override
1912 public int getLteOnCdmaMode(String callingPackage) {
1913 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001914 }
1915
Sanket Padawe356d7632015-06-22 14:03:32 -07001916 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001917 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1918 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1919 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1920 }
1921
Sanket Padawe356d7632015-06-22 14:03:32 -07001922 final Phone phone = getPhone(subId);
1923 if (phone == null) {
1924 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1925 } else {
1926 return phone.getLteOnCdmaMode();
1927 }
Wink Saville36469e72014-06-11 15:17:00 -07001928 }
1929
1930 public void setPhone(Phone phone) {
1931 mPhone = phone;
1932 }
1933
1934 /**
1935 * {@hide}
1936 * Returns Default subId, 0 in the case of single standby.
1937 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001938 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001939 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001940 }
1941
Wink Savilleb564aae2014-10-23 10:18:09 -07001942 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001943 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001944 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001945
1946 /**
1947 * @see android.telephony.TelephonyManager.WifiCallingChoices
1948 */
1949 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001950 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1951 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001952 }
1953
1954 /**
1955 * @see android.telephony.TelephonyManager.WifiCallingChoices
1956 */
1957 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001958 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1959 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1960 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001961 }
1962
Sailesh Nepald1e68152013-12-12 19:08:02 -08001963 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001964 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001965 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001966 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001967
Shishir Agrawal566b7612013-10-28 14:41:00 -07001968 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001969 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001970 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001971
1972 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001973 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1974 CMD_OPEN_CHANNEL, AID);
1975 if (DBG) log("iccOpenLogicalChannel: " + response);
1976 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001977 }
1978
1979 @Override
1980 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001981 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001982
1983 if (DBG) log("iccCloseLogicalChannel: " + channel);
1984 if (channel < 0) {
1985 return false;
1986 }
Jake Hambye994d462014-02-03 13:10:13 -08001987 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001988 if (DBG) log("iccCloseLogicalChannel: " + success);
1989 return success;
1990 }
1991
1992 @Override
1993 public String iccTransmitApduLogicalChannel(int channel, int cla,
1994 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001995 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001996
1997 if (DBG) {
1998 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
1999 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2000 " data=" + data);
2001 }
2002
2003 if (channel < 0) {
2004 return "";
2005 }
2006
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002007 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002008 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2009 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2010
Shishir Agrawal566b7612013-10-28 14:41:00 -07002011 // Append the returned status code to the end of the response payload.
2012 String s = Integer.toHexString(
2013 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002014 if (response.payload != null) {
2015 s = IccUtils.bytesToHexString(response.payload) + s;
2016 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002017 return s;
2018 }
Jake Hambye994d462014-02-03 13:10:13 -08002019
Evan Charltonc66da362014-05-16 14:06:40 -07002020 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002021 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2022 int p3, String data) {
2023 enforceModifyPermissionOrCarrierPrivilege();
2024
2025 if (DBG) {
2026 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2027 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2028 }
2029
2030 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2031 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2032 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2033
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002034 // Append the returned status code to the end of the response payload.
2035 String s = Integer.toHexString(
2036 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002037 if (response.payload != null) {
2038 s = IccUtils.bytesToHexString(response.payload) + s;
2039 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002040 return s;
2041 }
2042
2043 @Override
2044 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2045 String filePath) {
2046 enforceModifyPermissionOrCarrierPrivilege();
2047
2048 if (DBG) {
2049 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2050 p1 + " " + p2 + " " + p3 + ":" + filePath);
2051 }
2052
2053 IccIoResult response =
2054 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002055 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002056
2057 if (DBG) {
2058 log("Exchange SIM_IO [R]" + response);
2059 }
2060
2061 byte[] result = null;
2062 int length = 2;
2063 if (response.payload != null) {
2064 length = 2 + response.payload.length;
2065 result = new byte[length];
2066 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2067 } else {
2068 result = new byte[length];
2069 }
2070
2071 result[length - 1] = (byte) response.sw2;
2072 result[length - 2] = (byte) response.sw1;
2073 return result;
2074 }
2075
2076 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002077 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002078 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002079
2080 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2081 if (response.payload == null) {
2082 return "";
2083 }
2084
2085 // Append the returned status code to the end of the response payload.
2086 String s = Integer.toHexString(
2087 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2088 s = IccUtils.bytesToHexString(response.payload) + s;
2089 return s;
2090 }
2091
Jake Hambye994d462014-02-03 13:10:13 -08002092 /**
2093 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2094 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2095 *
2096 * @param itemID the ID of the item to read
2097 * @return the NV item as a String, or null on error.
2098 */
2099 @Override
2100 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002101 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002102 if (DBG) log("nvReadItem: item " + itemID);
2103 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2104 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2105 return value;
2106 }
2107
2108 /**
2109 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2110 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2111 *
2112 * @param itemID the ID of the item to read
2113 * @param itemValue the value to write, as a String
2114 * @return true on success; false on any failure
2115 */
2116 @Override
2117 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002118 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002119 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2120 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2121 new Pair<Integer, String>(itemID, itemValue));
2122 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2123 return success;
2124 }
2125
2126 /**
2127 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2128 * Used for device configuration by some CDMA operators.
2129 *
2130 * @param preferredRoamingList byte array containing the new PRL
2131 * @return true on success; false on any failure
2132 */
2133 @Override
2134 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002135 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002136 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2137 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2138 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2139 return success;
2140 }
2141
2142 /**
2143 * Perform the specified type of NV config reset.
2144 * Used for device configuration by some CDMA operators.
2145 *
2146 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2147 * @return true on success; false on any failure
2148 */
2149 @Override
2150 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002151 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002152 if (DBG) log("nvResetConfig: type " + resetType);
2153 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2154 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2155 return success;
2156 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002157
2158 /**
Wink Saville36469e72014-06-11 15:17:00 -07002159 * {@hide}
2160 * Returns Default sim, 0 in the case of single standby.
2161 */
2162 public int getDefaultSim() {
2163 //TODO Need to get it from Telephony Devcontroller
2164 return 0;
2165 }
2166
Svet Ganovb320e182015-04-16 12:30:10 -07002167 public String[] getPcscfAddress(String apnType, String callingPackage) {
2168 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2169 return new String[0];
2170 }
2171
2172
ram87fca6f2014-07-18 18:58:44 +05302173 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002174 }
2175
2176 public void setImsRegistrationState(boolean registered) {
2177 enforceModifyPermission();
2178 mPhone.setImsRegistrationState(registered);
2179 }
2180
2181 /**
Stuart Scott54788802015-03-30 13:18:01 -07002182 * Set the network selection mode to automatic.
2183 *
2184 */
2185 @Override
2186 public void setNetworkSelectionModeAutomatic(int subId) {
2187 enforceModifyPermissionOrCarrierPrivilege();
2188 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2189 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2190 }
2191
2192 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002193 * Set the network selection mode to manual with the selected carrier.
2194 */
2195 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002196 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2197 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002198 enforceModifyPermissionOrCarrierPrivilege();
2199 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002200 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2201 persistSelection);
2202 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002203 }
2204
2205 /**
2206 * Scans for available networks.
2207 */
2208 @Override
2209 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2210 enforceModifyPermissionOrCarrierPrivilege();
2211 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2212 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2213 CMD_PERFORM_NETWORK_SCAN, null, subId);
2214 return result;
2215 }
2216
2217 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002218 * Get the calculated preferred network type.
2219 * Used for debugging incorrect network type.
2220 *
2221 * @return the preferred network type, defined in RILConstants.java.
2222 */
2223 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002224 public int getCalculatedPreferredNetworkType(String callingPackage) {
2225 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2226 return RILConstants.PREFERRED_NETWORK_MODE;
2227 }
2228
Amit Mahajan43330e02014-11-18 11:54:45 -08002229 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002230 }
2231
2232 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002233 * Get the preferred network type.
2234 * Used for device configuration by some CDMA operators.
2235 *
2236 * @return the preferred network type, defined in RILConstants.java.
2237 */
2238 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002239 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002240 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002241 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002242 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002243 int networkType = (result != null ? result[0] : -1);
2244 if (DBG) log("getPreferredNetworkType: " + networkType);
2245 return networkType;
2246 }
2247
2248 /**
2249 * Set the preferred network type.
2250 * Used for device configuration by some CDMA operators.
2251 *
2252 * @param networkType the preferred network type, defined in RILConstants.java.
2253 * @return true on success; false on any failure.
2254 */
2255 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002256 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002257 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002258 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2259 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002260 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002261 if (success) {
2262 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002263 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002264 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002265 return success;
2266 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002267
2268 /**
Junda Liu475951f2014-11-07 16:45:03 -08002269 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2270 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2271 * tethering.
2272 *
2273 * @return 0: Not required. 1: required. 2: Not set.
2274 * @hide
2275 */
2276 @Override
2277 public int getTetherApnRequired() {
2278 enforceModifyPermissionOrCarrierPrivilege();
2279 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2280 Settings.Global.TETHER_DUN_REQUIRED, 2);
2281 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2282 // config_tether_apndata.
2283 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2284 dunRequired = 1;
2285 }
2286 return dunRequired;
2287 }
2288
2289 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002290 * Set mobile data enabled
2291 * Used by the user through settings etc to turn on/off mobile data
2292 *
2293 * @param enable {@code true} turn turn data on, else {@code false}
2294 */
2295 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002296 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002297 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002298 int phoneId = mSubscriptionController.getPhoneId(subId);
2299 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2300 Phone phone = PhoneFactory.getPhone(phoneId);
2301 if (phone != null) {
2302 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2303 phone.setDataEnabled(enable);
2304 } else {
2305 loge("setDataEnabled: no phone for subId=" + subId);
2306 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002307 }
2308
2309 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002310 * Get whether mobile data is enabled.
2311 *
2312 * Note that this used to be available from ConnectivityService, gated by
2313 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2314 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002315 *
2316 * @return {@code true} if data is enabled else {@code false}
2317 */
2318 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002319 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002320 try {
2321 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2322 null);
2323 } catch (Exception e) {
2324 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2325 null);
2326 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002327 int phoneId = mSubscriptionController.getPhoneId(subId);
2328 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2329 Phone phone = PhoneFactory.getPhone(phoneId);
2330 if (phone != null) {
2331 boolean retVal = phone.getDataEnabled();
2332 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2333 return retVal;
2334 } else {
2335 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2336 return false;
2337 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002338 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002339
2340 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002341 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002342 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002343 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002344 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002345 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2346 }
2347 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002348 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002349 }
Junda Liu29340342014-07-10 15:23:27 -07002350
2351 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002352 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002353 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002354 if (card == null) {
2355 loge("checkCarrierPrivilegesForPackage: No UICC");
2356 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2357 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002358 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2359 }
2360
2361 @Override
2362 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2363 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2364 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2365 UiccCard card = UiccController.getInstance().getUiccCard(i);
2366 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002367 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002368 continue;
2369 }
2370
2371 result = card.getCarrierPrivilegeStatus(
2372 mPhone.getContext().getPackageManager(), pkgName);
2373 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2374 break;
2375 }
2376 }
2377
2378 return result;
Junda Liu29340342014-07-10 15:23:27 -07002379 }
Derek Tan89e89d42014-07-08 17:00:10 -07002380
2381 @Override
Junda Liue64de782015-04-16 17:19:16 -07002382 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2383 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2384 loge("phoneId " + phoneId + " is not valid.");
2385 return null;
2386 }
2387 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002388 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002389 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002390 return null ;
2391 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002392 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002393 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002394 }
2395
Wink Savilleb564aae2014-10-23 10:18:09 -07002396 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002397 final Phone phone = getPhone(subId);
2398 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002399 if (card == null) {
2400 loge("getIccId: No UICC");
2401 return null;
2402 }
2403 String iccId = card.getIccId();
2404 if (TextUtils.isEmpty(iccId)) {
2405 loge("getIccId: ICC ID is null or empty.");
2406 return null;
2407 }
2408 return iccId;
2409 }
2410
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002411 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002412 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2413 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002414 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002415
Jeff Sharkey85190e62014-12-05 09:40:12 -08002416 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002417 final Phone phone = getPhone(subId);
2418 if (phone == null) {
2419 return false;
2420 }
2421 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002422
2423 if (DBG_MERGE) {
2424 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2425 + subscriberId + " to " + number);
2426 }
2427
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002428 if (TextUtils.isEmpty(iccId)) {
2429 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002430 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002431
Jeff Sharkey85190e62014-12-05 09:40:12 -08002432 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2433
2434 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002435 if (alphaTag == null) {
2436 editor.remove(alphaTagPrefKey);
2437 } else {
2438 editor.putString(alphaTagPrefKey, alphaTag);
2439 }
2440
Jeff Sharkey85190e62014-12-05 09:40:12 -08002441 // Record both the line number and IMSI for this ICCID, since we need to
2442 // track all merged IMSIs based on line number
2443 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2444 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002445 if (number == null) {
2446 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002447 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002448 } else {
2449 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002450 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002451 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002452
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002453 editor.commit();
2454 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002455 }
2456
2457 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002458 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002459 // This is open to apps with WRITE_SMS.
2460 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002461 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002462 return null;
2463 }
Derek Tan97ebb422014-09-05 16:55:38 -07002464
2465 String iccId = getIccId(subId);
2466 if (iccId != null) {
2467 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002468 if (DBG_MERGE) {
2469 log("getLine1NumberForDisplay returning " +
2470 mTelephonySharedPreferences.getString(numberPrefKey, null));
2471 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002472 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002473 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002474 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002475 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002476 }
2477
2478 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002479 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2480 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2481 return null;
2482 }
Derek Tan97ebb422014-09-05 16:55:38 -07002483
2484 String iccId = getIccId(subId);
2485 if (iccId != null) {
2486 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002487 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002488 }
Derek Tan97ebb422014-09-05 16:55:38 -07002489 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002490 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002491
2492 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002493 public String[] getMergedSubscriberIds(String callingPackage) {
2494 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2495 return null;
2496 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002497 final Context context = mPhone.getContext();
2498 final TelephonyManager tele = TelephonyManager.from(context);
2499 final SubscriptionManager sub = SubscriptionManager.from(context);
2500
2501 // Figure out what subscribers are currently active
2502 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002503 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2504 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2505 final long identity = Binder.clearCallingIdentity();
2506 try {
2507 final int[] subIds = sub.getActiveSubscriptionIdList();
2508 for (int subId : subIds) {
2509 activeSubscriberIds.add(tele.getSubscriberId(subId));
2510 }
2511 } finally {
2512 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002513 }
2514
2515 // First pass, find a number override for an active subscriber
2516 String mergeNumber = null;
2517 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2518 for (String key : prefs.keySet()) {
2519 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2520 final String subscriberId = (String) prefs.get(key);
2521 if (activeSubscriberIds.contains(subscriberId)) {
2522 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2523 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2524 mergeNumber = (String) prefs.get(numberKey);
2525 if (DBG_MERGE) {
2526 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2527 + " for active subscriber " + subscriberId);
2528 }
2529 if (!TextUtils.isEmpty(mergeNumber)) {
2530 break;
2531 }
2532 }
2533 }
2534 }
2535
2536 // Shortcut when no active merged subscribers
2537 if (TextUtils.isEmpty(mergeNumber)) {
2538 return null;
2539 }
2540
2541 // Second pass, find all subscribers under that line override
2542 final ArraySet<String> result = new ArraySet<>();
2543 for (String key : prefs.keySet()) {
2544 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2545 final String number = (String) prefs.get(key);
2546 if (mergeNumber.equals(number)) {
2547 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2548 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2549 final String subscriberId = (String) prefs.get(subscriberKey);
2550 if (!TextUtils.isEmpty(subscriberId)) {
2551 result.add(subscriberId);
2552 }
2553 }
2554 }
2555 }
2556
2557 final String[] resultArray = result.toArray(new String[result.size()]);
2558 Arrays.sort(resultArray);
2559 if (DBG_MERGE) {
2560 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2561 }
2562 return resultArray;
2563 }
2564
2565 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002566 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002567 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002568 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002569 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002570
2571 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002572 public boolean setRoamingOverride(List<String> gsmRoamingList,
2573 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2574 List<String> cdmaNonRoamingList) {
2575 enforceCarrierPrivilege();
2576 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2577 cdmaNonRoamingList);
2578 }
2579
2580 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002581 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2582 enforceModifyPermission();
2583
2584 int returnValue = 0;
2585 try {
2586 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2587 if(result.exception == null) {
2588 if (result.result != null) {
2589 byte[] responseData = (byte[])(result.result);
2590 if(responseData.length > oemResp.length) {
2591 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2592 responseData.length + "bytes. Buffer Size is " +
2593 oemResp.length + "bytes.");
2594 }
2595 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2596 returnValue = responseData.length;
2597 }
2598 } else {
2599 CommandException ex = (CommandException) result.exception;
2600 returnValue = ex.getCommandError().ordinal();
2601 if(returnValue > 0) returnValue *= -1;
2602 }
2603 } catch (RuntimeException e) {
2604 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2605 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2606 if(returnValue > 0) returnValue *= -1;
2607 }
2608
2609 return returnValue;
2610 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002611
2612 @Override
2613 public void setRadioCapability(RadioAccessFamily[] rafs) {
2614 try {
2615 ProxyController.getInstance().setRadioCapability(rafs);
2616 } catch (RuntimeException e) {
2617 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2618 }
2619 }
2620
2621 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002622 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2623 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2624 return RadioAccessFamily.RAF_UNKNOWN;
2625 }
2626
Wink Saville5d475dd2014-10-17 15:00:58 -07002627 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2628 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002629
2630 @Override
2631 public void enableVideoCalling(boolean enable) {
2632 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002633 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002634 }
2635
2636 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002637 public boolean isVideoCallingEnabled(String callingPackage) {
2638 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2639 return false;
2640 }
2641
Andrew Lee77527ac2014-10-21 16:57:39 -07002642 // Check the user preference and the system-level IMS setting. Even if the user has
2643 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2644 // In the long run, we may instead need to check if there exists a connection service
2645 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002646 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2647 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002648 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002649 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002650
Andrew Leea1239f22015-03-02 17:44:07 -08002651 @Override
2652 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002653 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002654 }
2655
2656 @Override
2657 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002658 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002659 }
2660
Andrew Lee9431b832015-03-09 18:46:45 -07002661 @Override
2662 public boolean isTtyModeSupported() {
2663 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2664 TelephonyManager telephonyManager =
2665 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2666 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2667 }
2668
2669 @Override
2670 public boolean isHearingAidCompatibilitySupported() {
2671 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2672 }
2673
Sanket Padawe7310cc72015-01-14 09:53:20 -08002674 /**
2675 * Returns the unique device ID of phone, for example, the IMEI for
2676 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2677 *
2678 * <p>Requires Permission:
2679 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2680 */
2681 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002682 public String getDeviceId(String callingPackage) {
2683 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2684 return null;
2685 }
2686
Sanket Padawe7310cc72015-01-14 09:53:20 -08002687 final Phone phone = PhoneFactory.getPhone(0);
2688 if (phone != null) {
2689 return phone.getDeviceId();
2690 } else {
2691 return null;
2692 }
2693 }
2694
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002695 /*
2696 * {@hide}
2697 * Returns the IMS Registration Status
2698 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002699 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002700 public boolean isImsRegistered() {
2701 return mPhone.isImsRegistered();
2702 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002703
2704 @Override
2705 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2706 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2707 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002708
Nathan Haroldc55097a2015-03-11 18:14:50 -07002709 /*
2710 * {@hide}
2711 * Returns the IMS Registration Status
2712 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002713 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002714 return mPhone.isWifiCallingEnabled();
2715 }
2716
2717 /*
2718 * {@hide}
2719 * Returns the IMS Registration Status
2720 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002721 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002722 return mPhone.isVolteEnabled();
2723 }
Svet Ganovb320e182015-04-16 12:30:10 -07002724
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002725 /*
2726 * {@hide} Returns the IMS Registration Status
2727 */
2728 public boolean isVideoTelephonyAvailable() {
2729 return mPhone.isVideoEnabled();
2730 }
2731
Svet Ganovb320e182015-04-16 12:30:10 -07002732 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002733 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002734 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002735 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2736
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002737 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002738 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002739 } catch (SecurityException e) {
2740 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2741 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002742 }
Svet Ganovb320e182015-04-16 12:30:10 -07002743
2744 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2745 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2746 return false;
2747 }
2748
2749 return true;
2750 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002751
Makoto Onukifee69342015-06-29 14:44:50 -07002752 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002753 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002754 */
2755 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002756 // Default SMS app can always read it.
2757 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2758 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2759 return true;
2760 }
2761 try {
2762 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002763 } catch (SecurityException readPhoneStateSecurityException) {
2764 try {
2765 // Can be read with READ_SMS too.
2766 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2767 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2768 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2769 } catch (SecurityException readSmsSecurityException) {
2770 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2771 // permissions
2772 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2773 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2774 " or " + android.Manifest.permission.READ_SMS + ".");
2775 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002776 }
Makoto Onukifee69342015-06-29 14:44:50 -07002777 }
2778
Stuart Scott8eef64f2015-04-08 15:13:54 -07002779 @Override
2780 public void factoryReset(int subId) {
2781 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002782 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2783 return;
2784 }
2785
Svet Ganovcc087f82015-05-12 20:35:54 -07002786 final long identity = Binder.clearCallingIdentity();
2787 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002788 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2789 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002790 // Enable data
2791 setDataEnabled(subId, true);
2792 // Set network selection mode to automatic
2793 setNetworkSelectionModeAutomatic(subId);
2794 // Set preferred mobile network type to the best available
2795 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2796 // Turn off roaming
2797 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2798 }
2799 } finally {
2800 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002801 }
2802 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002803
2804 @Override
2805 public String getLocaleFromDefaultSim() {
2806 // We query all subscriptions instead of just the active ones, because
2807 // this might be called early on in the provisioning flow when the
2808 // subscriptions potentially aren't active yet.
2809 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2810 if (slist == null || slist.isEmpty()) {
2811 return null;
2812 }
2813
2814 // This function may be called very early, say, from the setup wizard, at
2815 // which point we won't have a default subscription set. If that's the case
2816 // we just choose the first, which will be valid in "most cases".
2817 final int defaultSubId = getDefaultSubscription();
2818 SubscriptionInfo info = null;
2819 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2820 info = slist.get(0);
2821 } else {
2822 for (SubscriptionInfo item : slist) {
2823 if (item.getSubscriptionId() == defaultSubId) {
2824 info = item;
2825 break;
2826 }
2827 }
2828
2829 if (info == null) {
2830 return null;
2831 }
2832 }
2833
2834 // Try and fetch the locale from the carrier properties or from the SIM language
2835 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002836 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002837 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002838 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002839 if (defaultPhone != null) {
2840 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2841 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002842 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002843 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2844 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002845 } else {
2846 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002847 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002848 }
2849 }
2850
Narayan Kamath011676f2015-07-29 12:04:08 +01002851 // The SIM language preferences only store a language (e.g. fr = French), not an
2852 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2853 // the SIM and carrier preferences does not include a country we add the country
2854 // determined from the SIM MCC to provide an exact locale.
2855 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002856 if (mccLocale != null) {
2857 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2858 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002859 }
2860
Tony Hill183b2de2015-06-24 14:53:58 +01002861 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002862 return null;
2863 }
2864
2865 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2866 final long identity = Binder.clearCallingIdentity();
2867 try {
2868 return mSubscriptionController.getAllSubInfoList(
2869 mPhone.getContext().getOpPackageName());
2870 } finally {
2871 Binder.restoreCallingIdentity(identity);
2872 }
2873 }
2874
2875 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2876 final long identity = Binder.clearCallingIdentity();
2877 try {
2878 return mSubscriptionController.getActiveSubscriptionInfoList(
2879 mPhone.getContext().getOpPackageName());
2880 } finally {
2881 Binder.restoreCallingIdentity(identity);
2882 }
2883 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002884
2885 /**
2886 * {@hide}
2887 * Returns the modem stats
2888 */
2889 @Override
2890 public ModemActivityInfo getModemActivityInfo() {
2891 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2892 }
Jack Yu85bd38a2015-11-09 11:34:32 -08002893
2894 /**
2895 * {@hide}
2896 * Returns the service state information on specified subscription.
2897 */
2898 @Override
2899 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2900
2901 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2902 return null;
2903 }
2904
2905 final Phone phone = getPhone(subId);
2906 if (phone == null) {
2907 return null;
2908 }
2909
2910 return phone.getServiceState();
2911 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08002912
2913 /**
2914 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
2915 *
2916 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2917 * voicemail ringtone.
2918 * @return The URI for the ringtone to play when receiving a voicemail from a specific
2919 * PhoneAccount.
2920 */
2921 @Override
2922 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
2923 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2924 if (phone == null) {
2925 return null;
2926 }
2927
2928 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
2929 }
2930
2931 /**
2932 * Returns whether vibration is set for voicemail notification in Phone settings.
2933 *
2934 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2935 * voicemail vibration setting.
2936 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
2937 */
2938 @Override
2939 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
2940 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2941 if (phone == null) {
2942 return false;
2943 }
2944
2945 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
2946 }
2947
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002948}