blob: bd96d1267f996596ca364427652a343dafdafbae [file] [log] [blame]
Santos Cordon176ae282014-07-14 02:02:14 -07001/*
2 * Copyright (C) 2014 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
Tyler Gunn7cc70b42014-09-12 22:17:27 -070017package com.android.server.telecom;
Santos Cordon176ae282014-07-14 02:02:14 -070018
Tyler Gunncb59b672014-08-20 09:02:11 -070019import android.Manifest;
Evan Charlton105d9772014-11-25 14:08:53 -080020import android.content.ComponentName;
21import android.content.Context;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070022import android.content.Intent;
23import android.content.pm.PackageManager;
24import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070025import android.content.pm.ServiceInfo;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000026import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070027import android.graphics.Bitmap;
28import android.graphics.BitmapFactory;
Santos Cordon9c30c282015-05-13 16:28:27 -070029import android.graphics.drawable.Icon;
Evan Charlton105d9772014-11-25 14:08:53 -080030import android.net.Uri;
Tyler Gunn369c8742015-10-07 15:39:28 -070031import android.os.Bundle;
Hall Liu3f7f64d2016-01-07 17:40:13 -080032import android.os.AsyncTask;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070033import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080034import android.os.Process;
35import android.os.UserHandle;
36import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070037import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070038import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070039import android.telecom.DefaultDialerManager;
Brad Ebinger953e1af2016-10-05 15:45:22 -070040import android.telecom.Log;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070041import android.telecom.PhoneAccount;
42import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070043import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070044import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070045import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080046import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070047import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070048import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070049import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070050import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070051
Tyler Gunn91d43cf2014-09-17 12:19:39 -070052// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070053import com.android.internal.annotations.VisibleForTesting;
54import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070055import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070056import com.android.internal.util.XmlUtils;
57
Evan Charltonaf51ceb2014-07-30 11:56:36 -070058import org.xmlpull.v1.XmlPullParser;
59import org.xmlpull.v1.XmlPullParserException;
60import org.xmlpull.v1.XmlSerializer;
61
Ihab Awadb78b2762014-07-25 15:16:23 -070062import java.io.BufferedInputStream;
63import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070064import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070065import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070066import java.io.File;
67import java.io.FileNotFoundException;
68import java.io.FileOutputStream;
69import java.io.IOException;
70import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070071import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070072import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070073import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070074import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080075import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070076import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070077import java.util.List;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000078import java.util.Map;
Santos Cordon176ae282014-07-14 02:02:14 -070079import java.util.Objects;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000080import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070081import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070082
83/**
Evan Charlton89176372014-07-19 18:23:09 -070084 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070085 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
86 * implemented in {@link TelecomServiceImpl}, with the notable exception that
87 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
88 * proper authority over the {@code ComponentName}s they are declaring in their
89 * {@code PhoneAccountHandle}s.
90 *
91 *
92 * -- About Users and Phone Accounts --
93 *
Santos Cordon9c30c282015-05-13 16:28:27 -070094 * We store all phone accounts for all users in a single place, which means that there are three
95 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070096 * 1) The Android User that is currently active on the device.
97 * 2) The user which owns/registers the phone account.
98 * 3) The user running the app that is requesting the phone account information.
99 *
100 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
Tony Mak240656f2015-12-04 11:36:22 +0000101 * has a work profile running as another user (B2). Each user/profile only have the visibility of
102 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
103 * and the list only contains phone accounts owned by user B and accounts with
104 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700105 *
106 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
107 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
108 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700109 */
Brad Ebinger53855132015-10-30 10:58:19 -0700110public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700111
Yorke Lee5e8836a2014-08-22 15:25:18 -0700112 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
113 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
114
Ihab Awadb78b2762014-07-25 15:16:23 -0700115 public abstract static class Listener {
116 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
117 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
118 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
119 }
120
121 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700122 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000123 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700124
125 /** Keep in sync with the same in SipSettings.java */
126 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700127
128 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
129 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700130 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800131 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800132 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700133 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800134 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800135 private interface PhoneAccountRegistrarWriteLock {}
136 private final PhoneAccountRegistrarWriteLock mWriteLock =
137 new PhoneAccountRegistrarWriteLock() {};
Santos Cordon176ae282014-07-14 02:02:14 -0700138
Nancy Chen06ce0622014-10-23 01:17:35 +0000139 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700140 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700141 this(context, FILE_NAME);
142 }
143
144 @VisibleForTesting
145 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700146 // TODO: This file path is subject to change -- it is storing the phone account registry
147 // state file in the path /data/system/users/0/, which is likely not correct in a
148 // multi-user setting.
149 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
150 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
151 getAbsolutePath();
152 mAtomicFile = new AtomicFile(new File(filePath, fileName));
153 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700154 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700155
Ihab Awadb78b2762014-07-25 15:16:23 -0700156 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700157 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800158 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800159 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800160 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700161 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700162 }
163
Tyler Gunn84253572014-09-02 14:50:05 -0700164 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700165 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
166 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
167 * subscription id.
168 * @param accountHandle The handle for the phone account for which to retrieve the
169 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700170 * @return The value of the subscription id or -1 if it does not exist or is not valid.
Nancy Chen140004a2014-10-15 15:48:38 -0700171 */
Wink Saville35850602014-10-23 15:57:21 -0700172 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000173 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800174
Santos Cordon6a212642015-05-08 16:35:23 -0700175 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800176 TelephonyManager tm =
177 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
178 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700179 }
Santos Cordon479b3022015-02-06 04:27:15 -0800180 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700181 }
182
183 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800184 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
185 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
186 * will be returned.
187 *
Tyler Gunn84253572014-09-02 14:50:05 -0700188 * @param uriScheme The URI scheme for the outgoing call.
189 * @return The {@link PhoneAccountHandle} to use.
190 */
Tony Mak240656f2015-12-04 11:36:22 +0000191 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
192 UserHandle userHandle) {
193 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700194
Yorke Lee5e8836a2014-08-22 15:25:18 -0700195 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700196 // If there is a default PhoneAccount, ensure it supports calls to handles with the
197 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000198 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700199 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700200 return userSelected;
201 }
Ihab Awad293edf22014-07-24 17:52:29 -0700202 }
203
Tony Mak240656f2015-12-04 11:36:22 +0000204 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
205 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700206 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700207 case 0:
208 // There are no accounts, so there can be no default
209 return null;
210 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800211 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700212 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700213 default:
214 // There are multiple accounts with no selected default
215 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700216 }
Ihab Awad104f8062014-07-17 11:29:35 -0700217 }
Santos Cordon176ae282014-07-14 02:02:14 -0700218
Tony Mak240656f2015-12-04 11:36:22 +0000219 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
220 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
221 }
222
Evan Charlton105d9772014-11-25 14:08:53 -0800223 /**
224 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
225 * if it was set by another user).
226 */
Hall Liuecda5542015-12-04 11:31:31 -0800227 @VisibleForTesting
228 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000229 if (userHandle == null) {
230 return null;
231 }
232 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
233 .get(userHandle);
234 if (defaultPhoneAccountHandle == null) {
235 return null;
236 }
237 // Make sure the account is still registered and owned by the user.
238 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
239 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800240
Santos Cordon6a212642015-05-08 16:35:23 -0700241 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000242 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700243 }
244 return null;
245 }
246
Santos Cordon6a212642015-05-08 16:35:23 -0700247 /**
248 * Sets the phone account with which to place all calls by default. Set by the user
249 * within phone settings.
250 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000251 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
252 UserHandle userHandle) {
253 if (userHandle == null) {
254 return;
255 }
Evan Charlton89176372014-07-19 18:23:09 -0700256 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700257 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000258 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700259 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000260 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700261 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700262 Log.w(this, "Trying to set nonexistent default outgoing %s",
263 accountHandle);
264 return;
265 }
266
Santos Cordon6a212642015-05-08 16:35:23 -0700267 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700268 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700269 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700270 return;
271 }
272
Santos Cordon6a212642015-05-08 16:35:23 -0700273 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700274 // If the account selected is a SIM account, propagate down to the subscription
275 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700276 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800277 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700278 }
279
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000280 mState.defaultOutgoingAccountHandles
281 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle));
Santos Cordon176ae282014-07-14 02:02:14 -0700282 }
283
Ihab Awad293edf22014-07-24 17:52:29 -0700284 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700285 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700286 }
287
Nancy Chen668dee02014-11-19 15:31:31 -0800288 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800289 return getSubscriptionIdForPhoneAccount(accountHandle) ==
290 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700291 }
292
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700293 public ComponentName getSystemSimCallManagerComponent() {
294 String defaultSimCallManager = null;
295 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
296 Context.CARRIER_CONFIG_SERVICE);
297 PersistableBundle configBundle = configManager.getConfig();
298 if (configBundle != null) {
299 defaultSimCallManager = configBundle.getString(
300 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
301 }
302 return TextUtils.isEmpty(defaultSimCallManager)
303 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
304 }
305
Tony Mak240656f2015-12-04 11:36:22 +0000306 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
307 return getSimCallManager(mCurrentUserHandle);
308 }
309
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700310 /**
311 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
312 * Manager. SIM Call Manager returned corresponds to the following priority order:
313 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
314 * default dialer, then that one is returned.
315 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
316 * carrier configuration's default, then that one is returned.
317 * 3. Otherwise, we return null.
318 */
Tony Mak240656f2015-12-04 11:36:22 +0000319 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700320 // Get the default dialer in case it has a connection manager associated with it.
Tony Mak240656f2015-12-04 11:36:22 +0000321 String dialerPackage = DefaultDialerManager
322 .getDefaultDialerApplication(mContext, userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700323
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700324 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700325 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800326
Santos Cordon59c21a72015-06-11 10:11:21 -0700327 PhoneAccountHandle dialerSimCallManager = null;
328 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800329
Santos Cordon59c21a72015-06-11 10:11:21 -0700330 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
331 // loop through and look for any connection manager in the same package.
332 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
333 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000334 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700335 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
336 ComponentName component = accountHandle.getComponentName();
337
338 // Store the system connection manager if found
339 if (systemSimCallManager == null
340 && Objects.equals(component, systemSimCallManagerComponent)
341 && !resolveComponent(accountHandle).isEmpty()) {
342 systemSimCallManager = accountHandle;
343
344 // Store the dialer connection manager if found
345 } else if (dialerSimCallManager == null
346 && Objects.equals(component.getPackageName(), dialerPackage)
347 && !resolveComponent(accountHandle).isEmpty()) {
348 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700349 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700350 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700351 }
352
Santos Cordon59c21a72015-06-11 10:11:21 -0700353 PhoneAccountHandle retval = dialerSimCallManager != null ?
354 dialerSimCallManager : systemSimCallManager;
355
356 Log.i(this, "SimCallManager queried, returning: %s", retval);
357
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700358 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700359 }
360
Evan Charlton105d9772014-11-25 14:08:53 -0800361 /**
Tony Mak240656f2015-12-04 11:36:22 +0000362 * If it is a outgoing call, sim call manager of call-initiating user is returned.
363 * Otherwise, we return the sim call manager of the user associated with the
364 * target phone account.
365 * @return phone account handle of sim call manager based on the ongoing call.
366 */
367 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
368 if (call == null) {
369 return null;
370 }
371 UserHandle userHandle = call.getInitiatingUser();
372 if (userHandle == null) {
373 userHandle = call.getTargetPhoneAccount().getUserHandle();
374 }
375 return getSimCallManager(userHandle);
376 }
377
378 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800379 * Update the current UserHandle to track when users are switched. This will allow the
380 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
381 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700382 * We cannot simply check the calling user because that would always return the primary user for
383 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800384 *
385 * @param userHandle The {@link UserHandle}, as delivered by
386 * {@link Intent#ACTION_USER_SWITCHED}.
387 */
388 public void setCurrentUserHandle(UserHandle userHandle) {
389 if (userHandle == null) {
390 Log.d(this, "setCurrentUserHandle, userHandle = null");
391 userHandle = Process.myUserHandle();
392 }
393 Log.d(this, "setCurrentUserHandle, %s", userHandle);
394 mCurrentUserHandle = userHandle;
395 }
396
Yorke Lee71734c22015-06-02 14:22:56 -0700397 /**
398 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
399 * otherwise.
400 */
401 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000402 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700403 if (account == null) {
404 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700405 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700406 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700407 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700408 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700409 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700410 }
411
Santos Cordon638f05c2015-05-19 17:12:33 -0700412 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700413 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000414 if (!isEnabled) {
415 // If the disabled account is the default, remove it.
416 removeDefaultPhoneAccountHandle(accountHandle);
417 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700418 write();
419 fireAccountsChanged();
420 }
Yorke Lee71734c22015-06-02 14:22:56 -0700421 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700422 }
423
Tony Mak48367162016-01-15 22:58:18 +0000424 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
425 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
426 mState.defaultOutgoingAccountHandles.entrySet().iterator();
427 while (iterator.hasNext()) {
428 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
429 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
430 iterator.remove();
431 }
432 }
433 }
434
Tony Mak240656f2015-12-04 11:36:22 +0000435 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
436 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800437 if (account == null) {
438 return false;
439 }
440
Tony Mak240656f2015-12-04 11:36:22 +0000441 if (userHandle == null) {
442 Log.w(this, "userHandle is null in isVisibleForUser");
443 return false;
444 }
445
Evan Charlton105d9772014-11-25 14:08:53 -0800446 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
447 // all profiles. Only Telephony and SIP accounts should have this capability.
448 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
449 return true;
450 }
451
452 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
453 if (phoneAccountUserHandle == null) {
454 return false;
455 }
456
457 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000458 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800459 Log.d(this, "Current user is null; assuming true");
460 return true;
461 }
462
Tony Mak240656f2015-12-04 11:36:22 +0000463 if (acrossProfiles) {
464 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
465 phoneAccountUserHandle.getIdentifier());
466 } else {
467 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800468 }
Evan Charlton105d9772014-11-25 14:08:53 -0800469 }
470
471 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
472 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000473 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800474 }
475
476 private List<ResolveInfo> resolveComponent(ComponentName componentName,
477 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800478 PackageManager pm = mContext.getPackageManager();
479 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
480 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800481 try {
482 if (userHandle != null) {
483 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
484 } else {
485 return pm.queryIntentServices(intent, 0);
486 }
487 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700488 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800489 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800490 }
mike dooley10a58312014-11-06 13:46:19 -0800491 }
492
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700493 /**
494 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700495 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700496 *
497 * @return The list of {@link PhoneAccountHandle}s.
498 */
Tony Mak240656f2015-12-04 11:36:22 +0000499 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
500 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700501 }
502
Tony Mak240656f2015-12-04 11:36:22 +0000503 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
504 return getPhoneAccounts(0, null, null, false, userHandle);
505 }
506
507 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
508 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700509 }
510
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700511 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700512 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700513 * specified URI scheme.
514 *
515 * @param uriScheme The URI scheme.
516 * @return The phone account handles.
517 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700518 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000519 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700520 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700521 PhoneAccount.CAPABILITY_CALL_PROVIDER,
522 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000523 uriScheme, null, includeDisabledAccounts, userHandle);
524 }
525
526 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
527 String uriScheme, boolean includeDisabledAccounts) {
528 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700529 }
530
531 /**
532 * Retrieves a list of all the SIM-based phone accounts.
533 */
Tony Mak240656f2015-12-04 11:36:22 +0000534 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700535 return getPhoneAccountHandles(
536 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000537 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700538 }
539
Tony Mak240656f2015-12-04 11:36:22 +0000540 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
541 return getSimPhoneAccounts(mCurrentUserHandle);
542 }
543
544 /**
545 * Retrieves a list of all phone accounts registered by a specified package.
546 *
547 * @param packageName The name of the package that registered the phone accounts.
548 * @return The phone account handles.
549 */
550 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
551 UserHandle userHandle) {
552 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700553 }
554
Ihab Awad104f8062014-07-17 11:29:35 -0700555 // TODO: Should we implement an artificial limit for # of accounts associated with a single
556 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700557 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700558 // Enforce the requirement that a connection service for a phone account has the correct
559 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700560 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700561 Log.w(this,
562 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700563 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700564 throw new SecurityException("PhoneAccount connection service requires "
565 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700566 }
567
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700568 addOrReplacePhoneAccount(account);
569 }
570
571 /**
572 * Adds a {@code PhoneAccount}, replacing an existing one if found.
573 *
574 * @param account The {@code PhoneAccount} to add or replace.
575 */
576 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800577 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
578 account.getAccountHandle(), account);
579
Santos Cordonea5cb932015-05-07 16:28:38 -0700580 // Start _enabled_ property as false.
581 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
582 // source app provides or else an third party app could enable itself.
583 boolean isEnabled = false;
584
Tony Mak98e6bdc2015-11-25 21:54:05 +0000585 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700586 if (oldAccount != null) {
587 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700588 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700589 Log.i(this, getAccountDiffString(account, oldAccount));
590 } else {
591 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700592 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700593
Santos Cordon6a212642015-05-08 16:35:23 -0700594 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700595 // Reset enabled state to whatever the value was if the account was already registered,
596 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
597 account.setIsEnabled(
598 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700599
Ihab Awad293edf22014-07-24 17:52:29 -0700600 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700601 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700602 }
603
Evan Charlton89176372014-07-19 18:23:09 -0700604 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000605 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700606 if (account != null) {
607 if (mState.accounts.remove(account)) {
608 write();
609 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700610 }
611 }
Santos Cordon176ae282014-07-14 02:02:14 -0700612 }
613
Tyler Gunnd900ce62014-08-13 11:40:59 -0700614 /**
615 * Un-registers all phone accounts associated with a specified package.
616 *
617 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800618 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700619 */
Evan Charlton105d9772014-11-25 14:08:53 -0800620 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700621 boolean accountsRemoved = false;
622 Iterator<PhoneAccount> it = mState.accounts.iterator();
623 while (it.hasNext()) {
624 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800625 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
626 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
627 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700628 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530629 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700630 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700631 }
632 }
633
Tyler Gunnd900ce62014-08-13 11:40:59 -0700634 if (accountsRemoved) {
635 write();
636 fireAccountsChanged();
637 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700638 }
639
Nancy Chen140004a2014-10-15 15:48:38 -0700640 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700641 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700642 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700643 }
644
Ihab Awadb78b2762014-07-25 15:16:23 -0700645 public void addListener(Listener l) {
646 mListeners.add(l);
647 }
648
649 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700650 if (l != null) {
651 mListeners.remove(l);
652 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700653 }
654
655 private void fireAccountsChanged() {
656 for (Listener l : mListeners) {
657 l.onAccountsChanged(this);
658 }
659 }
660
661 private void fireDefaultOutgoingChanged() {
662 for (Listener l : mListeners) {
663 l.onDefaultOutgoingChanged(this);
664 }
665 }
666
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700667 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
668 if (account1 == null || account2 == null) {
669 return "Diff: " + account1 + ", " + account2;
670 }
671
672 StringBuffer sb = new StringBuffer();
673 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700674 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
675 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700676 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
677 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
678 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
679 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
680 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700681 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
682 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700683 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
684 account2.getSupportedUriSchemes());
685 sb.append("]");
686 return sb.toString();
687 }
688
689 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
690 if (!Objects.equals(obj1, obj2)) {
691 sb.append("(")
692 .append(attrName)
693 .append(": ")
694 .append(obj1)
695 .append(" -> ")
696 .append(obj2)
697 .append(")");
698 }
699 }
700
Tyler Gunncb59b672014-08-20 09:02:11 -0700701 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700702 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700703 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700704 *
705 * @param phoneAccountHandle The phone account to check.
706 * @return {@code True} if the phone account has permission.
707 */
Santos Cordon6a212642015-05-08 16:35:23 -0700708 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800709 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
710 if (resolveInfos.isEmpty()) {
711 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700712 return false;
713 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800714 for (ResolveInfo resolveInfo : resolveInfos) {
715 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700716 if (serviceInfo == null) {
717 return false;
718 }
719
720 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
721 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
722 serviceInfo.permission)) {
723 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
724 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
725 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800726 return false;
727 }
728 }
729 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700730 }
731
Santos Cordon6a212642015-05-08 16:35:23 -0700732 //
733 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
734 //
Ihab Awad293edf22014-07-24 17:52:29 -0700735
Santos Cordonafe59e52014-08-22 16:48:43 -0700736 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700737 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700738 *
Santos Cordon6a212642015-05-08 16:35:23 -0700739 * @param handle
740 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700741 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000742 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700743 for (PhoneAccount m : mState.accounts) {
744 if (Objects.equals(handle, m.getAccountHandle())) {
745 return m;
746 }
747 }
748 return null;
749 }
750
751 /**
752 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
753 * account before returning it. The current user is the active user on the actual android
754 * device.
755 */
Tony Mak240656f2015-12-04 11:36:22 +0000756 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
757 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
758 }
759
760 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
761 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000762 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000763 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700764 return account;
765 }
766 return null;
767 }
768
Tony Mak240656f2015-12-04 11:36:22 +0000769 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
770 return getPhoneAccount(handle, mCurrentUserHandle);
771 }
772
Bryce Leea0bb7052015-10-16 13:31:40 -0700773 private List<PhoneAccountHandle> getPhoneAccountHandles(
774 int capabilities,
775 String uriScheme,
776 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000777 boolean includeDisabledAccounts,
778 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700779 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000780 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700781 }
782
Santos Cordon6a212642015-05-08 16:35:23 -0700783 /**
784 * Returns a list of phone account handles with the specified capabilities, uri scheme,
785 * and package name.
786 */
787 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700788 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700789 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700790 String uriScheme,
791 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000792 boolean includeDisabledAccounts,
793 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700794 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700795
796 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700797 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000798 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700799 handles.add(account.getAccountHandle());
800 }
801 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700802 }
803
Bryce Leea0bb7052015-10-16 13:31:40 -0700804 private List<PhoneAccount> getPhoneAccounts(
805 int capabilities,
806 String uriScheme,
807 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000808 boolean includeDisabledAccounts,
809 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700810 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000811 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700812 }
813
Tyler Gunn84253572014-09-02 14:50:05 -0700814 /**
815 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700816 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700817 *
Santos Cordon6a212642015-05-08 16:35:23 -0700818 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700819 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
820 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700821 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700822 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700823 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700824 */
Santos Cordon6a212642015-05-08 16:35:23 -0700825 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700826 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700827 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700828 String uriScheme,
829 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000830 boolean includeDisabledAccounts,
831 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700832 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700833 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700834 if (!(m.isEnabled() || includeDisabledAccounts)) {
835 // Do not include disabled accounts.
836 continue;
837 }
838
Bryce Leea0bb7052015-10-16 13:31:40 -0700839 if ((m.getCapabilities() & excludedCapabilities) != 0) {
840 // If an excluded capability is present, skip.
841 continue;
842 }
843
Santos Cordon6a212642015-05-08 16:35:23 -0700844 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800845 // Account doesn't have the right capabilities; skip this one.
846 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700847 }
Evan Charlton105d9772014-11-25 14:08:53 -0800848 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
849 // Account doesn't support this URI scheme; skip this one.
850 continue;
851 }
Santos Cordon6a212642015-05-08 16:35:23 -0700852 PhoneAccountHandle handle = m.getAccountHandle();
853
854 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800855 // This component cannot be resolved anymore; skip this one.
856 continue;
857 }
Santos Cordon6a212642015-05-08 16:35:23 -0700858 if (packageName != null &&
859 !packageName.equals(handle.getComponentName().getPackageName())) {
860 // Not the right package name; skip this one.
861 continue;
862 }
Tony Mak240656f2015-12-04 11:36:22 +0000863 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800864 // Account is not visible for the current user; skip this one.
865 continue;
866 }
Santos Cordon6a212642015-05-08 16:35:23 -0700867 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700868 }
Santos Cordon6a212642015-05-08 16:35:23 -0700869 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700870 }
871
Santos Cordon6a212642015-05-08 16:35:23 -0700872 //
873 // State Implementation for PhoneAccountRegistrar
874 //
875
Ihab Awad293edf22014-07-24 17:52:29 -0700876 /**
877 * The state of this {@code PhoneAccountRegistrar}.
878 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700879 @VisibleForTesting
880 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700881 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000882 * Store the default phone account handle of users. If no record of a user can be found in
883 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -0700884 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000885 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
886 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700887
888 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700889 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700890 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530891 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700892
893 /**
894 * The version number of the State data.
895 */
896 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700897 }
898
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700899 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000900 * The default {@link PhoneAccountHandle} of a user.
901 */
902 public static class DefaultPhoneAccountHandle {
903
904 public final UserHandle userHandle;
905
906 public final PhoneAccountHandle phoneAccountHandle;
907
908 public DefaultPhoneAccountHandle(UserHandle userHandle,
909 PhoneAccountHandle phoneAccountHandle) {
910 this.userHandle = userHandle;
911 this.phoneAccountHandle = phoneAccountHandle;
912 }
913 }
914
915 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700916 * Dumps the state of the {@link CallsManager}.
917 *
918 * @param pw The {@code IndentingPrintWriter} to write the state to.
919 */
920 public void dump(IndentingPrintWriter pw) {
921 if (mState != null) {
922 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000923 DefaultPhoneAccountHandle defaultPhoneAccountHandle
924 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
925 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
926 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +0000927 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700928 pw.println("phoneAccounts:");
929 pw.increaseIndent();
930 for (PhoneAccount phoneAccount : mState.accounts) {
931 pw.println(phoneAccount);
932 }
933 pw.decreaseIndent();
934 }
935 }
936
Ihab Awad293edf22014-07-24 17:52:29 -0700937 ////////////////////////////////////////////////////////////////////////////////////////////////
938 //
939 // State management
940 //
941
Hall Liu3f7f64d2016-01-07 17:40:13 -0800942 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
943 @Override
944 public Void doInBackground(ByteArrayOutputStream... args) {
945 final ByteArrayOutputStream buffer = args[0];
946 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700947 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -0800948 synchronized (mWriteLock) {
949 fileOutput = mAtomicFile.startWrite();
950 buffer.writeTo(fileOutput);
951 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -0700952 }
Hall Liu3f7f64d2016-01-07 17:40:13 -0800953 } catch (IOException e) {
954 Log.e(this, e, "Writing state to XML file");
955 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -0700956 }
Hall Liu3f7f64d2016-01-07 17:40:13 -0800957 return null;
958 }
959 }
960
961 private void write() {
962 try {
963 ByteArrayOutputStream os = new ByteArrayOutputStream();
964 XmlSerializer serializer = new FastXmlSerializer();
965 serializer.setOutput(os, "utf-8");
966 writeToXml(mState, serializer, mContext);
967 serializer.flush();
968 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -0700969 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -0800970 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -0700971 }
972 }
973
Ihab Awadb78b2762014-07-25 15:16:23 -0700974 private void read() {
975 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700976 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700977 is = mAtomicFile.openRead();
978 } catch (FileNotFoundException ex) {
979 return;
980 }
981
Tyler Gunn84253572014-09-02 14:50:05 -0700982 boolean versionChanged = false;
983
Ihab Awadb78b2762014-07-25 15:16:23 -0700984 XmlPullParser parser;
985 try {
986 parser = Xml.newPullParser();
987 parser.setInput(new BufferedInputStream(is), null);
988 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700989 mState = readFromXml(parser, mContext);
990 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
991
Ihab Awadb78b2762014-07-25 15:16:23 -0700992 } catch (IOException | XmlPullParserException e) {
993 Log.e(this, e, "Reading state from XML file");
994 mState = new State();
995 } finally {
996 try {
997 is.close();
998 } catch (IOException e) {
999 Log.e(this, e, "Closing InputStream");
1000 }
Ihab Awad104f8062014-07-17 11:29:35 -07001001 }
Tyler Gunn84253572014-09-02 14:50:05 -07001002
Evan Charlton105d9772014-11-25 14:08:53 -08001003 // Verify all of the UserHandles.
1004 List<PhoneAccount> badAccounts = new ArrayList<>();
1005 for (PhoneAccount phoneAccount : mState.accounts) {
1006 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1007 if (userHandle == null) {
1008 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1009 badAccounts.add(phoneAccount);
1010 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1011 Log.w(this, "User does not exist for %s", phoneAccount);
1012 badAccounts.add(phoneAccount);
1013 }
1014 }
1015 mState.accounts.removeAll(badAccounts);
1016
Tyler Gunn84253572014-09-02 14:50:05 -07001017 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001018 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001019 write();
1020 }
Santos Cordon176ae282014-07-14 02:02:14 -07001021 }
1022
Evan Charlton105d9772014-11-25 14:08:53 -08001023 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001024 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001025 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001026 }
Ihab Awad104f8062014-07-17 11:29:35 -07001027
Tyler Gunn84253572014-09-02 14:50:05 -07001028 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001029 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001030 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001031 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001032 }
1033
Ihab Awad293edf22014-07-24 17:52:29 -07001034 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001035 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001036 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001037 //
1038
Ihab Awadb78b2762014-07-25 15:16:23 -07001039 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001040 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001041 private static final String TAG_VALUE = "value";
1042 private static final String ATTRIBUTE_LENGTH = "length";
1043 private static final String ATTRIBUTE_KEY = "key";
1044 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1045 private static final String VALUE_TYPE_STRING = "string";
1046 private static final String VALUE_TYPE_INTEGER = "integer";
1047 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001048
Ihab Awadb78b2762014-07-25 15:16:23 -07001049 /**
1050 * Write the supplied object to XML
1051 */
Evan Charlton105d9772014-11-25 14:08:53 -08001052 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001053 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001054
1055 /**
1056 * Read from the supplied XML into a new object, returning null in case of an
1057 * unrecoverable schema mismatch or other data error. 'parser' must be already
1058 * positioned at the first tag that is expected to have been emitted by this
1059 * object's writeToXml(). This object tries to fail early without modifying
1060 * 'parser' if it does not recognize the data it sees.
1061 */
Tyler Gunn84253572014-09-02 14:50:05 -07001062 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001063 throws IOException, XmlPullParserException;
1064
Ihab Awadd9f54382014-10-24 11:44:47 -07001065 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001066 throws IOException {
1067 if (value != null) {
1068 serializer.startTag(null, tagName);
1069 serializer.text(Objects.toString(value));
1070 serializer.endTag(null, tagName);
1071 }
1072 }
Tyler Gunn84253572014-09-02 14:50:05 -07001073
1074 /**
1075 * Serializes a string array.
1076 *
1077 * @param tagName The tag name for the string array.
1078 * @param values The string values to serialize.
1079 * @param serializer The serializer.
1080 * @throws IOException
1081 */
1082 protected void writeStringList(String tagName, List<String> values,
1083 XmlSerializer serializer)
1084 throws IOException {
1085
1086 serializer.startTag(null, tagName);
1087 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001088 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001089 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001090 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001091 if (toSerialize != null ){
1092 serializer.text(toSerialize);
1093 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001094 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001095 }
1096 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001097 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1098 }
1099 serializer.endTag(null, tagName);
1100 }
1101
1102 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1103 throws IOException {
1104
1105 serializer.startTag(null, tagName);
1106 if (values != null) {
1107 for (String key : values.keySet()) {
1108 Object value = values.get(key);
1109
1110 if (value == null) {
1111 continue;
1112 }
1113
1114 String valueType;
1115 if (value instanceof String) {
1116 valueType = VALUE_TYPE_STRING;
1117 } else if (value instanceof Integer) {
1118 valueType = VALUE_TYPE_INTEGER;
1119 } else if (value instanceof Boolean) {
1120 valueType = VALUE_TYPE_BOOLEAN;
1121 } else {
1122 Log.w(this,
1123 "PhoneAccounts support only string, integer and boolean extras TY.");
1124 continue;
1125 }
1126
1127 serializer.startTag(null, TAG_VALUE);
1128 serializer.attribute(null, ATTRIBUTE_KEY, key);
1129 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1130 serializer.text(Objects.toString(value));
1131 serializer.endTag(null, TAG_VALUE);
1132 }
Tyler Gunn84253572014-09-02 14:50:05 -07001133 }
1134 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001135 }
Tyler Gunn84253572014-09-02 14:50:05 -07001136
Santos Cordon9c30c282015-05-13 16:28:27 -07001137 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001138 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001139 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001140 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001141 value.writeToStream(stream);
1142 byte[] iconByteArray = stream.toByteArray();
1143 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001144
1145 serializer.startTag(null, tagName);
1146 serializer.text(text);
1147 serializer.endTag(null, tagName);
1148 }
Tyler Gunn84253572014-09-02 14:50:05 -07001149 }
1150
Evan Charlton105d9772014-11-25 14:08:53 -08001151 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1152 throws IOException {
1153 serializer.startTag(null, tagName);
1154 serializer.text(Long.valueOf(value).toString());
1155 serializer.endTag(null, tagName);
1156 }
1157
Tyler Gunn84253572014-09-02 14:50:05 -07001158 /**
1159 * Reads a string array from the XML parser.
1160 *
1161 * @param parser The XML parser.
1162 * @return String array containing the parsed values.
1163 * @throws IOException Exception related to IO.
1164 * @throws XmlPullParserException Exception related to parsing.
1165 */
1166 protected List<String> readStringList(XmlPullParser parser)
1167 throws IOException, XmlPullParserException {
1168
Tyler Gunn369c8742015-10-07 15:39:28 -07001169 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001170 List<String> arrayEntries = new ArrayList<String>(length);
1171 String value = null;
1172
1173 if (length == 0) {
1174 return arrayEntries;
1175 }
1176
1177 int outerDepth = parser.getDepth();
1178 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001179 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001180 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001181 value = parser.getText();
1182 arrayEntries.add(value);
1183 }
1184 }
1185
1186 return arrayEntries;
1187 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001188
Tyler Gunn369c8742015-10-07 15:39:28 -07001189 /**
1190 * Reads a bundle from the XML parser.
1191 *
1192 * @param parser The XML parser.
1193 * @return Bundle containing the parsed values.
1194 * @throws IOException Exception related to IO.
1195 * @throws XmlPullParserException Exception related to parsing.
1196 */
1197 protected Bundle readBundle(XmlPullParser parser)
1198 throws IOException, XmlPullParserException {
1199
1200 Bundle bundle = null;
1201 int outerDepth = parser.getDepth();
1202 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1203 if (parser.getName().equals(TAG_VALUE)) {
1204 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1205 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1206 parser.next();
1207 String value = parser.getText();
1208
1209 if (bundle == null) {
1210 bundle = new Bundle();
1211 }
1212
1213 // Do not write null values to the bundle.
1214 if (value == null) {
1215 continue;
1216 }
1217
1218 if (VALUE_TYPE_STRING.equals(valueType)) {
1219 bundle.putString(key, value);
1220 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1221 try {
1222 int intValue = Integer.parseInt(value);
1223 bundle.putInt(key, intValue);
1224 } catch (NumberFormatException nfe) {
1225 Log.w(this, "Invalid integer PhoneAccount extra.");
1226 }
1227 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1228 boolean boolValue = Boolean.parseBoolean(value);
1229 bundle.putBoolean(key, boolValue);
1230 } else {
1231 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1232 }
1233 }
1234 }
1235 return bundle;
1236 }
1237
Santos Cordon9c30c282015-05-13 16:28:27 -07001238 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001239 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1240 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1241 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001242
1243 protected Icon readIcon(XmlPullParser parser) throws IOException {
1244 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1245 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1246 return Icon.createFromStream(stream);
1247 }
Ihab Awad104f8062014-07-17 11:29:35 -07001248 }
1249
Ihab Awadb78b2762014-07-25 15:16:23 -07001250 @VisibleForTesting
1251 public static final XmlSerialization<State> sStateXml =
1252 new XmlSerialization<State>() {
1253 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001254 private static final String DEFAULT_OUTGOING = "default_outgoing";
1255 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001256 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001257
1258 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001259 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001260 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001261 if (o != null) {
1262 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001263 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001264
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001265 serializer.startTag(null, DEFAULT_OUTGOING);
1266 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1267 .defaultOutgoingAccountHandles.values()) {
1268 sDefaultPhoneAcountHandleXml
1269 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001270 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001271 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001272
Ihab Awad26923222014-07-30 10:54:35 -07001273 serializer.startTag(null, ACCOUNTS);
1274 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001275 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001276 }
1277 serializer.endTag(null, ACCOUNTS);
1278
1279 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001280 }
Ihab Awad104f8062014-07-17 11:29:35 -07001281 }
1282
1283 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001284 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001285 throws IOException, XmlPullParserException {
1286 if (parser.getName().equals(CLASS_STATE)) {
1287 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001288
1289 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001290 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001291
Ihab Awadb78b2762014-07-25 15:16:23 -07001292 int outerDepth = parser.getDepth();
1293 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1294 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001295 if (s.versionNumber < 9) {
1296 // Migration old default phone account handle here by assuming the
1297 // default phone account handle is belong to primary user.
1298 parser.nextTag();
1299 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1300 .readFromXml(parser, s.versionNumber, context);
1301 UserManager userManager = UserManager.get(context);
1302 UserInfo primaryUser = userManager.getPrimaryUser();
1303 if (primaryUser != null) {
1304 UserHandle userHandle = primaryUser.getUserHandle();
1305 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1306 = new DefaultPhoneAccountHandle(userHandle,
1307 phoneAccountHandle);
1308 s.defaultOutgoingAccountHandles
1309 .put(userHandle, defaultPhoneAccountHandle);
1310 }
1311 } else {
1312 int defaultAccountHandlesDepth = parser.getDepth();
1313 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1314 DefaultPhoneAccountHandle accountHandle
1315 = sDefaultPhoneAcountHandleXml
1316 .readFromXml(parser, s.versionNumber, context);
1317 if (accountHandle != null && s.accounts != null) {
1318 s.defaultOutgoingAccountHandles
1319 .put(accountHandle.userHandle, accountHandle);
1320 }
1321 }
1322 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001323 } else if (parser.getName().equals(ACCOUNTS)) {
1324 int accountsDepth = parser.getDepth();
1325 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001326 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1327 s.versionNumber, context);
1328
1329 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001330 s.accounts.add(account);
1331 }
1332 }
Ihab Awad104f8062014-07-17 11:29:35 -07001333 }
1334 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001335 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001336 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001337 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001338 }
1339 };
1340
Ihab Awadb78b2762014-07-25 15:16:23 -07001341 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001342 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1343 new XmlSerialization<DefaultPhoneAccountHandle>() {
1344 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1345 = "default_outgoing_phone_account_handle";
1346 private static final String USER_SERIAL_NUMBER = "user_serial_number";
1347 private static final String ACCOUNT_HANDLE = "account_handle";
1348
1349 @Override
1350 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1351 Context context) throws IOException {
1352 if (o != null) {
1353 final UserManager userManager = UserManager.get(context);
1354 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1355 if (serialNumber != -1) {
1356 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1357 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
1358 serializer.startTag(null, ACCOUNT_HANDLE);
1359 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1360 context);
1361 serializer.endTag(null, ACCOUNT_HANDLE);
1362 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1363 }
1364 }
1365 }
1366
1367 @Override
1368 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1369 Context context)
1370 throws IOException, XmlPullParserException {
1371 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1372 int outerDepth = parser.getDepth();
1373 PhoneAccountHandle accountHandle = null;
1374 String userSerialNumberString = null;
1375 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1376 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1377 parser.nextTag();
1378 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1379 context);
1380 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1381 parser.next();
1382 userSerialNumberString = parser.getText();
1383 }
1384 }
1385 UserHandle userHandle = null;
1386 if (userSerialNumberString != null) {
1387 try {
1388 long serialNumber = Long.parseLong(userSerialNumberString);
1389 userHandle = UserManager.get(context)
1390 .getUserForSerialNumber(serialNumber);
1391 } catch (NumberFormatException e) {
1392 Log.e(this, e,
1393 "Could not parse UserHandle " + userSerialNumberString);
1394 }
1395 }
1396 if (accountHandle != null && userHandle != null) {
1397 return new DefaultPhoneAccountHandle(userHandle, accountHandle);
1398 }
1399 }
1400 return null;
1401 }
1402 };
1403
1404
1405 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001406 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1407 new XmlSerialization<PhoneAccount>() {
1408 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1409 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001410 private static final String ADDRESS = "handle";
1411 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001412 private static final String CAPABILITIES = "capabilities";
1413 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001414 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1415 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001416 private static final String ICON_TINT = "icon_tint";
1417 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001418 private static final String LABEL = "label";
1419 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001420 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001421 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001422 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001423 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001424
1425 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001426 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001427 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001428 if (o != null) {
1429 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001430
Ihab Awad26923222014-07-30 10:54:35 -07001431 if (o.getAccountHandle() != null) {
1432 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001433 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001434 serializer.endTag(null, ACCOUNT_HANDLE);
1435 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001436
Ihab Awadd9f54382014-10-24 11:44:47 -07001437 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1438 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1439 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001440 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001441 writeTextIfNonNull(HIGHLIGHT_COLOR,
1442 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001443 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1444 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001445 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001446 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001447 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001448
Ihab Awad26923222014-07-30 10:54:35 -07001449 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1450 }
Ihab Awad104f8062014-07-17 11:29:35 -07001451 }
1452
Tyler Gunn84253572014-09-02 14:50:05 -07001453 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001454 throws IOException, XmlPullParserException {
1455 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1456 int outerDepth = parser.getDepth();
1457 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001458 Uri address = null;
1459 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001460 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001461 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001462 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001463 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001464 int iconTint = PhoneAccount.NO_ICON_TINT;
1465 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001466 String label = null;
1467 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001468 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001469 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001470 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001471 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001472
1473 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1474 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1475 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001476 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1477 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001478 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001479 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001480 address = Uri.parse(parser.getText());
1481 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001482 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001483 String nextText = parser.getText();
1484 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001485 } else if (parser.getName().equals(CAPABILITIES)) {
1486 parser.next();
1487 capabilities = Integer.parseInt(parser.getText());
1488 } else if (parser.getName().equals(ICON_RES_ID)) {
1489 parser.next();
1490 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001491 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1492 parser.next();
1493 iconPackageName = parser.getText();
1494 } else if (parser.getName().equals(ICON_BITMAP)) {
1495 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001496 iconBitmap = readBitmap(parser);
1497 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001498 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001499 iconTint = Integer.parseInt(parser.getText());
1500 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1501 parser.next();
1502 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001503 } else if (parser.getName().equals(LABEL)) {
1504 parser.next();
1505 label = parser.getText();
1506 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1507 parser.next();
1508 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001509 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1510 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001511 } else if (parser.getName().equals(ICON)) {
1512 parser.next();
1513 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001514 } else if (parser.getName().equals(ENABLED)) {
1515 parser.next();
1516 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001517 } else if (parser.getName().equals(EXTRAS)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001518 extras = readBundle(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -07001519 }
1520 }
Tyler Gunn84253572014-09-02 14:50:05 -07001521
Santos Cordona9eebe42015-06-11 14:07:44 -07001522 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1523 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001524 ComponentName sipComponentName = new ComponentName("com.android.phone",
1525 "com.android.services.telephony.sip.SipConnectionService");
1526
Tyler Gunn84253572014-09-02 14:50:05 -07001527 // Upgrade older phone accounts to specify the supported URI schemes.
1528 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001529 supportedUriSchemes = new ArrayList<>();
1530
1531 // Handle the SIP connection service.
1532 // Check the system settings to see if it also should handle "tel" calls.
1533 if (accountHandle.getComponentName().equals(sipComponentName)) {
1534 boolean useSipForPstn = useSipForPstnCalls(context);
1535 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1536 if (useSipForPstn) {
1537 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1538 }
1539 } else {
1540 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1541 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1542 }
1543 }
1544
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001545 // Upgrade older phone accounts with explicit package name
1546 if (version < 5) {
1547 if (iconBitmap == null) {
1548 iconPackageName = accountHandle.getComponentName().getPackageName();
1549 }
1550 }
1551
Santos Cordona82aed82015-05-26 10:43:56 -07001552 if (version < 6) {
1553 // Always enable all SIP accounts on upgrade to version 6
1554 if (accountHandle.getComponentName().equals(sipComponentName)) {
1555 enabled = true;
1556 }
1557 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001558 if (version < 7) {
1559 // Always enabled all PSTN acocunts on upgrade to version 7
1560 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1561 enabled = true;
1562 }
1563 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001564 if (version < 8) {
1565 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1566 if (accountHandle.getComponentName().equals(sipComponentName)) {
1567 Uri accountUri = Uri.parse(accountHandle.getId());
1568 if (accountUri.getScheme() != null &&
1569 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1570 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1571 accountUri.getSchemeSpecificPart(),
1572 accountHandle.getUserHandle());
1573 }
1574 }
1575 }
Santos Cordona82aed82015-05-26 10:43:56 -07001576
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001577 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001578 .setAddress(address)
1579 .setSubscriptionAddress(subscriptionAddress)
1580 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001581 .setShortDescription(shortDescription)
1582 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001583 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001584 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001585 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001586
Santos Cordon9c30c282015-05-13 16:28:27 -07001587 if (icon != null) {
1588 builder.setIcon(icon);
1589 } else if (iconBitmap != null) {
1590 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1591 } else if (!TextUtils.isEmpty(iconPackageName)) {
1592 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1593 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001594 }
1595
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001596 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001597 }
1598 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001599 }
Tyler Gunn84253572014-09-02 14:50:05 -07001600
1601 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001602 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1603 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001604 *
1605 * @param context The context.
1606 * @return {@code True} if SIP should be used for all calls.
1607 */
1608 private boolean useSipForPstnCalls(Context context) {
1609 String option = Settings.System.getString(context.getContentResolver(),
1610 Settings.System.SIP_CALL_OPTIONS);
1611 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1612 return option.equals(Settings.System.SIP_ALWAYS);
1613 }
Ihab Awad104f8062014-07-17 11:29:35 -07001614 };
1615
Ihab Awadb78b2762014-07-25 15:16:23 -07001616 @VisibleForTesting
1617 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1618 new XmlSerialization<PhoneAccountHandle>() {
1619 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001620 private static final String COMPONENT_NAME = "component_name";
1621 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001622 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001623
1624 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001625 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001626 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001627 if (o != null) {
1628 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001629
Ihab Awad26923222014-07-30 10:54:35 -07001630 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001631 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001632 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1633 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001634
Ihab Awadd9f54382014-10-24 11:44:47 -07001635 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001636
Evan Charlton105d9772014-11-25 14:08:53 -08001637 if (o.getUserHandle() != null && context != null) {
1638 UserManager userManager = UserManager.get(context);
1639 writeLong(USER_SERIAL_NUMBER,
1640 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1641 }
1642
Ihab Awad26923222014-07-30 10:54:35 -07001643 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1644 }
Ihab Awad104f8062014-07-17 11:29:35 -07001645 }
1646
1647 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001648 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001649 throws IOException, XmlPullParserException {
1650 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1651 String componentNameString = null;
1652 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001653 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001654 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001655
1656 UserManager userManager = UserManager.get(context);
1657
Ihab Awadb78b2762014-07-25 15:16:23 -07001658 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1659 if (parser.getName().equals(COMPONENT_NAME)) {
1660 parser.next();
1661 componentNameString = parser.getText();
1662 } else if (parser.getName().equals(ID)) {
1663 parser.next();
1664 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001665 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1666 parser.next();
1667 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001668 }
1669 }
Ihab Awad26923222014-07-30 10:54:35 -07001670 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001671 UserHandle userHandle = null;
1672 if (userSerialNumberString != null) {
1673 try {
1674 long serialNumber = Long.parseLong(userSerialNumberString);
1675 userHandle = userManager.getUserForSerialNumber(serialNumber);
1676 } catch (NumberFormatException e) {
1677 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1678 }
1679 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001680 return new PhoneAccountHandle(
1681 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001682 idString,
1683 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001684 }
1685 }
1686 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001687 }
1688 };
Santos Cordon176ae282014-07-14 02:02:14 -07001689}