blob: fbc36085020453c8b3c20ccd35ee85d8e1835813 [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;
Santos Cordon59c21a72015-06-11 10:11:21 -070038import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070039import android.telecom.PhoneAccount;
40import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070041import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070042import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070043import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080044import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070045import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070046import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070047import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070048import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070049
Tyler Gunn91d43cf2014-09-17 12:19:39 -070050// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070051import com.android.internal.annotations.VisibleForTesting;
52import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070053import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070054import com.android.internal.util.XmlUtils;
55
Evan Charltonaf51ceb2014-07-30 11:56:36 -070056import org.xmlpull.v1.XmlPullParser;
57import org.xmlpull.v1.XmlPullParserException;
58import org.xmlpull.v1.XmlSerializer;
59
Ihab Awadb78b2762014-07-25 15:16:23 -070060import java.io.BufferedInputStream;
61import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070062import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070063import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070064import java.io.File;
65import java.io.FileNotFoundException;
66import java.io.FileOutputStream;
67import java.io.IOException;
68import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070069import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070070import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070071import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070072import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080073import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070074import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070075import java.util.List;
76import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070077import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070078
79/**
Evan Charlton89176372014-07-19 18:23:09 -070080 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070081 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
82 * implemented in {@link TelecomServiceImpl}, with the notable exception that
83 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
84 * proper authority over the {@code ComponentName}s they are declaring in their
85 * {@code PhoneAccountHandle}s.
86 *
87 *
88 * -- About Users and Phone Accounts --
89 *
Santos Cordon9c30c282015-05-13 16:28:27 -070090 * We store all phone accounts for all users in a single place, which means that there are three
91 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070092 * 1) The Android User that is currently active on the device.
93 * 2) The user which owns/registers the phone account.
94 * 3) The user running the app that is requesting the phone account information.
95 *
96 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
97 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
98 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -070099 * account list. Lets also say that an app running in the work profile has registered a phone
100 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -0700101 *
102 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700103 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
104 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700105 *
106 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
107 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
108 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700109 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700110public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700111
Yorke Lee5e8836a2014-08-22 15:25:18 -0700112 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
113 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
114
Ihab Awadb78b2762014-07-25 15:16:23 -0700115 public abstract static class Listener {
116 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
117 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
118 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
119 }
120
121 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700122 @VisibleForTesting
Santos Cordona9eebe42015-06-11 14:07:44 -0700123 public static final int EXPECTED_STATE_VERSION = 7;
Tyler Gunn84253572014-09-02 14:50:05 -0700124
125 /** Keep in sync with the same in SipSettings.java */
126 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700127
128 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
129 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700130 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800131 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800132 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700133 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800134 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700135
Nancy Chen06ce0622014-10-23 01:17:35 +0000136 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700137 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700138 this(context, FILE_NAME);
139 }
140
141 @VisibleForTesting
142 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700143 // TODO: This file path is subject to change -- it is storing the phone account registry
144 // state file in the path /data/system/users/0/, which is likely not correct in a
145 // multi-user setting.
146 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
147 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
148 getAbsolutePath();
149 mAtomicFile = new AtomicFile(new File(filePath, fileName));
150 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700151 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700152
Ihab Awadb78b2762014-07-25 15:16:23 -0700153 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700154 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800155 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800156 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800157 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700158 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700159 }
160
Tyler Gunn84253572014-09-02 14:50:05 -0700161 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700162 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
163 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
164 * subscription id.
165 * @param accountHandle The handle for the phone account for which to retrieve the
166 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700167 * @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 -0700168 */
Wink Saville35850602014-10-23 15:57:21 -0700169 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700170 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800171
Santos Cordon6a212642015-05-08 16:35:23 -0700172 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800173 TelephonyManager tm =
174 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
175 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700176 }
Santos Cordon479b3022015-02-06 04:27:15 -0800177 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700178 }
179
180 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800181 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
182 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
183 * will be returned.
184 *
Tyler Gunn84253572014-09-02 14:50:05 -0700185 * @param uriScheme The URI scheme for the outgoing call.
186 * @return The {@link PhoneAccountHandle} to use.
187 */
Santos Cordon6a212642015-05-08 16:35:23 -0700188 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700189 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700190
Yorke Lee5e8836a2014-08-22 15:25:18 -0700191 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700192 // If there is a default PhoneAccount, ensure it supports calls to handles with the
193 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700194 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
195 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700196 return userSelected;
197 }
Ihab Awad293edf22014-07-24 17:52:29 -0700198 }
199
Santos Cordonea5cb932015-05-07 16:28:38 -0700200 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700201 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700202 case 0:
203 // There are no accounts, so there can be no default
204 return null;
205 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800206 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700207 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700208 default:
209 // There are multiple accounts with no selected default
210 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700211 }
Ihab Awad104f8062014-07-17 11:29:35 -0700212 }
Santos Cordon176ae282014-07-14 02:02:14 -0700213
Evan Charlton105d9772014-11-25 14:08:53 -0800214 /**
215 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
216 * if it was set by another user).
217 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700218 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700219 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
220 if (account != null) {
221 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700222 }
223 return null;
224 }
225
Santos Cordon6a212642015-05-08 16:35:23 -0700226 /**
227 * Sets the phone account with which to place all calls by default. Set by the user
228 * within phone settings.
229 */
Andrew Leea51a3862014-09-03 14:58:45 -0700230 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700231 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700232 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700233 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700234 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700235 // TODO: Do we really want to return for *any* user?
236 PhoneAccount account = getPhoneAccount(accountHandle);
237 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700238 Log.w(this, "Trying to set nonexistent default outgoing %s",
239 accountHandle);
240 return;
241 }
242
Santos Cordon6a212642015-05-08 16:35:23 -0700243 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700244 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700245 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700246 return;
247 }
248
Santos Cordon6a212642015-05-08 16:35:23 -0700249 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700250 // If the account selected is a SIM account, propagate down to the subscription
251 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700252 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800253 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700254 }
255
Ihab Awad293edf22014-07-24 17:52:29 -0700256 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700257 }
258
Ihab Awad293edf22014-07-24 17:52:29 -0700259 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700260 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700261 }
262
Nancy Chen668dee02014-11-19 15:31:31 -0800263 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
264 return getSubscriptionIdForPhoneAccount(accountHandle) ==
265 SubscriptionManager.getDefaultSmsSubId();
266 }
267
Evan Charlton105d9772014-11-25 14:08:53 -0800268 /**
Santos Cordon59c21a72015-06-11 10:11:21 -0700269 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
270 * Manager. SIM Call Manager returned corresponds to the following priority order:
271 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
272 * default dialer, then that one is returned.
273 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
274 * carrier configuration's default, then that one is returned.
275 * 3. Otherwise, we return null.
Evan Charlton105d9772014-11-25 14:08:53 -0800276 */
Ihab Awad293edf22014-07-24 17:52:29 -0700277 public PhoneAccountHandle getSimCallManager() {
Santos Cordon59c21a72015-06-11 10:11:21 -0700278 // Get the default dialer in case it has a connection manager associated with it.
279 String dialerPackage = DefaultDialerManager.getDefaultDialerApplication(mContext);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700280
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700281 // Check carrier config.
282 String defaultSimCallManager = null;
283 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
284 Context.CARRIER_CONFIG_SERVICE);
285 PersistableBundle configBundle = configManager.getConfig();
286 if (configBundle != null) {
287 defaultSimCallManager = configBundle.getString(
288 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
289 }
290
Santos Cordon59c21a72015-06-11 10:11:21 -0700291 ComponentName systemSimCallManagerComponent = TextUtils.isEmpty(defaultSimCallManager) ?
292 null : ComponentName.unflattenFromString(defaultSimCallManager);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800293
Santos Cordon59c21a72015-06-11 10:11:21 -0700294 PhoneAccountHandle dialerSimCallManager = null;
295 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800296
Santos Cordon59c21a72015-06-11 10:11:21 -0700297 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
298 // loop through and look for any connection manager in the same package.
299 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
300 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
301 true /* includeDisabledAccounts */);
302 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
303 ComponentName component = accountHandle.getComponentName();
304
305 // Store the system connection manager if found
306 if (systemSimCallManager == null
307 && Objects.equals(component, systemSimCallManagerComponent)
308 && !resolveComponent(accountHandle).isEmpty()) {
309 systemSimCallManager = accountHandle;
310
311 // Store the dialer connection manager if found
312 } else if (dialerSimCallManager == null
313 && Objects.equals(component.getPackageName(), dialerPackage)
314 && !resolveComponent(accountHandle).isEmpty()) {
315 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700316 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700317 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700318 }
319
Santos Cordon59c21a72015-06-11 10:11:21 -0700320 PhoneAccountHandle retval = dialerSimCallManager != null ?
321 dialerSimCallManager : systemSimCallManager;
322
323 Log.i(this, "SimCallManager queried, returning: %s", retval);
324
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700325 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700326 }
327
Evan Charlton105d9772014-11-25 14:08:53 -0800328 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800329 * Update the current UserHandle to track when users are switched. This will allow the
330 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
331 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700332 * We cannot simply check the calling user because that would always return the primary user for
333 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800334 *
335 * @param userHandle The {@link UserHandle}, as delivered by
336 * {@link Intent#ACTION_USER_SWITCHED}.
337 */
338 public void setCurrentUserHandle(UserHandle userHandle) {
339 if (userHandle == null) {
340 Log.d(this, "setCurrentUserHandle, userHandle = null");
341 userHandle = Process.myUserHandle();
342 }
343 Log.d(this, "setCurrentUserHandle, %s", userHandle);
344 mCurrentUserHandle = userHandle;
345 }
346
Yorke Lee71734c22015-06-02 14:22:56 -0700347 /**
348 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
349 * otherwise.
350 */
351 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700352 PhoneAccount account = getPhoneAccount(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700353 if (account == null) {
354 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700355 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700356 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700357 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700358 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700359 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700360 }
361
Santos Cordon638f05c2015-05-19 17:12:33 -0700362 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700363 account.setIsEnabled(isEnabled);
364 write();
365 fireAccountsChanged();
366 }
Yorke Lee71734c22015-06-02 14:22:56 -0700367 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700368 }
369
Evan Charlton105d9772014-11-25 14:08:53 -0800370 private boolean isVisibleForUser(PhoneAccount account) {
371 if (account == null) {
372 return false;
373 }
374
375 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
376 // all profiles. Only Telephony and SIP accounts should have this capability.
377 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
378 return true;
379 }
380
381 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
382 if (phoneAccountUserHandle == null) {
383 return false;
384 }
385
386 if (mCurrentUserHandle == null) {
387 Log.d(this, "Current user is null; assuming true");
388 return true;
389 }
390
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800391 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
392 return true;
393 }
394
Santos Cordon6a212642015-05-08 16:35:23 -0700395 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800396 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
397 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
398 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800399 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
400 List<UserInfo> profileUsers =
401 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
402 for (UserInfo profileInfo : profileUsers) {
403 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
404 return true;
405 }
Evan Charlton105d9772014-11-25 14:08:53 -0800406 }
407 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800408
Evan Charlton105d9772014-11-25 14:08:53 -0800409 return false;
410 }
411
412 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
413 return resolveComponent(phoneAccountHandle.getComponentName(),
414 phoneAccountHandle.getUserHandle());
415 }
416
417 private List<ResolveInfo> resolveComponent(ComponentName componentName,
418 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800419 PackageManager pm = mContext.getPackageManager();
420 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
421 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800422 try {
423 if (userHandle != null) {
424 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
425 } else {
426 return pm.queryIntentServices(intent, 0);
427 }
428 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700429 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800430 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800431 }
mike dooley10a58312014-11-06 13:46:19 -0800432 }
433
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700434 /**
435 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700436 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700437 *
438 * @return The list of {@link PhoneAccountHandle}s.
439 */
Ihab Awad293edf22014-07-24 17:52:29 -0700440 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700441 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700442 }
443
444 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700445 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700446 }
447
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700448 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700449 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700450 * specified URI scheme.
451 *
452 * @param uriScheme The URI scheme.
453 * @return The phone account handles.
454 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700455 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
456 String uriScheme, boolean includeDisabledAccounts) {
457 return getPhoneAccountHandles(
458 PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700459 }
460
461 /**
462 * Retrieves a list of all the SIM-based phone accounts.
463 */
464 public List<PhoneAccountHandle> getSimPhoneAccounts() {
465 return getPhoneAccountHandles(
466 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700467 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700468 }
469
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700470 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700471 * Retrieves a list of all phone accounts registered by a specified package.
472 *
473 * @param packageName The name of the package that registered the phone accounts.
474 * @return The phone account handles.
475 */
476 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700477 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700478 }
479
Ihab Awad104f8062014-07-17 11:29:35 -0700480 // TODO: Should we implement an artificial limit for # of accounts associated with a single
481 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700482 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700483 // Enforce the requirement that a connection service for a phone account has the correct
484 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700485 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700486 Log.w(this,
487 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700488 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700489 throw new SecurityException("PhoneAccount connection service requires "
490 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700491 }
492
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700493 addOrReplacePhoneAccount(account);
494 }
495
496 /**
497 * Adds a {@code PhoneAccount}, replacing an existing one if found.
498 *
499 * @param account The {@code PhoneAccount} to add or replace.
500 */
501 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800502 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
503 account.getAccountHandle(), account);
504
Santos Cordonea5cb932015-05-07 16:28:38 -0700505 // Start _enabled_ property as false.
506 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
507 // source app provides or else an third party app could enable itself.
508 boolean isEnabled = false;
509
Santos Cordon6a212642015-05-08 16:35:23 -0700510 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
511 if (oldAccount != null) {
512 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700513 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700514 Log.i(this, getAccountDiffString(account, oldAccount));
515 } else {
516 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700517 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700518
Santos Cordon6a212642015-05-08 16:35:23 -0700519 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700520 // Reset enabled state to whatever the value was if the account was already registered,
521 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
522 account.setIsEnabled(
523 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700524
Ihab Awad293edf22014-07-24 17:52:29 -0700525 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700526 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700527 }
528
Evan Charlton89176372014-07-19 18:23:09 -0700529 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700530 PhoneAccount account = getPhoneAccount(accountHandle);
531 if (account != null) {
532 if (mState.accounts.remove(account)) {
533 write();
534 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700535 }
536 }
Santos Cordon176ae282014-07-14 02:02:14 -0700537 }
538
Tyler Gunnd900ce62014-08-13 11:40:59 -0700539 /**
540 * Un-registers all phone accounts associated with a specified package.
541 *
542 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800543 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700544 */
Evan Charlton105d9772014-11-25 14:08:53 -0800545 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700546 boolean accountsRemoved = false;
547 Iterator<PhoneAccount> it = mState.accounts.iterator();
548 while (it.hasNext()) {
549 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800550 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
551 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
552 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700553 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
554 it.remove();
555 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700556 }
557 }
558
Tyler Gunnd900ce62014-08-13 11:40:59 -0700559 if (accountsRemoved) {
560 write();
561 fireAccountsChanged();
562 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700563 }
564
Nancy Chen140004a2014-10-15 15:48:38 -0700565 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700566 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700567 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
568 }
569
Ihab Awadb78b2762014-07-25 15:16:23 -0700570 public void addListener(Listener l) {
571 mListeners.add(l);
572 }
573
574 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700575 if (l != null) {
576 mListeners.remove(l);
577 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700578 }
579
580 private void fireAccountsChanged() {
581 for (Listener l : mListeners) {
582 l.onAccountsChanged(this);
583 }
584 }
585
586 private void fireDefaultOutgoingChanged() {
587 for (Listener l : mListeners) {
588 l.onDefaultOutgoingChanged(this);
589 }
590 }
591
592 private void fireSimCallManagerChanged() {
593 for (Listener l : mListeners) {
594 l.onSimCallManagerChanged(this);
595 }
Santos Cordon176ae282014-07-14 02:02:14 -0700596 }
597
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700598 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
599 if (account1 == null || account2 == null) {
600 return "Diff: " + account1 + ", " + account2;
601 }
602
603 StringBuffer sb = new StringBuffer();
604 sb.append("[").append(account1.getAccountHandle());
605 appendDiff(sb, "addr", account1.getAddress(), account2.getAddress());
606 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
607 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
608 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
609 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
610 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
611 appendDiff(sb, "subAddr", account1.getSubscriptionAddress(),
612 account2.getSubscriptionAddress());
613 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
614 account2.getSupportedUriSchemes());
615 sb.append("]");
616 return sb.toString();
617 }
618
619 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
620 if (!Objects.equals(obj1, obj2)) {
621 sb.append("(")
622 .append(attrName)
623 .append(": ")
624 .append(obj1)
625 .append(" -> ")
626 .append(obj2)
627 .append(")");
628 }
629 }
630
Tyler Gunncb59b672014-08-20 09:02:11 -0700631 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700632 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700633 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700634 *
635 * @param phoneAccountHandle The phone account to check.
636 * @return {@code True} if the phone account has permission.
637 */
Santos Cordon6a212642015-05-08 16:35:23 -0700638 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800639 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
640 if (resolveInfos.isEmpty()) {
641 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700642 return false;
643 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800644 for (ResolveInfo resolveInfo : resolveInfos) {
645 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700646 if (serviceInfo == null) {
647 return false;
648 }
649
650 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
651 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
652 serviceInfo.permission)) {
653 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
654 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
655 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800656 return false;
657 }
658 }
659 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700660 }
661
Santos Cordon6a212642015-05-08 16:35:23 -0700662 //
663 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
664 //
Ihab Awad293edf22014-07-24 17:52:29 -0700665
Santos Cordonafe59e52014-08-22 16:48:43 -0700666 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700667 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700668 *
Santos Cordon6a212642015-05-08 16:35:23 -0700669 * @param handle
670 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700671 */
Santos Cordon6a212642015-05-08 16:35:23 -0700672 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
673 for (PhoneAccount m : mState.accounts) {
674 if (Objects.equals(handle, m.getAccountHandle())) {
675 return m;
676 }
677 }
678 return null;
679 }
680
681 /**
682 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
683 * account before returning it. The current user is the active user on the actual android
684 * device.
685 */
686 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
687 PhoneAccount account = getPhoneAccount(handle);
688 if (account != null && isVisibleForUser(account)) {
689 return account;
690 }
691 return null;
692 }
693
694 /**
695 * Returns a list of phone account handles with the specified capabilities, uri scheme,
696 * and package name.
697 */
698 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700699 int capabilities,
700 String uriScheme,
701 String packageName,
702 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700703 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700704
705 for (PhoneAccount account : getPhoneAccounts(
706 capabilities, uriScheme, packageName, includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700707 handles.add(account.getAccountHandle());
708 }
709 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700710 }
711
712 /**
713 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700714 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700715 *
Santos Cordon6a212642015-05-08 16:35:23 -0700716 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
717 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700718 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700719 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700720 */
Santos Cordon6a212642015-05-08 16:35:23 -0700721 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700722 int capabilities,
723 String uriScheme,
724 String packageName,
725 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700726 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700727 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700728 if (!(m.isEnabled() || includeDisabledAccounts)) {
729 // Do not include disabled accounts.
730 continue;
731 }
732
Santos Cordon6a212642015-05-08 16:35:23 -0700733 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800734 // Account doesn't have the right capabilities; skip this one.
735 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700736 }
Evan Charlton105d9772014-11-25 14:08:53 -0800737 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
738 // Account doesn't support this URI scheme; skip this one.
739 continue;
740 }
Santos Cordon6a212642015-05-08 16:35:23 -0700741 PhoneAccountHandle handle = m.getAccountHandle();
742
743 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800744 // This component cannot be resolved anymore; skip this one.
745 continue;
746 }
Santos Cordon6a212642015-05-08 16:35:23 -0700747 if (packageName != null &&
748 !packageName.equals(handle.getComponentName().getPackageName())) {
749 // Not the right package name; skip this one.
750 continue;
751 }
Evan Charlton105d9772014-11-25 14:08:53 -0800752 if (!isVisibleForUser(m)) {
753 // Account is not visible for the current user; skip this one.
754 continue;
755 }
Santos Cordon6a212642015-05-08 16:35:23 -0700756 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700757 }
Santos Cordon6a212642015-05-08 16:35:23 -0700758 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700759 }
760
Santos Cordon6a212642015-05-08 16:35:23 -0700761 //
762 // State Implementation for PhoneAccountRegistrar
763 //
764
Ihab Awad293edf22014-07-24 17:52:29 -0700765 /**
766 * The state of this {@code PhoneAccountRegistrar}.
767 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700768 @VisibleForTesting
769 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700770 /**
771 * The account selected by the user to be employed by default for making outgoing calls.
772 * If the user has not made such a selection, then this is null.
773 */
774 public PhoneAccountHandle defaultOutgoing = null;
775
776 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700777 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700778 */
779 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700780
781 /**
782 * The version number of the State data.
783 */
784 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700785 }
786
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700787 /**
788 * Dumps the state of the {@link CallsManager}.
789 *
790 * @param pw The {@code IndentingPrintWriter} to write the state to.
791 */
792 public void dump(IndentingPrintWriter pw) {
793 if (mState != null) {
794 pw.println("xmlVersion: " + mState.versionNumber);
795 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
796 mState.defaultOutgoing));
Santos Cordon59c21a72015-06-11 10:11:21 -0700797 pw.println("simCallManager: " + getSimCallManager());
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700798 pw.println("phoneAccounts:");
799 pw.increaseIndent();
800 for (PhoneAccount phoneAccount : mState.accounts) {
801 pw.println(phoneAccount);
802 }
803 pw.decreaseIndent();
804 }
805 }
806
Ihab Awad293edf22014-07-24 17:52:29 -0700807 ////////////////////////////////////////////////////////////////////////////////////////////////
808 //
809 // State management
810 //
811
812 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700813 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700814 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700815 os = mAtomicFile.startWrite();
816 boolean success = false;
817 try {
818 XmlSerializer serializer = new FastXmlSerializer();
819 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800820 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700821 serializer.flush();
822 success = true;
823 } finally {
824 if (success) {
825 mAtomicFile.finishWrite(os);
826 } else {
827 mAtomicFile.failWrite(os);
828 }
829 }
830 } catch (IOException e) {
831 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700832 }
833 }
834
Ihab Awadb78b2762014-07-25 15:16:23 -0700835 private void read() {
836 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700837 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700838 is = mAtomicFile.openRead();
839 } catch (FileNotFoundException ex) {
840 return;
841 }
842
Tyler Gunn84253572014-09-02 14:50:05 -0700843 boolean versionChanged = false;
844
Ihab Awadb78b2762014-07-25 15:16:23 -0700845 XmlPullParser parser;
846 try {
847 parser = Xml.newPullParser();
848 parser.setInput(new BufferedInputStream(is), null);
849 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700850 mState = readFromXml(parser, mContext);
851 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
852
Ihab Awadb78b2762014-07-25 15:16:23 -0700853 } catch (IOException | XmlPullParserException e) {
854 Log.e(this, e, "Reading state from XML file");
855 mState = new State();
856 } finally {
857 try {
858 is.close();
859 } catch (IOException e) {
860 Log.e(this, e, "Closing InputStream");
861 }
Ihab Awad104f8062014-07-17 11:29:35 -0700862 }
Tyler Gunn84253572014-09-02 14:50:05 -0700863
Evan Charlton105d9772014-11-25 14:08:53 -0800864 // Verify all of the UserHandles.
865 List<PhoneAccount> badAccounts = new ArrayList<>();
866 for (PhoneAccount phoneAccount : mState.accounts) {
867 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
868 if (userHandle == null) {
869 Log.w(this, "Missing UserHandle for %s", phoneAccount);
870 badAccounts.add(phoneAccount);
871 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
872 Log.w(this, "User does not exist for %s", phoneAccount);
873 badAccounts.add(phoneAccount);
874 }
875 }
876 mState.accounts.removeAll(badAccounts);
877
Tyler Gunn84253572014-09-02 14:50:05 -0700878 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800879 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700880 write();
881 }
Santos Cordon176ae282014-07-14 02:02:14 -0700882 }
883
Evan Charlton105d9772014-11-25 14:08:53 -0800884 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700885 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800886 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700887 }
Ihab Awad104f8062014-07-17 11:29:35 -0700888
Tyler Gunn84253572014-09-02 14:50:05 -0700889 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700890 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700891 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700892 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700893 }
894
Ihab Awad293edf22014-07-24 17:52:29 -0700895 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700896 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700897 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700898 //
899
Ihab Awadb78b2762014-07-25 15:16:23 -0700900 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700901 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700902 private static final String LENGTH_ATTRIBUTE = "length";
903 private static final String VALUE_TAG = "value";
904
Ihab Awadb78b2762014-07-25 15:16:23 -0700905 /**
906 * Write the supplied object to XML
907 */
Evan Charlton105d9772014-11-25 14:08:53 -0800908 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700909 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700910
911 /**
912 * Read from the supplied XML into a new object, returning null in case of an
913 * unrecoverable schema mismatch or other data error. 'parser' must be already
914 * positioned at the first tag that is expected to have been emitted by this
915 * object's writeToXml(). This object tries to fail early without modifying
916 * 'parser' if it does not recognize the data it sees.
917 */
Tyler Gunn84253572014-09-02 14:50:05 -0700918 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700919 throws IOException, XmlPullParserException;
920
Ihab Awadd9f54382014-10-24 11:44:47 -0700921 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700922 throws IOException {
923 if (value != null) {
924 serializer.startTag(null, tagName);
925 serializer.text(Objects.toString(value));
926 serializer.endTag(null, tagName);
927 }
928 }
Tyler Gunn84253572014-09-02 14:50:05 -0700929
930 /**
931 * Serializes a string array.
932 *
933 * @param tagName The tag name for the string array.
934 * @param values The string values to serialize.
935 * @param serializer The serializer.
936 * @throws IOException
937 */
938 protected void writeStringList(String tagName, List<String> values,
939 XmlSerializer serializer)
940 throws IOException {
941
942 serializer.startTag(null, tagName);
943 if (values != null) {
944 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
945 for (String toSerialize : values) {
946 serializer.startTag(null, VALUE_TAG);
947 if (toSerialize != null ){
948 serializer.text(toSerialize);
949 }
Tyler Gunn84253572014-09-02 14:50:05 -0700950 serializer.endTag(null, VALUE_TAG);
951 }
952 } else {
953 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
954 }
955 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700956 }
Tyler Gunn84253572014-09-02 14:50:05 -0700957
Santos Cordon9c30c282015-05-13 16:28:27 -0700958 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -0700959 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -0700960 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -0700961 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -0700962 value.writeToStream(stream);
963 byte[] iconByteArray = stream.toByteArray();
964 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -0700965
966 serializer.startTag(null, tagName);
967 serializer.text(text);
968 serializer.endTag(null, tagName);
969 }
Tyler Gunn84253572014-09-02 14:50:05 -0700970 }
971
Evan Charlton105d9772014-11-25 14:08:53 -0800972 protected void writeLong(String tagName, long value, XmlSerializer serializer)
973 throws IOException {
974 serializer.startTag(null, tagName);
975 serializer.text(Long.valueOf(value).toString());
976 serializer.endTag(null, tagName);
977 }
978
Tyler Gunn84253572014-09-02 14:50:05 -0700979 /**
980 * Reads a string array from the XML parser.
981 *
982 * @param parser The XML parser.
983 * @return String array containing the parsed values.
984 * @throws IOException Exception related to IO.
985 * @throws XmlPullParserException Exception related to parsing.
986 */
987 protected List<String> readStringList(XmlPullParser parser)
988 throws IOException, XmlPullParserException {
989
990 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
991 List<String> arrayEntries = new ArrayList<String>(length);
992 String value = null;
993
994 if (length == 0) {
995 return arrayEntries;
996 }
997
998 int outerDepth = parser.getDepth();
999 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1000 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001001 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001002 value = parser.getText();
1003 arrayEntries.add(value);
1004 }
1005 }
1006
1007 return arrayEntries;
1008 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001009
Santos Cordon9c30c282015-05-13 16:28:27 -07001010 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001011 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1012 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1013 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001014
1015 protected Icon readIcon(XmlPullParser parser) throws IOException {
1016 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1017 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1018 return Icon.createFromStream(stream);
1019 }
Ihab Awad104f8062014-07-17 11:29:35 -07001020 }
1021
Ihab Awadb78b2762014-07-25 15:16:23 -07001022 @VisibleForTesting
1023 public static final XmlSerialization<State> sStateXml =
1024 new XmlSerialization<State>() {
1025 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001026 private static final String DEFAULT_OUTGOING = "default_outgoing";
1027 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001028 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001029
1030 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001031 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001032 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001033 if (o != null) {
1034 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001035 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001036
Ihab Awad26923222014-07-30 10:54:35 -07001037 if (o.defaultOutgoing != null) {
1038 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001039 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001040 serializer.endTag(null, DEFAULT_OUTGOING);
1041 }
1042
Ihab Awad26923222014-07-30 10:54:35 -07001043 serializer.startTag(null, ACCOUNTS);
1044 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001045 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001046 }
1047 serializer.endTag(null, ACCOUNTS);
1048
1049 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001050 }
Ihab Awad104f8062014-07-17 11:29:35 -07001051 }
1052
1053 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001054 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001055 throws IOException, XmlPullParserException {
1056 if (parser.getName().equals(CLASS_STATE)) {
1057 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001058
1059 String rawVersion = parser.getAttributeValue(null, VERSION);
1060 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1061 Integer.parseInt(rawVersion);
1062
Ihab Awadb78b2762014-07-25 15:16:23 -07001063 int outerDepth = parser.getDepth();
1064 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1065 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1066 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001067 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1068 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001069 } else if (parser.getName().equals(ACCOUNTS)) {
1070 int accountsDepth = parser.getDepth();
1071 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001072 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1073 s.versionNumber, context);
1074
1075 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001076 s.accounts.add(account);
1077 }
1078 }
Ihab Awad104f8062014-07-17 11:29:35 -07001079 }
1080 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001081 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001082 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001083 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001084 }
1085 };
1086
Ihab Awadb78b2762014-07-25 15:16:23 -07001087 @VisibleForTesting
1088 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1089 new XmlSerialization<PhoneAccount>() {
1090 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1091 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001092 private static final String ADDRESS = "handle";
1093 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001094 private static final String CAPABILITIES = "capabilities";
1095 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001096 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1097 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001098 private static final String ICON_TINT = "icon_tint";
1099 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001100 private static final String LABEL = "label";
1101 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001102 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001103 private static final String ICON = "icon";
Santos Cordonea5cb932015-05-07 16:28:38 -07001104 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001105
1106 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001107 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001108 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001109 if (o != null) {
1110 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001111
Ihab Awad26923222014-07-30 10:54:35 -07001112 if (o.getAccountHandle() != null) {
1113 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001114 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001115 serializer.endTag(null, ACCOUNT_HANDLE);
1116 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001117
Ihab Awadd9f54382014-10-24 11:44:47 -07001118 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1119 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1120 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001121 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001122 writeTextIfNonNull(HIGHLIGHT_COLOR,
1123 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001124 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1125 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001126 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001127 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001128
Ihab Awad26923222014-07-30 10:54:35 -07001129 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1130 }
Ihab Awad104f8062014-07-17 11:29:35 -07001131 }
1132
Tyler Gunn84253572014-09-02 14:50:05 -07001133 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001134 throws IOException, XmlPullParserException {
1135 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1136 int outerDepth = parser.getDepth();
1137 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001138 Uri address = null;
1139 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001140 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001141 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001142 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001143 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001144 int iconTint = PhoneAccount.NO_ICON_TINT;
1145 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001146 String label = null;
1147 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001148 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001149 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001150 boolean enabled = false;
Ihab Awadb78b2762014-07-25 15:16:23 -07001151
1152 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1153 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1154 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001155 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1156 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001157 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001158 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001159 address = Uri.parse(parser.getText());
1160 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001161 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001162 String nextText = parser.getText();
1163 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001164 } else if (parser.getName().equals(CAPABILITIES)) {
1165 parser.next();
1166 capabilities = Integer.parseInt(parser.getText());
1167 } else if (parser.getName().equals(ICON_RES_ID)) {
1168 parser.next();
1169 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001170 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1171 parser.next();
1172 iconPackageName = parser.getText();
1173 } else if (parser.getName().equals(ICON_BITMAP)) {
1174 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001175 iconBitmap = readBitmap(parser);
1176 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001177 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001178 iconTint = Integer.parseInt(parser.getText());
1179 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1180 parser.next();
1181 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001182 } else if (parser.getName().equals(LABEL)) {
1183 parser.next();
1184 label = parser.getText();
1185 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1186 parser.next();
1187 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001188 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1189 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001190 } else if (parser.getName().equals(ICON)) {
1191 parser.next();
1192 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001193 } else if (parser.getName().equals(ENABLED)) {
1194 parser.next();
1195 enabled = "true".equalsIgnoreCase(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001196 }
1197 }
Tyler Gunn84253572014-09-02 14:50:05 -07001198
Santos Cordona9eebe42015-06-11 14:07:44 -07001199 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1200 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001201 ComponentName sipComponentName = new ComponentName("com.android.phone",
1202 "com.android.services.telephony.sip.SipConnectionService");
1203
Tyler Gunn84253572014-09-02 14:50:05 -07001204 // Upgrade older phone accounts to specify the supported URI schemes.
1205 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001206 supportedUriSchemes = new ArrayList<>();
1207
1208 // Handle the SIP connection service.
1209 // Check the system settings to see if it also should handle "tel" calls.
1210 if (accountHandle.getComponentName().equals(sipComponentName)) {
1211 boolean useSipForPstn = useSipForPstnCalls(context);
1212 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1213 if (useSipForPstn) {
1214 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1215 }
1216 } else {
1217 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1218 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1219 }
1220 }
1221
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001222 // Upgrade older phone accounts with explicit package name
1223 if (version < 5) {
1224 if (iconBitmap == null) {
1225 iconPackageName = accountHandle.getComponentName().getPackageName();
1226 }
1227 }
1228
Santos Cordona82aed82015-05-26 10:43:56 -07001229 if (version < 6) {
1230 // Always enable all SIP accounts on upgrade to version 6
1231 if (accountHandle.getComponentName().equals(sipComponentName)) {
1232 enabled = true;
1233 }
1234 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001235 if (version < 7) {
1236 // Always enabled all PSTN acocunts on upgrade to version 7
1237 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1238 enabled = true;
1239 }
1240 }
Santos Cordona82aed82015-05-26 10:43:56 -07001241
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001242 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001243 .setAddress(address)
1244 .setSubscriptionAddress(subscriptionAddress)
1245 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001246 .setShortDescription(shortDescription)
1247 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001248 .setHighlightColor(highlightColor)
1249 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001250
Santos Cordon9c30c282015-05-13 16:28:27 -07001251 if (icon != null) {
1252 builder.setIcon(icon);
1253 } else if (iconBitmap != null) {
1254 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1255 } else if (!TextUtils.isEmpty(iconPackageName)) {
1256 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1257 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001258 }
1259
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001260 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001261 }
1262 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001263 }
Tyler Gunn84253572014-09-02 14:50:05 -07001264
1265 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001266 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1267 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001268 *
1269 * @param context The context.
1270 * @return {@code True} if SIP should be used for all calls.
1271 */
1272 private boolean useSipForPstnCalls(Context context) {
1273 String option = Settings.System.getString(context.getContentResolver(),
1274 Settings.System.SIP_CALL_OPTIONS);
1275 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1276 return option.equals(Settings.System.SIP_ALWAYS);
1277 }
Ihab Awad104f8062014-07-17 11:29:35 -07001278 };
1279
Ihab Awadb78b2762014-07-25 15:16:23 -07001280 @VisibleForTesting
1281 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1282 new XmlSerialization<PhoneAccountHandle>() {
1283 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001284 private static final String COMPONENT_NAME = "component_name";
1285 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001286 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001287
1288 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001289 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001290 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001291 if (o != null) {
1292 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001293
Ihab Awad26923222014-07-30 10:54:35 -07001294 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001295 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001296 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1297 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001298
Ihab Awadd9f54382014-10-24 11:44:47 -07001299 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001300
Evan Charlton105d9772014-11-25 14:08:53 -08001301 if (o.getUserHandle() != null && context != null) {
1302 UserManager userManager = UserManager.get(context);
1303 writeLong(USER_SERIAL_NUMBER,
1304 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1305 }
1306
Ihab Awad26923222014-07-30 10:54:35 -07001307 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1308 }
Ihab Awad104f8062014-07-17 11:29:35 -07001309 }
1310
1311 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001312 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001313 throws IOException, XmlPullParserException {
1314 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1315 String componentNameString = null;
1316 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001317 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001318 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001319
1320 UserManager userManager = UserManager.get(context);
1321
Ihab Awadb78b2762014-07-25 15:16:23 -07001322 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1323 if (parser.getName().equals(COMPONENT_NAME)) {
1324 parser.next();
1325 componentNameString = parser.getText();
1326 } else if (parser.getName().equals(ID)) {
1327 parser.next();
1328 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001329 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1330 parser.next();
1331 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001332 }
1333 }
Ihab Awad26923222014-07-30 10:54:35 -07001334 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001335 UserHandle userHandle = null;
1336 if (userSerialNumberString != null) {
1337 try {
1338 long serialNumber = Long.parseLong(userSerialNumberString);
1339 userHandle = userManager.getUserForSerialNumber(serialNumber);
1340 } catch (NumberFormatException e) {
1341 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1342 }
1343 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001344 return new PhoneAccountHandle(
1345 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001346 idString,
1347 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001348 }
1349 }
1350 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001351 }
1352 };
Santos Cordon176ae282014-07-14 02:02:14 -07001353}