blob: aa17b14c940459eb2da1b13b79e4d5d78d774db1 [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;
Evan Charlton105d9772014-11-25 14:08:53 -080026import 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;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080031import android.os.Binder;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070032import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080033import android.os.Process;
34import android.os.UserHandle;
35import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070036import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070037import android.telecom.ConnectionService;
38import android.telecom.PhoneAccount;
39import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070040import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070041import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070042import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080043import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070044import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070045import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070046import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070047import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070048
Tyler Gunn91d43cf2014-09-17 12:19:39 -070049// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070050import com.android.internal.annotations.VisibleForTesting;
51import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070052import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070053import com.android.internal.util.XmlUtils;
54
Evan Charltonaf51ceb2014-07-30 11:56:36 -070055import org.xmlpull.v1.XmlPullParser;
56import org.xmlpull.v1.XmlPullParserException;
57import org.xmlpull.v1.XmlSerializer;
58
Ihab Awadb78b2762014-07-25 15:16:23 -070059import java.io.BufferedInputStream;
60import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070061import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070062import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070063import java.io.File;
64import java.io.FileNotFoundException;
65import java.io.FileOutputStream;
66import java.io.IOException;
67import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070068import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070069import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070070import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070071import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080072import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070073import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070074import java.util.List;
75import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070076import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070077
78/**
Evan Charlton89176372014-07-19 18:23:09 -070079 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070080 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
81 * implemented in {@link TelecomServiceImpl}, with the notable exception that
82 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
83 * proper authority over the {@code ComponentName}s they are declaring in their
84 * {@code PhoneAccountHandle}s.
85 *
86 *
87 * -- About Users and Phone Accounts --
88 *
Santos Cordon9c30c282015-05-13 16:28:27 -070089 * We store all phone accounts for all users in a single place, which means that there are three
90 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070091 * 1) The Android User that is currently active on the device.
92 * 2) The user which owns/registers the phone account.
93 * 3) The user running the app that is requesting the phone account information.
94 *
95 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
96 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
97 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -070098 * account list. Lets also say that an app running in the work profile has registered a phone
99 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -0700100 *
101 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700102 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
103 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700104 *
105 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
106 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
107 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700108 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700109public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700110
Yorke Lee5e8836a2014-08-22 15:25:18 -0700111 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
112 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
113
Ihab Awadb78b2762014-07-25 15:16:23 -0700114 public abstract static class Listener {
115 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
116 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
117 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
118 }
119
120 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700121 @VisibleForTesting
Santos Cordona9eebe42015-06-11 14:07:44 -0700122 public static final int EXPECTED_STATE_VERSION = 7;
Tyler Gunn84253572014-09-02 14:50:05 -0700123
124 /** Keep in sync with the same in SipSettings.java */
125 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700126
127 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
128 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700129 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800130 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800131 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700132 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800133 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700134
Nancy Chen06ce0622014-10-23 01:17:35 +0000135 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700136 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700137 this(context, FILE_NAME);
138 }
139
140 @VisibleForTesting
141 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700142 // TODO: This file path is subject to change -- it is storing the phone account registry
143 // state file in the path /data/system/users/0/, which is likely not correct in a
144 // multi-user setting.
145 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
146 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
147 getAbsolutePath();
148 mAtomicFile = new AtomicFile(new File(filePath, fileName));
149 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700150 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700151
Ihab Awadb78b2762014-07-25 15:16:23 -0700152 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700153 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800154 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800155 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800156 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700157 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700158 }
159
Tyler Gunn84253572014-09-02 14:50:05 -0700160 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700161 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
162 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
163 * subscription id.
164 * @param accountHandle The handle for the phone account for which to retrieve the
165 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700166 * @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 -0700167 */
Wink Saville35850602014-10-23 15:57:21 -0700168 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700169 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800170
Santos Cordon6a212642015-05-08 16:35:23 -0700171 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800172 TelephonyManager tm =
173 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
174 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700175 }
Santos Cordon479b3022015-02-06 04:27:15 -0800176 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700177 }
178
179 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800180 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
181 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
182 * will be returned.
183 *
Tyler Gunn84253572014-09-02 14:50:05 -0700184 * @param uriScheme The URI scheme for the outgoing call.
185 * @return The {@link PhoneAccountHandle} to use.
186 */
Santos Cordon6a212642015-05-08 16:35:23 -0700187 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700188 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700189
Yorke Lee5e8836a2014-08-22 15:25:18 -0700190 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700191 // If there is a default PhoneAccount, ensure it supports calls to handles with the
192 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700193 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
194 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700195 return userSelected;
196 }
Ihab Awad293edf22014-07-24 17:52:29 -0700197 }
198
Santos Cordonea5cb932015-05-07 16:28:38 -0700199 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700200 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700201 case 0:
202 // There are no accounts, so there can be no default
203 return null;
204 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800205 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700206 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700207 default:
208 // There are multiple accounts with no selected default
209 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700210 }
Ihab Awad104f8062014-07-17 11:29:35 -0700211 }
Santos Cordon176ae282014-07-14 02:02:14 -0700212
Evan Charlton105d9772014-11-25 14:08:53 -0800213 /**
214 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
215 * if it was set by another user).
216 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700217 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700218 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
219 if (account != null) {
220 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700221 }
222 return null;
223 }
224
Santos Cordon6a212642015-05-08 16:35:23 -0700225 /**
226 * Sets the phone account with which to place all calls by default. Set by the user
227 * within phone settings.
228 */
Andrew Leea51a3862014-09-03 14:58:45 -0700229 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700230 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700231 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700232 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700233 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700234 // TODO: Do we really want to return for *any* user?
235 PhoneAccount account = getPhoneAccount(accountHandle);
236 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700237 Log.w(this, "Trying to set nonexistent default outgoing %s",
238 accountHandle);
239 return;
240 }
241
Santos Cordon6a212642015-05-08 16:35:23 -0700242 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700243 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700244 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700245 return;
246 }
247
Santos Cordon6a212642015-05-08 16:35:23 -0700248 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700249 // If the account selected is a SIM account, propagate down to the subscription
250 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700251 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800252 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700253 }
254
Ihab Awad293edf22014-07-24 17:52:29 -0700255 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700256 }
257
Ihab Awad293edf22014-07-24 17:52:29 -0700258 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700259 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700260 }
261
Nancy Chen668dee02014-11-19 15:31:31 -0800262 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
263 return getSubscriptionIdForPhoneAccount(accountHandle) ==
264 SubscriptionManager.getDefaultSmsSubId();
265 }
266
Ihab Awad293edf22014-07-24 17:52:29 -0700267 public void setSimCallManager(PhoneAccountHandle callManager) {
268 if (callManager != null) {
Santos Cordon6a212642015-05-08 16:35:23 -0700269 // TODO: Do we really want to return for *any* user?
270 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
Ihab Awad293edf22014-07-24 17:52:29 -0700271 if (callManagerAccount == null) {
272 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
273 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700274 } else if (!callManagerAccount.hasCapabilities(
275 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700276 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
277 return;
278 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700279 } else {
280 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700281 }
282 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700283
Ihab Awad293edf22014-07-24 17:52:29 -0700284 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700285 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700286 }
287
Evan Charlton105d9772014-11-25 14:08:53 -0800288 /**
289 * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}.
290 */
Ihab Awad293edf22014-07-24 17:52:29 -0700291 public PhoneAccountHandle getSimCallManager() {
Sharvil Nanavatia8d6a382015-05-21 15:53:53 -0700292 // If the "None" account was selected, return null (symmetry with setSimCallManager).
293 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
294 return null;
295 }
296
Santos Cordon6a212642015-05-08 16:35:23 -0700297 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.simCallManager);
298
299 // Return the registered sim call manager iff it still exists (we keep a sticky
300 // setting to survive account deletion and re-addition)
301 if (account != null && !resolveComponent(mState.simCallManager).isEmpty()) {
302 return mState.simCallManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700303 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700304
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700305 // Check carrier config.
306 String defaultSimCallManager = null;
307 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
308 Context.CARRIER_CONFIG_SERVICE);
309 PersistableBundle configBundle = configManager.getConfig();
310 if (configBundle != null) {
311 defaultSimCallManager = configBundle.getString(
312 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
313 }
314
315 // Check OEM config. TODO: Remove this once the carrier config value is set.
316 if (TextUtils.isEmpty(defaultSimCallManager)) {
317 defaultSimCallManager = mContext.getResources().getString(
318 R.string.default_connection_manager_component);
319 }
320
321 if (!TextUtils.isEmpty(defaultSimCallManager)) {
322 ComponentName componentName = ComponentName.unflattenFromString(defaultSimCallManager);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800323 if (componentName == null) {
324 return null;
325 }
326
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700327 // Make sure that the component can be resolved.
Evan Charlton105d9772014-11-25 14:08:53 -0800328 List<ResolveInfo> resolveInfos = resolveComponent(componentName, null);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800329 if (resolveInfos.isEmpty()) {
330 resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle());
331 }
332
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700333 if (!resolveInfos.isEmpty()) {
334 // See if there is registered PhoneAccount by this component.
Sharvil Nanavati76bcc372015-06-02 12:32:29 -0700335 List<PhoneAccountHandle> handles = getPhoneAccountHandles(0, null, null, true);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700336 for (PhoneAccountHandle handle : handles) {
Santos Cordon6a212642015-05-08 16:35:23 -0700337 if (componentName.equals(handle.getComponentName())) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700338 return handle;
339 }
340 }
341 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
342 } else {
343 Log.d(this, "%s could not be resolved; not using as default", componentName);
344 }
345 } else {
346 Log.v(this, "No default connection manager specified");
347 }
348
Ihab Awadb78b2762014-07-25 15:16:23 -0700349 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700350 }
351
Evan Charlton105d9772014-11-25 14:08:53 -0800352 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800353 * Update the current UserHandle to track when users are switched. This will allow the
354 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
355 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700356 * We cannot simply check the calling user because that would always return the primary user for
357 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800358 *
359 * @param userHandle The {@link UserHandle}, as delivered by
360 * {@link Intent#ACTION_USER_SWITCHED}.
361 */
362 public void setCurrentUserHandle(UserHandle userHandle) {
363 if (userHandle == null) {
364 Log.d(this, "setCurrentUserHandle, userHandle = null");
365 userHandle = Process.myUserHandle();
366 }
367 Log.d(this, "setCurrentUserHandle, %s", userHandle);
368 mCurrentUserHandle = userHandle;
369 }
370
Yorke Lee71734c22015-06-02 14:22:56 -0700371 /**
372 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
373 * otherwise.
374 */
375 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700376 PhoneAccount account = getPhoneAccount(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700377 if (account == null) {
378 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700379 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700380 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700381 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700382 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700383 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700384 }
385
Santos Cordon638f05c2015-05-19 17:12:33 -0700386 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700387 account.setIsEnabled(isEnabled);
388 write();
389 fireAccountsChanged();
390 }
Yorke Lee71734c22015-06-02 14:22:56 -0700391 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700392 }
393
Evan Charlton105d9772014-11-25 14:08:53 -0800394 private boolean isVisibleForUser(PhoneAccount account) {
395 if (account == null) {
396 return false;
397 }
398
399 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
400 // all profiles. Only Telephony and SIP accounts should have this capability.
401 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
402 return true;
403 }
404
405 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
406 if (phoneAccountUserHandle == null) {
407 return false;
408 }
409
410 if (mCurrentUserHandle == null) {
411 Log.d(this, "Current user is null; assuming true");
412 return true;
413 }
414
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800415 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
416 return true;
417 }
418
Santos Cordon6a212642015-05-08 16:35:23 -0700419 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800420 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
421 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
422 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800423 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
424 List<UserInfo> profileUsers =
425 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
426 for (UserInfo profileInfo : profileUsers) {
427 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
428 return true;
429 }
Evan Charlton105d9772014-11-25 14:08:53 -0800430 }
431 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800432
Evan Charlton105d9772014-11-25 14:08:53 -0800433 return false;
434 }
435
436 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
437 return resolveComponent(phoneAccountHandle.getComponentName(),
438 phoneAccountHandle.getUserHandle());
439 }
440
441 private List<ResolveInfo> resolveComponent(ComponentName componentName,
442 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800443 PackageManager pm = mContext.getPackageManager();
444 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
445 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800446 try {
447 if (userHandle != null) {
448 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
449 } else {
450 return pm.queryIntentServices(intent, 0);
451 }
452 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700453 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800454 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800455 }
mike dooley10a58312014-11-06 13:46:19 -0800456 }
457
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700458 /**
459 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700460 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700461 *
462 * @return The list of {@link PhoneAccountHandle}s.
463 */
Ihab Awad293edf22014-07-24 17:52:29 -0700464 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700465 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700466 }
467
468 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700469 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700470 }
471
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700472 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700473 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700474 * specified URI scheme.
475 *
476 * @param uriScheme The URI scheme.
477 * @return The phone account handles.
478 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700479 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
480 String uriScheme, boolean includeDisabledAccounts) {
481 return getPhoneAccountHandles(
482 PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700483 }
484
485 /**
486 * Retrieves a list of all the SIM-based phone accounts.
487 */
488 public List<PhoneAccountHandle> getSimPhoneAccounts() {
489 return getPhoneAccountHandles(
490 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700491 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700492 }
493
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700494 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700495 * Retrieves a list of all phone accounts registered by a specified package.
496 *
497 * @param packageName The name of the package that registered the phone accounts.
498 * @return The phone account handles.
499 */
500 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700501 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700502 }
503
504 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700505 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700506 *
507 * @return The phone account handles.
508 */
509 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700510 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null, true);
Santos Cordon176ae282014-07-14 02:02:14 -0700511 }
512
Ihab Awad104f8062014-07-17 11:29:35 -0700513 // TODO: Should we implement an artificial limit for # of accounts associated with a single
514 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700515 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700516 // Enforce the requirement that a connection service for a phone account has the correct
517 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700518 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700519 Log.w(this,
520 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700521 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700522 throw new SecurityException("PhoneAccount connection service requires "
523 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700524 }
525
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700526 addOrReplacePhoneAccount(account);
527 }
528
529 /**
530 * Adds a {@code PhoneAccount}, replacing an existing one if found.
531 *
532 * @param account The {@code PhoneAccount} to add or replace.
533 */
534 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800535 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
536 account.getAccountHandle(), account);
537
Santos Cordonea5cb932015-05-07 16:28:38 -0700538 // Start _enabled_ property as false.
539 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
540 // source app provides or else an third party app could enable itself.
541 boolean isEnabled = false;
542
Santos Cordon6a212642015-05-08 16:35:23 -0700543 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
544 if (oldAccount != null) {
545 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700546 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700547 Log.i(this, getAccountDiffString(account, oldAccount));
548 } else {
549 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700550 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700551
Santos Cordon6a212642015-05-08 16:35:23 -0700552 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700553 // Reset enabled state to whatever the value was if the account was already registered,
554 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
555 account.setIsEnabled(
556 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700557
Ihab Awad293edf22014-07-24 17:52:29 -0700558 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700559 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700560 }
561
Evan Charlton89176372014-07-19 18:23:09 -0700562 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700563 PhoneAccount account = getPhoneAccount(accountHandle);
564 if (account != null) {
565 if (mState.accounts.remove(account)) {
566 write();
567 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700568 }
569 }
Santos Cordon176ae282014-07-14 02:02:14 -0700570 }
571
Tyler Gunnd900ce62014-08-13 11:40:59 -0700572 /**
573 * Un-registers all phone accounts associated with a specified package.
574 *
575 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800576 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700577 */
Evan Charlton105d9772014-11-25 14:08:53 -0800578 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700579 boolean accountsRemoved = false;
580 Iterator<PhoneAccount> it = mState.accounts.iterator();
581 while (it.hasNext()) {
582 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800583 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
584 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
585 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700586 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
587 it.remove();
588 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700589 }
590 }
591
Tyler Gunnd900ce62014-08-13 11:40:59 -0700592 if (accountsRemoved) {
593 write();
594 fireAccountsChanged();
595 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700596 }
597
Nancy Chen140004a2014-10-15 15:48:38 -0700598 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700599 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700600 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
601 }
602
Ihab Awadb78b2762014-07-25 15:16:23 -0700603 public void addListener(Listener l) {
604 mListeners.add(l);
605 }
606
607 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700608 if (l != null) {
609 mListeners.remove(l);
610 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700611 }
612
613 private void fireAccountsChanged() {
614 for (Listener l : mListeners) {
615 l.onAccountsChanged(this);
616 }
617 }
618
619 private void fireDefaultOutgoingChanged() {
620 for (Listener l : mListeners) {
621 l.onDefaultOutgoingChanged(this);
622 }
623 }
624
625 private void fireSimCallManagerChanged() {
626 for (Listener l : mListeners) {
627 l.onSimCallManagerChanged(this);
628 }
Santos Cordon176ae282014-07-14 02:02:14 -0700629 }
630
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700631 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
632 if (account1 == null || account2 == null) {
633 return "Diff: " + account1 + ", " + account2;
634 }
635
636 StringBuffer sb = new StringBuffer();
637 sb.append("[").append(account1.getAccountHandle());
638 appendDiff(sb, "addr", account1.getAddress(), account2.getAddress());
639 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
640 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
641 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
642 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
643 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
644 appendDiff(sb, "subAddr", account1.getSubscriptionAddress(),
645 account2.getSubscriptionAddress());
646 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
647 account2.getSupportedUriSchemes());
648 sb.append("]");
649 return sb.toString();
650 }
651
652 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
653 if (!Objects.equals(obj1, obj2)) {
654 sb.append("(")
655 .append(attrName)
656 .append(": ")
657 .append(obj1)
658 .append(" -> ")
659 .append(obj2)
660 .append(")");
661 }
662 }
663
Tyler Gunncb59b672014-08-20 09:02:11 -0700664 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700665 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700666 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700667 *
668 * @param phoneAccountHandle The phone account to check.
669 * @return {@code True} if the phone account has permission.
670 */
Santos Cordon6a212642015-05-08 16:35:23 -0700671 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800672 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
673 if (resolveInfos.isEmpty()) {
674 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700675 return false;
676 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800677 for (ResolveInfo resolveInfo : resolveInfos) {
678 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700679 if (serviceInfo == null) {
680 return false;
681 }
682
683 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
684 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
685 serviceInfo.permission)) {
686 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
687 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
688 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800689 return false;
690 }
691 }
692 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700693 }
694
Santos Cordon6a212642015-05-08 16:35:23 -0700695 //
696 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
697 //
Ihab Awad293edf22014-07-24 17:52:29 -0700698
Santos Cordonafe59e52014-08-22 16:48:43 -0700699 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700700 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700701 *
Santos Cordon6a212642015-05-08 16:35:23 -0700702 * @param handle
703 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700704 */
Santos Cordon6a212642015-05-08 16:35:23 -0700705 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
706 for (PhoneAccount m : mState.accounts) {
707 if (Objects.equals(handle, m.getAccountHandle())) {
708 return m;
709 }
710 }
711 return null;
712 }
713
714 /**
715 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
716 * account before returning it. The current user is the active user on the actual android
717 * device.
718 */
719 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
720 PhoneAccount account = getPhoneAccount(handle);
721 if (account != null && isVisibleForUser(account)) {
722 return account;
723 }
724 return null;
725 }
726
727 /**
728 * Returns a list of phone account handles with the specified capabilities, uri scheme,
729 * and package name.
730 */
731 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700732 int capabilities,
733 String uriScheme,
734 String packageName,
735 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700736 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700737
738 for (PhoneAccount account : getPhoneAccounts(
739 capabilities, uriScheme, packageName, includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700740 handles.add(account.getAccountHandle());
741 }
742 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700743 }
744
745 /**
746 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700747 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700748 *
Santos Cordon6a212642015-05-08 16:35:23 -0700749 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
750 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700751 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700752 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700753 */
Santos Cordon6a212642015-05-08 16:35:23 -0700754 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700755 int capabilities,
756 String uriScheme,
757 String packageName,
758 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700759 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700760 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700761 if (!(m.isEnabled() || includeDisabledAccounts)) {
762 // Do not include disabled accounts.
763 continue;
764 }
765
Santos Cordon6a212642015-05-08 16:35:23 -0700766 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800767 // Account doesn't have the right capabilities; skip this one.
768 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700769 }
Evan Charlton105d9772014-11-25 14:08:53 -0800770 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
771 // Account doesn't support this URI scheme; skip this one.
772 continue;
773 }
Santos Cordon6a212642015-05-08 16:35:23 -0700774 PhoneAccountHandle handle = m.getAccountHandle();
775
776 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800777 // This component cannot be resolved anymore; skip this one.
778 continue;
779 }
Santos Cordon6a212642015-05-08 16:35:23 -0700780 if (packageName != null &&
781 !packageName.equals(handle.getComponentName().getPackageName())) {
782 // Not the right package name; skip this one.
783 continue;
784 }
Evan Charlton105d9772014-11-25 14:08:53 -0800785 if (!isVisibleForUser(m)) {
786 // Account is not visible for the current user; skip this one.
787 continue;
788 }
Santos Cordon6a212642015-05-08 16:35:23 -0700789 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700790 }
Santos Cordon6a212642015-05-08 16:35:23 -0700791 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700792 }
793
Santos Cordon6a212642015-05-08 16:35:23 -0700794 //
795 // State Implementation for PhoneAccountRegistrar
796 //
797
Ihab Awad293edf22014-07-24 17:52:29 -0700798 /**
799 * The state of this {@code PhoneAccountRegistrar}.
800 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700801 @VisibleForTesting
802 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700803 /**
804 * The account selected by the user to be employed by default for making outgoing calls.
805 * If the user has not made such a selection, then this is null.
806 */
807 public PhoneAccountHandle defaultOutgoing = null;
808
809 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700810 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700811 * manages and optimizes a user's PSTN SIM connections.
812 */
813 public PhoneAccountHandle simCallManager;
814
815 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700816 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700817 */
818 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700819
820 /**
821 * The version number of the State data.
822 */
823 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700824 }
825
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700826 /**
827 * Dumps the state of the {@link CallsManager}.
828 *
829 * @param pw The {@code IndentingPrintWriter} to write the state to.
830 */
831 public void dump(IndentingPrintWriter pw) {
832 if (mState != null) {
833 pw.println("xmlVersion: " + mState.versionNumber);
834 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
835 mState.defaultOutgoing));
836 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
837 mState.simCallManager));
838 pw.println("phoneAccounts:");
839 pw.increaseIndent();
840 for (PhoneAccount phoneAccount : mState.accounts) {
841 pw.println(phoneAccount);
842 }
843 pw.decreaseIndent();
844 }
845 }
846
Ihab Awad293edf22014-07-24 17:52:29 -0700847 ////////////////////////////////////////////////////////////////////////////////////////////////
848 //
849 // State management
850 //
851
852 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700853 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700854 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700855 os = mAtomicFile.startWrite();
856 boolean success = false;
857 try {
858 XmlSerializer serializer = new FastXmlSerializer();
859 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800860 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700861 serializer.flush();
862 success = true;
863 } finally {
864 if (success) {
865 mAtomicFile.finishWrite(os);
866 } else {
867 mAtomicFile.failWrite(os);
868 }
869 }
870 } catch (IOException e) {
871 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700872 }
873 }
874
Ihab Awadb78b2762014-07-25 15:16:23 -0700875 private void read() {
876 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700877 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700878 is = mAtomicFile.openRead();
879 } catch (FileNotFoundException ex) {
880 return;
881 }
882
Tyler Gunn84253572014-09-02 14:50:05 -0700883 boolean versionChanged = false;
884
Ihab Awadb78b2762014-07-25 15:16:23 -0700885 XmlPullParser parser;
886 try {
887 parser = Xml.newPullParser();
888 parser.setInput(new BufferedInputStream(is), null);
889 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700890 mState = readFromXml(parser, mContext);
891 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
892
Ihab Awadb78b2762014-07-25 15:16:23 -0700893 } catch (IOException | XmlPullParserException e) {
894 Log.e(this, e, "Reading state from XML file");
895 mState = new State();
896 } finally {
897 try {
898 is.close();
899 } catch (IOException e) {
900 Log.e(this, e, "Closing InputStream");
901 }
Ihab Awad104f8062014-07-17 11:29:35 -0700902 }
Tyler Gunn84253572014-09-02 14:50:05 -0700903
Evan Charlton105d9772014-11-25 14:08:53 -0800904 // Verify all of the UserHandles.
905 List<PhoneAccount> badAccounts = new ArrayList<>();
906 for (PhoneAccount phoneAccount : mState.accounts) {
907 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
908 if (userHandle == null) {
909 Log.w(this, "Missing UserHandle for %s", phoneAccount);
910 badAccounts.add(phoneAccount);
911 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
912 Log.w(this, "User does not exist for %s", phoneAccount);
913 badAccounts.add(phoneAccount);
914 }
915 }
916 mState.accounts.removeAll(badAccounts);
917
Tyler Gunn84253572014-09-02 14:50:05 -0700918 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800919 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700920 write();
921 }
Santos Cordon176ae282014-07-14 02:02:14 -0700922 }
923
Evan Charlton105d9772014-11-25 14:08:53 -0800924 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700925 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800926 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700927 }
Ihab Awad104f8062014-07-17 11:29:35 -0700928
Tyler Gunn84253572014-09-02 14:50:05 -0700929 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700930 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700931 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700932 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700933 }
934
Ihab Awad293edf22014-07-24 17:52:29 -0700935 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700936 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700937 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700938 //
939
Ihab Awadb78b2762014-07-25 15:16:23 -0700940 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700941 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700942 private static final String LENGTH_ATTRIBUTE = "length";
943 private static final String VALUE_TAG = "value";
944
Ihab Awadb78b2762014-07-25 15:16:23 -0700945 /**
946 * Write the supplied object to XML
947 */
Evan Charlton105d9772014-11-25 14:08:53 -0800948 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700949 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700950
951 /**
952 * Read from the supplied XML into a new object, returning null in case of an
953 * unrecoverable schema mismatch or other data error. 'parser' must be already
954 * positioned at the first tag that is expected to have been emitted by this
955 * object's writeToXml(). This object tries to fail early without modifying
956 * 'parser' if it does not recognize the data it sees.
957 */
Tyler Gunn84253572014-09-02 14:50:05 -0700958 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700959 throws IOException, XmlPullParserException;
960
Ihab Awadd9f54382014-10-24 11:44:47 -0700961 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700962 throws IOException {
963 if (value != null) {
964 serializer.startTag(null, tagName);
965 serializer.text(Objects.toString(value));
966 serializer.endTag(null, tagName);
967 }
968 }
Tyler Gunn84253572014-09-02 14:50:05 -0700969
970 /**
971 * Serializes a string array.
972 *
973 * @param tagName The tag name for the string array.
974 * @param values The string values to serialize.
975 * @param serializer The serializer.
976 * @throws IOException
977 */
978 protected void writeStringList(String tagName, List<String> values,
979 XmlSerializer serializer)
980 throws IOException {
981
982 serializer.startTag(null, tagName);
983 if (values != null) {
984 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
985 for (String toSerialize : values) {
986 serializer.startTag(null, VALUE_TAG);
987 if (toSerialize != null ){
988 serializer.text(toSerialize);
989 }
Tyler Gunn84253572014-09-02 14:50:05 -0700990 serializer.endTag(null, VALUE_TAG);
991 }
992 } else {
993 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
994 }
995 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700996 }
Tyler Gunn84253572014-09-02 14:50:05 -0700997
Santos Cordon9c30c282015-05-13 16:28:27 -0700998 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -0700999 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001000 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001001 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001002 value.writeToStream(stream);
1003 byte[] iconByteArray = stream.toByteArray();
1004 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001005
1006 serializer.startTag(null, tagName);
1007 serializer.text(text);
1008 serializer.endTag(null, tagName);
1009 }
Tyler Gunn84253572014-09-02 14:50:05 -07001010 }
1011
Evan Charlton105d9772014-11-25 14:08:53 -08001012 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1013 throws IOException {
1014 serializer.startTag(null, tagName);
1015 serializer.text(Long.valueOf(value).toString());
1016 serializer.endTag(null, tagName);
1017 }
1018
Tyler Gunn84253572014-09-02 14:50:05 -07001019 /**
1020 * Reads a string array from the XML parser.
1021 *
1022 * @param parser The XML parser.
1023 * @return String array containing the parsed values.
1024 * @throws IOException Exception related to IO.
1025 * @throws XmlPullParserException Exception related to parsing.
1026 */
1027 protected List<String> readStringList(XmlPullParser parser)
1028 throws IOException, XmlPullParserException {
1029
1030 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
1031 List<String> arrayEntries = new ArrayList<String>(length);
1032 String value = null;
1033
1034 if (length == 0) {
1035 return arrayEntries;
1036 }
1037
1038 int outerDepth = parser.getDepth();
1039 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1040 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001041 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001042 value = parser.getText();
1043 arrayEntries.add(value);
1044 }
1045 }
1046
1047 return arrayEntries;
1048 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001049
Santos Cordon9c30c282015-05-13 16:28:27 -07001050 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001051 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1052 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1053 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001054
1055 protected Icon readIcon(XmlPullParser parser) throws IOException {
1056 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1057 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1058 return Icon.createFromStream(stream);
1059 }
Ihab Awad104f8062014-07-17 11:29:35 -07001060 }
1061
Ihab Awadb78b2762014-07-25 15:16:23 -07001062 @VisibleForTesting
1063 public static final XmlSerialization<State> sStateXml =
1064 new XmlSerialization<State>() {
1065 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001066 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -07001067 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -07001068 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001069 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001070
1071 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001072 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001073 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001074 if (o != null) {
1075 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001076 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001077
Ihab Awad26923222014-07-30 10:54:35 -07001078 if (o.defaultOutgoing != null) {
1079 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001080 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001081 serializer.endTag(null, DEFAULT_OUTGOING);
1082 }
1083
1084 if (o.simCallManager != null) {
1085 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -08001086 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001087 serializer.endTag(null, SIM_CALL_MANAGER);
1088 }
1089
1090 serializer.startTag(null, ACCOUNTS);
1091 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001092 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001093 }
1094 serializer.endTag(null, ACCOUNTS);
1095
1096 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001097 }
Ihab Awad104f8062014-07-17 11:29:35 -07001098 }
1099
1100 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001101 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001102 throws IOException, XmlPullParserException {
1103 if (parser.getName().equals(CLASS_STATE)) {
1104 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001105
1106 String rawVersion = parser.getAttributeValue(null, VERSION);
1107 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1108 Integer.parseInt(rawVersion);
1109
Ihab Awadb78b2762014-07-25 15:16:23 -07001110 int outerDepth = parser.getDepth();
1111 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1112 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1113 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001114 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1115 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001116 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
1117 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001118 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
1119 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -08001120 if (s.simCallManager.getUserHandle() == null) {
1121 // This should never happen, but handle the upgrade case.
1122 s.simCallManager = new PhoneAccountHandle(
1123 s.simCallManager.getComponentName(),
1124 s.simCallManager.getId(),
1125 Process.myUserHandle());
1126 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001127 } else if (parser.getName().equals(ACCOUNTS)) {
1128 int accountsDepth = parser.getDepth();
1129 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001130 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1131 s.versionNumber, context);
1132
1133 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001134 s.accounts.add(account);
1135 }
1136 }
Ihab Awad104f8062014-07-17 11:29:35 -07001137 }
1138 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001139 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001140 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001141 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001142 }
1143 };
1144
Ihab Awadb78b2762014-07-25 15:16:23 -07001145 @VisibleForTesting
1146 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1147 new XmlSerialization<PhoneAccount>() {
1148 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1149 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001150 private static final String ADDRESS = "handle";
1151 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001152 private static final String CAPABILITIES = "capabilities";
1153 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001154 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1155 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001156 private static final String ICON_TINT = "icon_tint";
1157 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001158 private static final String LABEL = "label";
1159 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001160 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001161 private static final String ICON = "icon";
Santos Cordonea5cb932015-05-07 16:28:38 -07001162 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001163
1164 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001165 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001166 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001167 if (o != null) {
1168 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001169
Ihab Awad26923222014-07-30 10:54:35 -07001170 if (o.getAccountHandle() != null) {
1171 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001172 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001173 serializer.endTag(null, ACCOUNT_HANDLE);
1174 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001175
Ihab Awadd9f54382014-10-24 11:44:47 -07001176 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1177 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1178 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001179 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001180 writeTextIfNonNull(HIGHLIGHT_COLOR,
1181 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001182 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1183 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001184 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001185 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001186
Ihab Awad26923222014-07-30 10:54:35 -07001187 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1188 }
Ihab Awad104f8062014-07-17 11:29:35 -07001189 }
1190
Tyler Gunn84253572014-09-02 14:50:05 -07001191 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001192 throws IOException, XmlPullParserException {
1193 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1194 int outerDepth = parser.getDepth();
1195 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001196 Uri address = null;
1197 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001198 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001199 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001200 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001201 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001202 int iconTint = PhoneAccount.NO_ICON_TINT;
1203 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001204 String label = null;
1205 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001206 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001207 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001208 boolean enabled = false;
Ihab Awadb78b2762014-07-25 15:16:23 -07001209
1210 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1211 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1212 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001213 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1214 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001215 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001216 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001217 address = Uri.parse(parser.getText());
1218 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001219 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001220 String nextText = parser.getText();
1221 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001222 } else if (parser.getName().equals(CAPABILITIES)) {
1223 parser.next();
1224 capabilities = Integer.parseInt(parser.getText());
1225 } else if (parser.getName().equals(ICON_RES_ID)) {
1226 parser.next();
1227 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001228 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1229 parser.next();
1230 iconPackageName = parser.getText();
1231 } else if (parser.getName().equals(ICON_BITMAP)) {
1232 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001233 iconBitmap = readBitmap(parser);
1234 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001235 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001236 iconTint = Integer.parseInt(parser.getText());
1237 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1238 parser.next();
1239 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001240 } else if (parser.getName().equals(LABEL)) {
1241 parser.next();
1242 label = parser.getText();
1243 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1244 parser.next();
1245 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001246 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1247 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001248 } else if (parser.getName().equals(ICON)) {
1249 parser.next();
1250 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001251 } else if (parser.getName().equals(ENABLED)) {
1252 parser.next();
1253 enabled = "true".equalsIgnoreCase(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001254 }
1255 }
Tyler Gunn84253572014-09-02 14:50:05 -07001256
Santos Cordona9eebe42015-06-11 14:07:44 -07001257 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1258 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001259 ComponentName sipComponentName = new ComponentName("com.android.phone",
1260 "com.android.services.telephony.sip.SipConnectionService");
1261
Tyler Gunn84253572014-09-02 14:50:05 -07001262 // Upgrade older phone accounts to specify the supported URI schemes.
1263 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001264 supportedUriSchemes = new ArrayList<>();
1265
1266 // Handle the SIP connection service.
1267 // Check the system settings to see if it also should handle "tel" calls.
1268 if (accountHandle.getComponentName().equals(sipComponentName)) {
1269 boolean useSipForPstn = useSipForPstnCalls(context);
1270 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1271 if (useSipForPstn) {
1272 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1273 }
1274 } else {
1275 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1276 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1277 }
1278 }
1279
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001280 // Upgrade older phone accounts with explicit package name
1281 if (version < 5) {
1282 if (iconBitmap == null) {
1283 iconPackageName = accountHandle.getComponentName().getPackageName();
1284 }
1285 }
1286
Santos Cordona82aed82015-05-26 10:43:56 -07001287 if (version < 6) {
1288 // Always enable all SIP accounts on upgrade to version 6
1289 if (accountHandle.getComponentName().equals(sipComponentName)) {
1290 enabled = true;
1291 }
1292 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001293 if (version < 7) {
1294 // Always enabled all PSTN acocunts on upgrade to version 7
1295 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1296 enabled = true;
1297 }
1298 }
Santos Cordona82aed82015-05-26 10:43:56 -07001299
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001300 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001301 .setAddress(address)
1302 .setSubscriptionAddress(subscriptionAddress)
1303 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001304 .setShortDescription(shortDescription)
1305 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001306 .setHighlightColor(highlightColor)
1307 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001308
Santos Cordon9c30c282015-05-13 16:28:27 -07001309 if (icon != null) {
1310 builder.setIcon(icon);
1311 } else if (iconBitmap != null) {
1312 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1313 } else if (!TextUtils.isEmpty(iconPackageName)) {
1314 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1315 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001316 }
1317
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001318 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001319 }
1320 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001321 }
Tyler Gunn84253572014-09-02 14:50:05 -07001322
1323 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001324 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1325 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001326 *
1327 * @param context The context.
1328 * @return {@code True} if SIP should be used for all calls.
1329 */
1330 private boolean useSipForPstnCalls(Context context) {
1331 String option = Settings.System.getString(context.getContentResolver(),
1332 Settings.System.SIP_CALL_OPTIONS);
1333 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1334 return option.equals(Settings.System.SIP_ALWAYS);
1335 }
Ihab Awad104f8062014-07-17 11:29:35 -07001336 };
1337
Ihab Awadb78b2762014-07-25 15:16:23 -07001338 @VisibleForTesting
1339 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1340 new XmlSerialization<PhoneAccountHandle>() {
1341 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001342 private static final String COMPONENT_NAME = "component_name";
1343 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001344 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001345
1346 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001347 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001348 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001349 if (o != null) {
1350 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001351
Ihab Awad26923222014-07-30 10:54:35 -07001352 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001353 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001354 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1355 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001356
Ihab Awadd9f54382014-10-24 11:44:47 -07001357 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001358
Evan Charlton105d9772014-11-25 14:08:53 -08001359 if (o.getUserHandle() != null && context != null) {
1360 UserManager userManager = UserManager.get(context);
1361 writeLong(USER_SERIAL_NUMBER,
1362 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1363 }
1364
Ihab Awad26923222014-07-30 10:54:35 -07001365 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1366 }
Ihab Awad104f8062014-07-17 11:29:35 -07001367 }
1368
1369 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001370 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001371 throws IOException, XmlPullParserException {
1372 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1373 String componentNameString = null;
1374 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001375 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001376 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001377
1378 UserManager userManager = UserManager.get(context);
1379
Ihab Awadb78b2762014-07-25 15:16:23 -07001380 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1381 if (parser.getName().equals(COMPONENT_NAME)) {
1382 parser.next();
1383 componentNameString = parser.getText();
1384 } else if (parser.getName().equals(ID)) {
1385 parser.next();
1386 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001387 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1388 parser.next();
1389 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001390 }
1391 }
Ihab Awad26923222014-07-30 10:54:35 -07001392 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001393 UserHandle userHandle = null;
1394 if (userSerialNumberString != null) {
1395 try {
1396 long serialNumber = Long.parseLong(userSerialNumberString);
1397 userHandle = userManager.getUserForSerialNumber(serialNumber);
1398 } catch (NumberFormatException e) {
1399 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1400 }
1401 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001402 return new PhoneAccountHandle(
1403 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001404 idString,
1405 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001406 }
1407 }
1408 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001409 }
1410 };
Santos Cordon176ae282014-07-14 02:02:14 -07001411}