blob: af633fbb0f9af9484771eafffd102fd7ec69d955 [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;
Christine Hallstromffe558c2016-11-30 16:05:13 -080038import android.telecom.CallAudioState;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070040import android.telecom.DefaultDialerManager;
Brad Ebinger953e1af2016-10-05 15:45:22 -070041import android.telecom.Log;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070042import android.telecom.PhoneAccount;
43import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070044import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070045import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070046import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080047import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070048import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070049import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070050import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070051import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070052
Tyler Gunn91d43cf2014-09-17 12:19:39 -070053// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070054import com.android.internal.annotations.VisibleForTesting;
55import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070056import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070057import com.android.internal.util.XmlUtils;
58
Evan Charltonaf51ceb2014-07-30 11:56:36 -070059import org.xmlpull.v1.XmlPullParser;
60import org.xmlpull.v1.XmlPullParserException;
61import org.xmlpull.v1.XmlSerializer;
62
Ihab Awadb78b2762014-07-25 15:16:23 -070063import java.io.BufferedInputStream;
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;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070080import java.util.Optional;
Tony Mak4a3e2fd2015-12-04 11:58:38 +000081import java.util.concurrent.ConcurrentHashMap;
Ihab Awadb78b2762014-07-25 15:16:23 -070082import java.util.concurrent.CopyOnWriteArrayList;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -070083import java.util.stream.Collector;
84import java.util.stream.Collectors;
85import java.util.stream.Stream;
Santos Cordon176ae282014-07-14 02:02:14 -070086
87/**
Evan Charlton89176372014-07-19 18:23:09 -070088 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070089 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
90 * implemented in {@link TelecomServiceImpl}, with the notable exception that
91 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
92 * proper authority over the {@code ComponentName}s they are declaring in their
93 * {@code PhoneAccountHandle}s.
94 *
95 *
96 * -- About Users and Phone Accounts --
97 *
Santos Cordon9c30c282015-05-13 16:28:27 -070098 * We store all phone accounts for all users in a single place, which means that there are three
99 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -0700100 * 1) The Android User that is currently active on the device.
101 * 2) The user which owns/registers the phone account.
102 * 3) The user running the app that is requesting the phone account information.
103 *
104 * 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 +0000105 * has a work profile running as another user (B2). Each user/profile only have the visibility of
106 * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts,
107 * and the list only contains phone accounts owned by user B and accounts with
108 * {@link PhoneAccount#CAPABILITY_MULTI_USER}.
Santos Cordon6a212642015-05-08 16:35:23 -0700109 *
110 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
111 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
112 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700113 */
Brad Ebinger53855132015-10-30 10:58:19 -0700114public class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700115
Yorke Lee5e8836a2014-08-22 15:25:18 -0700116 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
117 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
118
Ihab Awadb78b2762014-07-25 15:16:23 -0700119 public abstract static class Listener {
120 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
121 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
122 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
123 }
124
Tyler Gunn2b17f232017-03-08 08:51:00 -0800125 /**
126 * Abstracts away dependency on the {@link PackageManager} required to fetch the label for an
127 * app.
128 */
129 public interface AppLabelProxy {
130 CharSequence getAppLabel(String packageName);
131 }
132
Ihab Awadb78b2762014-07-25 15:16:23 -0700133 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700134 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000135 public static final int EXPECTED_STATE_VERSION = 9;
Tyler Gunn84253572014-09-02 14:50:05 -0700136
137 /** Keep in sync with the same in SipSettings.java */
138 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700139
140 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
141 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700142 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800143 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800144 private final SubscriptionManager mSubscriptionManager;
Hall Liu2b33df72016-12-06 18:15:39 -0800145 private final DefaultDialerCache mDefaultDialerCache;
Tyler Gunn2b17f232017-03-08 08:51:00 -0800146 private final AppLabelProxy mAppLabelProxy;
Ihab Awadb78b2762014-07-25 15:16:23 -0700147 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800148 private UserHandle mCurrentUserHandle;
Hall Liu3f7f64d2016-01-07 17:40:13 -0800149 private interface PhoneAccountRegistrarWriteLock {}
150 private final PhoneAccountRegistrarWriteLock mWriteLock =
151 new PhoneAccountRegistrarWriteLock() {};
Santos Cordon176ae282014-07-14 02:02:14 -0700152
Nancy Chen06ce0622014-10-23 01:17:35 +0000153 @VisibleForTesting
Tyler Gunn2b17f232017-03-08 08:51:00 -0800154 public PhoneAccountRegistrar(Context context, DefaultDialerCache defaultDialerCache,
155 AppLabelProxy appLabelProxy) {
156 this(context, FILE_NAME, defaultDialerCache, appLabelProxy);
Ihab Awadb78b2762014-07-25 15:16:23 -0700157 }
158
159 @VisibleForTesting
Hall Liu2b33df72016-12-06 18:15:39 -0800160 public PhoneAccountRegistrar(Context context, String fileName,
Tyler Gunn2b17f232017-03-08 08:51:00 -0800161 DefaultDialerCache defaultDialerCache, AppLabelProxy appLabelProxy) {
162
Ihab Awadb78b2762014-07-25 15:16:23 -0700163 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700164
Ihab Awadb78b2762014-07-25 15:16:23 -0700165 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700166 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800167 mUserManager = UserManager.get(context);
Hall Liu2b33df72016-12-06 18:15:39 -0800168 mDefaultDialerCache = defaultDialerCache;
Wink Saville33c05d32014-11-20 13:04:17 -0800169 mSubscriptionManager = SubscriptionManager.from(mContext);
Tyler Gunn2b17f232017-03-08 08:51:00 -0800170 mAppLabelProxy = appLabelProxy;
Evan Charlton105d9772014-11-25 14:08:53 -0800171 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700172 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700173 }
174
Tyler Gunn84253572014-09-02 14:50:05 -0700175 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700176 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
177 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
178 * subscription id.
179 * @param accountHandle The handle for the phone account for which to retrieve the
180 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700181 * @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 -0700182 */
Wink Saville35850602014-10-23 15:57:21 -0700183 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak240656f2015-12-04 11:36:22 +0000184 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800185
Santos Cordon6a212642015-05-08 16:35:23 -0700186 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800187 TelephonyManager tm =
188 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
189 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700190 }
Santos Cordon479b3022015-02-06 04:27:15 -0800191 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700192 }
193
194 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800195 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
196 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
197 * will be returned.
198 *
Tyler Gunn84253572014-09-02 14:50:05 -0700199 * @param uriScheme The URI scheme for the outgoing call.
200 * @return The {@link PhoneAccountHandle} to use.
201 */
Tony Mak240656f2015-12-04 11:36:22 +0000202 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme,
203 UserHandle userHandle) {
204 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700205
Yorke Lee5e8836a2014-08-22 15:25:18 -0700206 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700207 // If there is a default PhoneAccount, ensure it supports calls to handles with the
208 // specified uriScheme.
Tony Mak240656f2015-12-04 11:36:22 +0000209 final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected);
Santos Cordon6a212642015-05-08 16:35:23 -0700210 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700211 return userSelected;
212 }
Ihab Awad293edf22014-07-24 17:52:29 -0700213 }
214
Tony Mak240656f2015-12-04 11:36:22 +0000215 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false,
216 userHandle);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700217 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700218 case 0:
219 // There are no accounts, so there can be no default
220 return null;
221 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800222 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700223 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700224 default:
225 // There are multiple accounts with no selected default
226 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700227 }
Ihab Awad104f8062014-07-17 11:29:35 -0700228 }
Santos Cordon176ae282014-07-14 02:02:14 -0700229
Tony Mak240656f2015-12-04 11:36:22 +0000230 public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) {
231 return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle);
232 }
233
Evan Charlton105d9772014-11-25 14:08:53 -0800234 /**
235 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
236 * if it was set by another user).
237 */
Hall Liuecda5542015-12-04 11:31:31 -0800238 @VisibleForTesting
239 public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000240 if (userHandle == null) {
241 return null;
242 }
243 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
244 .get(userHandle);
245 if (defaultPhoneAccountHandle == null) {
246 return null;
247 }
248 // Make sure the account is still registered and owned by the user.
249 PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle,
250 userHandle);
Hall Liuecda5542015-12-04 11:31:31 -0800251
Santos Cordon6a212642015-05-08 16:35:23 -0700252 if (account != null) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000253 return defaultPhoneAccountHandle.phoneAccountHandle;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700254 }
255 return null;
256 }
257
Santos Cordon6a212642015-05-08 16:35:23 -0700258 /**
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700259 * @return The {@link DefaultPhoneAccountHandle} containing the user-selected default calling
260 * account and group Id for the {@link UserHandle} specified.
261 */
262 private DefaultPhoneAccountHandle getUserSelectedDefaultPhoneAccount(UserHandle userHandle) {
263 if (userHandle == null) {
264 return null;
265 }
266 DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles
267 .get(userHandle);
268 if (defaultPhoneAccountHandle == null) {
269 return null;
270 }
271
272 return defaultPhoneAccountHandle;
273 }
274
275 /**
276 * @return The currently registered PhoneAccount in Telecom that has the same group Id.
277 */
278 private PhoneAccount getPhoneAccountByGroupId(String groupId, ComponentName groupComponentName,
279 UserHandle userHandle, PhoneAccountHandle excludePhoneAccountHandle) {
280 if (groupId == null || groupId.isEmpty() || userHandle == null) {
281 return null;
282 }
283 // Get the PhoneAccount with the same group Id (and same ComponentName) that is not the
284 // newAccount that was just added
285 List<PhoneAccount> accounts = getAllPhoneAccounts(userHandle).stream()
286 .filter(account -> groupId.equals(account.getGroupId()) &&
287 !account.getAccountHandle().equals(excludePhoneAccountHandle) &&
288 Objects.equals(account.getAccountHandle().getComponentName(),
289 groupComponentName))
290 .collect(Collectors.toList());
291 // There should be one or no PhoneAccounts with the same group Id
292 if (accounts.size() > 1) {
293 Log.w(this, "Found multiple PhoneAccounts registered to the same Group Id!");
294 }
295 return accounts.isEmpty() ? null : accounts.get(0);
296 }
297
298 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700299 * Sets the phone account with which to place all calls by default. Set by the user
300 * within phone settings.
301 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000302 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle,
303 UserHandle userHandle) {
304 if (userHandle == null) {
305 return;
306 }
Evan Charlton89176372014-07-19 18:23:09 -0700307 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700308 // Asking to clear the default outgoing is a valid request
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000309 mState.defaultOutgoingAccountHandles.remove(userHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700310 } else {
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000311 PhoneAccount account = getPhoneAccount(accountHandle, userHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700312 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700313 Log.w(this, "Trying to set nonexistent default outgoing %s",
314 accountHandle);
315 return;
316 }
317
Santos Cordon6a212642015-05-08 16:35:23 -0700318 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700319 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700320 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700321 return;
322 }
323
Santos Cordon6a212642015-05-08 16:35:23 -0700324 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700325 // If the account selected is a SIM account, propagate down to the subscription
326 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700327 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800328 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700329 }
330
Tony Mak4a3e2fd2015-12-04 11:58:38 +0000331 mState.defaultOutgoingAccountHandles
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700332 .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle,
333 account.getGroupId()));
Santos Cordon176ae282014-07-14 02:02:14 -0700334 }
335
Ihab Awad293edf22014-07-24 17:52:29 -0700336 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700337 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700338 }
339
Nancy Chen668dee02014-11-19 15:31:31 -0800340 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
Shishir Agrawalce688742016-01-25 15:02:21 -0800341 return getSubscriptionIdForPhoneAccount(accountHandle) ==
342 SubscriptionManager.getDefaultSmsSubscriptionId();
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700343 }
344
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700345 public ComponentName getSystemSimCallManagerComponent() {
346 String defaultSimCallManager = null;
347 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
348 Context.CARRIER_CONFIG_SERVICE);
349 PersistableBundle configBundle = configManager.getConfig();
350 if (configBundle != null) {
351 defaultSimCallManager = configBundle.getString(
352 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
353 }
354 return TextUtils.isEmpty(defaultSimCallManager)
355 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
356 }
357
Tony Mak240656f2015-12-04 11:36:22 +0000358 public PhoneAccountHandle getSimCallManagerOfCurrentUser() {
359 return getSimCallManager(mCurrentUserHandle);
360 }
361
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700362 /**
363 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
364 * Manager. SIM Call Manager returned corresponds to the following priority order:
365 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
366 * default dialer, then that one is returned.
367 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
368 * carrier configuration's default, then that one is returned.
369 * 3. Otherwise, we return null.
370 */
Tony Mak240656f2015-12-04 11:36:22 +0000371 public PhoneAccountHandle getSimCallManager(UserHandle userHandle) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700372 // Get the default dialer in case it has a connection manager associated with it.
Hall Liu2b33df72016-12-06 18:15:39 -0800373 String dialerPackage = mDefaultDialerCache
374 .getDefaultDialerApplication(userHandle.getIdentifier());
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700375
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700376 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700377 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800378
Santos Cordon59c21a72015-06-11 10:11:21 -0700379 PhoneAccountHandle dialerSimCallManager = null;
380 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800381
Santos Cordon59c21a72015-06-11 10:11:21 -0700382 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
383 // loop through and look for any connection manager in the same package.
384 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
385 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
Tony Mak240656f2015-12-04 11:36:22 +0000386 true /* includeDisabledAccounts */, userHandle);
Santos Cordon59c21a72015-06-11 10:11:21 -0700387 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
388 ComponentName component = accountHandle.getComponentName();
389
390 // Store the system connection manager if found
391 if (systemSimCallManager == null
392 && Objects.equals(component, systemSimCallManagerComponent)
393 && !resolveComponent(accountHandle).isEmpty()) {
394 systemSimCallManager = accountHandle;
395
396 // Store the dialer connection manager if found
397 } else if (dialerSimCallManager == null
398 && Objects.equals(component.getPackageName(), dialerPackage)
399 && !resolveComponent(accountHandle).isEmpty()) {
400 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700401 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700402 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700403 }
404
Santos Cordon59c21a72015-06-11 10:11:21 -0700405 PhoneAccountHandle retval = dialerSimCallManager != null ?
406 dialerSimCallManager : systemSimCallManager;
407
408 Log.i(this, "SimCallManager queried, returning: %s", retval);
409
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700410 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700411 }
412
Evan Charlton105d9772014-11-25 14:08:53 -0800413 /**
Tony Mak240656f2015-12-04 11:36:22 +0000414 * If it is a outgoing call, sim call manager of call-initiating user is returned.
415 * Otherwise, we return the sim call manager of the user associated with the
416 * target phone account.
417 * @return phone account handle of sim call manager based on the ongoing call.
418 */
419 public PhoneAccountHandle getSimCallManagerFromCall(Call call) {
420 if (call == null) {
421 return null;
422 }
423 UserHandle userHandle = call.getInitiatingUser();
424 if (userHandle == null) {
425 userHandle = call.getTargetPhoneAccount().getUserHandle();
426 }
427 return getSimCallManager(userHandle);
428 }
429
430 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800431 * Update the current UserHandle to track when users are switched. This will allow the
432 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
433 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700434 * We cannot simply check the calling user because that would always return the primary user for
435 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800436 *
437 * @param userHandle The {@link UserHandle}, as delivered by
438 * {@link Intent#ACTION_USER_SWITCHED}.
439 */
440 public void setCurrentUserHandle(UserHandle userHandle) {
441 if (userHandle == null) {
442 Log.d(this, "setCurrentUserHandle, userHandle = null");
443 userHandle = Process.myUserHandle();
444 }
445 Log.d(this, "setCurrentUserHandle, %s", userHandle);
446 mCurrentUserHandle = userHandle;
447 }
448
Yorke Lee71734c22015-06-02 14:22:56 -0700449 /**
450 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
451 * otherwise.
452 */
453 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000454 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Hall Liu518a7dd2017-01-26 14:20:04 -0800455 Log.i(this, "Phone account %s %s.", accountHandle, isEnabled ? "enabled" : "disabled");
Santos Cordon638f05c2015-05-19 17:12:33 -0700456 if (account == null) {
457 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700458 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700459 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700460 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700461 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700462 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700463 }
464
Santos Cordon638f05c2015-05-19 17:12:33 -0700465 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700466 account.setIsEnabled(isEnabled);
Tony Mak48367162016-01-15 22:58:18 +0000467 if (!isEnabled) {
468 // If the disabled account is the default, remove it.
469 removeDefaultPhoneAccountHandle(accountHandle);
470 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700471 write();
472 fireAccountsChanged();
473 }
Yorke Lee71734c22015-06-02 14:22:56 -0700474 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700475 }
476
Tony Mak48367162016-01-15 22:58:18 +0000477 private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) {
478 Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator =
479 mState.defaultOutgoingAccountHandles.entrySet().iterator();
480 while (iterator.hasNext()) {
481 Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next();
482 if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) {
483 iterator.remove();
484 }
485 }
486 }
487
Tony Mak240656f2015-12-04 11:36:22 +0000488 private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle,
489 boolean acrossProfiles) {
Evan Charlton105d9772014-11-25 14:08:53 -0800490 if (account == null) {
491 return false;
492 }
493
Tony Mak240656f2015-12-04 11:36:22 +0000494 if (userHandle == null) {
495 Log.w(this, "userHandle is null in isVisibleForUser");
496 return false;
497 }
498
Evan Charlton105d9772014-11-25 14:08:53 -0800499 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
500 // all profiles. Only Telephony and SIP accounts should have this capability.
501 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
502 return true;
503 }
504
505 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
506 if (phoneAccountUserHandle == null) {
507 return false;
508 }
509
510 if (mCurrentUserHandle == null) {
Tony Mak240656f2015-12-04 11:36:22 +0000511 // In case we need to have emergency phone calls from the lock screen.
Evan Charlton105d9772014-11-25 14:08:53 -0800512 Log.d(this, "Current user is null; assuming true");
513 return true;
514 }
515
Tony Mak240656f2015-12-04 11:36:22 +0000516 if (acrossProfiles) {
517 return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(),
518 phoneAccountUserHandle.getIdentifier());
519 } else {
520 return phoneAccountUserHandle.equals(userHandle);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800521 }
Evan Charlton105d9772014-11-25 14:08:53 -0800522 }
523
524 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
525 return resolveComponent(phoneAccountHandle.getComponentName(),
Tony Mak240656f2015-12-04 11:36:22 +0000526 phoneAccountHandle.getUserHandle());
Evan Charlton105d9772014-11-25 14:08:53 -0800527 }
528
529 private List<ResolveInfo> resolveComponent(ComponentName componentName,
530 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800531 PackageManager pm = mContext.getPackageManager();
532 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
533 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800534 try {
535 if (userHandle != null) {
536 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
537 } else {
538 return pm.queryIntentServices(intent, 0);
539 }
540 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700541 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800542 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800543 }
mike dooley10a58312014-11-06 13:46:19 -0800544 }
545
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700546 /**
547 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700548 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700549 *
550 * @return The list of {@link PhoneAccountHandle}s.
551 */
Tony Mak240656f2015-12-04 11:36:22 +0000552 public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) {
553 return getPhoneAccountHandles(0, null, null, false, userHandle);
Ihab Awad293edf22014-07-24 17:52:29 -0700554 }
555
Tony Mak240656f2015-12-04 11:36:22 +0000556 public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) {
557 return getPhoneAccounts(0, null, null, false, userHandle);
558 }
559
560 public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() {
561 return getAllPhoneAccounts(mCurrentUserHandle);
Santos Cordonafe59e52014-08-22 16:48:43 -0700562 }
563
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700564 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700565 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700566 * specified URI scheme.
567 *
568 * @param uriScheme The URI scheme.
569 * @return The phone account handles.
570 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700571 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
Tony Mak240656f2015-12-04 11:36:22 +0000572 String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700573 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700574 PhoneAccount.CAPABILITY_CALL_PROVIDER,
575 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
Tony Mak240656f2015-12-04 11:36:22 +0000576 uriScheme, null, includeDisabledAccounts, userHandle);
577 }
578
Tyler Gunn66ab9ae2017-04-07 15:00:49 -0700579 /**
580 * Retrieves a list of all phone accounts which have
581 * {@link PhoneAccount#CAPABILITY_SELF_MANAGED}.
582 * <p>
583 * Returns only the {@link PhoneAccount}s which are enabled as self-managed accounts are
584 * automatically enabled by default (see {@link #registerPhoneAccount(PhoneAccount)}).
585 *
586 * @param userHandle User handle of phone account owner.
587 * @return The phone account handles.
588 */
589 public List<PhoneAccountHandle> getSelfManagedPhoneAccounts(UserHandle userHandle) {
590 return getPhoneAccountHandles(
591 PhoneAccount.CAPABILITY_SELF_MANAGED,
592 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /* excludedCapabilities */,
593 null /* uriScheme */, null /* packageName */, false /* includeDisabledAccounts */,
594 userHandle);
595 }
596
Tony Mak240656f2015-12-04 11:36:22 +0000597 public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser(
598 String uriScheme, boolean includeDisabledAccounts) {
599 return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700600 }
601
602 /**
603 * Retrieves a list of all the SIM-based phone accounts.
604 */
Tony Mak240656f2015-12-04 11:36:22 +0000605 public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700606 return getPhoneAccountHandles(
607 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Tony Mak240656f2015-12-04 11:36:22 +0000608 null, null, false, userHandle);
Tyler Gunn84253572014-09-02 14:50:05 -0700609 }
610
Tony Mak240656f2015-12-04 11:36:22 +0000611 public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() {
612 return getSimPhoneAccounts(mCurrentUserHandle);
613 }
614
615 /**
616 * Retrieves a list of all phone accounts registered by a specified package.
617 *
618 * @param packageName The name of the package that registered the phone accounts.
619 * @return The phone account handles.
620 */
621 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName,
622 UserHandle userHandle) {
623 return getPhoneAccountHandles(0, null, packageName, false, userHandle);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700624 }
625
Ihab Awad104f8062014-07-17 11:29:35 -0700626 // TODO: Should we implement an artificial limit for # of accounts associated with a single
627 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700628 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700629 // Enforce the requirement that a connection service for a phone account has the correct
630 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700631 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700632 Log.w(this,
633 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700634 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700635 throw new SecurityException("PhoneAccount connection service requires "
636 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700637 }
638
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700639 addOrReplacePhoneAccount(account);
640 }
641
642 /**
643 * Adds a {@code PhoneAccount}, replacing an existing one if found.
644 *
645 * @param account The {@code PhoneAccount} to add or replace.
646 */
647 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800648 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
649 account.getAccountHandle(), account);
650
Santos Cordonea5cb932015-05-07 16:28:38 -0700651 // Start _enabled_ property as false.
652 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
653 // source app provides or else an third party app could enable itself.
654 boolean isEnabled = false;
655
Tony Mak98e6bdc2015-11-25 21:54:05 +0000656 PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle());
Santos Cordon6a212642015-05-08 16:35:23 -0700657 if (oldAccount != null) {
658 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700659 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700660 Log.i(this, getAccountDiffString(account, oldAccount));
661 } else {
662 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700663 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700664
Tyler Gunn2b17f232017-03-08 08:51:00 -0800665 // When registering a self-managed PhoneAccount we enforce the rule that the label that the
666 // app uses is also its phone account label. Also ensure it does not attempt to declare
667 // itself as a sim acct, call manager or call provider.
668 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) {
669 // Turn off bits we don't want to be able to set (TelecomServiceImpl protects against
670 // this but we'll also prevent it from happening here, just to be safe).
671 int newCapabilities = account.getCapabilities() &
672 ~(PhoneAccount.CAPABILITY_CALL_PROVIDER |
673 PhoneAccount.CAPABILITY_CONNECTION_MANAGER |
674 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
675
676 // Ensure name is correct.
677 CharSequence newLabel = mAppLabelProxy.getAppLabel(
678 account.getAccountHandle().getComponentName().getPackageName());
679
680 account = account.toBuilder()
681 .setLabel(newLabel)
682 .setCapabilities(newCapabilities)
683 .build();
684 }
685
Santos Cordon6a212642015-05-08 16:35:23 -0700686 mState.accounts.add(account);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700687 // Set defaults and replace based on the group Id.
688 maybeReplaceOldAccount(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700689 // Reset enabled state to whatever the value was if the account was already registered,
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800690 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled,
691 // as are all self-managed phone accounts.
Santos Cordonea5cb932015-05-07 16:28:38 -0700692 account.setIsEnabled(
Tyler Gunnacb3bc82017-01-09 09:43:56 -0800693 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)
694 || account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED));
Santos Cordon176ae282014-07-14 02:02:14 -0700695
Ihab Awad293edf22014-07-24 17:52:29 -0700696 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700697 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700698 }
699
Evan Charlton89176372014-07-19 18:23:09 -0700700 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000701 PhoneAccount account = getPhoneAccountUnchecked(accountHandle);
Santos Cordon6a212642015-05-08 16:35:23 -0700702 if (account != null) {
703 if (mState.accounts.remove(account)) {
704 write();
705 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700706 }
707 }
Santos Cordon176ae282014-07-14 02:02:14 -0700708 }
709
Tyler Gunnd900ce62014-08-13 11:40:59 -0700710 /**
711 * Un-registers all phone accounts associated with a specified package.
712 *
713 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800714 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700715 */
Evan Charlton105d9772014-11-25 14:08:53 -0800716 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700717 boolean accountsRemoved = false;
718 Iterator<PhoneAccount> it = mState.accounts.iterator();
719 while (it.hasNext()) {
720 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800721 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
722 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
723 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700724 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530725 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700726 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700727 }
728 }
729
Tyler Gunnd900ce62014-08-13 11:40:59 -0700730 if (accountsRemoved) {
731 write();
732 fireAccountsChanged();
733 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700734 }
735
Nancy Chen140004a2014-10-15 15:48:38 -0700736 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700737 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700738 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700739 }
740
Ihab Awadb78b2762014-07-25 15:16:23 -0700741 public void addListener(Listener l) {
742 mListeners.add(l);
743 }
744
745 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700746 if (l != null) {
747 mListeners.remove(l);
748 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700749 }
750
751 private void fireAccountsChanged() {
752 for (Listener l : mListeners) {
753 l.onAccountsChanged(this);
754 }
755 }
756
757 private void fireDefaultOutgoingChanged() {
758 for (Listener l : mListeners) {
759 l.onDefaultOutgoingChanged(this);
760 }
761 }
762
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700763 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
764 if (account1 == null || account2 == null) {
765 return "Diff: " + account1 + ", " + account2;
766 }
767
768 StringBuffer sb = new StringBuffer();
769 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700770 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
771 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700772 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
773 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
774 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
775 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
776 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700777 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
778 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700779 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
780 account2.getSupportedUriSchemes());
781 sb.append("]");
782 return sb.toString();
783 }
784
785 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
786 if (!Objects.equals(obj1, obj2)) {
787 sb.append("(")
788 .append(attrName)
789 .append(": ")
790 .append(obj1)
791 .append(" -> ")
792 .append(obj2)
793 .append(")");
794 }
795 }
796
Brad Ebinger0c87b3c2016-06-10 17:23:02 -0700797 private void maybeReplaceOldAccount(PhoneAccount newAccount) {
798 UserHandle newAccountUserHandle = newAccount.getAccountHandle().getUserHandle();
799 DefaultPhoneAccountHandle defaultHandle =
800 getUserSelectedDefaultPhoneAccount(newAccountUserHandle);
801 if (defaultHandle == null || defaultHandle.groupId.isEmpty()) {
802 Log.v(this, "maybeReplaceOldAccount: Not replacing PhoneAccount, no group Id or " +
803 "default.");
804 return;
805 }
806 if (!defaultHandle.groupId.equals(newAccount.getGroupId())) {
807 Log.v(this, "maybeReplaceOldAccount: group Ids are not equal.");
808 return;
809 }
810 if (Objects.equals(newAccount.getAccountHandle().getComponentName(),
811 defaultHandle.phoneAccountHandle.getComponentName())) {
812 // Move default calling account over to new user, since the ComponentNames and Group Ids
813 // are the same.
814 setUserSelectedOutgoingPhoneAccount(newAccount.getAccountHandle(),
815 newAccountUserHandle);
816 } else {
817 Log.v(this, "maybeReplaceOldAccount: group Ids are equal, but ComponentName is not" +
818 " the same as the default. Not replacing default PhoneAccount.");
819 }
820 PhoneAccount replacementAccount = getPhoneAccountByGroupId(newAccount.getGroupId(),
821 newAccount.getAccountHandle().getComponentName(), newAccountUserHandle,
822 newAccount.getAccountHandle());
823 if (replacementAccount != null) {
824 // Unregister the old PhoneAccount.
825 Log.v(this, "maybeReplaceOldAccount: Unregistering old PhoneAccount: " +
826 replacementAccount.getAccountHandle());
827 unregisterPhoneAccount(replacementAccount.getAccountHandle());
828 }
829 }
830
Tyler Gunncb59b672014-08-20 09:02:11 -0700831 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700832 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700833 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700834 *
835 * @param phoneAccountHandle The phone account to check.
836 * @return {@code True} if the phone account has permission.
837 */
Santos Cordon6a212642015-05-08 16:35:23 -0700838 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800839 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
840 if (resolveInfos.isEmpty()) {
841 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700842 return false;
843 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800844 for (ResolveInfo resolveInfo : resolveInfos) {
845 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700846 if (serviceInfo == null) {
847 return false;
848 }
849
850 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
851 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
852 serviceInfo.permission)) {
853 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
854 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
855 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800856 return false;
857 }
858 }
859 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700860 }
861
Santos Cordon6a212642015-05-08 16:35:23 -0700862 //
863 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
864 //
Ihab Awad293edf22014-07-24 17:52:29 -0700865
Santos Cordonafe59e52014-08-22 16:48:43 -0700866 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700867 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700868 *
Santos Cordon6a212642015-05-08 16:35:23 -0700869 * @param handle
870 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700871 */
Tony Mak98e6bdc2015-11-25 21:54:05 +0000872 public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700873 for (PhoneAccount m : mState.accounts) {
874 if (Objects.equals(handle, m.getAccountHandle())) {
875 return m;
876 }
877 }
878 return null;
879 }
880
881 /**
882 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
883 * account before returning it. The current user is the active user on the actual android
884 * device.
885 */
Tony Mak240656f2015-12-04 11:36:22 +0000886 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) {
887 return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false);
888 }
889
890 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle,
891 UserHandle userHandle, boolean acrossProfiles) {
Tony Mak98e6bdc2015-11-25 21:54:05 +0000892 PhoneAccount account = getPhoneAccountUnchecked(handle);
Tony Mak240656f2015-12-04 11:36:22 +0000893 if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) {
Santos Cordon6a212642015-05-08 16:35:23 -0700894 return account;
895 }
896 return null;
897 }
898
Tony Mak240656f2015-12-04 11:36:22 +0000899 public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) {
900 return getPhoneAccount(handle, mCurrentUserHandle);
901 }
902
Bryce Leea0bb7052015-10-16 13:31:40 -0700903 private List<PhoneAccountHandle> getPhoneAccountHandles(
904 int capabilities,
905 String uriScheme,
906 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000907 boolean includeDisabledAccounts,
908 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700909 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
Tony Mak240656f2015-12-04 11:36:22 +0000910 packageName, includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700911 }
912
Santos Cordon6a212642015-05-08 16:35:23 -0700913 /**
914 * Returns a list of phone account handles with the specified capabilities, uri scheme,
915 * and package name.
916 */
917 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700918 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700919 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700920 String uriScheme,
921 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000922 boolean includeDisabledAccounts,
923 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700924 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700925
926 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700927 capabilities, excludedCapabilities, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000928 includeDisabledAccounts, userHandle)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700929 handles.add(account.getAccountHandle());
930 }
931 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700932 }
933
Bryce Leea0bb7052015-10-16 13:31:40 -0700934 private List<PhoneAccount> getPhoneAccounts(
935 int capabilities,
936 String uriScheme,
937 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000938 boolean includeDisabledAccounts,
939 UserHandle userHandle) {
Bryce Leea0bb7052015-10-16 13:31:40 -0700940 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000941 includeDisabledAccounts, userHandle);
Bryce Leea0bb7052015-10-16 13:31:40 -0700942 }
943
Tyler Gunn84253572014-09-02 14:50:05 -0700944 /**
945 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700946 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700947 *
Santos Cordon6a212642015-05-08 16:35:23 -0700948 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700949 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
950 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700951 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700952 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700953 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700954 */
Santos Cordon6a212642015-05-08 16:35:23 -0700955 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700956 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700957 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700958 String uriScheme,
959 String packageName,
Tony Mak240656f2015-12-04 11:36:22 +0000960 boolean includeDisabledAccounts,
961 UserHandle userHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700962 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700963 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700964 if (!(m.isEnabled() || includeDisabledAccounts)) {
965 // Do not include disabled accounts.
966 continue;
967 }
968
Bryce Leea0bb7052015-10-16 13:31:40 -0700969 if ((m.getCapabilities() & excludedCapabilities) != 0) {
970 // If an excluded capability is present, skip.
971 continue;
972 }
973
Santos Cordon6a212642015-05-08 16:35:23 -0700974 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800975 // Account doesn't have the right capabilities; skip this one.
976 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700977 }
Evan Charlton105d9772014-11-25 14:08:53 -0800978 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
979 // Account doesn't support this URI scheme; skip this one.
980 continue;
981 }
Santos Cordon6a212642015-05-08 16:35:23 -0700982 PhoneAccountHandle handle = m.getAccountHandle();
983
984 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800985 // This component cannot be resolved anymore; skip this one.
986 continue;
987 }
Santos Cordon6a212642015-05-08 16:35:23 -0700988 if (packageName != null &&
989 !packageName.equals(handle.getComponentName().getPackageName())) {
990 // Not the right package name; skip this one.
991 continue;
992 }
Tony Mak240656f2015-12-04 11:36:22 +0000993 if (!isVisibleForUser(m, userHandle, false)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800994 // Account is not visible for the current user; skip this one.
995 continue;
996 }
Santos Cordon6a212642015-05-08 16:35:23 -0700997 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700998 }
Santos Cordon6a212642015-05-08 16:35:23 -0700999 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -07001000 }
1001
Santos Cordon6a212642015-05-08 16:35:23 -07001002 //
1003 // State Implementation for PhoneAccountRegistrar
1004 //
1005
Ihab Awad293edf22014-07-24 17:52:29 -07001006 /**
1007 * The state of this {@code PhoneAccountRegistrar}.
1008 */
Ihab Awadb78b2762014-07-25 15:16:23 -07001009 @VisibleForTesting
1010 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -07001011 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001012 * Store the default phone account handle of users. If no record of a user can be found in
1013 * the map, it means that no default phone account handle is set in that user.
Ihab Awad293edf22014-07-24 17:52:29 -07001014 */
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001015 public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles
1016 = new ConcurrentHashMap<>();
Ihab Awad293edf22014-07-24 17:52:29 -07001017
1018 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -07001019 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -07001020 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +05301021 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -07001022
1023 /**
1024 * The version number of the State data.
1025 */
1026 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -07001027 }
1028
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001029 /**
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001030 * The default {@link PhoneAccountHandle} of a user.
1031 */
1032 public static class DefaultPhoneAccountHandle {
1033
1034 public final UserHandle userHandle;
1035
1036 public final PhoneAccountHandle phoneAccountHandle;
1037
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001038 public final String groupId;
1039
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001040 public DefaultPhoneAccountHandle(UserHandle userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001041 PhoneAccountHandle phoneAccountHandle, String groupId) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001042 this.userHandle = userHandle;
1043 this.phoneAccountHandle = phoneAccountHandle;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001044 this.groupId = groupId;
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001045 }
1046 }
1047
1048 /**
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001049 * Dumps the state of the {@link CallsManager}.
1050 *
1051 * @param pw The {@code IndentingPrintWriter} to write the state to.
1052 */
1053 public void dump(IndentingPrintWriter pw) {
1054 if (mState != null) {
1055 pw.println("xmlVersion: " + mState.versionNumber);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001056 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1057 = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle());
1058 pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" :
1059 defaultPhoneAccountHandle.phoneAccountHandle));
Tony Mak240656f2015-12-04 11:36:22 +00001060 pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle));
Tyler Gunn9787e0e2014-10-14 14:36:12 -07001061 pw.println("phoneAccounts:");
1062 pw.increaseIndent();
1063 for (PhoneAccount phoneAccount : mState.accounts) {
1064 pw.println(phoneAccount);
1065 }
1066 pw.decreaseIndent();
1067 }
1068 }
1069
Ihab Awad293edf22014-07-24 17:52:29 -07001070 ////////////////////////////////////////////////////////////////////////////////////////////////
1071 //
1072 // State management
1073 //
1074
Hall Liu3f7f64d2016-01-07 17:40:13 -08001075 private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> {
1076 @Override
1077 public Void doInBackground(ByteArrayOutputStream... args) {
1078 final ByteArrayOutputStream buffer = args[0];
1079 FileOutputStream fileOutput = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001080 try {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001081 synchronized (mWriteLock) {
1082 fileOutput = mAtomicFile.startWrite();
1083 buffer.writeTo(fileOutput);
1084 mAtomicFile.finishWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001085 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001086 } catch (IOException e) {
1087 Log.e(this, e, "Writing state to XML file");
1088 mAtomicFile.failWrite(fileOutput);
Ihab Awadb78b2762014-07-25 15:16:23 -07001089 }
Hall Liu3f7f64d2016-01-07 17:40:13 -08001090 return null;
1091 }
1092 }
1093
1094 private void write() {
1095 try {
1096 ByteArrayOutputStream os = new ByteArrayOutputStream();
1097 XmlSerializer serializer = new FastXmlSerializer();
1098 serializer.setOutput(os, "utf-8");
1099 writeToXml(mState, serializer, mContext);
1100 serializer.flush();
1101 new AsyncXmlWriter().execute(os);
Ihab Awadb78b2762014-07-25 15:16:23 -07001102 } catch (IOException e) {
Hall Liu3f7f64d2016-01-07 17:40:13 -08001103 Log.e(this, e, "Writing state to XML buffer");
Ihab Awad104f8062014-07-17 11:29:35 -07001104 }
1105 }
1106
Ihab Awadb78b2762014-07-25 15:16:23 -07001107 private void read() {
1108 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -07001109 try {
Ihab Awadb78b2762014-07-25 15:16:23 -07001110 is = mAtomicFile.openRead();
1111 } catch (FileNotFoundException ex) {
1112 return;
1113 }
1114
Tyler Gunn84253572014-09-02 14:50:05 -07001115 boolean versionChanged = false;
1116
Ihab Awadb78b2762014-07-25 15:16:23 -07001117 XmlPullParser parser;
1118 try {
1119 parser = Xml.newPullParser();
1120 parser.setInput(new BufferedInputStream(is), null);
1121 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001122 mState = readFromXml(parser, mContext);
1123 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
1124
Ihab Awadb78b2762014-07-25 15:16:23 -07001125 } catch (IOException | XmlPullParserException e) {
1126 Log.e(this, e, "Reading state from XML file");
1127 mState = new State();
1128 } finally {
1129 try {
1130 is.close();
1131 } catch (IOException e) {
1132 Log.e(this, e, "Closing InputStream");
1133 }
Ihab Awad104f8062014-07-17 11:29:35 -07001134 }
Tyler Gunn84253572014-09-02 14:50:05 -07001135
Evan Charlton105d9772014-11-25 14:08:53 -08001136 // Verify all of the UserHandles.
1137 List<PhoneAccount> badAccounts = new ArrayList<>();
1138 for (PhoneAccount phoneAccount : mState.accounts) {
1139 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
1140 if (userHandle == null) {
1141 Log.w(this, "Missing UserHandle for %s", phoneAccount);
1142 badAccounts.add(phoneAccount);
1143 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
1144 Log.w(this, "User does not exist for %s", phoneAccount);
1145 badAccounts.add(phoneAccount);
1146 }
1147 }
1148 mState.accounts.removeAll(badAccounts);
1149
Tyler Gunn84253572014-09-02 14:50:05 -07001150 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -08001151 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -07001152 write();
1153 }
Santos Cordon176ae282014-07-14 02:02:14 -07001154 }
1155
Evan Charlton105d9772014-11-25 14:08:53 -08001156 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001157 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -08001158 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -07001159 }
Ihab Awad104f8062014-07-17 11:29:35 -07001160
Tyler Gunn84253572014-09-02 14:50:05 -07001161 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001162 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -07001163 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001164 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -07001165 }
1166
Ihab Awad293edf22014-07-24 17:52:29 -07001167 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -07001168 //
Ihab Awadb78b2762014-07-25 15:16:23 -07001169 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -07001170 //
1171
Ihab Awadb78b2762014-07-25 15:16:23 -07001172 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -07001173 public abstract static class XmlSerialization<T> {
Tyler Gunn369c8742015-10-07 15:39:28 -07001174 private static final String TAG_VALUE = "value";
1175 private static final String ATTRIBUTE_LENGTH = "length";
1176 private static final String ATTRIBUTE_KEY = "key";
1177 private static final String ATTRIBUTE_VALUE_TYPE = "type";
1178 private static final String VALUE_TYPE_STRING = "string";
1179 private static final String VALUE_TYPE_INTEGER = "integer";
1180 private static final String VALUE_TYPE_BOOLEAN = "boolean";
Tyler Gunn84253572014-09-02 14:50:05 -07001181
Ihab Awadb78b2762014-07-25 15:16:23 -07001182 /**
1183 * Write the supplied object to XML
1184 */
Evan Charlton105d9772014-11-25 14:08:53 -08001185 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001186 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -07001187
1188 /**
1189 * Read from the supplied XML into a new object, returning null in case of an
1190 * unrecoverable schema mismatch or other data error. 'parser' must be already
1191 * positioned at the first tag that is expected to have been emitted by this
1192 * object's writeToXml(). This object tries to fail early without modifying
1193 * 'parser' if it does not recognize the data it sees.
1194 */
Tyler Gunn84253572014-09-02 14:50:05 -07001195 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -07001196 throws IOException, XmlPullParserException;
1197
Ihab Awadd9f54382014-10-24 11:44:47 -07001198 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -07001199 throws IOException {
1200 if (value != null) {
1201 serializer.startTag(null, tagName);
1202 serializer.text(Objects.toString(value));
1203 serializer.endTag(null, tagName);
1204 }
1205 }
Tyler Gunn84253572014-09-02 14:50:05 -07001206
1207 /**
1208 * Serializes a string array.
1209 *
1210 * @param tagName The tag name for the string array.
1211 * @param values The string values to serialize.
1212 * @param serializer The serializer.
1213 * @throws IOException
1214 */
1215 protected void writeStringList(String tagName, List<String> values,
1216 XmlSerializer serializer)
1217 throws IOException {
1218
1219 serializer.startTag(null, tagName);
1220 if (values != null) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001221 serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size()));
Tyler Gunn84253572014-09-02 14:50:05 -07001222 for (String toSerialize : values) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001223 serializer.startTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001224 if (toSerialize != null ){
1225 serializer.text(toSerialize);
1226 }
Tyler Gunn369c8742015-10-07 15:39:28 -07001227 serializer.endTag(null, TAG_VALUE);
Tyler Gunn84253572014-09-02 14:50:05 -07001228 }
1229 } else {
Tyler Gunn369c8742015-10-07 15:39:28 -07001230 serializer.attribute(null, ATTRIBUTE_LENGTH, "0");
1231 }
1232 serializer.endTag(null, tagName);
1233 }
1234
1235 protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer)
1236 throws IOException {
1237
1238 serializer.startTag(null, tagName);
1239 if (values != null) {
1240 for (String key : values.keySet()) {
1241 Object value = values.get(key);
1242
1243 if (value == null) {
1244 continue;
1245 }
1246
1247 String valueType;
1248 if (value instanceof String) {
1249 valueType = VALUE_TYPE_STRING;
1250 } else if (value instanceof Integer) {
1251 valueType = VALUE_TYPE_INTEGER;
1252 } else if (value instanceof Boolean) {
1253 valueType = VALUE_TYPE_BOOLEAN;
1254 } else {
1255 Log.w(this,
1256 "PhoneAccounts support only string, integer and boolean extras TY.");
1257 continue;
1258 }
1259
1260 serializer.startTag(null, TAG_VALUE);
1261 serializer.attribute(null, ATTRIBUTE_KEY, key);
1262 serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType);
1263 serializer.text(Objects.toString(value));
1264 serializer.endTag(null, TAG_VALUE);
1265 }
Tyler Gunn84253572014-09-02 14:50:05 -07001266 }
1267 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001268 }
Tyler Gunn84253572014-09-02 14:50:05 -07001269
Santos Cordon9c30c282015-05-13 16:28:27 -07001270 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001271 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001272 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001273 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001274 value.writeToStream(stream);
1275 byte[] iconByteArray = stream.toByteArray();
1276 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001277
1278 serializer.startTag(null, tagName);
1279 serializer.text(text);
1280 serializer.endTag(null, tagName);
1281 }
Tyler Gunn84253572014-09-02 14:50:05 -07001282 }
1283
Evan Charlton105d9772014-11-25 14:08:53 -08001284 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1285 throws IOException {
1286 serializer.startTag(null, tagName);
1287 serializer.text(Long.valueOf(value).toString());
1288 serializer.endTag(null, tagName);
1289 }
1290
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001291 protected void writeNonNullString(String tagName, String value, XmlSerializer serializer)
1292 throws IOException {
1293 serializer.startTag(null, tagName);
1294 serializer.text(value != null ? value : "");
1295 serializer.endTag(null, tagName);
1296 }
1297
Tyler Gunn84253572014-09-02 14:50:05 -07001298 /**
1299 * Reads a string array from the XML parser.
1300 *
1301 * @param parser The XML parser.
1302 * @return String array containing the parsed values.
1303 * @throws IOException Exception related to IO.
1304 * @throws XmlPullParserException Exception related to parsing.
1305 */
1306 protected List<String> readStringList(XmlPullParser parser)
1307 throws IOException, XmlPullParserException {
1308
Tyler Gunn369c8742015-10-07 15:39:28 -07001309 int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH));
Tyler Gunn84253572014-09-02 14:50:05 -07001310 List<String> arrayEntries = new ArrayList<String>(length);
1311 String value = null;
1312
1313 if (length == 0) {
1314 return arrayEntries;
1315 }
1316
1317 int outerDepth = parser.getDepth();
1318 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001319 if (parser.getName().equals(TAG_VALUE)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001320 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001321 value = parser.getText();
1322 arrayEntries.add(value);
1323 }
1324 }
1325
1326 return arrayEntries;
1327 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001328
Tyler Gunn369c8742015-10-07 15:39:28 -07001329 /**
1330 * Reads a bundle from the XML parser.
1331 *
1332 * @param parser The XML parser.
1333 * @return Bundle containing the parsed values.
1334 * @throws IOException Exception related to IO.
1335 * @throws XmlPullParserException Exception related to parsing.
1336 */
1337 protected Bundle readBundle(XmlPullParser parser)
1338 throws IOException, XmlPullParserException {
1339
1340 Bundle bundle = null;
1341 int outerDepth = parser.getDepth();
1342 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1343 if (parser.getName().equals(TAG_VALUE)) {
1344 String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE);
1345 String key = parser.getAttributeValue(null, ATTRIBUTE_KEY);
1346 parser.next();
1347 String value = parser.getText();
1348
1349 if (bundle == null) {
1350 bundle = new Bundle();
1351 }
1352
1353 // Do not write null values to the bundle.
1354 if (value == null) {
1355 continue;
1356 }
1357
1358 if (VALUE_TYPE_STRING.equals(valueType)) {
1359 bundle.putString(key, value);
1360 } else if (VALUE_TYPE_INTEGER.equals(valueType)) {
1361 try {
1362 int intValue = Integer.parseInt(value);
1363 bundle.putInt(key, intValue);
1364 } catch (NumberFormatException nfe) {
1365 Log.w(this, "Invalid integer PhoneAccount extra.");
1366 }
1367 } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) {
1368 boolean boolValue = Boolean.parseBoolean(value);
1369 bundle.putBoolean(key, boolValue);
1370 } else {
1371 Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle.");
1372 }
1373 }
1374 }
1375 return bundle;
1376 }
1377
Santos Cordon9c30c282015-05-13 16:28:27 -07001378 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001379 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1380 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1381 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001382
1383 protected Icon readIcon(XmlPullParser parser) throws IOException {
1384 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1385 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1386 return Icon.createFromStream(stream);
1387 }
Ihab Awad104f8062014-07-17 11:29:35 -07001388 }
1389
Ihab Awadb78b2762014-07-25 15:16:23 -07001390 @VisibleForTesting
1391 public static final XmlSerialization<State> sStateXml =
1392 new XmlSerialization<State>() {
1393 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001394 private static final String DEFAULT_OUTGOING = "default_outgoing";
1395 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001396 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001397
1398 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001399 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001400 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001401 if (o != null) {
1402 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001403 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001404
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001405 serializer.startTag(null, DEFAULT_OUTGOING);
1406 for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o
1407 .defaultOutgoingAccountHandles.values()) {
1408 sDefaultPhoneAcountHandleXml
1409 .writeToXml(defaultPhoneAccountHandle, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001410 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001411 serializer.endTag(null, DEFAULT_OUTGOING);
Ihab Awad26923222014-07-30 10:54:35 -07001412
Ihab Awad26923222014-07-30 10:54:35 -07001413 serializer.startTag(null, ACCOUNTS);
1414 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001415 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001416 }
1417 serializer.endTag(null, ACCOUNTS);
1418
1419 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001420 }
Ihab Awad104f8062014-07-17 11:29:35 -07001421 }
1422
1423 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001424 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001425 throws IOException, XmlPullParserException {
1426 if (parser.getName().equals(CLASS_STATE)) {
1427 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001428
1429 String rawVersion = parser.getAttributeValue(null, VERSION);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001430 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion);
Tyler Gunn84253572014-09-02 14:50:05 -07001431
Ihab Awadb78b2762014-07-25 15:16:23 -07001432 int outerDepth = parser.getDepth();
1433 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1434 if (parser.getName().equals(DEFAULT_OUTGOING)) {
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001435 if (s.versionNumber < 9) {
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001436 // Migrate old default phone account handle here by assuming the
1437 // default phone account handle belongs to the primary user. Also,
1438 // assume there are no groups.
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001439 parser.nextTag();
1440 PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml
1441 .readFromXml(parser, s.versionNumber, context);
1442 UserManager userManager = UserManager.get(context);
1443 UserInfo primaryUser = userManager.getPrimaryUser();
1444 if (primaryUser != null) {
1445 UserHandle userHandle = primaryUser.getUserHandle();
1446 DefaultPhoneAccountHandle defaultPhoneAccountHandle
1447 = new DefaultPhoneAccountHandle(userHandle,
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001448 phoneAccountHandle, "" /* groupId */);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001449 s.defaultOutgoingAccountHandles
1450 .put(userHandle, defaultPhoneAccountHandle);
1451 }
1452 } else {
1453 int defaultAccountHandlesDepth = parser.getDepth();
1454 while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) {
1455 DefaultPhoneAccountHandle accountHandle
1456 = sDefaultPhoneAcountHandleXml
1457 .readFromXml(parser, s.versionNumber, context);
1458 if (accountHandle != null && s.accounts != null) {
1459 s.defaultOutgoingAccountHandles
1460 .put(accountHandle.userHandle, accountHandle);
1461 }
1462 }
1463 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001464 } else if (parser.getName().equals(ACCOUNTS)) {
1465 int accountsDepth = parser.getDepth();
1466 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001467 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1468 s.versionNumber, context);
1469
1470 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001471 s.accounts.add(account);
1472 }
1473 }
Ihab Awad104f8062014-07-17 11:29:35 -07001474 }
1475 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001476 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001477 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001478 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001479 }
1480 };
1481
Ihab Awadb78b2762014-07-25 15:16:23 -07001482 @VisibleForTesting
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001483 public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml =
1484 new XmlSerialization<DefaultPhoneAccountHandle>() {
1485 private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE
1486 = "default_outgoing_phone_account_handle";
1487 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001488 private static final String GROUP_ID = "group_id";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001489 private static final String ACCOUNT_HANDLE = "account_handle";
1490
1491 @Override
1492 public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer,
1493 Context context) throws IOException {
1494 if (o != null) {
1495 final UserManager userManager = UserManager.get(context);
1496 final long serialNumber = userManager.getSerialNumberForUser(o.userHandle);
1497 if (serialNumber != -1) {
1498 serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1499 writeLong(USER_SERIAL_NUMBER, serialNumber, serializer);
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001500 writeNonNullString(GROUP_ID, o.groupId, serializer);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001501 serializer.startTag(null, ACCOUNT_HANDLE);
1502 sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer,
1503 context);
1504 serializer.endTag(null, ACCOUNT_HANDLE);
1505 serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE);
1506 }
1507 }
1508 }
1509
1510 @Override
1511 public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version,
1512 Context context)
1513 throws IOException, XmlPullParserException {
1514 if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) {
1515 int outerDepth = parser.getDepth();
1516 PhoneAccountHandle accountHandle = null;
1517 String userSerialNumberString = null;
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001518 String groupId = "";
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001519 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1520 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1521 parser.nextTag();
1522 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1523 context);
1524 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1525 parser.next();
1526 userSerialNumberString = parser.getText();
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001527 } else if (parser.getName().equals(GROUP_ID)) {
Hall Liuded880c2017-01-20 16:13:07 -08001528 if (parser.next() == XmlPullParser.TEXT) {
1529 groupId = parser.getText();
1530 }
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001531 }
1532 }
1533 UserHandle userHandle = null;
1534 if (userSerialNumberString != null) {
1535 try {
1536 long serialNumber = Long.parseLong(userSerialNumberString);
1537 userHandle = UserManager.get(context)
1538 .getUserForSerialNumber(serialNumber);
1539 } catch (NumberFormatException e) {
1540 Log.e(this, e,
1541 "Could not parse UserHandle " + userSerialNumberString);
1542 }
1543 }
Brad Ebinger0c87b3c2016-06-10 17:23:02 -07001544 if (accountHandle != null && userHandle != null && groupId != null) {
1545 return new DefaultPhoneAccountHandle(userHandle, accountHandle,
1546 groupId);
Tony Mak4a3e2fd2015-12-04 11:58:38 +00001547 }
1548 }
1549 return null;
1550 }
1551 };
1552
1553
1554 @VisibleForTesting
Ihab Awadb78b2762014-07-25 15:16:23 -07001555 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1556 new XmlSerialization<PhoneAccount>() {
1557 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1558 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001559 private static final String ADDRESS = "handle";
1560 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001561 private static final String CAPABILITIES = "capabilities";
Christine Hallstromffe558c2016-11-30 16:05:13 -08001562 private static final String SUPPORTED_AUDIO_ROUTES = "supported_audio_routes";
Ihab Awad104f8062014-07-17 11:29:35 -07001563 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001564 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1565 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001566 private static final String ICON_TINT = "icon_tint";
1567 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001568 private static final String LABEL = "label";
1569 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001570 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001571 private static final String ICON = "icon";
Tyler Gunn369c8742015-10-07 15:39:28 -07001572 private static final String EXTRAS = "extras";
Santos Cordonea5cb932015-05-07 16:28:38 -07001573 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001574
1575 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001576 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001577 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001578 if (o != null) {
1579 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001580
Ihab Awad26923222014-07-30 10:54:35 -07001581 if (o.getAccountHandle() != null) {
1582 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001583 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001584 serializer.endTag(null, ACCOUNT_HANDLE);
1585 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001586
Ihab Awadd9f54382014-10-24 11:44:47 -07001587 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1588 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1589 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001590 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001591 writeTextIfNonNull(HIGHLIGHT_COLOR,
1592 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001593 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1594 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001595 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Tyler Gunn369c8742015-10-07 15:39:28 -07001596 writeBundle(EXTRAS, o.getExtras(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001597 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001598 writeTextIfNonNull(SUPPORTED_AUDIO_ROUTES, Integer.toString(
1599 o.getSupportedAudioRoutes()), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001600
Ihab Awad26923222014-07-30 10:54:35 -07001601 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1602 }
Ihab Awad104f8062014-07-17 11:29:35 -07001603 }
1604
Tyler Gunn84253572014-09-02 14:50:05 -07001605 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001606 throws IOException, XmlPullParserException {
1607 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1608 int outerDepth = parser.getDepth();
1609 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001610 Uri address = null;
1611 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001612 int capabilities = 0;
Christine Hallstromffe558c2016-11-30 16:05:13 -08001613 int supportedAudioRoutes = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001614 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001615 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001616 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001617 int iconTint = PhoneAccount.NO_ICON_TINT;
1618 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001619 String label = null;
1620 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001621 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001622 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001623 boolean enabled = false;
Tyler Gunn369c8742015-10-07 15:39:28 -07001624 Bundle extras = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001625
1626 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1627 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1628 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001629 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1630 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001631 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001632 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001633 address = Uri.parse(parser.getText());
1634 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001635 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001636 String nextText = parser.getText();
1637 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001638 } else if (parser.getName().equals(CAPABILITIES)) {
1639 parser.next();
1640 capabilities = Integer.parseInt(parser.getText());
1641 } else if (parser.getName().equals(ICON_RES_ID)) {
1642 parser.next();
1643 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001644 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1645 parser.next();
1646 iconPackageName = parser.getText();
1647 } else if (parser.getName().equals(ICON_BITMAP)) {
1648 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001649 iconBitmap = readBitmap(parser);
1650 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001651 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001652 iconTint = Integer.parseInt(parser.getText());
1653 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1654 parser.next();
1655 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001656 } else if (parser.getName().equals(LABEL)) {
1657 parser.next();
1658 label = parser.getText();
1659 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1660 parser.next();
1661 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001662 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1663 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001664 } else if (parser.getName().equals(ICON)) {
1665 parser.next();
1666 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001667 } else if (parser.getName().equals(ENABLED)) {
1668 parser.next();
1669 enabled = "true".equalsIgnoreCase(parser.getText());
Tyler Gunn369c8742015-10-07 15:39:28 -07001670 } else if (parser.getName().equals(EXTRAS)) {
Tyler Gunn369c8742015-10-07 15:39:28 -07001671 extras = readBundle(parser);
Christine Hallstromffe558c2016-11-30 16:05:13 -08001672 } else if (parser.getName().equals(SUPPORTED_AUDIO_ROUTES)) {
1673 parser.next();
1674 supportedAudioRoutes = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001675 }
1676 }
Tyler Gunn84253572014-09-02 14:50:05 -07001677
Santos Cordona9eebe42015-06-11 14:07:44 -07001678 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1679 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001680 ComponentName sipComponentName = new ComponentName("com.android.phone",
1681 "com.android.services.telephony.sip.SipConnectionService");
1682
Tyler Gunn84253572014-09-02 14:50:05 -07001683 // Upgrade older phone accounts to specify the supported URI schemes.
1684 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001685 supportedUriSchemes = new ArrayList<>();
1686
1687 // Handle the SIP connection service.
1688 // Check the system settings to see if it also should handle "tel" calls.
1689 if (accountHandle.getComponentName().equals(sipComponentName)) {
1690 boolean useSipForPstn = useSipForPstnCalls(context);
1691 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1692 if (useSipForPstn) {
1693 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1694 }
1695 } else {
1696 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1697 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1698 }
1699 }
1700
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001701 // Upgrade older phone accounts with explicit package name
1702 if (version < 5) {
1703 if (iconBitmap == null) {
1704 iconPackageName = accountHandle.getComponentName().getPackageName();
1705 }
1706 }
1707
Santos Cordona82aed82015-05-26 10:43:56 -07001708 if (version < 6) {
1709 // Always enable all SIP accounts on upgrade to version 6
1710 if (accountHandle.getComponentName().equals(sipComponentName)) {
1711 enabled = true;
1712 }
1713 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001714 if (version < 7) {
1715 // Always enabled all PSTN acocunts on upgrade to version 7
1716 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1717 enabled = true;
1718 }
1719 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001720 if (version < 8) {
1721 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1722 if (accountHandle.getComponentName().equals(sipComponentName)) {
1723 Uri accountUri = Uri.parse(accountHandle.getId());
1724 if (accountUri.getScheme() != null &&
1725 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1726 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1727 accountUri.getSchemeSpecificPart(),
1728 accountHandle.getUserHandle());
1729 }
1730 }
1731 }
Santos Cordona82aed82015-05-26 10:43:56 -07001732
Christine Hallstromffe558c2016-11-30 16:05:13 -08001733 if (version < 9) {
1734 // Set supported audio routes to all by default
1735 supportedAudioRoutes = CallAudioState.ROUTE_ALL;
1736 }
1737
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001738 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001739 .setAddress(address)
1740 .setSubscriptionAddress(subscriptionAddress)
1741 .setCapabilities(capabilities)
Christine Hallstromffe558c2016-11-30 16:05:13 -08001742 .setSupportedAudioRoutes(supportedAudioRoutes)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001743 .setShortDescription(shortDescription)
1744 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001745 .setHighlightColor(highlightColor)
Tyler Gunn369c8742015-10-07 15:39:28 -07001746 .setExtras(extras)
Santos Cordonea5cb932015-05-07 16:28:38 -07001747 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001748
Santos Cordon9c30c282015-05-13 16:28:27 -07001749 if (icon != null) {
1750 builder.setIcon(icon);
1751 } else if (iconBitmap != null) {
1752 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1753 } else if (!TextUtils.isEmpty(iconPackageName)) {
1754 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1755 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001756 }
1757
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001758 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001759 }
1760 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001761 }
Tyler Gunn84253572014-09-02 14:50:05 -07001762
1763 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001764 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1765 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001766 *
1767 * @param context The context.
1768 * @return {@code True} if SIP should be used for all calls.
1769 */
1770 private boolean useSipForPstnCalls(Context context) {
1771 String option = Settings.System.getString(context.getContentResolver(),
1772 Settings.System.SIP_CALL_OPTIONS);
1773 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1774 return option.equals(Settings.System.SIP_ALWAYS);
1775 }
Ihab Awad104f8062014-07-17 11:29:35 -07001776 };
1777
Ihab Awadb78b2762014-07-25 15:16:23 -07001778 @VisibleForTesting
1779 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1780 new XmlSerialization<PhoneAccountHandle>() {
1781 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001782 private static final String COMPONENT_NAME = "component_name";
1783 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001784 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001785
1786 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001787 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001788 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001789 if (o != null) {
1790 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001791
Ihab Awad26923222014-07-30 10:54:35 -07001792 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001793 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001794 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1795 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001796
Ihab Awadd9f54382014-10-24 11:44:47 -07001797 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001798
Evan Charlton105d9772014-11-25 14:08:53 -08001799 if (o.getUserHandle() != null && context != null) {
1800 UserManager userManager = UserManager.get(context);
1801 writeLong(USER_SERIAL_NUMBER,
1802 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1803 }
1804
Ihab Awad26923222014-07-30 10:54:35 -07001805 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1806 }
Ihab Awad104f8062014-07-17 11:29:35 -07001807 }
1808
1809 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001810 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001811 throws IOException, XmlPullParserException {
1812 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1813 String componentNameString = null;
1814 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001815 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001816 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001817
1818 UserManager userManager = UserManager.get(context);
1819
Ihab Awadb78b2762014-07-25 15:16:23 -07001820 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1821 if (parser.getName().equals(COMPONENT_NAME)) {
1822 parser.next();
1823 componentNameString = parser.getText();
1824 } else if (parser.getName().equals(ID)) {
1825 parser.next();
1826 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001827 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1828 parser.next();
1829 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001830 }
1831 }
Ihab Awad26923222014-07-30 10:54:35 -07001832 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001833 UserHandle userHandle = null;
1834 if (userSerialNumberString != null) {
1835 try {
1836 long serialNumber = Long.parseLong(userSerialNumberString);
1837 userHandle = userManager.getUserForSerialNumber(serialNumber);
1838 } catch (NumberFormatException e) {
1839 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1840 }
1841 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001842 return new PhoneAccountHandle(
1843 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001844 idString,
1845 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001846 }
1847 }
1848 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001849 }
1850 };
Santos Cordon176ae282014-07-14 02:02:14 -07001851}