blob: d61ef59c7737a462f433fc5f14df2ced014aa056 [file] [log] [blame]
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.phone;
18
19import android.app.ActivityManager;
20import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070021import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070022import android.content.Context;
23import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070024import android.content.SharedPreferences;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070025import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070026import android.net.Uri;
27import android.os.AsyncResult;
28import android.os.Binder;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.os.Process;
34import android.os.ServiceManager;
35import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070036import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070037import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080038import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080039import android.telecom.PhoneAccount;
Andrew Lee9431b832015-03-09 18:46:45 -070040import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070041import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070042import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070043import android.telephony.IccOpenLogicalChannelResponse;
Jake Hambye994d462014-02-03 13:10:13 -080044import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070045import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070046import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080047import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080048import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080049import android.telephony.TelephonyManager;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -070050import android.telephony.ModemActivityInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070051import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080052import android.util.ArrayMap;
53import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070054import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080055import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080056import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070057
Andrew Lee312e8172014-10-23 17:01:36 -070058import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070059import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070060import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070061import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070062import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080064import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010065import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070067import com.android.internal.telephony.Phone;
Wink Saville36469e72014-06-11 15:17:00 -070068import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070069import com.android.internal.telephony.ProxyController;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070070import com.android.internal.telephony.PhoneConstants;
Svet Ganovb320e182015-04-16 12:30:10 -070071import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080072import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070073import com.android.internal.telephony.uicc.IccIoResult;
74import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070075import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070076import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080077import com.android.internal.util.HexDump;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070078
Wink Saville36469e72014-06-11 15:17:00 -070079import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
80
Santos Cordon7d4ddf62013-07-10 11:58:08 -070081import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080082import java.util.Arrays;
Shishir Agrawal621a47c2014-12-01 10:25:09 -080083import java.util.HashMap;
84import java.util.Iterator;
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;
88import java.util.Objects;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070089
90/**
91 * Implementation of the ITelephony interface.
92 */
Santos Cordon117fee72014-05-16 17:56:12 -070093public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070094 private static final String LOG_TAG = "PhoneInterfaceManager";
95 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
96 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -080097 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098
99 // Message codes used with mMainThreadHandler
100 private static final int CMD_HANDLE_PIN_MMI = 1;
101 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
102 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
103 private static final int CMD_ANSWER_RINGING_CALL = 4;
104 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700105 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
106 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700107 private static final int CMD_OPEN_CHANNEL = 9;
108 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
109 private static final int CMD_CLOSE_CHANNEL = 11;
110 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800111 private static final int CMD_NV_READ_ITEM = 13;
112 private static final int EVENT_NV_READ_ITEM_DONE = 14;
113 private static final int CMD_NV_WRITE_ITEM = 15;
114 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
115 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
116 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
117 private static final int CMD_NV_RESET_CONFIG = 19;
118 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800119 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
120 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
121 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
122 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800123 private static final int CMD_SEND_ENVELOPE = 25;
124 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700125 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
126 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
127 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
128 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
129 private static final int CMD_EXCHANGE_SIM_IO = 31;
130 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800131 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
132 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700133 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
134 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700135 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
136 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700137 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
138 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
139 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
140 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700141
142 /** The singleton instance. */
143 private static PhoneInterfaceManager sInstance;
144
Wink Saville3ab207e2014-11-20 13:07:20 -0800145 private PhoneGlobals mApp;
146 private Phone mPhone;
147 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700148 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800149 private AppOpsManager mAppOps;
150 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800151 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800152 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700153
Derek Tan97ebb422014-09-05 16:55:38 -0700154 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
155 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800156 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Andrew Leedf14ead2014-10-17 14:22:52 -0700157 private static final String PREF_ENABLE_VIDEO_CALLING = "enable_video_calling";
Derek Tan89e89d42014-07-08 17:00:10 -0700158
159 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700160 * A request object to use for transmitting data to an ICC.
161 */
162 private static final class IccAPDUArgument {
163 public int channel, cla, command, p1, p2, p3;
164 public String data;
165
166 public IccAPDUArgument(int channel, int cla, int command,
167 int p1, int p2, int p3, String data) {
168 this.channel = channel;
169 this.cla = cla;
170 this.command = command;
171 this.p1 = p1;
172 this.p2 = p2;
173 this.p3 = p3;
174 this.data = data;
175 }
176 }
177
178 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700179 * A request object to use for transmitting data to an ICC.
180 */
181 private static final class ManualNetworkSelectionArgument {
182 public OperatorInfo operatorInfo;
183 public boolean persistSelection;
184
185 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
186 this.operatorInfo = operatorInfo;
187 this.persistSelection = persistSelection;
188 }
189 }
190
191 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700192 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
193 * request after sending. The main thread will notify the request when it is complete.
194 */
195 private static final class MainThreadRequest {
196 /** The argument to use for the request */
197 public Object argument;
198 /** The result of the request that is run on the main thread */
199 public Object result;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800200 /** The subscriber id that this request applies to. Null if default. */
201 public Integer subId;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700202
203 public MainThreadRequest(Object argument) {
204 this.argument = argument;
205 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800206
207 public MainThreadRequest(Object argument, Integer subId) {
208 this.argument = argument;
209 this.subId = subId;
210 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700211 }
212
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800213 private static final class IncomingThirdPartyCallArgs {
214 public final ComponentName component;
215 public final String callId;
216 public final String callerDisplayName;
217
218 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
219 String callerDisplayName) {
220 this.component = component;
221 this.callId = callId;
222 this.callerDisplayName = callerDisplayName;
223 }
224 }
225
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700226 /**
227 * A handler that processes messages on the main thread in the phone process. Since many
228 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
229 * inbound binder threads to the main thread in the phone process. The Binder thread
230 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
231 * on, which will be notified when the operation completes and will contain the result of the
232 * request.
233 *
234 * <p>If a MainThreadRequest object is provided in the msg.obj field,
235 * note that request.result must be set to something non-null for the calling thread to
236 * unblock.
237 */
238 private final class MainThreadHandler extends Handler {
239 @Override
240 public void handleMessage(Message msg) {
241 MainThreadRequest request;
242 Message onCompleted;
243 AsyncResult ar;
Shishir Agrawal21409252015-01-15 23:33:50 -0800244 UiccCard uiccCard = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700245 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700246
247 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700248 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700249 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700250 final Phone phone = getPhoneFromRequest(request);
251 request.result = phone != null ?
252 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
253 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700254 // Wake up the requesting thread
255 synchronized (request) {
256 request.notifyAll();
257 }
258 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700259 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260
261 case CMD_HANDLE_NEIGHBORING_CELL:
262 request = (MainThreadRequest) msg.obj;
263 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
264 request);
265 mPhone.getNeighboringCids(onCompleted);
266 break;
267
268 case EVENT_NEIGHBORING_CELL_DONE:
269 ar = (AsyncResult) msg.obj;
270 request = (MainThreadRequest) ar.userObj;
271 if (ar.exception == null && ar.result != null) {
272 request.result = ar.result;
273 } else {
274 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800275 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700276 }
277 // Wake up the requesting thread
278 synchronized (request) {
279 request.notifyAll();
280 }
281 break;
282
283 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700284 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800285 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700286 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700287 break;
288
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700289 case CMD_END_CALL:
290 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800291 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700292 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700293 Phone phone = getPhone(end_subId);
294 if (phone == null) {
295 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
296 break;
297 }
298 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700299 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
300 // CDMA: If the user presses the Power button we treat it as
301 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700302 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700303 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
304 // GSM: End the call as per the Phone state
305 hungUp = PhoneUtils.hangup(mCM);
306 } else {
307 throw new IllegalStateException("Unexpected phone type: " + phoneType);
308 }
309 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
310 request.result = hungUp;
311 // Wake up the requesting thread
312 synchronized (request) {
313 request.notifyAll();
314 }
315 break;
316
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700317 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700318 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700319 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700320 if (uiccCard == null) {
321 loge("iccTransmitApduLogicalChannel: No UICC");
322 request.result = new IccIoResult(0x6F, 0, (byte[])null);
323 synchronized (request) {
324 request.notifyAll();
325 }
326 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700327 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
328 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700329 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700330 iccArgument.channel, iccArgument.cla, iccArgument.command,
331 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700332 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700333 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700334 break;
335
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700336 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700337 ar = (AsyncResult) msg.obj;
338 request = (MainThreadRequest) ar.userObj;
339 if (ar.exception == null && ar.result != null) {
340 request.result = ar.result;
341 } else {
342 request.result = new IccIoResult(0x6F, 0, (byte[])null);
343 if (ar.result == null) {
344 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800345 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700346 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800347 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700348 } else {
349 loge("iccTransmitApduLogicalChannel: Unknown exception");
350 }
351 }
352 synchronized (request) {
353 request.notifyAll();
354 }
355 break;
356
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700357 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
358 request = (MainThreadRequest) msg.obj;
359 iccArgument = (IccAPDUArgument) request.argument;
360 if (uiccCard == null) {
361 loge("iccTransmitApduBasicChannel: No UICC");
362 request.result = new IccIoResult(0x6F, 0, (byte[])null);
363 synchronized (request) {
364 request.notifyAll();
365 }
366 } else {
367 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
368 request);
369 uiccCard.iccTransmitApduBasicChannel(
370 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
371 iccArgument.p3, iccArgument.data, onCompleted);
372 }
373 break;
374
375 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
376 ar = (AsyncResult) msg.obj;
377 request = (MainThreadRequest) ar.userObj;
378 if (ar.exception == null && ar.result != null) {
379 request.result = ar.result;
380 } else {
381 request.result = new IccIoResult(0x6F, 0, (byte[])null);
382 if (ar.result == null) {
383 loge("iccTransmitApduBasicChannel: Empty response");
384 } else if (ar.exception instanceof CommandException) {
385 loge("iccTransmitApduBasicChannel: CommandException: " +
386 ar.exception);
387 } else {
388 loge("iccTransmitApduBasicChannel: Unknown exception");
389 }
390 }
391 synchronized (request) {
392 request.notifyAll();
393 }
394 break;
395
396 case CMD_EXCHANGE_SIM_IO:
397 request = (MainThreadRequest) msg.obj;
398 iccArgument = (IccAPDUArgument) request.argument;
399 if (uiccCard == null) {
400 loge("iccExchangeSimIO: No UICC");
401 request.result = new IccIoResult(0x6F, 0, (byte[])null);
402 synchronized (request) {
403 request.notifyAll();
404 }
405 } else {
406 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
407 request);
408 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
409 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
410 iccArgument.data, onCompleted);
411 }
412 break;
413
414 case EVENT_EXCHANGE_SIM_IO_DONE:
415 ar = (AsyncResult) msg.obj;
416 request = (MainThreadRequest) ar.userObj;
417 if (ar.exception == null && ar.result != null) {
418 request.result = ar.result;
419 } else {
420 request.result = new IccIoResult(0x6f, 0, (byte[])null);
421 }
422 synchronized (request) {
423 request.notifyAll();
424 }
425 break;
426
Derek Tan4d5e5c12014-02-04 11:54:58 -0800427 case CMD_SEND_ENVELOPE:
428 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700429 if (uiccCard == null) {
430 loge("sendEnvelopeWithStatus: No UICC");
431 request.result = new IccIoResult(0x6F, 0, (byte[])null);
432 synchronized (request) {
433 request.notifyAll();
434 }
435 } else {
436 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
437 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
438 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800439 break;
440
441 case EVENT_SEND_ENVELOPE_DONE:
442 ar = (AsyncResult) msg.obj;
443 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700444 if (ar.exception == null && ar.result != null) {
445 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800446 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700447 request.result = new IccIoResult(0x6F, 0, (byte[])null);
448 if (ar.result == null) {
449 loge("sendEnvelopeWithStatus: Empty response");
450 } else if (ar.exception instanceof CommandException) {
451 loge("sendEnvelopeWithStatus: CommandException: " +
452 ar.exception);
453 } else {
454 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
455 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800456 }
457 synchronized (request) {
458 request.notifyAll();
459 }
460 break;
461
Shishir Agrawal566b7612013-10-28 14:41:00 -0700462 case CMD_OPEN_CHANNEL:
463 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700464 if (uiccCard == null) {
465 loge("iccOpenLogicalChannel: No UICC");
466 request.result = new IccIoResult(0x6F, 0, (byte[])null);
467 synchronized (request) {
468 request.notifyAll();
469 }
470 } else {
471 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
472 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
473 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700474 break;
475
476 case EVENT_OPEN_CHANNEL_DONE:
477 ar = (AsyncResult) msg.obj;
478 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700479 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700480 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700481 int[] result = (int[]) ar.result;
482 int channelId = result[0];
483 byte[] selectResponse = null;
484 if (result.length > 1) {
485 selectResponse = new byte[result.length - 1];
486 for (int i = 1; i < result.length; ++i) {
487 selectResponse[i - 1] = (byte) result[i];
488 }
489 }
490 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700491 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700493 if (ar.result == null) {
494 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700495 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700496 if (ar.exception != null) {
497 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
498 }
499
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700500 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700501 if (ar.exception instanceof CommandException) {
502 CommandException.Error error =
503 ((CommandException) (ar.exception)).getCommandError();
504 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700505 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700506 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700507 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700508 }
509 }
510 openChannelResp = new IccOpenLogicalChannelResponse(
511 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700512 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700513 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700514 synchronized (request) {
515 request.notifyAll();
516 }
517 break;
518
519 case CMD_CLOSE_CHANNEL:
520 request = (MainThreadRequest) msg.obj;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700521 if (uiccCard == null) {
522 loge("iccCloseLogicalChannel: No UICC");
523 request.result = new IccIoResult(0x6F, 0, (byte[])null);
524 synchronized (request) {
525 request.notifyAll();
526 }
527 } else {
528 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
529 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
530 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700531 break;
532
533 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800534 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
535 break;
536
537 case CMD_NV_READ_ITEM:
538 request = (MainThreadRequest) msg.obj;
539 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
540 mPhone.nvReadItem((Integer) request.argument, onCompleted);
541 break;
542
543 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 ar = (AsyncResult) msg.obj;
545 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800546 if (ar.exception == null && ar.result != null) {
547 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700548 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800549 request.result = "";
550 if (ar.result == null) {
551 loge("nvReadItem: Empty response");
552 } else if (ar.exception instanceof CommandException) {
553 loge("nvReadItem: CommandException: " +
554 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700555 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800556 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 }
558 }
559 synchronized (request) {
560 request.notifyAll();
561 }
562 break;
563
Jake Hambye994d462014-02-03 13:10:13 -0800564 case CMD_NV_WRITE_ITEM:
565 request = (MainThreadRequest) msg.obj;
566 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
567 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
568 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
569 break;
570
571 case EVENT_NV_WRITE_ITEM_DONE:
572 handleNullReturnEvent(msg, "nvWriteItem");
573 break;
574
575 case CMD_NV_WRITE_CDMA_PRL:
576 request = (MainThreadRequest) msg.obj;
577 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
578 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
579 break;
580
581 case EVENT_NV_WRITE_CDMA_PRL_DONE:
582 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
583 break;
584
585 case CMD_NV_RESET_CONFIG:
586 request = (MainThreadRequest) msg.obj;
587 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
588 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
589 break;
590
591 case EVENT_NV_RESET_CONFIG_DONE:
592 handleNullReturnEvent(msg, "nvResetConfig");
593 break;
594
Jake Hamby7c27be32014-03-03 13:25:59 -0800595 case CMD_GET_PREFERRED_NETWORK_TYPE:
596 request = (MainThreadRequest) msg.obj;
597 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700598 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800599 break;
600
601 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
602 ar = (AsyncResult) msg.obj;
603 request = (MainThreadRequest) ar.userObj;
604 if (ar.exception == null && ar.result != null) {
605 request.result = ar.result; // Integer
606 } else {
607 request.result = -1;
608 if (ar.result == null) {
609 loge("getPreferredNetworkType: Empty response");
610 } else if (ar.exception instanceof CommandException) {
611 loge("getPreferredNetworkType: CommandException: " +
612 ar.exception);
613 } else {
614 loge("getPreferredNetworkType: Unknown exception");
615 }
616 }
617 synchronized (request) {
618 request.notifyAll();
619 }
620 break;
621
622 case CMD_SET_PREFERRED_NETWORK_TYPE:
623 request = (MainThreadRequest) msg.obj;
624 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
625 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700626 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800627 break;
628
629 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
630 handleNullReturnEvent(msg, "setPreferredNetworkType");
631 break;
632
Steven Liu4bf01bc2014-07-17 11:05:29 -0500633 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
634 request = (MainThreadRequest)msg.obj;
635 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
636 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
637 break;
638
639 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
640 ar = (AsyncResult)msg.obj;
641 request = (MainThreadRequest)ar.userObj;
642 request.result = ar;
643 synchronized (request) {
644 request.notifyAll();
645 }
646 break;
647
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800648 case CMD_SET_VOICEMAIL_NUMBER:
649 request = (MainThreadRequest) msg.obj;
650 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
651 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800652 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
653 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800654 break;
655
656 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
657 handleNullReturnEvent(msg, "setVoicemailNumber");
658 break;
659
Stuart Scott54788802015-03-30 13:18:01 -0700660 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
661 request = (MainThreadRequest) msg.obj;
662 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
663 request);
664 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
665 break;
666
667 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
668 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
669 break;
670
Shishir Agrawal302c8692015-06-19 13:49:39 -0700671 case CMD_PERFORM_NETWORK_SCAN:
672 request = (MainThreadRequest) msg.obj;
673 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
674 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
675 break;
676
677 case EVENT_PERFORM_NETWORK_SCAN_DONE:
678 ar = (AsyncResult) msg.obj;
679 request = (MainThreadRequest) ar.userObj;
680 CellNetworkScanResult cellScanResult;
681 if (ar.exception == null && ar.result != null) {
682 cellScanResult = new CellNetworkScanResult(
683 CellNetworkScanResult.STATUS_SUCCESS,
684 (List<OperatorInfo>) ar.result);
685 } else {
686 if (ar.result == null) {
687 loge("getCellNetworkScanResults: Empty response");
688 }
689 if (ar.exception != null) {
690 loge("getCellNetworkScanResults: Exception: " + ar.exception);
691 }
692 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
693 if (ar.exception instanceof CommandException) {
694 CommandException.Error error =
695 ((CommandException) (ar.exception)).getCommandError();
696 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
697 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
698 } else if (error == CommandException.Error.GENERIC_FAILURE) {
699 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
700 }
701 }
702 cellScanResult = new CellNetworkScanResult(errorCode, null);
703 }
704 request.result = cellScanResult;
705 synchronized (request) {
706 request.notifyAll();
707 }
708 break;
709
710 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
711 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700712 ManualNetworkSelectionArgument selArg =
713 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700714 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
715 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700716 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
717 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700718 break;
719
720 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
721 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
722 break;
723
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700724 case CMD_GET_MODEM_ACTIVITY_INFO:
725 request = (MainThreadRequest) msg.obj;
726 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700727 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700728 break;
729
730 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
731 ar = (AsyncResult) msg.obj;
732 request = (MainThreadRequest) ar.userObj;
733 if (ar.exception == null && ar.result != null) {
734 request.result = ar.result;
735 } else {
736 if (ar.result == null) {
737 loge("queryModemActivityInfo: Empty response");
738 } else if (ar.exception instanceof CommandException) {
739 loge("queryModemActivityInfo: CommandException: " +
740 ar.exception);
741 } else {
742 loge("queryModemActivityInfo: Unknown exception");
743 }
744 }
745 synchronized (request) {
746 request.notifyAll();
747 }
748 break;
749
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700750 default:
751 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
752 break;
753 }
754 }
Jake Hambye994d462014-02-03 13:10:13 -0800755
756 private void handleNullReturnEvent(Message msg, String command) {
757 AsyncResult ar = (AsyncResult) msg.obj;
758 MainThreadRequest request = (MainThreadRequest) ar.userObj;
759 if (ar.exception == null) {
760 request.result = true;
761 } else {
762 request.result = false;
763 if (ar.exception instanceof CommandException) {
764 loge(command + ": CommandException: " + ar.exception);
765 } else {
766 loge(command + ": Unknown exception");
767 }
768 }
769 synchronized (request) {
770 request.notifyAll();
771 }
772 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700773 }
774
775 /**
776 * Posts the specified command to be executed on the main thread,
777 * waits for the request to complete, and returns the result.
778 * @see #sendRequestAsync
779 */
780 private Object sendRequest(int command, Object argument) {
Santos Cordon500b0e02014-06-17 10:33:33 -0700781 return sendRequest(command, argument, null);
Wink Saville36469e72014-06-11 15:17:00 -0700782 }
783
784 /**
785 * Posts the specified command to be executed on the main thread,
786 * waits for the request to complete, and returns the result.
787 * @see #sendRequestAsync
788 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800789 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
791 throw new RuntimeException("This method will deadlock if called from the main thread.");
792 }
793
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800794 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700795 Message msg = mMainThreadHandler.obtainMessage(command, request);
796 msg.sendToTarget();
797
798 // Wait for the request to complete
799 synchronized (request) {
800 while (request.result == null) {
801 try {
802 request.wait();
803 } catch (InterruptedException e) {
804 // Do nothing, go back and wait until the request is complete
805 }
806 }
807 }
808 return request.result;
809 }
810
811 /**
812 * Asynchronous ("fire and forget") version of sendRequest():
813 * Posts the specified command to be executed on the main thread, and
814 * returns immediately.
815 * @see #sendRequest
816 */
817 private void sendRequestAsync(int command) {
818 mMainThreadHandler.sendEmptyMessage(command);
819 }
820
821 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700822 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
823 * @see {@link #sendRequest(int,Object)}
824 */
825 private void sendRequestAsync(int command, Object argument) {
826 MainThreadRequest request = new MainThreadRequest(argument);
827 Message msg = mMainThreadHandler.obtainMessage(command, request);
828 msg.sendToTarget();
829 }
830
831 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700832 * Initialize the singleton PhoneInterfaceManager instance.
833 * This is only done once, at startup, from PhoneApp.onCreate().
834 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700835 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700836 synchronized (PhoneInterfaceManager.class) {
837 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700838 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700839 } else {
840 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
841 }
842 return sInstance;
843 }
844 }
845
846 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700847 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700848 mApp = app;
849 mPhone = phone;
850 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700851 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700852 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
853 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700854 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700855 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800856 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800857
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700858 publish();
859 }
860
861 private void publish() {
862 if (DBG) log("publish: " + this);
863
864 ServiceManager.addService("phone", this);
865 }
866
Stuart Scott584921c2015-01-15 17:10:34 -0800867 private Phone getPhoneFromRequest(MainThreadRequest request) {
868 return (request.subId == null) ? mPhone : getPhone(request.subId);
869 }
870
Wink Saville36469e72014-06-11 15:17:00 -0700871 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700872 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800873 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700874 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 //
876 // Implementation of the ITelephony interface.
877 //
878
879 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700880 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700881 }
882
Wink Savilleb564aae2014-10-23 10:18:09 -0700883 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700884 if (DBG) log("dial: " + number);
885 // No permission check needed here: This is just a wrapper around the
886 // ACTION_DIAL intent, which is available to any app since it puts up
887 // the UI before it does anything.
888
889 String url = createTelUrl(number);
890 if (url == null) {
891 return;
892 }
893
894 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700895 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700896 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
897 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
898 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
899 mApp.startActivity(intent);
900 }
901 }
902
903 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700904 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700905 }
906
Wink Savilleb564aae2014-10-23 10:18:09 -0700907 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 if (DBG) log("call: " + number);
909
910 // This is just a wrapper around the ACTION_CALL intent, but we still
911 // need to do a permission check since we're calling startActivity()
912 // from the context of the phone app.
913 enforceCallPermission();
914
915 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
916 != AppOpsManager.MODE_ALLOWED) {
917 return;
918 }
919
920 String url = createTelUrl(number);
921 if (url == null) {
922 return;
923 }
924
Wink Saville08874612014-08-31 19:19:58 -0700925 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100926 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800927 if (slist != null) {
928 for (SubscriptionInfo subInfoRecord : slist) {
929 if (subInfoRecord.getSubscriptionId() == subId) {
930 isValid = true;
931 break;
932 }
Wink Saville08874612014-08-31 19:19:58 -0700933 }
934 }
935 if (isValid == false) {
936 return;
937 }
938
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700939 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700940 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700941 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
942 mApp.startActivity(intent);
943 }
944
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700945 /**
946 * End a call based on call state
947 * @return true is a call was ended
948 */
949 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700950 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700951 }
952
953 /**
954 * End a call based on the call state of the subId
955 * @return true is a call was ended
956 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700957 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700958 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800959 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700960 }
961
962 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700963 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700964 }
965
Wink Savilleb564aae2014-10-23 10:18:09 -0700966 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700967 if (DBG) log("answerRingingCall...");
968 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
969 // but that can probably wait till the big TelephonyManager API overhaul.
970 // For now, protect this call with the MODIFY_PHONE_STATE permission.
971 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800972 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700973 }
974
975 /**
976 * Make the actual telephony calls to implement answerRingingCall().
977 * This should only be called from the main thread of the Phone app.
978 * @see #answerRingingCall
979 *
980 * TODO: it would be nice to return true if we answered the call, or
981 * false if there wasn't actually a ringing incoming call, or some
982 * other error occurred. (In other words, pass back the return value
983 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
984 * But that would require calling this method via sendRequest() rather
985 * than sendRequestAsync(), and right now we don't actually *need* that
986 * return value, so let's just return void for now.
987 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700988 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -0700989 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700990 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -0700991 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
992 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700993 if (hasActiveCall && hasHoldingCall) {
994 // Both lines are in use!
995 // TODO: provide a flag to let the caller specify what
996 // policy to use if both lines are in use. (The current
997 // behavior is hardwired to "answer incoming, end ongoing",
998 // which is how the CALL button is specced to behave.)
999 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1000 return;
1001 } else {
1002 // answerCall() will automatically hold the current active
1003 // call, if there is one.
1004 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1005 return;
1006 }
1007 } else {
1008 // No call was ringing.
1009 return;
1010 }
1011 }
1012
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001013 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001014 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001015 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001016 public void silenceRinger() {
1017 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001018 }
1019
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001020 @Override
1021 public boolean isOffhook(String callingPackage) {
1022 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001023 }
1024
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001025 @Override
1026 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1027 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1028 return false;
1029 }
1030
Sanket Padawe356d7632015-06-22 14:03:32 -07001031 final Phone phone = getPhone(subId);
1032 if (phone != null) {
1033 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1034 } else {
1035 return false;
1036 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 }
1038
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001039 @Override
1040 public boolean isRinging(String callingPackage) {
1041 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001042 }
1043
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001044 @Override
1045 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1046 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1047 return false;
1048 }
1049
Sanket Padawe356d7632015-06-22 14:03:32 -07001050 final Phone phone = getPhone(subId);
1051 if (phone != null) {
1052 return (phone.getState() == PhoneConstants.State.RINGING);
1053 } else {
1054 return false;
1055 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001056 }
1057
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001058 @Override
1059 public boolean isIdle(String callingPackage) {
1060 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001061 }
1062
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001063 @Override
1064 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1065 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1066 return false;
1067 }
1068
Sanket Padawe356d7632015-06-22 14:03:32 -07001069 final Phone phone = getPhone(subId);
1070 if (phone != null) {
1071 return (phone.getState() == PhoneConstants.State.IDLE);
1072 } else {
1073 return false;
1074 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001075 }
1076
Svet Ganovb320e182015-04-16 12:30:10 -07001077 public boolean isSimPinEnabled(String callingPackage) {
1078 if (!canReadPhoneState(callingPackage, "isSimPinEnabled")) {
1079 return false;
1080 }
1081
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001082 return (PhoneGlobals.getInstance().isSimPinEnabled());
1083 }
1084
1085 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001086 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001087 }
1088
Wink Savilleb564aae2014-10-23 10:18:09 -07001089 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001090 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001091 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1092 }
1093
1094 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001095 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001096 }
1097
Wink Savilleb564aae2014-10-23 10:18:09 -07001098 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001099 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001100 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1101 }
1102
1103 /** {@hide} */
1104 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001105 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001106 }
1107
Wink Savilleb564aae2014-10-23 10:18:09 -07001108 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001109 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001110 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001111 checkSimPin.start();
1112 return checkSimPin.unlockSim(null, pin);
1113 }
1114
Wink Saville9de0f752013-10-22 19:04:03 -07001115 /** {@hide} */
1116 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001117 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001118 }
1119
Wink Savilleb564aae2014-10-23 10:18:09 -07001120 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001121 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001122 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001123 checkSimPuk.start();
1124 return checkSimPuk.unlockSim(puk, pin);
1125 }
1126
1127 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001128 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001129 * a synchronous one.
1130 */
1131 private static class UnlockSim extends Thread {
1132
1133 private final IccCard mSimCard;
1134
1135 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001136 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1137 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001138
1139 // For replies from SimCard interface
1140 private Handler mHandler;
1141
1142 // For async handler to identify request type
1143 private static final int SUPPLY_PIN_COMPLETE = 100;
1144
1145 public UnlockSim(IccCard simCard) {
1146 mSimCard = simCard;
1147 }
1148
1149 @Override
1150 public void run() {
1151 Looper.prepare();
1152 synchronized (UnlockSim.this) {
1153 mHandler = new Handler() {
1154 @Override
1155 public void handleMessage(Message msg) {
1156 AsyncResult ar = (AsyncResult) msg.obj;
1157 switch (msg.what) {
1158 case SUPPLY_PIN_COMPLETE:
1159 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1160 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001161 mRetryCount = msg.arg1;
1162 if (ar.exception != null) {
1163 if (ar.exception instanceof CommandException &&
1164 ((CommandException)(ar.exception)).getCommandError()
1165 == CommandException.Error.PASSWORD_INCORRECT) {
1166 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1167 } else {
1168 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1169 }
1170 } else {
1171 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1172 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001173 mDone = true;
1174 UnlockSim.this.notifyAll();
1175 }
1176 break;
1177 }
1178 }
1179 };
1180 UnlockSim.this.notifyAll();
1181 }
1182 Looper.loop();
1183 }
1184
1185 /*
1186 * Use PIN or PUK to unlock SIM card
1187 *
1188 * If PUK is null, unlock SIM card with PIN
1189 *
1190 * If PUK is not null, unlock SIM card with PUK and set PIN code
1191 */
Wink Saville9de0f752013-10-22 19:04:03 -07001192 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001193
1194 while (mHandler == null) {
1195 try {
1196 wait();
1197 } catch (InterruptedException e) {
1198 Thread.currentThread().interrupt();
1199 }
1200 }
1201 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1202
1203 if (puk == null) {
1204 mSimCard.supplyPin(pin, callback);
1205 } else {
1206 mSimCard.supplyPuk(puk, pin, callback);
1207 }
1208
1209 while (!mDone) {
1210 try {
1211 Log.d(LOG_TAG, "wait for done");
1212 wait();
1213 } catch (InterruptedException e) {
1214 // Restore the interrupted status
1215 Thread.currentThread().interrupt();
1216 }
1217 }
1218 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001219 int[] resultArray = new int[2];
1220 resultArray[0] = mResult;
1221 resultArray[1] = mRetryCount;
1222 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001223 }
1224 }
1225
1226 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001227 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001228
1229 }
1230
Wink Savilleb564aae2014-10-23 10:18:09 -07001231 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001232 // No permission check needed here: this call is harmless, and it's
1233 // needed for the ServiceState.requestStateUpdate() call (which is
1234 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001235 final Phone phone = getPhone(subId);
1236 if (phone != null) {
1237 phone.updateServiceLocation();
1238 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239 }
1240
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001241 @Override
1242 public boolean isRadioOn(String callingPackage) {
1243 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001244 }
1245
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001246 @Override
1247 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1248 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1249 return false;
1250 }
1251 return isRadioOnForSubscriber(subId);
1252 }
1253
1254 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001255 final Phone phone = getPhone(subId);
1256 if (phone != null) {
1257 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1258 } else {
1259 return false;
1260 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001261 }
1262
1263 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001264 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001265
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001266 }
Wink Saville36469e72014-06-11 15:17:00 -07001267
Wink Savilleb564aae2014-10-23 10:18:09 -07001268 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001269 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001270 final Phone phone = getPhone(subId);
1271 if (phone != null) {
1272 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1273 }
Wink Saville36469e72014-06-11 15:17:00 -07001274 }
1275
1276 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001277 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001278 }
1279
Wink Savilleb564aae2014-10-23 10:18:09 -07001280 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001281 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001282 final Phone phone = getPhone(subId);
1283 if (phone == null) {
1284 return false;
1285 }
1286 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001287 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001288 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001289 }
1290 return true;
1291 }
Wink Saville36469e72014-06-11 15:17:00 -07001292
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001293 public boolean needMobileRadioShutdown() {
1294 /*
1295 * If any of the Radios are available, it will need to be
1296 * shutdown. So return true if any Radio is available.
1297 */
1298 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1299 Phone phone = PhoneFactory.getPhone(i);
1300 if (phone != null && phone.isRadioAvailable()) return true;
1301 }
1302 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1303 return false;
1304 }
1305
1306 public void shutdownMobileRadios() {
1307 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1308 logv("Shutting down Phone " + i);
1309 shutdownRadioUsingPhoneId(i);
1310 }
1311 }
1312
1313 private void shutdownRadioUsingPhoneId(int phoneId) {
1314 enforceModifyPermission();
1315 Phone phone = PhoneFactory.getPhone(phoneId);
1316 if (phone != null && phone.isRadioAvailable()) {
1317 phone.shutdownRadio();
1318 }
1319 }
1320
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001321 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001322 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001323 }
1324
Wink Savilleb564aae2014-10-23 10:18:09 -07001325 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001326 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001327 final Phone phone = getPhone(subId);
1328 if (phone != null) {
1329 phone.setRadioPower(turnOn);
1330 return true;
1331 } else {
1332 return false;
1333 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001334 }
1335
Wink Saville36469e72014-06-11 15:17:00 -07001336 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001337 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001338 public boolean enableDataConnectivity() {
1339 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001340 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001341 final Phone phone = getPhone(subId);
1342 if (phone != null) {
1343 phone.setDataEnabled(true);
1344 return true;
1345 } else {
1346 return false;
1347 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001348 }
1349
Wink Saville36469e72014-06-11 15:17:00 -07001350 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001351 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001352 public boolean disableDataConnectivity() {
1353 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001354 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001355 final Phone phone = getPhone(subId);
1356 if (phone != null) {
1357 phone.setDataEnabled(false);
1358 return true;
1359 } else {
1360 return false;
1361 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001362 }
1363
Wink Saville36469e72014-06-11 15:17:00 -07001364 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001365 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001366 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001367 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001368 final Phone phone = getPhone(subId);
1369 if (phone != null) {
1370 return phone.isDataConnectivityPossible();
1371 } else {
1372 return false;
1373 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001374 }
1375
1376 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001377 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001378 }
1379
Wink Savilleb564aae2014-10-23 10:18:09 -07001380 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001381 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001382 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1383 return false;
1384 }
Wink Saville36469e72014-06-11 15:17:00 -07001385 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001386 }
1387
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001388 public int getCallState() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001389 return getCallStateForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001390 }
1391
Wink Savilleb564aae2014-10-23 10:18:09 -07001392 public int getCallStateForSubscriber(int subId) {
Wink Saville36469e72014-06-11 15:17:00 -07001393 return DefaultPhoneNotifier.convertCallState(getPhone(subId).getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001394 }
1395
Sanket Padawe356d7632015-06-22 14:03:32 -07001396 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001397 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001398 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001399 if (phone != null) {
1400 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1401 } else {
1402 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1403 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 }
1405
Sanket Padawe356d7632015-06-22 14:03:32 -07001406 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001407 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001408 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001409 if (phone != null) {
1410 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1411 } else {
1412 return TelephonyManager.DATA_ACTIVITY_NONE;
1413 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001414 }
1415
1416 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001417 public Bundle getCellLocation(String callingPackage) {
1418 enforceFineOrCoarseLocationPermission("getCellLocation");
1419
1420 // OP_COARSE_LOCATION controls both fine and coarse location.
1421 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1422 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001423 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001424 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 }
1426
Jake Hambye994d462014-02-03 13:10:13 -08001427 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001428 if (DBG_LOC) log("getCellLocation: is active user");
1429 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001430 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001431 if (phone == null) {
1432 return null;
1433 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001434 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001435 return data;
1436 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001437 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001438 return null;
1439 }
1440 }
1441
Svetoslav64fad262015-04-14 14:35:21 -07001442 private void enforceFineOrCoarseLocationPermission(String message) {
1443 try {
1444 mApp.enforceCallingOrSelfPermission(
1445 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1446 } catch (SecurityException e) {
1447 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1448 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1449 // is the weaker precondition
1450 mApp.enforceCallingOrSelfPermission(
1451 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1452 }
1453 }
1454
1455
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001456 @Override
1457 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001458 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001459 }
1460
Sanket Padawe356d7632015-06-22 14:03:32 -07001461 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001462 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001463 mApp.enforceCallingOrSelfPermission(
1464 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001465 final Phone phone = getPhone(subId);
1466 if (phone != null) {
1467 phone.enableLocationUpdates();
1468 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001469 }
1470
1471 @Override
1472 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001473 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001474 }
1475
Sanket Padawe356d7632015-06-22 14:03:32 -07001476 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001477 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001478 mApp.enforceCallingOrSelfPermission(
1479 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001480 final Phone phone = getPhone(subId);
1481 if (phone != null) {
1482 phone.disableLocationUpdates();
1483 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001484 }
1485
1486 @Override
1487 @SuppressWarnings("unchecked")
1488 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001489 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1490
1491 // OP_COARSE_LOCATION controls both fine and coarse location.
1492 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1493 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1494 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001495 }
1496
1497 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1498 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1499 return null;
1500 }
Svetoslav64fad262015-04-14 14:35:21 -07001501
Jake Hambye994d462014-02-03 13:10:13 -08001502 if (checkIfCallerIsSelfOrForegroundUser()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1504
1505 ArrayList<NeighboringCellInfo> cells = null;
1506
1507 try {
1508 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Wink Saville36469e72014-06-11 15:17:00 -07001509 CMD_HANDLE_NEIGHBORING_CELL, null, null);
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")) {
Svet Ganovb320e182015-04-16 12:30:10 -07002463 return null;
2464 }
Derek Tan97ebb422014-09-05 16:55:38 -07002465
2466 String iccId = getIccId(subId);
2467 if (iccId != null) {
2468 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002469 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002470 }
Derek Tan97ebb422014-09-05 16:55:38 -07002471 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002472 }
2473
2474 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002475 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2476 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2477 return null;
2478 }
Derek Tan97ebb422014-09-05 16:55:38 -07002479
2480 String iccId = getIccId(subId);
2481 if (iccId != null) {
2482 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002483 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002484 }
Derek Tan97ebb422014-09-05 16:55:38 -07002485 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002486 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002487
2488 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002489 public String[] getMergedSubscriberIds(String callingPackage) {
2490 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2491 return null;
2492 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002493 final Context context = mPhone.getContext();
2494 final TelephonyManager tele = TelephonyManager.from(context);
2495 final SubscriptionManager sub = SubscriptionManager.from(context);
2496
2497 // Figure out what subscribers are currently active
2498 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002499 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2500 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2501 final long identity = Binder.clearCallingIdentity();
2502 try {
2503 final int[] subIds = sub.getActiveSubscriptionIdList();
2504 for (int subId : subIds) {
2505 activeSubscriberIds.add(tele.getSubscriberId(subId));
2506 }
2507 } finally {
2508 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002509 }
2510
2511 // First pass, find a number override for an active subscriber
2512 String mergeNumber = null;
2513 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2514 for (String key : prefs.keySet()) {
2515 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2516 final String subscriberId = (String) prefs.get(key);
2517 if (activeSubscriberIds.contains(subscriberId)) {
2518 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2519 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2520 mergeNumber = (String) prefs.get(numberKey);
2521 if (DBG_MERGE) {
2522 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2523 + " for active subscriber " + subscriberId);
2524 }
2525 if (!TextUtils.isEmpty(mergeNumber)) {
2526 break;
2527 }
2528 }
2529 }
2530 }
2531
2532 // Shortcut when no active merged subscribers
2533 if (TextUtils.isEmpty(mergeNumber)) {
2534 return null;
2535 }
2536
2537 // Second pass, find all subscribers under that line override
2538 final ArraySet<String> result = new ArraySet<>();
2539 for (String key : prefs.keySet()) {
2540 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2541 final String number = (String) prefs.get(key);
2542 if (mergeNumber.equals(number)) {
2543 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2544 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2545 final String subscriberId = (String) prefs.get(subscriberKey);
2546 if (!TextUtils.isEmpty(subscriberId)) {
2547 result.add(subscriberId);
2548 }
2549 }
2550 }
2551 }
2552
2553 final String[] resultArray = result.toArray(new String[result.size()]);
2554 Arrays.sort(resultArray);
2555 if (DBG_MERGE) {
2556 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2557 }
2558 return resultArray;
2559 }
2560
2561 @Override
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002562 public boolean setOperatorBrandOverride(String brand) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002563 enforceCarrierPrivilege();
Shishir Agrawala3dfd752014-09-04 13:25:42 -07002564 return mPhone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002565 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002566
2567 @Override
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002568 public boolean setRoamingOverride(List<String> gsmRoamingList,
2569 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2570 List<String> cdmaNonRoamingList) {
2571 enforceCarrierPrivilege();
2572 return mPhone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
2573 cdmaNonRoamingList);
2574 }
2575
2576 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002577 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2578 enforceModifyPermission();
2579
2580 int returnValue = 0;
2581 try {
2582 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2583 if(result.exception == null) {
2584 if (result.result != null) {
2585 byte[] responseData = (byte[])(result.result);
2586 if(responseData.length > oemResp.length) {
2587 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2588 responseData.length + "bytes. Buffer Size is " +
2589 oemResp.length + "bytes.");
2590 }
2591 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2592 returnValue = responseData.length;
2593 }
2594 } else {
2595 CommandException ex = (CommandException) result.exception;
2596 returnValue = ex.getCommandError().ordinal();
2597 if(returnValue > 0) returnValue *= -1;
2598 }
2599 } catch (RuntimeException e) {
2600 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2601 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2602 if(returnValue > 0) returnValue *= -1;
2603 }
2604
2605 return returnValue;
2606 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002607
2608 @Override
2609 public void setRadioCapability(RadioAccessFamily[] rafs) {
2610 try {
2611 ProxyController.getInstance().setRadioCapability(rafs);
2612 } catch (RuntimeException e) {
2613 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2614 }
2615 }
2616
2617 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002618 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2619 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2620 return RadioAccessFamily.RAF_UNKNOWN;
2621 }
2622
Wink Saville5d475dd2014-10-17 15:00:58 -07002623 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2624 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002625
2626 @Override
2627 public void enableVideoCalling(boolean enable) {
2628 enforceModifyPermission();
2629 SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2630 editor.putBoolean(PREF_ENABLE_VIDEO_CALLING, enable);
2631 editor.commit();
2632 }
2633
2634 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002635 public boolean isVideoCallingEnabled(String callingPackage) {
2636 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2637 return false;
2638 }
2639
Andrew Lee77527ac2014-10-21 16:57:39 -07002640 // Check the user preference and the system-level IMS setting. Even if the user has
2641 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2642 // In the long run, we may instead need to check if there exists a connection service
2643 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002644 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2645 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
2646 && mTelephonySharedPreferences.getBoolean(PREF_ENABLE_VIDEO_CALLING, true);
Andrew Leedf14ead2014-10-17 14:22:52 -07002647 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002648
Andrew Leea1239f22015-03-02 17:44:07 -08002649 @Override
2650 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002651 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002652 }
2653
2654 @Override
2655 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002656 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002657 }
2658
Andrew Lee9431b832015-03-09 18:46:45 -07002659 @Override
2660 public boolean isTtyModeSupported() {
2661 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2662 TelephonyManager telephonyManager =
2663 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2664 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2665 }
2666
2667 @Override
2668 public boolean isHearingAidCompatibilitySupported() {
2669 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2670 }
2671
Sanket Padawe7310cc72015-01-14 09:53:20 -08002672 /**
2673 * Returns the unique device ID of phone, for example, the IMEI for
2674 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2675 *
2676 * <p>Requires Permission:
2677 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2678 */
2679 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002680 public String getDeviceId(String callingPackage) {
2681 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2682 return null;
2683 }
2684
Sanket Padawe7310cc72015-01-14 09:53:20 -08002685 final Phone phone = PhoneFactory.getPhone(0);
2686 if (phone != null) {
2687 return phone.getDeviceId();
2688 } else {
2689 return null;
2690 }
2691 }
2692
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002693 /*
2694 * {@hide}
2695 * Returns the IMS Registration Status
2696 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002697 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002698 public boolean isImsRegistered() {
2699 return mPhone.isImsRegistered();
2700 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002701
2702 @Override
2703 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2704 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2705 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002706
Nathan Haroldc55097a2015-03-11 18:14:50 -07002707 /*
2708 * {@hide}
2709 * Returns the IMS Registration Status
2710 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002711 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002712 return mPhone.isWifiCallingEnabled();
2713 }
2714
2715 /*
2716 * {@hide}
2717 * Returns the IMS Registration Status
2718 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002719 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002720 return mPhone.isVolteEnabled();
2721 }
Svet Ganovb320e182015-04-16 12:30:10 -07002722
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002723 /*
2724 * {@hide} Returns the IMS Registration Status
2725 */
2726 public boolean isVideoTelephonyAvailable() {
2727 return mPhone.isVideoEnabled();
2728 }
2729
Svet Ganovb320e182015-04-16 12:30:10 -07002730 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002731 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002732 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002733 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2734
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002735 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002736 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002737 } catch (SecurityException e) {
2738 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2739 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002740 }
Svet Ganovb320e182015-04-16 12:30:10 -07002741
2742 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2743 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2744 return false;
2745 }
2746
2747 return true;
2748 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002749
Makoto Onukifee69342015-06-29 14:44:50 -07002750 /**
2751 * Besides READ_PHONE_STATE, WRITE_SMS also allows apps to get phone numbers.
2752 */
2753 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002754 // Default SMS app can always read it.
2755 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2756 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2757 return true;
2758 }
2759 try {
2760 return canReadPhoneState(callingPackage, message);
2761 } catch (SecurityException e) {
2762 // Can be read with READ_SMS too.
2763 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2764 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2765 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2766 }
Makoto Onukifee69342015-06-29 14:44:50 -07002767 }
2768
Stuart Scott8eef64f2015-04-08 15:13:54 -07002769 @Override
2770 public void factoryReset(int subId) {
2771 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002772 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2773 return;
2774 }
2775
Svet Ganovcc087f82015-05-12 20:35:54 -07002776 final long identity = Binder.clearCallingIdentity();
2777 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002778 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2779 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002780 // Enable data
2781 setDataEnabled(subId, true);
2782 // Set network selection mode to automatic
2783 setNetworkSelectionModeAutomatic(subId);
2784 // Set preferred mobile network type to the best available
2785 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2786 // Turn off roaming
2787 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2788 }
2789 } finally {
2790 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002791 }
2792 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002793
2794 @Override
2795 public String getLocaleFromDefaultSim() {
2796 // We query all subscriptions instead of just the active ones, because
2797 // this might be called early on in the provisioning flow when the
2798 // subscriptions potentially aren't active yet.
2799 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2800 if (slist == null || slist.isEmpty()) {
2801 return null;
2802 }
2803
2804 // This function may be called very early, say, from the setup wizard, at
2805 // which point we won't have a default subscription set. If that's the case
2806 // we just choose the first, which will be valid in "most cases".
2807 final int defaultSubId = getDefaultSubscription();
2808 SubscriptionInfo info = null;
2809 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2810 info = slist.get(0);
2811 } else {
2812 for (SubscriptionInfo item : slist) {
2813 if (item.getSubscriptionId() == defaultSubId) {
2814 info = item;
2815 break;
2816 }
2817 }
2818
2819 if (info == null) {
2820 return null;
2821 }
2822 }
2823
2824 // Try and fetch the locale from the carrier properties or from the SIM language
2825 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002826 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002827 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002828 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002829 if (defaultPhone != null) {
2830 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2831 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002832 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002833 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2834 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002835 } else {
2836 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002837 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002838 }
2839 }
2840
Narayan Kamath011676f2015-07-29 12:04:08 +01002841 // The SIM language preferences only store a language (e.g. fr = French), not an
2842 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
2843 // the SIM and carrier preferences does not include a country we add the country
2844 // determined from the SIM MCC to provide an exact locale.
2845 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01002846 if (mccLocale != null) {
2847 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
2848 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002849 }
2850
Tony Hill183b2de2015-06-24 14:53:58 +01002851 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01002852 return null;
2853 }
2854
2855 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
2856 final long identity = Binder.clearCallingIdentity();
2857 try {
2858 return mSubscriptionController.getAllSubInfoList(
2859 mPhone.getContext().getOpPackageName());
2860 } finally {
2861 Binder.restoreCallingIdentity(identity);
2862 }
2863 }
2864
2865 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
2866 final long identity = Binder.clearCallingIdentity();
2867 try {
2868 return mSubscriptionController.getActiveSubscriptionInfoList(
2869 mPhone.getContext().getOpPackageName());
2870 } finally {
2871 Binder.restoreCallingIdentity(identity);
2872 }
2873 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07002874
2875 /**
2876 * {@hide}
2877 * Returns the modem stats
2878 */
2879 @Override
2880 public ModemActivityInfo getModemActivityInfo() {
2881 return (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
2882 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002883}