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