blob: aab193ee7fc3a64145f91571a7853a3f228512bc [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
Sailesh Nepalfc43ea82015-07-28 19:30:28 -070019import android.app.ActivityManager;
Tyler Gunncb59b672014-08-20 09:02:11 -070020import android.Manifest;
Evan Charlton105d9772014-11-25 14:08:53 -080021import android.content.ComponentName;
22import android.content.Context;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070023import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070026import android.content.pm.ServiceInfo;
Evan Charlton105d9772014-11-25 14:08:53 -080027import android.content.pm.UserInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070028import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
Santos Cordon9c30c282015-05-13 16:28:27 -070030import android.graphics.drawable.Icon;
Evan Charlton105d9772014-11-25 14:08:53 -080031import android.net.Uri;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080032import android.os.Binder;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070033import android.os.PersistableBundle;
Evan Charlton105d9772014-11-25 14:08:53 -080034import android.os.Process;
35import android.os.UserHandle;
36import android.os.UserManager;
Tyler Gunn84253572014-09-02 14:50:05 -070037import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070038import android.telecom.ConnectionService;
Santos Cordon59c21a72015-06-11 10:11:21 -070039import android.telecom.DefaultDialerManager;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070040import android.telecom.PhoneAccount;
41import android.telecom.PhoneAccountHandle;
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -070042import android.telephony.CarrierConfigManager;
Nancy Chen140004a2014-10-15 15:48:38 -070043import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070044import android.telephony.SubscriptionManager;
Santos Cordon479b3022015-02-06 04:27:15 -080045import android.telephony.TelephonyManager;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070046import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070047import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070048import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070049import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070050
Tyler Gunn91d43cf2014-09-17 12:19:39 -070051// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070052import com.android.internal.annotations.VisibleForTesting;
53import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070054import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070055import com.android.internal.util.XmlUtils;
56
Evan Charltonaf51ceb2014-07-30 11:56:36 -070057import org.xmlpull.v1.XmlPullParser;
58import org.xmlpull.v1.XmlPullParserException;
59import org.xmlpull.v1.XmlSerializer;
60
Ihab Awadb78b2762014-07-25 15:16:23 -070061import java.io.BufferedInputStream;
62import java.io.BufferedOutputStream;
Santos Cordon9c30c282015-05-13 16:28:27 -070063import java.io.ByteArrayInputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070064import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070065import java.io.File;
66import java.io.FileNotFoundException;
67import java.io.FileOutputStream;
68import java.io.IOException;
69import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070070import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070071import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070072import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070073import java.util.ArrayList;
Sailesh Nepal91fc8092015-02-14 15:44:55 -080074import java.util.Collections;
Tyler Gunnd900ce62014-08-13 11:40:59 -070075import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070076import java.util.List;
77import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070078import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070079
80/**
Evan Charlton89176372014-07-19 18:23:09 -070081 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Santos Cordon6a212642015-05-08 16:35:23 -070082 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as
83 * implemented in {@link TelecomServiceImpl}, with the notable exception that
84 * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has
85 * proper authority over the {@code ComponentName}s they are declaring in their
86 * {@code PhoneAccountHandle}s.
87 *
88 *
89 * -- About Users and Phone Accounts --
90 *
Santos Cordon9c30c282015-05-13 16:28:27 -070091 * We store all phone accounts for all users in a single place, which means that there are three
92 * users that we have to deal with in code:
Santos Cordon6a212642015-05-08 16:35:23 -070093 * 1) The Android User that is currently active on the device.
94 * 2) The user which owns/registers the phone account.
95 * 3) The user running the app that is requesting the phone account information.
96 *
97 * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user
98 * has a work profile running as another user (B2). Lets say that user B opens the phone settings
99 * (not currently supported, but theoretically speaking), and phone settings queries for a phone
Santos Cordon9c30c282015-05-13 16:28:27 -0700100 * account list. Lets also say that an app running in the work profile has registered a phone
101 * account. This means that:
Santos Cordon6a212642015-05-08 16:35:23 -0700102 *
103 * Since phone settings always runs as the primary user, We have the following situation:
Santos Cordon9c30c282015-05-13 16:28:27 -0700104 * User A (settings) is requesting a list of phone accounts while the active user is User B, and
105 * that list contains a phone account for profile User B2.
Santos Cordon6a212642015-05-08 16:35:23 -0700106 *
107 * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is
108 * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these
109 * users for visibility before returning any phone accounts.
Santos Cordon176ae282014-07-14 02:02:14 -0700110 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700111public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -0700112
Yorke Lee5e8836a2014-08-22 15:25:18 -0700113 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
114 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
115
Ihab Awadb78b2762014-07-25 15:16:23 -0700116 public abstract static class Listener {
117 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
118 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
119 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
120 }
121
122 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -0700123 @VisibleForTesting
Roshan Pius6f752c82015-08-10 10:42:06 -0700124 public static final int EXPECTED_STATE_VERSION = 8;
Tyler Gunn84253572014-09-02 14:50:05 -0700125
126 /** Keep in sync with the same in SipSettings.java */
127 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -0700128
129 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
130 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -0700131 private final Context mContext;
Evan Charlton105d9772014-11-25 14:08:53 -0800132 private final UserManager mUserManager;
Wink Saville33c05d32014-11-20 13:04:17 -0800133 private final SubscriptionManager mSubscriptionManager;
Ihab Awadb78b2762014-07-25 15:16:23 -0700134 private State mState;
Evan Charlton105d9772014-11-25 14:08:53 -0800135 private UserHandle mCurrentUserHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700136
Nancy Chen06ce0622014-10-23 01:17:35 +0000137 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700138 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700139 this(context, FILE_NAME);
140 }
141
142 @VisibleForTesting
143 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700144 // TODO: This file path is subject to change -- it is storing the phone account registry
145 // state file in the path /data/system/users/0/, which is likely not correct in a
146 // multi-user setting.
147 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
148 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
149 getAbsolutePath();
150 mAtomicFile = new AtomicFile(new File(filePath, fileName));
151 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700152 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700153
Ihab Awadb78b2762014-07-25 15:16:23 -0700154 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700155 mContext = context;
Evan Charlton105d9772014-11-25 14:08:53 -0800156 mUserManager = UserManager.get(context);
Wink Saville33c05d32014-11-20 13:04:17 -0800157 mSubscriptionManager = SubscriptionManager.from(mContext);
Evan Charlton105d9772014-11-25 14:08:53 -0800158 mCurrentUserHandle = Process.myUserHandle();
Ihab Awadb78b2762014-07-25 15:16:23 -0700159 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700160 }
161
Tyler Gunn84253572014-09-02 14:50:05 -0700162 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700163 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
164 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
165 * subscription id.
166 * @param accountHandle The handle for the phone account for which to retrieve the
167 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700168 * @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 -0700169 */
Wink Saville35850602014-10-23 15:57:21 -0700170 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700171 PhoneAccount account = getPhoneAccountCheckCallingUser(accountHandle);
Santos Cordon479b3022015-02-06 04:27:15 -0800172
Santos Cordon6a212642015-05-08 16:35:23 -0700173 if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordon479b3022015-02-06 04:27:15 -0800174 TelephonyManager tm =
175 (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
176 return tm.getSubIdForPhoneAccount(account);
Nancy Chen140004a2014-10-15 15:48:38 -0700177 }
Santos Cordon479b3022015-02-06 04:27:15 -0800178 return SubscriptionManager.INVALID_SUBSCRIPTION_ID;
Nancy Chen140004a2014-10-15 15:48:38 -0700179 }
180
181 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800182 * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if
183 * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null}
184 * will be returned.
185 *
Tyler Gunn84253572014-09-02 14:50:05 -0700186 * @param uriScheme The URI scheme for the outgoing call.
187 * @return The {@link PhoneAccountHandle} to use.
188 */
Santos Cordon6a212642015-05-08 16:35:23 -0700189 public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700190 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700191
Yorke Lee5e8836a2014-08-22 15:25:18 -0700192 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700193 // If there is a default PhoneAccount, ensure it supports calls to handles with the
194 // specified uriScheme.
Santos Cordon6a212642015-05-08 16:35:23 -0700195 final PhoneAccount userSelectedAccount = getPhoneAccountCheckCallingUser(userSelected);
196 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700197 return userSelected;
198 }
Ihab Awad293edf22014-07-24 17:52:29 -0700199 }
200
Santos Cordonea5cb932015-05-07 16:28:38 -0700201 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700202 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700203 case 0:
204 // There are no accounts, so there can be no default
205 return null;
206 case 1:
Evan Charlton105d9772014-11-25 14:08:53 -0800207 // There is only one account, which is by definition the default.
Santos Cordon6a212642015-05-08 16:35:23 -0700208 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700209 default:
210 // There are multiple accounts with no selected default
211 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700212 }
Ihab Awad104f8062014-07-17 11:29:35 -0700213 }
Santos Cordon176ae282014-07-14 02:02:14 -0700214
Evan Charlton105d9772014-11-25 14:08:53 -0800215 /**
216 * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or
217 * if it was set by another user).
218 */
Yorke Lee5e8836a2014-08-22 15:25:18 -0700219 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
Santos Cordon6a212642015-05-08 16:35:23 -0700220 PhoneAccount account = getPhoneAccountCheckCallingUser(mState.defaultOutgoing);
221 if (account != null) {
222 return mState.defaultOutgoing;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700223 }
224 return null;
225 }
226
Santos Cordon6a212642015-05-08 16:35:23 -0700227 /**
228 * Sets the phone account with which to place all calls by default. Set by the user
229 * within phone settings.
230 */
Andrew Leea51a3862014-09-03 14:58:45 -0700231 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700232 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700233 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700234 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700235 } else {
Santos Cordon6a212642015-05-08 16:35:23 -0700236 // TODO: Do we really want to return for *any* user?
237 PhoneAccount account = getPhoneAccount(accountHandle);
238 if (account == null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700239 Log.w(this, "Trying to set nonexistent default outgoing %s",
240 accountHandle);
241 return;
242 }
243
Santos Cordon6a212642015-05-08 16:35:23 -0700244 if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700245 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700246 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700247 return;
248 }
249
Santos Cordon6a212642015-05-08 16:35:23 -0700250 if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700251 // If the account selected is a SIM account, propagate down to the subscription
252 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700253 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Wink Saville33c05d32014-11-20 13:04:17 -0800254 mSubscriptionManager.setDefaultVoiceSubId(subId);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700255 }
256
Ihab Awad293edf22014-07-24 17:52:29 -0700257 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700258 }
259
Ihab Awad293edf22014-07-24 17:52:29 -0700260 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700261 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700262 }
263
Nancy Chen668dee02014-11-19 15:31:31 -0800264 boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) {
265 return getSubscriptionIdForPhoneAccount(accountHandle) ==
266 SubscriptionManager.getDefaultSmsSubId();
267 }
268
Evan Charlton105d9772014-11-25 14:08:53 -0800269 /**
Santos Cordon59c21a72015-06-11 10:11:21 -0700270 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
271 * Manager. SIM Call Manager returned corresponds to the following priority order:
272 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
273 * default dialer, then that one is returned.
274 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
275 * carrier configuration's default, then that one is returned.
276 * 3. Otherwise, we return null.
Evan Charlton105d9772014-11-25 14:08:53 -0800277 */
Ihab Awad293edf22014-07-24 17:52:29 -0700278 public PhoneAccountHandle getSimCallManager() {
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700279 long token = Binder.clearCallingIdentity();
280 int user;
281 try {
282 user = ActivityManager.getCurrentUser();
283 } finally {
284 Binder.restoreCallingIdentity(token);
285 }
286 return getSimCallManager(user);
287 }
288
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700289 public ComponentName getSystemSimCallManagerComponent() {
290 String defaultSimCallManager = null;
291 CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService(
292 Context.CARRIER_CONFIG_SERVICE);
293 PersistableBundle configBundle = configManager.getConfig();
294 if (configBundle != null) {
295 defaultSimCallManager = configBundle.getString(
296 CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING);
297 }
298 return TextUtils.isEmpty(defaultSimCallManager)
299 ? null : ComponentName.unflattenFromString(defaultSimCallManager);
300 }
301
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700302 /**
303 * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call
304 * Manager. SIM Call Manager returned corresponds to the following priority order:
305 * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the
306 * default dialer, then that one is returned.
307 * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the
308 * carrier configuration's default, then that one is returned.
309 * 3. Otherwise, we return null.
310 */
311 public PhoneAccountHandle getSimCallManager(int user) {
Santos Cordon59c21a72015-06-11 10:11:21 -0700312 // Get the default dialer in case it has a connection manager associated with it.
Sailesh Nepalfc43ea82015-07-28 19:30:28 -0700313 String dialerPackage = DefaultDialerManager.getDefaultDialerApplication(mContext, user);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700314
Sailesh Nepalaecfa9b2015-06-16 14:55:27 -0700315 // Check carrier config.
Sailesh Nepal8ed7eeb2015-09-08 12:30:13 -0700316 ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent();
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800317
Santos Cordon59c21a72015-06-11 10:11:21 -0700318 PhoneAccountHandle dialerSimCallManager = null;
319 PhoneAccountHandle systemSimCallManager = null;
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800320
Santos Cordon59c21a72015-06-11 10:11:21 -0700321 if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) {
322 // loop through and look for any connection manager in the same package.
323 List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles(
324 PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null,
325 true /* includeDisabledAccounts */);
326 for (PhoneAccountHandle accountHandle : allSimCallManagers) {
327 ComponentName component = accountHandle.getComponentName();
328
329 // Store the system connection manager if found
330 if (systemSimCallManager == null
331 && Objects.equals(component, systemSimCallManagerComponent)
332 && !resolveComponent(accountHandle).isEmpty()) {
333 systemSimCallManager = accountHandle;
334
335 // Store the dialer connection manager if found
336 } else if (dialerSimCallManager == null
337 && Objects.equals(component.getPackageName(), dialerPackage)
338 && !resolveComponent(accountHandle).isEmpty()) {
339 dialerSimCallManager = accountHandle;
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700340 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700341 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700342 }
343
Santos Cordon59c21a72015-06-11 10:11:21 -0700344 PhoneAccountHandle retval = dialerSimCallManager != null ?
345 dialerSimCallManager : systemSimCallManager;
346
347 Log.i(this, "SimCallManager queried, returning: %s", retval);
348
Sailesh Nepalb3ccd7e2015-06-26 18:16:18 -0700349 return retval;
Ihab Awad293edf22014-07-24 17:52:29 -0700350 }
351
Evan Charlton105d9772014-11-25 14:08:53 -0800352 /**
Evan Charlton105d9772014-11-25 14:08:53 -0800353 * Update the current UserHandle to track when users are switched. This will allow the
354 * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything
355 * across users.
Santos Cordon6a212642015-05-08 16:35:23 -0700356 * We cannot simply check the calling user because that would always return the primary user for
357 * all invocations originating with the system process.
Evan Charlton105d9772014-11-25 14:08:53 -0800358 *
359 * @param userHandle The {@link UserHandle}, as delivered by
360 * {@link Intent#ACTION_USER_SWITCHED}.
361 */
362 public void setCurrentUserHandle(UserHandle userHandle) {
363 if (userHandle == null) {
364 Log.d(this, "setCurrentUserHandle, userHandle = null");
365 userHandle = Process.myUserHandle();
366 }
367 Log.d(this, "setCurrentUserHandle, %s", userHandle);
368 mCurrentUserHandle = userHandle;
369 }
370
Yorke Lee71734c22015-06-02 14:22:56 -0700371 /**
372 * @return {@code true} if the phone account was successfully enabled/disabled, {@code false}
373 * otherwise.
374 */
375 public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700376 PhoneAccount account = getPhoneAccount(accountHandle);
Santos Cordon638f05c2015-05-19 17:12:33 -0700377 if (account == null) {
378 Log.w(this, "Could not find account to enable: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700379 return false;
Santos Cordon638f05c2015-05-19 17:12:33 -0700380 } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700381 // We never change the enabled state of SIM-based accounts.
Santos Cordon638f05c2015-05-19 17:12:33 -0700382 Log.w(this, "Could not change enable state of SIM account: " + accountHandle);
Yorke Lee71734c22015-06-02 14:22:56 -0700383 return false;
Santos Cordonea5cb932015-05-07 16:28:38 -0700384 }
385
Santos Cordon638f05c2015-05-19 17:12:33 -0700386 if (account.isEnabled() != isEnabled) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700387 account.setIsEnabled(isEnabled);
388 write();
389 fireAccountsChanged();
390 }
Yorke Lee71734c22015-06-02 14:22:56 -0700391 return true;
Santos Cordonea5cb932015-05-07 16:28:38 -0700392 }
393
Evan Charlton105d9772014-11-25 14:08:53 -0800394 private boolean isVisibleForUser(PhoneAccount account) {
395 if (account == null) {
396 return false;
397 }
398
399 // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and
400 // all profiles. Only Telephony and SIP accounts should have this capability.
401 if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) {
402 return true;
403 }
404
405 UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle();
406 if (phoneAccountUserHandle == null) {
407 return false;
408 }
409
410 if (mCurrentUserHandle == null) {
411 Log.d(this, "Current user is null; assuming true");
412 return true;
413 }
414
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800415 if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) {
416 return true;
417 }
418
Santos Cordon6a212642015-05-08 16:35:23 -0700419 // Special check for work profiles.
Evan Charlton105d9772014-11-25 14:08:53 -0800420 // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure
421 // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is
422 // fine.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800423 if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) {
424 List<UserInfo> profileUsers =
425 mUserManager.getProfiles(mCurrentUserHandle.getIdentifier());
426 for (UserInfo profileInfo : profileUsers) {
427 if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) {
428 return true;
429 }
Evan Charlton105d9772014-11-25 14:08:53 -0800430 }
431 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800432
Evan Charlton105d9772014-11-25 14:08:53 -0800433 return false;
434 }
435
436 private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) {
437 return resolveComponent(phoneAccountHandle.getComponentName(),
438 phoneAccountHandle.getUserHandle());
439 }
440
441 private List<ResolveInfo> resolveComponent(ComponentName componentName,
442 UserHandle userHandle) {
mike dooley10a58312014-11-06 13:46:19 -0800443 PackageManager pm = mContext.getPackageManager();
444 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
445 intent.setComponent(componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800446 try {
447 if (userHandle != null) {
448 return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier());
449 } else {
450 return pm.queryIntentServices(intent, 0);
451 }
452 } catch (SecurityException e) {
Santos Cordon3188b362015-05-22 13:01:10 -0700453 Log.e(this, e, "%s is not visible for the calling user", componentName);
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800454 return Collections.EMPTY_LIST;
Evan Charlton105d9772014-11-25 14:08:53 -0800455 }
mike dooley10a58312014-11-06 13:46:19 -0800456 }
457
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700458 /**
459 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
Santos Cordonea5cb932015-05-07 16:28:38 -0700460 * Only returns accounts which are enabled.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700461 *
462 * @return The list of {@link PhoneAccountHandle}s.
463 */
Ihab Awad293edf22014-07-24 17:52:29 -0700464 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700465 return getPhoneAccountHandles(0, null, null, false);
Ihab Awad293edf22014-07-24 17:52:29 -0700466 }
467
468 public List<PhoneAccount> getAllPhoneAccounts() {
Santos Cordonea5cb932015-05-07 16:28:38 -0700469 return getPhoneAccounts(0, null, null, false);
Santos Cordonafe59e52014-08-22 16:48:43 -0700470 }
471
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700472 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700473 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700474 * specified URI scheme.
475 *
476 * @param uriScheme The URI scheme.
477 * @return The phone account handles.
478 */
Santos Cordonea5cb932015-05-07 16:28:38 -0700479 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(
480 String uriScheme, boolean includeDisabledAccounts) {
481 return getPhoneAccountHandles(
Bryce Leea0bb7052015-10-16 13:31:40 -0700482 PhoneAccount.CAPABILITY_CALL_PROVIDER,
483 PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/,
484 uriScheme, null, includeDisabledAccounts);
Santos Cordon6a212642015-05-08 16:35:23 -0700485 }
486
487 /**
488 * Retrieves a list of all the SIM-based phone accounts.
489 */
490 public List<PhoneAccountHandle> getSimPhoneAccounts() {
491 return getPhoneAccountHandles(
492 PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION,
Santos Cordonea5cb932015-05-07 16:28:38 -0700493 null, null, false);
Tyler Gunn84253572014-09-02 14:50:05 -0700494 }
495
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700496 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700497 * Retrieves a list of all phone accounts registered by a specified package.
498 *
499 * @param packageName The name of the package that registered the phone accounts.
500 * @return The phone account handles.
501 */
502 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700503 return getPhoneAccountHandles(0, null, packageName, false);
Nancy Chen1c5926f2014-09-17 14:44:14 -0700504 }
505
Ihab Awad104f8062014-07-17 11:29:35 -0700506 // TODO: Should we implement an artificial limit for # of accounts associated with a single
507 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700508 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700509 // Enforce the requirement that a connection service for a phone account has the correct
510 // permission.
Santos Cordon6a212642015-05-08 16:35:23 -0700511 if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) {
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700512 Log.w(this,
513 "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.",
Tyler Gunncb59b672014-08-20 09:02:11 -0700514 account.getAccountHandle());
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700515 throw new SecurityException("PhoneAccount connection service requires "
516 + "BIND_TELECOM_CONNECTION_SERVICE permission.");
Tyler Gunncb59b672014-08-20 09:02:11 -0700517 }
518
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700519 addOrReplacePhoneAccount(account);
520 }
521
522 /**
523 * Adds a {@code PhoneAccount}, replacing an existing one if found.
524 *
525 * @param account The {@code PhoneAccount} to add or replace.
526 */
527 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad7e2c7f32014-11-03 09:49:45 -0800528 Log.d(this, "addOrReplacePhoneAccount(%s -> %s)",
529 account.getAccountHandle(), account);
530
Santos Cordonea5cb932015-05-07 16:28:38 -0700531 // Start _enabled_ property as false.
532 // !!! IMPORTANT !!! It is important that we do not read the enabled state that the
533 // source app provides or else an third party app could enable itself.
534 boolean isEnabled = false;
535
Santos Cordon6a212642015-05-08 16:35:23 -0700536 PhoneAccount oldAccount = getPhoneAccount(account.getAccountHandle());
537 if (oldAccount != null) {
538 mState.accounts.remove(oldAccount);
Santos Cordonea5cb932015-05-07 16:28:38 -0700539 isEnabled = oldAccount.isEnabled();
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700540 Log.i(this, getAccountDiffString(account, oldAccount));
541 } else {
542 Log.i(this, "New phone account registered: " + account);
Santos Cordon176ae282014-07-14 02:02:14 -0700543 }
Santos Cordonea5cb932015-05-07 16:28:38 -0700544
Santos Cordon6a212642015-05-08 16:35:23 -0700545 mState.accounts.add(account);
Santos Cordonea5cb932015-05-07 16:28:38 -0700546 // Reset enabled state to whatever the value was if the account was already registered,
547 // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled.
548 account.setIsEnabled(
549 isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION));
Santos Cordon176ae282014-07-14 02:02:14 -0700550
Ihab Awad293edf22014-07-24 17:52:29 -0700551 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700552 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700553 }
554
Evan Charlton89176372014-07-19 18:23:09 -0700555 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Santos Cordon6a212642015-05-08 16:35:23 -0700556 PhoneAccount account = getPhoneAccount(accountHandle);
557 if (account != null) {
558 if (mState.accounts.remove(account)) {
559 write();
560 fireAccountsChanged();
Ihab Awad104f8062014-07-17 11:29:35 -0700561 }
562 }
Santos Cordon176ae282014-07-14 02:02:14 -0700563 }
564
Tyler Gunnd900ce62014-08-13 11:40:59 -0700565 /**
566 * Un-registers all phone accounts associated with a specified package.
567 *
568 * @param packageName The package for which phone accounts will be removed.
Evan Charlton105d9772014-11-25 14:08:53 -0800569 * @param userHandle The {@link UserHandle} the package is running under.
Tyler Gunnd900ce62014-08-13 11:40:59 -0700570 */
Evan Charlton105d9772014-11-25 14:08:53 -0800571 public void clearAccounts(String packageName, UserHandle userHandle) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700572 boolean accountsRemoved = false;
573 Iterator<PhoneAccount> it = mState.accounts.iterator();
574 while (it.hasNext()) {
575 PhoneAccount phoneAccount = it.next();
Evan Charlton105d9772014-11-25 14:08:53 -0800576 PhoneAccountHandle handle = phoneAccount.getAccountHandle();
577 if (Objects.equals(packageName, handle.getComponentName().getPackageName())
578 && Objects.equals(userHandle, handle.getUserHandle())) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700579 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530580 mState.accounts.remove(phoneAccount);
Tyler Gunnd900ce62014-08-13 11:40:59 -0700581 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700582 }
583 }
584
Tyler Gunnd900ce62014-08-13 11:40:59 -0700585 if (accountsRemoved) {
586 write();
587 fireAccountsChanged();
588 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700589 }
590
Nancy Chen140004a2014-10-15 15:48:38 -0700591 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700592 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Yorke Leeb1957232015-06-30 10:09:54 -0700593 return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number);
Nancy Chen140004a2014-10-15 15:48:38 -0700594 }
595
Ihab Awadb78b2762014-07-25 15:16:23 -0700596 public void addListener(Listener l) {
597 mListeners.add(l);
598 }
599
600 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700601 if (l != null) {
602 mListeners.remove(l);
603 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700604 }
605
606 private void fireAccountsChanged() {
607 for (Listener l : mListeners) {
608 l.onAccountsChanged(this);
609 }
610 }
611
612 private void fireDefaultOutgoingChanged() {
613 for (Listener l : mListeners) {
614 l.onDefaultOutgoingChanged(this);
615 }
616 }
617
618 private void fireSimCallManagerChanged() {
619 for (Listener l : mListeners) {
620 l.onSimCallManagerChanged(this);
621 }
Santos Cordon176ae282014-07-14 02:02:14 -0700622 }
623
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700624 private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) {
625 if (account1 == null || account2 == null) {
626 return "Diff: " + account1 + ", " + account2;
627 }
628
629 StringBuffer sb = new StringBuffer();
630 sb.append("[").append(account1.getAccountHandle());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700631 appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()),
632 Log.piiHandle(account2.getAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700633 appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities());
634 appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor());
635 appendDiff(sb, "icon", account1.getIcon(), account2.getIcon());
636 appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel());
637 appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription());
Anthony Lee7c64a4e2015-08-25 09:27:47 -0700638 appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()),
639 Log.piiHandle(account2.getSubscriptionAddress()));
Santos Cordon5fa4e4f2015-06-10 14:56:01 -0700640 appendDiff(sb, "uris", account1.getSupportedUriSchemes(),
641 account2.getSupportedUriSchemes());
642 sb.append("]");
643 return sb.toString();
644 }
645
646 private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) {
647 if (!Objects.equals(obj1, obj2)) {
648 sb.append("(")
649 .append(attrName)
650 .append(": ")
651 .append(obj1)
652 .append(" -> ")
653 .append(obj2)
654 .append(")");
655 }
656 }
657
Tyler Gunncb59b672014-08-20 09:02:11 -0700658 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700659 * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700660 * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission.
Tyler Gunncb59b672014-08-20 09:02:11 -0700661 *
662 * @param phoneAccountHandle The phone account to check.
663 * @return {@code True} if the phone account has permission.
664 */
Santos Cordon6a212642015-05-08 16:35:23 -0700665 public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) {
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800666 List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle);
667 if (resolveInfos.isEmpty()) {
668 Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName());
Tyler Gunncb59b672014-08-20 09:02:11 -0700669 return false;
670 }
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800671 for (ResolveInfo resolveInfo : resolveInfos) {
672 ServiceInfo serviceInfo = resolveInfo.serviceInfo;
Yorke Lee7bb8ce92015-05-13 16:28:29 -0700673 if (serviceInfo == null) {
674 return false;
675 }
676
677 if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) &&
678 !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals(
679 serviceInfo.permission)) {
680 // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE,
681 // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are
682 // system/signature only.
Sailesh Nepal91fc8092015-02-14 15:44:55 -0800683 return false;
684 }
685 }
686 return true;
Tyler Gunncb59b672014-08-20 09:02:11 -0700687 }
688
Santos Cordon6a212642015-05-08 16:35:23 -0700689 //
690 // Methods for retrieving PhoneAccounts and PhoneAccountHandles
691 //
Ihab Awad293edf22014-07-24 17:52:29 -0700692
Santos Cordonafe59e52014-08-22 16:48:43 -0700693 /**
Santos Cordon6a212642015-05-08 16:35:23 -0700694 * Returns the PhoneAccount for the specified handle. Does no user checking.
Tyler Gunn84253572014-09-02 14:50:05 -0700695 *
Santos Cordon6a212642015-05-08 16:35:23 -0700696 * @param handle
697 * @return The corresponding phone account if one exists.
Santos Cordonafe59e52014-08-22 16:48:43 -0700698 */
Santos Cordon6a212642015-05-08 16:35:23 -0700699 PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
700 for (PhoneAccount m : mState.accounts) {
701 if (Objects.equals(handle, m.getAccountHandle())) {
702 return m;
703 }
704 }
705 return null;
706 }
707
708 /**
709 * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone
710 * account before returning it. The current user is the active user on the actual android
711 * device.
712 */
713 public PhoneAccount getPhoneAccountCheckCallingUser(PhoneAccountHandle handle) {
714 PhoneAccount account = getPhoneAccount(handle);
715 if (account != null && isVisibleForUser(account)) {
716 return account;
717 }
718 return null;
719 }
720
Bryce Leea0bb7052015-10-16 13:31:40 -0700721 private List<PhoneAccountHandle> getPhoneAccountHandles(
722 int capabilities,
723 String uriScheme,
724 String packageName,
725 boolean includeDisabledAccounts) {
726 return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme,
727 packageName, includeDisabledAccounts);
728 }
729
Santos Cordon6a212642015-05-08 16:35:23 -0700730 /**
731 * Returns a list of phone account handles with the specified capabilities, uri scheme,
732 * and package name.
733 */
734 private List<PhoneAccountHandle> getPhoneAccountHandles(
Santos Cordonea5cb932015-05-07 16:28:38 -0700735 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700736 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700737 String uriScheme,
738 String packageName,
739 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700740 List<PhoneAccountHandle> handles = new ArrayList<>();
Santos Cordonea5cb932015-05-07 16:28:38 -0700741
742 for (PhoneAccount account : getPhoneAccounts(
Bryce Leea0bb7052015-10-16 13:31:40 -0700743 capabilities, excludedCapabilities, uriScheme, packageName,
744 includeDisabledAccounts)) {
Santos Cordon6a212642015-05-08 16:35:23 -0700745 handles.add(account.getAccountHandle());
746 }
747 return handles;
Tyler Gunn84253572014-09-02 14:50:05 -0700748 }
749
Bryce Leea0bb7052015-10-16 13:31:40 -0700750 private List<PhoneAccount> getPhoneAccounts(
751 int capabilities,
752 String uriScheme,
753 String packageName,
754 boolean includeDisabledAccounts) {
755 return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName,
756 includeDisabledAccounts);
757 }
758
Tyler Gunn84253572014-09-02 14:50:05 -0700759 /**
760 * Returns a list of phone account handles with the specified flag, supporting the specified
Santos Cordon6a212642015-05-08 16:35:23 -0700761 * URI scheme, within the specified package name.
Tyler Gunn84253572014-09-02 14:50:05 -0700762 *
Santos Cordon6a212642015-05-08 16:35:23 -0700763 * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0.
Bryce Leea0bb7052015-10-16 13:31:40 -0700764 * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have.
765 * Ignored if 0.
Santos Cordon6a212642015-05-08 16:35:23 -0700766 * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the
Tyler Gunn84253572014-09-02 14:50:05 -0700767 * URI scheme check.
Santos Cordon6a212642015-05-08 16:35:23 -0700768 * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check.
Tyler Gunn84253572014-09-02 14:50:05 -0700769 */
Santos Cordon6a212642015-05-08 16:35:23 -0700770 private List<PhoneAccount> getPhoneAccounts(
Santos Cordonea5cb932015-05-07 16:28:38 -0700771 int capabilities,
Bryce Leea0bb7052015-10-16 13:31:40 -0700772 int excludedCapabilities,
Santos Cordonea5cb932015-05-07 16:28:38 -0700773 String uriScheme,
774 String packageName,
775 boolean includeDisabledAccounts) {
Santos Cordon6a212642015-05-08 16:35:23 -0700776 List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size());
Ihab Awad293edf22014-07-24 17:52:29 -0700777 for (PhoneAccount m : mState.accounts) {
Santos Cordonea5cb932015-05-07 16:28:38 -0700778 if (!(m.isEnabled() || includeDisabledAccounts)) {
779 // Do not include disabled accounts.
780 continue;
781 }
782
Bryce Leea0bb7052015-10-16 13:31:40 -0700783 if ((m.getCapabilities() & excludedCapabilities) != 0) {
784 // If an excluded capability is present, skip.
785 continue;
786 }
787
Santos Cordon6a212642015-05-08 16:35:23 -0700788 if (capabilities != 0 && !m.hasCapabilities(capabilities)) {
Evan Charlton105d9772014-11-25 14:08:53 -0800789 // Account doesn't have the right capabilities; skip this one.
790 continue;
Ihab Awadf2a84912014-07-22 21:09:25 -0700791 }
Evan Charlton105d9772014-11-25 14:08:53 -0800792 if (uriScheme != null && !m.supportsUriScheme(uriScheme)) {
793 // Account doesn't support this URI scheme; skip this one.
794 continue;
795 }
Santos Cordon6a212642015-05-08 16:35:23 -0700796 PhoneAccountHandle handle = m.getAccountHandle();
797
798 if (resolveComponent(handle).isEmpty()) {
Evan Charlton105d9772014-11-25 14:08:53 -0800799 // This component cannot be resolved anymore; skip this one.
800 continue;
801 }
Santos Cordon6a212642015-05-08 16:35:23 -0700802 if (packageName != null &&
803 !packageName.equals(handle.getComponentName().getPackageName())) {
804 // Not the right package name; skip this one.
805 continue;
806 }
Evan Charlton105d9772014-11-25 14:08:53 -0800807 if (!isVisibleForUser(m)) {
808 // Account is not visible for the current user; skip this one.
809 continue;
810 }
Santos Cordon6a212642015-05-08 16:35:23 -0700811 accounts.add(m);
Ihab Awad104f8062014-07-17 11:29:35 -0700812 }
Santos Cordon6a212642015-05-08 16:35:23 -0700813 return accounts;
Ihab Awad104f8062014-07-17 11:29:35 -0700814 }
815
Santos Cordon6a212642015-05-08 16:35:23 -0700816 //
817 // State Implementation for PhoneAccountRegistrar
818 //
819
Ihab Awad293edf22014-07-24 17:52:29 -0700820 /**
821 * The state of this {@code PhoneAccountRegistrar}.
822 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700823 @VisibleForTesting
824 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700825 /**
826 * The account selected by the user to be employed by default for making outgoing calls.
827 * If the user has not made such a selection, then this is null.
828 */
829 public PhoneAccountHandle defaultOutgoing = null;
830
831 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700832 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700833 */
Shriram Ganesh8f89f8d2015-04-15 12:35:43 +0530834 public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700835
836 /**
837 * The version number of the State data.
838 */
839 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700840 }
841
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700842 /**
843 * Dumps the state of the {@link CallsManager}.
844 *
845 * @param pw The {@code IndentingPrintWriter} to write the state to.
846 */
847 public void dump(IndentingPrintWriter pw) {
848 if (mState != null) {
849 pw.println("xmlVersion: " + mState.versionNumber);
850 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
851 mState.defaultOutgoing));
Santos Cordon59c21a72015-06-11 10:11:21 -0700852 pw.println("simCallManager: " + getSimCallManager());
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700853 pw.println("phoneAccounts:");
854 pw.increaseIndent();
855 for (PhoneAccount phoneAccount : mState.accounts) {
856 pw.println(phoneAccount);
857 }
858 pw.decreaseIndent();
859 }
860 }
861
Ihab Awad293edf22014-07-24 17:52:29 -0700862 ////////////////////////////////////////////////////////////////////////////////////////////////
863 //
864 // State management
865 //
866
867 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700868 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700869 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700870 os = mAtomicFile.startWrite();
871 boolean success = false;
872 try {
873 XmlSerializer serializer = new FastXmlSerializer();
874 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
Evan Charlton105d9772014-11-25 14:08:53 -0800875 writeToXml(mState, serializer, mContext);
Ihab Awadb78b2762014-07-25 15:16:23 -0700876 serializer.flush();
877 success = true;
878 } finally {
879 if (success) {
880 mAtomicFile.finishWrite(os);
881 } else {
882 mAtomicFile.failWrite(os);
883 }
884 }
885 } catch (IOException e) {
886 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700887 }
888 }
889
Ihab Awadb78b2762014-07-25 15:16:23 -0700890 private void read() {
891 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700892 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700893 is = mAtomicFile.openRead();
894 } catch (FileNotFoundException ex) {
895 return;
896 }
897
Tyler Gunn84253572014-09-02 14:50:05 -0700898 boolean versionChanged = false;
899
Ihab Awadb78b2762014-07-25 15:16:23 -0700900 XmlPullParser parser;
901 try {
902 parser = Xml.newPullParser();
903 parser.setInput(new BufferedInputStream(is), null);
904 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700905 mState = readFromXml(parser, mContext);
906 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
907
Ihab Awadb78b2762014-07-25 15:16:23 -0700908 } catch (IOException | XmlPullParserException e) {
909 Log.e(this, e, "Reading state from XML file");
910 mState = new State();
911 } finally {
912 try {
913 is.close();
914 } catch (IOException e) {
915 Log.e(this, e, "Closing InputStream");
916 }
Ihab Awad104f8062014-07-17 11:29:35 -0700917 }
Tyler Gunn84253572014-09-02 14:50:05 -0700918
Evan Charlton105d9772014-11-25 14:08:53 -0800919 // Verify all of the UserHandles.
920 List<PhoneAccount> badAccounts = new ArrayList<>();
921 for (PhoneAccount phoneAccount : mState.accounts) {
922 UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle();
923 if (userHandle == null) {
924 Log.w(this, "Missing UserHandle for %s", phoneAccount);
925 badAccounts.add(phoneAccount);
926 } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) {
927 Log.w(this, "User does not exist for %s", phoneAccount);
928 badAccounts.add(phoneAccount);
929 }
930 }
931 mState.accounts.removeAll(badAccounts);
932
Tyler Gunn84253572014-09-02 14:50:05 -0700933 // If an upgrade occurred, write out the changed data.
Evan Charlton105d9772014-11-25 14:08:53 -0800934 if (versionChanged || !badAccounts.isEmpty()) {
Tyler Gunn84253572014-09-02 14:50:05 -0700935 write();
936 }
Santos Cordon176ae282014-07-14 02:02:14 -0700937 }
938
Evan Charlton105d9772014-11-25 14:08:53 -0800939 private static void writeToXml(State state, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700940 throws IOException {
Evan Charlton105d9772014-11-25 14:08:53 -0800941 sStateXml.writeToXml(state, serializer, context);
Santos Cordon176ae282014-07-14 02:02:14 -0700942 }
Ihab Awad104f8062014-07-17 11:29:35 -0700943
Tyler Gunn84253572014-09-02 14:50:05 -0700944 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700945 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700946 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700947 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700948 }
949
Ihab Awad293edf22014-07-24 17:52:29 -0700950 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700951 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700952 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700953 //
954
Ihab Awadb78b2762014-07-25 15:16:23 -0700955 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700956 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700957 private static final String LENGTH_ATTRIBUTE = "length";
958 private static final String VALUE_TAG = "value";
959
Ihab Awadb78b2762014-07-25 15:16:23 -0700960 /**
961 * Write the supplied object to XML
962 */
Evan Charlton105d9772014-11-25 14:08:53 -0800963 public abstract void writeToXml(T o, XmlSerializer serializer, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700964 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700965
966 /**
967 * Read from the supplied XML into a new object, returning null in case of an
968 * unrecoverable schema mismatch or other data error. 'parser' must be already
969 * positioned at the first tag that is expected to have been emitted by this
970 * object's writeToXml(). This object tries to fail early without modifying
971 * 'parser' if it does not recognize the data it sees.
972 */
Tyler Gunn84253572014-09-02 14:50:05 -0700973 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700974 throws IOException, XmlPullParserException;
975
Ihab Awadd9f54382014-10-24 11:44:47 -0700976 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700977 throws IOException {
978 if (value != null) {
979 serializer.startTag(null, tagName);
980 serializer.text(Objects.toString(value));
981 serializer.endTag(null, tagName);
982 }
983 }
Tyler Gunn84253572014-09-02 14:50:05 -0700984
985 /**
986 * Serializes a string array.
987 *
988 * @param tagName The tag name for the string array.
989 * @param values The string values to serialize.
990 * @param serializer The serializer.
991 * @throws IOException
992 */
993 protected void writeStringList(String tagName, List<String> values,
994 XmlSerializer serializer)
995 throws IOException {
996
997 serializer.startTag(null, tagName);
998 if (values != null) {
999 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
1000 for (String toSerialize : values) {
1001 serializer.startTag(null, VALUE_TAG);
1002 if (toSerialize != null ){
1003 serializer.text(toSerialize);
1004 }
Tyler Gunn84253572014-09-02 14:50:05 -07001005 serializer.endTag(null, VALUE_TAG);
1006 }
1007 } else {
1008 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
1009 }
1010 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -07001011 }
Tyler Gunn84253572014-09-02 14:50:05 -07001012
Santos Cordon9c30c282015-05-13 16:28:27 -07001013 protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer)
Ihab Awadd9f54382014-10-24 11:44:47 -07001014 throws IOException {
Santos Cordon9c30c282015-05-13 16:28:27 -07001015 if (value != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001016 ByteArrayOutputStream stream = new ByteArrayOutputStream();
Santos Cordon9c30c282015-05-13 16:28:27 -07001017 value.writeToStream(stream);
1018 byte[] iconByteArray = stream.toByteArray();
1019 String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0);
Ihab Awadd9f54382014-10-24 11:44:47 -07001020
1021 serializer.startTag(null, tagName);
1022 serializer.text(text);
1023 serializer.endTag(null, tagName);
1024 }
Tyler Gunn84253572014-09-02 14:50:05 -07001025 }
1026
Evan Charlton105d9772014-11-25 14:08:53 -08001027 protected void writeLong(String tagName, long value, XmlSerializer serializer)
1028 throws IOException {
1029 serializer.startTag(null, tagName);
1030 serializer.text(Long.valueOf(value).toString());
1031 serializer.endTag(null, tagName);
1032 }
1033
Tyler Gunn84253572014-09-02 14:50:05 -07001034 /**
1035 * Reads a string array from the XML parser.
1036 *
1037 * @param parser The XML parser.
1038 * @return String array containing the parsed values.
1039 * @throws IOException Exception related to IO.
1040 * @throws XmlPullParserException Exception related to parsing.
1041 */
1042 protected List<String> readStringList(XmlPullParser parser)
1043 throws IOException, XmlPullParserException {
1044
1045 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
1046 List<String> arrayEntries = new ArrayList<String>(length);
1047 String value = null;
1048
1049 if (length == 0) {
1050 return arrayEntries;
1051 }
1052
1053 int outerDepth = parser.getDepth();
1054 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1055 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -07001056 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -07001057 value = parser.getText();
1058 arrayEntries.add(value);
1059 }
1060 }
1061
1062 return arrayEntries;
1063 }
Ihab Awadd9f54382014-10-24 11:44:47 -07001064
Santos Cordon9c30c282015-05-13 16:28:27 -07001065 protected Bitmap readBitmap(XmlPullParser parser) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001066 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
1067 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
1068 }
Santos Cordon9c30c282015-05-13 16:28:27 -07001069
1070 protected Icon readIcon(XmlPullParser parser) throws IOException {
1071 byte[] iconByteArray = Base64.decode(parser.getText(), 0);
1072 ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray);
1073 return Icon.createFromStream(stream);
1074 }
Ihab Awad104f8062014-07-17 11:29:35 -07001075 }
1076
Ihab Awadb78b2762014-07-25 15:16:23 -07001077 @VisibleForTesting
1078 public static final XmlSerialization<State> sStateXml =
1079 new XmlSerialization<State>() {
1080 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -07001081 private static final String DEFAULT_OUTGOING = "default_outgoing";
1082 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -07001083 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -07001084
1085 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001086 public void writeToXml(State o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001087 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001088 if (o != null) {
1089 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -07001090 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -07001091
Ihab Awad26923222014-07-30 10:54:35 -07001092 if (o.defaultOutgoing != null) {
1093 serializer.startTag(null, DEFAULT_OUTGOING);
Evan Charlton105d9772014-11-25 14:08:53 -08001094 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001095 serializer.endTag(null, DEFAULT_OUTGOING);
1096 }
1097
Ihab Awad26923222014-07-30 10:54:35 -07001098 serializer.startTag(null, ACCOUNTS);
1099 for (PhoneAccount m : o.accounts) {
Evan Charlton105d9772014-11-25 14:08:53 -08001100 sPhoneAccountXml.writeToXml(m, serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001101 }
1102 serializer.endTag(null, ACCOUNTS);
1103
1104 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -07001105 }
Ihab Awad104f8062014-07-17 11:29:35 -07001106 }
1107
1108 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001109 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001110 throws IOException, XmlPullParserException {
1111 if (parser.getName().equals(CLASS_STATE)) {
1112 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -07001113
1114 String rawVersion = parser.getAttributeValue(null, VERSION);
1115 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
1116 Integer.parseInt(rawVersion);
1117
Ihab Awadb78b2762014-07-25 15:16:23 -07001118 int outerDepth = parser.getDepth();
1119 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1120 if (parser.getName().equals(DEFAULT_OUTGOING)) {
1121 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001122 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
1123 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -07001124 } else if (parser.getName().equals(ACCOUNTS)) {
1125 int accountsDepth = parser.getDepth();
1126 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -07001127 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
1128 s.versionNumber, context);
1129
1130 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001131 s.accounts.add(account);
1132 }
1133 }
Ihab Awad104f8062014-07-17 11:29:35 -07001134 }
1135 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001136 return s;
Ihab Awad104f8062014-07-17 11:29:35 -07001137 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001138 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001139 }
1140 };
1141
Ihab Awadb78b2762014-07-25 15:16:23 -07001142 @VisibleForTesting
1143 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
1144 new XmlSerialization<PhoneAccount>() {
1145 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
1146 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -07001147 private static final String ADDRESS = "handle";
1148 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001149 private static final String CAPABILITIES = "capabilities";
1150 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -07001151 private static final String ICON_PACKAGE_NAME = "icon_package_name";
1152 private static final String ICON_BITMAP = "icon_bitmap";
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001153 private static final String ICON_TINT = "icon_tint";
1154 private static final String HIGHLIGHT_COLOR = "highlight_color";
Ihab Awad104f8062014-07-17 11:29:35 -07001155 private static final String LABEL = "label";
1156 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -07001157 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Santos Cordon9c30c282015-05-13 16:28:27 -07001158 private static final String ICON = "icon";
Santos Cordonea5cb932015-05-07 16:28:38 -07001159 private static final String ENABLED = "enabled";
Ihab Awad104f8062014-07-17 11:29:35 -07001160
1161 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001162 public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001163 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001164 if (o != null) {
1165 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -07001166
Ihab Awad26923222014-07-30 10:54:35 -07001167 if (o.getAccountHandle() != null) {
1168 serializer.startTag(null, ACCOUNT_HANDLE);
Evan Charlton105d9772014-11-25 14:08:53 -08001169 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context);
Ihab Awad26923222014-07-30 10:54:35 -07001170 serializer.endTag(null, ACCOUNT_HANDLE);
1171 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001172
Ihab Awadd9f54382014-10-24 11:44:47 -07001173 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
1174 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
1175 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
Santos Cordon9c30c282015-05-13 16:28:27 -07001176 writeIconIfNonNull(ICON, o.getIcon(), serializer);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001177 writeTextIfNonNull(HIGHLIGHT_COLOR,
1178 Integer.toString(o.getHighlightColor()), serializer);
Ihab Awadd9f54382014-10-24 11:44:47 -07001179 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
1180 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -07001181 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Santos Cordonea5cb932015-05-07 16:28:38 -07001182 writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001183
Ihab Awad26923222014-07-30 10:54:35 -07001184 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
1185 }
Ihab Awad104f8062014-07-17 11:29:35 -07001186 }
1187
Tyler Gunn84253572014-09-02 14:50:05 -07001188 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001189 throws IOException, XmlPullParserException {
1190 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
1191 int outerDepth = parser.getDepth();
1192 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -07001193 Uri address = null;
1194 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001195 int capabilities = 0;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001196 int iconResId = PhoneAccount.NO_RESOURCE_ID;
Ihab Awadd9f54382014-10-24 11:44:47 -07001197 String iconPackageName = null;
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001198 Bitmap iconBitmap = null;
Ihab Awad07bc5ee2014-11-12 13:42:52 -08001199 int iconTint = PhoneAccount.NO_ICON_TINT;
1200 int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR;
Ihab Awadb78b2762014-07-25 15:16:23 -07001201 String label = null;
1202 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -07001203 List<String> supportedUriSchemes = null;
Santos Cordon9c30c282015-05-13 16:28:27 -07001204 Icon icon = null;
Santos Cordonea5cb932015-05-07 16:28:38 -07001205 boolean enabled = false;
Ihab Awadb78b2762014-07-25 15:16:23 -07001206
1207 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1208 if (parser.getName().equals(ACCOUNT_HANDLE)) {
1209 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -07001210 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
1211 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -07001212 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001213 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001214 address = Uri.parse(parser.getText());
1215 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001216 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -07001217 String nextText = parser.getText();
1218 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -07001219 } else if (parser.getName().equals(CAPABILITIES)) {
1220 parser.next();
1221 capabilities = Integer.parseInt(parser.getText());
1222 } else if (parser.getName().equals(ICON_RES_ID)) {
1223 parser.next();
1224 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -07001225 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
1226 parser.next();
1227 iconPackageName = parser.getText();
1228 } else if (parser.getName().equals(ICON_BITMAP)) {
1229 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001230 iconBitmap = readBitmap(parser);
1231 } else if (parser.getName().equals(ICON_TINT)) {
Nancy Chen06ce0622014-10-23 01:17:35 +00001232 parser.next();
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001233 iconTint = Integer.parseInt(parser.getText());
1234 } else if (parser.getName().equals(HIGHLIGHT_COLOR)) {
1235 parser.next();
1236 highlightColor = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001237 } else if (parser.getName().equals(LABEL)) {
1238 parser.next();
1239 label = parser.getText();
1240 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
1241 parser.next();
1242 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -07001243 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
1244 supportedUriSchemes = readStringList(parser);
Santos Cordon9c30c282015-05-13 16:28:27 -07001245 } else if (parser.getName().equals(ICON)) {
1246 parser.next();
1247 icon = readIcon(parser);
Santos Cordonea5cb932015-05-07 16:28:38 -07001248 } else if (parser.getName().equals(ENABLED)) {
1249 parser.next();
1250 enabled = "true".equalsIgnoreCase(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -07001251 }
1252 }
Tyler Gunn84253572014-09-02 14:50:05 -07001253
Santos Cordona9eebe42015-06-11 14:07:44 -07001254 ComponentName pstnComponentName = new ComponentName("com.android.phone",
1255 "com.android.services.telephony.TelephonyConnectionService");
Santos Cordona82aed82015-05-26 10:43:56 -07001256 ComponentName sipComponentName = new ComponentName("com.android.phone",
1257 "com.android.services.telephony.sip.SipConnectionService");
1258
Tyler Gunn84253572014-09-02 14:50:05 -07001259 // Upgrade older phone accounts to specify the supported URI schemes.
1260 if (version < 2) {
Tyler Gunn84253572014-09-02 14:50:05 -07001261 supportedUriSchemes = new ArrayList<>();
1262
1263 // Handle the SIP connection service.
1264 // Check the system settings to see if it also should handle "tel" calls.
1265 if (accountHandle.getComponentName().equals(sipComponentName)) {
1266 boolean useSipForPstn = useSipForPstnCalls(context);
1267 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
1268 if (useSipForPstn) {
1269 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1270 }
1271 } else {
1272 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
1273 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
1274 }
1275 }
1276
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001277 // Upgrade older phone accounts with explicit package name
1278 if (version < 5) {
1279 if (iconBitmap == null) {
1280 iconPackageName = accountHandle.getComponentName().getPackageName();
1281 }
1282 }
1283
Santos Cordona82aed82015-05-26 10:43:56 -07001284 if (version < 6) {
1285 // Always enable all SIP accounts on upgrade to version 6
1286 if (accountHandle.getComponentName().equals(sipComponentName)) {
1287 enabled = true;
1288 }
1289 }
Santos Cordona9eebe42015-06-11 14:07:44 -07001290 if (version < 7) {
1291 // Always enabled all PSTN acocunts on upgrade to version 7
1292 if (accountHandle.getComponentName().equals(pstnComponentName)) {
1293 enabled = true;
1294 }
1295 }
Roshan Pius6f752c82015-08-10 10:42:06 -07001296 if (version < 8) {
1297 // Migrate the SIP account handle ids to use SIP username instead of SIP URI.
1298 if (accountHandle.getComponentName().equals(sipComponentName)) {
1299 Uri accountUri = Uri.parse(accountHandle.getId());
1300 if (accountUri.getScheme() != null &&
1301 accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) {
1302 accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(),
1303 accountUri.getSchemeSpecificPart(),
1304 accountHandle.getUserHandle());
1305 }
1306 }
1307 }
Santos Cordona82aed82015-05-26 10:43:56 -07001308
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001309 PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001310 .setAddress(address)
1311 .setSubscriptionAddress(subscriptionAddress)
1312 .setCapabilities(capabilities)
Andrew Lee7129f1c2014-09-04 11:55:07 -07001313 .setShortDescription(shortDescription)
1314 .setSupportedUriSchemes(supportedUriSchemes)
Santos Cordonea5cb932015-05-07 16:28:38 -07001315 .setHighlightColor(highlightColor)
1316 .setIsEnabled(enabled);
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001317
Santos Cordon9c30c282015-05-13 16:28:27 -07001318 if (icon != null) {
1319 builder.setIcon(icon);
1320 } else if (iconBitmap != null) {
1321 builder.setIcon(Icon.createWithBitmap(iconBitmap));
1322 } else if (!TextUtils.isEmpty(iconPackageName)) {
1323 builder.setIcon(Icon.createWithResource(iconPackageName, iconResId));
1324 // TODO: Need to set tint.
Ihab Awad7e2c7f32014-11-03 09:49:45 -08001325 }
1326
Ihab Awad0a4b95f2015-05-18 10:15:38 -07001327 return builder.build();
Ihab Awadb78b2762014-07-25 15:16:23 -07001328 }
1329 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001330 }
Tyler Gunn84253572014-09-02 14:50:05 -07001331
1332 /**
Santos Cordon9c30c282015-05-13 16:28:27 -07001333 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN
1334 * calls.
Tyler Gunn84253572014-09-02 14:50:05 -07001335 *
1336 * @param context The context.
1337 * @return {@code True} if SIP should be used for all calls.
1338 */
1339 private boolean useSipForPstnCalls(Context context) {
1340 String option = Settings.System.getString(context.getContentResolver(),
1341 Settings.System.SIP_CALL_OPTIONS);
1342 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
1343 return option.equals(Settings.System.SIP_ALWAYS);
1344 }
Ihab Awad104f8062014-07-17 11:29:35 -07001345 };
1346
Ihab Awadb78b2762014-07-25 15:16:23 -07001347 @VisibleForTesting
1348 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
1349 new XmlSerialization<PhoneAccountHandle>() {
1350 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -07001351 private static final String COMPONENT_NAME = "component_name";
1352 private static final String ID = "id";
Evan Charlton105d9772014-11-25 14:08:53 -08001353 private static final String USER_SERIAL_NUMBER = "user_serial_number";
Ihab Awad104f8062014-07-17 11:29:35 -07001354
1355 @Override
Evan Charlton105d9772014-11-25 14:08:53 -08001356 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001357 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001358 if (o != null) {
1359 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001360
Ihab Awad26923222014-07-30 10:54:35 -07001361 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001362 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001363 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1364 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001365
Ihab Awadd9f54382014-10-24 11:44:47 -07001366 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001367
Evan Charlton105d9772014-11-25 14:08:53 -08001368 if (o.getUserHandle() != null && context != null) {
1369 UserManager userManager = UserManager.get(context);
1370 writeLong(USER_SERIAL_NUMBER,
1371 userManager.getSerialNumberForUser(o.getUserHandle()), serializer);
1372 }
1373
Ihab Awad26923222014-07-30 10:54:35 -07001374 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1375 }
Ihab Awad104f8062014-07-17 11:29:35 -07001376 }
1377
1378 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001379 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001380 throws IOException, XmlPullParserException {
1381 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1382 String componentNameString = null;
1383 String idString = null;
Evan Charlton105d9772014-11-25 14:08:53 -08001384 String userSerialNumberString = null;
Ihab Awadb78b2762014-07-25 15:16:23 -07001385 int outerDepth = parser.getDepth();
Evan Charlton105d9772014-11-25 14:08:53 -08001386
1387 UserManager userManager = UserManager.get(context);
1388
Ihab Awadb78b2762014-07-25 15:16:23 -07001389 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1390 if (parser.getName().equals(COMPONENT_NAME)) {
1391 parser.next();
1392 componentNameString = parser.getText();
1393 } else if (parser.getName().equals(ID)) {
1394 parser.next();
1395 idString = parser.getText();
Evan Charlton105d9772014-11-25 14:08:53 -08001396 } else if (parser.getName().equals(USER_SERIAL_NUMBER)) {
1397 parser.next();
1398 userSerialNumberString = parser.getText();
Ihab Awadb78b2762014-07-25 15:16:23 -07001399 }
1400 }
Ihab Awad26923222014-07-30 10:54:35 -07001401 if (componentNameString != null) {
Evan Charlton105d9772014-11-25 14:08:53 -08001402 UserHandle userHandle = null;
1403 if (userSerialNumberString != null) {
1404 try {
1405 long serialNumber = Long.parseLong(userSerialNumberString);
1406 userHandle = userManager.getUserForSerialNumber(serialNumber);
1407 } catch (NumberFormatException e) {
1408 Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString);
1409 }
1410 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001411 return new PhoneAccountHandle(
1412 ComponentName.unflattenFromString(componentNameString),
Evan Charlton105d9772014-11-25 14:08:53 -08001413 idString,
1414 userHandle);
Ihab Awadb78b2762014-07-25 15:16:23 -07001415 }
1416 }
1417 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001418 }
1419 };
Santos Cordon176ae282014-07-14 02:02:14 -07001420}