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