blob: 43d937967dcfd91ba80ac2b83601ec0d01f4d3be [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;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800198 // The subscriber id that this request applies to. Defaults to
199 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
200 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700201
202 public MainThreadRequest(Object argument) {
203 this.argument = argument;
204 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800205
206 public MainThreadRequest(Object argument, Integer subId) {
207 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800208 if (subId != null) {
209 this.subId = subId;
210 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800211 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700212 }
213
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800214 private static final class IncomingThirdPartyCallArgs {
215 public final ComponentName component;
216 public final String callId;
217 public final String callerDisplayName;
218
219 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
220 String callerDisplayName) {
221 this.component = component;
222 this.callId = callId;
223 this.callerDisplayName = callerDisplayName;
224 }
225 }
226
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700227 /**
228 * A handler that processes messages on the main thread in the phone process. Since many
229 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
230 * inbound binder threads to the main thread in the phone process. The Binder thread
231 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
232 * on, which will be notified when the operation completes and will contain the result of the
233 * request.
234 *
235 * <p>If a MainThreadRequest object is provided in the msg.obj field,
236 * note that request.result must be set to something non-null for the calling thread to
237 * unblock.
238 */
239 private final class MainThreadHandler extends Handler {
240 @Override
241 public void handleMessage(Message msg) {
242 MainThreadRequest request;
243 Message onCompleted;
244 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800245 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700246 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700247
248 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700249 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700250 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700251 final Phone phone = getPhoneFromRequest(request);
252 request.result = phone != null ?
253 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
254 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 // Wake up the requesting thread
256 synchronized (request) {
257 request.notifyAll();
258 }
259 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700261
262 case CMD_HANDLE_NEIGHBORING_CELL:
263 request = (MainThreadRequest) msg.obj;
264 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
265 request);
266 mPhone.getNeighboringCids(onCompleted);
267 break;
268
269 case EVENT_NEIGHBORING_CELL_DONE:
270 ar = (AsyncResult) msg.obj;
271 request = (MainThreadRequest) ar.userObj;
272 if (ar.exception == null && ar.result != null) {
273 request.result = ar.result;
274 } else {
275 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800276 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700277 }
278 // Wake up the requesting thread
279 synchronized (request) {
280 request.notifyAll();
281 }
282 break;
283
284 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700285 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800286 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700287 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700288 break;
289
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700290 case CMD_END_CALL:
291 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800292 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700293 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700294 Phone phone = getPhone(end_subId);
295 if (phone == null) {
296 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
297 break;
298 }
299 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700300 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
301 // CDMA: If the user presses the Power button we treat it as
302 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700303 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700304 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
305 // GSM: End the call as per the Phone state
306 hungUp = PhoneUtils.hangup(mCM);
307 } else {
308 throw new IllegalStateException("Unexpected phone type: " + phoneType);
309 }
310 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
311 request.result = hungUp;
312 // Wake up the requesting thread
313 synchronized (request) {
314 request.notifyAll();
315 }
316 break;
317
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700318 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700319 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700320 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700321 if (uiccCard == null) {
322 loge("iccTransmitApduLogicalChannel: No UICC");
323 request.result = new IccIoResult(0x6F, 0, (byte[])null);
324 synchronized (request) {
325 request.notifyAll();
326 }
327 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700328 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
329 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700330 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700331 iccArgument.channel, iccArgument.cla, iccArgument.command,
332 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700333 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700334 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700335 break;
336
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700337 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700338 ar = (AsyncResult) msg.obj;
339 request = (MainThreadRequest) ar.userObj;
340 if (ar.exception == null && ar.result != null) {
341 request.result = ar.result;
342 } else {
343 request.result = new IccIoResult(0x6F, 0, (byte[])null);
344 if (ar.result == null) {
345 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800346 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700347 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800348 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700349 } else {
350 loge("iccTransmitApduLogicalChannel: Unknown exception");
351 }
352 }
353 synchronized (request) {
354 request.notifyAll();
355 }
356 break;
357
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700358 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
359 request = (MainThreadRequest) msg.obj;
360 iccArgument = (IccAPDUArgument) request.argument;
361 if (uiccCard == null) {
362 loge("iccTransmitApduBasicChannel: No UICC");
363 request.result = new IccIoResult(0x6F, 0, (byte[])null);
364 synchronized (request) {
365 request.notifyAll();
366 }
367 } else {
368 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
369 request);
370 uiccCard.iccTransmitApduBasicChannel(
371 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
372 iccArgument.p3, iccArgument.data, onCompleted);
373 }
374 break;
375
376 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
377 ar = (AsyncResult) msg.obj;
378 request = (MainThreadRequest) ar.userObj;
379 if (ar.exception == null && ar.result != null) {
380 request.result = ar.result;
381 } else {
382 request.result = new IccIoResult(0x6F, 0, (byte[])null);
383 if (ar.result == null) {
384 loge("iccTransmitApduBasicChannel: Empty response");
385 } else if (ar.exception instanceof CommandException) {
386 loge("iccTransmitApduBasicChannel: CommandException: " +
387 ar.exception);
388 } else {
389 loge("iccTransmitApduBasicChannel: Unknown exception");
390 }
391 }
392 synchronized (request) {
393 request.notifyAll();
394 }
395 break;
396
397 case CMD_EXCHANGE_SIM_IO:
398 request = (MainThreadRequest) msg.obj;
399 iccArgument = (IccAPDUArgument) request.argument;
400 if (uiccCard == null) {
401 loge("iccExchangeSimIO: No UICC");
402 request.result = new IccIoResult(0x6F, 0, (byte[])null);
403 synchronized (request) {
404 request.notifyAll();
405 }
406 } else {
407 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
408 request);
409 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
410 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
411 iccArgument.data, onCompleted);
412 }
413 break;
414
415 case EVENT_EXCHANGE_SIM_IO_DONE:
416 ar = (AsyncResult) msg.obj;
417 request = (MainThreadRequest) ar.userObj;
418 if (ar.exception == null && ar.result != null) {
419 request.result = ar.result;
420 } else {
421 request.result = new IccIoResult(0x6f, 0, (byte[])null);
422 }
423 synchronized (request) {
424 request.notifyAll();
425 }
426 break;
427
Derek Tan4d5e5c12014-02-04 11:54:58 -0800428 case CMD_SEND_ENVELOPE:
429 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700430 if (uiccCard == null) {
431 loge("sendEnvelopeWithStatus: No UICC");
432 request.result = new IccIoResult(0x6F, 0, (byte[])null);
433 synchronized (request) {
434 request.notifyAll();
435 }
436 } else {
437 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
438 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
439 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800440 break;
441
442 case EVENT_SEND_ENVELOPE_DONE:
443 ar = (AsyncResult) msg.obj;
444 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700445 if (ar.exception == null && ar.result != null) {
446 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800447 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700448 request.result = new IccIoResult(0x6F, 0, (byte[])null);
449 if (ar.result == null) {
450 loge("sendEnvelopeWithStatus: Empty response");
451 } else if (ar.exception instanceof CommandException) {
452 loge("sendEnvelopeWithStatus: CommandException: " +
453 ar.exception);
454 } else {
455 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
456 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800457 }
458 synchronized (request) {
459 request.notifyAll();
460 }
461 break;
462
Shishir Agrawal566b7612013-10-28 14:41:00 -0700463 case CMD_OPEN_CHANNEL:
464 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700465 if (uiccCard == null) {
466 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800467 request.result = new IccOpenLogicalChannelResponse(-1,
468 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700469 synchronized (request) {
470 request.notifyAll();
471 }
472 } else {
473 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
474 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
475 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700476 break;
477
478 case EVENT_OPEN_CHANNEL_DONE:
479 ar = (AsyncResult) msg.obj;
480 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700481 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700482 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700483 int[] result = (int[]) ar.result;
484 int channelId = result[0];
485 byte[] selectResponse = null;
486 if (result.length > 1) {
487 selectResponse = new byte[result.length - 1];
488 for (int i = 1; i < result.length; ++i) {
489 selectResponse[i - 1] = (byte) result[i];
490 }
491 }
492 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700493 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700494 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 if (ar.result == null) {
496 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700497 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700498 if (ar.exception != null) {
499 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
500 }
501
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700502 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700503 if (ar.exception instanceof CommandException) {
504 CommandException.Error error =
505 ((CommandException) (ar.exception)).getCommandError();
506 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700507 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700508 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700509 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700510 }
511 }
512 openChannelResp = new IccOpenLogicalChannelResponse(
513 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700515 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700516 synchronized (request) {
517 request.notifyAll();
518 }
519 break;
520
521 case CMD_CLOSE_CHANNEL:
522 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700523 if (uiccCard == null) {
524 loge("iccCloseLogicalChannel: No UICC");
525 request.result = new IccIoResult(0x6F, 0, (byte[])null);
526 synchronized (request) {
527 request.notifyAll();
528 }
529 } else {
530 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
531 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
532 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700533 break;
534
535 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800536 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
537 break;
538
539 case CMD_NV_READ_ITEM:
540 request = (MainThreadRequest) msg.obj;
541 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
542 mPhone.nvReadItem((Integer) request.argument, onCompleted);
543 break;
544
545 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700546 ar = (AsyncResult) msg.obj;
547 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800548 if (ar.exception == null && ar.result != null) {
549 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700550 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800551 request.result = "";
552 if (ar.result == null) {
553 loge("nvReadItem: Empty response");
554 } else if (ar.exception instanceof CommandException) {
555 loge("nvReadItem: CommandException: " +
556 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800558 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700559 }
560 }
561 synchronized (request) {
562 request.notifyAll();
563 }
564 break;
565
Jake Hambye994d462014-02-03 13:10:13 -0800566 case CMD_NV_WRITE_ITEM:
567 request = (MainThreadRequest) msg.obj;
568 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
569 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
570 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
571 break;
572
573 case EVENT_NV_WRITE_ITEM_DONE:
574 handleNullReturnEvent(msg, "nvWriteItem");
575 break;
576
577 case CMD_NV_WRITE_CDMA_PRL:
578 request = (MainThreadRequest) msg.obj;
579 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
580 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
581 break;
582
583 case EVENT_NV_WRITE_CDMA_PRL_DONE:
584 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
585 break;
586
587 case CMD_NV_RESET_CONFIG:
588 request = (MainThreadRequest) msg.obj;
589 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
590 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
591 break;
592
593 case EVENT_NV_RESET_CONFIG_DONE:
594 handleNullReturnEvent(msg, "nvResetConfig");
595 break;
596
Jake Hamby7c27be32014-03-03 13:25:59 -0800597 case CMD_GET_PREFERRED_NETWORK_TYPE:
598 request = (MainThreadRequest) msg.obj;
599 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700600 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800601 break;
602
603 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
604 ar = (AsyncResult) msg.obj;
605 request = (MainThreadRequest) ar.userObj;
606 if (ar.exception == null && ar.result != null) {
607 request.result = ar.result; // Integer
608 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800609 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800610 if (ar.result == null) {
611 loge("getPreferredNetworkType: Empty response");
612 } else if (ar.exception instanceof CommandException) {
613 loge("getPreferredNetworkType: CommandException: " +
614 ar.exception);
615 } else {
616 loge("getPreferredNetworkType: Unknown exception");
617 }
618 }
619 synchronized (request) {
620 request.notifyAll();
621 }
622 break;
623
624 case CMD_SET_PREFERRED_NETWORK_TYPE:
625 request = (MainThreadRequest) msg.obj;
626 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
627 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700628 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800629 break;
630
631 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
632 handleNullReturnEvent(msg, "setPreferredNetworkType");
633 break;
634
Steven Liu4bf01bc2014-07-17 11:05:29 -0500635 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
636 request = (MainThreadRequest)msg.obj;
637 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
638 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
639 break;
640
641 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
642 ar = (AsyncResult)msg.obj;
643 request = (MainThreadRequest)ar.userObj;
644 request.result = ar;
645 synchronized (request) {
646 request.notifyAll();
647 }
648 break;
649
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800650 case CMD_SET_VOICEMAIL_NUMBER:
651 request = (MainThreadRequest) msg.obj;
652 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
653 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800654 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
655 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800656 break;
657
658 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
659 handleNullReturnEvent(msg, "setVoicemailNumber");
660 break;
661
Stuart Scott54788802015-03-30 13:18:01 -0700662 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
663 request = (MainThreadRequest) msg.obj;
664 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
665 request);
666 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
667 break;
668
669 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
670 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
671 break;
672
Shishir Agrawal302c8692015-06-19 13:49:39 -0700673 case CMD_PERFORM_NETWORK_SCAN:
674 request = (MainThreadRequest) msg.obj;
675 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
676 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
677 break;
678
679 case EVENT_PERFORM_NETWORK_SCAN_DONE:
680 ar = (AsyncResult) msg.obj;
681 request = (MainThreadRequest) ar.userObj;
682 CellNetworkScanResult cellScanResult;
683 if (ar.exception == null && ar.result != null) {
684 cellScanResult = new CellNetworkScanResult(
685 CellNetworkScanResult.STATUS_SUCCESS,
686 (List<OperatorInfo>) ar.result);
687 } else {
688 if (ar.result == null) {
689 loge("getCellNetworkScanResults: Empty response");
690 }
691 if (ar.exception != null) {
692 loge("getCellNetworkScanResults: Exception: " + ar.exception);
693 }
694 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
695 if (ar.exception instanceof CommandException) {
696 CommandException.Error error =
697 ((CommandException) (ar.exception)).getCommandError();
698 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
699 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
700 } else if (error == CommandException.Error.GENERIC_FAILURE) {
701 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
702 }
703 }
704 cellScanResult = new CellNetworkScanResult(errorCode, null);
705 }
706 request.result = cellScanResult;
707 synchronized (request) {
708 request.notifyAll();
709 }
710 break;
711
712 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
713 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700714 ManualNetworkSelectionArgument selArg =
715 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700716 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
717 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700718 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
719 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700720 break;
721
722 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
723 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
724 break;
725
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700726 case CMD_GET_MODEM_ACTIVITY_INFO:
727 request = (MainThreadRequest) msg.obj;
728 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700729 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700730 break;
731
732 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
733 ar = (AsyncResult) msg.obj;
734 request = (MainThreadRequest) ar.userObj;
735 if (ar.exception == null && ar.result != null) {
736 request.result = ar.result;
737 } else {
738 if (ar.result == null) {
739 loge("queryModemActivityInfo: Empty response");
740 } else if (ar.exception instanceof CommandException) {
741 loge("queryModemActivityInfo: CommandException: " +
742 ar.exception);
743 } else {
744 loge("queryModemActivityInfo: Unknown exception");
745 }
746 }
Amit Mahajand4766222016-01-28 15:28:28 -0800747 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
748 if (request.result == null) {
749 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
750 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700751 synchronized (request) {
752 request.notifyAll();
753 }
754 break;
755
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700756 default:
757 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
758 break;
759 }
760 }
Jake Hambye994d462014-02-03 13:10:13 -0800761
762 private void handleNullReturnEvent(Message msg, String command) {
763 AsyncResult ar = (AsyncResult) msg.obj;
764 MainThreadRequest request = (MainThreadRequest) ar.userObj;
765 if (ar.exception == null) {
766 request.result = true;
767 } else {
768 request.result = false;
769 if (ar.exception instanceof CommandException) {
770 loge(command + ": CommandException: " + ar.exception);
771 } else {
772 loge(command + ": Unknown exception");
773 }
774 }
775 synchronized (request) {
776 request.notifyAll();
777 }
778 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700779 }
780
781 /**
782 * Posts the specified command to be executed on the main thread,
783 * waits for the request to complete, and returns the result.
784 * @see #sendRequestAsync
785 */
786 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800787 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700788 }
789
790 /**
791 * Posts the specified command to be executed on the main thread,
792 * waits for the request to complete, and returns the result.
793 * @see #sendRequestAsync
794 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800795 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700796 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
797 throw new RuntimeException("This method will deadlock if called from the main thread.");
798 }
799
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800800 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700801 Message msg = mMainThreadHandler.obtainMessage(command, request);
802 msg.sendToTarget();
803
804 // Wait for the request to complete
805 synchronized (request) {
806 while (request.result == null) {
807 try {
808 request.wait();
809 } catch (InterruptedException e) {
810 // Do nothing, go back and wait until the request is complete
811 }
812 }
813 }
814 return request.result;
815 }
816
817 /**
818 * Asynchronous ("fire and forget") version of sendRequest():
819 * Posts the specified command to be executed on the main thread, and
820 * returns immediately.
821 * @see #sendRequest
822 */
823 private void sendRequestAsync(int command) {
824 mMainThreadHandler.sendEmptyMessage(command);
825 }
826
827 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700828 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
829 * @see {@link #sendRequest(int,Object)}
830 */
831 private void sendRequestAsync(int command, Object argument) {
832 MainThreadRequest request = new MainThreadRequest(argument);
833 Message msg = mMainThreadHandler.obtainMessage(command, request);
834 msg.sendToTarget();
835 }
836
837 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700838 * Initialize the singleton PhoneInterfaceManager instance.
839 * This is only done once, at startup, from PhoneApp.onCreate().
840 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700841 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700842 synchronized (PhoneInterfaceManager.class) {
843 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700844 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700845 } else {
846 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
847 }
848 return sInstance;
849 }
850 }
851
852 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700853 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700854 mApp = app;
855 mPhone = phone;
856 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700857 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
859 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700860 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700861 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800862 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800863
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700864 publish();
865 }
866
867 private void publish() {
868 if (DBG) log("publish: " + this);
869
870 ServiceManager.addService("phone", this);
871 }
872
Stuart Scott584921c2015-01-15 17:10:34 -0800873 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800874 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
875 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800876 }
877
Wink Saville36469e72014-06-11 15:17:00 -0700878 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700879 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800880 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700881 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700882 //
883 // Implementation of the ITelephony interface.
884 //
885
886 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700887 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700888 }
889
Wink Savilleb564aae2014-10-23 10:18:09 -0700890 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700891 if (DBG) log("dial: " + number);
892 // No permission check needed here: This is just a wrapper around the
893 // ACTION_DIAL intent, which is available to any app since it puts up
894 // the UI before it does anything.
895
896 String url = createTelUrl(number);
897 if (url == null) {
898 return;
899 }
900
901 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700902 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700903 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
904 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
905 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
906 mApp.startActivity(intent);
907 }
908 }
909
910 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700911 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700912 }
913
Wink Savilleb564aae2014-10-23 10:18:09 -0700914 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700915 if (DBG) log("call: " + number);
916
917 // This is just a wrapper around the ACTION_CALL intent, but we still
918 // need to do a permission check since we're calling startActivity()
919 // from the context of the phone app.
920 enforceCallPermission();
921
922 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
923 != AppOpsManager.MODE_ALLOWED) {
924 return;
925 }
926
927 String url = createTelUrl(number);
928 if (url == null) {
929 return;
930 }
931
Wink Saville08874612014-08-31 19:19:58 -0700932 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100933 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800934 if (slist != null) {
935 for (SubscriptionInfo subInfoRecord : slist) {
936 if (subInfoRecord.getSubscriptionId() == subId) {
937 isValid = true;
938 break;
939 }
Wink Saville08874612014-08-31 19:19:58 -0700940 }
941 }
942 if (isValid == false) {
943 return;
944 }
945
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700946 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700947 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700948 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
949 mApp.startActivity(intent);
950 }
951
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700952 /**
953 * End a call based on call state
954 * @return true is a call was ended
955 */
956 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700957 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700958 }
959
960 /**
961 * End a call based on the call state of the subId
962 * @return true is a call was ended
963 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700964 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800966 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 }
968
969 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700970 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700971 }
972
Wink Savilleb564aae2014-10-23 10:18:09 -0700973 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700974 if (DBG) log("answerRingingCall...");
975 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
976 // but that can probably wait till the big TelephonyManager API overhaul.
977 // For now, protect this call with the MODIFY_PHONE_STATE permission.
978 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800979 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700980 }
981
982 /**
983 * Make the actual telephony calls to implement answerRingingCall().
984 * This should only be called from the main thread of the Phone app.
985 * @see #answerRingingCall
986 *
987 * TODO: it would be nice to return true if we answered the call, or
988 * false if there wasn't actually a ringing incoming call, or some
989 * other error occurred. (In other words, pass back the return value
990 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
991 * But that would require calling this method via sendRequest() rather
992 * than sendRequestAsync(), and right now we don't actually *need* that
993 * return value, so let's just return void for now.
994 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700995 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700996 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700998 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
999 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001000 if (hasActiveCall && hasHoldingCall) {
1001 // Both lines are in use!
1002 // TODO: provide a flag to let the caller specify what
1003 // policy to use if both lines are in use. (The current
1004 // behavior is hardwired to "answer incoming, end ongoing",
1005 // which is how the CALL button is specced to behave.)
1006 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1007 return;
1008 } else {
1009 // answerCall() will automatically hold the current active
1010 // call, if there is one.
1011 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1012 return;
1013 }
1014 } else {
1015 // No call was ringing.
1016 return;
1017 }
1018 }
1019
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001020 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001021 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001022 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001023 public void silenceRinger() {
1024 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001025 }
1026
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001027 @Override
1028 public boolean isOffhook(String callingPackage) {
1029 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001030 }
1031
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001032 @Override
1033 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1034 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1035 return false;
1036 }
1037
Sanket Padawe356d7632015-06-22 14:03:32 -07001038 final Phone phone = getPhone(subId);
1039 if (phone != null) {
1040 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1041 } else {
1042 return false;
1043 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001044 }
1045
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001046 @Override
1047 public boolean isRinging(String callingPackage) {
1048 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001049 }
1050
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001051 @Override
1052 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1053 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1054 return false;
1055 }
1056
Sanket Padawe356d7632015-06-22 14:03:32 -07001057 final Phone phone = getPhone(subId);
1058 if (phone != null) {
1059 return (phone.getState() == PhoneConstants.State.RINGING);
1060 } else {
1061 return false;
1062 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001063 }
1064
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001065 @Override
1066 public boolean isIdle(String callingPackage) {
1067 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001068 }
1069
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001070 @Override
1071 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1072 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1073 return false;
1074 }
1075
Sanket Padawe356d7632015-06-22 14:03:32 -07001076 final Phone phone = getPhone(subId);
1077 if (phone != null) {
1078 return (phone.getState() == PhoneConstants.State.IDLE);
1079 } else {
1080 return false;
1081 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 }
1083
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001084 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001085 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001086 }
1087
Wink Savilleb564aae2014-10-23 10:18:09 -07001088 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001089 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001090 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1091 }
1092
1093 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001094 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001095 }
1096
Wink Savilleb564aae2014-10-23 10:18:09 -07001097 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001098 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001099 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1100 }
1101
1102 /** {@hide} */
1103 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001104 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001105 }
1106
Wink Savilleb564aae2014-10-23 10:18:09 -07001107 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001108 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001109 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001110 checkSimPin.start();
1111 return checkSimPin.unlockSim(null, pin);
1112 }
1113
Wink Saville9de0f752013-10-22 19:04:03 -07001114 /** {@hide} */
1115 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001116 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001117 }
1118
Wink Savilleb564aae2014-10-23 10:18:09 -07001119 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001120 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001121 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001122 checkSimPuk.start();
1123 return checkSimPuk.unlockSim(puk, pin);
1124 }
1125
1126 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001127 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001128 * a synchronous one.
1129 */
1130 private static class UnlockSim extends Thread {
1131
1132 private final IccCard mSimCard;
1133
1134 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001135 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1136 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137
1138 // For replies from SimCard interface
1139 private Handler mHandler;
1140
1141 // For async handler to identify request type
1142 private static final int SUPPLY_PIN_COMPLETE = 100;
1143
1144 public UnlockSim(IccCard simCard) {
1145 mSimCard = simCard;
1146 }
1147
1148 @Override
1149 public void run() {
1150 Looper.prepare();
1151 synchronized (UnlockSim.this) {
1152 mHandler = new Handler() {
1153 @Override
1154 public void handleMessage(Message msg) {
1155 AsyncResult ar = (AsyncResult) msg.obj;
1156 switch (msg.what) {
1157 case SUPPLY_PIN_COMPLETE:
1158 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1159 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001160 mRetryCount = msg.arg1;
1161 if (ar.exception != null) {
1162 if (ar.exception instanceof CommandException &&
1163 ((CommandException)(ar.exception)).getCommandError()
1164 == CommandException.Error.PASSWORD_INCORRECT) {
1165 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1166 } else {
1167 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1168 }
1169 } else {
1170 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1171 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001172 mDone = true;
1173 UnlockSim.this.notifyAll();
1174 }
1175 break;
1176 }
1177 }
1178 };
1179 UnlockSim.this.notifyAll();
1180 }
1181 Looper.loop();
1182 }
1183
1184 /*
1185 * Use PIN or PUK to unlock SIM card
1186 *
1187 * If PUK is null, unlock SIM card with PIN
1188 *
1189 * If PUK is not null, unlock SIM card with PUK and set PIN code
1190 */
Wink Saville9de0f752013-10-22 19:04:03 -07001191 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001192
1193 while (mHandler == null) {
1194 try {
1195 wait();
1196 } catch (InterruptedException e) {
1197 Thread.currentThread().interrupt();
1198 }
1199 }
1200 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1201
1202 if (puk == null) {
1203 mSimCard.supplyPin(pin, callback);
1204 } else {
1205 mSimCard.supplyPuk(puk, pin, callback);
1206 }
1207
1208 while (!mDone) {
1209 try {
1210 Log.d(LOG_TAG, "wait for done");
1211 wait();
1212 } catch (InterruptedException e) {
1213 // Restore the interrupted status
1214 Thread.currentThread().interrupt();
1215 }
1216 }
1217 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001218 int[] resultArray = new int[2];
1219 resultArray[0] = mResult;
1220 resultArray[1] = mRetryCount;
1221 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001222 }
1223 }
1224
1225 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001226 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001227
1228 }
1229
Wink Savilleb564aae2014-10-23 10:18:09 -07001230 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001231 // No permission check needed here: this call is harmless, and it's
1232 // needed for the ServiceState.requestStateUpdate() call (which is
1233 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001234 final Phone phone = getPhone(subId);
1235 if (phone != null) {
1236 phone.updateServiceLocation();
1237 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001238 }
1239
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001240 @Override
1241 public boolean isRadioOn(String callingPackage) {
1242 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001243 }
1244
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001245 @Override
1246 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1247 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1248 return false;
1249 }
1250 return isRadioOnForSubscriber(subId);
1251 }
1252
1253 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001254 final Phone phone = getPhone(subId);
1255 if (phone != null) {
1256 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1257 } else {
1258 return false;
1259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001260 }
1261
1262 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001263 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001264
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001265 }
Wink Saville36469e72014-06-11 15:17:00 -07001266
Wink Savilleb564aae2014-10-23 10:18:09 -07001267 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001268 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001269 final Phone phone = getPhone(subId);
1270 if (phone != null) {
1271 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1272 }
Wink Saville36469e72014-06-11 15:17:00 -07001273 }
1274
1275 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001276 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001277 }
1278
Wink Savilleb564aae2014-10-23 10:18:09 -07001279 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001280 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001281 final Phone phone = getPhone(subId);
1282 if (phone == null) {
1283 return false;
1284 }
1285 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001286 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001287 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001288 }
1289 return true;
1290 }
Wink Saville36469e72014-06-11 15:17:00 -07001291
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001292 public boolean needMobileRadioShutdown() {
1293 /*
1294 * If any of the Radios are available, it will need to be
1295 * shutdown. So return true if any Radio is available.
1296 */
1297 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1298 Phone phone = PhoneFactory.getPhone(i);
1299 if (phone != null && phone.isRadioAvailable()) return true;
1300 }
1301 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1302 return false;
1303 }
1304
1305 public void shutdownMobileRadios() {
1306 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1307 logv("Shutting down Phone " + i);
1308 shutdownRadioUsingPhoneId(i);
1309 }
1310 }
1311
1312 private void shutdownRadioUsingPhoneId(int phoneId) {
1313 enforceModifyPermission();
1314 Phone phone = PhoneFactory.getPhone(phoneId);
1315 if (phone != null && phone.isRadioAvailable()) {
1316 phone.shutdownRadio();
1317 }
1318 }
1319
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001320 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001321 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001322 }
1323
Wink Savilleb564aae2014-10-23 10:18:09 -07001324 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001325 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001326 final Phone phone = getPhone(subId);
1327 if (phone != null) {
1328 phone.setRadioPower(turnOn);
1329 return true;
1330 } else {
1331 return false;
1332 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001333 }
1334
Wink Saville36469e72014-06-11 15:17:00 -07001335 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001336 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001337 public boolean enableDataConnectivity() {
1338 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001339 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001340 final Phone phone = getPhone(subId);
1341 if (phone != null) {
1342 phone.setDataEnabled(true);
1343 return true;
1344 } else {
1345 return false;
1346 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001347 }
1348
Wink Saville36469e72014-06-11 15:17:00 -07001349 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001350 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001351 public boolean disableDataConnectivity() {
1352 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001353 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001354 final Phone phone = getPhone(subId);
1355 if (phone != null) {
1356 phone.setDataEnabled(false);
1357 return true;
1358 } else {
1359 return false;
1360 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001361 }
1362
Wink Saville36469e72014-06-11 15:17:00 -07001363 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001364 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001365 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001366 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001367 final Phone phone = getPhone(subId);
1368 if (phone != null) {
1369 return phone.isDataConnectivityPossible();
1370 } else {
1371 return false;
1372 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001373 }
1374
1375 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001376 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001377 }
1378
Wink Savilleb564aae2014-10-23 10:18:09 -07001379 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001380 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001381 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1382 return false;
1383 }
Wink Saville36469e72014-06-11 15:17:00 -07001384 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001385 }
1386
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001387 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001388 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001389 }
1390
Wink Savilleb564aae2014-10-23 10:18:09 -07001391 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001392 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001393 }
1394
Sanket Padawe356d7632015-06-22 14:03:32 -07001395 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001396 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001397 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001398 if (phone != null) {
1399 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1400 } else {
1401 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1402 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001403 }
1404
Sanket Padawe356d7632015-06-22 14:03:32 -07001405 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001406 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001407 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001408 if (phone != null) {
1409 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1410 } else {
1411 return TelephonyManager.DATA_ACTIVITY_NONE;
1412 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001413 }
1414
1415 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001416 public Bundle getCellLocation(String callingPackage) {
1417 enforceFineOrCoarseLocationPermission("getCellLocation");
1418
1419 // OP_COARSE_LOCATION controls both fine and coarse location.
1420 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1421 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001422 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001423 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001424 }
1425
Jake Hambye994d462014-02-03 13:10:13 -08001426 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001427 if (DBG_LOC) log("getCellLocation: is active user");
1428 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001429 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001430 if (phone == null) {
1431 return null;
1432 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001433 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001434 return data;
1435 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001436 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001437 return null;
1438 }
1439 }
1440
Svetoslav64fad262015-04-14 14:35:21 -07001441 private void enforceFineOrCoarseLocationPermission(String message) {
1442 try {
1443 mApp.enforceCallingOrSelfPermission(
1444 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1445 } catch (SecurityException e) {
1446 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1447 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1448 // is the weaker precondition
1449 mApp.enforceCallingOrSelfPermission(
1450 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1451 }
1452 }
1453
1454
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001455 @Override
1456 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001457 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001458 }
1459
Sanket Padawe356d7632015-06-22 14:03:32 -07001460 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001461 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001462 mApp.enforceCallingOrSelfPermission(
1463 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001464 final Phone phone = getPhone(subId);
1465 if (phone != null) {
1466 phone.enableLocationUpdates();
1467 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001468 }
1469
1470 @Override
1471 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001472 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001473 }
1474
Sanket Padawe356d7632015-06-22 14:03:32 -07001475 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001476 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001477 mApp.enforceCallingOrSelfPermission(
1478 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001479 final Phone phone = getPhone(subId);
1480 if (phone != null) {
1481 phone.disableLocationUpdates();
1482 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001483 }
1484
1485 @Override
1486 @SuppressWarnings("unchecked")
1487 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001488 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1489
1490 // OP_COARSE_LOCATION controls both fine and coarse location.
1491 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1492 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1493 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001494 }
1495
1496 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1497 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1498 return null;
1499 }
Svetoslav64fad262015-04-14 14:35:21 -07001500
Jake Hambye994d462014-02-03 13:10:13 -08001501 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001502 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1503
1504 ArrayList<NeighboringCellInfo> cells = null;
1505
1506 try {
1507 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001508 CMD_HANDLE_NEIGHBORING_CELL, null,
1509 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001510 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001511 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001512 }
1513 return cells;
1514 } else {
1515 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1516 return null;
1517 }
1518 }
1519
1520
1521 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001522 public List<CellInfo> getAllCellInfo(String callingPackage) {
1523 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1524
1525 // OP_COARSE_LOCATION controls both fine and coarse location.
1526 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1527 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1528 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001529 }
1530
Jake Hambye994d462014-02-03 13:10:13 -08001531 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001532 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001533 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1534 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001535 final List<CellInfo> info = phone.getAllCellInfo();
1536 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001537 }
1538 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001539 } else {
1540 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1541 return null;
1542 }
1543 }
1544
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001545 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001546 public void setCellInfoListRate(int rateInMillis) {
1547 mPhone.setCellInfoListRate(rateInMillis);
1548 }
1549
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 //
1551 // Internal helper methods.
1552 //
1553
Jake Hambye994d462014-02-03 13:10:13 -08001554 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001555 boolean ok;
1556
1557 boolean self = Binder.getCallingUid() == Process.myUid();
1558 if (!self) {
1559 // Get the caller's user id then clear the calling identity
1560 // which will be restored in the finally clause.
1561 int callingUser = UserHandle.getCallingUserId();
1562 long ident = Binder.clearCallingIdentity();
1563
1564 try {
1565 // With calling identity cleared the current user is the foreground user.
1566 int foregroundUser = ActivityManager.getCurrentUser();
1567 ok = (foregroundUser == callingUser);
1568 if (DBG_LOC) {
1569 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1570 + " callingUser=" + callingUser + " ok=" + ok);
1571 }
1572 } catch (Exception ex) {
1573 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1574 ok = false;
1575 } finally {
1576 Binder.restoreCallingIdentity(ident);
1577 }
1578 } else {
1579 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1580 ok = true;
1581 }
1582 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1583 return ok;
1584 }
1585
1586 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001587 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1588 *
1589 * @throws SecurityException if the caller does not have the required permission
1590 */
1591 private void enforceModifyPermission() {
1592 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1593 }
1594
1595 /**
Junda Liua2e36012014-07-09 18:30:01 -07001596 * Make sure either system app or the caller has carrier privilege.
1597 *
1598 * @throws SecurityException if the caller does not have the required permission/privilege
1599 */
1600 private void enforceModifyPermissionOrCarrierPrivilege() {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001601 int permission = mApp.checkCallingOrSelfPermission(
1602 android.Manifest.permission.MODIFY_PHONE_STATE);
1603 if (permission == PackageManager.PERMISSION_GRANTED) {
1604 return;
1605 }
1606
1607 log("No modify permission, check carrier privilege next.");
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001608 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001609 loge("No Carrier Privilege.");
1610 throw new SecurityException("No modify permission or carrier privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001611 }
1612 }
1613
1614 /**
1615 * Make sure the caller has carrier privilege.
1616 *
1617 * @throws SecurityException if the caller does not have the required permission
1618 */
1619 private void enforceCarrierPrivilege() {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001620 if (getCarrierPrivilegeStatus() != TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001621 loge("No Carrier Privilege.");
1622 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001623 }
1624 }
1625
1626 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001627 * Make sure the caller has the CALL_PHONE permission.
1628 *
1629 * @throws SecurityException if the caller does not have the required permission
1630 */
1631 private void enforceCallPermission() {
1632 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1633 }
1634
Stuart Scott8eef64f2015-04-08 15:13:54 -07001635 private void enforceConnectivityInternalPermission() {
1636 mApp.enforceCallingOrSelfPermission(
1637 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1638 "ConnectivityService");
1639 }
1640
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001641 private String createTelUrl(String number) {
1642 if (TextUtils.isEmpty(number)) {
1643 return null;
1644 }
1645
Jake Hambye994d462014-02-03 13:10:13 -08001646 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001647 }
1648
Ihab Awadf9e92732013-12-05 18:02:52 -08001649 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001650 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1651 }
1652
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001653 private static void logv(String msg) {
1654 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1655 }
1656
Ihab Awadf9e92732013-12-05 18:02:52 -08001657 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001658 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1659 }
1660
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001661 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001662 public int getActivePhoneType() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001663 return getActivePhoneTypeForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001664 }
1665
Sanket Padawe356d7632015-06-22 14:03:32 -07001666 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001667 public int getActivePhoneTypeForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001668 final Phone phone = getPhone(subId);
1669 if (phone == null) {
1670 return PhoneConstants.PHONE_TYPE_NONE;
1671 } else {
1672 return getPhone(subId).getPhoneType();
1673 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 }
1675
1676 /**
1677 * Returns the CDMA ERI icon index to display
1678 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001679 @Override
1680 public int getCdmaEriIconIndex(String callingPackage) {
1681 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001682 }
1683
Sanket Padawe356d7632015-06-22 14:03:32 -07001684 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001685 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1686 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1687 return -1;
1688 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001689 final Phone phone = getPhone(subId);
1690 if (phone != null) {
1691 return phone.getCdmaEriIconIndex();
1692 } else {
1693 return -1;
1694 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001695 }
1696
1697 /**
1698 * Returns the CDMA ERI icon mode,
1699 * 0 - ON
1700 * 1 - FLASHING
1701 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001702 @Override
1703 public int getCdmaEriIconMode(String callingPackage) {
1704 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001705 }
1706
Sanket Padawe356d7632015-06-22 14:03:32 -07001707 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001708 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1709 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1710 return -1;
1711 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001712 final Phone phone = getPhone(subId);
1713 if (phone != null) {
1714 return phone.getCdmaEriIconMode();
1715 } else {
1716 return -1;
1717 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001718 }
1719
1720 /**
1721 * Returns the CDMA ERI text,
1722 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001723 @Override
1724 public String getCdmaEriText(String callingPackage) {
1725 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001726 }
1727
Sanket Padawe356d7632015-06-22 14:03:32 -07001728 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001729 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1730 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1731 return null;
1732 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001733 final Phone phone = getPhone(subId);
1734 if (phone != null) {
1735 return phone.getCdmaEriText();
1736 } else {
1737 return null;
1738 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001739 }
1740
1741 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001742 * Returns the CDMA MDN.
1743 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001744 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001745 public String getCdmaMdn(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001746 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001747 final Phone phone = getPhone(subId);
1748 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1749 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001750 } else {
1751 return null;
1752 }
1753 }
1754
1755 /**
1756 * Returns the CDMA MIN.
1757 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001758 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001759 public String getCdmaMin(int subId) {
Junda Liuca05d5d2014-08-14 22:36:34 -07001760 enforceModifyPermissionOrCarrierPrivilege();
Sanket Padawe356d7632015-06-22 14:03:32 -07001761 final Phone phone = getPhone(subId);
1762 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1763 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001764 } else {
1765 return null;
1766 }
1767 }
1768
1769 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001770 * Returns true if CDMA provisioning needs to run.
1771 */
1772 public boolean needsOtaServiceProvisioning() {
1773 return mPhone.needsOtaServiceProvisioning();
1774 }
1775
1776 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001777 * Sets the voice mail number of a given subId.
1778 */
1779 @Override
1780 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08001781 enforceCarrierPrivilege();
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001782 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1783 new Pair<String, String>(alphaTag, number), new Integer(subId));
1784 return success;
1785 }
1786
1787 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001788 * Returns the unread count of voicemails
1789 */
1790 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001791 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001792 }
1793
1794 /**
1795 * Returns the unread count of voicemails for a subId
1796 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001797 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001798 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001799 final Phone phone = getPhone(subId);
1800 if (phone != null) {
1801 return phone.getVoiceMessageCount();
1802 } else {
1803 return 0;
1804 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001805 }
1806
1807 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001808 * Returns the data network type.
1809 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001810 *
1811 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1812 */
1813 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001814 public int getNetworkType() {
1815 final Phone phone = getPhone(getDefaultSubscription());
1816 if (phone != null) {
1817 return phone.getServiceState().getDataNetworkType();
1818 } else {
1819 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1820 }
Wink Saville36469e72014-06-11 15:17:00 -07001821 }
1822
1823 /**
1824 * Returns the network type for a subId
1825 */
1826 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001827 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1828 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1829 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1830 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001831
Sanket Padawe356d7632015-06-22 14:03:32 -07001832 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001833 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001834 return phone.getServiceState().getDataNetworkType();
1835 } else {
1836 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1837 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001838 }
1839
1840 /**
1841 * Returns the data network type
1842 */
1843 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001844 public int getDataNetworkType(String callingPackage) {
1845 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001846 }
1847
1848 /**
1849 * Returns the data network type for a subId
1850 */
1851 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001852 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1853 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1854 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1855 }
1856
Sanket Padawe356d7632015-06-22 14:03:32 -07001857 final Phone phone = getPhone(subId);
1858 if (phone != null) {
1859 return phone.getServiceState().getDataNetworkType();
1860 } else {
1861 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1862 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001863 }
1864
1865 /**
Wink Saville36469e72014-06-11 15:17:00 -07001866 * Returns the Voice network type for a subId
1867 */
1868 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001869 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1870 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1871 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1872 }
1873
Sanket Padawe356d7632015-06-22 14:03:32 -07001874 final Phone phone = getPhone(subId);
1875 if (phone != null) {
1876 return phone.getServiceState().getVoiceNetworkType();
1877 } else {
1878 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1879 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001880 }
1881
1882 /**
1883 * @return true if a ICC card is present
1884 */
1885 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001886 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001887 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001888 }
1889
1890 /**
1891 * @return true if a ICC card is present for a slotId
1892 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001893 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001894 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001895 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001896 final Phone phone = getPhone(subId[0]);
1897 if (subId != null && phone != null) {
1898 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001899 } else {
1900 return false;
1901 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001902 }
1903
1904 /**
1905 * Return if the current radio is LTE on CDMA. This
1906 * is a tri-state return value as for a period of time
1907 * the mode may be unknown.
1908 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001909 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001910 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08001911 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001912 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001913 @Override
1914 public int getLteOnCdmaMode(String callingPackage) {
1915 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001916 }
1917
Sanket Padawe356d7632015-06-22 14:03:32 -07001918 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001919 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
1920 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
1921 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1922 }
1923
Sanket Padawe356d7632015-06-22 14:03:32 -07001924 final Phone phone = getPhone(subId);
1925 if (phone == null) {
1926 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
1927 } else {
1928 return phone.getLteOnCdmaMode();
1929 }
Wink Saville36469e72014-06-11 15:17:00 -07001930 }
1931
1932 public void setPhone(Phone phone) {
1933 mPhone = phone;
1934 }
1935
1936 /**
1937 * {@hide}
1938 * Returns Default subId, 0 in the case of single standby.
1939 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001940 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001941 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07001942 }
1943
Wink Savilleb564aae2014-10-23 10:18:09 -07001944 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001945 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001946 }
Ihab Awadf2177b72013-11-25 13:33:23 -08001947
1948 /**
1949 * @see android.telephony.TelephonyManager.WifiCallingChoices
1950 */
1951 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001952 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
1953 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08001954 }
1955
1956 /**
1957 * @see android.telephony.TelephonyManager.WifiCallingChoices
1958 */
1959 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08001960 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
1961 Settings.System.putInt(mPhone.getContext().getContentResolver(),
1962 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08001963 }
1964
Sailesh Nepald1e68152013-12-12 19:08:02 -08001965 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07001966 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08001967 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08001968 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08001969
Shishir Agrawal566b7612013-10-28 14:41:00 -07001970 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001971 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(String AID) {
Junda Liua2e36012014-07-09 18:30:01 -07001972 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001973
1974 if (DBG) log("iccOpenLogicalChannel: " + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07001975 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
1976 CMD_OPEN_CHANNEL, AID);
1977 if (DBG) log("iccOpenLogicalChannel: " + response);
1978 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07001979 }
1980
1981 @Override
1982 public boolean iccCloseLogicalChannel(int channel) {
Junda Liua2e36012014-07-09 18:30:01 -07001983 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001984
1985 if (DBG) log("iccCloseLogicalChannel: " + channel);
1986 if (channel < 0) {
1987 return false;
1988 }
Jake Hambye994d462014-02-03 13:10:13 -08001989 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07001990 if (DBG) log("iccCloseLogicalChannel: " + success);
1991 return success;
1992 }
1993
1994 @Override
1995 public String iccTransmitApduLogicalChannel(int channel, int cla,
1996 int command, int p1, int p2, int p3, String data) {
Junda Liua2e36012014-07-09 18:30:01 -07001997 enforceModifyPermissionOrCarrierPrivilege();
Shishir Agrawal566b7612013-10-28 14:41:00 -07001998
1999 if (DBG) {
2000 log("iccTransmitApduLogicalChannel: chnl=" + channel + " cla=" + cla +
2001 " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
2002 " data=" + data);
2003 }
2004
2005 if (channel < 0) {
2006 return "";
2007 }
2008
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002009 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002010 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data));
2011 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2012
Shishir Agrawal566b7612013-10-28 14:41:00 -07002013 // Append the returned status code to the end of the response payload.
2014 String s = Integer.toHexString(
2015 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002016 if (response.payload != null) {
2017 s = IccUtils.bytesToHexString(response.payload) + s;
2018 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002019 return s;
2020 }
Jake Hambye994d462014-02-03 13:10:13 -08002021
Evan Charltonc66da362014-05-16 14:06:40 -07002022 @Override
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002023 public String iccTransmitApduBasicChannel(int cla, int command, int p1, int p2,
2024 int p3, String data) {
2025 enforceModifyPermissionOrCarrierPrivilege();
2026
2027 if (DBG) {
2028 log("iccTransmitApduBasicChannel: cla=" + cla + " cmd=" + command + " p1="
2029 + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
2030 }
2031
2032 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
2033 new IccAPDUArgument(0, cla, command, p1, p2, p3, data));
2034 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2035
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002036 // Append the returned status code to the end of the response payload.
2037 String s = Integer.toHexString(
2038 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002039 if (response.payload != null) {
2040 s = IccUtils.bytesToHexString(response.payload) + s;
2041 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002042 return s;
2043 }
2044
2045 @Override
2046 public byte[] iccExchangeSimIO(int fileID, int command, int p1, int p2, int p3,
2047 String filePath) {
2048 enforceModifyPermissionOrCarrierPrivilege();
2049
2050 if (DBG) {
2051 log("Exchange SIM_IO " + fileID + ":" + command + " " +
2052 p1 + " " + p2 + " " + p3 + ":" + filePath);
2053 }
2054
2055 IccIoResult response =
2056 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Yong Jiang3edf3782014-10-03 13:23:28 -05002057 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath));
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002058
2059 if (DBG) {
2060 log("Exchange SIM_IO [R]" + response);
2061 }
2062
2063 byte[] result = null;
2064 int length = 2;
2065 if (response.payload != null) {
2066 length = 2 + response.payload.length;
2067 result = new byte[length];
2068 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2069 } else {
2070 result = new byte[length];
2071 }
2072
2073 result[length - 1] = (byte) response.sw2;
2074 result[length - 2] = (byte) response.sw1;
2075 return result;
2076 }
2077
2078 @Override
Evan Charltonc66da362014-05-16 14:06:40 -07002079 public String sendEnvelopeWithStatus(String content) {
Junda Liua2e36012014-07-09 18:30:01 -07002080 enforceModifyPermissionOrCarrierPrivilege();
Evan Charltonc66da362014-05-16 14:06:40 -07002081
2082 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content);
2083 if (response.payload == null) {
2084 return "";
2085 }
2086
2087 // Append the returned status code to the end of the response payload.
2088 String s = Integer.toHexString(
2089 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2090 s = IccUtils.bytesToHexString(response.payload) + s;
2091 return s;
2092 }
2093
Jake Hambye994d462014-02-03 13:10:13 -08002094 /**
2095 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2096 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2097 *
2098 * @param itemID the ID of the item to read
2099 * @return the NV item as a String, or null on error.
2100 */
2101 @Override
2102 public String nvReadItem(int itemID) {
Junda Liua2e36012014-07-09 18:30:01 -07002103 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002104 if (DBG) log("nvReadItem: item " + itemID);
2105 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2106 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2107 return value;
2108 }
2109
2110 /**
2111 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2112 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2113 *
2114 * @param itemID the ID of the item to read
2115 * @param itemValue the value to write, as a String
2116 * @return true on success; false on any failure
2117 */
2118 @Override
2119 public boolean nvWriteItem(int itemID, String itemValue) {
Junda Liua2e36012014-07-09 18:30:01 -07002120 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002121 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2122 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2123 new Pair<Integer, String>(itemID, itemValue));
2124 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2125 return success;
2126 }
2127
2128 /**
2129 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2130 * Used for device configuration by some CDMA operators.
2131 *
2132 * @param preferredRoamingList byte array containing the new PRL
2133 * @return true on success; false on any failure
2134 */
2135 @Override
2136 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Junda Liua2e36012014-07-09 18:30:01 -07002137 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002138 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2139 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2140 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2141 return success;
2142 }
2143
2144 /**
2145 * Perform the specified type of NV config reset.
2146 * Used for device configuration by some CDMA operators.
2147 *
2148 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2149 * @return true on success; false on any failure
2150 */
2151 @Override
2152 public boolean nvResetConfig(int resetType) {
Junda Liua2e36012014-07-09 18:30:01 -07002153 enforceModifyPermissionOrCarrierPrivilege();
Jake Hambye994d462014-02-03 13:10:13 -08002154 if (DBG) log("nvResetConfig: type " + resetType);
2155 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2156 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2157 return success;
2158 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002159
2160 /**
Wink Saville36469e72014-06-11 15:17:00 -07002161 * {@hide}
2162 * Returns Default sim, 0 in the case of single standby.
2163 */
2164 public int getDefaultSim() {
2165 //TODO Need to get it from Telephony Devcontroller
2166 return 0;
2167 }
2168
Svet Ganovb320e182015-04-16 12:30:10 -07002169 public String[] getPcscfAddress(String apnType, String callingPackage) {
2170 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2171 return new String[0];
2172 }
2173
2174
ram87fca6f2014-07-18 18:58:44 +05302175 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002176 }
2177
2178 public void setImsRegistrationState(boolean registered) {
2179 enforceModifyPermission();
2180 mPhone.setImsRegistrationState(registered);
2181 }
2182
2183 /**
Stuart Scott54788802015-03-30 13:18:01 -07002184 * Set the network selection mode to automatic.
2185 *
2186 */
2187 @Override
2188 public void setNetworkSelectionModeAutomatic(int subId) {
2189 enforceModifyPermissionOrCarrierPrivilege();
2190 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2191 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2192 }
2193
2194 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002195 * Set the network selection mode to manual with the selected carrier.
2196 */
2197 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002198 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2199 boolean persistSelection) {
Shishir Agrawal302c8692015-06-19 13:49:39 -07002200 enforceModifyPermissionOrCarrierPrivilege();
2201 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002202 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2203 persistSelection);
2204 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002205 }
2206
2207 /**
2208 * Scans for available networks.
2209 */
2210 @Override
2211 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
2212 enforceModifyPermissionOrCarrierPrivilege();
2213 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2214 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2215 CMD_PERFORM_NETWORK_SCAN, null, subId);
2216 return result;
2217 }
2218
2219 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002220 * Get the calculated preferred network type.
2221 * Used for debugging incorrect network type.
2222 *
2223 * @return the preferred network type, defined in RILConstants.java.
2224 */
2225 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002226 public int getCalculatedPreferredNetworkType(String callingPackage) {
2227 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2228 return RILConstants.PREFERRED_NETWORK_MODE;
2229 }
2230
Amit Mahajan43330e02014-11-18 11:54:45 -08002231 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002232 }
2233
2234 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002235 * Get the preferred network type.
2236 * Used for device configuration by some CDMA operators.
2237 *
2238 * @return the preferred network type, defined in RILConstants.java.
2239 */
2240 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002241 public int getPreferredNetworkType(int subId) {
Junda Liua2e36012014-07-09 18:30:01 -07002242 enforceModifyPermissionOrCarrierPrivilege();
Jake Hamby7c27be32014-03-03 13:25:59 -08002243 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002244 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002245 int networkType = (result != null ? result[0] : -1);
2246 if (DBG) log("getPreferredNetworkType: " + networkType);
2247 return networkType;
2248 }
2249
2250 /**
2251 * Set the preferred network type.
2252 * Used for device configuration by some CDMA operators.
2253 *
2254 * @param networkType the preferred network type, defined in RILConstants.java.
2255 * @return true on success; false on any failure.
2256 */
2257 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002258 public boolean setPreferredNetworkType(int subId, int networkType) {
Junda Liua2e36012014-07-09 18:30:01 -07002259 enforceModifyPermissionOrCarrierPrivilege();
Stuart Scott54788802015-03-30 13:18:01 -07002260 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2261 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002262 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002263 if (success) {
2264 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002265 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002266 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002267 return success;
2268 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002269
2270 /**
Junda Liu475951f2014-11-07 16:45:03 -08002271 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2272 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2273 * tethering.
2274 *
2275 * @return 0: Not required. 1: required. 2: Not set.
2276 * @hide
2277 */
2278 @Override
2279 public int getTetherApnRequired() {
2280 enforceModifyPermissionOrCarrierPrivilege();
2281 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2282 Settings.Global.TETHER_DUN_REQUIRED, 2);
2283 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2284 // config_tether_apndata.
2285 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2286 dunRequired = 1;
2287 }
2288 return dunRequired;
2289 }
2290
2291 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002292 * Set mobile data enabled
2293 * Used by the user through settings etc to turn on/off mobile data
2294 *
2295 * @param enable {@code true} turn turn data on, else {@code false}
2296 */
2297 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002298 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002299 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002300 int phoneId = mSubscriptionController.getPhoneId(subId);
2301 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2302 Phone phone = PhoneFactory.getPhone(phoneId);
2303 if (phone != null) {
2304 log("setDataEnabled: subId=" + subId + " enable=" + enable);
2305 phone.setDataEnabled(enable);
2306 } else {
2307 loge("setDataEnabled: no phone for subId=" + subId);
2308 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002309 }
2310
2311 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002312 * Get whether mobile data is enabled.
2313 *
2314 * Note that this used to be available from ConnectivityService, gated by
2315 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2316 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002317 *
2318 * @return {@code true} if data is enabled else {@code false}
2319 */
2320 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002321 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002322 try {
2323 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2324 null);
2325 } catch (Exception e) {
2326 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2327 null);
2328 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002329 int phoneId = mSubscriptionController.getPhoneId(subId);
2330 log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
2331 Phone phone = PhoneFactory.getPhone(phoneId);
2332 if (phone != null) {
2333 boolean retVal = phone.getDataEnabled();
2334 log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
2335 return retVal;
2336 } else {
2337 loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
2338 return false;
2339 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002340 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002341
2342 @Override
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002343 public int getCarrierPrivilegeStatus() {
Shishir Agrawal21409252015-01-15 23:33:50 -08002344 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002345 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002346 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002347 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2348 }
2349 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07002350 mPhone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002351 }
Junda Liu29340342014-07-10 15:23:27 -07002352
2353 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002354 public int checkCarrierPrivilegesForPackage(String pkgName) {
Shishir Agrawal21409252015-01-15 23:33:50 -08002355 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002356 if (card == null) {
2357 loge("checkCarrierPrivilegesForPackage: No UICC");
2358 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2359 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002360 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2361 }
2362
2363 @Override
2364 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
2365 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2366 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2367 UiccCard card = UiccController.getInstance().getUiccCard(i);
2368 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002369 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002370 continue;
2371 }
2372
2373 result = card.getCarrierPrivilegeStatus(
2374 mPhone.getContext().getPackageManager(), pkgName);
2375 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2376 break;
2377 }
2378 }
2379
2380 return result;
Junda Liu29340342014-07-10 15:23:27 -07002381 }
Derek Tan89e89d42014-07-08 17:00:10 -07002382
2383 @Override
Junda Liue64de782015-04-16 17:19:16 -07002384 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2385 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2386 loge("phoneId " + phoneId + " is not valid.");
2387 return null;
2388 }
2389 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002390 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002391 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002392 return null ;
2393 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002394 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002395 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002396 }
2397
Wink Savilleb564aae2014-10-23 10:18:09 -07002398 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002399 final Phone phone = getPhone(subId);
2400 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002401 if (card == null) {
2402 loge("getIccId: No UICC");
2403 return null;
2404 }
2405 String iccId = card.getIccId();
2406 if (TextUtils.isEmpty(iccId)) {
2407 loge("getIccId: ICC ID is null or empty.");
2408 return null;
2409 }
2410 return iccId;
2411 }
2412
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002413 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002414 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2415 String number) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002416 enforceCarrierPrivilege();
Derek Tan97ebb422014-09-05 16:55:38 -07002417
Jeff Sharkey85190e62014-12-05 09:40:12 -08002418 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002419 final Phone phone = getPhone(subId);
2420 if (phone == null) {
2421 return false;
2422 }
2423 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002424
2425 if (DBG_MERGE) {
2426 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2427 + subscriberId + " to " + number);
2428 }
2429
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002430 if (TextUtils.isEmpty(iccId)) {
2431 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002432 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002433
Jeff Sharkey85190e62014-12-05 09:40:12 -08002434 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2435
2436 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002437 if (alphaTag == null) {
2438 editor.remove(alphaTagPrefKey);
2439 } else {
2440 editor.putString(alphaTagPrefKey, alphaTag);
2441 }
2442
Jeff Sharkey85190e62014-12-05 09:40:12 -08002443 // Record both the line number and IMSI for this ICCID, since we need to
2444 // track all merged IMSIs based on line number
2445 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2446 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002447 if (number == null) {
2448 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002449 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002450 } else {
2451 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002452 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002453 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002454
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002455 editor.commit();
2456 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002457 }
2458
2459 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002460 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002461 // This is open to apps with WRITE_SMS.
2462 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002463 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002464 return null;
2465 }
Derek Tan97ebb422014-09-05 16:55:38 -07002466
2467 String iccId = getIccId(subId);
2468 if (iccId != null) {
2469 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002470 if (DBG_MERGE) {
2471 log("getLine1NumberForDisplay returning " +
2472 mTelephonySharedPreferences.getString(numberPrefKey, null));
2473 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002474 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002475 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002476 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002477 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002478 }
2479
2480 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002481 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2482 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2483 return null;
2484 }
Derek Tan97ebb422014-09-05 16:55:38 -07002485
2486 String iccId = getIccId(subId);
2487 if (iccId != null) {
2488 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002489 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002490 }
Derek Tan97ebb422014-09-05 16:55:38 -07002491 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002492 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002493
2494 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002495 public String[] getMergedSubscriberIds(String callingPackage) {
2496 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2497 return null;
2498 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002499 final Context context = mPhone.getContext();
2500 final TelephonyManager tele = TelephonyManager.from(context);
2501 final SubscriptionManager sub = SubscriptionManager.from(context);
2502
2503 // Figure out what subscribers are currently active
2504 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002505 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2506 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2507 final long identity = Binder.clearCallingIdentity();
2508 try {
2509 final int[] subIds = sub.getActiveSubscriptionIdList();
2510 for (int subId : subIds) {
2511 activeSubscriberIds.add(tele.getSubscriberId(subId));
2512 }
2513 } finally {
2514 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002515 }
2516
2517 // First pass, find a number override for an active subscriber
2518 String mergeNumber = null;
2519 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2520 for (String key : prefs.keySet()) {
2521 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2522 final String subscriberId = (String) prefs.get(key);
2523 if (activeSubscriberIds.contains(subscriberId)) {
2524 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2525 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2526 mergeNumber = (String) prefs.get(numberKey);
2527 if (DBG_MERGE) {
2528 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2529 + " for active subscriber " + subscriberId);
2530 }
2531 if (!TextUtils.isEmpty(mergeNumber)) {
2532 break;
2533 }
2534 }
2535 }
2536 }
2537
2538 // Shortcut when no active merged subscribers
2539 if (TextUtils.isEmpty(mergeNumber)) {
2540 return null;
2541 }
2542
2543 // Second pass, find all subscribers under that line override
2544 final ArraySet<String> result = new ArraySet<>();
2545 for (String key : prefs.keySet()) {
2546 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2547 final String number = (String) prefs.get(key);
2548 if (mergeNumber.equals(number)) {
2549 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2550 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2551 final String subscriberId = (String) prefs.get(subscriberKey);
2552 if (!TextUtils.isEmpty(subscriberId)) {
2553 result.add(subscriberId);
2554 }
2555 }
2556 }
2557 }
2558
2559 final String[] resultArray = result.toArray(new String[result.size()]);
2560 Arrays.sort(resultArray);
2561 if (DBG_MERGE) {
2562 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2563 }
2564 return resultArray;
2565 }
2566
2567 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002568 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002569 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002570 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002571 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002572
2573 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002574 public boolean setRoamingOverride(List<String> gsmRoamingList,
2575 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2576 List<String> cdmaNonRoamingList) {
2577 enforceCarrierPrivilege();
2578 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2579 cdmaNonRoamingList);
2580 }
2581
2582 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002583 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2584 enforceModifyPermission();
2585
2586 int returnValue = 0;
2587 try {
2588 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2589 if(result.exception == null) {
2590 if (result.result != null) {
2591 byte[] responseData = (byte[])(result.result);
2592 if(responseData.length > oemResp.length) {
2593 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2594 responseData.length + "bytes. Buffer Size is " +
2595 oemResp.length + "bytes.");
2596 }
2597 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2598 returnValue = responseData.length;
2599 }
2600 } else {
2601 CommandException ex = (CommandException) result.exception;
2602 returnValue = ex.getCommandError().ordinal();
2603 if(returnValue > 0) returnValue *= -1;
2604 }
2605 } catch (RuntimeException e) {
2606 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2607 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2608 if(returnValue > 0) returnValue *= -1;
2609 }
2610
2611 return returnValue;
2612 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002613
2614 @Override
2615 public void setRadioCapability(RadioAccessFamily[] rafs) {
2616 try {
2617 ProxyController.getInstance().setRadioCapability(rafs);
2618 } catch (RuntimeException e) {
2619 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2620 }
2621 }
2622
2623 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002624 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2625 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2626 return RadioAccessFamily.RAF_UNKNOWN;
2627 }
2628
Wink Saville5d475dd2014-10-17 15:00:58 -07002629 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2630 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002631
2632 @Override
2633 public void enableVideoCalling(boolean enable) {
2634 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002635 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002636 }
2637
2638 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002639 public boolean isVideoCallingEnabled(String callingPackage) {
2640 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2641 return false;
2642 }
2643
Andrew Lee77527ac2014-10-21 16:57:39 -07002644 // Check the user preference and the system-level IMS setting. Even if the user has
2645 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2646 // In the long run, we may instead need to check if there exists a connection service
2647 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002648 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2649 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002650 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002651 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002652
Andrew Leea1239f22015-03-02 17:44:07 -08002653 @Override
2654 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002655 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002656 }
2657
2658 @Override
2659 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002660 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002661 }
2662
Andrew Lee9431b832015-03-09 18:46:45 -07002663 @Override
2664 public boolean isTtyModeSupported() {
2665 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2666 TelephonyManager telephonyManager =
2667 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2668 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2669 }
2670
2671 @Override
2672 public boolean isHearingAidCompatibilitySupported() {
2673 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2674 }
2675
Sanket Padawe7310cc72015-01-14 09:53:20 -08002676 /**
2677 * Returns the unique device ID of phone, for example, the IMEI for
2678 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2679 *
2680 * <p>Requires Permission:
2681 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2682 */
2683 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002684 public String getDeviceId(String callingPackage) {
2685 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2686 return null;
2687 }
2688
Sanket Padawe7310cc72015-01-14 09:53:20 -08002689 final Phone phone = PhoneFactory.getPhone(0);
2690 if (phone != null) {
2691 return phone.getDeviceId();
2692 } else {
2693 return null;
2694 }
2695 }
2696
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002697 /*
2698 * {@hide}
2699 * Returns the IMS Registration Status
2700 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002701 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002702 public boolean isImsRegistered() {
2703 return mPhone.isImsRegistered();
2704 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002705
2706 @Override
2707 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2708 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2709 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002710
Nathan Haroldc55097a2015-03-11 18:14:50 -07002711 /*
2712 * {@hide}
2713 * Returns the IMS Registration Status
2714 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002715 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002716 return mPhone.isWifiCallingEnabled();
2717 }
2718
2719 /*
2720 * {@hide}
2721 * Returns the IMS Registration Status
2722 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002723 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002724 return mPhone.isVolteEnabled();
2725 }
Svet Ganovb320e182015-04-16 12:30:10 -07002726
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002727 /*
2728 * {@hide} Returns the IMS Registration Status
2729 */
2730 public boolean isVideoTelephonyAvailable() {
2731 return mPhone.isVideoEnabled();
2732 }
2733
Svet Ganovb320e182015-04-16 12:30:10 -07002734 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002735 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002736 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002737 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2738
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002739 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002740 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002741 } catch (SecurityException e) {
2742 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2743 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002744 }
Svet Ganovb320e182015-04-16 12:30:10 -07002745
2746 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2747 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2748 return false;
2749 }
2750
2751 return true;
2752 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002753
Makoto Onukifee69342015-06-29 14:44:50 -07002754 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002755 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002756 */
2757 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002758 // Default SMS app can always read it.
2759 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2760 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2761 return true;
2762 }
2763 try {
2764 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002765 } catch (SecurityException readPhoneStateSecurityException) {
2766 try {
2767 // Can be read with READ_SMS too.
2768 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2769 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2770 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2771 } catch (SecurityException readSmsSecurityException) {
2772 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2773 // permissions
2774 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2775 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2776 " or " + android.Manifest.permission.READ_SMS + ".");
2777 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002778 }
Makoto Onukifee69342015-06-29 14:44:50 -07002779 }
2780
Stuart Scott8eef64f2015-04-08 15:13:54 -07002781 @Override
2782 public void factoryReset(int subId) {
2783 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002784 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2785 return;
2786 }
2787
Svet Ganovcc087f82015-05-12 20:35:54 -07002788 final long identity = Binder.clearCallingIdentity();
2789 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002790 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2791 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002792 // Enable data
2793 setDataEnabled(subId, true);
2794 // Set network selection mode to automatic
2795 setNetworkSelectionModeAutomatic(subId);
2796 // Set preferred mobile network type to the best available
2797 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2798 // Turn off roaming
2799 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2800 }
2801 } finally {
2802 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002803 }
2804 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002805
2806 @Override
2807 public String getLocaleFromDefaultSim() {
2808 // We query all subscriptions instead of just the active ones, because
2809 // this might be called early on in the provisioning flow when the
2810 // subscriptions potentially aren't active yet.
2811 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2812 if (slist == null || slist.isEmpty()) {
2813 return null;
2814 }
2815
2816 // This function may be called very early, say, from the setup wizard, at
2817 // which point we won't have a default subscription set. If that's the case
2818 // we just choose the first, which will be valid in "most cases".
2819 final int defaultSubId = getDefaultSubscription();
2820 SubscriptionInfo info = null;
2821 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2822 info = slist.get(0);
2823 } else {
2824 for (SubscriptionInfo item : slist) {
2825 if (item.getSubscriptionId() == defaultSubId) {
2826 info = item;
2827 break;
2828 }
2829 }
2830
2831 if (info == null) {
2832 return null;
2833 }
2834 }
2835
2836 // Try and fetch the locale from the carrier properties or from the SIM language
2837 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002838 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002839 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002840 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002841 if (defaultPhone != null) {
2842 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2843 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002844 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002845 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2846 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002847 } else {
2848 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002849 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002850 }
2851 }
2852
Narayan Kamath011676f2015-07-29 12:04:08 +01002853 // The SIM language preferences only store a language (e.g. fr = French), not an
2854 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2855 // the SIM and carrier preferences does not include a country we add the country
2856 // determined from the SIM MCC to provide an exact locale.
2857 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002858 if (mccLocale != null) {
2859 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2860 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002861 }
2862
Tony Hill183b2de2015-06-24 14:53:58 +01002863 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002864 return null;
2865 }
2866
2867 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2868 final long identity = Binder.clearCallingIdentity();
2869 try {
2870 return mSubscriptionController.getAllSubInfoList(
2871 mPhone.getContext().getOpPackageName());
2872 } finally {
2873 Binder.restoreCallingIdentity(identity);
2874 }
2875 }
2876
2877 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2878 final long identity = Binder.clearCallingIdentity();
2879 try {
2880 return mSubscriptionController.getActiveSubscriptionInfoList(
2881 mPhone.getContext().getOpPackageName());
2882 } finally {
2883 Binder.restoreCallingIdentity(identity);
2884 }
2885 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002886
2887 /**
2888 * {@hide}
2889 * Returns the modem stats
2890 */
2891 @Override
2892 public ModemActivityInfo getModemActivityInfo() {
2893 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2894 }
Jack Yu85bd38a2015-11-09 11:34:32 -08002895
2896 /**
2897 * {@hide}
2898 * Returns the service state information on specified subscription.
2899 */
2900 @Override
2901 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
2902
2903 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
2904 return null;
2905 }
2906
2907 final Phone phone = getPhone(subId);
2908 if (phone == null) {
2909 return null;
2910 }
2911
2912 return phone.getServiceState();
2913 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08002914
2915 /**
2916 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
2917 *
2918 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2919 * voicemail ringtone.
2920 * @return The URI for the ringtone to play when receiving a voicemail from a specific
2921 * PhoneAccount.
2922 */
2923 @Override
2924 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
2925 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2926 if (phone == null) {
2927 return null;
2928 }
2929
2930 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
2931 }
2932
2933 /**
2934 * Returns whether vibration is set for voicemail notification in Phone settings.
2935 *
2936 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
2937 * voicemail vibration setting.
2938 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
2939 */
2940 @Override
2941 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
2942 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
2943 if (phone == null) {
2944 return false;
2945 }
2946
2947 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
2948 }
2949
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002950}