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