Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1 | /* |
| 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 Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 17 | package com.android.server.telecom; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 18 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 19 | import android.Manifest; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 22 | import android.content.Intent; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 25 | import android.content.pm.ServiceInfo; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 26 | import android.content.pm.UserInfo; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 27 | import android.graphics.Bitmap; |
| 28 | import android.graphics.BitmapFactory; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 29 | import android.graphics.drawable.Icon; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 30 | import android.net.Uri; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 31 | import android.os.Bundle; |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 32 | import android.os.AsyncTask; |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 33 | import android.os.PersistableBundle; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 34 | import android.os.Process; |
| 35 | import android.os.UserHandle; |
| 36 | import android.os.UserManager; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 37 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 38 | import android.telecom.ConnectionService; |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 39 | import android.telecom.DefaultDialerManager; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 40 | import android.telecom.PhoneAccount; |
| 41 | import android.telecom.PhoneAccountHandle; |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 42 | import android.telephony.CarrierConfigManager; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 43 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 44 | import android.telephony.SubscriptionManager; |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 45 | import android.telephony.TelephonyManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 46 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 47 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 48 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 49 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 50 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 51 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 52 | import com.android.internal.annotations.VisibleForTesting; |
| 53 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 54 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 55 | import com.android.internal.util.XmlUtils; |
| 56 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 57 | import org.xmlpull.v1.XmlPullParser; |
| 58 | import org.xmlpull.v1.XmlPullParserException; |
| 59 | import org.xmlpull.v1.XmlSerializer; |
| 60 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 61 | import java.io.BufferedInputStream; |
| 62 | import java.io.BufferedOutputStream; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 63 | import java.io.ByteArrayInputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 64 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 65 | import java.io.File; |
| 66 | import java.io.FileNotFoundException; |
| 67 | import java.io.FileOutputStream; |
| 68 | import java.io.IOException; |
| 69 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 70 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 71 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 72 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 73 | import java.util.ArrayList; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 74 | import java.util.Collections; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 75 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 76 | import java.util.List; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 77 | import java.util.Map; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 78 | import java.util.Objects; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 79 | import java.util.concurrent.ConcurrentHashMap; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 80 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 81 | |
| 82 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 83 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 84 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as |
| 85 | * implemented in {@link TelecomServiceImpl}, with the notable exception that |
| 86 | * {@link TelecomServiceImpl} is responsible for security checking to make sure that the caller has |
| 87 | * proper authority over the {@code ComponentName}s they are declaring in their |
| 88 | * {@code PhoneAccountHandle}s. |
| 89 | * |
| 90 | * |
| 91 | * -- About Users and Phone Accounts -- |
| 92 | * |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 93 | * We store all phone accounts for all users in a single place, which means that there are three |
| 94 | * users that we have to deal with in code: |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 95 | * 1) The Android User that is currently active on the device. |
| 96 | * 2) The user which owns/registers the phone account. |
| 97 | * 3) The user running the app that is requesting the phone account information. |
| 98 | * |
| 99 | * For example, I have a device with 2 users, primary (A) and secondary (B), and the secondary user |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 100 | * has a work profile running as another user (B2). Each user/profile only have the visibility of |
| 101 | * phone accounts owned by them. Lets say, user B (settings) is requesting a list of phone accounts, |
| 102 | * and the list only contains phone accounts owned by user B and accounts with |
| 103 | * {@link PhoneAccount#CAPABILITY_MULTI_USER}. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 104 | * |
| 105 | * In practice, (2) is stored with the phone account handle and is part of the handle's ID. (1) is |
| 106 | * saved in {@link #mCurrentUserHandle} and (3) we get from Binder.getCallingUser(). We check these |
| 107 | * users for visibility before returning any phone accounts. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 108 | */ |
Brad Ebinger | 5385513 | 2015-10-30 10:58:19 -0700 | [diff] [blame] | 109 | public class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 110 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 111 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 112 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 113 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 114 | public abstract static class Listener { |
| 115 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 116 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 117 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 118 | } |
| 119 | |
| 120 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 121 | @VisibleForTesting |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 122 | public static final int EXPECTED_STATE_VERSION = 9; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 123 | |
| 124 | /** Keep in sync with the same in SipSettings.java */ |
| 125 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 126 | |
| 127 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 128 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 129 | private final Context mContext; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 130 | private final UserManager mUserManager; |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 131 | private final SubscriptionManager mSubscriptionManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 132 | private State mState; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 133 | private UserHandle mCurrentUserHandle; |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 134 | private interface PhoneAccountRegistrarWriteLock {} |
| 135 | private final PhoneAccountRegistrarWriteLock mWriteLock = |
| 136 | new PhoneAccountRegistrarWriteLock() {}; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 137 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 138 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 139 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 140 | this(context, FILE_NAME); |
| 141 | } |
| 142 | |
| 143 | @VisibleForTesting |
| 144 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 145 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 146 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 147 | // multi-user setting. |
| 148 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 149 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 150 | getAbsolutePath(); |
| 151 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 152 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 153 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 154 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 155 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 156 | mContext = context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 157 | mUserManager = UserManager.get(context); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 158 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 159 | mCurrentUserHandle = Process.myUserHandle(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 160 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 163 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 164 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 165 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 166 | * subscription id. |
| 167 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 168 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 169 | * @return The value of the subscription id or -1 if it does not exist or is not valid. |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 170 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 171 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 172 | PhoneAccount account = getPhoneAccountUnchecked(accountHandle); |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 173 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 174 | if (account != null && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 175 | TelephonyManager tm = |
| 176 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 177 | return tm.getSubIdForPhoneAccount(account); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 178 | } |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 179 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 183 | * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if |
| 184 | * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null} |
| 185 | * will be returned. |
| 186 | * |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 187 | * @param uriScheme The URI scheme for the outgoing call. |
| 188 | * @return The {@link PhoneAccountHandle} to use. |
| 189 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 190 | public PhoneAccountHandle getOutgoingPhoneAccountForScheme(String uriScheme, |
| 191 | UserHandle userHandle) { |
| 192 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(userHandle); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 193 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 194 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 195 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 196 | // specified uriScheme. |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 197 | final PhoneAccount userSelectedAccount = getPhoneAccountUnchecked(userSelected); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 198 | if (userSelectedAccount.supportsUriScheme(uriScheme)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 199 | return userSelected; |
| 200 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 203 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme, false, |
| 204 | userHandle); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 205 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 206 | case 0: |
| 207 | // There are no accounts, so there can be no default |
| 208 | return null; |
| 209 | case 1: |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 210 | // There is only one account, which is by definition the default. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 211 | return outgoing.get(0); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 212 | default: |
| 213 | // There are multiple accounts with no selected default |
| 214 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 215 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 216 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 217 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 218 | public PhoneAccountHandle getOutgoingPhoneAccountForSchemeOfCurrentUser(String uriScheme) { |
| 219 | return getOutgoingPhoneAccountForScheme(uriScheme, mCurrentUserHandle); |
| 220 | } |
| 221 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 222 | /** |
| 223 | * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or |
| 224 | * if it was set by another user). |
| 225 | */ |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 226 | @VisibleForTesting |
| 227 | public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount(UserHandle userHandle) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 228 | if (userHandle == null) { |
| 229 | return null; |
| 230 | } |
| 231 | DefaultPhoneAccountHandle defaultPhoneAccountHandle = mState.defaultOutgoingAccountHandles |
| 232 | .get(userHandle); |
| 233 | if (defaultPhoneAccountHandle == null) { |
| 234 | return null; |
| 235 | } |
| 236 | // Make sure the account is still registered and owned by the user. |
| 237 | PhoneAccount account = getPhoneAccount(defaultPhoneAccountHandle.phoneAccountHandle, |
| 238 | userHandle); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 239 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 240 | if (account != null) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 241 | return defaultPhoneAccountHandle.phoneAccountHandle; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 242 | } |
| 243 | return null; |
| 244 | } |
| 245 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Sets the phone account with which to place all calls by default. Set by the user |
| 248 | * within phone settings. |
| 249 | */ |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 250 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle, |
| 251 | UserHandle userHandle) { |
| 252 | if (userHandle == null) { |
| 253 | return; |
| 254 | } |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 255 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 256 | // Asking to clear the default outgoing is a valid request |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 257 | mState.defaultOutgoingAccountHandles.remove(userHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 258 | } else { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 259 | PhoneAccount account = getPhoneAccount(accountHandle, userHandle); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 260 | if (account == null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 261 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 262 | accountHandle); |
| 263 | return; |
| 264 | } |
| 265 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 266 | if (!account.hasCapabilities(PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 267 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 268 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 269 | return; |
| 270 | } |
| 271 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 272 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 273 | // If the account selected is a SIM account, propagate down to the subscription |
| 274 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 275 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 276 | mSubscriptionManager.setDefaultVoiceSubId(subId); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 279 | mState.defaultOutgoingAccountHandles |
| 280 | .put(userHandle, new DefaultPhoneAccountHandle(userHandle, accountHandle)); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 281 | } |
| 282 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 283 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 284 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Nancy Chen | 668dee0 | 2014-11-19 15:31:31 -0800 | [diff] [blame] | 287 | boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) { |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 288 | return getSubscriptionIdForPhoneAccount(accountHandle) == SubscriptionManager |
| 289 | .getDefaultSmsSubId(); |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 292 | public ComponentName getSystemSimCallManagerComponent() { |
| 293 | String defaultSimCallManager = null; |
| 294 | CarrierConfigManager configManager = (CarrierConfigManager) mContext.getSystemService( |
| 295 | Context.CARRIER_CONFIG_SERVICE); |
| 296 | PersistableBundle configBundle = configManager.getConfig(); |
| 297 | if (configBundle != null) { |
| 298 | defaultSimCallManager = configBundle.getString( |
| 299 | CarrierConfigManager.KEY_DEFAULT_SIM_CALL_MANAGER_STRING); |
| 300 | } |
| 301 | return TextUtils.isEmpty(defaultSimCallManager) |
| 302 | ? null : ComponentName.unflattenFromString(defaultSimCallManager); |
| 303 | } |
| 304 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 305 | public PhoneAccountHandle getSimCallManagerOfCurrentUser() { |
| 306 | return getSimCallManager(mCurrentUserHandle); |
| 307 | } |
| 308 | |
Sailesh Nepal | fc43ea8 | 2015-07-28 19:30:28 -0700 | [diff] [blame] | 309 | /** |
| 310 | * Returns the {@link PhoneAccountHandle} corresponding to the currently active SIM Call |
| 311 | * Manager. SIM Call Manager returned corresponds to the following priority order: |
| 312 | * 1. If a SIM Call Manager {@link PhoneAccount} is registered for the same package as the |
| 313 | * default dialer, then that one is returned. |
| 314 | * 2. If there is a SIM Call Manager {@link PhoneAccount} registered which matches the |
| 315 | * carrier configuration's default, then that one is returned. |
| 316 | * 3. Otherwise, we return null. |
| 317 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 318 | public PhoneAccountHandle getSimCallManager(UserHandle userHandle) { |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 319 | // Get the default dialer in case it has a connection manager associated with it. |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 320 | String dialerPackage = DefaultDialerManager |
| 321 | .getDefaultDialerApplication(mContext, userHandle.getIdentifier()); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 322 | |
Sailesh Nepal | aecfa9b | 2015-06-16 14:55:27 -0700 | [diff] [blame] | 323 | // Check carrier config. |
Sailesh Nepal | 8ed7eeb | 2015-09-08 12:30:13 -0700 | [diff] [blame] | 324 | ComponentName systemSimCallManagerComponent = getSystemSimCallManagerComponent(); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 325 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 326 | PhoneAccountHandle dialerSimCallManager = null; |
| 327 | PhoneAccountHandle systemSimCallManager = null; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 328 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 329 | if (!TextUtils.isEmpty(dialerPackage) || systemSimCallManagerComponent != null) { |
| 330 | // loop through and look for any connection manager in the same package. |
| 331 | List<PhoneAccountHandle> allSimCallManagers = getPhoneAccountHandles( |
| 332 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER, null, null, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 333 | true /* includeDisabledAccounts */, userHandle); |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 334 | for (PhoneAccountHandle accountHandle : allSimCallManagers) { |
| 335 | ComponentName component = accountHandle.getComponentName(); |
| 336 | |
| 337 | // Store the system connection manager if found |
| 338 | if (systemSimCallManager == null |
| 339 | && Objects.equals(component, systemSimCallManagerComponent) |
| 340 | && !resolveComponent(accountHandle).isEmpty()) { |
| 341 | systemSimCallManager = accountHandle; |
| 342 | |
| 343 | // Store the dialer connection manager if found |
| 344 | } else if (dialerSimCallManager == null |
| 345 | && Objects.equals(component.getPackageName(), dialerPackage) |
| 346 | && !resolveComponent(accountHandle).isEmpty()) { |
| 347 | dialerSimCallManager = accountHandle; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 348 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 349 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Santos Cordon | 59c21a7 | 2015-06-11 10:11:21 -0700 | [diff] [blame] | 352 | PhoneAccountHandle retval = dialerSimCallManager != null ? |
| 353 | dialerSimCallManager : systemSimCallManager; |
| 354 | |
| 355 | Log.i(this, "SimCallManager queried, returning: %s", retval); |
| 356 | |
Sailesh Nepal | b3ccd7e | 2015-06-26 18:16:18 -0700 | [diff] [blame] | 357 | return retval; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 360 | /** |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 361 | * If it is a outgoing call, sim call manager of call-initiating user is returned. |
| 362 | * Otherwise, we return the sim call manager of the user associated with the |
| 363 | * target phone account. |
| 364 | * @return phone account handle of sim call manager based on the ongoing call. |
| 365 | */ |
| 366 | public PhoneAccountHandle getSimCallManagerFromCall(Call call) { |
| 367 | if (call == null) { |
| 368 | return null; |
| 369 | } |
| 370 | UserHandle userHandle = call.getInitiatingUser(); |
| 371 | if (userHandle == null) { |
| 372 | userHandle = call.getTargetPhoneAccount().getUserHandle(); |
| 373 | } |
| 374 | return getSimCallManager(userHandle); |
| 375 | } |
| 376 | |
| 377 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 378 | * Update the current UserHandle to track when users are switched. This will allow the |
| 379 | * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything |
| 380 | * across users. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 381 | * We cannot simply check the calling user because that would always return the primary user for |
| 382 | * all invocations originating with the system process. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 383 | * |
| 384 | * @param userHandle The {@link UserHandle}, as delivered by |
| 385 | * {@link Intent#ACTION_USER_SWITCHED}. |
| 386 | */ |
| 387 | public void setCurrentUserHandle(UserHandle userHandle) { |
| 388 | if (userHandle == null) { |
| 389 | Log.d(this, "setCurrentUserHandle, userHandle = null"); |
| 390 | userHandle = Process.myUserHandle(); |
| 391 | } |
| 392 | Log.d(this, "setCurrentUserHandle, %s", userHandle); |
| 393 | mCurrentUserHandle = userHandle; |
| 394 | } |
| 395 | |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 396 | /** |
| 397 | * @return {@code true} if the phone account was successfully enabled/disabled, {@code false} |
| 398 | * otherwise. |
| 399 | */ |
| 400 | public boolean enablePhoneAccount(PhoneAccountHandle accountHandle, boolean isEnabled) { |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 401 | PhoneAccount account = getPhoneAccountUnchecked(accountHandle); |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 402 | if (account == null) { |
| 403 | Log.w(this, "Could not find account to enable: " + accountHandle); |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 404 | return false; |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 405 | } else if (account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 406 | // We never change the enabled state of SIM-based accounts. |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 407 | Log.w(this, "Could not change enable state of SIM account: " + accountHandle); |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 408 | return false; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Santos Cordon | 638f05c | 2015-05-19 17:12:33 -0700 | [diff] [blame] | 411 | if (account.isEnabled() != isEnabled) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 412 | account.setIsEnabled(isEnabled); |
Tony Mak | 4836716 | 2016-01-15 22:58:18 +0000 | [diff] [blame] | 413 | if (!isEnabled) { |
| 414 | // If the disabled account is the default, remove it. |
| 415 | removeDefaultPhoneAccountHandle(accountHandle); |
| 416 | } |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 417 | write(); |
| 418 | fireAccountsChanged(); |
| 419 | } |
Yorke Lee | 71734c2 | 2015-06-02 14:22:56 -0700 | [diff] [blame] | 420 | return true; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Tony Mak | 4836716 | 2016-01-15 22:58:18 +0000 | [diff] [blame] | 423 | private void removeDefaultPhoneAccountHandle(PhoneAccountHandle phoneAccountHandle) { |
| 424 | Iterator<Map.Entry<UserHandle, DefaultPhoneAccountHandle>> iterator = |
| 425 | mState.defaultOutgoingAccountHandles.entrySet().iterator(); |
| 426 | while (iterator.hasNext()) { |
| 427 | Map.Entry<UserHandle, DefaultPhoneAccountHandle> entry = iterator.next(); |
| 428 | if (phoneAccountHandle.equals(entry.getValue().phoneAccountHandle)) { |
| 429 | iterator.remove(); |
| 430 | } |
| 431 | } |
| 432 | } |
| 433 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 434 | private boolean isVisibleForUser(PhoneAccount account, UserHandle userHandle, |
| 435 | boolean acrossProfiles) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 436 | if (account == null) { |
| 437 | return false; |
| 438 | } |
| 439 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 440 | if (userHandle == null) { |
| 441 | Log.w(this, "userHandle is null in isVisibleForUser"); |
| 442 | return false; |
| 443 | } |
| 444 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 445 | // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and |
| 446 | // all profiles. Only Telephony and SIP accounts should have this capability. |
| 447 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle(); |
| 452 | if (phoneAccountUserHandle == null) { |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | if (mCurrentUserHandle == null) { |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 457 | // In case we need to have emergency phone calls from the lock screen. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 458 | Log.d(this, "Current user is null; assuming true"); |
| 459 | return true; |
| 460 | } |
| 461 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 462 | if (acrossProfiles) { |
| 463 | return UserManager.get(mContext).isSameProfileGroup(userHandle.getIdentifier(), |
| 464 | phoneAccountUserHandle.getIdentifier()); |
| 465 | } else { |
| 466 | return phoneAccountUserHandle.equals(userHandle); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 467 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) { |
| 471 | return resolveComponent(phoneAccountHandle.getComponentName(), |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 472 | phoneAccountHandle.getUserHandle()); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | private List<ResolveInfo> resolveComponent(ComponentName componentName, |
| 476 | UserHandle userHandle) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 477 | PackageManager pm = mContext.getPackageManager(); |
| 478 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 479 | intent.setComponent(componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 480 | try { |
| 481 | if (userHandle != null) { |
| 482 | return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier()); |
| 483 | } else { |
| 484 | return pm.queryIntentServices(intent, 0); |
| 485 | } |
| 486 | } catch (SecurityException e) { |
Santos Cordon | 3188b36 | 2015-05-22 13:01:10 -0700 | [diff] [blame] | 487 | Log.e(this, e, "%s is not visible for the calling user", componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 488 | return Collections.EMPTY_LIST; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 489 | } |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 494 | * Only returns accounts which are enabled. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 495 | * |
| 496 | * @return The list of {@link PhoneAccountHandle}s. |
| 497 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 498 | public List<PhoneAccountHandle> getAllPhoneAccountHandles(UserHandle userHandle) { |
| 499 | return getPhoneAccountHandles(0, null, null, false, userHandle); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 502 | public List<PhoneAccount> getAllPhoneAccounts(UserHandle userHandle) { |
| 503 | return getPhoneAccounts(0, null, null, false, userHandle); |
| 504 | } |
| 505 | |
| 506 | public List<PhoneAccount> getAllPhoneAccountsOfCurrentUser() { |
| 507 | return getAllPhoneAccounts(mCurrentUserHandle); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 510 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 511 | * Retrieves a list of all phone account call provider phone accounts supporting the |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 512 | * specified URI scheme. |
| 513 | * |
| 514 | * @param uriScheme The URI scheme. |
| 515 | * @return The phone account handles. |
| 516 | */ |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 517 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts( |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 518 | String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 519 | return getPhoneAccountHandles( |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 520 | PhoneAccount.CAPABILITY_CALL_PROVIDER, |
| 521 | PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 522 | uriScheme, null, includeDisabledAccounts, userHandle); |
| 523 | } |
| 524 | |
| 525 | public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser( |
| 526 | String uriScheme, boolean includeDisabledAccounts) { |
| 527 | return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Retrieves a list of all the SIM-based phone accounts. |
| 532 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 533 | public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 534 | return getPhoneAccountHandles( |
| 535 | PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 536 | null, null, false, userHandle); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 539 | public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() { |
| 540 | return getSimPhoneAccounts(mCurrentUserHandle); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Retrieves a list of all phone accounts registered by a specified package. |
| 545 | * |
| 546 | * @param packageName The name of the package that registered the phone accounts. |
| 547 | * @return The phone account handles. |
| 548 | */ |
| 549 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName, |
| 550 | UserHandle userHandle) { |
| 551 | return getPhoneAccountHandles(0, null, packageName, false, userHandle); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 554 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 555 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 556 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 557 | // Enforce the requirement that a connection service for a phone account has the correct |
| 558 | // permission. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 559 | if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) { |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 560 | Log.w(this, |
| 561 | "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.", |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 562 | account.getAccountHandle()); |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 563 | throw new SecurityException("PhoneAccount connection service requires " |
| 564 | + "BIND_TELECOM_CONNECTION_SERVICE permission."); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 567 | addOrReplacePhoneAccount(account); |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 572 | * |
| 573 | * @param account The {@code PhoneAccount} to add or replace. |
| 574 | */ |
| 575 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 576 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 577 | account.getAccountHandle(), account); |
| 578 | |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 579 | // Start _enabled_ property as false. |
| 580 | // !!! IMPORTANT !!! It is important that we do not read the enabled state that the |
| 581 | // source app provides or else an third party app could enable itself. |
| 582 | boolean isEnabled = false; |
| 583 | |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 584 | PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle()); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 585 | if (oldAccount != null) { |
| 586 | mState.accounts.remove(oldAccount); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 587 | isEnabled = oldAccount.isEnabled(); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 588 | Log.i(this, getAccountDiffString(account, oldAccount)); |
| 589 | } else { |
| 590 | Log.i(this, "New phone account registered: " + account); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 591 | } |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 592 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 593 | mState.accounts.add(account); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 594 | // Reset enabled state to whatever the value was if the account was already registered, |
| 595 | // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled. |
| 596 | account.setIsEnabled( |
| 597 | isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 598 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 599 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 600 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 601 | } |
| 602 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 603 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 604 | PhoneAccount account = getPhoneAccountUnchecked(accountHandle); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 605 | if (account != null) { |
| 606 | if (mState.accounts.remove(account)) { |
| 607 | write(); |
| 608 | fireAccountsChanged(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 609 | } |
| 610 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 613 | /** |
| 614 | * Un-registers all phone accounts associated with a specified package. |
| 615 | * |
| 616 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 617 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 618 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 619 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 620 | boolean accountsRemoved = false; |
| 621 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 622 | while (it.hasNext()) { |
| 623 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 624 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 625 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 626 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 627 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 628 | mState.accounts.remove(phoneAccount); |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 629 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 633 | if (accountsRemoved) { |
| 634 | write(); |
| 635 | fireAccountsChanged(); |
| 636 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 637 | } |
| 638 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 639 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 640 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Yorke Lee | b195723 | 2015-06-30 10:09:54 -0700 | [diff] [blame] | 641 | return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 644 | public void addListener(Listener l) { |
| 645 | mListeners.add(l); |
| 646 | } |
| 647 | |
| 648 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 649 | if (l != null) { |
| 650 | mListeners.remove(l); |
| 651 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | private void fireAccountsChanged() { |
| 655 | for (Listener l : mListeners) { |
| 656 | l.onAccountsChanged(this); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | private void fireDefaultOutgoingChanged() { |
| 661 | for (Listener l : mListeners) { |
| 662 | l.onDefaultOutgoingChanged(this); |
| 663 | } |
| 664 | } |
| 665 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 666 | private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) { |
| 667 | if (account1 == null || account2 == null) { |
| 668 | return "Diff: " + account1 + ", " + account2; |
| 669 | } |
| 670 | |
| 671 | StringBuffer sb = new StringBuffer(); |
| 672 | sb.append("[").append(account1.getAccountHandle()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 673 | appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()), |
| 674 | Log.piiHandle(account2.getAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 675 | appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities()); |
| 676 | appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor()); |
| 677 | appendDiff(sb, "icon", account1.getIcon(), account2.getIcon()); |
| 678 | appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel()); |
| 679 | appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 680 | appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()), |
| 681 | Log.piiHandle(account2.getSubscriptionAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 682 | appendDiff(sb, "uris", account1.getSupportedUriSchemes(), |
| 683 | account2.getSupportedUriSchemes()); |
| 684 | sb.append("]"); |
| 685 | return sb.toString(); |
| 686 | } |
| 687 | |
| 688 | private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) { |
| 689 | if (!Objects.equals(obj1, obj2)) { |
| 690 | sb.append("(") |
| 691 | .append(attrName) |
| 692 | .append(": ") |
| 693 | .append(obj1) |
| 694 | .append(" -> ") |
| 695 | .append(obj2) |
| 696 | .append(")"); |
| 697 | } |
| 698 | } |
| 699 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 700 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 701 | * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 702 | * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission. |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 703 | * |
| 704 | * @param phoneAccountHandle The phone account to check. |
| 705 | * @return {@code True} if the phone account has permission. |
| 706 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 707 | public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 708 | List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle); |
| 709 | if (resolveInfos.isEmpty()) { |
| 710 | Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName()); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 711 | return false; |
| 712 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 713 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 714 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 715 | if (serviceInfo == null) { |
| 716 | return false; |
| 717 | } |
| 718 | |
| 719 | if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) && |
| 720 | !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals( |
| 721 | serviceInfo.permission)) { |
| 722 | // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE, |
| 723 | // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are |
| 724 | // system/signature only. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 725 | return false; |
| 726 | } |
| 727 | } |
| 728 | return true; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 729 | } |
| 730 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 731 | // |
| 732 | // Methods for retrieving PhoneAccounts and PhoneAccountHandles |
| 733 | // |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 734 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 735 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 736 | * Returns the PhoneAccount for the specified handle. Does no user checking. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 737 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 738 | * @param handle |
| 739 | * @return The corresponding phone account if one exists. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 740 | */ |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 741 | public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 742 | for (PhoneAccount m : mState.accounts) { |
| 743 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 744 | return m; |
| 745 | } |
| 746 | } |
| 747 | return null; |
| 748 | } |
| 749 | |
| 750 | /** |
| 751 | * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone |
| 752 | * account before returning it. The current user is the active user on the actual android |
| 753 | * device. |
| 754 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 755 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) { |
| 756 | return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false); |
| 757 | } |
| 758 | |
| 759 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, |
| 760 | UserHandle userHandle, boolean acrossProfiles) { |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 761 | PhoneAccount account = getPhoneAccountUnchecked(handle); |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 762 | if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 763 | return account; |
| 764 | } |
| 765 | return null; |
| 766 | } |
| 767 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 768 | public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) { |
| 769 | return getPhoneAccount(handle, mCurrentUserHandle); |
| 770 | } |
| 771 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 772 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
| 773 | int capabilities, |
| 774 | String uriScheme, |
| 775 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 776 | boolean includeDisabledAccounts, |
| 777 | UserHandle userHandle) { |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 778 | return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 779 | packageName, includeDisabledAccounts, userHandle); |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 782 | /** |
| 783 | * Returns a list of phone account handles with the specified capabilities, uri scheme, |
| 784 | * and package name. |
| 785 | */ |
| 786 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 787 | int capabilities, |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 788 | int excludedCapabilities, |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 789 | String uriScheme, |
| 790 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 791 | boolean includeDisabledAccounts, |
| 792 | UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 793 | List<PhoneAccountHandle> handles = new ArrayList<>(); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 794 | |
| 795 | for (PhoneAccount account : getPhoneAccounts( |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 796 | capabilities, excludedCapabilities, uriScheme, packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 797 | includeDisabledAccounts, userHandle)) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 798 | handles.add(account.getAccountHandle()); |
| 799 | } |
| 800 | return handles; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 801 | } |
| 802 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 803 | private List<PhoneAccount> getPhoneAccounts( |
| 804 | int capabilities, |
| 805 | String uriScheme, |
| 806 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 807 | boolean includeDisabledAccounts, |
| 808 | UserHandle userHandle) { |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 809 | return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 810 | includeDisabledAccounts, userHandle); |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 811 | } |
| 812 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 813 | /** |
| 814 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 815 | * URI scheme, within the specified package name. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 816 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 817 | * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0. |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 818 | * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have. |
| 819 | * Ignored if 0. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 820 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 821 | * URI scheme check. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 822 | * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 823 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 824 | private List<PhoneAccount> getPhoneAccounts( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 825 | int capabilities, |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 826 | int excludedCapabilities, |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 827 | String uriScheme, |
| 828 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 829 | boolean includeDisabledAccounts, |
| 830 | UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 831 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 832 | for (PhoneAccount m : mState.accounts) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 833 | if (!(m.isEnabled() || includeDisabledAccounts)) { |
| 834 | // Do not include disabled accounts. |
| 835 | continue; |
| 836 | } |
| 837 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 838 | if ((m.getCapabilities() & excludedCapabilities) != 0) { |
| 839 | // If an excluded capability is present, skip. |
| 840 | continue; |
| 841 | } |
| 842 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 843 | if (capabilities != 0 && !m.hasCapabilities(capabilities)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 844 | // Account doesn't have the right capabilities; skip this one. |
| 845 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 846 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 847 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 848 | // Account doesn't support this URI scheme; skip this one. |
| 849 | continue; |
| 850 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 851 | PhoneAccountHandle handle = m.getAccountHandle(); |
| 852 | |
| 853 | if (resolveComponent(handle).isEmpty()) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 854 | // This component cannot be resolved anymore; skip this one. |
| 855 | continue; |
| 856 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 857 | if (packageName != null && |
| 858 | !packageName.equals(handle.getComponentName().getPackageName())) { |
| 859 | // Not the right package name; skip this one. |
| 860 | continue; |
| 861 | } |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 862 | if (!isVisibleForUser(m, userHandle, false)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 863 | // Account is not visible for the current user; skip this one. |
| 864 | continue; |
| 865 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 866 | accounts.add(m); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 867 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 868 | return accounts; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 871 | // |
| 872 | // State Implementation for PhoneAccountRegistrar |
| 873 | // |
| 874 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 875 | /** |
| 876 | * The state of this {@code PhoneAccountRegistrar}. |
| 877 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 878 | @VisibleForTesting |
| 879 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 880 | /** |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 881 | * Store the default phone account handle of users. If no record of a user can be found in |
| 882 | * the map, it means that no default phone account handle is set in that user. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 883 | */ |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 884 | public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles |
| 885 | = new ConcurrentHashMap<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 886 | |
| 887 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 888 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 889 | */ |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 890 | public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 891 | |
| 892 | /** |
| 893 | * The version number of the State data. |
| 894 | */ |
| 895 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 896 | } |
| 897 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 898 | /** |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 899 | * The default {@link PhoneAccountHandle} of a user. |
| 900 | */ |
| 901 | public static class DefaultPhoneAccountHandle { |
| 902 | |
| 903 | public final UserHandle userHandle; |
| 904 | |
| 905 | public final PhoneAccountHandle phoneAccountHandle; |
| 906 | |
| 907 | public DefaultPhoneAccountHandle(UserHandle userHandle, |
| 908 | PhoneAccountHandle phoneAccountHandle) { |
| 909 | this.userHandle = userHandle; |
| 910 | this.phoneAccountHandle = phoneAccountHandle; |
| 911 | } |
| 912 | } |
| 913 | |
| 914 | /** |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 915 | * Dumps the state of the {@link CallsManager}. |
| 916 | * |
| 917 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 918 | */ |
| 919 | public void dump(IndentingPrintWriter pw) { |
| 920 | if (mState != null) { |
| 921 | pw.println("xmlVersion: " + mState.versionNumber); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 922 | DefaultPhoneAccountHandle defaultPhoneAccountHandle |
| 923 | = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle()); |
| 924 | pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" : |
| 925 | defaultPhoneAccountHandle.phoneAccountHandle)); |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 926 | pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle)); |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 927 | pw.println("phoneAccounts:"); |
| 928 | pw.increaseIndent(); |
| 929 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 930 | pw.println(phoneAccount); |
| 931 | } |
| 932 | pw.decreaseIndent(); |
| 933 | } |
| 934 | } |
| 935 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 936 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 937 | // |
| 938 | // State management |
| 939 | // |
| 940 | |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 941 | private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> { |
| 942 | @Override |
| 943 | public Void doInBackground(ByteArrayOutputStream... args) { |
| 944 | final ByteArrayOutputStream buffer = args[0]; |
| 945 | FileOutputStream fileOutput = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 946 | try { |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 947 | synchronized (mWriteLock) { |
| 948 | fileOutput = mAtomicFile.startWrite(); |
| 949 | buffer.writeTo(fileOutput); |
| 950 | mAtomicFile.finishWrite(fileOutput); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 951 | } |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 952 | } catch (IOException e) { |
| 953 | Log.e(this, e, "Writing state to XML file"); |
| 954 | mAtomicFile.failWrite(fileOutput); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 955 | } |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 956 | return null; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | private void write() { |
| 961 | try { |
| 962 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 963 | XmlSerializer serializer = new FastXmlSerializer(); |
| 964 | serializer.setOutput(os, "utf-8"); |
| 965 | writeToXml(mState, serializer, mContext); |
| 966 | serializer.flush(); |
| 967 | new AsyncXmlWriter().execute(os); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 968 | } catch (IOException e) { |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame^] | 969 | Log.e(this, e, "Writing state to XML buffer"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 973 | private void read() { |
| 974 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 975 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 976 | is = mAtomicFile.openRead(); |
| 977 | } catch (FileNotFoundException ex) { |
| 978 | return; |
| 979 | } |
| 980 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 981 | boolean versionChanged = false; |
| 982 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 983 | XmlPullParser parser; |
| 984 | try { |
| 985 | parser = Xml.newPullParser(); |
| 986 | parser.setInput(new BufferedInputStream(is), null); |
| 987 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 988 | mState = readFromXml(parser, mContext); |
| 989 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 990 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 991 | } catch (IOException | XmlPullParserException e) { |
| 992 | Log.e(this, e, "Reading state from XML file"); |
| 993 | mState = new State(); |
| 994 | } finally { |
| 995 | try { |
| 996 | is.close(); |
| 997 | } catch (IOException e) { |
| 998 | Log.e(this, e, "Closing InputStream"); |
| 999 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1000 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1001 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1002 | // Verify all of the UserHandles. |
| 1003 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 1004 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 1005 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 1006 | if (userHandle == null) { |
| 1007 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 1008 | badAccounts.add(phoneAccount); |
| 1009 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 1010 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 1011 | badAccounts.add(phoneAccount); |
| 1012 | } |
| 1013 | } |
| 1014 | mState.accounts.removeAll(badAccounts); |
| 1015 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1016 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1017 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1018 | write(); |
| 1019 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1022 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1023 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1024 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1025 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1026 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1027 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1028 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1029 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1030 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1033 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1034 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1035 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1036 | // |
| 1037 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1038 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1039 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1040 | private static final String TAG_VALUE = "value"; |
| 1041 | private static final String ATTRIBUTE_LENGTH = "length"; |
| 1042 | private static final String ATTRIBUTE_KEY = "key"; |
| 1043 | private static final String ATTRIBUTE_VALUE_TYPE = "type"; |
| 1044 | private static final String VALUE_TYPE_STRING = "string"; |
| 1045 | private static final String VALUE_TYPE_INTEGER = "integer"; |
| 1046 | private static final String VALUE_TYPE_BOOLEAN = "boolean"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1047 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1048 | /** |
| 1049 | * Write the supplied object to XML |
| 1050 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1051 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1052 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1053 | |
| 1054 | /** |
| 1055 | * Read from the supplied XML into a new object, returning null in case of an |
| 1056 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 1057 | * positioned at the first tag that is expected to have been emitted by this |
| 1058 | * object's writeToXml(). This object tries to fail early without modifying |
| 1059 | * 'parser' if it does not recognize the data it sees. |
| 1060 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1061 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1062 | throws IOException, XmlPullParserException; |
| 1063 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1064 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1065 | throws IOException { |
| 1066 | if (value != null) { |
| 1067 | serializer.startTag(null, tagName); |
| 1068 | serializer.text(Objects.toString(value)); |
| 1069 | serializer.endTag(null, tagName); |
| 1070 | } |
| 1071 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1072 | |
| 1073 | /** |
| 1074 | * Serializes a string array. |
| 1075 | * |
| 1076 | * @param tagName The tag name for the string array. |
| 1077 | * @param values The string values to serialize. |
| 1078 | * @param serializer The serializer. |
| 1079 | * @throws IOException |
| 1080 | */ |
| 1081 | protected void writeStringList(String tagName, List<String> values, |
| 1082 | XmlSerializer serializer) |
| 1083 | throws IOException { |
| 1084 | |
| 1085 | serializer.startTag(null, tagName); |
| 1086 | if (values != null) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1087 | serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size())); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1088 | for (String toSerialize : values) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1089 | serializer.startTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1090 | if (toSerialize != null ){ |
| 1091 | serializer.text(toSerialize); |
| 1092 | } |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1093 | serializer.endTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1094 | } |
| 1095 | } else { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1096 | serializer.attribute(null, ATTRIBUTE_LENGTH, "0"); |
| 1097 | } |
| 1098 | serializer.endTag(null, tagName); |
| 1099 | } |
| 1100 | |
| 1101 | protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer) |
| 1102 | throws IOException { |
| 1103 | |
| 1104 | serializer.startTag(null, tagName); |
| 1105 | if (values != null) { |
| 1106 | for (String key : values.keySet()) { |
| 1107 | Object value = values.get(key); |
| 1108 | |
| 1109 | if (value == null) { |
| 1110 | continue; |
| 1111 | } |
| 1112 | |
| 1113 | String valueType; |
| 1114 | if (value instanceof String) { |
| 1115 | valueType = VALUE_TYPE_STRING; |
| 1116 | } else if (value instanceof Integer) { |
| 1117 | valueType = VALUE_TYPE_INTEGER; |
| 1118 | } else if (value instanceof Boolean) { |
| 1119 | valueType = VALUE_TYPE_BOOLEAN; |
| 1120 | } else { |
| 1121 | Log.w(this, |
| 1122 | "PhoneAccounts support only string, integer and boolean extras TY."); |
| 1123 | continue; |
| 1124 | } |
| 1125 | |
| 1126 | serializer.startTag(null, TAG_VALUE); |
| 1127 | serializer.attribute(null, ATTRIBUTE_KEY, key); |
| 1128 | serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType); |
| 1129 | serializer.text(Objects.toString(value)); |
| 1130 | serializer.endTag(null, TAG_VALUE); |
| 1131 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1132 | } |
| 1133 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1134 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1135 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1136 | protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer) |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1137 | throws IOException { |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1138 | if (value != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1139 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1140 | value.writeToStream(stream); |
| 1141 | byte[] iconByteArray = stream.toByteArray(); |
| 1142 | String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1143 | |
| 1144 | serializer.startTag(null, tagName); |
| 1145 | serializer.text(text); |
| 1146 | serializer.endTag(null, tagName); |
| 1147 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1148 | } |
| 1149 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1150 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 1151 | throws IOException { |
| 1152 | serializer.startTag(null, tagName); |
| 1153 | serializer.text(Long.valueOf(value).toString()); |
| 1154 | serializer.endTag(null, tagName); |
| 1155 | } |
| 1156 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1157 | /** |
| 1158 | * Reads a string array from the XML parser. |
| 1159 | * |
| 1160 | * @param parser The XML parser. |
| 1161 | * @return String array containing the parsed values. |
| 1162 | * @throws IOException Exception related to IO. |
| 1163 | * @throws XmlPullParserException Exception related to parsing. |
| 1164 | */ |
| 1165 | protected List<String> readStringList(XmlPullParser parser) |
| 1166 | throws IOException, XmlPullParserException { |
| 1167 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1168 | int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH)); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1169 | List<String> arrayEntries = new ArrayList<String>(length); |
| 1170 | String value = null; |
| 1171 | |
| 1172 | if (length == 0) { |
| 1173 | return arrayEntries; |
| 1174 | } |
| 1175 | |
| 1176 | int outerDepth = parser.getDepth(); |
| 1177 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1178 | if (parser.getName().equals(TAG_VALUE)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 1179 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1180 | value = parser.getText(); |
| 1181 | arrayEntries.add(value); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | return arrayEntries; |
| 1186 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1187 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * Reads a bundle from the XML parser. |
| 1190 | * |
| 1191 | * @param parser The XML parser. |
| 1192 | * @return Bundle containing the parsed values. |
| 1193 | * @throws IOException Exception related to IO. |
| 1194 | * @throws XmlPullParserException Exception related to parsing. |
| 1195 | */ |
| 1196 | protected Bundle readBundle(XmlPullParser parser) |
| 1197 | throws IOException, XmlPullParserException { |
| 1198 | |
| 1199 | Bundle bundle = null; |
| 1200 | int outerDepth = parser.getDepth(); |
| 1201 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1202 | if (parser.getName().equals(TAG_VALUE)) { |
| 1203 | String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE); |
| 1204 | String key = parser.getAttributeValue(null, ATTRIBUTE_KEY); |
| 1205 | parser.next(); |
| 1206 | String value = parser.getText(); |
| 1207 | |
| 1208 | if (bundle == null) { |
| 1209 | bundle = new Bundle(); |
| 1210 | } |
| 1211 | |
| 1212 | // Do not write null values to the bundle. |
| 1213 | if (value == null) { |
| 1214 | continue; |
| 1215 | } |
| 1216 | |
| 1217 | if (VALUE_TYPE_STRING.equals(valueType)) { |
| 1218 | bundle.putString(key, value); |
| 1219 | } else if (VALUE_TYPE_INTEGER.equals(valueType)) { |
| 1220 | try { |
| 1221 | int intValue = Integer.parseInt(value); |
| 1222 | bundle.putInt(key, intValue); |
| 1223 | } catch (NumberFormatException nfe) { |
| 1224 | Log.w(this, "Invalid integer PhoneAccount extra."); |
| 1225 | } |
| 1226 | } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) { |
| 1227 | boolean boolValue = Boolean.parseBoolean(value); |
| 1228 | bundle.putBoolean(key, boolValue); |
| 1229 | } else { |
| 1230 | Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle."); |
| 1231 | } |
| 1232 | } |
| 1233 | } |
| 1234 | return bundle; |
| 1235 | } |
| 1236 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1237 | protected Bitmap readBitmap(XmlPullParser parser) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1238 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 1239 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 1240 | } |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1241 | |
| 1242 | protected Icon readIcon(XmlPullParser parser) throws IOException { |
| 1243 | byte[] iconByteArray = Base64.decode(parser.getText(), 0); |
| 1244 | ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray); |
| 1245 | return Icon.createFromStream(stream); |
| 1246 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1247 | } |
| 1248 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1249 | @VisibleForTesting |
| 1250 | public static final XmlSerialization<State> sStateXml = |
| 1251 | new XmlSerialization<State>() { |
| 1252 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1253 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 1254 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1255 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1256 | |
| 1257 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1258 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1259 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1260 | if (o != null) { |
| 1261 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1262 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1263 | |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1264 | serializer.startTag(null, DEFAULT_OUTGOING); |
| 1265 | for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o |
| 1266 | .defaultOutgoingAccountHandles.values()) { |
| 1267 | sDefaultPhoneAcountHandleXml |
| 1268 | .writeToXml(defaultPhoneAccountHandle, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1269 | } |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1270 | serializer.endTag(null, DEFAULT_OUTGOING); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1271 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1272 | serializer.startTag(null, ACCOUNTS); |
| 1273 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1274 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1275 | } |
| 1276 | serializer.endTag(null, ACCOUNTS); |
| 1277 | |
| 1278 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1279 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1280 | } |
| 1281 | |
| 1282 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1283 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1284 | throws IOException, XmlPullParserException { |
| 1285 | if (parser.getName().equals(CLASS_STATE)) { |
| 1286 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1287 | |
| 1288 | String rawVersion = parser.getAttributeValue(null, VERSION); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1289 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1290 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1291 | int outerDepth = parser.getDepth(); |
| 1292 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1293 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1294 | if (s.versionNumber < 9) { |
| 1295 | // Migration old default phone account handle here by assuming the |
| 1296 | // default phone account handle is belong to primary user. |
| 1297 | parser.nextTag(); |
| 1298 | PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml |
| 1299 | .readFromXml(parser, s.versionNumber, context); |
| 1300 | UserManager userManager = UserManager.get(context); |
| 1301 | UserInfo primaryUser = userManager.getPrimaryUser(); |
| 1302 | if (primaryUser != null) { |
| 1303 | UserHandle userHandle = primaryUser.getUserHandle(); |
| 1304 | DefaultPhoneAccountHandle defaultPhoneAccountHandle |
| 1305 | = new DefaultPhoneAccountHandle(userHandle, |
| 1306 | phoneAccountHandle); |
| 1307 | s.defaultOutgoingAccountHandles |
| 1308 | .put(userHandle, defaultPhoneAccountHandle); |
| 1309 | } |
| 1310 | } else { |
| 1311 | int defaultAccountHandlesDepth = parser.getDepth(); |
| 1312 | while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) { |
| 1313 | DefaultPhoneAccountHandle accountHandle |
| 1314 | = sDefaultPhoneAcountHandleXml |
| 1315 | .readFromXml(parser, s.versionNumber, context); |
| 1316 | if (accountHandle != null && s.accounts != null) { |
| 1317 | s.defaultOutgoingAccountHandles |
| 1318 | .put(accountHandle.userHandle, accountHandle); |
| 1319 | } |
| 1320 | } |
| 1321 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1322 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 1323 | int accountsDepth = parser.getDepth(); |
| 1324 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1325 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 1326 | s.versionNumber, context); |
| 1327 | |
| 1328 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1329 | s.accounts.add(account); |
| 1330 | } |
| 1331 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1332 | } |
| 1333 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1334 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1335 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1336 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1337 | } |
| 1338 | }; |
| 1339 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1340 | @VisibleForTesting |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1341 | public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml = |
| 1342 | new XmlSerialization<DefaultPhoneAccountHandle>() { |
| 1343 | private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE |
| 1344 | = "default_outgoing_phone_account_handle"; |
| 1345 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
| 1346 | private static final String ACCOUNT_HANDLE = "account_handle"; |
| 1347 | |
| 1348 | @Override |
| 1349 | public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer, |
| 1350 | Context context) throws IOException { |
| 1351 | if (o != null) { |
| 1352 | final UserManager userManager = UserManager.get(context); |
| 1353 | final long serialNumber = userManager.getSerialNumberForUser(o.userHandle); |
| 1354 | if (serialNumber != -1) { |
| 1355 | serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE); |
| 1356 | writeLong(USER_SERIAL_NUMBER, serialNumber, serializer); |
| 1357 | serializer.startTag(null, ACCOUNT_HANDLE); |
| 1358 | sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer, |
| 1359 | context); |
| 1360 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1361 | serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE); |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | @Override |
| 1367 | public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version, |
| 1368 | Context context) |
| 1369 | throws IOException, XmlPullParserException { |
| 1370 | if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) { |
| 1371 | int outerDepth = parser.getDepth(); |
| 1372 | PhoneAccountHandle accountHandle = null; |
| 1373 | String userSerialNumberString = null; |
| 1374 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1375 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1376 | parser.nextTag(); |
| 1377 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1378 | context); |
| 1379 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1380 | parser.next(); |
| 1381 | userSerialNumberString = parser.getText(); |
| 1382 | } |
| 1383 | } |
| 1384 | UserHandle userHandle = null; |
| 1385 | if (userSerialNumberString != null) { |
| 1386 | try { |
| 1387 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1388 | userHandle = UserManager.get(context) |
| 1389 | .getUserForSerialNumber(serialNumber); |
| 1390 | } catch (NumberFormatException e) { |
| 1391 | Log.e(this, e, |
| 1392 | "Could not parse UserHandle " + userSerialNumberString); |
| 1393 | } |
| 1394 | } |
| 1395 | if (accountHandle != null && userHandle != null) { |
| 1396 | return new DefaultPhoneAccountHandle(userHandle, accountHandle); |
| 1397 | } |
| 1398 | } |
| 1399 | return null; |
| 1400 | } |
| 1401 | }; |
| 1402 | |
| 1403 | |
| 1404 | @VisibleForTesting |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1405 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1406 | new XmlSerialization<PhoneAccount>() { |
| 1407 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1408 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1409 | private static final String ADDRESS = "handle"; |
| 1410 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1411 | private static final String CAPABILITIES = "capabilities"; |
| 1412 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1413 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1414 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1415 | private static final String ICON_TINT = "icon_tint"; |
| 1416 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1417 | private static final String LABEL = "label"; |
| 1418 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1419 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1420 | private static final String ICON = "icon"; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1421 | private static final String EXTRAS = "extras"; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1422 | private static final String ENABLED = "enabled"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1423 | |
| 1424 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1425 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1426 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1427 | if (o != null) { |
| 1428 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1429 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1430 | if (o.getAccountHandle() != null) { |
| 1431 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1432 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1433 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1434 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1435 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1436 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1437 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1438 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1439 | writeIconIfNonNull(ICON, o.getIcon(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1440 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1441 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1442 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1443 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1444 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1445 | writeBundle(EXTRAS, o.getExtras(), serializer); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1446 | writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1447 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1448 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1449 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1450 | } |
| 1451 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1452 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1453 | throws IOException, XmlPullParserException { |
| 1454 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1455 | int outerDepth = parser.getDepth(); |
| 1456 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1457 | Uri address = null; |
| 1458 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1459 | int capabilities = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1460 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1461 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1462 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1463 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1464 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1465 | String label = null; |
| 1466 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1467 | List<String> supportedUriSchemes = null; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1468 | Icon icon = null; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1469 | boolean enabled = false; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1470 | Bundle extras = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1471 | |
| 1472 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1473 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1474 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1475 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1476 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1477 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1478 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1479 | address = Uri.parse(parser.getText()); |
| 1480 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1481 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1482 | String nextText = parser.getText(); |
| 1483 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1484 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1485 | parser.next(); |
| 1486 | capabilities = Integer.parseInt(parser.getText()); |
| 1487 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1488 | parser.next(); |
| 1489 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1490 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1491 | parser.next(); |
| 1492 | iconPackageName = parser.getText(); |
| 1493 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1494 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1495 | iconBitmap = readBitmap(parser); |
| 1496 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1497 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1498 | iconTint = Integer.parseInt(parser.getText()); |
| 1499 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1500 | parser.next(); |
| 1501 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1502 | } else if (parser.getName().equals(LABEL)) { |
| 1503 | parser.next(); |
| 1504 | label = parser.getText(); |
| 1505 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1506 | parser.next(); |
| 1507 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1508 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1509 | supportedUriSchemes = readStringList(parser); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1510 | } else if (parser.getName().equals(ICON)) { |
| 1511 | parser.next(); |
| 1512 | icon = readIcon(parser); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1513 | } else if (parser.getName().equals(ENABLED)) { |
| 1514 | parser.next(); |
| 1515 | enabled = "true".equalsIgnoreCase(parser.getText()); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1516 | } else if (parser.getName().equals(EXTRAS)) { |
| 1517 | parser.next(); |
| 1518 | extras = readBundle(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1519 | } |
| 1520 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1521 | |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1522 | ComponentName pstnComponentName = new ComponentName("com.android.phone", |
| 1523 | "com.android.services.telephony.TelephonyConnectionService"); |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1524 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1525 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1526 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1527 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1528 | if (version < 2) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1529 | supportedUriSchemes = new ArrayList<>(); |
| 1530 | |
| 1531 | // Handle the SIP connection service. |
| 1532 | // Check the system settings to see if it also should handle "tel" calls. |
| 1533 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1534 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1535 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1536 | if (useSipForPstn) { |
| 1537 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1538 | } |
| 1539 | } else { |
| 1540 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1541 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1542 | } |
| 1543 | } |
| 1544 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1545 | // Upgrade older phone accounts with explicit package name |
| 1546 | if (version < 5) { |
| 1547 | if (iconBitmap == null) { |
| 1548 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1549 | } |
| 1550 | } |
| 1551 | |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1552 | if (version < 6) { |
| 1553 | // Always enable all SIP accounts on upgrade to version 6 |
| 1554 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1555 | enabled = true; |
| 1556 | } |
| 1557 | } |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1558 | if (version < 7) { |
| 1559 | // Always enabled all PSTN acocunts on upgrade to version 7 |
| 1560 | if (accountHandle.getComponentName().equals(pstnComponentName)) { |
| 1561 | enabled = true; |
| 1562 | } |
| 1563 | } |
Roshan Pius | 6f752c8 | 2015-08-10 10:42:06 -0700 | [diff] [blame] | 1564 | if (version < 8) { |
| 1565 | // Migrate the SIP account handle ids to use SIP username instead of SIP URI. |
| 1566 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1567 | Uri accountUri = Uri.parse(accountHandle.getId()); |
| 1568 | if (accountUri.getScheme() != null && |
| 1569 | accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) { |
| 1570 | accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(), |
| 1571 | accountUri.getSchemeSpecificPart(), |
| 1572 | accountHandle.getUserHandle()); |
| 1573 | } |
| 1574 | } |
| 1575 | } |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1576 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1577 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1578 | .setAddress(address) |
| 1579 | .setSubscriptionAddress(subscriptionAddress) |
| 1580 | .setCapabilities(capabilities) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1581 | .setShortDescription(shortDescription) |
| 1582 | .setSupportedUriSchemes(supportedUriSchemes) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1583 | .setHighlightColor(highlightColor) |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1584 | .setExtras(extras) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1585 | .setIsEnabled(enabled); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1586 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1587 | if (icon != null) { |
| 1588 | builder.setIcon(icon); |
| 1589 | } else if (iconBitmap != null) { |
| 1590 | builder.setIcon(Icon.createWithBitmap(iconBitmap)); |
| 1591 | } else if (!TextUtils.isEmpty(iconPackageName)) { |
| 1592 | builder.setIcon(Icon.createWithResource(iconPackageName, iconResId)); |
| 1593 | // TODO: Need to set tint. |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
Ihab Awad | 0a4b95f | 2015-05-18 10:15:38 -0700 | [diff] [blame] | 1596 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1597 | } |
| 1598 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1599 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1600 | |
| 1601 | /** |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1602 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN |
| 1603 | * calls. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1604 | * |
| 1605 | * @param context The context. |
| 1606 | * @return {@code True} if SIP should be used for all calls. |
| 1607 | */ |
| 1608 | private boolean useSipForPstnCalls(Context context) { |
| 1609 | String option = Settings.System.getString(context.getContentResolver(), |
| 1610 | Settings.System.SIP_CALL_OPTIONS); |
| 1611 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1612 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1613 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1614 | }; |
| 1615 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1616 | @VisibleForTesting |
| 1617 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1618 | new XmlSerialization<PhoneAccountHandle>() { |
| 1619 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1620 | private static final String COMPONENT_NAME = "component_name"; |
| 1621 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1622 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1623 | |
| 1624 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1625 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1626 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1627 | if (o != null) { |
| 1628 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1629 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1630 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1631 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1632 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1633 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1634 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1635 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1636 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1637 | if (o.getUserHandle() != null && context != null) { |
| 1638 | UserManager userManager = UserManager.get(context); |
| 1639 | writeLong(USER_SERIAL_NUMBER, |
| 1640 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1641 | } |
| 1642 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1643 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1644 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1645 | } |
| 1646 | |
| 1647 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1648 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1649 | throws IOException, XmlPullParserException { |
| 1650 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1651 | String componentNameString = null; |
| 1652 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1653 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1654 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1655 | |
| 1656 | UserManager userManager = UserManager.get(context); |
| 1657 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1658 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1659 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1660 | parser.next(); |
| 1661 | componentNameString = parser.getText(); |
| 1662 | } else if (parser.getName().equals(ID)) { |
| 1663 | parser.next(); |
| 1664 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1665 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1666 | parser.next(); |
| 1667 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1668 | } |
| 1669 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1670 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1671 | UserHandle userHandle = null; |
| 1672 | if (userSerialNumberString != null) { |
| 1673 | try { |
| 1674 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1675 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1676 | } catch (NumberFormatException e) { |
| 1677 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1678 | } |
| 1679 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1680 | return new PhoneAccountHandle( |
| 1681 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1682 | idString, |
| 1683 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1684 | } |
| 1685 | } |
| 1686 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1687 | } |
| 1688 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1689 | } |