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