blob: bb231237caee979055d4391476c936039a5dffeb [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 Charltonaf51ceb2014-07-30 11:56:36 -070020import android.content.Intent;
21import android.content.pm.PackageManager;
22import android.content.pm.ResolveInfo;
Tyler Gunncb59b672014-08-20 09:02:11 -070023import android.content.pm.ServiceInfo;
Ihab Awadd9f54382014-10-24 11:44:47 -070024import android.graphics.Bitmap;
25import android.graphics.BitmapFactory;
Tyler Gunn84253572014-09-02 14:50:05 -070026import android.provider.Settings;
Tyler Gunn7cc70b42014-09-12 22:17:27 -070027import android.telecom.ConnectionService;
28import android.telecom.PhoneAccount;
29import android.telecom.PhoneAccountHandle;
Nancy Chen140004a2014-10-15 15:48:38 -070030import android.telephony.PhoneNumberUtils;
Nancy Chen5a36b6e2014-10-23 17:42:42 -070031import android.telephony.SubscriptionManager;
Santos Cordon176ae282014-07-14 02:02:14 -070032import android.content.ComponentName;
33import android.content.Context;
Santos Cordon176ae282014-07-14 02:02:14 -070034import android.net.Uri;
Evan Charltonaf51ceb2014-07-30 11:56:36 -070035import android.text.TextUtils;
Ihab Awadb78b2762014-07-25 15:16:23 -070036import android.util.AtomicFile;
Ihab Awadd9f54382014-10-24 11:44:47 -070037import android.util.Base64;
Ihab Awadb78b2762014-07-25 15:16:23 -070038import android.util.Xml;
Santos Cordon176ae282014-07-14 02:02:14 -070039
Tyler Gunn91d43cf2014-09-17 12:19:39 -070040// TODO: Needed for move to system service: import com.android.internal.R;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070041import com.android.internal.annotations.VisibleForTesting;
42import com.android.internal.util.FastXmlSerializer;
Tyler Gunn9787e0e2014-10-14 14:36:12 -070043import com.android.internal.util.IndentingPrintWriter;
Sailesh Nepal0e1dc5a2014-07-30 11:08:54 -070044import com.android.internal.util.XmlUtils;
45
Evan Charltonaf51ceb2014-07-30 11:56:36 -070046import org.xmlpull.v1.XmlPullParser;
47import org.xmlpull.v1.XmlPullParserException;
48import org.xmlpull.v1.XmlSerializer;
49
Ihab Awadb78b2762014-07-25 15:16:23 -070050import java.io.BufferedInputStream;
51import java.io.BufferedOutputStream;
Ihab Awadd9f54382014-10-24 11:44:47 -070052import java.io.ByteArrayOutputStream;
Ihab Awadb78b2762014-07-25 15:16:23 -070053import java.io.File;
54import java.io.FileNotFoundException;
55import java.io.FileOutputStream;
56import java.io.IOException;
57import java.io.InputStream;
Tyler Gunn84253572014-09-02 14:50:05 -070058import java.lang.Integer;
Tyler Gunncb59b672014-08-20 09:02:11 -070059import java.lang.SecurityException;
Tyler Gunnd900ce62014-08-13 11:40:59 -070060import java.lang.String;
Santos Cordon176ae282014-07-14 02:02:14 -070061import java.util.ArrayList;
Tyler Gunnd900ce62014-08-13 11:40:59 -070062import java.util.Iterator;
Santos Cordon176ae282014-07-14 02:02:14 -070063import java.util.List;
64import java.util.Objects;
Ihab Awadb78b2762014-07-25 15:16:23 -070065import java.util.concurrent.CopyOnWriteArrayList;
Santos Cordon176ae282014-07-14 02:02:14 -070066
67/**
Evan Charlton89176372014-07-19 18:23:09 -070068 * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim
Tyler Gunn7cc70b42014-09-12 22:17:27 -070069 * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in
70 * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is
Ihab Awad104f8062014-07-17 11:29:35 -070071 * responsible for security checking to make sure that the caller has proper authority over
Evan Charlton89176372014-07-19 18:23:09 -070072 * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s.
Santos Cordon176ae282014-07-14 02:02:14 -070073 */
Ihab Awadb78b2762014-07-25 15:16:23 -070074public final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -070075
Yorke Lee5e8836a2014-08-22 15:25:18 -070076 public static final PhoneAccountHandle NO_ACCOUNT_SELECTED =
77 new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED");
78
Ihab Awadb78b2762014-07-25 15:16:23 -070079 public abstract static class Listener {
80 public void onAccountsChanged(PhoneAccountRegistrar registrar) {}
81 public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {}
82 public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {}
83 }
84
85 private static final String FILE_NAME = "phone-account-registrar-state.xml";
Tyler Gunn84253572014-09-02 14:50:05 -070086 @VisibleForTesting
Ihab Awadd9f54382014-10-24 11:44:47 -070087 public static final int EXPECTED_STATE_VERSION = 4;
Tyler Gunn84253572014-09-02 14:50:05 -070088
89 /** Keep in sync with the same in SipSettings.java */
90 private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES";
Ihab Awadb78b2762014-07-25 15:16:23 -070091
92 private final List<Listener> mListeners = new CopyOnWriteArrayList<>();
93 private final AtomicFile mAtomicFile;
Santos Cordonafe59e52014-08-22 16:48:43 -070094 private final Context mContext;
Ihab Awadb78b2762014-07-25 15:16:23 -070095 private State mState;
Santos Cordon176ae282014-07-14 02:02:14 -070096
Nancy Chen06ce0622014-10-23 01:17:35 +000097 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -070098 public PhoneAccountRegistrar(Context context) {
Ihab Awadb78b2762014-07-25 15:16:23 -070099 this(context, FILE_NAME);
100 }
101
102 @VisibleForTesting
103 public PhoneAccountRegistrar(Context context, String fileName) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700104 // TODO: This file path is subject to change -- it is storing the phone account registry
105 // state file in the path /data/system/users/0/, which is likely not correct in a
106 // multi-user setting.
107 /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE
108 String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()).
109 getAbsolutePath();
110 mAtomicFile = new AtomicFile(new File(filePath, fileName));
111 UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */
Ihab Awadb78b2762014-07-25 15:16:23 -0700112 mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName));
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700113
Ihab Awadb78b2762014-07-25 15:16:23 -0700114 mState = new State();
Santos Cordonafe59e52014-08-22 16:48:43 -0700115 mContext = context;
Ihab Awadb78b2762014-07-25 15:16:23 -0700116 read();
Santos Cordon176ae282014-07-14 02:02:14 -0700117 }
118
Tyler Gunn84253572014-09-02 14:50:05 -0700119 /**
Nancy Chen140004a2014-10-15 15:48:38 -0700120 * Retrieves the subscription id for a given phone account if it exists. Subscription ids
121 * apply only to PSTN/SIM card phone accounts so all other accounts should not have a
122 * subscription id.
123 * @param accountHandle The handle for the phone account for which to retrieve the
124 * subscription id.
Wink Saville35850602014-10-23 15:57:21 -0700125 * @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 -0700126 */
Wink Saville35850602014-10-23 15:57:21 -0700127 public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) {
Nancy Chen140004a2014-10-15 15:48:38 -0700128 PhoneAccount account = getPhoneAccount(accountHandle);
129 if (account == null || !account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) ||
130 !TextUtils.isDigitsOnly(accountHandle.getId())) {
131 // Since no decimals or negative numbers can be valid subscription ids, only a string of
132 // numbers can be subscription id
133 return -1;
134 }
Wink Saville35850602014-10-23 15:57:21 -0700135 return Integer.parseInt(accountHandle.getId());
Nancy Chen140004a2014-10-15 15:48:38 -0700136 }
137
138 /**
Tyler Gunn84253572014-09-02 14:50:05 -0700139 * Retrieves the default outgoing phone account supporting the specified uriScheme.
140 * @param uriScheme The URI scheme for the outgoing call.
141 * @return The {@link PhoneAccountHandle} to use.
142 */
143 public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700144 final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount();
Tyler Gunn84253572014-09-02 14:50:05 -0700145
Yorke Lee5e8836a2014-08-22 15:25:18 -0700146 if (userSelected != null) {
Tyler Gunn84253572014-09-02 14:50:05 -0700147 // If there is a default PhoneAccount, ensure it supports calls to handles with the
148 // specified uriScheme.
149 final PhoneAccount userSelectedAccount = getPhoneAccount(userSelected);
150 if (userSelectedAccount.supportsUriScheme(uriScheme)) {
151 return userSelected;
152 }
Ihab Awad293edf22014-07-24 17:52:29 -0700153 }
154
Nancy Chen309198e2014-09-15 18:02:49 -0700155 List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme);
Ihab Awad6fb37c82014-08-07 19:48:57 -0700156 switch (outgoing.size()) {
Ihab Awad293edf22014-07-24 17:52:29 -0700157 case 0:
158 // There are no accounts, so there can be no default
159 return null;
160 case 1:
161 // There is only one account, which is by definition the default
Ihab Awad6fb37c82014-08-07 19:48:57 -0700162 return outgoing.get(0);
Ihab Awad293edf22014-07-24 17:52:29 -0700163 default:
164 // There are multiple accounts with no selected default
165 return null;
Ihab Awadf2a84912014-07-22 21:09:25 -0700166 }
Ihab Awad104f8062014-07-17 11:29:35 -0700167 }
Santos Cordon176ae282014-07-14 02:02:14 -0700168
Yorke Lee5e8836a2014-08-22 15:25:18 -0700169 PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
170 if (mState.defaultOutgoing != null) {
171 // Return the registered outgoing default iff it still exists (we keep a sticky
172 // default to survive account deletion and re-addition)
173 for (int i = 0; i < mState.accounts.size(); i++) {
174 if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing)) {
175 return mState.defaultOutgoing;
176 }
177 }
178 // At this point, there was a registered default but it has been deleted; proceed
179 // as though there were no default
180 }
181 return null;
182 }
183
Andrew Leea51a3862014-09-03 14:58:45 -0700184 public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) {
Evan Charlton89176372014-07-19 18:23:09 -0700185 if (accountHandle == null) {
Ihab Awad104f8062014-07-17 11:29:35 -0700186 // Asking to clear the default outgoing is a valid request
Ihab Awad293edf22014-07-24 17:52:29 -0700187 mState.defaultOutgoing = null;
Ihab Awad104f8062014-07-17 11:29:35 -0700188 } else {
189 boolean found = false;
Ihab Awad293edf22014-07-24 17:52:29 -0700190 for (PhoneAccount m : mState.accounts) {
Evan Charlton94d01622014-07-20 12:32:05 -0700191 if (Objects.equals(accountHandle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700192 found = true;
193 break;
194 }
Santos Cordon176ae282014-07-14 02:02:14 -0700195 }
Ihab Awad104f8062014-07-17 11:29:35 -0700196
197 if (!found) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700198 Log.w(this, "Trying to set nonexistent default outgoing %s",
199 accountHandle);
200 return;
201 }
202
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700203 if (!getPhoneAccount(accountHandle).hasCapabilities(
204 PhoneAccount.CAPABILITY_CALL_PROVIDER)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700205 Log.w(this, "Trying to set non-call-provider default outgoing %s",
Evan Charlton89176372014-07-19 18:23:09 -0700206 accountHandle);
Ihab Awad104f8062014-07-17 11:29:35 -0700207 return;
208 }
209
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700210 if (getPhoneAccount(accountHandle).hasCapabilities(
211 PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) {
212 // If the account selected is a SIM account, propagate down to the subscription
213 // record.
Wink Saville7ce6e782014-10-27 10:56:46 -0700214 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen5a36b6e2014-10-23 17:42:42 -0700215 SubscriptionManager.setDefaultVoiceSubId(subId);
216 }
217
Ihab Awad293edf22014-07-24 17:52:29 -0700218 mState.defaultOutgoing = accountHandle;
Santos Cordon176ae282014-07-14 02:02:14 -0700219 }
220
Ihab Awad293edf22014-07-24 17:52:29 -0700221 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700222 fireDefaultOutgoingChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700223 }
224
Ihab Awad293edf22014-07-24 17:52:29 -0700225 public void setSimCallManager(PhoneAccountHandle callManager) {
226 if (callManager != null) {
227 PhoneAccount callManagerAccount = getPhoneAccount(callManager);
228 if (callManagerAccount == null) {
229 Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager);
230 return;
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700231 } else if (!callManagerAccount.hasCapabilities(
232 PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) {
Ihab Awad293edf22014-07-24 17:52:29 -0700233 Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount);
234 return;
235 }
Yorke Lee5e8836a2014-08-22 15:25:18 -0700236 } else {
237 callManager = NO_ACCOUNT_SELECTED;
Ihab Awad293edf22014-07-24 17:52:29 -0700238 }
239 mState.simCallManager = callManager;
Yorke Lee5e8836a2014-08-22 15:25:18 -0700240
Ihab Awad293edf22014-07-24 17:52:29 -0700241 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700242 fireSimCallManagerChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700243 }
244
245 public PhoneAccountHandle getSimCallManager() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700246 if (mState.simCallManager != null) {
Yorke Lee5e8836a2014-08-22 15:25:18 -0700247 if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) {
248 return null;
249 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700250 // Return the registered sim call manager iff it still exists (we keep a sticky
251 // setting to survive account deletion and re-addition)
252 for (int i = 0; i < mState.accounts.size(); i++) {
253 if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager)) {
254 return mState.simCallManager;
255 }
256 }
257 }
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700258
259 // See if the OEM has specified a default one.
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700260 String defaultConnectionMgr =
Santos Cordonafe59e52014-08-22 16:48:43 -0700261 mContext.getResources().getString(R.string.default_connection_manager_component);
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700262 if (!TextUtils.isEmpty(defaultConnectionMgr)) {
Santos Cordonafe59e52014-08-22 16:48:43 -0700263 PackageManager pm = mContext.getPackageManager();
Evan Charltonaf51ceb2014-07-30 11:56:36 -0700264
265 ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr);
266 Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE);
267 intent.setComponent(componentName);
268
269 // Make sure that the component can be resolved.
270 List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0);
271 if (!resolveInfos.isEmpty()) {
272 // See if there is registered PhoneAccount by this component.
273 List<PhoneAccountHandle> handles = getAllPhoneAccountHandles();
274 for (PhoneAccountHandle handle : handles) {
275 if (componentName.equals(handle.getComponentName())) {
276 return handle;
277 }
278 }
279 Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName);
280 } else {
281 Log.d(this, "%s could not be resolved; not using as default", componentName);
282 }
283 } else {
284 Log.v(this, "No default connection manager specified");
285 }
286
Ihab Awadb78b2762014-07-25 15:16:23 -0700287 return null;
Ihab Awad293edf22014-07-24 17:52:29 -0700288 }
289
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700290 /**
291 * Retrieves a list of all {@link PhoneAccountHandle}s registered.
292 *
293 * @return The list of {@link PhoneAccountHandle}s.
294 */
Ihab Awad293edf22014-07-24 17:52:29 -0700295 public List<PhoneAccountHandle> getAllPhoneAccountHandles() {
296 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
297 for (PhoneAccount m : mState.accounts) {
298 accountHandles.add(m.getAccountHandle());
299 }
300 return accountHandles;
301 }
302
303 public List<PhoneAccount> getAllPhoneAccounts() {
304 return new ArrayList<>(mState.accounts);
305 }
306
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700307 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700308 * Determines the number of all {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700309 *
Nancy Chen309198e2014-09-15 18:02:49 -0700310 * @return The total number {@link PhoneAccount}s.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700311 */
312 public int getAllPhoneAccountsCount() {
313 return mState.accounts.size();
314 }
315
316 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700317 * Retrieves a list of all call provider phone accounts.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700318 *
319 * @return The phone account handles.
320 */
Nancy Chen309198e2014-09-15 18:02:49 -0700321 public List<PhoneAccountHandle> getCallCapablePhoneAccounts() {
Santos Cordonafe59e52014-08-22 16:48:43 -0700322 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER);
323 }
324
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700325 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700326 * Retrieves a list of all phone account call provider phone accounts supporting the
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700327 * specified URI scheme.
328 *
329 * @param uriScheme The URI scheme.
330 * @return The phone account handles.
331 */
Nancy Chen309198e2014-09-15 18:02:49 -0700332 public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) {
333 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme);
Tyler Gunn84253572014-09-02 14:50:05 -0700334 }
335
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700336 /**
Nancy Chen1c5926f2014-09-17 14:44:14 -0700337 * Retrieves a list of all phone accounts registered by a specified package.
338 *
339 * @param packageName The name of the package that registered the phone accounts.
340 * @return The phone account handles.
341 */
342 public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) {
343 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
344 for (PhoneAccount m : mState.accounts) {
345 if (Objects.equals(
346 packageName,
347 m.getAccountHandle().getComponentName().getPackageName())) {
348 accountHandles.add(m.getAccountHandle());
349 }
350 }
351 return accountHandles;
352 }
353
354 /**
Nancy Chen309198e2014-09-15 18:02:49 -0700355 * Retrieves a list of all phone account handles with the connection manager capability.
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700356 *
357 * @return The phone account handles.
358 */
359 public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() {
Santos Cordone3d82452014-09-15 13:44:29 -0700360 return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER,
Nancy Chen309198e2014-09-15 18:02:49 -0700361 null /* supportedUriScheme */);
Santos Cordon176ae282014-07-14 02:02:14 -0700362 }
363
Ihab Awad293edf22014-07-24 17:52:29 -0700364 public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) {
365 for (PhoneAccount m : mState.accounts) {
366 if (Objects.equals(handle, m.getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700367 return m;
Santos Cordon176ae282014-07-14 02:02:14 -0700368 }
369 }
370 return null;
371 }
372
Ihab Awad104f8062014-07-17 11:29:35 -0700373 // TODO: Should we implement an artificial limit for # of accounts associated with a single
374 // ComponentName?
Ihab Awad293edf22014-07-24 17:52:29 -0700375 public void registerPhoneAccount(PhoneAccount account) {
Tyler Gunncb59b672014-08-20 09:02:11 -0700376 // Enforce the requirement that a connection service for a phone account has the correct
377 // permission.
378 if (!phoneAccountHasPermission(account.getAccountHandle())) {
379 Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.",
380 account.getAccountHandle());
381 throw new SecurityException(
382 "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission.");
383 }
384
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700385 addOrReplacePhoneAccount(account);
386 }
387
388 /**
389 * Adds a {@code PhoneAccount}, replacing an existing one if found.
390 *
391 * @param account The {@code PhoneAccount} to add or replace.
392 */
393 private void addOrReplacePhoneAccount(PhoneAccount account) {
Ihab Awad293edf22014-07-24 17:52:29 -0700394 mState.accounts.add(account);
Ihab Awad104f8062014-07-17 11:29:35 -0700395 // Search for duplicates and remove any that are found.
Ihab Awad293edf22014-07-24 17:52:29 -0700396 for (int i = 0; i < mState.accounts.size() - 1; i++) {
397 if (Objects.equals(
398 account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) {
Ihab Awad104f8062014-07-17 11:29:35 -0700399 // replace existing entry.
Ihab Awad293edf22014-07-24 17:52:29 -0700400 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700401 break;
Santos Cordon176ae282014-07-14 02:02:14 -0700402 }
403 }
404
Ihab Awad293edf22014-07-24 17:52:29 -0700405 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700406 fireAccountsChanged();
Ihab Awad293edf22014-07-24 17:52:29 -0700407 }
408
Evan Charlton89176372014-07-19 18:23:09 -0700409 public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) {
Ihab Awad293edf22014-07-24 17:52:29 -0700410 for (int i = 0; i < mState.accounts.size(); i++) {
411 if (Objects.equals(accountHandle, mState.accounts.get(i).getAccountHandle())) {
412 mState.accounts.remove(i);
Ihab Awad104f8062014-07-17 11:29:35 -0700413 break;
414 }
415 }
416
Ihab Awad293edf22014-07-24 17:52:29 -0700417 write();
Ihab Awadb78b2762014-07-25 15:16:23 -0700418 fireAccountsChanged();
Santos Cordon176ae282014-07-14 02:02:14 -0700419 }
420
Tyler Gunnd900ce62014-08-13 11:40:59 -0700421 /**
422 * Un-registers all phone accounts associated with a specified package.
423 *
424 * @param packageName The package for which phone accounts will be removed.
425 */
Ihab Awad104f8062014-07-17 11:29:35 -0700426 public void clearAccounts(String packageName) {
Tyler Gunnd900ce62014-08-13 11:40:59 -0700427 boolean accountsRemoved = false;
428 Iterator<PhoneAccount> it = mState.accounts.iterator();
429 while (it.hasNext()) {
430 PhoneAccount phoneAccount = it.next();
Ihab Awad104f8062014-07-17 11:29:35 -0700431 if (Objects.equals(
432 packageName,
Tyler Gunnd900ce62014-08-13 11:40:59 -0700433 phoneAccount.getAccountHandle().getComponentName().getPackageName())) {
434 Log.i(this, "Removing phone account " + phoneAccount.getLabel());
435 it.remove();
436 accountsRemoved = true;
Ihab Awad104f8062014-07-17 11:29:35 -0700437 }
438 }
439
Tyler Gunnd900ce62014-08-13 11:40:59 -0700440 if (accountsRemoved) {
441 write();
442 fireAccountsChanged();
443 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700444 }
445
Nancy Chen140004a2014-10-15 15:48:38 -0700446 public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) {
Wink Saville35850602014-10-23 15:57:21 -0700447 int subId = getSubscriptionIdForPhoneAccount(accountHandle);
Nancy Chen140004a2014-10-15 15:48:38 -0700448 return PhoneNumberUtils.isVoiceMailNumber(subId, number);
449 }
450
Ihab Awadb78b2762014-07-25 15:16:23 -0700451 public void addListener(Listener l) {
452 mListeners.add(l);
453 }
454
455 public void removeListener(Listener l) {
Jay Shraunera82c8f72014-08-14 15:49:16 -0700456 if (l != null) {
457 mListeners.remove(l);
458 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700459 }
460
461 private void fireAccountsChanged() {
462 for (Listener l : mListeners) {
463 l.onAccountsChanged(this);
464 }
465 }
466
467 private void fireDefaultOutgoingChanged() {
468 for (Listener l : mListeners) {
469 l.onDefaultOutgoingChanged(this);
470 }
471 }
472
473 private void fireSimCallManagerChanged() {
474 for (Listener l : mListeners) {
475 l.onSimCallManagerChanged(this);
476 }
Santos Cordon176ae282014-07-14 02:02:14 -0700477 }
478
Tyler Gunncb59b672014-08-20 09:02:11 -0700479 /**
480 * Determines if the connection service specified by a {@link PhoneAccountHandle} has the
481 * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission.
482 *
483 * @param phoneAccountHandle The phone account to check.
484 * @return {@code True} if the phone account has permission.
485 */
486 public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) {
Tyler Gunn91d43cf2014-09-17 12:19:39 -0700487 PackageManager packageManager = mContext.getPackageManager();
Tyler Gunncb59b672014-08-20 09:02:11 -0700488 try {
489 ServiceInfo serviceInfo = packageManager.getServiceInfo(
490 phoneAccountHandle.getComponentName(), 0);
491
492 return serviceInfo.permission != null &&
493 serviceInfo.permission.equals(Manifest.permission.BIND_CONNECTION_SERVICE);
494 } catch (PackageManager.NameNotFoundException e) {
495 Log.w(this, "Name not found %s", e);
496 return false;
497 }
498 }
499
Ihab Awad293edf22014-07-24 17:52:29 -0700500 ////////////////////////////////////////////////////////////////////////////////////////////////
501
Santos Cordonafe59e52014-08-22 16:48:43 -0700502 /**
503 * Returns a list of phone account handles with the specified flag.
Tyler Gunn84253572014-09-02 14:50:05 -0700504 *
505 * @param flags Flags which the {@code PhoneAccount} must have.
Santos Cordonafe59e52014-08-22 16:48:43 -0700506 */
507 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) {
Nancy Chen309198e2014-09-15 18:02:49 -0700508 return getPhoneAccountHandles(flags, null);
Tyler Gunn84253572014-09-02 14:50:05 -0700509 }
510
511 /**
512 * Returns a list of phone account handles with the specified flag, supporting the specified
Nancy Chen309198e2014-09-15 18:02:49 -0700513 * URI scheme.
Tyler Gunn84253572014-09-02 14:50:05 -0700514 *
515 * @param flags Flags which the {@code PhoneAccount} must have.
516 * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the
517 * URI scheme check.
518 */
Nancy Chen309198e2014-09-15 18:02:49 -0700519 private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) {
Evan Charlton94d01622014-07-20 12:32:05 -0700520 List<PhoneAccountHandle> accountHandles = new ArrayList<>();
Ihab Awad293edf22014-07-24 17:52:29 -0700521 for (PhoneAccount m : mState.accounts) {
Nancy Chen309198e2014-09-15 18:02:49 -0700522 if (m.hasCapabilities(flags) && (uriScheme == null || m.supportsUriScheme(uriScheme))) {
Ihab Awadf2a84912014-07-22 21:09:25 -0700523 accountHandles.add(m.getAccountHandle());
524 }
Ihab Awad104f8062014-07-17 11:29:35 -0700525 }
Evan Charlton94d01622014-07-20 12:32:05 -0700526 return accountHandles;
Ihab Awad104f8062014-07-17 11:29:35 -0700527 }
528
Ihab Awad293edf22014-07-24 17:52:29 -0700529 /**
530 * The state of this {@code PhoneAccountRegistrar}.
531 */
Ihab Awadb78b2762014-07-25 15:16:23 -0700532 @VisibleForTesting
533 public static class State {
Ihab Awad293edf22014-07-24 17:52:29 -0700534 /**
535 * The account selected by the user to be employed by default for making outgoing calls.
536 * If the user has not made such a selection, then this is null.
537 */
538 public PhoneAccountHandle defaultOutgoing = null;
539
540 /**
Ihab Awadb78b2762014-07-25 15:16:23 -0700541 * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which
Ihab Awad293edf22014-07-24 17:52:29 -0700542 * manages and optimizes a user's PSTN SIM connections.
543 */
544 public PhoneAccountHandle simCallManager;
545
546 /**
Tyler Gunn7cc70b42014-09-12 22:17:27 -0700547 * The complete list of {@code PhoneAccount}s known to the Telecom subsystem.
Ihab Awad293edf22014-07-24 17:52:29 -0700548 */
549 public final List<PhoneAccount> accounts = new ArrayList<>();
Tyler Gunn84253572014-09-02 14:50:05 -0700550
551 /**
552 * The version number of the State data.
553 */
554 public int versionNumber;
Ihab Awad293edf22014-07-24 17:52:29 -0700555 }
556
Tyler Gunn9787e0e2014-10-14 14:36:12 -0700557 /**
558 * Dumps the state of the {@link CallsManager}.
559 *
560 * @param pw The {@code IndentingPrintWriter} to write the state to.
561 */
562 public void dump(IndentingPrintWriter pw) {
563 if (mState != null) {
564 pw.println("xmlVersion: " + mState.versionNumber);
565 pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" :
566 mState.defaultOutgoing));
567 pw.println("simCallManager: " + (mState.simCallManager == null ? "none" :
568 mState.simCallManager));
569 pw.println("phoneAccounts:");
570 pw.increaseIndent();
571 for (PhoneAccount phoneAccount : mState.accounts) {
572 pw.println(phoneAccount);
573 }
574 pw.decreaseIndent();
575 }
576 }
577
Ihab Awad293edf22014-07-24 17:52:29 -0700578 ////////////////////////////////////////////////////////////////////////////////////////////////
579 //
580 // State management
581 //
582
583 private void write() {
Ihab Awadb78b2762014-07-25 15:16:23 -0700584 final FileOutputStream os;
Ihab Awad104f8062014-07-17 11:29:35 -0700585 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700586 os = mAtomicFile.startWrite();
587 boolean success = false;
588 try {
589 XmlSerializer serializer = new FastXmlSerializer();
590 serializer.setOutput(new BufferedOutputStream(os), "utf-8");
591 writeToXml(mState, serializer);
592 serializer.flush();
593 success = true;
594 } finally {
595 if (success) {
596 mAtomicFile.finishWrite(os);
597 } else {
598 mAtomicFile.failWrite(os);
599 }
600 }
601 } catch (IOException e) {
602 Log.e(this, e, "Writing state to XML file");
Ihab Awad104f8062014-07-17 11:29:35 -0700603 }
604 }
605
Ihab Awadb78b2762014-07-25 15:16:23 -0700606 private void read() {
607 final InputStream is;
Ihab Awad104f8062014-07-17 11:29:35 -0700608 try {
Ihab Awadb78b2762014-07-25 15:16:23 -0700609 is = mAtomicFile.openRead();
610 } catch (FileNotFoundException ex) {
611 return;
612 }
613
Tyler Gunn84253572014-09-02 14:50:05 -0700614 boolean versionChanged = false;
615
Ihab Awadb78b2762014-07-25 15:16:23 -0700616 XmlPullParser parser;
617 try {
618 parser = Xml.newPullParser();
619 parser.setInput(new BufferedInputStream(is), null);
620 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700621 mState = readFromXml(parser, mContext);
622 versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION;
623
Ihab Awadb78b2762014-07-25 15:16:23 -0700624 } catch (IOException | XmlPullParserException e) {
625 Log.e(this, e, "Reading state from XML file");
626 mState = new State();
627 } finally {
628 try {
629 is.close();
630 } catch (IOException e) {
631 Log.e(this, e, "Closing InputStream");
632 }
Ihab Awad104f8062014-07-17 11:29:35 -0700633 }
Tyler Gunn84253572014-09-02 14:50:05 -0700634
635 // If an upgrade occurred, write out the changed data.
636 if (versionChanged) {
637 write();
638 }
Santos Cordon176ae282014-07-14 02:02:14 -0700639 }
640
Ihab Awadb78b2762014-07-25 15:16:23 -0700641 private static void writeToXml(State state, XmlSerializer serializer)
642 throws IOException {
643 sStateXml.writeToXml(state, serializer);
Santos Cordon176ae282014-07-14 02:02:14 -0700644 }
Ihab Awad104f8062014-07-17 11:29:35 -0700645
Tyler Gunn84253572014-09-02 14:50:05 -0700646 private static State readFromXml(XmlPullParser parser, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700647 throws IOException, XmlPullParserException {
Tyler Gunn84253572014-09-02 14:50:05 -0700648 State s = sStateXml.readFromXml(parser, 0, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700649 return s != null ? s : new State();
Ihab Awad104f8062014-07-17 11:29:35 -0700650 }
651
Ihab Awad293edf22014-07-24 17:52:29 -0700652 ////////////////////////////////////////////////////////////////////////////////////////////////
Ihab Awad104f8062014-07-17 11:29:35 -0700653 //
Ihab Awadb78b2762014-07-25 15:16:23 -0700654 // XML serialization
Ihab Awad104f8062014-07-17 11:29:35 -0700655 //
656
Ihab Awadb78b2762014-07-25 15:16:23 -0700657 @VisibleForTesting
Ihab Awad26923222014-07-30 10:54:35 -0700658 public abstract static class XmlSerialization<T> {
Tyler Gunn84253572014-09-02 14:50:05 -0700659 private static final String LENGTH_ATTRIBUTE = "length";
660 private static final String VALUE_TAG = "value";
661
Ihab Awadb78b2762014-07-25 15:16:23 -0700662 /**
663 * Write the supplied object to XML
664 */
Ihab Awad26923222014-07-30 10:54:35 -0700665 public abstract void writeToXml(T o, XmlSerializer serializer)
666 throws IOException;
Ihab Awadb78b2762014-07-25 15:16:23 -0700667
668 /**
669 * Read from the supplied XML into a new object, returning null in case of an
670 * unrecoverable schema mismatch or other data error. 'parser' must be already
671 * positioned at the first tag that is expected to have been emitted by this
672 * object's writeToXml(). This object tries to fail early without modifying
673 * 'parser' if it does not recognize the data it sees.
674 */
Tyler Gunn84253572014-09-02 14:50:05 -0700675 public abstract T readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awad26923222014-07-30 10:54:35 -0700676 throws IOException, XmlPullParserException;
677
Ihab Awadd9f54382014-10-24 11:44:47 -0700678 protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer)
Ihab Awad26923222014-07-30 10:54:35 -0700679 throws IOException {
680 if (value != null) {
681 serializer.startTag(null, tagName);
682 serializer.text(Objects.toString(value));
683 serializer.endTag(null, tagName);
684 }
685 }
Tyler Gunn84253572014-09-02 14:50:05 -0700686
687 /**
688 * Serializes a string array.
689 *
690 * @param tagName The tag name for the string array.
691 * @param values The string values to serialize.
692 * @param serializer The serializer.
693 * @throws IOException
694 */
695 protected void writeStringList(String tagName, List<String> values,
696 XmlSerializer serializer)
697 throws IOException {
698
699 serializer.startTag(null, tagName);
700 if (values != null) {
701 serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size()));
702 for (String toSerialize : values) {
703 serializer.startTag(null, VALUE_TAG);
704 if (toSerialize != null ){
705 serializer.text(toSerialize);
706 }
Tyler Gunn84253572014-09-02 14:50:05 -0700707 serializer.endTag(null, VALUE_TAG);
708 }
709 } else {
710 serializer.attribute(null, LENGTH_ATTRIBUTE, "0");
711 }
712 serializer.endTag(null, tagName);
Ihab Awadd9f54382014-10-24 11:44:47 -0700713 }
Tyler Gunn84253572014-09-02 14:50:05 -0700714
Ihab Awadd9f54382014-10-24 11:44:47 -0700715 protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer)
716 throws IOException {
717 if (value != null && value.getByteCount() > 0) {
718 ByteArrayOutputStream stream = new ByteArrayOutputStream();
719 value.compress(Bitmap.CompressFormat.PNG, 100, stream);
720 byte[] imageByteArray = stream.toByteArray();
721 String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0);
722
723 serializer.startTag(null, tagName);
724 serializer.text(text);
725 serializer.endTag(null, tagName);
726 }
Tyler Gunn84253572014-09-02 14:50:05 -0700727 }
728
729 /**
730 * Reads a string array from the XML parser.
731 *
732 * @param parser The XML parser.
733 * @return String array containing the parsed values.
734 * @throws IOException Exception related to IO.
735 * @throws XmlPullParserException Exception related to parsing.
736 */
737 protected List<String> readStringList(XmlPullParser parser)
738 throws IOException, XmlPullParserException {
739
740 int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE));
741 List<String> arrayEntries = new ArrayList<String>(length);
742 String value = null;
743
744 if (length == 0) {
745 return arrayEntries;
746 }
747
748 int outerDepth = parser.getDepth();
749 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
750 if (parser.getName().equals(VALUE_TAG)) {
Tyler Gunn8e0fef42014-09-08 18:34:44 -0700751 parser.next();
Tyler Gunn84253572014-09-02 14:50:05 -0700752 value = parser.getText();
753 arrayEntries.add(value);
754 }
755 }
756
757 return arrayEntries;
758 }
Ihab Awadd9f54382014-10-24 11:44:47 -0700759
760 protected Bitmap readBitmap(XmlPullParser parser)
761 throws IOException, XmlPullParserException {
762 byte[] imageByteArray = Base64.decode(parser.getText(), 0);
763 return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length);
764 }
Ihab Awad104f8062014-07-17 11:29:35 -0700765 }
766
Ihab Awadb78b2762014-07-25 15:16:23 -0700767 @VisibleForTesting
768 public static final XmlSerialization<State> sStateXml =
769 new XmlSerialization<State>() {
770 private static final String CLASS_STATE = "phone_account_registrar_state";
Ihab Awad104f8062014-07-17 11:29:35 -0700771 private static final String DEFAULT_OUTGOING = "default_outgoing";
Ihab Awad293edf22014-07-24 17:52:29 -0700772 private static final String SIM_CALL_MANAGER = "sim_call_manager";
Ihab Awad104f8062014-07-17 11:29:35 -0700773 private static final String ACCOUNTS = "accounts";
Tyler Gunn84253572014-09-02 14:50:05 -0700774 private static final String VERSION = "version";
Ihab Awad104f8062014-07-17 11:29:35 -0700775
776 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700777 public void writeToXml(State o, XmlSerializer serializer)
778 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700779 if (o != null) {
780 serializer.startTag(null, CLASS_STATE);
Tyler Gunn84253572014-09-02 14:50:05 -0700781 serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION));
Ihab Awadb78b2762014-07-25 15:16:23 -0700782
Ihab Awad26923222014-07-30 10:54:35 -0700783 if (o.defaultOutgoing != null) {
784 serializer.startTag(null, DEFAULT_OUTGOING);
785 sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer);
786 serializer.endTag(null, DEFAULT_OUTGOING);
787 }
788
789 if (o.simCallManager != null) {
790 serializer.startTag(null, SIM_CALL_MANAGER);
791 sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer);
792 serializer.endTag(null, SIM_CALL_MANAGER);
793 }
794
795 serializer.startTag(null, ACCOUNTS);
796 for (PhoneAccount m : o.accounts) {
797 sPhoneAccountXml.writeToXml(m, serializer);
798 }
799 serializer.endTag(null, ACCOUNTS);
800
801 serializer.endTag(null, CLASS_STATE);
Ihab Awad293edf22014-07-24 17:52:29 -0700802 }
Ihab Awad104f8062014-07-17 11:29:35 -0700803 }
804
805 @Override
Tyler Gunn84253572014-09-02 14:50:05 -0700806 public State readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700807 throws IOException, XmlPullParserException {
808 if (parser.getName().equals(CLASS_STATE)) {
809 State s = new State();
Tyler Gunn84253572014-09-02 14:50:05 -0700810
811 String rawVersion = parser.getAttributeValue(null, VERSION);
812 s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 :
813 Integer.parseInt(rawVersion);
814
Ihab Awadb78b2762014-07-25 15:16:23 -0700815 int outerDepth = parser.getDepth();
816 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
817 if (parser.getName().equals(DEFAULT_OUTGOING)) {
818 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700819 s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser,
820 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700821 } else if (parser.getName().equals(SIM_CALL_MANAGER)) {
822 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700823 s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser,
824 s.versionNumber, context);
Ihab Awadb78b2762014-07-25 15:16:23 -0700825 } else if (parser.getName().equals(ACCOUNTS)) {
826 int accountsDepth = parser.getDepth();
827 while (XmlUtils.nextElementWithin(parser, accountsDepth)) {
Tyler Gunn84253572014-09-02 14:50:05 -0700828 PhoneAccount account = sPhoneAccountXml.readFromXml(parser,
829 s.versionNumber, context);
830
831 if (account != null && s.accounts != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700832 s.accounts.add(account);
833 }
834 }
Ihab Awad104f8062014-07-17 11:29:35 -0700835 }
836 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700837 return s;
Ihab Awad104f8062014-07-17 11:29:35 -0700838 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700839 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700840 }
841 };
842
Ihab Awadb78b2762014-07-25 15:16:23 -0700843 @VisibleForTesting
844 public static final XmlSerialization<PhoneAccount> sPhoneAccountXml =
845 new XmlSerialization<PhoneAccount>() {
846 private static final String CLASS_PHONE_ACCOUNT = "phone_account";
847 private static final String ACCOUNT_HANDLE = "account_handle";
Andrew Lee7129f1c2014-09-04 11:55:07 -0700848 private static final String ADDRESS = "handle";
849 private static final String SUBSCRIPTION_ADDRESS = "subscription_number";
Ihab Awad104f8062014-07-17 11:29:35 -0700850 private static final String CAPABILITIES = "capabilities";
851 private static final String ICON_RES_ID = "icon_res_id";
Ihab Awadd9f54382014-10-24 11:44:47 -0700852 private static final String ICON_PACKAGE_NAME = "icon_package_name";
853 private static final String ICON_BITMAP = "icon_bitmap";
Nancy Chen06ce0622014-10-23 01:17:35 +0000854 private static final String COLOR = "color";
Ihab Awad104f8062014-07-17 11:29:35 -0700855 private static final String LABEL = "label";
856 private static final String SHORT_DESCRIPTION = "short_description";
Tyler Gunn84253572014-09-02 14:50:05 -0700857 private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes";
Ihab Awad104f8062014-07-17 11:29:35 -0700858
859 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700860 public void writeToXml(PhoneAccount o, XmlSerializer serializer)
861 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -0700862 if (o != null) {
863 serializer.startTag(null, CLASS_PHONE_ACCOUNT);
Ihab Awadb78b2762014-07-25 15:16:23 -0700864
Ihab Awad26923222014-07-30 10:54:35 -0700865 if (o.getAccountHandle() != null) {
866 serializer.startTag(null, ACCOUNT_HANDLE);
867 sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer);
868 serializer.endTag(null, ACCOUNT_HANDLE);
869 }
Ihab Awadb78b2762014-07-25 15:16:23 -0700870
Ihab Awadd9f54382014-10-24 11:44:47 -0700871 writeTextIfNonNull(ADDRESS, o.getAddress(), serializer);
872 writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer);
873 writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer);
874 writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer);
875 writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer);
876 writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer);
877 writeTextIfNonNull(COLOR, Integer.toString(o.getColor()), serializer);
878 writeTextIfNonNull(LABEL, o.getLabel(), serializer);
879 writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer);
Tyler Gunn84253572014-09-02 14:50:05 -0700880 writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -0700881
Ihab Awad26923222014-07-30 10:54:35 -0700882 serializer.endTag(null, CLASS_PHONE_ACCOUNT);
883 }
Ihab Awad104f8062014-07-17 11:29:35 -0700884 }
885
Tyler Gunn84253572014-09-02 14:50:05 -0700886 public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -0700887 throws IOException, XmlPullParserException {
888 if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) {
889 int outerDepth = parser.getDepth();
890 PhoneAccountHandle accountHandle = null;
Andrew Lee7129f1c2014-09-04 11:55:07 -0700891 Uri address = null;
892 Uri subscriptionAddress = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700893 int capabilities = 0;
894 int iconResId = 0;
Ihab Awadd9f54382014-10-24 11:44:47 -0700895 String iconPackageName = null;
896 Bitmap icon = null;
Nancy Chen06ce0622014-10-23 01:17:35 +0000897 int color = 0;
Ihab Awadb78b2762014-07-25 15:16:23 -0700898 String label = null;
899 String shortDescription = null;
Tyler Gunn84253572014-09-02 14:50:05 -0700900 List<String> supportedUriSchemes = null;
Ihab Awadb78b2762014-07-25 15:16:23 -0700901
902 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
903 if (parser.getName().equals(ACCOUNT_HANDLE)) {
904 parser.nextTag();
Tyler Gunn84253572014-09-02 14:50:05 -0700905 accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version,
906 context);
Andrew Lee7129f1c2014-09-04 11:55:07 -0700907 } else if (parser.getName().equals(ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700908 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700909 address = Uri.parse(parser.getText());
910 } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) {
Ihab Awadb78b2762014-07-25 15:16:23 -0700911 parser.next();
Andrew Lee7129f1c2014-09-04 11:55:07 -0700912 String nextText = parser.getText();
913 subscriptionAddress = nextText == null ? null : Uri.parse(nextText);
Ihab Awadb78b2762014-07-25 15:16:23 -0700914 } else if (parser.getName().equals(CAPABILITIES)) {
915 parser.next();
916 capabilities = Integer.parseInt(parser.getText());
917 } else if (parser.getName().equals(ICON_RES_ID)) {
918 parser.next();
919 iconResId = Integer.parseInt(parser.getText());
Ihab Awadd9f54382014-10-24 11:44:47 -0700920 } else if (parser.getName().equals(ICON_PACKAGE_NAME)) {
921 parser.next();
922 iconPackageName = parser.getText();
923 } else if (parser.getName().equals(ICON_BITMAP)) {
924 parser.next();
925 icon = readBitmap(parser);
Nancy Chen06ce0622014-10-23 01:17:35 +0000926 } else if (parser.getName().equals(COLOR)) {
927 parser.next();
928 color = Integer.parseInt(parser.getText());
Ihab Awadb78b2762014-07-25 15:16:23 -0700929 } else if (parser.getName().equals(LABEL)) {
930 parser.next();
931 label = parser.getText();
932 } else if (parser.getName().equals(SHORT_DESCRIPTION)) {
933 parser.next();
934 shortDescription = parser.getText();
Tyler Gunn84253572014-09-02 14:50:05 -0700935 } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) {
936 supportedUriSchemes = readStringList(parser);
Ihab Awadb78b2762014-07-25 15:16:23 -0700937 }
938 }
Tyler Gunn84253572014-09-02 14:50:05 -0700939
940 // Upgrade older phone accounts to specify the supported URI schemes.
941 if (version < 2) {
942 ComponentName sipComponentName = new ComponentName("com.android.phone",
943 "com.android.services.telephony.sip.SipConnectionService");
944
945 supportedUriSchemes = new ArrayList<>();
946
947 // Handle the SIP connection service.
948 // Check the system settings to see if it also should handle "tel" calls.
949 if (accountHandle.getComponentName().equals(sipComponentName)) {
950 boolean useSipForPstn = useSipForPstnCalls(context);
951 supportedUriSchemes.add(PhoneAccount.SCHEME_SIP);
952 if (useSipForPstn) {
953 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
954 }
955 } else {
956 supportedUriSchemes.add(PhoneAccount.SCHEME_TEL);
957 supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL);
958 }
959 }
960
Andrew Lee7129f1c2014-09-04 11:55:07 -0700961 return PhoneAccount.builder(accountHandle, label)
962 .setAddress(address)
963 .setSubscriptionAddress(subscriptionAddress)
964 .setCapabilities(capabilities)
965 .setIconResId(iconResId)
Ihab Awadd9f54382014-10-24 11:44:47 -0700966 .setIconPackageName(iconPackageName)
967 .setIconBitmap(icon)
Nancy Chen06ce0622014-10-23 01:17:35 +0000968 .setColor(color)
Andrew Lee7129f1c2014-09-04 11:55:07 -0700969 .setShortDescription(shortDescription)
970 .setSupportedUriSchemes(supportedUriSchemes)
Ihab Awad6fb37c82014-08-07 19:48:57 -0700971 .build();
Ihab Awadb78b2762014-07-25 15:16:23 -0700972 }
973 return null;
Ihab Awad104f8062014-07-17 11:29:35 -0700974 }
Tyler Gunn84253572014-09-02 14:50:05 -0700975
976 /**
977 * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls.
978 *
979 * @param context The context.
980 * @return {@code True} if SIP should be used for all calls.
981 */
982 private boolean useSipForPstnCalls(Context context) {
983 String option = Settings.System.getString(context.getContentResolver(),
984 Settings.System.SIP_CALL_OPTIONS);
985 option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY;
986 return option.equals(Settings.System.SIP_ALWAYS);
987 }
Ihab Awad104f8062014-07-17 11:29:35 -0700988 };
989
Ihab Awadb78b2762014-07-25 15:16:23 -0700990 @VisibleForTesting
991 public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml =
992 new XmlSerialization<PhoneAccountHandle>() {
993 private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle";
Ihab Awad104f8062014-07-17 11:29:35 -0700994 private static final String COMPONENT_NAME = "component_name";
995 private static final String ID = "id";
996
997 @Override
Ihab Awadb78b2762014-07-25 15:16:23 -0700998 public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer)
999 throws IOException {
Ihab Awad26923222014-07-30 10:54:35 -07001000 if (o != null) {
1001 serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
Ihab Awadb78b2762014-07-25 15:16:23 -07001002
Ihab Awad26923222014-07-30 10:54:35 -07001003 if (o.getComponentName() != null) {
Ihab Awadd9f54382014-10-24 11:44:47 -07001004 writeTextIfNonNull(
Ihab Awad26923222014-07-30 10:54:35 -07001005 COMPONENT_NAME, o.getComponentName().flattenToString(), serializer);
1006 }
Ihab Awadb78b2762014-07-25 15:16:23 -07001007
Ihab Awadd9f54382014-10-24 11:44:47 -07001008 writeTextIfNonNull(ID, o.getId(), serializer);
Ihab Awadb78b2762014-07-25 15:16:23 -07001009
Ihab Awad26923222014-07-30 10:54:35 -07001010 serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE);
1011 }
Ihab Awad104f8062014-07-17 11:29:35 -07001012 }
1013
1014 @Override
Tyler Gunn84253572014-09-02 14:50:05 -07001015 public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context)
Ihab Awadb78b2762014-07-25 15:16:23 -07001016 throws IOException, XmlPullParserException {
1017 if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) {
1018 String componentNameString = null;
1019 String idString = null;
1020 int outerDepth = parser.getDepth();
1021 while (XmlUtils.nextElementWithin(parser, outerDepth)) {
1022 if (parser.getName().equals(COMPONENT_NAME)) {
1023 parser.next();
1024 componentNameString = parser.getText();
1025 } else if (parser.getName().equals(ID)) {
1026 parser.next();
1027 idString = parser.getText();
1028 }
1029 }
Ihab Awad26923222014-07-30 10:54:35 -07001030 if (componentNameString != null) {
Ihab Awadb78b2762014-07-25 15:16:23 -07001031 return new PhoneAccountHandle(
1032 ComponentName.unflattenFromString(componentNameString),
1033 idString);
1034 }
1035 }
1036 return null;
Ihab Awad104f8062014-07-17 11:29:35 -07001037 }
1038 };
Santos Cordon176ae282014-07-14 02:02:14 -07001039}