blob: ee218d793fac8b123ece8791cfeb692ca5c3d91d [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;
Evan Charlton105d9772014-11-25 14:08:53 -080032import android.os.Process;
33import android.os.UserHandle;
34import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070035import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070036import android.telecom.ConnectionService;
37import android.telecom.PhoneAccount;
38import android.telecom.PhoneAccountHandle;
Nancy Chen140004a2014-10-15 15:48:38 -070039import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070040import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080041import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070042import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070043import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070044import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070045import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070046
Tyler Gunn91d43cf2014-09-17 12:19:39 -070047// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070048import com.android.internal.annotations.VisibleForTesting;
49import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070050import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.util.XmlUtils;
52
Evan Charltonaf51ceb2014-07-30 11:56:36 -070053import org.xmlpull.v1.XmlPullParser;
54import org.xmlpull.v1.XmlPullParserException;
55import org.xmlpull.v1.XmlSerializer;
56
Ihab Awadb78b2762014-07-25 15:16:23 -070057import java.io.BufferedInputStream;
58import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070059import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070060import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070061import java.io.File;
62import java.io.FileNotFoundException;
63import java.io.FileOutputStream;
64import java.io.IOException;
65import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070066import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070067import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070068import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070069import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080070import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070071import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070072import java.util.List;
73import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070074import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070075
76/**
Evan Charlton89176372014-07-19 18:23:09 -070077 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070078 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
79 * implemented in {@link TelecomServiceImpl}, with the notable exception that
80 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
81 * proper authority over the {@code ComponentName}s they are declaring in their
82 * {@code PhoneAccountHandle}s.
83 *
84 *
85 * -- About Users and Phone Accounts --
86 *
Santos Cordon9c30c282015-05-13 16:28:27 -070087 * We store all phone accounts for all users in a single place, which means that there are three
88 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070089 * 1) The Android User that is currently active on the device.
90 * 2) The user which owns/registers the phone account.
91 * 3) The user running the app that is requesting the phone account information.
92 *
93 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
94 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
95 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -070096 * account list. Lets also say that an app running in the work profile has registered a phone
97 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -070098 *
99 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700100 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
101 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700102 *
103 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
104 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
105 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700106 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700107public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700108
Yorke Lee5e8836a2014-08-22 15:25:18 -0700109 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
110 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
111
Ihab Awadb78b2762014-07-25 15:16:23 -0700112 public abstract static class Listener {
113 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
114 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
115 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
116 }
117
118 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700119 @VisibleForTesting
Santos Cordona9eebe42015-06-11 14:07:44 -0700120 public static final int EXPECTED_STATE_VERSION = 7;
Tyler Gunn84253572014-09-02 14:50:05 -0700121
122 /** Keep in sync with the same in SipSettings.java */
123 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700124
125 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
126 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700127 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800128 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800129 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700130 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800131 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700132
Nancy Chen06ce0622014-10-23 01:17:35 +0000133 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700134 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700135 this(context, FILE_NAME);
136 }
137
138 @VisibleForTesting
139 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700140 // TODO: This file path is subject to change -- it is storing the phone account registry
141 // state file in the path /data/system/users/0/, which is likely not correct in a
142 // multi-user setting.
143 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
144 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
145 getAbsolutePath();
146 mAtomicFile = new AtomicFile(new File(filePath, fileName));
147 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700148 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700149
Ihab Awadb78b2762014-07-25 15:16:23 -0700150 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700151 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800152 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800153 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800154 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700155 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700156 }
157
Tyler Gunn84253572014-09-02 14:50:05 -0700158 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700159 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
160 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
161 * subscription id.
162 * @param accountHandle The handle for the phone account for which to retrieve the
163 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700164 * @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 -0700165 */
Wink Saville35850602014-10-23 15:57:21 -0700166 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700167 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800168
Santos Cordon6a212642015-05-08 16:35:23 -0700169 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800170 TelephonyManager tm =
171 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
172 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700173 }
Santos Cordon479b3022015-02-06 04:27:15 -0800174 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700175 }
176
177 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800178 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
179 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
180 * will be returned.
181 *
Tyler Gunn84253572014-09-02 14:50:05 -0700182 * @param uriScheme The URI scheme for the outgoing call.
183 * @return The {@link PhoneAccountHandle} to use.
184 */
Santos Cordon6a212642015-05-08 16:35:23 -0700185 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700186 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700187
Yorke Lee5e8836a2014-08-22 15:25:18 -0700188 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700189 // If there is a default PhoneAccount, ensure it supports calls to handles with the
190 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700191 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
192 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700193 return userSelected;
194 }
Ihab Awad293edf22014-07-24 17:52:29 -0700195 }
196
Santos Cordonea5cb932015-05-07 16:28:38 -0700197 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700198 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700199 case 0:
200 // There are no accounts, so there can be no default
201 return null;
202 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800203 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700204 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700205 default:
206 // There are multiple accounts with no selected default
207 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700208 }
Ihab Awad104f8062014-07-17 11:29:35 -0700209 }
Santos Cordon176ae282014-07-14 02:02:14 -0700210
Evan Charlton105d9772014-11-25 14:08:53 -0800211 /**
212 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
213 * if it was set by another user).
214 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700215 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700216 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
217 if (account != null) {
218 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700219 }
220 return null;
221 }
222
Santos Cordon6a212642015-05-08 16:35:23 -0700223 /**
224 * Sets the phone account with which to place all calls by default. Set by the user
225 * within phone settings.
226 */
Andrew Leea51a3862014-09-03 14:58:45 -0700227 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700228 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700229 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700230 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700231 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700232 // TODO: Do we really want to return for *any* user?
233 PhoneAccount account = getPhoneAccount(accountHandle);
234 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700235 Log.w(this, "Trying to set nonexistent default outgoing %s",
236 accountHandle);
237 return;
238 }
239
Santos Cordon6a212642015-05-08 16:35:23 -0700240 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700241 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700242 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700243 return;
244 }
245
Santos Cordon6a212642015-05-08 16:35:23 -0700246 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700247 // If the account selected is a SIM account, propagate down to the subscription
248 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700249 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800250 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700251 }
252
Ihab Awad293edf22014-07-24 17:52:29 -0700253 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700254 }
255
Ihab Awad293edf22014-07-24 17:52:29 -0700256 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700257 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700258 }
259
Nancy Chen668dee02014-11-19 15:31:31 -0800260 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
261 return getSubscriptionIdForPhoneAccount(accountHandle) ==
262 SubscriptionManager.getDefaultSmsSubId();
263 }
264
Ihab Awad293edf22014-07-24 17:52:29 -0700265 public void setSimCallManager(PhoneAccountHandle callManager) {
266 if (callManager != null) {
Santos Cordon6a212642015-05-08 16:35:23 -0700267 // TODO: Do we really want to return for *any* user?
268 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
Ihab Awad293edf22014-07-24 17:52:29 -0700269 if (callManagerAccount == null) {
270 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
271 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700272 } else if (!callManagerAccount.hasCapabilities(
273 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700274 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
275 return;
276 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700277 } else {
278 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700279 }
280 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700281
Ihab Awad293edf22014-07-24 17:52:29 -0700282 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700283 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700284 }
285
Evan Charlton105d9772014-11-25 14:08:53 -0800286 /**
287 * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}.
288 */
Ihab Awad293edf22014-07-24 17:52:29 -0700289 public PhoneAccountHandle getSimCallManager() {
Sharvil Nanavatia8d6a382015-05-21 15:53:53 -0700290 // If the "None" account was selected, return null (symmetry with setSimCallManager).
291 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
292 return null;
293 }
294
Santos Cordon6a212642015-05-08 16:35:23 -0700295 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.simCallManager);
296
297 // Return the registered sim call manager iff it still exists (we keep a sticky
298 // setting to survive account deletion and re-addition)
299 if (account != null && !resolveComponent(mState.simCallManager).isEmpty()) {
300 return mState.simCallManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700301 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700302
Santos Cordon6a212642015-05-08 16:35:23 -0700303 // We have no set call manager, but check to see if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700304 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700305 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700306 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700307 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800308 if (componentName == null) {
309 return null;
310 }
311
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700312 // Make sure that the component can be resolved.
Evan Charlton105d9772014-11-25 14:08:53 -0800313 List<ResolveInfo> resolveInfos = resolveComponent(componentName, null);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800314 if (resolveInfos.isEmpty()) {
315 resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle());
316 }
317
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700318 if (!resolveInfos.isEmpty()) {
319 // See if there is registered PhoneAccount by this component.
Sharvil Nanavati76bcc372015-06-02 12:32:29 -0700320 List<PhoneAccountHandle> handles = getPhoneAccountHandles(0, null, null, true);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700321 for (PhoneAccountHandle handle : handles) {
Santos Cordon6a212642015-05-08 16:35:23 -0700322 if (componentName.equals(handle.getComponentName())) {
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700323 return handle;
324 }
325 }
326 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
327 } else {
328 Log.d(this, "%s could not be resolved; not using as default", componentName);
329 }
330 } else {
331 Log.v(this, "No default connection manager specified");
332 }
333
Ihab Awadb78b2762014-07-25 15:16:23 -0700334 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700335 }
336
Evan Charlton105d9772014-11-25 14:08:53 -0800337 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800338 * Update the current UserHandle to track when users are switched. This will allow the
339 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
340 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700341 * We cannot simply check the calling user because that would always return the primary user for
342 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800343 *
344 * @param userHandle The {@link UserHandle}, as delivered by
345 * {@link Intent#ACTION_USER_SWITCHED}.
346 */
347 public void setCurrentUserHandle(UserHandle userHandle) {
348 if (userHandle == null) {
349 Log.d(this, "setCurrentUserHandle, userHandle = null");
350 userHandle = Process.myUserHandle();
351 }
352 Log.d(this, "setCurrentUserHandle, %s", userHandle);
353 mCurrentUserHandle = userHandle;
354 }
355
Yorke Lee71734c22015-06-02 14:22:56 -0700356 /**
357 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
358 * otherwise.
359 */
360 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700361 PhoneAccount account = getPhoneAccount(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700362 if (account == null) {
363 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700364 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700365 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700366 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700367 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700368 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700369 }
370
Santos Cordon638f05c2015-05-19 17:12:33 -0700371 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700372 account.setIsEnabled(isEnabled);
373 write();
374 fireAccountsChanged();
375 }
Yorke Lee71734c22015-06-02 14:22:56 -0700376 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700377 }
378
Evan Charlton105d9772014-11-25 14:08:53 -0800379 private boolean isVisibleForUser(PhoneAccount account) {
380 if (account == null) {
381 return false;
382 }
383
384 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
385 // all profiles. Only Telephony and SIP accounts should have this capability.
386 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
387 return true;
388 }
389
390 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
391 if (phoneAccountUserHandle == null) {
392 return false;
393 }
394
395 if (mCurrentUserHandle == null) {
396 Log.d(this, "Current user is null; assuming true");
397 return true;
398 }
399
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800400 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
401 return true;
402 }
403
Santos Cordon6a212642015-05-08 16:35:23 -0700404 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800405 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
406 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
407 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800408 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
409 List<UserInfo> profileUsers =
410 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
411 for (UserInfo profileInfo : profileUsers) {
412 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
413 return true;
414 }
Evan Charlton105d9772014-11-25 14:08:53 -0800415 }
416 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800417
Evan Charlton105d9772014-11-25 14:08:53 -0800418 return false;
419 }
420
421 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
422 return resolveComponent(phoneAccountHandle.getComponentName(),
423 phoneAccountHandle.getUserHandle());
424 }
425
426 private List<ResolveInfo> resolveComponent(ComponentName componentName,
427 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800428 PackageManager pm = mContext.getPackageManager();
429 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
430 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800431 try {
432 if (userHandle != null) {
433 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
434 } else {
435 return pm.queryIntentServices(intent, 0);
436 }
437 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700438 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800439 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800440 }
mike dooley10a58312014-11-06 13:46:19 -0800441 }
442
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700443 /**
444 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700445 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700446 *
447 * @return The list of {@link PhoneAccountHandle}s.
448 */
Ihab Awad293edf22014-07-24 17:52:29 -0700449 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700450 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700451 }
452
453 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700454 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700455 }
456
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700457 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700458 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700459 * specified URI scheme.
460 *
461 * @param uriScheme The URI scheme.
462 * @return The phone account handles.
463 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700464 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
465 String uriScheme, boolean includeDisabledAccounts) {
466 return getPhoneAccountHandles(
467 PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700468 }
469
470 /**
471 * Retrieves a list of all the SIM-based phone accounts.
472 */
473 public List<PhoneAccountHandle> getSimPhoneAccounts() {
474 return getPhoneAccountHandles(
475 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700476 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700477 }
478
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700479 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700480 * Retrieves a list of all phone accounts registered by a specified package.
481 *
482 * @param packageName The name of the package that registered the phone accounts.
483 * @return The phone account handles.
484 */
485 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700486 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700487 }
488
489 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700490 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700491 *
492 * @return The phone account handles.
493 */
494 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700495 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null, true);
Santos Cordon176ae282014-07-14 02:02:14 -0700496 }
497
Ihab Awad104f8062014-07-17 11:29:35 -0700498 // TODO: Should we implement an artificial limit for # of accounts associated with a single
499 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700500 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700501 // Enforce the requirement that a connection service for a phone account has the correct
502 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700503 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700504 Log.w(this,
505 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700506 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700507 throw new SecurityException("PhoneAccount connection service requires "
508 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700509 }
510
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700511 addOrReplacePhoneAccount(account);
512 }
513
514 /**
515 * Adds a {@code PhoneAccount}, replacing an existing one if found.
516 *
517 * @param account The {@code PhoneAccount} to add or replace.
518 */
519 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800520 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
521 account.getAccountHandle(), account);
522
Santos Cordonea5cb932015-05-07 16:28:38 -0700523 // Start _enabled_ property as false.
524 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
525 // source app provides or else an third party app could enable itself.
526 boolean isEnabled = false;
527
Santos Cordon6a212642015-05-08 16:35:23 -0700528 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
529 if (oldAccount != null) {
530 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700531 isEnabled = oldAccount.isEnabled();
Santos Cordon176ae282014-07-14 02:02:14 -0700532 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700533
Santos Cordon6a212642015-05-08 16:35:23 -0700534 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700535 // Reset enabled state to whatever the value was if the account was already registered,
536 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
537 account.setIsEnabled(
538 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700539
Ihab Awad293edf22014-07-24 17:52:29 -0700540 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700541 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700542 }
543
Evan Charlton89176372014-07-19 18:23:09 -0700544 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700545 PhoneAccount account = getPhoneAccount(accountHandle);
546 if (account != null) {
547 if (mState.accounts.remove(account)) {
548 write();
549 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700550 }
551 }
Santos Cordon176ae282014-07-14 02:02:14 -0700552 }
553
Tyler Gunnd900ce62014-08-13 11:40:59 -0700554 /**
555 * Un-registers all phone accounts associated with a specified package.
556 *
557 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800558 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700559 */
Evan Charlton105d9772014-11-25 14:08:53 -0800560 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700561 boolean accountsRemoved = false;
562 Iterator<PhoneAccount> it = mState.accounts.iterator();
563 while (it.hasNext()) {
564 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800565 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
566 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
567 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700568 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
569 it.remove();
570 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700571 }
572 }
573
Tyler Gunnd900ce62014-08-13 11:40:59 -0700574 if (accountsRemoved) {
575 write();
576 fireAccountsChanged();
577 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700578 }
579
Nancy Chen140004a2014-10-15 15:48:38 -0700580 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700581 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700582 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
583 }
584
Ihab Awadb78b2762014-07-25 15:16:23 -0700585 public void addListener(Listener l) {
586 mListeners.add(l);
587 }
588
589 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700590 if (l != null) {
591 mListeners.remove(l);
592 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700593 }
594
595 private void fireAccountsChanged() {
596 for (Listener l : mListeners) {
597 l.onAccountsChanged(this);
598 }
599 }
600
601 private void fireDefaultOutgoingChanged() {
602 for (Listener l : mListeners) {
603 l.onDefaultOutgoingChanged(this);
604 }
605 }
606
607 private void fireSimCallManagerChanged() {
608 for (Listener l : mListeners) {
609 l.onSimCallManagerChanged(this);
610 }
Santos Cordon176ae282014-07-14 02:02:14 -0700611 }
612
Tyler Gunncb59b672014-08-20 09:02:11 -0700613 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700614 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700615 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700616 *
617 * @param phoneAccountHandle The phone account to check.
618 * @return {@code True} if the phone account has permission.
619 */
Santos Cordon6a212642015-05-08 16:35:23 -0700620 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800621 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
622 if (resolveInfos.isEmpty()) {
623 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700624 return false;
625 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800626 for (ResolveInfo resolveInfo : resolveInfos) {
627 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700628 if (serviceInfo == null) {
629 return false;
630 }
631
632 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
633 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
634 serviceInfo.permission)) {
635 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
636 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
637 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800638 return false;
639 }
640 }
641 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700642 }
643
Santos Cordon6a212642015-05-08 16:35:23 -0700644 //
645 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
646 //
Ihab Awad293edf22014-07-24 17:52:29 -0700647
Santos Cordonafe59e52014-08-22 16:48:43 -0700648 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700649 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700650 *
Santos Cordon6a212642015-05-08 16:35:23 -0700651 * @param handle
652 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700653 */
Santos Cordon6a212642015-05-08 16:35:23 -0700654 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
655 for (PhoneAccount m : mState.accounts) {
656 if (Objects.equals(handle, m.getAccountHandle())) {
657 return m;
658 }
659 }
660 return null;
661 }
662
663 /**
664 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
665 * account before returning it. The current user is the active user on the actual android
666 * device.
667 */
668 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
669 PhoneAccount account = getPhoneAccount(handle);
670 if (account != null && isVisibleForUser(account)) {
671 return account;
672 }
673 return null;
674 }
675
676 /**
677 * Returns a list of phone account handles with the specified capabilities, uri scheme,
678 * and package name.
679 */
680 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700681 int capabilities,
682 String uriScheme,
683 String packageName,
684 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700685 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700686
687 for (PhoneAccount account : getPhoneAccounts(
688 capabilities, uriScheme, packageName, includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700689 handles.add(account.getAccountHandle());
690 }
691 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700692 }
693
694 /**
695 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700696 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700697 *
Santos Cordon6a212642015-05-08 16:35:23 -0700698 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
699 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700700 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700701 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700702 */
Santos Cordon6a212642015-05-08 16:35:23 -0700703 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700704 int capabilities,
705 String uriScheme,
706 String packageName,
707 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700708 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700709 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700710 if (!(m.isEnabled() || includeDisabledAccounts)) {
711 // Do not include disabled accounts.
712 continue;
713 }
714
Santos Cordon6a212642015-05-08 16:35:23 -0700715 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800716 // Account doesn't have the right capabilities; skip this one.
717 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700718 }
Evan Charlton105d9772014-11-25 14:08:53 -0800719 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
720 // Account doesn't support this URI scheme; skip this one.
721 continue;
722 }
Santos Cordon6a212642015-05-08 16:35:23 -0700723 PhoneAccountHandle handle = m.getAccountHandle();
724
725 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800726 // This component cannot be resolved anymore; skip this one.
727 continue;
728 }
Santos Cordon6a212642015-05-08 16:35:23 -0700729 if (packageName != null &&
730 !packageName.equals(handle.getComponentName().getPackageName())) {
731 // Not the right package name; skip this one.
732 continue;
733 }
Evan Charlton105d9772014-11-25 14:08:53 -0800734 if (!isVisibleForUser(m)) {
735 // Account is not visible for the current user; skip this one.
736 continue;
737 }
Santos Cordon6a212642015-05-08 16:35:23 -0700738 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700739 }
Santos Cordon6a212642015-05-08 16:35:23 -0700740 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700741 }
742
Santos Cordon6a212642015-05-08 16:35:23 -0700743 //
744 // State Implementation for PhoneAccountRegistrar
745 //
746
Ihab Awad293edf22014-07-24 17:52:29 -0700747 /**
748 * The state of this {@code PhoneAccountRegistrar}.
749 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700750 @VisibleForTesting
751 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700752 /**
753 * The account selected by the user to be employed by default for making outgoing calls.
754 * If the user has not made such a selection, then this is null.
755 */
756 public PhoneAccountHandle defaultOutgoing = null;
757
758 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700759 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700760 * manages and optimizes a user's PSTN SIM connections.
761 */
762 public PhoneAccountHandle simCallManager;
763
764 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700765 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700766 */
767 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700768
769 /**
770 * The version number of the State data.
771 */
772 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700773 }
774
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700775 /**
776 * Dumps the state of the {@link CallsManager}.
777 *
778 * @param pw The {@code IndentingPrintWriter} to write the state to.
779 */
780 public void dump(IndentingPrintWriter pw) {
781 if (mState != null) {
782 pw.println("xmlVersion: " + mState.versionNumber);
783 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
784 mState.defaultOutgoing));
785 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
786 mState.simCallManager));
787 pw.println("phoneAccounts:");
788 pw.increaseIndent();
789 for (PhoneAccount phoneAccount : mState.accounts) {
790 pw.println(phoneAccount);
791 }
792 pw.decreaseIndent();
793 }
794 }
795
Ihab Awad293edf22014-07-24 17:52:29 -0700796 ////////////////////////////////////////////////////////////////////////////////////////////////
797 //
798 // State management
799 //
800
801 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700802 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700803 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700804 os = mAtomicFile.startWrite();
805 boolean success = false;
806 try {
807 XmlSerializer serializer = new FastXmlSerializer();
808 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800809 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700810 serializer.flush();
811 success = true;
812 } finally {
813 if (success) {
814 mAtomicFile.finishWrite(os);
815 } else {
816 mAtomicFile.failWrite(os);
817 }
818 }
819 } catch (IOException e) {
820 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700821 }
822 }
823
Ihab Awadb78b2762014-07-25 15:16:23 -0700824 private void read() {
825 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700826 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700827 is = mAtomicFile.openRead();
828 } catch (FileNotFoundException ex) {
829 return;
830 }
831
Tyler Gunn84253572014-09-02 14:50:05 -0700832 boolean versionChanged = false;
833
Ihab Awadb78b2762014-07-25 15:16:23 -0700834 XmlPullParser parser;
835 try {
836 parser = Xml.newPullParser();
837 parser.setInput(new BufferedInputStream(is), null);
838 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700839 mState = readFromXml(parser, mContext);
840 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
841
Ihab Awadb78b2762014-07-25 15:16:23 -0700842 } catch (IOException | XmlPullParserException e) {
843 Log.e(this, e, "Reading state from XML file");
844 mState = new State();
845 } finally {
846 try {
847 is.close();
848 } catch (IOException e) {
849 Log.e(this, e, "Closing InputStream");
850 }
Ihab Awad104f8062014-07-17 11:29:35 -0700851 }
Tyler Gunn84253572014-09-02 14:50:05 -0700852
Evan Charlton105d9772014-11-25 14:08:53 -0800853 // Verify all of the UserHandles.
854 List<PhoneAccount> badAccounts = new ArrayList<>();
855 for (PhoneAccount phoneAccount : mState.accounts) {
856 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
857 if (userHandle == null) {
858 Log.w(this, "Missing UserHandle for %s", phoneAccount);
859 badAccounts.add(phoneAccount);
860 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
861 Log.w(this, "User does not exist for %s", phoneAccount);
862 badAccounts.add(phoneAccount);
863 }
864 }
865 mState.accounts.removeAll(badAccounts);
866
Tyler Gunn84253572014-09-02 14:50:05 -0700867 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800868 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700869 write();
870 }
Santos Cordon176ae282014-07-14 02:02:14 -0700871 }
872
Evan Charlton105d9772014-11-25 14:08:53 -0800873 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700874 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800875 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700876 }
Ihab Awad104f8062014-07-17 11:29:35 -0700877
Tyler Gunn84253572014-09-02 14:50:05 -0700878 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700879 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700880 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700881 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700882 }
883
Ihab Awad293edf22014-07-24 17:52:29 -0700884 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700885 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700886 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700887 //
888
Ihab Awadb78b2762014-07-25 15:16:23 -0700889 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700890 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700891 private static final String LENGTH_ATTRIBUTE = "length";
892 private static final String VALUE_TAG = "value";
893
Ihab Awadb78b2762014-07-25 15:16:23 -0700894 /**
895 * Write the supplied object to XML
896 */
Evan Charlton105d9772014-11-25 14:08:53 -0800897 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700898 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700899
900 /**
901 * Read from the supplied XML into a new object, returning null in case of an
902 * unrecoverable schema mismatch or other data error. 'parser' must be already
903 * positioned at the first tag that is expected to have been emitted by this
904 * object's writeToXml(). This object tries to fail early without modifying
905 * 'parser' if it does not recognize the data it sees.
906 */
Tyler Gunn84253572014-09-02 14:50:05 -0700907 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700908 throws IOException, XmlPullParserException;
909
Ihab Awadd9f54382014-10-24 11:44:47 -0700910 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700911 throws IOException {
912 if (value != null) {
913 serializer.startTag(null, tagName);
914 serializer.text(Objects.toString(value));
915 serializer.endTag(null, tagName);
916 }
917 }
Tyler Gunn84253572014-09-02 14:50:05 -0700918
919 /**
920 * Serializes a string array.
921 *
922 * @param tagName The tag name for the string array.
923 * @param values The string values to serialize.
924 * @param serializer The serializer.
925 * @throws IOException
926 */
927 protected void writeStringList(String tagName, List<String> values,
928 XmlSerializer serializer)
929 throws IOException {
930
931 serializer.startTag(null, tagName);
932 if (values != null) {
933 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
934 for (String toSerialize : values) {
935 serializer.startTag(null, VALUE_TAG);
936 if (toSerialize != null ){
937 serializer.text(toSerialize);
938 }
Tyler Gunn84253572014-09-02 14:50:05 -0700939 serializer.endTag(null, VALUE_TAG);
940 }
941 } else {
942 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
943 }
944 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700945 }
Tyler Gunn84253572014-09-02 14:50:05 -0700946
Santos Cordon9c30c282015-05-13 16:28:27 -0700947 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -0700948 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -0700949 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700950 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -0700951 value.writeToStream(stream);
952 byte[] iconByteArray = stream.toByteArray();
953 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -0700954
955 serializer.startTag(null, tagName);
956 serializer.text(text);
957 serializer.endTag(null, tagName);
958 }
Tyler Gunn84253572014-09-02 14:50:05 -0700959 }
960
Evan Charlton105d9772014-11-25 14:08:53 -0800961 protected void writeLong(String tagName, long value, XmlSerializer serializer)
962 throws IOException {
963 serializer.startTag(null, tagName);
964 serializer.text(Long.valueOf(value).toString());
965 serializer.endTag(null, tagName);
966 }
967
Tyler Gunn84253572014-09-02 14:50:05 -0700968 /**
969 * Reads a string array from the XML parser.
970 *
971 * @param parser The XML parser.
972 * @return String array containing the parsed values.
973 * @throws IOException Exception related to IO.
974 * @throws XmlPullParserException Exception related to parsing.
975 */
976 protected List<String> readStringList(XmlPullParser parser)
977 throws IOException, XmlPullParserException {
978
979 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
980 List<String> arrayEntries = new ArrayList<String>(length);
981 String value = null;
982
983 if (length == 0) {
984 return arrayEntries;
985 }
986
987 int outerDepth = parser.getDepth();
988 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
989 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700990 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700991 value = parser.getText();
992 arrayEntries.add(value);
993 }
994 }
995
996 return arrayEntries;
997 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700998
Santos Cordon9c30c282015-05-13 16:28:27 -0700999 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001000 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1001 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1002 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001003
1004 protected Icon readIcon(XmlPullParser parser) throws IOException {
1005 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1006 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1007 return Icon.createFromStream(stream);
1008 }
Ihab Awad104f8062014-07-17 11:29:35 -07001009 }
1010
Ihab Awadb78b2762014-07-25 15:16:23 -07001011 @VisibleForTesting
1012 public static final XmlSerialization<State> sStateXml =
1013 new XmlSerialization<State>() {
1014 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001015 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -07001016 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -07001017 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001018 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001019
1020 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001021 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001022 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001023 if (o != null) {
1024 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001025 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001026
Ihab Awad26923222014-07-30 10:54:35 -07001027 if (o.defaultOutgoing != null) {
1028 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001029 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001030 serializer.endTag(null, DEFAULT_OUTGOING);
1031 }
1032
1033 if (o.simCallManager != null) {
1034 serializer.startTag(null, SIM_CALL_MANAGER);
Evan Charlton105d9772014-11-25 14:08:53 -08001035 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001036 serializer.endTag(null, SIM_CALL_MANAGER);
1037 }
1038
1039 serializer.startTag(null, ACCOUNTS);
1040 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001041 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001042 }
1043 serializer.endTag(null, ACCOUNTS);
1044
1045 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001046 }
Ihab Awad104f8062014-07-17 11:29:35 -07001047 }
1048
1049 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001050 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001051 throws IOException, XmlPullParserException {
1052 if (parser.getName().equals(CLASS_STATE)) {
1053 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001054
1055 String rawVersion = parser.getAttributeValue(null, VERSION);
1056 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1057 Integer.parseInt(rawVersion);
1058
Ihab Awadb78b2762014-07-25 15:16:23 -07001059 int outerDepth = parser.getDepth();
1060 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1061 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1062 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001063 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1064 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001065 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
1066 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001067 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
1068 s.versionNumber, context);
Evan Charlton105d9772014-11-25 14:08:53 -08001069 if (s.simCallManager.getUserHandle() == null) {
1070 // This should never happen, but handle the upgrade case.
1071 s.simCallManager = new PhoneAccountHandle(
1072 s.simCallManager.getComponentName(),
1073 s.simCallManager.getId(),
1074 Process.myUserHandle());
1075 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001076 } else if (parser.getName().equals(ACCOUNTS)) {
1077 int accountsDepth = parser.getDepth();
1078 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001079 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1080 s.versionNumber, context);
1081
1082 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001083 s.accounts.add(account);
1084 }
1085 }
Ihab Awad104f8062014-07-17 11:29:35 -07001086 }
1087 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001088 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001089 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001090 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001091 }
1092 };
1093
Ihab Awadb78b2762014-07-25 15:16:23 -07001094 @VisibleForTesting
1095 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1096 new XmlSerialization<PhoneAccount>() {
1097 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1098 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001099 private static final String ADDRESS = "handle";
1100 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001101 private static final String CAPABILITIES = "capabilities";
1102 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001103 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1104 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001105 private static final String ICON_TINT = "icon_tint";
1106 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001107 private static final String LABEL = "label";
1108 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001109 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001110 private static final String ICON = "icon";
Santos Cordonea5cb932015-05-07 16:28:38 -07001111 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001112
1113 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001114 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001115 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001116 if (o != null) {
1117 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001118
Ihab Awad26923222014-07-30 10:54:35 -07001119 if (o.getAccountHandle() != null) {
1120 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001121 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001122 serializer.endTag(null, ACCOUNT_HANDLE);
1123 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001124
Ihab Awadd9f54382014-10-24 11:44:47 -07001125 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1126 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1127 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001128 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001129 writeTextIfNonNull(HIGHLIGHT_COLOR,
1130 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001131 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1132 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001133 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001134 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001135
Ihab Awad26923222014-07-30 10:54:35 -07001136 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1137 }
Ihab Awad104f8062014-07-17 11:29:35 -07001138 }
1139
Tyler Gunn84253572014-09-02 14:50:05 -07001140 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001141 throws IOException, XmlPullParserException {
1142 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1143 int outerDepth = parser.getDepth();
1144 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001145 Uri address = null;
1146 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001147 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001148 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001149 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001150 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001151 int iconTint = PhoneAccount.NO_ICON_TINT;
1152 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001153 String label = null;
1154 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001155 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001156 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001157 boolean enabled = false;
Ihab Awadb78b2762014-07-25 15:16:23 -07001158
1159 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1160 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1161 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001162 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1163 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001164 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001165 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001166 address = Uri.parse(parser.getText());
1167 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001168 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001169 String nextText = parser.getText();
1170 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001171 } else if (parser.getName().equals(CAPABILITIES)) {
1172 parser.next();
1173 capabilities = Integer.parseInt(parser.getText());
1174 } else if (parser.getName().equals(ICON_RES_ID)) {
1175 parser.next();
1176 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001177 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1178 parser.next();
1179 iconPackageName = parser.getText();
1180 } else if (parser.getName().equals(ICON_BITMAP)) {
1181 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001182 iconBitmap = readBitmap(parser);
1183 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001184 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001185 iconTint = Integer.parseInt(parser.getText());
1186 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1187 parser.next();
1188 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001189 } else if (parser.getName().equals(LABEL)) {
1190 parser.next();
1191 label = parser.getText();
1192 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1193 parser.next();
1194 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001195 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1196 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001197 } else if (parser.getName().equals(ICON)) {
1198 parser.next();
1199 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001200 } else if (parser.getName().equals(ENABLED)) {
1201 parser.next();
1202 enabled = "true".equalsIgnoreCase(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001203 }
1204 }
Tyler Gunn84253572014-09-02 14:50:05 -07001205
Santos Cordona9eebe42015-06-11 14:07:44 -07001206 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1207 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001208 ComponentName sipComponentName = new ComponentName("com.android.phone",
1209 "com.android.services.telephony.sip.SipConnectionService");
1210
Tyler Gunn84253572014-09-02 14:50:05 -07001211 // Upgrade older phone accounts to specify the supported URI schemes.
1212 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001213 supportedUriSchemes = new ArrayList<>();
1214
1215 // Handle the SIP connection service.
1216 // Check the system settings to see if it also should handle "tel" calls.
1217 if (accountHandle.getComponentName().equals(sipComponentName)) {
1218 boolean useSipForPstn = useSipForPstnCalls(context);
1219 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1220 if (useSipForPstn) {
1221 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1222 }
1223 } else {
1224 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1225 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1226 }
1227 }
1228
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001229 // Upgrade older phone accounts with explicit package name
1230 if (version < 5) {
1231 if (iconBitmap == null) {
1232 iconPackageName = accountHandle.getComponentName().getPackageName();
1233 }
1234 }
1235
Santos Cordona82aed82015-05-26 10:43:56 -07001236 if (version < 6) {
1237 // Always enable all SIP accounts on upgrade to version 6
1238 if (accountHandle.getComponentName().equals(sipComponentName)) {
1239 enabled = true;
1240 }
1241 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001242 if (version < 7) {
1243 // Always enabled all PSTN acocunts on upgrade to version 7
1244 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1245 enabled = true;
1246 }
1247 }
Santos Cordona82aed82015-05-26 10:43:56 -07001248
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001249 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001250 .setAddress(address)
1251 .setSubscriptionAddress(subscriptionAddress)
1252 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001253 .setShortDescription(shortDescription)
1254 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001255 .setHighlightColor(highlightColor)
1256 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001257
Santos Cordon9c30c282015-05-13 16:28:27 -07001258 if (icon != null) {
1259 builder.setIcon(icon);
1260 } else if (iconBitmap != null) {
1261 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1262 } else if (!TextUtils.isEmpty(iconPackageName)) {
1263 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1264 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001265 }
1266
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001267 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001268 }
1269 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001270 }
Tyler Gunn84253572014-09-02 14:50:05 -07001271
1272 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001273 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1274 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001275 *
1276 * @param context The context.
1277 * @return {@code True} if SIP should be used for all calls.
1278 */
1279 private boolean useSipForPstnCalls(Context context) {
1280 String option = Settings.System.getString(context.getContentResolver(),
1281 Settings.System.SIP_CALL_OPTIONS);
1282 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1283 return option.equals(Settings.System.SIP_ALWAYS);
1284 }
Ihab Awad104f8062014-07-17 11:29:35 -07001285 };
1286
Ihab Awadb78b2762014-07-25 15:16:23 -07001287 @VisibleForTesting
1288 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1289 new XmlSerialization<PhoneAccountHandle>() {
1290 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001291 private static final String COMPONENT_NAME = "component_name";
1292 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001293 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001294
1295 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001296 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001297 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001298 if (o != null) {
1299 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001300
Ihab Awad26923222014-07-30 10:54:35 -07001301 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001302 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001303 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1304 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001305
Ihab Awadd9f54382014-10-24 11:44:47 -07001306 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001307
Evan Charlton105d9772014-11-25 14:08:53 -08001308 if (o.getUserHandle() != null && context != null) {
1309 UserManager userManager = UserManager.get(context);
1310 writeLong(USER_SERIAL_NUMBER,
1311 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1312 }
1313
Ihab Awad26923222014-07-30 10:54:35 -07001314 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1315 }
Ihab Awad104f8062014-07-17 11:29:35 -07001316 }
1317
1318 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001319 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001320 throws IOException, XmlPullParserException {
1321 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1322 String componentNameString = null;
1323 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001324 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001325 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001326
1327 UserManager userManager = UserManager.get(context);
1328
Ihab Awadb78b2762014-07-25 15:16:23 -07001329 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1330 if (parser.getName().equals(COMPONENT_NAME)) {
1331 parser.next();
1332 componentNameString = parser.getText();
1333 } else if (parser.getName().equals(ID)) {
1334 parser.next();
1335 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001336 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1337 parser.next();
1338 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001339 }
1340 }
Ihab Awad26923222014-07-30 10:54:35 -07001341 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001342 UserHandle userHandle = null;
1343 if (userSerialNumberString != null) {
1344 try {
1345 long serialNumber = Long.parseLong(userSerialNumberString);
1346 userHandle = userManager.getUserForSerialNumber(serialNumber);
1347 } catch (NumberFormatException e) {
1348 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1349 }
1350 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001351 return new PhoneAccountHandle(
1352 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001353 idString,
1354 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001355 }
1356 }
1357 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001358 }
1359 };
Santos Cordon176ae282014-07-14 02:02:14 -07001360}