blob: 68ab93703e6717878dcf81620b1527df4e0d194d [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
Ta-wei Yen87c49842016-05-13 21:19:52 -070019import static com.android.internal.telephony.PhoneConstants.SUBSCRIPTION_KEY;
20
Santos Cordon7d4ddf62013-07-10 11:58:08 -070021import android.app.ActivityManager;
22import android.app.AppOpsManager;
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -070023import android.content.ComponentName;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070024import android.content.Context;
25import android.content.Intent;
Derek Tan97ebb422014-09-05 16:55:38 -070026import android.content.SharedPreferences;
Amith Yamasani6e118872016-02-19 12:53:51 -080027import android.content.pm.PackageInfo;
Shishir Agrawal60f9c952014-06-23 12:00:43 -070028import android.content.pm.PackageManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070029import android.net.Uri;
30import android.os.AsyncResult;
31import android.os.Binder;
32import android.os.Bundle;
33import android.os.Handler;
34import android.os.Looper;
35import android.os.Message;
36import android.os.Process;
Adam Lesinski903a54c2016-04-11 14:49:52 -070037import android.os.ResultReceiver;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070038import android.os.ServiceManager;
39import android.os.UserHandle;
Stuart Scott981d8582015-04-21 14:09:50 -070040import android.os.UserManager;
Derek Tan97ebb422014-09-05 16:55:38 -070041import android.preference.PreferenceManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080042import android.provider.Settings;
Santos Cordon7a1885b2015-02-03 11:15:19 -080043import android.telecom.PhoneAccount;
Nancy Chen31f9ba12016-01-06 11:42:12 -080044import android.telecom.PhoneAccountHandle;
Andrew Lee9431b832015-03-09 18:46:45 -070045import android.telecom.TelecomManager;
Junda Liu12f7d802015-05-01 12:06:44 -070046import android.telephony.CarrierConfigManager;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070047import android.telephony.CellInfo;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -070048import android.telephony.IccOpenLogicalChannelResponse;
Ta-wei Yen87c49842016-05-13 21:19:52 -070049import android.telephony.ModemActivityInfo;
Jake Hambye994d462014-02-03 13:10:13 -080050import android.telephony.NeighboringCellInfo;
Wink Saville5d475dd2014-10-17 15:00:58 -070051import android.telephony.RadioAccessFamily;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070052import android.telephony.ServiceState;
Wink Saville0f3b5fc2014-11-11 08:40:49 -080053import android.telephony.SubscriptionInfo;
Jeff Sharkey85190e62014-12-05 09:40:12 -080054import android.telephony.SubscriptionManager;
Ihab Awadf2177b72013-11-25 13:33:23 -080055import android.telephony.TelephonyManager;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070056import android.telephony.ModemActivityInfo;
57import android.telephony.TelephonyHistogram;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070058import android.text.TextUtils;
Jeff Sharkey85190e62014-12-05 09:40:12 -080059import android.util.ArraySet;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070060import android.util.Log;
Jake Hambye994d462014-02-03 13:10:13 -080061import android.util.Pair;
Jeff Sharkey85190e62014-12-05 09:40:12 -080062import android.util.Slog;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070063
Andrew Lee312e8172014-10-23 17:01:36 -070064import com.android.ims.ImsManager;
Shishir Agrawal566b7612013-10-28 14:41:00 -070065import com.android.internal.telephony.CallManager;
Shishir Agrawal302c8692015-06-19 13:49:39 -070066import com.android.internal.telephony.CellNetworkScanResult;
Shishir Agrawal566b7612013-10-28 14:41:00 -070067import com.android.internal.telephony.CommandException;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070068import com.android.internal.telephony.DefaultPhoneNotifier;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070069import com.android.internal.telephony.ITelephony;
Jake Hambye994d462014-02-03 13:10:13 -080070import com.android.internal.telephony.IccCard;
Narayan Kamath1c496c22015-04-16 14:40:19 +010071import com.android.internal.telephony.MccTable;
Shishir Agrawal302c8692015-06-19 13:49:39 -070072import com.android.internal.telephony.OperatorInfo;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070073import com.android.internal.telephony.Phone;
Ta-wei Yen87c49842016-05-13 21:19:52 -070074import com.android.internal.telephony.PhoneConstants;
Wink Saville36469e72014-06-11 15:17:00 -070075import com.android.internal.telephony.PhoneFactory;
Wink Saville5d475dd2014-10-17 15:00:58 -070076import com.android.internal.telephony.ProxyController;
Sanket Padawe99ef1e32016-05-18 16:12:33 -070077import com.android.internal.telephony.PhoneConstants;
78import com.android.internal.telephony.RIL;
Svet Ganovb320e182015-04-16 12:30:10 -070079import com.android.internal.telephony.RILConstants;
Wink Savilleac1bdfd2014-11-20 23:04:44 -080080import com.android.internal.telephony.SubscriptionController;
Shishir Agrawal566b7612013-10-28 14:41:00 -070081import com.android.internal.telephony.uicc.IccIoResult;
82import com.android.internal.telephony.uicc.IccUtils;
Shishir Agrawaleb8771e2014-07-22 11:24:08 -070083import com.android.internal.telephony.uicc.UiccCard;
Shishir Agrawal566b7612013-10-28 14:41:00 -070084import com.android.internal.telephony.uicc.UiccController;
Jake Hambye994d462014-02-03 13:10:13 -080085import com.android.internal.util.HexDump;
Nancy Chen31f9ba12016-01-06 11:42:12 -080086import com.android.phone.settings.VoicemailNotificationSettingsUtil;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070087
Santos Cordon7d4ddf62013-07-10 11:58:08 -070088import java.util.ArrayList;
Jeff Sharkey85190e62014-12-05 09:40:12 -080089import java.util.Arrays;
Jake Hambye994d462014-02-03 13:10:13 -080090import java.util.List;
Narayan Kamath1c496c22015-04-16 14:40:19 +010091import java.util.Locale;
Jeff Sharkey85190e62014-12-05 09:40:12 -080092import java.util.Map;
Santos Cordon7d4ddf62013-07-10 11:58:08 -070093
94/**
95 * Implementation of the ITelephony interface.
96 */
Santos Cordon117fee72014-05-16 17:56:12 -070097public class PhoneInterfaceManager extends ITelephony.Stub {
Santos Cordon7d4ddf62013-07-10 11:58:08 -070098 private static final String LOG_TAG = "PhoneInterfaceManager";
99 private static final boolean DBG = (PhoneGlobals.DBG_LEVEL >= 2);
100 private static final boolean DBG_LOC = false;
Jeff Sharkey85190e62014-12-05 09:40:12 -0800101 private static final boolean DBG_MERGE = false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700102
103 // Message codes used with mMainThreadHandler
104 private static final int CMD_HANDLE_PIN_MMI = 1;
105 private static final int CMD_HANDLE_NEIGHBORING_CELL = 2;
106 private static final int EVENT_NEIGHBORING_CELL_DONE = 3;
107 private static final int CMD_ANSWER_RINGING_CALL = 4;
108 private static final int CMD_END_CALL = 5; // not used yet
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700109 private static final int CMD_TRANSMIT_APDU_LOGICAL_CHANNEL = 7;
110 private static final int EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE = 8;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700111 private static final int CMD_OPEN_CHANNEL = 9;
112 private static final int EVENT_OPEN_CHANNEL_DONE = 10;
113 private static final int CMD_CLOSE_CHANNEL = 11;
114 private static final int EVENT_CLOSE_CHANNEL_DONE = 12;
Jake Hambye994d462014-02-03 13:10:13 -0800115 private static final int CMD_NV_READ_ITEM = 13;
116 private static final int EVENT_NV_READ_ITEM_DONE = 14;
117 private static final int CMD_NV_WRITE_ITEM = 15;
118 private static final int EVENT_NV_WRITE_ITEM_DONE = 16;
119 private static final int CMD_NV_WRITE_CDMA_PRL = 17;
120 private static final int EVENT_NV_WRITE_CDMA_PRL_DONE = 18;
121 private static final int CMD_NV_RESET_CONFIG = 19;
122 private static final int EVENT_NV_RESET_CONFIG_DONE = 20;
Jake Hamby7c27be32014-03-03 13:25:59 -0800123 private static final int CMD_GET_PREFERRED_NETWORK_TYPE = 21;
124 private static final int EVENT_GET_PREFERRED_NETWORK_TYPE_DONE = 22;
125 private static final int CMD_SET_PREFERRED_NETWORK_TYPE = 23;
126 private static final int EVENT_SET_PREFERRED_NETWORK_TYPE_DONE = 24;
Sailesh Nepal35b59452014-03-06 09:26:56 -0800127 private static final int CMD_SEND_ENVELOPE = 25;
128 private static final int EVENT_SEND_ENVELOPE_DONE = 26;
Derek Tan6b088ee2014-09-05 14:15:18 -0700129 private static final int CMD_INVOKE_OEM_RIL_REQUEST_RAW = 27;
130 private static final int EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE = 28;
131 private static final int CMD_TRANSMIT_APDU_BASIC_CHANNEL = 29;
132 private static final int EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE = 30;
133 private static final int CMD_EXCHANGE_SIM_IO = 31;
134 private static final int EVENT_EXCHANGE_SIM_IO_DONE = 32;
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800135 private static final int CMD_SET_VOICEMAIL_NUMBER = 33;
136 private static final int EVENT_SET_VOICEMAIL_NUMBER_DONE = 34;
Stuart Scott54788802015-03-30 13:18:01 -0700137 private static final int CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC = 35;
138 private static final int EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE = 36;
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700139 private static final int CMD_GET_MODEM_ACTIVITY_INFO = 37;
140 private static final int EVENT_GET_MODEM_ACTIVITY_INFO_DONE = 38;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700141 private static final int CMD_PERFORM_NETWORK_SCAN = 39;
142 private static final int EVENT_PERFORM_NETWORK_SCAN_DONE = 40;
143 private static final int CMD_SET_NETWORK_SELECTION_MODE_MANUAL = 41;
144 private static final int EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE = 42;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700145
146 /** The singleton instance. */
147 private static PhoneInterfaceManager sInstance;
148
Wink Saville3ab207e2014-11-20 13:07:20 -0800149 private PhoneGlobals mApp;
150 private Phone mPhone;
151 private CallManager mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700152 private UserManager mUserManager;
Wink Saville3ab207e2014-11-20 13:07:20 -0800153 private AppOpsManager mAppOps;
154 private MainThreadHandler mMainThreadHandler;
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800155 private SubscriptionController mSubscriptionController;
Wink Saville3ab207e2014-11-20 13:07:20 -0800156 private SharedPreferences mTelephonySharedPreferences;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700157
Derek Tan97ebb422014-09-05 16:55:38 -0700158 private static final String PREF_CARRIERS_ALPHATAG_PREFIX = "carrier_alphtag_";
159 private static final String PREF_CARRIERS_NUMBER_PREFIX = "carrier_number_";
Jeff Sharkey85190e62014-12-05 09:40:12 -0800160 private static final String PREF_CARRIERS_SUBSCRIBER_PREFIX = "carrier_subscriber_";
Derek Tan89e89d42014-07-08 17:00:10 -0700161
162 /**
Shishir Agrawal566b7612013-10-28 14:41:00 -0700163 * A request object to use for transmitting data to an ICC.
164 */
165 private static final class IccAPDUArgument {
166 public int channel, cla, command, p1, p2, p3;
167 public String data;
168
169 public IccAPDUArgument(int channel, int cla, int command,
170 int p1, int p2, int p3, String data) {
171 this.channel = channel;
172 this.cla = cla;
173 this.command = command;
174 this.p1 = p1;
175 this.p2 = p2;
176 this.p3 = p3;
177 this.data = data;
178 }
179 }
180
181 /**
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700182 * A request object to use for transmitting data to an ICC.
183 */
184 private static final class ManualNetworkSelectionArgument {
185 public OperatorInfo operatorInfo;
186 public boolean persistSelection;
187
188 public ManualNetworkSelectionArgument(OperatorInfo operatorInfo, boolean persistSelection) {
189 this.operatorInfo = operatorInfo;
190 this.persistSelection = persistSelection;
191 }
192 }
193
194 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700195 * A request object for use with {@link MainThreadHandler}. Requesters should wait() on the
196 * request after sending. The main thread will notify the request when it is complete.
197 */
198 private static final class MainThreadRequest {
199 /** The argument to use for the request */
200 public Object argument;
201 /** The result of the request that is run on the main thread */
202 public Object result;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800203 // The subscriber id that this request applies to. Defaults to
204 // SubscriptionManager.INVALID_SUBSCRIPTION_ID
205 public Integer subId = SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700206
207 public MainThreadRequest(Object argument) {
208 this.argument = argument;
209 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800210
211 public MainThreadRequest(Object argument, Integer subId) {
212 this.argument = argument;
Sanket Padawe56e75a32016-02-08 12:18:19 -0800213 if (subId != null) {
214 this.subId = subId;
215 }
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800216 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700217 }
218
Sailesh Nepalcc0375f2013-11-13 09:15:18 -0800219 private static final class IncomingThirdPartyCallArgs {
220 public final ComponentName component;
221 public final String callId;
222 public final String callerDisplayName;
223
224 public IncomingThirdPartyCallArgs(ComponentName component, String callId,
225 String callerDisplayName) {
226 this.component = component;
227 this.callId = callId;
228 this.callerDisplayName = callerDisplayName;
229 }
230 }
231
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700232 /**
233 * A handler that processes messages on the main thread in the phone process. Since many
234 * of the Phone calls are not thread safe this is needed to shuttle the requests from the
235 * inbound binder threads to the main thread in the phone process. The Binder thread
236 * may provide a {@link MainThreadRequest} object in the msg.obj field that they are waiting
237 * on, which will be notified when the operation completes and will contain the result of the
238 * request.
239 *
240 * <p>If a MainThreadRequest object is provided in the msg.obj field,
241 * note that request.result must be set to something non-null for the calling thread to
242 * unblock.
243 */
244 private final class MainThreadHandler extends Handler {
245 @Override
246 public void handleMessage(Message msg) {
247 MainThreadRequest request;
248 Message onCompleted;
249 AsyncResult ar;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800250 UiccCard uiccCard;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700251 IccAPDUArgument iccArgument;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700252
253 switch (msg.what) {
Yorke Lee716f67e2015-06-17 15:39:16 -0700254 case CMD_HANDLE_PIN_MMI: {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700255 request = (MainThreadRequest) msg.obj;
Yorke Lee716f67e2015-06-17 15:39:16 -0700256 final Phone phone = getPhoneFromRequest(request);
257 request.result = phone != null ?
258 getPhoneFromRequest(request).handlePinMmi((String) request.argument)
259 : false;
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700260 // Wake up the requesting thread
261 synchronized (request) {
262 request.notifyAll();
263 }
264 break;
Yorke Lee716f67e2015-06-17 15:39:16 -0700265 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700266
267 case CMD_HANDLE_NEIGHBORING_CELL:
268 request = (MainThreadRequest) msg.obj;
269 onCompleted = obtainMessage(EVENT_NEIGHBORING_CELL_DONE,
270 request);
271 mPhone.getNeighboringCids(onCompleted);
272 break;
273
274 case EVENT_NEIGHBORING_CELL_DONE:
275 ar = (AsyncResult) msg.obj;
276 request = (MainThreadRequest) ar.userObj;
277 if (ar.exception == null && ar.result != null) {
278 request.result = ar.result;
279 } else {
280 // create an empty list to notify the waiting thread
Jake Hambye994d462014-02-03 13:10:13 -0800281 request.result = new ArrayList<NeighboringCellInfo>(0);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700282 }
283 // Wake up the requesting thread
284 synchronized (request) {
285 request.notifyAll();
286 }
287 break;
288
289 case CMD_ANSWER_RINGING_CALL:
Wink Saville08874612014-08-31 19:19:58 -0700290 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800291 int answer_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700292 answerRingingCallInternal(answer_subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700293 break;
294
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700295 case CMD_END_CALL:
296 request = (MainThreadRequest) msg.obj;
Stuart Scott584921c2015-01-15 17:10:34 -0800297 int end_subId = request.subId;
Wink Saville08874612014-08-31 19:19:58 -0700298 final boolean hungUp;
Anthony Leeae4e36d2015-05-21 07:17:46 -0700299 Phone phone = getPhone(end_subId);
300 if (phone == null) {
301 if (DBG) log("CMD_END_CALL: no phone for id: " + end_subId);
302 break;
303 }
304 int phoneType = phone.getPhoneType();
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700305 if (phoneType == PhoneConstants.PHONE_TYPE_CDMA) {
306 // CDMA: If the user presses the Power button we treat it as
307 // ending the complete call session
Wink Saville08874612014-08-31 19:19:58 -0700308 hungUp = PhoneUtils.hangupRingingAndActive(getPhone(end_subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700309 } else if (phoneType == PhoneConstants.PHONE_TYPE_GSM) {
310 // GSM: End the call as per the Phone state
311 hungUp = PhoneUtils.hangup(mCM);
312 } else {
313 throw new IllegalStateException("Unexpected phone type: " + phoneType);
314 }
315 if (DBG) log("CMD_END_CALL: " + (hungUp ? "hung up!" : "no call to hang up"));
316 request.result = hungUp;
317 // Wake up the requesting thread
318 synchronized (request) {
319 request.notifyAll();
320 }
321 break;
322
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700323 case CMD_TRANSMIT_APDU_LOGICAL_CHANNEL:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700324 request = (MainThreadRequest) msg.obj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700325 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800326 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700327 if (uiccCard == null) {
328 loge("iccTransmitApduLogicalChannel: No UICC");
329 request.result = new IccIoResult(0x6F, 0, (byte[])null);
330 synchronized (request) {
331 request.notifyAll();
332 }
333 } else {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700334 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE,
335 request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700336 uiccCard.iccTransmitApduLogicalChannel(
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700337 iccArgument.channel, iccArgument.cla, iccArgument.command,
338 iccArgument.p1, iccArgument.p2, iccArgument.p3, iccArgument.data,
Shishir Agrawal566b7612013-10-28 14:41:00 -0700339 onCompleted);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700340 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700341 break;
342
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700343 case EVENT_TRANSMIT_APDU_LOGICAL_CHANNEL_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700344 ar = (AsyncResult) msg.obj;
345 request = (MainThreadRequest) ar.userObj;
346 if (ar.exception == null && ar.result != null) {
347 request.result = ar.result;
348 } else {
349 request.result = new IccIoResult(0x6F, 0, (byte[])null);
350 if (ar.result == null) {
351 loge("iccTransmitApduLogicalChannel: Empty response");
Jake Hambye994d462014-02-03 13:10:13 -0800352 } else if (ar.exception instanceof CommandException) {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700353 loge("iccTransmitApduLogicalChannel: CommandException: " +
Jake Hambye994d462014-02-03 13:10:13 -0800354 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700355 } else {
356 loge("iccTransmitApduLogicalChannel: Unknown exception");
357 }
358 }
359 synchronized (request) {
360 request.notifyAll();
361 }
362 break;
363
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700364 case CMD_TRANSMIT_APDU_BASIC_CHANNEL:
365 request = (MainThreadRequest) msg.obj;
366 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800367 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700368 if (uiccCard == null) {
369 loge("iccTransmitApduBasicChannel: No UICC");
370 request.result = new IccIoResult(0x6F, 0, (byte[])null);
371 synchronized (request) {
372 request.notifyAll();
373 }
374 } else {
375 onCompleted = obtainMessage(EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE,
376 request);
377 uiccCard.iccTransmitApduBasicChannel(
378 iccArgument.cla, iccArgument.command, iccArgument.p1, iccArgument.p2,
379 iccArgument.p3, iccArgument.data, onCompleted);
380 }
381 break;
382
383 case EVENT_TRANSMIT_APDU_BASIC_CHANNEL_DONE:
384 ar = (AsyncResult) msg.obj;
385 request = (MainThreadRequest) ar.userObj;
386 if (ar.exception == null && ar.result != null) {
387 request.result = ar.result;
388 } else {
389 request.result = new IccIoResult(0x6F, 0, (byte[])null);
390 if (ar.result == null) {
391 loge("iccTransmitApduBasicChannel: Empty response");
392 } else if (ar.exception instanceof CommandException) {
393 loge("iccTransmitApduBasicChannel: CommandException: " +
394 ar.exception);
395 } else {
396 loge("iccTransmitApduBasicChannel: Unknown exception");
397 }
398 }
399 synchronized (request) {
400 request.notifyAll();
401 }
402 break;
403
404 case CMD_EXCHANGE_SIM_IO:
405 request = (MainThreadRequest) msg.obj;
406 iccArgument = (IccAPDUArgument) request.argument;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800407 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700408 if (uiccCard == null) {
409 loge("iccExchangeSimIO: No UICC");
410 request.result = new IccIoResult(0x6F, 0, (byte[])null);
411 synchronized (request) {
412 request.notifyAll();
413 }
414 } else {
415 onCompleted = obtainMessage(EVENT_EXCHANGE_SIM_IO_DONE,
416 request);
417 uiccCard.iccExchangeSimIO(iccArgument.cla, /* fileID */
418 iccArgument.command, iccArgument.p1, iccArgument.p2, iccArgument.p3,
419 iccArgument.data, onCompleted);
420 }
421 break;
422
423 case EVENT_EXCHANGE_SIM_IO_DONE:
424 ar = (AsyncResult) msg.obj;
425 request = (MainThreadRequest) ar.userObj;
426 if (ar.exception == null && ar.result != null) {
427 request.result = ar.result;
428 } else {
429 request.result = new IccIoResult(0x6f, 0, (byte[])null);
430 }
431 synchronized (request) {
432 request.notifyAll();
433 }
434 break;
435
Derek Tan4d5e5c12014-02-04 11:54:58 -0800436 case CMD_SEND_ENVELOPE:
437 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800438 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700439 if (uiccCard == null) {
440 loge("sendEnvelopeWithStatus: No UICC");
441 request.result = new IccIoResult(0x6F, 0, (byte[])null);
442 synchronized (request) {
443 request.notifyAll();
444 }
445 } else {
446 onCompleted = obtainMessage(EVENT_SEND_ENVELOPE_DONE, request);
447 uiccCard.sendEnvelopeWithStatus((String)request.argument, onCompleted);
448 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800449 break;
450
451 case EVENT_SEND_ENVELOPE_DONE:
452 ar = (AsyncResult) msg.obj;
453 request = (MainThreadRequest) ar.userObj;
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700454 if (ar.exception == null && ar.result != null) {
455 request.result = ar.result;
Derek Tan4d5e5c12014-02-04 11:54:58 -0800456 } else {
Shishir Agrawal9f9877d2014-03-14 09:36:27 -0700457 request.result = new IccIoResult(0x6F, 0, (byte[])null);
458 if (ar.result == null) {
459 loge("sendEnvelopeWithStatus: Empty response");
460 } else if (ar.exception instanceof CommandException) {
461 loge("sendEnvelopeWithStatus: CommandException: " +
462 ar.exception);
463 } else {
464 loge("sendEnvelopeWithStatus: exception:" + ar.exception);
465 }
Derek Tan4d5e5c12014-02-04 11:54:58 -0800466 }
467 synchronized (request) {
468 request.notifyAll();
469 }
470 break;
471
Shishir Agrawal566b7612013-10-28 14:41:00 -0700472 case CMD_OPEN_CHANNEL:
473 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800474 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700475 if (uiccCard == null) {
476 loge("iccOpenLogicalChannel: No UICC");
Shishir Agrawalfc0492a2016-02-17 11:15:33 -0800477 request.result = new IccOpenLogicalChannelResponse(-1,
478 IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE, null);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700479 synchronized (request) {
480 request.notifyAll();
481 }
482 } else {
483 onCompleted = obtainMessage(EVENT_OPEN_CHANNEL_DONE, request);
484 uiccCard.iccOpenLogicalChannel((String)request.argument, onCompleted);
485 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700486 break;
487
488 case EVENT_OPEN_CHANNEL_DONE:
489 ar = (AsyncResult) msg.obj;
490 request = (MainThreadRequest) ar.userObj;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700491 IccOpenLogicalChannelResponse openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700492 if (ar.exception == null && ar.result != null) {
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700493 int[] result = (int[]) ar.result;
494 int channelId = result[0];
495 byte[] selectResponse = null;
496 if (result.length > 1) {
497 selectResponse = new byte[result.length - 1];
498 for (int i = 1; i < result.length; ++i) {
499 selectResponse[i - 1] = (byte) result[i];
500 }
501 }
502 openChannelResp = new IccOpenLogicalChannelResponse(channelId,
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700503 IccOpenLogicalChannelResponse.STATUS_NO_ERROR, selectResponse);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700504 } else {
Shishir Agrawal566b7612013-10-28 14:41:00 -0700505 if (ar.result == null) {
506 loge("iccOpenLogicalChannel: Empty response");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700507 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700508 if (ar.exception != null) {
509 loge("iccOpenLogicalChannel: Exception: " + ar.exception);
510 }
511
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700512 int errorCode = IccOpenLogicalChannelResponse.STATUS_UNKNOWN_ERROR;
Junda Liua754ba12015-05-20 01:17:52 -0700513 if (ar.exception instanceof CommandException) {
514 CommandException.Error error =
515 ((CommandException) (ar.exception)).getCommandError();
516 if (error == CommandException.Error.MISSING_RESOURCE) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700517 errorCode = IccOpenLogicalChannelResponse.STATUS_MISSING_RESOURCE;
Junda Liua754ba12015-05-20 01:17:52 -0700518 } else if (error == CommandException.Error.NO_SUCH_ELEMENT) {
Shishir Agrawal527e8bf2014-08-25 08:54:56 -0700519 errorCode = IccOpenLogicalChannelResponse.STATUS_NO_SUCH_ELEMENT;
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -0700520 }
521 }
522 openChannelResp = new IccOpenLogicalChannelResponse(
523 IccOpenLogicalChannelResponse.INVALID_CHANNEL, errorCode, null);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700524 }
Shishir Agrawal82c8a462014-07-31 18:13:17 -0700525 request.result = openChannelResp;
Shishir Agrawal566b7612013-10-28 14:41:00 -0700526 synchronized (request) {
527 request.notifyAll();
528 }
529 break;
530
531 case CMD_CLOSE_CHANNEL:
532 request = (MainThreadRequest) msg.obj;
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800533 uiccCard = getUiccCardFromRequest(request);
Shishir Agrawaleb8771e2014-07-22 11:24:08 -0700534 if (uiccCard == null) {
535 loge("iccCloseLogicalChannel: No UICC");
536 request.result = new IccIoResult(0x6F, 0, (byte[])null);
537 synchronized (request) {
538 request.notifyAll();
539 }
540 } else {
541 onCompleted = obtainMessage(EVENT_CLOSE_CHANNEL_DONE, request);
542 uiccCard.iccCloseLogicalChannel((Integer) request.argument, onCompleted);
543 }
Shishir Agrawal566b7612013-10-28 14:41:00 -0700544 break;
545
546 case EVENT_CLOSE_CHANNEL_DONE:
Jake Hambye994d462014-02-03 13:10:13 -0800547 handleNullReturnEvent(msg, "iccCloseLogicalChannel");
548 break;
549
550 case CMD_NV_READ_ITEM:
551 request = (MainThreadRequest) msg.obj;
552 onCompleted = obtainMessage(EVENT_NV_READ_ITEM_DONE, request);
553 mPhone.nvReadItem((Integer) request.argument, onCompleted);
554 break;
555
556 case EVENT_NV_READ_ITEM_DONE:
Shishir Agrawal566b7612013-10-28 14:41:00 -0700557 ar = (AsyncResult) msg.obj;
558 request = (MainThreadRequest) ar.userObj;
Jake Hambye994d462014-02-03 13:10:13 -0800559 if (ar.exception == null && ar.result != null) {
560 request.result = ar.result; // String
Shishir Agrawal566b7612013-10-28 14:41:00 -0700561 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800562 request.result = "";
563 if (ar.result == null) {
564 loge("nvReadItem: Empty response");
565 } else if (ar.exception instanceof CommandException) {
566 loge("nvReadItem: CommandException: " +
567 ar.exception);
Shishir Agrawal566b7612013-10-28 14:41:00 -0700568 } else {
Jake Hambye994d462014-02-03 13:10:13 -0800569 loge("nvReadItem: Unknown exception");
Shishir Agrawal566b7612013-10-28 14:41:00 -0700570 }
571 }
572 synchronized (request) {
573 request.notifyAll();
574 }
575 break;
576
Jake Hambye994d462014-02-03 13:10:13 -0800577 case CMD_NV_WRITE_ITEM:
578 request = (MainThreadRequest) msg.obj;
579 onCompleted = obtainMessage(EVENT_NV_WRITE_ITEM_DONE, request);
580 Pair<Integer, String> idValue = (Pair<Integer, String>) request.argument;
581 mPhone.nvWriteItem(idValue.first, idValue.second, onCompleted);
582 break;
583
584 case EVENT_NV_WRITE_ITEM_DONE:
585 handleNullReturnEvent(msg, "nvWriteItem");
586 break;
587
588 case CMD_NV_WRITE_CDMA_PRL:
589 request = (MainThreadRequest) msg.obj;
590 onCompleted = obtainMessage(EVENT_NV_WRITE_CDMA_PRL_DONE, request);
591 mPhone.nvWriteCdmaPrl((byte[]) request.argument, onCompleted);
592 break;
593
594 case EVENT_NV_WRITE_CDMA_PRL_DONE:
595 handleNullReturnEvent(msg, "nvWriteCdmaPrl");
596 break;
597
598 case CMD_NV_RESET_CONFIG:
599 request = (MainThreadRequest) msg.obj;
600 onCompleted = obtainMessage(EVENT_NV_RESET_CONFIG_DONE, request);
601 mPhone.nvResetConfig((Integer) request.argument, onCompleted);
602 break;
603
604 case EVENT_NV_RESET_CONFIG_DONE:
605 handleNullReturnEvent(msg, "nvResetConfig");
606 break;
607
Jake Hamby7c27be32014-03-03 13:25:59 -0800608 case CMD_GET_PREFERRED_NETWORK_TYPE:
609 request = (MainThreadRequest) msg.obj;
610 onCompleted = obtainMessage(EVENT_GET_PREFERRED_NETWORK_TYPE_DONE, request);
Stuart Scott54788802015-03-30 13:18:01 -0700611 getPhoneFromRequest(request).getPreferredNetworkType(onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800612 break;
613
614 case EVENT_GET_PREFERRED_NETWORK_TYPE_DONE:
615 ar = (AsyncResult) msg.obj;
616 request = (MainThreadRequest) ar.userObj;
617 if (ar.exception == null && ar.result != null) {
618 request.result = ar.result; // Integer
619 } else {
Sanket Padawecfc2d352016-01-05 19:52:14 -0800620 request.result = null;
Jake Hamby7c27be32014-03-03 13:25:59 -0800621 if (ar.result == null) {
622 loge("getPreferredNetworkType: Empty response");
623 } else if (ar.exception instanceof CommandException) {
624 loge("getPreferredNetworkType: CommandException: " +
625 ar.exception);
626 } else {
627 loge("getPreferredNetworkType: Unknown exception");
628 }
629 }
630 synchronized (request) {
631 request.notifyAll();
632 }
633 break;
634
635 case CMD_SET_PREFERRED_NETWORK_TYPE:
636 request = (MainThreadRequest) msg.obj;
637 onCompleted = obtainMessage(EVENT_SET_PREFERRED_NETWORK_TYPE_DONE, request);
638 int networkType = (Integer) request.argument;
Stuart Scott54788802015-03-30 13:18:01 -0700639 getPhoneFromRequest(request).setPreferredNetworkType(networkType, onCompleted);
Jake Hamby7c27be32014-03-03 13:25:59 -0800640 break;
641
642 case EVENT_SET_PREFERRED_NETWORK_TYPE_DONE:
643 handleNullReturnEvent(msg, "setPreferredNetworkType");
644 break;
645
Steven Liu4bf01bc2014-07-17 11:05:29 -0500646 case CMD_INVOKE_OEM_RIL_REQUEST_RAW:
647 request = (MainThreadRequest)msg.obj;
648 onCompleted = obtainMessage(EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE, request);
649 mPhone.invokeOemRilRequestRaw((byte[])request.argument, onCompleted);
650 break;
651
652 case EVENT_INVOKE_OEM_RIL_REQUEST_RAW_DONE:
653 ar = (AsyncResult)msg.obj;
654 request = (MainThreadRequest)ar.userObj;
655 request.result = ar;
656 synchronized (request) {
657 request.notifyAll();
658 }
659 break;
660
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800661 case CMD_SET_VOICEMAIL_NUMBER:
662 request = (MainThreadRequest) msg.obj;
663 onCompleted = obtainMessage(EVENT_SET_VOICEMAIL_NUMBER_DONE, request);
664 Pair<String, String> tagNum = (Pair<String, String>) request.argument;
Stuart Scott584921c2015-01-15 17:10:34 -0800665 getPhoneFromRequest(request).setVoiceMailNumber(tagNum.first, tagNum.second,
666 onCompleted);
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800667 break;
668
669 case EVENT_SET_VOICEMAIL_NUMBER_DONE:
670 handleNullReturnEvent(msg, "setVoicemailNumber");
671 break;
672
Stuart Scott54788802015-03-30 13:18:01 -0700673 case CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC:
674 request = (MainThreadRequest) msg.obj;
675 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE,
676 request);
677 getPhoneFromRequest(request).setNetworkSelectionModeAutomatic(onCompleted);
678 break;
679
680 case EVENT_SET_NETWORK_SELECTION_MODE_AUTOMATIC_DONE:
681 handleNullReturnEvent(msg, "setNetworkSelectionModeAutomatic");
682 break;
683
Shishir Agrawal302c8692015-06-19 13:49:39 -0700684 case CMD_PERFORM_NETWORK_SCAN:
685 request = (MainThreadRequest) msg.obj;
686 onCompleted = obtainMessage(EVENT_PERFORM_NETWORK_SCAN_DONE, request);
687 getPhoneFromRequest(request).getAvailableNetworks(onCompleted);
688 break;
689
690 case EVENT_PERFORM_NETWORK_SCAN_DONE:
691 ar = (AsyncResult) msg.obj;
692 request = (MainThreadRequest) ar.userObj;
693 CellNetworkScanResult cellScanResult;
694 if (ar.exception == null && ar.result != null) {
695 cellScanResult = new CellNetworkScanResult(
696 CellNetworkScanResult.STATUS_SUCCESS,
697 (List<OperatorInfo>) ar.result);
698 } else {
699 if (ar.result == null) {
700 loge("getCellNetworkScanResults: Empty response");
701 }
702 if (ar.exception != null) {
703 loge("getCellNetworkScanResults: Exception: " + ar.exception);
704 }
705 int errorCode = CellNetworkScanResult.STATUS_UNKNOWN_ERROR;
706 if (ar.exception instanceof CommandException) {
707 CommandException.Error error =
708 ((CommandException) (ar.exception)).getCommandError();
709 if (error == CommandException.Error.RADIO_NOT_AVAILABLE) {
710 errorCode = CellNetworkScanResult.STATUS_RADIO_NOT_AVAILABLE;
711 } else if (error == CommandException.Error.GENERIC_FAILURE) {
712 errorCode = CellNetworkScanResult.STATUS_RADIO_GENERIC_FAILURE;
713 }
714 }
715 cellScanResult = new CellNetworkScanResult(errorCode, null);
716 }
717 request.result = cellScanResult;
718 synchronized (request) {
719 request.notifyAll();
720 }
721 break;
722
723 case CMD_SET_NETWORK_SELECTION_MODE_MANUAL:
724 request = (MainThreadRequest) msg.obj;
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700725 ManualNetworkSelectionArgument selArg =
726 (ManualNetworkSelectionArgument) request.argument;
Shishir Agrawal302c8692015-06-19 13:49:39 -0700727 onCompleted = obtainMessage(EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE,
728 request);
Shishir Agrawal77ba3172015-09-10 14:50:19 -0700729 getPhoneFromRequest(request).selectNetworkManually(selArg.operatorInfo,
730 selArg.persistSelection, onCompleted);
Shishir Agrawal302c8692015-06-19 13:49:39 -0700731 break;
732
733 case EVENT_SET_NETWORK_SELECTION_MODE_MANUAL_DONE:
734 handleNullReturnEvent(msg, "setNetworkSelectionModeManual");
735 break;
736
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700737 case CMD_GET_MODEM_ACTIVITY_INFO:
738 request = (MainThreadRequest) msg.obj;
739 onCompleted = obtainMessage(EVENT_GET_MODEM_ACTIVITY_INFO_DONE, request);
Prerepa Viswanadham61a60ad2015-06-08 18:07:51 -0700740 mPhone.getModemActivityInfo(onCompleted);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700741 break;
742
743 case EVENT_GET_MODEM_ACTIVITY_INFO_DONE:
744 ar = (AsyncResult) msg.obj;
745 request = (MainThreadRequest) ar.userObj;
746 if (ar.exception == null && ar.result != null) {
747 request.result = ar.result;
748 } else {
749 if (ar.result == null) {
750 loge("queryModemActivityInfo: Empty response");
751 } else if (ar.exception instanceof CommandException) {
752 loge("queryModemActivityInfo: CommandException: " +
753 ar.exception);
754 } else {
755 loge("queryModemActivityInfo: Unknown exception");
756 }
757 }
Amit Mahajand4766222016-01-28 15:28:28 -0800758 // Result cannot be null. Return ModemActivityInfo with all fields set to 0.
759 if (request.result == null) {
760 request.result = new ModemActivityInfo(0, 0, 0, null, 0, 0);
761 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -0700762 synchronized (request) {
763 request.notifyAll();
764 }
765 break;
766
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700767 default:
768 Log.w(LOG_TAG, "MainThreadHandler: unexpected message code: " + msg.what);
769 break;
770 }
771 }
Jake Hambye994d462014-02-03 13:10:13 -0800772
773 private void handleNullReturnEvent(Message msg, String command) {
774 AsyncResult ar = (AsyncResult) msg.obj;
775 MainThreadRequest request = (MainThreadRequest) ar.userObj;
776 if (ar.exception == null) {
777 request.result = true;
778 } else {
779 request.result = false;
780 if (ar.exception instanceof CommandException) {
781 loge(command + ": CommandException: " + ar.exception);
782 } else {
783 loge(command + ": Unknown exception");
784 }
785 }
786 synchronized (request) {
787 request.notifyAll();
788 }
789 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700790 }
791
792 /**
793 * Posts the specified command to be executed on the main thread,
794 * waits for the request to complete, and returns the result.
795 * @see #sendRequestAsync
796 */
797 private Object sendRequest(int command, Object argument) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800798 return sendRequest(command, argument, SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Wink Saville36469e72014-06-11 15:17:00 -0700799 }
800
801 /**
802 * Posts the specified command to be executed on the main thread,
803 * waits for the request to complete, and returns the result.
804 * @see #sendRequestAsync
805 */
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800806 private Object sendRequest(int command, Object argument, Integer subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700807 if (Looper.myLooper() == mMainThreadHandler.getLooper()) {
808 throw new RuntimeException("This method will deadlock if called from the main thread.");
809 }
810
Shishir Agrawal76d5da92014-11-09 16:17:25 -0800811 MainThreadRequest request = new MainThreadRequest(argument, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700812 Message msg = mMainThreadHandler.obtainMessage(command, request);
813 msg.sendToTarget();
814
815 // Wait for the request to complete
816 synchronized (request) {
817 while (request.result == null) {
818 try {
819 request.wait();
820 } catch (InterruptedException e) {
821 // Do nothing, go back and wait until the request is complete
822 }
823 }
824 }
825 return request.result;
826 }
827
828 /**
829 * Asynchronous ("fire and forget") version of sendRequest():
830 * Posts the specified command to be executed on the main thread, and
831 * returns immediately.
832 * @see #sendRequest
833 */
834 private void sendRequestAsync(int command) {
835 mMainThreadHandler.sendEmptyMessage(command);
836 }
837
838 /**
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -0700839 * Same as {@link #sendRequestAsync(int)} except it takes an argument.
840 * @see {@link #sendRequest(int,Object)}
841 */
842 private void sendRequestAsync(int command, Object argument) {
843 MainThreadRequest request = new MainThreadRequest(argument);
844 Message msg = mMainThreadHandler.obtainMessage(command, request);
845 msg.sendToTarget();
846 }
847
848 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700849 * Initialize the singleton PhoneInterfaceManager instance.
850 * This is only done once, at startup, from PhoneApp.onCreate().
851 */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700852 /* package */ static PhoneInterfaceManager init(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700853 synchronized (PhoneInterfaceManager.class) {
854 if (sInstance == null) {
Sailesh Nepal194161e2014-07-03 08:57:44 -0700855 sInstance = new PhoneInterfaceManager(app, phone);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700856 } else {
857 Log.wtf(LOG_TAG, "init() called multiple times! sInstance = " + sInstance);
858 }
859 return sInstance;
860 }
861 }
862
863 /** Private constructor; @see init() */
Sailesh Nepal194161e2014-07-03 08:57:44 -0700864 private PhoneInterfaceManager(PhoneGlobals app, Phone phone) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700865 mApp = app;
866 mPhone = phone;
867 mCM = PhoneGlobals.getInstance().mCM;
Stuart Scott981d8582015-04-21 14:09:50 -0700868 mUserManager = (UserManager) app.getSystemService(Context.USER_SERVICE);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700869 mAppOps = (AppOpsManager)app.getSystemService(Context.APP_OPS_SERVICE);
870 mMainThreadHandler = new MainThreadHandler();
Andrew Leedf14ead2014-10-17 14:22:52 -0700871 mTelephonySharedPreferences =
Derek Tan97ebb422014-09-05 16:55:38 -0700872 PreferenceManager.getDefaultSharedPreferences(mPhone.getContext());
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800873 mSubscriptionController = SubscriptionController.getInstance();
Wink Saville3ab207e2014-11-20 13:07:20 -0800874
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700875 publish();
876 }
877
878 private void publish() {
879 if (DBG) log("publish: " + this);
880
881 ServiceManager.addService("phone", this);
882 }
883
Stuart Scott584921c2015-01-15 17:10:34 -0800884 private Phone getPhoneFromRequest(MainThreadRequest request) {
Sanket Padawe56e75a32016-02-08 12:18:19 -0800885 return (request.subId == SubscriptionManager.INVALID_SUBSCRIPTION_ID)
886 ? mPhone : getPhone(request.subId);
Stuart Scott584921c2015-01-15 17:10:34 -0800887 }
888
Shishir Agrawalc04d9752016-02-19 10:41:00 -0800889 private UiccCard getUiccCardFromRequest(MainThreadRequest request) {
890 Phone phone = getPhoneFromRequest(request);
891 return phone == null ? null :
892 UiccController.getInstance().getUiccCard(phone.getPhoneId());
893 }
894
Wink Saville36469e72014-06-11 15:17:00 -0700895 // returns phone associated with the subId.
Wink Savilleb564aae2014-10-23 10:18:09 -0700896 private Phone getPhone(int subId) {
Wink Savilleac1bdfd2014-11-20 23:04:44 -0800897 return PhoneFactory.getPhone(mSubscriptionController.getPhoneId(subId));
Wink Saville36469e72014-06-11 15:17:00 -0700898 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700899 //
900 // Implementation of the ITelephony interface.
901 //
902
903 public void dial(String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700904 dialForSubscriber(getPreferredVoiceSubscription(), number);
Wink Saville36469e72014-06-11 15:17:00 -0700905 }
906
Wink Savilleb564aae2014-10-23 10:18:09 -0700907 public void dialForSubscriber(int subId, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700908 if (DBG) log("dial: " + number);
909 // No permission check needed here: This is just a wrapper around the
910 // ACTION_DIAL intent, which is available to any app since it puts up
911 // the UI before it does anything.
912
913 String url = createTelUrl(number);
914 if (url == null) {
915 return;
916 }
917
918 // PENDING: should we just silently fail if phone is offhook or ringing?
Wink Saville36469e72014-06-11 15:17:00 -0700919 PhoneConstants.State state = mCM.getState(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700920 if (state != PhoneConstants.State.OFFHOOK && state != PhoneConstants.State.RINGING) {
921 Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse(url));
922 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
923 mApp.startActivity(intent);
924 }
925 }
926
927 public void call(String callingPackage, String number) {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700928 callForSubscriber(getPreferredVoiceSubscription(), callingPackage, number);
Wink Saville36469e72014-06-11 15:17:00 -0700929 }
930
Wink Savilleb564aae2014-10-23 10:18:09 -0700931 public void callForSubscriber(int subId, String callingPackage, String number) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700932 if (DBG) log("call: " + number);
933
934 // This is just a wrapper around the ACTION_CALL intent, but we still
935 // need to do a permission check since we're calling startActivity()
936 // from the context of the phone app.
937 enforceCallPermission();
938
939 if (mAppOps.noteOp(AppOpsManager.OP_CALL_PHONE, Binder.getCallingUid(), callingPackage)
940 != AppOpsManager.MODE_ALLOWED) {
941 return;
942 }
943
944 String url = createTelUrl(number);
945 if (url == null) {
946 return;
947 }
948
Wink Saville08874612014-08-31 19:19:58 -0700949 boolean isValid = false;
Narayan Kamath1c496c22015-04-16 14:40:19 +0100950 final List<SubscriptionInfo> slist = getActiveSubscriptionInfoList();
Wink Saville3ab207e2014-11-20 13:07:20 -0800951 if (slist != null) {
952 for (SubscriptionInfo subInfoRecord : slist) {
953 if (subInfoRecord.getSubscriptionId() == subId) {
954 isValid = true;
955 break;
956 }
Wink Saville08874612014-08-31 19:19:58 -0700957 }
958 }
959 if (isValid == false) {
960 return;
961 }
962
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700963 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
Wink Saville36469e72014-06-11 15:17:00 -0700964 intent.putExtra(SUBSCRIPTION_KEY, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700965 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
966 mApp.startActivity(intent);
967 }
968
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700969 /**
970 * End a call based on call state
971 * @return true is a call was ended
972 */
973 public boolean endCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700974 return endCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700975 }
976
977 /**
978 * End a call based on the call state of the subId
979 * @return true is a call was ended
980 */
Wink Savilleb564aae2014-10-23 10:18:09 -0700981 public boolean endCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700982 enforceCallPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800983 return (Boolean) sendRequest(CMD_END_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700984 }
985
986 public void answerRingingCall() {
Wink Savilleadd7cc52014-09-08 14:23:09 -0700987 answerRingingCallForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -0700988 }
989
Wink Savilleb564aae2014-10-23 10:18:09 -0700990 public void answerRingingCallForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700991 if (DBG) log("answerRingingCall...");
992 // TODO: there should eventually be a separate "ANSWER_PHONE" permission,
993 // but that can probably wait till the big TelephonyManager API overhaul.
994 // For now, protect this call with the MODIFY_PHONE_STATE permission.
995 enforceModifyPermission();
Stuart Scott584921c2015-01-15 17:10:34 -0800996 sendRequest(CMD_ANSWER_RINGING_CALL, null, new Integer(subId));
Santos Cordon7d4ddf62013-07-10 11:58:08 -0700997 }
998
999 /**
1000 * Make the actual telephony calls to implement answerRingingCall().
1001 * This should only be called from the main thread of the Phone app.
1002 * @see #answerRingingCall
1003 *
1004 * TODO: it would be nice to return true if we answered the call, or
1005 * false if there wasn't actually a ringing incoming call, or some
1006 * other error occurred. (In other words, pass back the return value
1007 * from PhoneUtils.answerCall() or PhoneUtils.answerAndEndActive().)
1008 * But that would require calling this method via sendRequest() rather
1009 * than sendRequestAsync(), and right now we don't actually *need* that
1010 * return value, so let's just return void for now.
1011 */
Wink Savilleb564aae2014-10-23 10:18:09 -07001012 private void answerRingingCallInternal(int subId) {
Wink Saville08874612014-08-31 19:19:58 -07001013 final boolean hasRingingCall = !getPhone(subId).getRingingCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001014 if (hasRingingCall) {
Wink Saville08874612014-08-31 19:19:58 -07001015 final boolean hasActiveCall = !getPhone(subId).getForegroundCall().isIdle();
1016 final boolean hasHoldingCall = !getPhone(subId).getBackgroundCall().isIdle();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001017 if (hasActiveCall && hasHoldingCall) {
1018 // Both lines are in use!
1019 // TODO: provide a flag to let the caller specify what
1020 // policy to use if both lines are in use. (The current
1021 // behavior is hardwired to "answer incoming, end ongoing",
1022 // which is how the CALL button is specced to behave.)
1023 PhoneUtils.answerAndEndActive(mCM, mCM.getFirstActiveRingingCall());
1024 return;
1025 } else {
1026 // answerCall() will automatically hold the current active
1027 // call, if there is one.
1028 PhoneUtils.answerCall(mCM.getFirstActiveRingingCall());
1029 return;
1030 }
1031 } else {
1032 // No call was ringing.
1033 return;
1034 }
1035 }
1036
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001037 /**
Santos Cordon5422a8d2014-09-12 04:20:56 -07001038 * This method is no longer used and can be removed once TelephonyManager stops referring to it.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001039 */
Santos Cordon5422a8d2014-09-12 04:20:56 -07001040 public void silenceRinger() {
1041 Log.e(LOG_TAG, "silenseRinger not supported");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001042 }
1043
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001044 @Override
1045 public boolean isOffhook(String callingPackage) {
1046 return isOffhookForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001047 }
1048
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001049 @Override
1050 public boolean isOffhookForSubscriber(int subId, String callingPackage) {
1051 if (!canReadPhoneState(callingPackage, "isOffhookForSubscriber")) {
1052 return false;
1053 }
1054
Sanket Padawe356d7632015-06-22 14:03:32 -07001055 final Phone phone = getPhone(subId);
1056 if (phone != null) {
1057 return (phone.getState() == PhoneConstants.State.OFFHOOK);
1058 } else {
1059 return false;
1060 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001061 }
1062
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001063 @Override
1064 public boolean isRinging(String callingPackage) {
1065 return (isRingingForSubscriber(getDefaultSubscription(), callingPackage));
Wink Saville36469e72014-06-11 15:17:00 -07001066 }
1067
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001068 @Override
1069 public boolean isRingingForSubscriber(int subId, String callingPackage) {
1070 if (!canReadPhoneState(callingPackage, "isRingingForSubscriber")) {
1071 return false;
1072 }
1073
Sanket Padawe356d7632015-06-22 14:03:32 -07001074 final Phone phone = getPhone(subId);
1075 if (phone != null) {
1076 return (phone.getState() == PhoneConstants.State.RINGING);
1077 } else {
1078 return false;
1079 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001080 }
1081
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001082 @Override
1083 public boolean isIdle(String callingPackage) {
1084 return isIdleForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001085 }
1086
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001087 @Override
1088 public boolean isIdleForSubscriber(int subId, String callingPackage) {
1089 if (!canReadPhoneState(callingPackage, "isIdleForSubscriber")) {
1090 return false;
1091 }
1092
Sanket Padawe356d7632015-06-22 14:03:32 -07001093 final Phone phone = getPhone(subId);
1094 if (phone != null) {
1095 return (phone.getState() == PhoneConstants.State.IDLE);
1096 } else {
1097 return false;
1098 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001099 }
1100
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001101 public boolean supplyPin(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001102 return supplyPinForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001103 }
1104
Wink Savilleb564aae2014-10-23 10:18:09 -07001105 public boolean supplyPinForSubscriber(int subId, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001106 int [] resultArray = supplyPinReportResultForSubscriber(subId, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001107 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1108 }
1109
1110 public boolean supplyPuk(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001111 return supplyPukForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001112 }
1113
Wink Savilleb564aae2014-10-23 10:18:09 -07001114 public boolean supplyPukForSubscriber(int subId, String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001115 int [] resultArray = supplyPukReportResultForSubscriber(subId, puk, pin);
Wink Saville9de0f752013-10-22 19:04:03 -07001116 return (resultArray[0] == PhoneConstants.PIN_RESULT_SUCCESS) ? true : false;
1117 }
1118
1119 /** {@hide} */
1120 public int[] supplyPinReportResult(String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001121 return supplyPinReportResultForSubscriber(getDefaultSubscription(), pin);
Wink Saville36469e72014-06-11 15:17:00 -07001122 }
1123
Wink Savilleb564aae2014-10-23 10:18:09 -07001124 public int[] supplyPinReportResultForSubscriber(int subId, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001125 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001126 final UnlockSim checkSimPin = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001127 checkSimPin.start();
1128 return checkSimPin.unlockSim(null, pin);
1129 }
1130
Wink Saville9de0f752013-10-22 19:04:03 -07001131 /** {@hide} */
1132 public int[] supplyPukReportResult(String puk, String pin) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001133 return supplyPukReportResultForSubscriber(getDefaultSubscription(), puk, pin);
Wink Saville36469e72014-06-11 15:17:00 -07001134 }
1135
Wink Savilleb564aae2014-10-23 10:18:09 -07001136 public int[] supplyPukReportResultForSubscriber(int subId, String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001137 enforceModifyPermission();
Wink Saville36469e72014-06-11 15:17:00 -07001138 final UnlockSim checkSimPuk = new UnlockSim(getPhone(subId).getIccCard());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001139 checkSimPuk.start();
1140 return checkSimPuk.unlockSim(puk, pin);
1141 }
1142
1143 /**
Wink Saville9de0f752013-10-22 19:04:03 -07001144 * Helper thread to turn async call to SimCard#supplyPin into
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001145 * a synchronous one.
1146 */
1147 private static class UnlockSim extends Thread {
1148
1149 private final IccCard mSimCard;
1150
1151 private boolean mDone = false;
Wink Saville9de0f752013-10-22 19:04:03 -07001152 private int mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1153 private int mRetryCount = -1;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001154
1155 // For replies from SimCard interface
1156 private Handler mHandler;
1157
1158 // For async handler to identify request type
1159 private static final int SUPPLY_PIN_COMPLETE = 100;
1160
1161 public UnlockSim(IccCard simCard) {
1162 mSimCard = simCard;
1163 }
1164
1165 @Override
1166 public void run() {
1167 Looper.prepare();
1168 synchronized (UnlockSim.this) {
1169 mHandler = new Handler() {
1170 @Override
1171 public void handleMessage(Message msg) {
1172 AsyncResult ar = (AsyncResult) msg.obj;
1173 switch (msg.what) {
1174 case SUPPLY_PIN_COMPLETE:
1175 Log.d(LOG_TAG, "SUPPLY_PIN_COMPLETE");
1176 synchronized (UnlockSim.this) {
Wink Saville9de0f752013-10-22 19:04:03 -07001177 mRetryCount = msg.arg1;
1178 if (ar.exception != null) {
1179 if (ar.exception instanceof CommandException &&
1180 ((CommandException)(ar.exception)).getCommandError()
1181 == CommandException.Error.PASSWORD_INCORRECT) {
1182 mResult = PhoneConstants.PIN_PASSWORD_INCORRECT;
1183 } else {
1184 mResult = PhoneConstants.PIN_GENERAL_FAILURE;
1185 }
1186 } else {
1187 mResult = PhoneConstants.PIN_RESULT_SUCCESS;
1188 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001189 mDone = true;
1190 UnlockSim.this.notifyAll();
1191 }
1192 break;
1193 }
1194 }
1195 };
1196 UnlockSim.this.notifyAll();
1197 }
1198 Looper.loop();
1199 }
1200
1201 /*
1202 * Use PIN or PUK to unlock SIM card
1203 *
1204 * If PUK is null, unlock SIM card with PIN
1205 *
1206 * If PUK is not null, unlock SIM card with PUK and set PIN code
1207 */
Wink Saville9de0f752013-10-22 19:04:03 -07001208 synchronized int[] unlockSim(String puk, String pin) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001209
1210 while (mHandler == null) {
1211 try {
1212 wait();
1213 } catch (InterruptedException e) {
1214 Thread.currentThread().interrupt();
1215 }
1216 }
1217 Message callback = Message.obtain(mHandler, SUPPLY_PIN_COMPLETE);
1218
1219 if (puk == null) {
1220 mSimCard.supplyPin(pin, callback);
1221 } else {
1222 mSimCard.supplyPuk(puk, pin, callback);
1223 }
1224
1225 while (!mDone) {
1226 try {
1227 Log.d(LOG_TAG, "wait for done");
1228 wait();
1229 } catch (InterruptedException e) {
1230 // Restore the interrupted status
1231 Thread.currentThread().interrupt();
1232 }
1233 }
1234 Log.d(LOG_TAG, "done");
Wink Saville9de0f752013-10-22 19:04:03 -07001235 int[] resultArray = new int[2];
1236 resultArray[0] = mResult;
1237 resultArray[1] = mRetryCount;
1238 return resultArray;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001239 }
1240 }
1241
1242 public void updateServiceLocation() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001243 updateServiceLocationForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001244
1245 }
1246
Wink Savilleb564aae2014-10-23 10:18:09 -07001247 public void updateServiceLocationForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001248 // No permission check needed here: this call is harmless, and it's
1249 // needed for the ServiceState.requestStateUpdate() call (which is
1250 // already intentionally exposed to 3rd parties.)
Sanket Padawe356d7632015-06-22 14:03:32 -07001251 final Phone phone = getPhone(subId);
1252 if (phone != null) {
1253 phone.updateServiceLocation();
1254 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001255 }
1256
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001257 @Override
1258 public boolean isRadioOn(String callingPackage) {
1259 return isRadioOnForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001260 }
1261
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001262 @Override
1263 public boolean isRadioOnForSubscriber(int subId, String callingPackage) {
1264 if (!canReadPhoneState(callingPackage, "isRadioOnForSubscriber")) {
1265 return false;
1266 }
1267 return isRadioOnForSubscriber(subId);
1268 }
1269
1270 private boolean isRadioOnForSubscriber(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001271 final Phone phone = getPhone(subId);
1272 if (phone != null) {
1273 return phone.getServiceState().getState() != ServiceState.STATE_POWER_OFF;
1274 } else {
1275 return false;
1276 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001277 }
1278
1279 public void toggleRadioOnOff() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001280 toggleRadioOnOffForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001281
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001282 }
Wink Saville36469e72014-06-11 15:17:00 -07001283
Wink Savilleb564aae2014-10-23 10:18:09 -07001284 public void toggleRadioOnOffForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001285 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001286 final Phone phone = getPhone(subId);
1287 if (phone != null) {
1288 phone.setRadioPower(!isRadioOnForSubscriber(subId));
1289 }
Wink Saville36469e72014-06-11 15:17:00 -07001290 }
1291
1292 public boolean setRadio(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001293 return setRadioForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001294 }
1295
Wink Savilleb564aae2014-10-23 10:18:09 -07001296 public boolean setRadioForSubscriber(int subId, boolean turnOn) {
Wink Saville36469e72014-06-11 15:17:00 -07001297 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001298 final Phone phone = getPhone(subId);
1299 if (phone == null) {
1300 return false;
1301 }
1302 if ((phone.getServiceState().getState() !=
Wink Saville36469e72014-06-11 15:17:00 -07001303 ServiceState.STATE_POWER_OFF) != turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001304 toggleRadioOnOffForSubscriber(subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001305 }
1306 return true;
1307 }
Wink Saville36469e72014-06-11 15:17:00 -07001308
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001309 public boolean needMobileRadioShutdown() {
1310 /*
1311 * If any of the Radios are available, it will need to be
1312 * shutdown. So return true if any Radio is available.
1313 */
1314 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1315 Phone phone = PhoneFactory.getPhone(i);
1316 if (phone != null && phone.isRadioAvailable()) return true;
1317 }
1318 logv(TelephonyManager.getDefault().getPhoneCount() + " Phones are shutdown.");
1319 return false;
1320 }
1321
1322 public void shutdownMobileRadios() {
1323 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
1324 logv("Shutting down Phone " + i);
1325 shutdownRadioUsingPhoneId(i);
1326 }
1327 }
1328
1329 private void shutdownRadioUsingPhoneId(int phoneId) {
1330 enforceModifyPermission();
1331 Phone phone = PhoneFactory.getPhone(phoneId);
1332 if (phone != null && phone.isRadioAvailable()) {
1333 phone.shutdownRadio();
1334 }
1335 }
1336
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001337 public boolean setRadioPower(boolean turnOn) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001338 return setRadioPowerForSubscriber(getDefaultSubscription(), turnOn);
Wink Saville36469e72014-06-11 15:17:00 -07001339 }
1340
Wink Savilleb564aae2014-10-23 10:18:09 -07001341 public boolean setRadioPowerForSubscriber(int subId, boolean turnOn) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001342 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001343 final Phone phone = getPhone(subId);
1344 if (phone != null) {
1345 phone.setRadioPower(turnOn);
1346 return true;
1347 } else {
1348 return false;
1349 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001350 }
1351
Wink Saville36469e72014-06-11 15:17:00 -07001352 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001353 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001354 public boolean enableDataConnectivity() {
1355 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001356 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001357 final Phone phone = getPhone(subId);
1358 if (phone != null) {
1359 phone.setDataEnabled(true);
1360 return true;
1361 } else {
1362 return false;
1363 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001364 }
1365
Wink Saville36469e72014-06-11 15:17:00 -07001366 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001367 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001368 public boolean disableDataConnectivity() {
1369 enforceModifyPermission();
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001370 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001371 final Phone phone = getPhone(subId);
1372 if (phone != null) {
1373 phone.setDataEnabled(false);
1374 return true;
1375 } else {
1376 return false;
1377 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001378 }
1379
Wink Saville36469e72014-06-11 15:17:00 -07001380 // FIXME: subId version needed
Sanket Padawe356d7632015-06-22 14:03:32 -07001381 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001382 public boolean isDataConnectivityPossible() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001383 int subId = mSubscriptionController.getDefaultDataSubId();
Sanket Padawe356d7632015-06-22 14:03:32 -07001384 final Phone phone = getPhone(subId);
1385 if (phone != null) {
1386 return phone.isDataConnectivityPossible();
1387 } else {
1388 return false;
1389 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001390 }
1391
1392 public boolean handlePinMmi(String dialString) {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001393 return handlePinMmiForSubscriber(getDefaultSubscription(), dialString);
Wink Saville36469e72014-06-11 15:17:00 -07001394 }
1395
Wink Savilleb564aae2014-10-23 10:18:09 -07001396 public boolean handlePinMmiForSubscriber(int subId, String dialString) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001397 enforceModifyPermission();
Sanket Padawe356d7632015-06-22 14:03:32 -07001398 if (!SubscriptionManager.isValidSubscriptionId(subId)) {
1399 return false;
1400 }
Wink Saville36469e72014-06-11 15:17:00 -07001401 return (Boolean) sendRequest(CMD_HANDLE_PIN_MMI, dialString, subId);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001402 }
1403
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001404 public int getCallState() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001405 return getCallStateForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001406 }
1407
Shishir Agrawala9f32182016-04-12 12:00:16 -07001408 public int getCallStateForSlot(int slotId) {
1409 Phone phone = PhoneFactory.getPhone(slotId);
1410 return phone == null ? TelephonyManager.CALL_STATE_IDLE :
1411 DefaultPhoneNotifier.convertCallState(phone.getState());
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001412 }
1413
Sanket Padawe356d7632015-06-22 14:03:32 -07001414 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001415 public int getDataState() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001416 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001417 if (phone != null) {
1418 return DefaultPhoneNotifier.convertDataState(phone.getDataConnectionState());
1419 } else {
1420 return DefaultPhoneNotifier.convertDataState(PhoneConstants.DataState.DISCONNECTED);
1421 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001422 }
1423
Sanket Padawe356d7632015-06-22 14:03:32 -07001424 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001425 public int getDataActivity() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08001426 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001427 if (phone != null) {
1428 return DefaultPhoneNotifier.convertDataActivityState(phone.getDataActivityState());
1429 } else {
1430 return TelephonyManager.DATA_ACTIVITY_NONE;
1431 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001432 }
1433
1434 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001435 public Bundle getCellLocation(String callingPackage) {
1436 enforceFineOrCoarseLocationPermission("getCellLocation");
1437
1438 // OP_COARSE_LOCATION controls both fine and coarse location.
1439 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1440 callingPackage) != AppOpsManager.MODE_ALLOWED) {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001441 log("getCellLocation: returning null; mode != allowed");
Svetoslav64fad262015-04-14 14:35:21 -07001442 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001443 }
1444
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001445 if (checkIfCallerIsSelfOrForegroundUser() ||
1446 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001447 if (DBG_LOC) log("getCellLocation: is active user");
1448 Bundle data = new Bundle();
Legler Wu2c01cdf2014-12-08 19:00:59 +08001449 Phone phone = getPhone(mSubscriptionController.getDefaultDataSubId());
Sanket Padawe356d7632015-06-22 14:03:32 -07001450 if (phone == null) {
1451 return null;
1452 }
Legler Wu2c01cdf2014-12-08 19:00:59 +08001453 phone.getCellLocation().fillInNotifierBundle(data);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001454 return data;
1455 } else {
Amit Mahajan9acc70d2015-06-02 13:17:33 -07001456 log("getCellLocation: suppress non-active user");
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001457 return null;
1458 }
1459 }
1460
Svetoslav64fad262015-04-14 14:35:21 -07001461 private void enforceFineOrCoarseLocationPermission(String message) {
1462 try {
1463 mApp.enforceCallingOrSelfPermission(
1464 android.Manifest.permission.ACCESS_FINE_LOCATION, null);
1465 } catch (SecurityException e) {
1466 // If we have ACCESS_FINE_LOCATION permission, skip the check for ACCESS_COARSE_LOCATION
1467 // A failure should throw the SecurityException from ACCESS_COARSE_LOCATION since this
1468 // is the weaker precondition
1469 mApp.enforceCallingOrSelfPermission(
1470 android.Manifest.permission.ACCESS_COARSE_LOCATION, message);
1471 }
1472 }
1473
1474
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001475 @Override
1476 public void enableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001477 enableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001478 }
1479
Sanket Padawe356d7632015-06-22 14:03:32 -07001480 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001481 public void enableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001482 mApp.enforceCallingOrSelfPermission(
1483 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001484 final Phone phone = getPhone(subId);
1485 if (phone != null) {
1486 phone.enableLocationUpdates();
1487 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001488 }
1489
1490 @Override
1491 public void disableLocationUpdates() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001492 disableLocationUpdatesForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001493 }
1494
Sanket Padawe356d7632015-06-22 14:03:32 -07001495 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001496 public void disableLocationUpdatesForSubscriber(int subId) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001497 mApp.enforceCallingOrSelfPermission(
1498 android.Manifest.permission.CONTROL_LOCATION_UPDATES, null);
Sanket Padawe356d7632015-06-22 14:03:32 -07001499 final Phone phone = getPhone(subId);
1500 if (phone != null) {
1501 phone.disableLocationUpdates();
1502 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001503 }
1504
1505 @Override
1506 @SuppressWarnings("unchecked")
1507 public List<NeighboringCellInfo> getNeighboringCellInfo(String callingPackage) {
Svetoslav64fad262015-04-14 14:35:21 -07001508 enforceFineOrCoarseLocationPermission("getNeighboringCellInfo");
1509
1510 // OP_COARSE_LOCATION controls both fine and coarse location.
1511 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1512 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1513 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001514 }
1515
1516 if (mAppOps.noteOp(AppOpsManager.OP_NEIGHBORING_CELLS, Binder.getCallingUid(),
1517 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1518 return null;
1519 }
Svetoslav64fad262015-04-14 14:35:21 -07001520
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001521 if (checkIfCallerIsSelfOrForegroundUser() ||
1522 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001523 if (DBG_LOC) log("getNeighboringCellInfo: is active user");
1524
1525 ArrayList<NeighboringCellInfo> cells = null;
1526
1527 try {
1528 cells = (ArrayList<NeighboringCellInfo>) sendRequest(
Sanket Padawe56e75a32016-02-08 12:18:19 -08001529 CMD_HANDLE_NEIGHBORING_CELL, null,
1530 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001531 } catch (RuntimeException e) {
Wink Saville36469e72014-06-11 15:17:00 -07001532 Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001533 }
1534 return cells;
1535 } else {
1536 if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
1537 return null;
1538 }
1539 }
1540
1541
1542 @Override
Svetoslav64fad262015-04-14 14:35:21 -07001543 public List<CellInfo> getAllCellInfo(String callingPackage) {
1544 enforceFineOrCoarseLocationPermission("getAllCellInfo");
1545
1546 // OP_COARSE_LOCATION controls both fine and coarse location.
1547 if (mAppOps.noteOp(AppOpsManager.OP_COARSE_LOCATION, Binder.getCallingUid(),
1548 callingPackage) != AppOpsManager.MODE_ALLOWED) {
1549 return null;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001550 }
1551
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001552 if (checkIfCallerIsSelfOrForegroundUser() ||
1553 checkCallerInteractAcrossUsersFull()) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001554 if (DBG_LOC) log("getAllCellInfo: is active user");
Legler Wu2c01cdf2014-12-08 19:00:59 +08001555 List<CellInfo> cellInfos = new ArrayList<CellInfo>();
1556 for (Phone phone : PhoneFactory.getPhones()) {
Robert Greenwaltd9eb7da2015-08-19 12:18:12 -07001557 final List<CellInfo> info = phone.getAllCellInfo();
1558 if (info != null) cellInfos.addAll(phone.getAllCellInfo());
Legler Wu2c01cdf2014-12-08 19:00:59 +08001559 }
1560 return cellInfos;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001561 } else {
1562 if (DBG_LOC) log("getAllCellInfo: suppress non-active user");
1563 return null;
1564 }
1565 }
1566
Sailesh Nepalbd76e4e2013-10-27 13:59:44 -07001567 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001568 public void setCellInfoListRate(int rateInMillis) {
1569 mPhone.setCellInfoListRate(rateInMillis);
1570 }
1571
Shishir Agrawala9f32182016-04-12 12:00:16 -07001572 @Override
1573 public String getImeiForSlot(int slotId, String callingPackage) {
1574 if (!canReadPhoneState(callingPackage, "getImeiForSlot")) {
1575 return null;
1576 }
1577 Phone phone = PhoneFactory.getPhone(slotId);
1578 return phone == null ? null : phone.getImei();
1579 }
1580
1581 @Override
1582 public String getDeviceSoftwareVersionForSlot(int slotId, String callingPackage) {
1583 if (!canReadPhoneState(callingPackage, "getDeviceSoftwareVersionForSlot")) {
1584 return null;
1585 }
1586 Phone phone = PhoneFactory.getPhone(slotId);
1587 return phone == null ? null : phone.getDeviceSvn();
1588 }
1589
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001590 //
1591 // Internal helper methods.
1592 //
1593
Sanket Padaweee13a9b2016-03-08 17:30:28 -08001594 /**
1595 * Returns true if the caller holds INTERACT_ACROSS_USERS_FULL.
1596 */
1597 private boolean checkCallerInteractAcrossUsersFull() {
1598 return mPhone.getContext().checkCallingOrSelfPermission(
1599 android.Manifest.permission.INTERACT_ACROSS_USERS_FULL)
1600 == PackageManager.PERMISSION_GRANTED;
1601 }
1602
Jake Hambye994d462014-02-03 13:10:13 -08001603 private static boolean checkIfCallerIsSelfOrForegroundUser() {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001604 boolean ok;
1605
1606 boolean self = Binder.getCallingUid() == Process.myUid();
1607 if (!self) {
1608 // Get the caller's user id then clear the calling identity
1609 // which will be restored in the finally clause.
1610 int callingUser = UserHandle.getCallingUserId();
1611 long ident = Binder.clearCallingIdentity();
1612
1613 try {
1614 // With calling identity cleared the current user is the foreground user.
1615 int foregroundUser = ActivityManager.getCurrentUser();
1616 ok = (foregroundUser == callingUser);
1617 if (DBG_LOC) {
1618 log("checkIfCallerIsSelfOrForegoundUser: foregroundUser=" + foregroundUser
1619 + " callingUser=" + callingUser + " ok=" + ok);
1620 }
1621 } catch (Exception ex) {
1622 if (DBG_LOC) loge("checkIfCallerIsSelfOrForegoundUser: Exception ex=" + ex);
1623 ok = false;
1624 } finally {
1625 Binder.restoreCallingIdentity(ident);
1626 }
1627 } else {
1628 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: is self");
1629 ok = true;
1630 }
1631 if (DBG_LOC) log("checkIfCallerIsSelfOrForegoundUser: ret=" + ok);
1632 return ok;
1633 }
1634
1635 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001636 * Make sure the caller has the MODIFY_PHONE_STATE permission.
1637 *
1638 * @throws SecurityException if the caller does not have the required permission
1639 */
1640 private void enforceModifyPermission() {
1641 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE, null);
1642 }
1643
1644 /**
Junda Liua2e36012014-07-09 18:30:01 -07001645 * Make sure either system app or the caller has carrier privilege.
1646 *
1647 * @throws SecurityException if the caller does not have the required permission/privilege
1648 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001649 private void enforceModifyPermissionOrCarrierPrivilege(int subId) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001650 int permission = mApp.checkCallingOrSelfPermission(
1651 android.Manifest.permission.MODIFY_PHONE_STATE);
1652 if (permission == PackageManager.PERMISSION_GRANTED) {
1653 return;
1654 }
1655
1656 log("No modify permission, check carrier privilege next.");
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001657 enforceCarrierPrivilege(subId);
Junda Liua2e36012014-07-09 18:30:01 -07001658 }
1659
1660 /**
1661 * Make sure the caller has carrier privilege.
1662 *
1663 * @throws SecurityException if the caller does not have the required permission
1664 */
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001665 private void enforceCarrierPrivilege(int subId) {
1666 if (getCarrierPrivilegeStatus(subId) !=
1667 TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
Shishir Agrawalf1ac4c92014-07-14 13:54:28 -07001668 loge("No Carrier Privilege.");
1669 throw new SecurityException("No Carrier Privilege.");
Junda Liua2e36012014-07-09 18:30:01 -07001670 }
1671 }
1672
1673 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001674 * Make sure the caller has the CALL_PHONE permission.
1675 *
1676 * @throws SecurityException if the caller does not have the required permission
1677 */
1678 private void enforceCallPermission() {
1679 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.CALL_PHONE, null);
1680 }
1681
Stuart Scott8eef64f2015-04-08 15:13:54 -07001682 private void enforceConnectivityInternalPermission() {
1683 mApp.enforceCallingOrSelfPermission(
1684 android.Manifest.permission.CONNECTIVITY_INTERNAL,
1685 "ConnectivityService");
1686 }
1687
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001688 private String createTelUrl(String number) {
1689 if (TextUtils.isEmpty(number)) {
1690 return null;
1691 }
1692
Jake Hambye994d462014-02-03 13:10:13 -08001693 return "tel:" + number;
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001694 }
1695
Ihab Awadf9e92732013-12-05 18:02:52 -08001696 private static void log(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001697 Log.d(LOG_TAG, "[PhoneIntfMgr] " + msg);
1698 }
1699
Naveen Kalla1fd79bd2014-08-08 00:48:59 -07001700 private static void logv(String msg) {
1701 Log.v(LOG_TAG, "[PhoneIntfMgr] " + msg);
1702 }
1703
Ihab Awadf9e92732013-12-05 18:02:52 -08001704 private static void loge(String msg) {
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001705 Log.e(LOG_TAG, "[PhoneIntfMgr] " + msg);
1706 }
1707
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001708 @Override
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001709 public int getActivePhoneType() {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001710 return getActivePhoneTypeForSlot(getSlotForDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001711 }
1712
Sanket Padawe356d7632015-06-22 14:03:32 -07001713 @Override
Shishir Agrawala9f32182016-04-12 12:00:16 -07001714 public int getActivePhoneTypeForSlot(int slotId) {
1715 final Phone phone = PhoneFactory.getPhone(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001716 if (phone == null) {
1717 return PhoneConstants.PHONE_TYPE_NONE;
1718 } else {
Shishir Agrawala9f32182016-04-12 12:00:16 -07001719 return phone.getPhoneType();
Sanket Padawe356d7632015-06-22 14:03:32 -07001720 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001721 }
1722
1723 /**
1724 * Returns the CDMA ERI icon index to display
1725 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001726 @Override
1727 public int getCdmaEriIconIndex(String callingPackage) {
1728 return getCdmaEriIconIndexForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001729 }
1730
Sanket Padawe356d7632015-06-22 14:03:32 -07001731 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001732 public int getCdmaEriIconIndexForSubscriber(int subId, String callingPackage) {
1733 if (!canReadPhoneState(callingPackage, "getCdmaEriIconIndexForSubscriber")) {
1734 return -1;
1735 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001736 final Phone phone = getPhone(subId);
1737 if (phone != null) {
1738 return phone.getCdmaEriIconIndex();
1739 } else {
1740 return -1;
1741 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001742 }
1743
1744 /**
1745 * Returns the CDMA ERI icon mode,
1746 * 0 - ON
1747 * 1 - FLASHING
1748 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001749 @Override
1750 public int getCdmaEriIconMode(String callingPackage) {
1751 return getCdmaEriIconModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001752 }
1753
Sanket Padawe356d7632015-06-22 14:03:32 -07001754 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001755 public int getCdmaEriIconModeForSubscriber(int subId, String callingPackage) {
1756 if (!canReadPhoneState(callingPackage, "getCdmaEriIconModeForSubscriber")) {
1757 return -1;
1758 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001759 final Phone phone = getPhone(subId);
1760 if (phone != null) {
1761 return phone.getCdmaEriIconMode();
1762 } else {
1763 return -1;
1764 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001765 }
1766
1767 /**
1768 * Returns the CDMA ERI text,
1769 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001770 @Override
1771 public String getCdmaEriText(String callingPackage) {
1772 return getCdmaEriTextForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001773 }
1774
Sanket Padawe356d7632015-06-22 14:03:32 -07001775 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001776 public String getCdmaEriTextForSubscriber(int subId, String callingPackage) {
1777 if (!canReadPhoneState(callingPackage, "getCdmaEriIconTextForSubscriber")) {
1778 return null;
1779 }
Sanket Padawe356d7632015-06-22 14:03:32 -07001780 final Phone phone = getPhone(subId);
1781 if (phone != null) {
1782 return phone.getCdmaEriText();
1783 } else {
1784 return null;
1785 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001786 }
1787
1788 /**
Junda Liuca05d5d2014-08-14 22:36:34 -07001789 * Returns the CDMA MDN.
1790 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001791 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001792 public String getCdmaMdn(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001793 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001794 final Phone phone = getPhone(subId);
1795 if (mPhone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA && phone != null) {
1796 return phone.getLine1Number();
Junda Liuca05d5d2014-08-14 22:36:34 -07001797 } else {
1798 return null;
1799 }
1800 }
1801
1802 /**
1803 * Returns the CDMA MIN.
1804 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001805 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001806 public String getCdmaMin(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001807 enforceModifyPermissionOrCarrierPrivilege(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001808 final Phone phone = getPhone(subId);
1809 if (phone != null && phone.getPhoneType() == PhoneConstants.PHONE_TYPE_CDMA) {
1810 return phone.getCdmaMin();
Junda Liuca05d5d2014-08-14 22:36:34 -07001811 } else {
1812 return null;
1813 }
1814 }
1815
1816 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001817 * Returns true if CDMA provisioning needs to run.
1818 */
1819 public boolean needsOtaServiceProvisioning() {
1820 return mPhone.needsOtaServiceProvisioning();
1821 }
1822
1823 /**
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001824 * Sets the voice mail number of a given subId.
1825 */
1826 @Override
1827 public boolean setVoiceMailNumber(int subId, String alphaTag, String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08001828 enforceCarrierPrivilege(subId);
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001829 Boolean success = (Boolean) sendRequest(CMD_SET_VOICEMAIL_NUMBER,
1830 new Pair<String, String>(alphaTag, number), new Integer(subId));
1831 return success;
1832 }
1833
Ta-wei Yen87c49842016-05-13 21:19:52 -07001834 @Override
1835 public void setVisualVoicemailSmsFilterEnabled(int subId, boolean value) {
1836 VisualVoicemailSmsFilterConfig
1837 .setVisualVoicemailSmsFilterEnabled(mPhone.getContext(), subId, value);
1838 }
1839
1840 @Override
1841 public boolean isVisualVoicemailSmsFilterEnabled(String packageName, int subId) {
1842 return VisualVoicemailSmsFilterConfig
1843 .isVisualVoicemailSmsFilterEnabled(mPhone.getContext(), packageName, subId);
1844 }
1845
1846 @Override
1847 public void setVisualVoicemailSmsFilterClientPrefix(int subId, String prefix) {
1848 VisualVoicemailSmsFilterConfig
1849 .setVisualVoicemailSmsFilterClientPrefix(mPhone.getContext(), subId, prefix);
1850 }
1851
1852 @Override
1853 public String getVisualVoicemailSmsFilterClientPrefix(String packageName, int subId) {
1854 return VisualVoicemailSmsFilterConfig
1855 .getVisualVoicemailSmsFilterClientPrefix(mPhone.getContext(), packageName, subId);
1856 }
1857
1858 @Override
1859 public void setVisualVoicemailSmsFilterOriginatingNumbers(int subId, String[] numbers) {
1860 VisualVoicemailSmsFilterConfig
1861 .setVisualVoicemailSmsFilterOriginatingNumbers(mPhone.getContext(), subId, numbers);
1862 }
1863
1864 @Override
1865 public String[] getVisualVoicemailSmsFilterOriginatingNumbers(String packageName, int subId) {
1866 return VisualVoicemailSmsFilterConfig
1867 .getVisualVoicemailSmsFilterOriginatingNumbers(mPhone.getContext(), packageName,
1868 subId);
1869 }
1870
1871 @Override
1872 public void setVisualVoicemailSmsFilterDestinationPort(int subId, int port) {
1873 VisualVoicemailSmsFilterConfig
1874 .setVisualVoicemailSmsFilterDestinationPort(mPhone.getContext(), subId, port);
1875 }
1876
1877 @Override
1878 public int getVisualVoicemailSmsFilterDestinationPort(String packageName, int subId) {
1879 return VisualVoicemailSmsFilterConfig
1880 .getVisualVoicemailSmsFilterDestinationPort(mPhone.getContext(), packageName,
1881 subId);
1882 }
1883
Shishir Agrawal76d5da92014-11-09 16:17:25 -08001884 /**
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001885 * Returns the unread count of voicemails
1886 */
1887 public int getVoiceMessageCount() {
Wink Savilleadd7cc52014-09-08 14:23:09 -07001888 return getVoiceMessageCountForSubscriber(getDefaultSubscription());
Wink Saville36469e72014-06-11 15:17:00 -07001889 }
1890
1891 /**
1892 * Returns the unread count of voicemails for a subId
1893 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001894 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001895 public int getVoiceMessageCountForSubscriber( int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001896 final Phone phone = getPhone(subId);
1897 if (phone != null) {
1898 return phone.getVoiceMessageCount();
1899 } else {
1900 return 0;
1901 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001902 }
1903
1904 /**
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001905 * Returns the data network type.
1906 * Legacy call, permission-free.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001907 *
1908 * @Deprecated to be removed Q3 2013 use {@link #getDataNetworkType}.
1909 */
1910 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001911 public int getNetworkType() {
1912 final Phone phone = getPhone(getDefaultSubscription());
1913 if (phone != null) {
1914 return phone.getServiceState().getDataNetworkType();
1915 } else {
1916 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1917 }
Wink Saville36469e72014-06-11 15:17:00 -07001918 }
1919
1920 /**
1921 * Returns the network type for a subId
1922 */
1923 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001924 public int getNetworkTypeForSubscriber(int subId, String callingPackage) {
1925 if (!canReadPhoneState(callingPackage, "getNetworkTypeForSubscriber")) {
1926 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1927 }
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001928
Sanket Padawe356d7632015-06-22 14:03:32 -07001929 final Phone phone = getPhone(subId);
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001930 if (phone != null) {
Sanket Padawe356d7632015-06-22 14:03:32 -07001931 return phone.getServiceState().getDataNetworkType();
1932 } else {
1933 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1934 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001935 }
1936
1937 /**
1938 * Returns the data network type
1939 */
1940 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001941 public int getDataNetworkType(String callingPackage) {
1942 return getDataNetworkTypeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07001943 }
1944
1945 /**
1946 * Returns the data network type for a subId
1947 */
1948 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07001949 public int getDataNetworkTypeForSubscriber(int subId, String callingPackage) {
1950 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1951 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1952 }
1953
Sanket Padawe356d7632015-06-22 14:03:32 -07001954 final Phone phone = getPhone(subId);
1955 if (phone != null) {
1956 return phone.getServiceState().getDataNetworkType();
1957 } else {
1958 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1959 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001960 }
1961
1962 /**
Wink Saville36469e72014-06-11 15:17:00 -07001963 * Returns the Voice network type for a subId
1964 */
1965 @Override
Robert Greenwalta5dcfcb2015-07-10 09:06:29 -07001966 public int getVoiceNetworkTypeForSubscriber(int subId, String callingPackage) {
1967 if (!canReadPhoneState(callingPackage, "getDataNetworkTypeForSubscriber")) {
1968 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1969 }
1970
Sanket Padawe356d7632015-06-22 14:03:32 -07001971 final Phone phone = getPhone(subId);
1972 if (phone != null) {
1973 return phone.getServiceState().getVoiceNetworkType();
1974 } else {
1975 return TelephonyManager.NETWORK_TYPE_UNKNOWN;
1976 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001977 }
1978
1979 /**
1980 * @return true if a ICC card is present
1981 */
1982 public boolean hasIccCard() {
Wink Saville36469e72014-06-11 15:17:00 -07001983 // FIXME Make changes to pass defaultSimId of type int
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001984 return hasIccCardUsingSlotId(mSubscriptionController.getSlotId(getDefaultSubscription()));
Wink Saville36469e72014-06-11 15:17:00 -07001985 }
1986
1987 /**
1988 * @return true if a ICC card is present for a slotId
1989 */
Sanket Padawe356d7632015-06-22 14:03:32 -07001990 @Override
Wink Savilleb564aae2014-10-23 10:18:09 -07001991 public boolean hasIccCardUsingSlotId(int slotId) {
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001992 int subId[] = mSubscriptionController.getSubIdUsingSlotId(slotId);
Sanket Padawe356d7632015-06-22 14:03:32 -07001993 final Phone phone = getPhone(subId[0]);
1994 if (subId != null && phone != null) {
1995 return phone.getIccCard().hasIccCard();
Amit Mahajana6fc2a82015-01-06 11:53:51 -08001996 } else {
1997 return false;
1998 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07001999 }
2000
2001 /**
2002 * Return if the current radio is LTE on CDMA. This
2003 * is a tri-state return value as for a period of time
2004 * the mode may be unknown.
2005 *
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002006 * @param callingPackage the name of the package making the call.
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002007 * @return {@link Phone#LTE_ON_CDMA_UNKNOWN}, {@link Phone#LTE_ON_CDMA_FALSE}
Jake Hambye994d462014-02-03 13:10:13 -08002008 * or {@link Phone#LTE_ON_CDMA_TRUE}
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002009 */
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002010 @Override
2011 public int getLteOnCdmaMode(String callingPackage) {
2012 return getLteOnCdmaModeForSubscriber(getDefaultSubscription(), callingPackage);
Wink Saville36469e72014-06-11 15:17:00 -07002013 }
2014
Sanket Padawe356d7632015-06-22 14:03:32 -07002015 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002016 public int getLteOnCdmaModeForSubscriber(int subId, String callingPackage) {
2017 if (!canReadPhoneState(callingPackage, "getLteOnCdmaModeForSubscriber")) {
2018 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2019 }
2020
Sanket Padawe356d7632015-06-22 14:03:32 -07002021 final Phone phone = getPhone(subId);
2022 if (phone == null) {
2023 return PhoneConstants.LTE_ON_CDMA_UNKNOWN;
2024 } else {
2025 return phone.getLteOnCdmaMode();
2026 }
Wink Saville36469e72014-06-11 15:17:00 -07002027 }
2028
2029 public void setPhone(Phone phone) {
2030 mPhone = phone;
2031 }
2032
2033 /**
2034 * {@hide}
2035 * Returns Default subId, 0 in the case of single standby.
2036 */
Wink Savilleb564aae2014-10-23 10:18:09 -07002037 private int getDefaultSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002038 return mSubscriptionController.getDefaultSubId();
Wink Saville36469e72014-06-11 15:17:00 -07002039 }
2040
Shishir Agrawala9f32182016-04-12 12:00:16 -07002041 private int getSlotForDefaultSubscription() {
2042 return mSubscriptionController.getPhoneId(getDefaultSubscription());
2043 }
2044
Wink Savilleb564aae2014-10-23 10:18:09 -07002045 private int getPreferredVoiceSubscription() {
Wink Savilleac1bdfd2014-11-20 23:04:44 -08002046 return mSubscriptionController.getDefaultVoiceSubId();
Santos Cordon7d4ddf62013-07-10 11:58:08 -07002047 }
Ihab Awadf2177b72013-11-25 13:33:23 -08002048
2049 /**
2050 * @see android.telephony.TelephonyManager.WifiCallingChoices
2051 */
2052 public int getWhenToMakeWifiCalls() {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002053 return Settings.System.getInt(mPhone.getContext().getContentResolver(),
2054 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, getWhenToMakeWifiCallsDefaultPreference());
Ihab Awadf2177b72013-11-25 13:33:23 -08002055 }
2056
2057 /**
2058 * @see android.telephony.TelephonyManager.WifiCallingChoices
2059 */
2060 public void setWhenToMakeWifiCalls(int preference) {
Sailesh Nepald1e68152013-12-12 19:08:02 -08002061 if (DBG) log("setWhenToMakeWifiCallsStr, storing setting = " + preference);
2062 Settings.System.putInt(mPhone.getContext().getContentResolver(),
2063 Settings.System.WHEN_TO_MAKE_WIFI_CALLS, preference);
Ihab Awadf9e92732013-12-05 18:02:52 -08002064 }
2065
Sailesh Nepald1e68152013-12-12 19:08:02 -08002066 private static int getWhenToMakeWifiCallsDefaultPreference() {
Santos Cordonda120f42014-08-06 04:44:34 -07002067 // TODO: Use a build property to choose this value.
Evan Charlton9829e882013-12-19 15:30:38 -08002068 return TelephonyManager.WifiCallingChoices.ALWAYS_USE;
Ihab Awadf2177b72013-11-25 13:33:23 -08002069 }
Shishir Agrawal69f68122013-12-16 17:25:49 -08002070
Shishir Agrawal566b7612013-10-28 14:41:00 -07002071 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002072 public IccOpenLogicalChannelResponse iccOpenLogicalChannel(int subId, String AID) {
2073 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002074
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002075 if (DBG) log("iccOpenLogicalChannel: subId=" + subId + " aid=" + AID);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002076 IccOpenLogicalChannelResponse response = (IccOpenLogicalChannelResponse)sendRequest(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002077 CMD_OPEN_CHANNEL, AID, subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002078 if (DBG) log("iccOpenLogicalChannel: " + response);
2079 return response;
Shishir Agrawal566b7612013-10-28 14:41:00 -07002080 }
2081
2082 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002083 public boolean iccCloseLogicalChannel(int subId, int channel) {
2084 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002085
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002086 if (DBG) log("iccCloseLogicalChannel: subId=" + subId + " chnl=" + channel);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002087 if (channel < 0) {
2088 return false;
2089 }
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002090 Boolean success = (Boolean)sendRequest(CMD_CLOSE_CHANNEL, channel, subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002091 if (DBG) log("iccCloseLogicalChannel: " + success);
2092 return success;
2093 }
2094
2095 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002096 public String iccTransmitApduLogicalChannel(int subId, int channel, int cla,
Shishir Agrawal566b7612013-10-28 14:41:00 -07002097 int command, int p1, int p2, int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002098 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002099
2100 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002101 log("iccTransmitApduLogicalChannel: subId=" + subId + " chnl=" + channel +
2102 " cla=" + cla + " cmd=" + command + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 +
Shishir Agrawal566b7612013-10-28 14:41:00 -07002103 " data=" + data);
2104 }
2105
2106 if (channel < 0) {
2107 return "";
2108 }
2109
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002110 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_LOGICAL_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002111 new IccAPDUArgument(channel, cla, command, p1, p2, p3, data), subId);
Shishir Agrawal566b7612013-10-28 14:41:00 -07002112 if (DBG) log("iccTransmitApduLogicalChannel: " + response);
2113
Shishir Agrawal566b7612013-10-28 14:41:00 -07002114 // Append the returned status code to the end of the response payload.
2115 String s = Integer.toHexString(
2116 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002117 if (response.payload != null) {
2118 s = IccUtils.bytesToHexString(response.payload) + s;
2119 }
Shishir Agrawal566b7612013-10-28 14:41:00 -07002120 return s;
2121 }
Jake Hambye994d462014-02-03 13:10:13 -08002122
Evan Charltonc66da362014-05-16 14:06:40 -07002123 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002124 public String iccTransmitApduBasicChannel(int subId, int cla, int command, int p1, int p2,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002125 int p3, String data) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002126 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002127
2128 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002129 log("iccTransmitApduBasicChannel: subId=" + subId + " cla=" + cla + " cmd=" + command
2130 + " p1=" + p1 + " p2=" + p2 + " p3=" + p3 + " data=" + data);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002131 }
2132
2133 IccIoResult response = (IccIoResult)sendRequest(CMD_TRANSMIT_APDU_BASIC_CHANNEL,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002134 new IccAPDUArgument(0, cla, command, p1, p2, p3, data), subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002135 if (DBG) log("iccTransmitApduBasicChannel: " + response);
2136
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002137 // Append the returned status code to the end of the response payload.
2138 String s = Integer.toHexString(
2139 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
Shishir Agrawal5ec14172014-08-05 17:05:45 -07002140 if (response.payload != null) {
2141 s = IccUtils.bytesToHexString(response.payload) + s;
2142 }
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002143 return s;
2144 }
2145
2146 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002147 public byte[] iccExchangeSimIO(int subId, int fileID, int command, int p1, int p2, int p3,
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002148 String filePath) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002149 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002150
2151 if (DBG) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002152 log("Exchange SIM_IO " + subId + ":" + fileID + ":" + command + " " +
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002153 p1 + " " + p2 + " " + p3 + ":" + filePath);
2154 }
2155
2156 IccIoResult response =
2157 (IccIoResult)sendRequest(CMD_EXCHANGE_SIM_IO,
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002158 new IccAPDUArgument(-1, fileID, command, p1, p2, p3, filePath),
2159 subId);
Shishir Agrawalda0bb0d2014-07-29 21:18:53 -07002160
2161 if (DBG) {
2162 log("Exchange SIM_IO [R]" + response);
2163 }
2164
2165 byte[] result = null;
2166 int length = 2;
2167 if (response.payload != null) {
2168 length = 2 + response.payload.length;
2169 result = new byte[length];
2170 System.arraycopy(response.payload, 0, result, 0, response.payload.length);
2171 } else {
2172 result = new byte[length];
2173 }
2174
2175 result[length - 1] = (byte) response.sw2;
2176 result[length - 2] = (byte) response.sw1;
2177 return result;
2178 }
2179
2180 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002181 public String sendEnvelopeWithStatus(int subId, String content) {
2182 enforceModifyPermissionOrCarrierPrivilege(subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002183
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002184 IccIoResult response = (IccIoResult)sendRequest(CMD_SEND_ENVELOPE, content, subId);
Evan Charltonc66da362014-05-16 14:06:40 -07002185 if (response.payload == null) {
2186 return "";
2187 }
2188
2189 // Append the returned status code to the end of the response payload.
2190 String s = Integer.toHexString(
2191 (response.sw1 << 8) + response.sw2 + 0x10000).substring(1);
2192 s = IccUtils.bytesToHexString(response.payload) + s;
2193 return s;
2194 }
2195
Jake Hambye994d462014-02-03 13:10:13 -08002196 /**
2197 * Read one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2198 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2199 *
2200 * @param itemID the ID of the item to read
2201 * @return the NV item as a String, or null on error.
2202 */
2203 @Override
2204 public String nvReadItem(int itemID) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002205 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002206 if (DBG) log("nvReadItem: item " + itemID);
2207 String value = (String) sendRequest(CMD_NV_READ_ITEM, itemID);
2208 if (DBG) log("nvReadItem: item " + itemID + " is \"" + value + '"');
2209 return value;
2210 }
2211
2212 /**
2213 * Write one of the NV items defined in {@link com.android.internal.telephony.RadioNVItems}
2214 * and {@code ril_nv_items.h}. Used for device configuration by some CDMA operators.
2215 *
2216 * @param itemID the ID of the item to read
2217 * @param itemValue the value to write, as a String
2218 * @return true on success; false on any failure
2219 */
2220 @Override
2221 public boolean nvWriteItem(int itemID, String itemValue) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002222 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002223 if (DBG) log("nvWriteItem: item " + itemID + " value \"" + itemValue + '"');
2224 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_ITEM,
2225 new Pair<Integer, String>(itemID, itemValue));
2226 if (DBG) log("nvWriteItem: item " + itemID + ' ' + (success ? "ok" : "fail"));
2227 return success;
2228 }
2229
2230 /**
2231 * Update the CDMA Preferred Roaming List (PRL) in the radio NV storage.
2232 * Used for device configuration by some CDMA operators.
2233 *
2234 * @param preferredRoamingList byte array containing the new PRL
2235 * @return true on success; false on any failure
2236 */
2237 @Override
2238 public boolean nvWriteCdmaPrl(byte[] preferredRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002239 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002240 if (DBG) log("nvWriteCdmaPrl: value: " + HexDump.toHexString(preferredRoamingList));
2241 Boolean success = (Boolean) sendRequest(CMD_NV_WRITE_CDMA_PRL, preferredRoamingList);
2242 if (DBG) log("nvWriteCdmaPrl: " + (success ? "ok" : "fail"));
2243 return success;
2244 }
2245
2246 /**
2247 * Perform the specified type of NV config reset.
2248 * Used for device configuration by some CDMA operators.
2249 *
2250 * @param resetType the type of reset to perform (1 == factory reset; 2 == NV-only reset)
2251 * @return true on success; false on any failure
2252 */
2253 @Override
2254 public boolean nvResetConfig(int resetType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002255 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
Jake Hambye994d462014-02-03 13:10:13 -08002256 if (DBG) log("nvResetConfig: type " + resetType);
2257 Boolean success = (Boolean) sendRequest(CMD_NV_RESET_CONFIG, resetType);
2258 if (DBG) log("nvResetConfig: type " + resetType + ' ' + (success ? "ok" : "fail"));
2259 return success;
2260 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002261
2262 /**
Wink Saville36469e72014-06-11 15:17:00 -07002263 * {@hide}
2264 * Returns Default sim, 0 in the case of single standby.
2265 */
2266 public int getDefaultSim() {
2267 //TODO Need to get it from Telephony Devcontroller
2268 return 0;
2269 }
2270
Svet Ganovb320e182015-04-16 12:30:10 -07002271 public String[] getPcscfAddress(String apnType, String callingPackage) {
2272 if (!canReadPhoneState(callingPackage, "getPcscfAddress")) {
2273 return new String[0];
2274 }
2275
2276
ram87fca6f2014-07-18 18:58:44 +05302277 return mPhone.getPcscfAddress(apnType);
Wink Saville36469e72014-06-11 15:17:00 -07002278 }
2279
2280 public void setImsRegistrationState(boolean registered) {
2281 enforceModifyPermission();
2282 mPhone.setImsRegistrationState(registered);
2283 }
2284
2285 /**
Stuart Scott54788802015-03-30 13:18:01 -07002286 * Set the network selection mode to automatic.
2287 *
2288 */
2289 @Override
2290 public void setNetworkSelectionModeAutomatic(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002291 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002292 if (DBG) log("setNetworkSelectionModeAutomatic: subId " + subId);
2293 sendRequest(CMD_SET_NETWORK_SELECTION_MODE_AUTOMATIC, null, subId);
2294 }
2295
2296 /**
Shishir Agrawal302c8692015-06-19 13:49:39 -07002297 * Set the network selection mode to manual with the selected carrier.
2298 */
2299 @Override
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002300 public boolean setNetworkSelectionModeManual(int subId, OperatorInfo operator,
2301 boolean persistSelection) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002302 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002303 if (DBG) log("setNetworkSelectionModeManual: subId:" + subId + " operator:" + operator);
Shishir Agrawal77ba3172015-09-10 14:50:19 -07002304 ManualNetworkSelectionArgument arg = new ManualNetworkSelectionArgument(operator,
2305 persistSelection);
2306 return (Boolean) sendRequest(CMD_SET_NETWORK_SELECTION_MODE_MANUAL, arg, subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002307 }
2308
2309 /**
2310 * Scans for available networks.
2311 */
2312 @Override
2313 public CellNetworkScanResult getCellNetworkScanResults(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002314 enforceModifyPermissionOrCarrierPrivilege(subId);
Shishir Agrawal302c8692015-06-19 13:49:39 -07002315 if (DBG) log("getCellNetworkScanResults: subId " + subId);
2316 CellNetworkScanResult result = (CellNetworkScanResult) sendRequest(
2317 CMD_PERFORM_NETWORK_SCAN, null, subId);
2318 return result;
2319 }
2320
2321 /**
Junda Liu84d15a22014-07-02 11:21:04 -07002322 * Get the calculated preferred network type.
2323 * Used for debugging incorrect network type.
2324 *
2325 * @return the preferred network type, defined in RILConstants.java.
2326 */
2327 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002328 public int getCalculatedPreferredNetworkType(String callingPackage) {
2329 if (!canReadPhoneState(callingPackage, "getCalculatedPreferredNetworkType")) {
2330 return RILConstants.PREFERRED_NETWORK_MODE;
2331 }
2332
Amit Mahajan43330e02014-11-18 11:54:45 -08002333 return PhoneFactory.calculatePreferredNetworkType(mPhone.getContext(), 0); // wink FIXME: need to get SubId from somewhere.
Junda Liu84d15a22014-07-02 11:21:04 -07002334 }
2335
2336 /**
Jake Hamby7c27be32014-03-03 13:25:59 -08002337 * Get the preferred network type.
2338 * Used for device configuration by some CDMA operators.
2339 *
2340 * @return the preferred network type, defined in RILConstants.java.
2341 */
2342 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002343 public int getPreferredNetworkType(int subId) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002344 enforceModifyPermissionOrCarrierPrivilege(subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002345 if (DBG) log("getPreferredNetworkType");
Stuart Scott54788802015-03-30 13:18:01 -07002346 int[] result = (int[]) sendRequest(CMD_GET_PREFERRED_NETWORK_TYPE, null, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002347 int networkType = (result != null ? result[0] : -1);
2348 if (DBG) log("getPreferredNetworkType: " + networkType);
2349 return networkType;
2350 }
2351
2352 /**
2353 * Set the preferred network type.
2354 * Used for device configuration by some CDMA operators.
2355 *
2356 * @param networkType the preferred network type, defined in RILConstants.java.
2357 * @return true on success; false on any failure.
2358 */
2359 @Override
Stuart Scott54788802015-03-30 13:18:01 -07002360 public boolean setPreferredNetworkType(int subId, int networkType) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002361 enforceModifyPermissionOrCarrierPrivilege(subId);
Stuart Scott54788802015-03-30 13:18:01 -07002362 if (DBG) log("setPreferredNetworkType: subId " + subId + " type " + networkType);
2363 Boolean success = (Boolean) sendRequest(CMD_SET_PREFERRED_NETWORK_TYPE, networkType, subId);
Jake Hamby7c27be32014-03-03 13:25:59 -08002364 if (DBG) log("setPreferredNetworkType: " + (success ? "ok" : "fail"));
Junda Liu80bc0d12014-07-14 16:36:44 -07002365 if (success) {
2366 Settings.Global.putInt(mPhone.getContext().getContentResolver(),
Stuart Scott54788802015-03-30 13:18:01 -07002367 Settings.Global.PREFERRED_NETWORK_MODE + subId, networkType);
Junda Liu80bc0d12014-07-14 16:36:44 -07002368 }
Jake Hamby7c27be32014-03-03 13:25:59 -08002369 return success;
2370 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002371
2372 /**
Junda Liu475951f2014-11-07 16:45:03 -08002373 * Check TETHER_DUN_REQUIRED and TETHER_DUN_APN settings, net.tethering.noprovisioning
2374 * SystemProperty, and config_tether_apndata to decide whether DUN APN is required for
2375 * tethering.
2376 *
2377 * @return 0: Not required. 1: required. 2: Not set.
2378 * @hide
2379 */
2380 @Override
2381 public int getTetherApnRequired() {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002382 enforceModifyPermission();
Junda Liu475951f2014-11-07 16:45:03 -08002383 int dunRequired = Settings.Global.getInt(mPhone.getContext().getContentResolver(),
2384 Settings.Global.TETHER_DUN_REQUIRED, 2);
2385 // If not set, check net.tethering.noprovisioning, TETHER_DUN_APN setting and
2386 // config_tether_apndata.
2387 if (dunRequired == 2 && mPhone.hasMatchedTetherApnSetting()) {
2388 dunRequired = 1;
2389 }
2390 return dunRequired;
2391 }
2392
2393 /**
Robert Greenwalted86e582014-05-21 20:03:20 -07002394 * Set mobile data enabled
2395 * Used by the user through settings etc to turn on/off mobile data
2396 *
2397 * @param enable {@code true} turn turn data on, else {@code false}
2398 */
2399 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002400 public void setDataEnabled(int subId, boolean enable) {
Robert Greenwalted86e582014-05-21 20:03:20 -07002401 enforceModifyPermission();
Wink Savillee7353bb2014-12-05 14:21:41 -08002402 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002403 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002404 Phone phone = PhoneFactory.getPhone(phoneId);
2405 if (phone != null) {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002406 if (DBG) log("setDataEnabled: subId=" + subId + " enable=" + enable);
Wink Savillee7353bb2014-12-05 14:21:41 -08002407 phone.setDataEnabled(enable);
2408 } else {
2409 loge("setDataEnabled: no phone for subId=" + subId);
2410 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002411 }
2412
2413 /**
Robert Greenwalt646120a2014-05-23 11:54:03 -07002414 * Get whether mobile data is enabled.
2415 *
2416 * Note that this used to be available from ConnectivityService, gated by
2417 * ACCESS_NETWORK_STATE permission, so this will accept either that or
2418 * our MODIFY_PHONE_STATE.
Robert Greenwalted86e582014-05-21 20:03:20 -07002419 *
2420 * @return {@code true} if data is enabled else {@code false}
2421 */
2422 @Override
Wink Savillee7353bb2014-12-05 14:21:41 -08002423 public boolean getDataEnabled(int subId) {
Robert Greenwalt646120a2014-05-23 11:54:03 -07002424 try {
2425 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NETWORK_STATE,
2426 null);
2427 } catch (Exception e) {
2428 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.MODIFY_PHONE_STATE,
2429 null);
2430 }
Wink Savillee7353bb2014-12-05 14:21:41 -08002431 int phoneId = mSubscriptionController.getPhoneId(subId);
Joe Onoratoa601dd22016-02-23 13:03:53 -08002432 if (DBG) log("getDataEnabled: subId=" + subId + " phoneId=" + phoneId);
Wink Savillee7353bb2014-12-05 14:21:41 -08002433 Phone phone = PhoneFactory.getPhone(phoneId);
2434 if (phone != null) {
2435 boolean retVal = phone.getDataEnabled();
Joe Onoratoa601dd22016-02-23 13:03:53 -08002436 if (DBG) log("getDataEnabled: subId=" + subId + " retVal=" + retVal);
Wink Savillee7353bb2014-12-05 14:21:41 -08002437 return retVal;
2438 } else {
Joe Onoratoa601dd22016-02-23 13:03:53 -08002439 if (DBG) loge("getDataEnabled: no phone subId=" + subId + " retVal=false");
Wink Savillee7353bb2014-12-05 14:21:41 -08002440 return false;
2441 }
Robert Greenwalted86e582014-05-21 20:03:20 -07002442 }
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002443
2444 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002445 public int getCarrierPrivilegeStatus(int subId) {
2446 final Phone phone = getPhone(subId);
2447 if (phone == null) {
2448 loge("getCarrierPrivilegeStatus: Invalid subId");
2449 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
2450 }
2451 UiccCard card = UiccController.getInstance().getUiccCard(phone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002452 if (card == null) {
Shishir Agrawal5e5becd2014-11-18 11:38:23 -08002453 loge("getCarrierPrivilegeStatus: No UICC");
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002454 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2455 }
2456 return card.getCarrierPrivilegeStatusForCurrentTransaction(
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002457 phone.getContext().getPackageManager());
Shishir Agrawal60f9c952014-06-23 12:00:43 -07002458 }
Junda Liu29340342014-07-10 15:23:27 -07002459
2460 @Override
Zach Johnson50ecba32015-05-19 00:24:21 -07002461 public int checkCarrierPrivilegesForPackage(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002462 if (TextUtils.isEmpty(pkgName))
2463 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Shishir Agrawal21409252015-01-15 23:33:50 -08002464 UiccCard card = UiccController.getInstance().getUiccCard(mPhone.getPhoneId());
Shishir Agrawaleb8771e2014-07-22 11:24:08 -07002465 if (card == null) {
2466 loge("checkCarrierPrivilegesForPackage: No UICC");
2467 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2468 }
Zach Johnson50ecba32015-05-19 00:24:21 -07002469 return card.getCarrierPrivilegeStatus(mPhone.getContext().getPackageManager(), pkgName);
2470 }
2471
2472 @Override
2473 public int checkCarrierPrivilegesForPackageAnyPhone(String pkgName) {
Junda Liu317d70b2016-03-08 09:33:53 -08002474 if (TextUtils.isEmpty(pkgName))
2475 return TelephonyManager.CARRIER_PRIVILEGE_STATUS_NO_ACCESS;
Zach Johnson50ecba32015-05-19 00:24:21 -07002476 int result = TelephonyManager.CARRIER_PRIVILEGE_STATUS_RULES_NOT_LOADED;
2477 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2478 UiccCard card = UiccController.getInstance().getUiccCard(i);
2479 if (card == null) {
Jonathan Basseri7d320df2015-06-16 12:17:08 -07002480 // No UICC in that slot.
Zach Johnson50ecba32015-05-19 00:24:21 -07002481 continue;
2482 }
2483
2484 result = card.getCarrierPrivilegeStatus(
2485 mPhone.getContext().getPackageManager(), pkgName);
2486 if (result == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2487 break;
2488 }
2489 }
2490
2491 return result;
Junda Liu29340342014-07-10 15:23:27 -07002492 }
Derek Tan89e89d42014-07-08 17:00:10 -07002493
2494 @Override
Junda Liue64de782015-04-16 17:19:16 -07002495 public List<String> getCarrierPackageNamesForIntentAndPhone(Intent intent, int phoneId) {
2496 if (!SubscriptionManager.isValidPhoneId(phoneId)) {
2497 loge("phoneId " + phoneId + " is not valid.");
2498 return null;
2499 }
2500 UiccCard card = UiccController.getInstance().getUiccCard(phoneId);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002501 if (card == null) {
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002502 loge("getCarrierPackageNamesForIntent: No UICC");
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002503 return null ;
2504 }
Diego Pontorieroaf74c862014-08-28 11:51:16 -07002505 return card.getCarrierPackageNamesForIntent(
Svetoslav483aff72015-04-21 14:16:07 -07002506 mPhone.getContext().getPackageManager(), intent);
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002507 }
2508
Amith Yamasani6e118872016-02-19 12:53:51 -08002509 @Override
2510 public List<String> getPackagesWithCarrierPrivileges() {
2511 PackageManager pm = mPhone.getContext().getPackageManager();
2512 List<String> privilegedPackages = new ArrayList<>();
2513 List<PackageInfo> packages = null;
2514 for (int i = 0; i < TelephonyManager.getDefault().getPhoneCount(); i++) {
2515 UiccCard card = UiccController.getInstance().getUiccCard(i);
2516 if (card == null) {
2517 // No UICC in that slot.
2518 continue;
2519 }
2520 if (card.hasCarrierPrivilegeRules()) {
2521 if (packages == null) {
2522 // Only check packages in user 0 for now
2523 packages = pm.getInstalledPackagesAsUser(
2524 PackageManager.MATCH_DISABLED_COMPONENTS
2525 | PackageManager.MATCH_DISABLED_UNTIL_USED_COMPONENTS
2526 | PackageManager.GET_SIGNATURES, UserHandle.USER_SYSTEM);
2527 }
2528 for (int p = packages.size() - 1; p >= 0; p--) {
2529 PackageInfo pkgInfo = packages.get(p);
2530 if (pkgInfo != null && pkgInfo.packageName != null
2531 && card.getCarrierPrivilegeStatus(pkgInfo)
2532 == TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS) {
2533 privilegedPackages.add(pkgInfo.packageName);
2534 }
2535 }
2536 }
2537 }
2538 return privilegedPackages;
2539 }
2540
Wink Savilleb564aae2014-10-23 10:18:09 -07002541 private String getIccId(int subId) {
Sanket Padawe356d7632015-06-22 14:03:32 -07002542 final Phone phone = getPhone(subId);
2543 UiccCard card = phone == null ? null : phone.getUiccCard();
Derek Tan97ebb422014-09-05 16:55:38 -07002544 if (card == null) {
2545 loge("getIccId: No UICC");
2546 return null;
2547 }
2548 String iccId = card.getIccId();
2549 if (TextUtils.isEmpty(iccId)) {
2550 loge("getIccId: ICC ID is null or empty.");
2551 return null;
2552 }
2553 return iccId;
2554 }
2555
Shishir Agrawaleb6439a2014-07-21 13:19:38 -07002556 @Override
Jeff Sharkey85190e62014-12-05 09:40:12 -08002557 public boolean setLine1NumberForDisplayForSubscriber(int subId, String alphaTag,
2558 String number) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002559 enforceCarrierPrivilege(subId);
Derek Tan97ebb422014-09-05 16:55:38 -07002560
Jeff Sharkey85190e62014-12-05 09:40:12 -08002561 final String iccId = getIccId(subId);
Sanket Padawe356d7632015-06-22 14:03:32 -07002562 final Phone phone = getPhone(subId);
2563 if (phone == null) {
2564 return false;
2565 }
2566 final String subscriberId = phone.getSubscriberId();
Jeff Sharkey85190e62014-12-05 09:40:12 -08002567
2568 if (DBG_MERGE) {
2569 Slog.d(LOG_TAG, "Setting line number for ICC=" + iccId + ", subscriberId="
2570 + subscriberId + " to " + number);
2571 }
2572
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002573 if (TextUtils.isEmpty(iccId)) {
2574 return false;
Derek Tan97ebb422014-09-05 16:55:38 -07002575 }
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002576
Jeff Sharkey85190e62014-12-05 09:40:12 -08002577 final SharedPreferences.Editor editor = mTelephonySharedPreferences.edit();
2578
2579 final String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002580 if (alphaTag == null) {
2581 editor.remove(alphaTagPrefKey);
2582 } else {
2583 editor.putString(alphaTagPrefKey, alphaTag);
2584 }
2585
Jeff Sharkey85190e62014-12-05 09:40:12 -08002586 // Record both the line number and IMSI for this ICCID, since we need to
2587 // track all merged IMSIs based on line number
2588 final String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2589 final String subscriberPrefKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002590 if (number == null) {
2591 editor.remove(numberPrefKey);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002592 editor.remove(subscriberPrefKey);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002593 } else {
2594 editor.putString(numberPrefKey, number);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002595 editor.putString(subscriberPrefKey, subscriberId);
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002596 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002597
Shishir Agrawal495d7e12014-12-01 11:50:28 -08002598 editor.commit();
2599 return true;
Derek Tan7226c842014-07-02 17:42:23 -07002600 }
2601
2602 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002603 public String getLine1NumberForDisplay(int subId, String callingPackage) {
Makoto Onukifee69342015-06-29 14:44:50 -07002604 // This is open to apps with WRITE_SMS.
2605 if (!canReadPhoneNumber(callingPackage, "getLine1NumberForDisplay")) {
Amit Mahajan9cf11512015-11-09 11:40:48 -08002606 if (DBG_MERGE) log("getLine1NumberForDisplay returning null due to permission");
Svet Ganovb320e182015-04-16 12:30:10 -07002607 return null;
2608 }
Derek Tan97ebb422014-09-05 16:55:38 -07002609
2610 String iccId = getIccId(subId);
2611 if (iccId != null) {
2612 String numberPrefKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
Amit Mahajan9cf11512015-11-09 11:40:48 -08002613 if (DBG_MERGE) {
2614 log("getLine1NumberForDisplay returning " +
2615 mTelephonySharedPreferences.getString(numberPrefKey, null));
2616 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002617 return mTelephonySharedPreferences.getString(numberPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002618 }
Amit Mahajan9cf11512015-11-09 11:40:48 -08002619 if (DBG_MERGE) log("getLine1NumberForDisplay returning null as iccId is null");
Derek Tan97ebb422014-09-05 16:55:38 -07002620 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002621 }
2622
2623 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002624 public String getLine1AlphaTagForDisplay(int subId, String callingPackage) {
2625 if (!canReadPhoneState(callingPackage, "getLine1AlphaTagForDisplay")) {
2626 return null;
2627 }
Derek Tan97ebb422014-09-05 16:55:38 -07002628
2629 String iccId = getIccId(subId);
2630 if (iccId != null) {
2631 String alphaTagPrefKey = PREF_CARRIERS_ALPHATAG_PREFIX + iccId;
Andrew Leedf14ead2014-10-17 14:22:52 -07002632 return mTelephonySharedPreferences.getString(alphaTagPrefKey, null);
Derek Tan7226c842014-07-02 17:42:23 -07002633 }
Derek Tan97ebb422014-09-05 16:55:38 -07002634 return null;
Derek Tan7226c842014-07-02 17:42:23 -07002635 }
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002636
2637 @Override
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002638 public String[] getMergedSubscriberIds(String callingPackage) {
2639 if (!canReadPhoneState(callingPackage, "getMergedSubscriberIds")) {
2640 return null;
2641 }
Jeff Sharkey85190e62014-12-05 09:40:12 -08002642 final Context context = mPhone.getContext();
2643 final TelephonyManager tele = TelephonyManager.from(context);
2644 final SubscriptionManager sub = SubscriptionManager.from(context);
2645
2646 // Figure out what subscribers are currently active
2647 final ArraySet<String> activeSubscriberIds = new ArraySet<>();
Fyodor Kupolov8e53b0b2015-06-17 13:17:50 -07002648 // Clear calling identity, when calling TelephonyManager, because callerUid must be
2649 // the process, where TelephonyManager was instantiated. Otherwise AppOps check will fail.
2650 final long identity = Binder.clearCallingIdentity();
2651 try {
2652 final int[] subIds = sub.getActiveSubscriptionIdList();
2653 for (int subId : subIds) {
2654 activeSubscriberIds.add(tele.getSubscriberId(subId));
2655 }
2656 } finally {
2657 Binder.restoreCallingIdentity(identity);
Jeff Sharkey85190e62014-12-05 09:40:12 -08002658 }
2659
2660 // First pass, find a number override for an active subscriber
2661 String mergeNumber = null;
2662 final Map<String, ?> prefs = mTelephonySharedPreferences.getAll();
2663 for (String key : prefs.keySet()) {
2664 if (key.startsWith(PREF_CARRIERS_SUBSCRIBER_PREFIX)) {
2665 final String subscriberId = (String) prefs.get(key);
2666 if (activeSubscriberIds.contains(subscriberId)) {
2667 final String iccId = key.substring(PREF_CARRIERS_SUBSCRIBER_PREFIX.length());
2668 final String numberKey = PREF_CARRIERS_NUMBER_PREFIX + iccId;
2669 mergeNumber = (String) prefs.get(numberKey);
2670 if (DBG_MERGE) {
2671 Slog.d(LOG_TAG, "Found line number " + mergeNumber
2672 + " for active subscriber " + subscriberId);
2673 }
2674 if (!TextUtils.isEmpty(mergeNumber)) {
2675 break;
2676 }
2677 }
2678 }
2679 }
2680
2681 // Shortcut when no active merged subscribers
2682 if (TextUtils.isEmpty(mergeNumber)) {
2683 return null;
2684 }
2685
2686 // Second pass, find all subscribers under that line override
2687 final ArraySet<String> result = new ArraySet<>();
2688 for (String key : prefs.keySet()) {
2689 if (key.startsWith(PREF_CARRIERS_NUMBER_PREFIX)) {
2690 final String number = (String) prefs.get(key);
2691 if (mergeNumber.equals(number)) {
2692 final String iccId = key.substring(PREF_CARRIERS_NUMBER_PREFIX.length());
2693 final String subscriberKey = PREF_CARRIERS_SUBSCRIBER_PREFIX + iccId;
2694 final String subscriberId = (String) prefs.get(subscriberKey);
2695 if (!TextUtils.isEmpty(subscriberId)) {
2696 result.add(subscriberId);
2697 }
2698 }
2699 }
2700 }
2701
2702 final String[] resultArray = result.toArray(new String[result.size()]);
2703 Arrays.sort(resultArray);
2704 if (DBG_MERGE) {
2705 Slog.d(LOG_TAG, "Found subscribers " + Arrays.toString(resultArray) + " after merge");
2706 }
2707 return resultArray;
2708 }
2709
2710 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002711 public boolean setOperatorBrandOverride(int subId, String brand) {
2712 enforceCarrierPrivilege(subId);
2713 final Phone phone = getPhone(subId);
2714 return phone == null ? false : phone.setOperatorBrandOverride(brand);
Shishir Agrawalb1ebf8c2014-07-17 16:32:41 -07002715 }
Steven Liu4bf01bc2014-07-17 11:05:29 -05002716
2717 @Override
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002718 public boolean setRoamingOverride(int subId, List<String> gsmRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002719 List<String> gsmNonRoamingList, List<String> cdmaRoamingList,
2720 List<String> cdmaNonRoamingList) {
Shishir Agrawalc04d9752016-02-19 10:41:00 -08002721 enforceCarrierPrivilege(subId);
2722 final Phone phone = getPhone(subId);
2723 if (phone == null) {
2724 return false;
2725 }
2726 return phone.setRoamingOverride(gsmRoamingList, gsmNonRoamingList, cdmaRoamingList,
Shishir Agrawal621a47c2014-12-01 10:25:09 -08002727 cdmaNonRoamingList);
2728 }
2729
2730 @Override
Steven Liu4bf01bc2014-07-17 11:05:29 -05002731 public int invokeOemRilRequestRaw(byte[] oemReq, byte[] oemResp) {
2732 enforceModifyPermission();
2733
2734 int returnValue = 0;
2735 try {
2736 AsyncResult result = (AsyncResult)sendRequest(CMD_INVOKE_OEM_RIL_REQUEST_RAW, oemReq);
2737 if(result.exception == null) {
2738 if (result.result != null) {
2739 byte[] responseData = (byte[])(result.result);
2740 if(responseData.length > oemResp.length) {
2741 Log.w(LOG_TAG, "Buffer to copy response too small: Response length is " +
2742 responseData.length + "bytes. Buffer Size is " +
2743 oemResp.length + "bytes.");
2744 }
2745 System.arraycopy(responseData, 0, oemResp, 0, responseData.length);
2746 returnValue = responseData.length;
2747 }
2748 } else {
2749 CommandException ex = (CommandException) result.exception;
2750 returnValue = ex.getCommandError().ordinal();
2751 if(returnValue > 0) returnValue *= -1;
2752 }
2753 } catch (RuntimeException e) {
2754 Log.w(LOG_TAG, "sendOemRilRequestRaw: Runtime Exception");
2755 returnValue = (CommandException.Error.GENERIC_FAILURE.ordinal());
2756 if(returnValue > 0) returnValue *= -1;
2757 }
2758
2759 return returnValue;
2760 }
Wink Saville5d475dd2014-10-17 15:00:58 -07002761
2762 @Override
2763 public void setRadioCapability(RadioAccessFamily[] rafs) {
2764 try {
2765 ProxyController.getInstance().setRadioCapability(rafs);
2766 } catch (RuntimeException e) {
2767 Log.w(LOG_TAG, "setRadioCapability: Runtime Exception");
2768 }
2769 }
2770
2771 @Override
Robert Greenwalt36b23af2015-07-06 17:59:14 -07002772 public int getRadioAccessFamily(int phoneId, String callingPackage) {
2773 if (!canReadPhoneState(callingPackage, "getRadioAccessFamily")) {
2774 return RadioAccessFamily.RAF_UNKNOWN;
2775 }
2776
Wink Saville5d475dd2014-10-17 15:00:58 -07002777 return ProxyController.getInstance().getRadioAccessFamily(phoneId);
2778 }
Andrew Leedf14ead2014-10-17 14:22:52 -07002779
2780 @Override
2781 public void enableVideoCalling(boolean enable) {
2782 enforceModifyPermission();
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002783 ImsManager.setVtSetting(mPhone.getContext(), enable);
Andrew Leedf14ead2014-10-17 14:22:52 -07002784 }
2785
2786 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002787 public boolean isVideoCallingEnabled(String callingPackage) {
2788 if (!canReadPhoneState(callingPackage, "isVideoCallingEnabled")) {
2789 return false;
2790 }
2791
Andrew Lee77527ac2014-10-21 16:57:39 -07002792 // Check the user preference and the system-level IMS setting. Even if the user has
2793 // enabled video calling, if IMS is disabled we aren't able to support video calling.
2794 // In the long run, we may instead need to check if there exists a connection service
2795 // which can support video calling.
Andrew Lee312e8172014-10-23 17:01:36 -07002796 return ImsManager.isVtEnabledByPlatform(mPhone.getContext())
2797 && ImsManager.isEnhanced4gLteModeSettingEnabledByUser(mPhone.getContext())
Tyler Gunnfdd69de2015-12-04 21:24:38 -08002798 && ImsManager.isVtEnabledByUser(mPhone.getContext());
Andrew Leedf14ead2014-10-17 14:22:52 -07002799 }
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002800
Andrew Leea1239f22015-03-02 17:44:07 -08002801 @Override
2802 public boolean canChangeDtmfToneLength() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002803 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_DTMF_TYPE_ENABLED_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002804 }
2805
2806 @Override
2807 public boolean isWorldPhone() {
Jonathan Basseri9504c6b2015-06-04 14:23:32 -07002808 return mApp.getCarrierConfig().getBoolean(CarrierConfigManager.KEY_WORLD_PHONE_BOOL);
Andrew Leea1239f22015-03-02 17:44:07 -08002809 }
2810
Andrew Lee9431b832015-03-09 18:46:45 -07002811 @Override
2812 public boolean isTtyModeSupported() {
2813 TelecomManager telecomManager = TelecomManager.from(mPhone.getContext());
2814 TelephonyManager telephonyManager =
2815 (TelephonyManager) mPhone.getContext().getSystemService(Context.TELEPHONY_SERVICE);
2816 return !telephonyManager.isMultiSimEnabled() && telecomManager.isTtySupported();
2817 }
2818
2819 @Override
2820 public boolean isHearingAidCompatibilitySupported() {
2821 return mPhone.getContext().getResources().getBoolean(R.bool.hac_enabled);
2822 }
2823
Sanket Padawe7310cc72015-01-14 09:53:20 -08002824 /**
2825 * Returns the unique device ID of phone, for example, the IMEI for
2826 * GSM and the MEID for CDMA phones. Return null if device ID is not available.
2827 *
2828 * <p>Requires Permission:
2829 * {@link android.Manifest.permission#READ_PHONE_STATE READ_PHONE_STATE}
2830 */
2831 @Override
Svet Ganovb320e182015-04-16 12:30:10 -07002832 public String getDeviceId(String callingPackage) {
2833 if (!canReadPhoneState(callingPackage, "getDeviceId")) {
2834 return null;
2835 }
2836
Sanket Padawe7310cc72015-01-14 09:53:20 -08002837 final Phone phone = PhoneFactory.getPhone(0);
2838 if (phone != null) {
2839 return phone.getDeviceId();
2840 } else {
2841 return null;
2842 }
2843 }
2844
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002845 /*
2846 * {@hide}
2847 * Returns the IMS Registration Status
2848 */
Santos Cordon7a1885b2015-02-03 11:15:19 -08002849 @Override
Libin.Tang@motorola.comafe82642014-12-18 13:27:53 -06002850 public boolean isImsRegistered() {
2851 return mPhone.isImsRegistered();
2852 }
Santos Cordon7a1885b2015-02-03 11:15:19 -08002853
2854 @Override
2855 public int getSubIdForPhoneAccount(PhoneAccount phoneAccount) {
2856 return PhoneUtils.getSubIdForPhoneAccount(phoneAccount);
2857 }
Nathan Harolddcfc7932015-03-18 10:01:20 -07002858
Nathan Haroldc55097a2015-03-11 18:14:50 -07002859 /*
2860 * {@hide}
2861 * Returns the IMS Registration Status
2862 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002863 public boolean isWifiCallingAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002864 return mPhone.isWifiCallingEnabled();
2865 }
2866
2867 /*
2868 * {@hide}
2869 * Returns the IMS Registration Status
2870 */
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002871 public boolean isVolteAvailable() {
Nathan Haroldc55097a2015-03-11 18:14:50 -07002872 return mPhone.isVolteEnabled();
2873 }
Svet Ganovb320e182015-04-16 12:30:10 -07002874
Etan Cohen3b7a1bc2015-05-28 15:57:13 -07002875 /*
2876 * {@hide} Returns the IMS Registration Status
2877 */
2878 public boolean isVideoTelephonyAvailable() {
2879 return mPhone.isVideoEnabled();
2880 }
2881
Svet Ganovb320e182015-04-16 12:30:10 -07002882 private boolean canReadPhoneState(String callingPackage, String message) {
Etan Cohen921655c2015-06-24 13:54:50 -07002883 try {
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002884 mApp.enforceCallingOrSelfPermission(
Etan Cohen921655c2015-06-24 13:54:50 -07002885 android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, message);
2886
Amit Mahajan83ea23b2015-07-30 16:05:11 -07002887 // SKIP checking for run-time permission since caller or self has PRIVILEDGED permission
Etan Cohen921655c2015-06-24 13:54:50 -07002888 return true;
Amit Mahajan785783f2015-06-29 10:36:50 -07002889 } catch (SecurityException e) {
2890 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PHONE_STATE,
2891 message);
Etan Cohen921655c2015-06-24 13:54:50 -07002892 }
Svet Ganovb320e182015-04-16 12:30:10 -07002893
2894 if (mAppOps.noteOp(AppOpsManager.OP_READ_PHONE_STATE, Binder.getCallingUid(),
2895 callingPackage) != AppOpsManager.MODE_ALLOWED) {
2896 return false;
2897 }
2898
2899 return true;
2900 }
Stuart Scott8eef64f2015-04-08 15:13:54 -07002901
Makoto Onukifee69342015-06-29 14:44:50 -07002902 /**
Amit Mahajanb9b49782015-09-15 18:16:32 -07002903 * Besides READ_PHONE_STATE, WRITE_SMS and READ_SMS also allow apps to get phone numbers.
Makoto Onukifee69342015-06-29 14:44:50 -07002904 */
2905 private boolean canReadPhoneNumber(String callingPackage, String message) {
Makoto Onukie4072d12015-08-03 15:12:23 -07002906 // Default SMS app can always read it.
2907 if (mAppOps.noteOp(AppOpsManager.OP_WRITE_SMS,
2908 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED) {
2909 return true;
2910 }
2911 try {
2912 return canReadPhoneState(callingPackage, message);
Amit Mahajanb9b49782015-09-15 18:16:32 -07002913 } catch (SecurityException readPhoneStateSecurityException) {
2914 try {
2915 // Can be read with READ_SMS too.
2916 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_SMS, message);
2917 return mAppOps.noteOp(AppOpsManager.OP_READ_SMS,
2918 Binder.getCallingUid(), callingPackage) == AppOpsManager.MODE_ALLOWED;
2919 } catch (SecurityException readSmsSecurityException) {
2920 // Throw exception with message including both READ_PHONE_STATE and READ_SMS
2921 // permissions
2922 throw new SecurityException(message + ": Neither user " + Binder.getCallingUid() +
2923 " nor current process has " + android.Manifest.permission.READ_PHONE_STATE +
2924 " or " + android.Manifest.permission.READ_SMS + ".");
2925 }
Makoto Onukie4072d12015-08-03 15:12:23 -07002926 }
Makoto Onukifee69342015-06-29 14:44:50 -07002927 }
2928
Stuart Scott8eef64f2015-04-08 15:13:54 -07002929 @Override
2930 public void factoryReset(int subId) {
2931 enforceConnectivityInternalPermission();
Stuart Scott981d8582015-04-21 14:09:50 -07002932 if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) {
2933 return;
2934 }
2935
Svet Ganovcc087f82015-05-12 20:35:54 -07002936 final long identity = Binder.clearCallingIdentity();
2937 try {
Stuart Scott981d8582015-04-21 14:09:50 -07002938 if (SubscriptionManager.isUsableSubIdValue(subId) && !mUserManager.hasUserRestriction(
2939 UserManager.DISALLOW_CONFIG_MOBILE_NETWORKS)) {
Svet Ganovcc087f82015-05-12 20:35:54 -07002940 // Enable data
2941 setDataEnabled(subId, true);
2942 // Set network selection mode to automatic
2943 setNetworkSelectionModeAutomatic(subId);
2944 // Set preferred mobile network type to the best available
2945 setPreferredNetworkType(subId, Phone.PREFERRED_NT_MODE);
2946 // Turn off roaming
2947 SubscriptionManager.from(mApp).setDataRoaming(0, subId);
2948 }
2949 } finally {
2950 Binder.restoreCallingIdentity(identity);
Stuart Scott8eef64f2015-04-08 15:13:54 -07002951 }
2952 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002953
2954 @Override
2955 public String getLocaleFromDefaultSim() {
2956 // We query all subscriptions instead of just the active ones, because
2957 // this might be called early on in the provisioning flow when the
2958 // subscriptions potentially aren't active yet.
2959 final List<SubscriptionInfo> slist = getAllSubscriptionInfoList();
2960 if (slist == null || slist.isEmpty()) {
2961 return null;
2962 }
2963
2964 // This function may be called very early, say, from the setup wizard, at
2965 // which point we won't have a default subscription set. If that's the case
2966 // we just choose the first, which will be valid in "most cases".
2967 final int defaultSubId = getDefaultSubscription();
2968 SubscriptionInfo info = null;
2969 if (defaultSubId == SubscriptionManager.INVALID_SUBSCRIPTION_ID) {
2970 info = slist.get(0);
2971 } else {
2972 for (SubscriptionInfo item : slist) {
2973 if (item.getSubscriptionId() == defaultSubId) {
2974 info = item;
2975 break;
2976 }
2977 }
2978
2979 if (info == null) {
2980 return null;
2981 }
2982 }
2983
2984 // Try and fetch the locale from the carrier properties or from the SIM language
2985 // preferences (EF-PL and EF-LI)...
Tony Hill183b2de2015-06-24 14:53:58 +01002986 final int mcc = info.getMcc();
Narayan Kamath1c496c22015-04-16 14:40:19 +01002987 final Phone defaultPhone = getPhone(info.getSubscriptionId());
Narayan Kamath011676f2015-07-29 12:04:08 +01002988 String simLanguage = null;
Narayan Kamath1c496c22015-04-16 14:40:19 +01002989 if (defaultPhone != null) {
2990 final Locale localeFromDefaultSim = defaultPhone.getLocaleFromSimAndCarrierPrefs();
2991 if (localeFromDefaultSim != null) {
Narayan Kamath011676f2015-07-29 12:04:08 +01002992 if (!localeFromDefaultSim.getCountry().isEmpty()) {
Tony Hill183b2de2015-06-24 14:53:58 +01002993 if (DBG) log("Using locale from default SIM:" + localeFromDefaultSim);
2994 return localeFromDefaultSim.toLanguageTag();
Narayan Kamath011676f2015-07-29 12:04:08 +01002995 } else {
2996 simLanguage = localeFromDefaultSim.getLanguage();
Tony Hill183b2de2015-06-24 14:53:58 +01002997 }
Narayan Kamath1c496c22015-04-16 14:40:19 +01002998 }
2999 }
3000
Narayan Kamath011676f2015-07-29 12:04:08 +01003001 // The SIM language preferences only store a language (e.g. fr = French), not an
3002 // exact locale (e.g. fr_FR = French/France). So, if the locale returned from
3003 // the SIM and carrier preferences does not include a country we add the country
3004 // determined from the SIM MCC to provide an exact locale.
3005 final Locale mccLocale = MccTable.getLocaleFromMcc(mPhone.getContext(), mcc, simLanguage);
Tony Hill183b2de2015-06-24 14:53:58 +01003006 if (mccLocale != null) {
3007 if (DBG) log("No locale from default SIM, using mcc locale:" + mccLocale);
3008 return mccLocale.toLanguageTag();
Narayan Kamath1c496c22015-04-16 14:40:19 +01003009 }
3010
Tony Hill183b2de2015-06-24 14:53:58 +01003011 if (DBG) log("No locale found - returning null");
Narayan Kamath1c496c22015-04-16 14:40:19 +01003012 return null;
3013 }
3014
3015 private List<SubscriptionInfo> getAllSubscriptionInfoList() {
3016 final long identity = Binder.clearCallingIdentity();
3017 try {
3018 return mSubscriptionController.getAllSubInfoList(
3019 mPhone.getContext().getOpPackageName());
3020 } finally {
3021 Binder.restoreCallingIdentity(identity);
3022 }
3023 }
3024
3025 private List<SubscriptionInfo> getActiveSubscriptionInfoList() {
3026 final long identity = Binder.clearCallingIdentity();
3027 try {
3028 return mSubscriptionController.getActiveSubscriptionInfoList(
3029 mPhone.getContext().getOpPackageName());
3030 } finally {
3031 Binder.restoreCallingIdentity(identity);
3032 }
3033 }
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003034
3035 /**
Adam Lesinski903a54c2016-04-11 14:49:52 -07003036 * Responds to the ResultReceiver with the {@link android.telephony.ModemActivityInfo} object
3037 * representing the state of the modem.
3038 *
3039 * NOTE: This clears the modem state, so there should only every be one caller.
3040 * @hide
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003041 */
3042 @Override
Adam Lesinski903a54c2016-04-11 14:49:52 -07003043 public void requestModemActivityInfo(ResultReceiver result) {
3044 enforceModifyPermission();
3045
3046 ModemActivityInfo info = (ModemActivityInfo) sendRequest(CMD_GET_MODEM_ACTIVITY_INFO, null);
3047 Bundle bundle = new Bundle();
3048 bundle.putParcelable(TelephonyManager.MODEM_ACTIVITY_RESULT_KEY, info);
3049 result.send(0, bundle);
Prerepa Viswanadham7fcff692015-06-03 11:20:55 -07003050 }
Jack Yu85bd38a2015-11-09 11:34:32 -08003051
3052 /**
3053 * {@hide}
3054 * Returns the service state information on specified subscription.
3055 */
3056 @Override
3057 public ServiceState getServiceStateForSubscriber(int subId, String callingPackage) {
3058
3059 if (!canReadPhoneState(callingPackage, "getServiceStateForSubscriber")) {
3060 return null;
3061 }
3062
3063 final Phone phone = getPhone(subId);
3064 if (phone == null) {
3065 return null;
3066 }
3067
3068 return phone.getServiceState();
3069 }
Nancy Chen31f9ba12016-01-06 11:42:12 -08003070
3071 /**
3072 * Returns the URI for the per-account voicemail ringtone set in Phone settings.
3073 *
3074 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3075 * voicemail ringtone.
3076 * @return The URI for the ringtone to play when receiving a voicemail from a specific
3077 * PhoneAccount.
3078 */
3079 @Override
3080 public Uri getVoicemailRingtoneUri(PhoneAccountHandle accountHandle) {
3081 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3082 if (phone == null) {
3083 return null;
3084 }
3085
3086 return VoicemailNotificationSettingsUtil.getRingtoneUri(phone);
3087 }
3088
3089 /**
3090 * Returns whether vibration is set for voicemail notification in Phone settings.
3091 *
3092 * @param accountHandle The handle for the {@link PhoneAccount} for which to retrieve the
3093 * voicemail vibration setting.
3094 * @return {@code true} if the vibration is set for this PhoneAccount, {@code false} otherwise.
3095 */
3096 @Override
3097 public boolean isVoicemailVibrationEnabled(PhoneAccountHandle accountHandle) {
3098 final Phone phone = PhoneUtils.getPhoneForPhoneAccountHandle(accountHandle);
3099 if (phone == null) {
3100 return false;
3101 }
3102
3103 return VoicemailNotificationSettingsUtil.isVibrationEnabled(phone);
3104 }
3105
Youhan Wange64578a2016-05-02 15:32:42 -07003106 /**
3107 * Make sure either called from same process as self (phone) or IPC caller has read privilege.
3108 *
3109 * @throws SecurityException if the caller does not have the required permission
3110 */
3111 private void enforceReadPrivilegedPermission() {
3112 mApp.enforceCallingOrSelfPermission(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE,
3113 null);
3114 }
3115
3116 /**
3117 * Return the application ID for the app type.
3118 *
3119 * @param subId the subscription ID that this request applies to.
3120 * @param appType the uicc app type.
3121 * @return Application ID for specificied app type, or null if no uicc.
3122 */
3123 @Override
3124 public String getAidForAppType(int subId, int appType) {
3125 enforceReadPrivilegedPermission();
3126 Phone phone = getPhone(subId);
3127 if (phone == null) {
3128 return null;
3129 }
3130 String aid = null;
3131 try {
3132 aid = UiccController.getInstance().getUiccCard(phone.getPhoneId())
3133 .getApplicationByType(appType).getAid();
3134 } catch (Exception e) {
3135 Log.e(LOG_TAG, "Not getting aid. Exception ex=" + e);
3136 }
3137 return aid;
3138 }
3139
Youhan Wang4001d252016-05-11 10:29:41 -07003140
3141 /**
3142 * Return the Electronic Serial Number.
3143 *
3144 * @param subId the subscription ID that this request applies to.
3145 * @return ESN or null if error.
3146 */
3147 @Override
3148 public String getEsn(int subId) {
3149 enforceReadPrivilegedPermission();
3150 Phone phone = getPhone(subId);
3151 if (phone == null) {
3152 return null;
3153 }
3154 String esn = null;
3155 try {
3156 esn = phone.getEsn();
3157 } catch (Exception e) {
3158 Log.e(LOG_TAG, "Not getting ESN. Exception ex=" + e);
3159 }
3160 return esn;
3161 }
3162
Sanket Padawe99ef1e32016-05-18 16:12:33 -07003163 /**
3164 * Get snapshot of Telephony histograms
3165 * @return List of Telephony histograms
3166 * @hide
3167 */
3168 @Override
3169 public List<TelephonyHistogram> getTelephonyHistograms() {
3170 enforceModifyPermissionOrCarrierPrivilege(getDefaultSubscription());
3171 return RIL.getTelephonyRILTimingHistograms();
3172 }
Santos Cordon7d4ddf62013-07-10 11:58:08 -07003173}