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. |
| 574 | * @return The phone account handles. |
| 575 | */ |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 576 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts( |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 577 | String uriScheme, boolean includeDisabledAccounts, UserHandle userHandle) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 578 | return getPhoneAccountHandles( |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 579 | PhoneAccount.CAPABILITY_CALL_PROVIDER, |
| 580 | PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /*excludedCapabilities*/, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 581 | uriScheme, null, includeDisabledAccounts, userHandle); |
| 582 | } |
| 583 | |
Tyler Gunn | 66ab9ae | 2017-04-07 15:00:49 -0700 | [diff] [blame] | 584 | /** |
| 585 | * Retrieves a list of all phone accounts which have |
| 586 | * {@link PhoneAccount#CAPABILITY_SELF_MANAGED}. |
| 587 | * <p> |
| 588 | * Returns only the {@link PhoneAccount}s which are enabled as self-managed accounts are |
| 589 | * automatically enabled by default (see {@link #registerPhoneAccount(PhoneAccount)}). |
| 590 | * |
| 591 | * @param userHandle User handle of phone account owner. |
| 592 | * @return The phone account handles. |
| 593 | */ |
| 594 | public List<PhoneAccountHandle> getSelfManagedPhoneAccounts(UserHandle userHandle) { |
| 595 | return getPhoneAccountHandles( |
| 596 | PhoneAccount.CAPABILITY_SELF_MANAGED, |
| 597 | PhoneAccount.CAPABILITY_EMERGENCY_CALLS_ONLY /* excludedCapabilities */, |
| 598 | null /* uriScheme */, null /* packageName */, false /* includeDisabledAccounts */, |
| 599 | userHandle); |
| 600 | } |
| 601 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 602 | public List<PhoneAccountHandle> getCallCapablePhoneAccountsOfCurrentUser( |
| 603 | String uriScheme, boolean includeDisabledAccounts) { |
| 604 | return getCallCapablePhoneAccounts(uriScheme, includeDisabledAccounts, mCurrentUserHandle); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | /** |
| 608 | * Retrieves a list of all the SIM-based phone accounts. |
| 609 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 610 | public List<PhoneAccountHandle> getSimPhoneAccounts(UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 611 | return getPhoneAccountHandles( |
| 612 | PhoneAccount.CAPABILITY_CALL_PROVIDER | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 613 | null, null, false, userHandle); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 614 | } |
| 615 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 616 | public List<PhoneAccountHandle> getSimPhoneAccountsOfCurrentUser() { |
| 617 | return getSimPhoneAccounts(mCurrentUserHandle); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Retrieves a list of all phone accounts registered by a specified package. |
| 622 | * |
| 623 | * @param packageName The name of the package that registered the phone accounts. |
| 624 | * @return The phone account handles. |
| 625 | */ |
| 626 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName, |
| 627 | UserHandle userHandle) { |
| 628 | return getPhoneAccountHandles(0, null, packageName, false, userHandle); |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 631 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 632 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 633 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 634 | // Enforce the requirement that a connection service for a phone account has the correct |
| 635 | // permission. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 636 | if (!phoneAccountRequiresBindPermission(account.getAccountHandle())) { |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 637 | Log.w(this, |
| 638 | "Phone account %s does not have BIND_TELECOM_CONNECTION_SERVICE permission.", |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 639 | account.getAccountHandle()); |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 640 | throw new SecurityException("PhoneAccount connection service requires " |
| 641 | + "BIND_TELECOM_CONNECTION_SERVICE permission."); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 642 | } |
| 643 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 644 | addOrReplacePhoneAccount(account); |
| 645 | } |
| 646 | |
| 647 | /** |
| 648 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 649 | * |
| 650 | * @param account The {@code PhoneAccount} to add or replace. |
| 651 | */ |
| 652 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 653 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 654 | account.getAccountHandle(), account); |
| 655 | |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 656 | // Start _enabled_ property as false. |
| 657 | // !!! IMPORTANT !!! It is important that we do not read the enabled state that the |
| 658 | // source app provides or else an third party app could enable itself. |
| 659 | boolean isEnabled = false; |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 660 | boolean isNewAccount; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 661 | |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 662 | PhoneAccount oldAccount = getPhoneAccountUnchecked(account.getAccountHandle()); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 663 | if (oldAccount != null) { |
| 664 | mState.accounts.remove(oldAccount); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 665 | isEnabled = oldAccount.isEnabled(); |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 666 | Log.i(this, "Modify account: %s", getAccountDiffString(account, oldAccount)); |
| 667 | isNewAccount = false; |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 668 | } else { |
| 669 | Log.i(this, "New phone account registered: " + account); |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 670 | isNewAccount = true; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 671 | } |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 672 | |
Tyler Gunn | 2b17f23 | 2017-03-08 08:51:00 -0800 | [diff] [blame] | 673 | // When registering a self-managed PhoneAccount we enforce the rule that the label that the |
| 674 | // app uses is also its phone account label. Also ensure it does not attempt to declare |
| 675 | // itself as a sim acct, call manager or call provider. |
| 676 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)) { |
| 677 | // Turn off bits we don't want to be able to set (TelecomServiceImpl protects against |
| 678 | // this but we'll also prevent it from happening here, just to be safe). |
| 679 | int newCapabilities = account.getCapabilities() & |
| 680 | ~(PhoneAccount.CAPABILITY_CALL_PROVIDER | |
| 681 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER | |
| 682 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION); |
| 683 | |
| 684 | // Ensure name is correct. |
| 685 | CharSequence newLabel = mAppLabelProxy.getAppLabel( |
| 686 | account.getAccountHandle().getComponentName().getPackageName()); |
| 687 | |
| 688 | account = account.toBuilder() |
| 689 | .setLabel(newLabel) |
| 690 | .setCapabilities(newCapabilities) |
| 691 | .build(); |
| 692 | } |
| 693 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 694 | mState.accounts.add(account); |
Srikanth Chintala | d5d2dd9 | 2016-06-23 16:02:33 +0530 | [diff] [blame^] | 695 | if (mState.accounts.size() > 1) { |
| 696 | mState.accounts.sort(new Comparator<PhoneAccount>() { |
| 697 | public int compare(PhoneAccount phoneaccount1, PhoneAccount phoneaccount2) { |
| 698 | if (phoneaccount1.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 699 | && phoneaccount2 |
| 700 | .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 701 | int slotId1 = phoneaccount1.getExtras() |
| 702 | .getInt(PhoneAccount.EXTRA_SORT_ORDER); |
| 703 | int slotId2 = phoneaccount2.getExtras() |
| 704 | .getInt(PhoneAccount.EXTRA_SORT_ORDER); |
| 705 | return (slotId1 < slotId2 ? -1 : (slotId1 == slotId2 ? 0 : 1)); |
| 706 | } else if (phoneaccount1 |
| 707 | .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 708 | && !phoneaccount2 |
| 709 | .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 710 | return -1; |
| 711 | } else if (!phoneaccount1 |
| 712 | .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 713 | && phoneaccount2 |
| 714 | .hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 715 | return 1; |
| 716 | } else { |
| 717 | return nullToEmpty(phoneaccount1.getLabel().toString()) |
| 718 | .compareToIgnoreCase( |
| 719 | nullToEmpty(phoneaccount2.getLabel().toString())); |
| 720 | } |
| 721 | } |
| 722 | }); |
| 723 | } |
| 724 | |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 725 | // Set defaults and replace based on the group Id. |
| 726 | maybeReplaceOldAccount(account); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 727 | // 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] | 728 | // or _true_ if this is a SIM-based account. All SIM-based accounts are always enabled, |
| 729 | // as are all self-managed phone accounts. |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 730 | account.setIsEnabled( |
Tyler Gunn | acb3bc8 | 2017-01-09 09:43:56 -0800 | [diff] [blame] | 731 | isEnabled || account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 732 | || account.hasCapabilities(PhoneAccount.CAPABILITY_SELF_MANAGED)); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 733 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 734 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 735 | fireAccountsChanged(); |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 736 | if (isNewAccount) { |
| 737 | fireAccountRegistered(account.getAccountHandle()); |
| 738 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 739 | } |
| 740 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 741 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 742 | PhoneAccount account = getPhoneAccountUnchecked(accountHandle); |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 743 | if (account != null) { |
| 744 | if (mState.accounts.remove(account)) { |
| 745 | write(); |
| 746 | fireAccountsChanged(); |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 747 | fireAccountUnRegistered(accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 748 | } |
| 749 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 752 | /** |
| 753 | * Un-registers all phone accounts associated with a specified package. |
| 754 | * |
| 755 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 756 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 757 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 758 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 759 | boolean accountsRemoved = false; |
| 760 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 761 | while (it.hasNext()) { |
| 762 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 763 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 764 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 765 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 766 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 767 | mState.accounts.remove(phoneAccount); |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 768 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 769 | } |
| 770 | } |
| 771 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 772 | if (accountsRemoved) { |
| 773 | write(); |
| 774 | fireAccountsChanged(); |
| 775 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 776 | } |
| 777 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 778 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 779 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Yorke Lee | b195723 | 2015-06-30 10:09:54 -0700 | [diff] [blame] | 780 | return PhoneNumberUtils.isVoiceMailNumber(mContext, subId, number); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 783 | public void addListener(Listener l) { |
| 784 | mListeners.add(l); |
| 785 | } |
| 786 | |
| 787 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 788 | if (l != null) { |
| 789 | mListeners.remove(l); |
| 790 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 791 | } |
| 792 | |
Tyler Gunn | ea4c6fb | 2017-04-14 13:46:21 -0700 | [diff] [blame] | 793 | private void fireAccountRegistered(PhoneAccountHandle handle) { |
| 794 | for (Listener l : mListeners) { |
| 795 | l.onPhoneAccountRegistered(this, handle); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | private void fireAccountUnRegistered(PhoneAccountHandle handle) { |
| 800 | for (Listener l : mListeners) { |
| 801 | l.onPhoneAccountUnRegistered(this, handle); |
| 802 | } |
| 803 | } |
| 804 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 805 | private void fireAccountsChanged() { |
| 806 | for (Listener l : mListeners) { |
| 807 | l.onAccountsChanged(this); |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | private void fireDefaultOutgoingChanged() { |
| 812 | for (Listener l : mListeners) { |
| 813 | l.onDefaultOutgoingChanged(this); |
| 814 | } |
| 815 | } |
| 816 | |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 817 | private String getAccountDiffString(PhoneAccount account1, PhoneAccount account2) { |
| 818 | if (account1 == null || account2 == null) { |
| 819 | return "Diff: " + account1 + ", " + account2; |
| 820 | } |
| 821 | |
| 822 | StringBuffer sb = new StringBuffer(); |
| 823 | sb.append("[").append(account1.getAccountHandle()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 824 | appendDiff(sb, "addr", Log.piiHandle(account1.getAddress()), |
| 825 | Log.piiHandle(account2.getAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 826 | appendDiff(sb, "cap", account1.getCapabilities(), account2.getCapabilities()); |
| 827 | appendDiff(sb, "hl", account1.getHighlightColor(), account2.getHighlightColor()); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 828 | appendDiff(sb, "lbl", account1.getLabel(), account2.getLabel()); |
| 829 | appendDiff(sb, "desc", account1.getShortDescription(), account2.getShortDescription()); |
Anthony Lee | 7c64a4e | 2015-08-25 09:27:47 -0700 | [diff] [blame] | 830 | appendDiff(sb, "subAddr", Log.piiHandle(account1.getSubscriptionAddress()), |
| 831 | Log.piiHandle(account2.getSubscriptionAddress())); |
Santos Cordon | 5fa4e4f | 2015-06-10 14:56:01 -0700 | [diff] [blame] | 832 | appendDiff(sb, "uris", account1.getSupportedUriSchemes(), |
| 833 | account2.getSupportedUriSchemes()); |
| 834 | sb.append("]"); |
| 835 | return sb.toString(); |
| 836 | } |
| 837 | |
| 838 | private void appendDiff(StringBuffer sb, String attrName, Object obj1, Object obj2) { |
| 839 | if (!Objects.equals(obj1, obj2)) { |
| 840 | sb.append("(") |
| 841 | .append(attrName) |
| 842 | .append(": ") |
| 843 | .append(obj1) |
| 844 | .append(" -> ") |
| 845 | .append(obj2) |
| 846 | .append(")"); |
| 847 | } |
| 848 | } |
| 849 | |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 850 | private void maybeReplaceOldAccount(PhoneAccount newAccount) { |
| 851 | UserHandle newAccountUserHandle = newAccount.getAccountHandle().getUserHandle(); |
| 852 | DefaultPhoneAccountHandle defaultHandle = |
| 853 | getUserSelectedDefaultPhoneAccount(newAccountUserHandle); |
| 854 | if (defaultHandle == null || defaultHandle.groupId.isEmpty()) { |
| 855 | Log.v(this, "maybeReplaceOldAccount: Not replacing PhoneAccount, no group Id or " + |
| 856 | "default."); |
| 857 | return; |
| 858 | } |
| 859 | if (!defaultHandle.groupId.equals(newAccount.getGroupId())) { |
| 860 | Log.v(this, "maybeReplaceOldAccount: group Ids are not equal."); |
| 861 | return; |
| 862 | } |
| 863 | if (Objects.equals(newAccount.getAccountHandle().getComponentName(), |
| 864 | defaultHandle.phoneAccountHandle.getComponentName())) { |
| 865 | // Move default calling account over to new user, since the ComponentNames and Group Ids |
| 866 | // are the same. |
| 867 | setUserSelectedOutgoingPhoneAccount(newAccount.getAccountHandle(), |
| 868 | newAccountUserHandle); |
| 869 | } else { |
| 870 | Log.v(this, "maybeReplaceOldAccount: group Ids are equal, but ComponentName is not" + |
| 871 | " the same as the default. Not replacing default PhoneAccount."); |
| 872 | } |
| 873 | PhoneAccount replacementAccount = getPhoneAccountByGroupId(newAccount.getGroupId(), |
| 874 | newAccount.getAccountHandle().getComponentName(), newAccountUserHandle, |
| 875 | newAccount.getAccountHandle()); |
| 876 | if (replacementAccount != null) { |
| 877 | // Unregister the old PhoneAccount. |
| 878 | Log.v(this, "maybeReplaceOldAccount: Unregistering old PhoneAccount: " + |
| 879 | replacementAccount.getAccountHandle()); |
| 880 | unregisterPhoneAccount(replacementAccount.getAccountHandle()); |
| 881 | } |
| 882 | } |
| 883 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 884 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 885 | * Determines if the connection service specified by a {@link PhoneAccountHandle} requires the |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 886 | * {@link Manifest.permission#BIND_TELECOM_CONNECTION_SERVICE} permission. |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 887 | * |
| 888 | * @param phoneAccountHandle The phone account to check. |
| 889 | * @return {@code True} if the phone account has permission. |
| 890 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 891 | public boolean phoneAccountRequiresBindPermission(PhoneAccountHandle phoneAccountHandle) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 892 | List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle); |
| 893 | if (resolveInfos.isEmpty()) { |
| 894 | Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName()); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 895 | return false; |
| 896 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 897 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 898 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
Yorke Lee | 7bb8ce9 | 2015-05-13 16:28:29 -0700 | [diff] [blame] | 899 | if (serviceInfo == null) { |
| 900 | return false; |
| 901 | } |
| 902 | |
| 903 | if (!Manifest.permission.BIND_CONNECTION_SERVICE.equals(serviceInfo.permission) && |
| 904 | !Manifest.permission.BIND_TELECOM_CONNECTION_SERVICE.equals( |
| 905 | serviceInfo.permission)) { |
| 906 | // The ConnectionService must require either the deprecated BIND_CONNECTION_SERVICE, |
| 907 | // or the public BIND_TELECOM_CONNECTION_SERVICE permissions, both of which are |
| 908 | // system/signature only. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame] | 909 | return false; |
| 910 | } |
| 911 | } |
| 912 | return true; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 915 | // |
| 916 | // Methods for retrieving PhoneAccounts and PhoneAccountHandles |
| 917 | // |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 918 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 919 | /** |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 920 | * Returns the PhoneAccount for the specified handle. Does no user checking. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 921 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 922 | * @param handle |
| 923 | * @return The corresponding phone account if one exists. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 924 | */ |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 925 | public PhoneAccount getPhoneAccountUnchecked(PhoneAccountHandle handle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 926 | for (PhoneAccount m : mState.accounts) { |
| 927 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 928 | return m; |
| 929 | } |
| 930 | } |
| 931 | return null; |
| 932 | } |
| 933 | |
| 934 | /** |
| 935 | * Like getPhoneAccount, but checks to see if the current user is allowed to see the phone |
| 936 | * account before returning it. The current user is the active user on the actual android |
| 937 | * device. |
| 938 | */ |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 939 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, UserHandle userHandle) { |
| 940 | return getPhoneAccount(handle, userHandle, /* acrossProfiles */ false); |
| 941 | } |
| 942 | |
| 943 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle, |
| 944 | UserHandle userHandle, boolean acrossProfiles) { |
Tony Mak | 98e6bdc | 2015-11-25 21:54:05 +0000 | [diff] [blame] | 945 | PhoneAccount account = getPhoneAccountUnchecked(handle); |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 946 | if (account != null && (isVisibleForUser(account, userHandle, acrossProfiles))) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 947 | return account; |
| 948 | } |
| 949 | return null; |
| 950 | } |
| 951 | |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 952 | public PhoneAccount getPhoneAccountOfCurrentUser(PhoneAccountHandle handle) { |
| 953 | return getPhoneAccount(handle, mCurrentUserHandle); |
| 954 | } |
| 955 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 956 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
| 957 | int capabilities, |
| 958 | String uriScheme, |
| 959 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 960 | boolean includeDisabledAccounts, |
| 961 | UserHandle userHandle) { |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 962 | return getPhoneAccountHandles(capabilities, 0 /*excludedCapabilities*/, uriScheme, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 963 | packageName, includeDisabledAccounts, userHandle); |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 966 | /** |
| 967 | * Returns a list of phone account handles with the specified capabilities, uri scheme, |
| 968 | * and package name. |
| 969 | */ |
| 970 | private List<PhoneAccountHandle> getPhoneAccountHandles( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 971 | int capabilities, |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 972 | int excludedCapabilities, |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 973 | String uriScheme, |
| 974 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 975 | boolean includeDisabledAccounts, |
| 976 | UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 977 | List<PhoneAccountHandle> handles = new ArrayList<>(); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 978 | |
| 979 | for (PhoneAccount account : getPhoneAccounts( |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 980 | capabilities, excludedCapabilities, uriScheme, packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 981 | includeDisabledAccounts, userHandle)) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 982 | handles.add(account.getAccountHandle()); |
| 983 | } |
| 984 | return handles; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 985 | } |
| 986 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 987 | private List<PhoneAccount> getPhoneAccounts( |
| 988 | int capabilities, |
| 989 | String uriScheme, |
| 990 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 991 | boolean includeDisabledAccounts, |
| 992 | UserHandle userHandle) { |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 993 | return getPhoneAccounts(capabilities, 0 /*excludedCapabilities*/, uriScheme, packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 994 | includeDisabledAccounts, userHandle); |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 997 | /** |
| 998 | * 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] | 999 | * URI scheme, within the specified package name. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1000 | * |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1001 | * @param capabilities Capabilities which the {@code PhoneAccount} must have. Ignored if 0. |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 1002 | * @param excludedCapabilities Capabilities which the {@code PhoneAccount} must not have. |
| 1003 | * Ignored if 0. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1004 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code null} bypasses the |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1005 | * URI scheme check. |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1006 | * @param packageName Package name of the PhoneAccount. {@code null} bypasses packageName check. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1007 | */ |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1008 | private List<PhoneAccount> getPhoneAccounts( |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1009 | int capabilities, |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 1010 | int excludedCapabilities, |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1011 | String uriScheme, |
| 1012 | String packageName, |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 1013 | boolean includeDisabledAccounts, |
| 1014 | UserHandle userHandle) { |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1015 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1016 | for (PhoneAccount m : mState.accounts) { |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1017 | if (!(m.isEnabled() || includeDisabledAccounts)) { |
| 1018 | // Do not include disabled accounts. |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
Bryce Lee | a0bb705 | 2015-10-16 13:31:40 -0700 | [diff] [blame] | 1022 | if ((m.getCapabilities() & excludedCapabilities) != 0) { |
| 1023 | // If an excluded capability is present, skip. |
| 1024 | continue; |
| 1025 | } |
| 1026 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1027 | if (capabilities != 0 && !m.hasCapabilities(capabilities)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1028 | // Account doesn't have the right capabilities; skip this one. |
| 1029 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 1030 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1031 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 1032 | // Account doesn't support this URI scheme; skip this one. |
| 1033 | continue; |
| 1034 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1035 | PhoneAccountHandle handle = m.getAccountHandle(); |
| 1036 | |
| 1037 | if (resolveComponent(handle).isEmpty()) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1038 | // This component cannot be resolved anymore; skip this one. |
| 1039 | continue; |
| 1040 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1041 | if (packageName != null && |
| 1042 | !packageName.equals(handle.getComponentName().getPackageName())) { |
| 1043 | // Not the right package name; skip this one. |
| 1044 | continue; |
| 1045 | } |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 1046 | if (!isVisibleForUser(m, userHandle, false)) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1047 | // Account is not visible for the current user; skip this one. |
| 1048 | continue; |
| 1049 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1050 | accounts.add(m); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1051 | } |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1052 | return accounts; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1053 | } |
| 1054 | |
Santos Cordon | 6a21264 | 2015-05-08 16:35:23 -0700 | [diff] [blame] | 1055 | // |
| 1056 | // State Implementation for PhoneAccountRegistrar |
| 1057 | // |
| 1058 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1059 | /** |
| 1060 | * The state of this {@code PhoneAccountRegistrar}. |
| 1061 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1062 | @VisibleForTesting |
| 1063 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1064 | /** |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1065 | * Store the default phone account handle of users. If no record of a user can be found in |
| 1066 | * 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] | 1067 | */ |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1068 | public final Map<UserHandle, DefaultPhoneAccountHandle> defaultOutgoingAccountHandles |
| 1069 | = new ConcurrentHashMap<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1070 | |
| 1071 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 1072 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1073 | */ |
Shriram Ganesh | 8f89f8d | 2015-04-15 12:35:43 +0530 | [diff] [blame] | 1074 | public final List<PhoneAccount> accounts = new CopyOnWriteArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1075 | |
| 1076 | /** |
| 1077 | * The version number of the State data. |
| 1078 | */ |
| 1079 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 1082 | /** |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1083 | * The default {@link PhoneAccountHandle} of a user. |
| 1084 | */ |
| 1085 | public static class DefaultPhoneAccountHandle { |
| 1086 | |
| 1087 | public final UserHandle userHandle; |
| 1088 | |
| 1089 | public final PhoneAccountHandle phoneAccountHandle; |
| 1090 | |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1091 | public final String groupId; |
| 1092 | |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1093 | public DefaultPhoneAccountHandle(UserHandle userHandle, |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1094 | PhoneAccountHandle phoneAccountHandle, String groupId) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1095 | this.userHandle = userHandle; |
| 1096 | this.phoneAccountHandle = phoneAccountHandle; |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1097 | this.groupId = groupId; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1098 | } |
| 1099 | } |
| 1100 | |
| 1101 | /** |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 1102 | * Dumps the state of the {@link CallsManager}. |
| 1103 | * |
| 1104 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 1105 | */ |
| 1106 | public void dump(IndentingPrintWriter pw) { |
| 1107 | if (mState != null) { |
| 1108 | pw.println("xmlVersion: " + mState.versionNumber); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1109 | DefaultPhoneAccountHandle defaultPhoneAccountHandle |
| 1110 | = mState.defaultOutgoingAccountHandles.get(Process.myUserHandle()); |
| 1111 | pw.println("defaultOutgoing: " + (defaultPhoneAccountHandle == null ? "none" : |
| 1112 | defaultPhoneAccountHandle.phoneAccountHandle)); |
Tony Mak | 240656f | 2015-12-04 11:36:22 +0000 | [diff] [blame] | 1113 | pw.println("simCallManager: " + getSimCallManager(mCurrentUserHandle)); |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 1114 | pw.println("phoneAccounts:"); |
| 1115 | pw.increaseIndent(); |
| 1116 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 1117 | pw.println(phoneAccount); |
| 1118 | } |
| 1119 | pw.decreaseIndent(); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1123 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 1124 | // |
| 1125 | // State management |
| 1126 | // |
| 1127 | |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame] | 1128 | private class AsyncXmlWriter extends AsyncTask<ByteArrayOutputStream, Void, Void> { |
| 1129 | @Override |
| 1130 | public Void doInBackground(ByteArrayOutputStream... args) { |
| 1131 | final ByteArrayOutputStream buffer = args[0]; |
| 1132 | FileOutputStream fileOutput = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1133 | try { |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame] | 1134 | synchronized (mWriteLock) { |
| 1135 | fileOutput = mAtomicFile.startWrite(); |
| 1136 | buffer.writeTo(fileOutput); |
| 1137 | mAtomicFile.finishWrite(fileOutput); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1138 | } |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame] | 1139 | } catch (IOException e) { |
| 1140 | Log.e(this, e, "Writing state to XML file"); |
| 1141 | mAtomicFile.failWrite(fileOutput); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1142 | } |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame] | 1143 | return null; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | private void write() { |
| 1148 | try { |
| 1149 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 1150 | XmlSerializer serializer = new FastXmlSerializer(); |
| 1151 | serializer.setOutput(os, "utf-8"); |
| 1152 | writeToXml(mState, serializer, mContext); |
| 1153 | serializer.flush(); |
| 1154 | new AsyncXmlWriter().execute(os); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1155 | } catch (IOException e) { |
Hall Liu | 3f7f64d | 2016-01-07 17:40:13 -0800 | [diff] [blame] | 1156 | Log.e(this, e, "Writing state to XML buffer"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1160 | private void read() { |
| 1161 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1162 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1163 | is = mAtomicFile.openRead(); |
| 1164 | } catch (FileNotFoundException ex) { |
| 1165 | return; |
| 1166 | } |
| 1167 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1168 | boolean versionChanged = false; |
| 1169 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1170 | XmlPullParser parser; |
| 1171 | try { |
| 1172 | parser = Xml.newPullParser(); |
| 1173 | parser.setInput(new BufferedInputStream(is), null); |
| 1174 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1175 | mState = readFromXml(parser, mContext); |
| 1176 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 1177 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1178 | } catch (IOException | XmlPullParserException e) { |
| 1179 | Log.e(this, e, "Reading state from XML file"); |
| 1180 | mState = new State(); |
| 1181 | } finally { |
| 1182 | try { |
| 1183 | is.close(); |
| 1184 | } catch (IOException e) { |
| 1185 | Log.e(this, e, "Closing InputStream"); |
| 1186 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1187 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1188 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1189 | // Verify all of the UserHandles. |
| 1190 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 1191 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 1192 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 1193 | if (userHandle == null) { |
| 1194 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 1195 | badAccounts.add(phoneAccount); |
| 1196 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 1197 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 1198 | badAccounts.add(phoneAccount); |
| 1199 | } |
| 1200 | } |
| 1201 | mState.accounts.removeAll(badAccounts); |
| 1202 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1203 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1204 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1205 | write(); |
| 1206 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1207 | } |
| 1208 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1209 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1210 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1211 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1212 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1213 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1214 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1215 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1216 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1217 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1218 | } |
| 1219 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1220 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1221 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1222 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1223 | // |
| 1224 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1225 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1226 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1227 | private static final String TAG_VALUE = "value"; |
| 1228 | private static final String ATTRIBUTE_LENGTH = "length"; |
| 1229 | private static final String ATTRIBUTE_KEY = "key"; |
| 1230 | private static final String ATTRIBUTE_VALUE_TYPE = "type"; |
| 1231 | private static final String VALUE_TYPE_STRING = "string"; |
| 1232 | private static final String VALUE_TYPE_INTEGER = "integer"; |
| 1233 | private static final String VALUE_TYPE_BOOLEAN = "boolean"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1234 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1235 | /** |
| 1236 | * Write the supplied object to XML |
| 1237 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1238 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1239 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1240 | |
| 1241 | /** |
| 1242 | * Read from the supplied XML into a new object, returning null in case of an |
| 1243 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 1244 | * positioned at the first tag that is expected to have been emitted by this |
| 1245 | * object's writeToXml(). This object tries to fail early without modifying |
| 1246 | * 'parser' if it does not recognize the data it sees. |
| 1247 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1248 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1249 | throws IOException, XmlPullParserException; |
| 1250 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1251 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1252 | throws IOException { |
| 1253 | if (value != null) { |
| 1254 | serializer.startTag(null, tagName); |
| 1255 | serializer.text(Objects.toString(value)); |
| 1256 | serializer.endTag(null, tagName); |
| 1257 | } |
| 1258 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1259 | |
| 1260 | /** |
| 1261 | * Serializes a string array. |
| 1262 | * |
| 1263 | * @param tagName The tag name for the string array. |
| 1264 | * @param values The string values to serialize. |
| 1265 | * @param serializer The serializer. |
| 1266 | * @throws IOException |
| 1267 | */ |
| 1268 | protected void writeStringList(String tagName, List<String> values, |
| 1269 | XmlSerializer serializer) |
| 1270 | throws IOException { |
| 1271 | |
| 1272 | serializer.startTag(null, tagName); |
| 1273 | if (values != null) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1274 | serializer.attribute(null, ATTRIBUTE_LENGTH, Objects.toString(values.size())); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1275 | for (String toSerialize : values) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1276 | serializer.startTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1277 | if (toSerialize != null ){ |
| 1278 | serializer.text(toSerialize); |
| 1279 | } |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1280 | serializer.endTag(null, TAG_VALUE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1281 | } |
| 1282 | } else { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1283 | serializer.attribute(null, ATTRIBUTE_LENGTH, "0"); |
| 1284 | } |
| 1285 | serializer.endTag(null, tagName); |
| 1286 | } |
| 1287 | |
| 1288 | protected void writeBundle(String tagName, Bundle values, XmlSerializer serializer) |
| 1289 | throws IOException { |
| 1290 | |
| 1291 | serializer.startTag(null, tagName); |
| 1292 | if (values != null) { |
| 1293 | for (String key : values.keySet()) { |
| 1294 | Object value = values.get(key); |
| 1295 | |
| 1296 | if (value == null) { |
| 1297 | continue; |
| 1298 | } |
| 1299 | |
| 1300 | String valueType; |
| 1301 | if (value instanceof String) { |
| 1302 | valueType = VALUE_TYPE_STRING; |
| 1303 | } else if (value instanceof Integer) { |
| 1304 | valueType = VALUE_TYPE_INTEGER; |
| 1305 | } else if (value instanceof Boolean) { |
| 1306 | valueType = VALUE_TYPE_BOOLEAN; |
| 1307 | } else { |
| 1308 | Log.w(this, |
| 1309 | "PhoneAccounts support only string, integer and boolean extras TY."); |
| 1310 | continue; |
| 1311 | } |
| 1312 | |
| 1313 | serializer.startTag(null, TAG_VALUE); |
| 1314 | serializer.attribute(null, ATTRIBUTE_KEY, key); |
| 1315 | serializer.attribute(null, ATTRIBUTE_VALUE_TYPE, valueType); |
| 1316 | serializer.text(Objects.toString(value)); |
| 1317 | serializer.endTag(null, TAG_VALUE); |
| 1318 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1319 | } |
| 1320 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1321 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1322 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1323 | protected void writeIconIfNonNull(String tagName, Icon value, XmlSerializer serializer) |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1324 | throws IOException { |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1325 | if (value != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1326 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1327 | value.writeToStream(stream); |
| 1328 | byte[] iconByteArray = stream.toByteArray(); |
| 1329 | String text = Base64.encodeToString(iconByteArray, 0, iconByteArray.length, 0); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1330 | |
| 1331 | serializer.startTag(null, tagName); |
| 1332 | serializer.text(text); |
| 1333 | serializer.endTag(null, tagName); |
| 1334 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1337 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 1338 | throws IOException { |
| 1339 | serializer.startTag(null, tagName); |
| 1340 | serializer.text(Long.valueOf(value).toString()); |
| 1341 | serializer.endTag(null, tagName); |
| 1342 | } |
| 1343 | |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1344 | protected void writeNonNullString(String tagName, String value, XmlSerializer serializer) |
| 1345 | throws IOException { |
| 1346 | serializer.startTag(null, tagName); |
| 1347 | serializer.text(value != null ? value : ""); |
| 1348 | serializer.endTag(null, tagName); |
| 1349 | } |
| 1350 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1351 | /** |
| 1352 | * Reads a string array from the XML parser. |
| 1353 | * |
| 1354 | * @param parser The XML parser. |
| 1355 | * @return String array containing the parsed values. |
| 1356 | * @throws IOException Exception related to IO. |
| 1357 | * @throws XmlPullParserException Exception related to parsing. |
| 1358 | */ |
| 1359 | protected List<String> readStringList(XmlPullParser parser) |
| 1360 | throws IOException, XmlPullParserException { |
| 1361 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1362 | int length = Integer.parseInt(parser.getAttributeValue(null, ATTRIBUTE_LENGTH)); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1363 | List<String> arrayEntries = new ArrayList<String>(length); |
| 1364 | String value = null; |
| 1365 | |
| 1366 | if (length == 0) { |
| 1367 | return arrayEntries; |
| 1368 | } |
| 1369 | |
| 1370 | int outerDepth = parser.getDepth(); |
| 1371 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1372 | if (parser.getName().equals(TAG_VALUE)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 1373 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1374 | value = parser.getText(); |
| 1375 | arrayEntries.add(value); |
| 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | return arrayEntries; |
| 1380 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1381 | |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1382 | /** |
| 1383 | * Reads a bundle from the XML parser. |
| 1384 | * |
| 1385 | * @param parser The XML parser. |
| 1386 | * @return Bundle containing the parsed values. |
| 1387 | * @throws IOException Exception related to IO. |
| 1388 | * @throws XmlPullParserException Exception related to parsing. |
| 1389 | */ |
| 1390 | protected Bundle readBundle(XmlPullParser parser) |
| 1391 | throws IOException, XmlPullParserException { |
| 1392 | |
| 1393 | Bundle bundle = null; |
| 1394 | int outerDepth = parser.getDepth(); |
| 1395 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1396 | if (parser.getName().equals(TAG_VALUE)) { |
| 1397 | String valueType = parser.getAttributeValue(null, ATTRIBUTE_VALUE_TYPE); |
| 1398 | String key = parser.getAttributeValue(null, ATTRIBUTE_KEY); |
| 1399 | parser.next(); |
| 1400 | String value = parser.getText(); |
| 1401 | |
| 1402 | if (bundle == null) { |
| 1403 | bundle = new Bundle(); |
| 1404 | } |
| 1405 | |
| 1406 | // Do not write null values to the bundle. |
| 1407 | if (value == null) { |
| 1408 | continue; |
| 1409 | } |
| 1410 | |
| 1411 | if (VALUE_TYPE_STRING.equals(valueType)) { |
| 1412 | bundle.putString(key, value); |
| 1413 | } else if (VALUE_TYPE_INTEGER.equals(valueType)) { |
| 1414 | try { |
| 1415 | int intValue = Integer.parseInt(value); |
| 1416 | bundle.putInt(key, intValue); |
| 1417 | } catch (NumberFormatException nfe) { |
| 1418 | Log.w(this, "Invalid integer PhoneAccount extra."); |
| 1419 | } |
| 1420 | } else if (VALUE_TYPE_BOOLEAN.equals(valueType)) { |
| 1421 | boolean boolValue = Boolean.parseBoolean(value); |
| 1422 | bundle.putBoolean(key, boolValue); |
| 1423 | } else { |
| 1424 | Log.w(this, "Invalid type " + valueType + " for PhoneAccount bundle."); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
| 1428 | return bundle; |
| 1429 | } |
| 1430 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1431 | protected Bitmap readBitmap(XmlPullParser parser) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1432 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 1433 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 1434 | } |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1435 | |
| 1436 | protected Icon readIcon(XmlPullParser parser) throws IOException { |
| 1437 | byte[] iconByteArray = Base64.decode(parser.getText(), 0); |
| 1438 | ByteArrayInputStream stream = new ByteArrayInputStream(iconByteArray); |
| 1439 | return Icon.createFromStream(stream); |
| 1440 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1443 | @VisibleForTesting |
| 1444 | public static final XmlSerialization<State> sStateXml = |
| 1445 | new XmlSerialization<State>() { |
| 1446 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1447 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 1448 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1449 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1450 | |
| 1451 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1452 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1453 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1454 | if (o != null) { |
| 1455 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1456 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1457 | |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1458 | serializer.startTag(null, DEFAULT_OUTGOING); |
| 1459 | for (DefaultPhoneAccountHandle defaultPhoneAccountHandle : o |
| 1460 | .defaultOutgoingAccountHandles.values()) { |
| 1461 | sDefaultPhoneAcountHandleXml |
| 1462 | .writeToXml(defaultPhoneAccountHandle, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1463 | } |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1464 | serializer.endTag(null, DEFAULT_OUTGOING); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1465 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1466 | serializer.startTag(null, ACCOUNTS); |
| 1467 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1468 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1469 | } |
| 1470 | serializer.endTag(null, ACCOUNTS); |
| 1471 | |
| 1472 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 1473 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1477 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1478 | throws IOException, XmlPullParserException { |
| 1479 | if (parser.getName().equals(CLASS_STATE)) { |
| 1480 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1481 | |
| 1482 | String rawVersion = parser.getAttributeValue(null, VERSION); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1483 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : Integer.parseInt(rawVersion); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1484 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1485 | int outerDepth = parser.getDepth(); |
| 1486 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1487 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1488 | if (s.versionNumber < 9) { |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1489 | // Migrate old default phone account handle here by assuming the |
| 1490 | // default phone account handle belongs to the primary user. Also, |
| 1491 | // assume there are no groups. |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1492 | parser.nextTag(); |
| 1493 | PhoneAccountHandle phoneAccountHandle = sPhoneAccountHandleXml |
| 1494 | .readFromXml(parser, s.versionNumber, context); |
| 1495 | UserManager userManager = UserManager.get(context); |
| 1496 | UserInfo primaryUser = userManager.getPrimaryUser(); |
| 1497 | if (primaryUser != null) { |
| 1498 | UserHandle userHandle = primaryUser.getUserHandle(); |
| 1499 | DefaultPhoneAccountHandle defaultPhoneAccountHandle |
| 1500 | = new DefaultPhoneAccountHandle(userHandle, |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1501 | phoneAccountHandle, "" /* groupId */); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1502 | s.defaultOutgoingAccountHandles |
| 1503 | .put(userHandle, defaultPhoneAccountHandle); |
| 1504 | } |
| 1505 | } else { |
| 1506 | int defaultAccountHandlesDepth = parser.getDepth(); |
| 1507 | while (XmlUtils.nextElementWithin(parser, defaultAccountHandlesDepth)) { |
| 1508 | DefaultPhoneAccountHandle accountHandle |
| 1509 | = sDefaultPhoneAcountHandleXml |
| 1510 | .readFromXml(parser, s.versionNumber, context); |
| 1511 | if (accountHandle != null && s.accounts != null) { |
| 1512 | s.defaultOutgoingAccountHandles |
| 1513 | .put(accountHandle.userHandle, accountHandle); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1517 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 1518 | int accountsDepth = parser.getDepth(); |
| 1519 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1520 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 1521 | s.versionNumber, context); |
| 1522 | |
| 1523 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1524 | s.accounts.add(account); |
| 1525 | } |
| 1526 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1527 | } |
| 1528 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1529 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1530 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1531 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1532 | } |
| 1533 | }; |
| 1534 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1535 | @VisibleForTesting |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1536 | public static final XmlSerialization<DefaultPhoneAccountHandle> sDefaultPhoneAcountHandleXml = |
| 1537 | new XmlSerialization<DefaultPhoneAccountHandle>() { |
| 1538 | private static final String CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE |
| 1539 | = "default_outgoing_phone_account_handle"; |
| 1540 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1541 | private static final String GROUP_ID = "group_id"; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1542 | private static final String ACCOUNT_HANDLE = "account_handle"; |
| 1543 | |
| 1544 | @Override |
| 1545 | public void writeToXml(DefaultPhoneAccountHandle o, XmlSerializer serializer, |
| 1546 | Context context) throws IOException { |
| 1547 | if (o != null) { |
| 1548 | final UserManager userManager = UserManager.get(context); |
| 1549 | final long serialNumber = userManager.getSerialNumberForUser(o.userHandle); |
| 1550 | if (serialNumber != -1) { |
| 1551 | serializer.startTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE); |
| 1552 | writeLong(USER_SERIAL_NUMBER, serialNumber, serializer); |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1553 | writeNonNullString(GROUP_ID, o.groupId, serializer); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1554 | serializer.startTag(null, ACCOUNT_HANDLE); |
| 1555 | sPhoneAccountHandleXml.writeToXml(o.phoneAccountHandle, serializer, |
| 1556 | context); |
| 1557 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1558 | serializer.endTag(null, CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE); |
| 1559 | } |
| 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | @Override |
| 1564 | public DefaultPhoneAccountHandle readFromXml(XmlPullParser parser, int version, |
| 1565 | Context context) |
| 1566 | throws IOException, XmlPullParserException { |
| 1567 | if (parser.getName().equals(CLASS_DEFAULT_OUTGOING_PHONE_ACCOUNT_HANDLE)) { |
| 1568 | int outerDepth = parser.getDepth(); |
| 1569 | PhoneAccountHandle accountHandle = null; |
| 1570 | String userSerialNumberString = null; |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1571 | String groupId = ""; |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1572 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1573 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1574 | parser.nextTag(); |
| 1575 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1576 | context); |
| 1577 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1578 | parser.next(); |
| 1579 | userSerialNumberString = parser.getText(); |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1580 | } else if (parser.getName().equals(GROUP_ID)) { |
Hall Liu | ded880c | 2017-01-20 16:13:07 -0800 | [diff] [blame] | 1581 | if (parser.next() == XmlPullParser.TEXT) { |
| 1582 | groupId = parser.getText(); |
| 1583 | } |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1584 | } |
| 1585 | } |
| 1586 | UserHandle userHandle = null; |
| 1587 | if (userSerialNumberString != null) { |
| 1588 | try { |
| 1589 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1590 | userHandle = UserManager.get(context) |
| 1591 | .getUserForSerialNumber(serialNumber); |
| 1592 | } catch (NumberFormatException e) { |
| 1593 | Log.e(this, e, |
| 1594 | "Could not parse UserHandle " + userSerialNumberString); |
| 1595 | } |
| 1596 | } |
Brad Ebinger | 0c87b3c | 2016-06-10 17:23:02 -0700 | [diff] [blame] | 1597 | if (accountHandle != null && userHandle != null && groupId != null) { |
| 1598 | return new DefaultPhoneAccountHandle(userHandle, accountHandle, |
| 1599 | groupId); |
Tony Mak | 4a3e2fd | 2015-12-04 11:58:38 +0000 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | return null; |
| 1603 | } |
| 1604 | }; |
| 1605 | |
| 1606 | |
| 1607 | @VisibleForTesting |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1608 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1609 | new XmlSerialization<PhoneAccount>() { |
| 1610 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1611 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1612 | private static final String ADDRESS = "handle"; |
| 1613 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1614 | private static final String CAPABILITIES = "capabilities"; |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1615 | private static final String SUPPORTED_AUDIO_ROUTES = "supported_audio_routes"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1616 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1617 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1618 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1619 | private static final String ICON_TINT = "icon_tint"; |
| 1620 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1621 | private static final String LABEL = "label"; |
| 1622 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1623 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1624 | private static final String ICON = "icon"; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1625 | private static final String EXTRAS = "extras"; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1626 | private static final String ENABLED = "enabled"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1627 | |
| 1628 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1629 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1630 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1631 | if (o != null) { |
| 1632 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1633 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1634 | if (o.getAccountHandle() != null) { |
| 1635 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1636 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1637 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1638 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1639 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1640 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1641 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1642 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1643 | writeIconIfNonNull(ICON, o.getIcon(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1644 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1645 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1646 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1647 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1648 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1649 | writeBundle(EXTRAS, o.getExtras(), serializer); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1650 | writeTextIfNonNull(ENABLED, o.isEnabled() ? "true" : "false" , serializer); |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1651 | writeTextIfNonNull(SUPPORTED_AUDIO_ROUTES, Integer.toString( |
| 1652 | o.getSupportedAudioRoutes()), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1653 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1654 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1655 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1658 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1659 | throws IOException, XmlPullParserException { |
| 1660 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1661 | int outerDepth = parser.getDepth(); |
| 1662 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1663 | Uri address = null; |
| 1664 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1665 | int capabilities = 0; |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1666 | int supportedAudioRoutes = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1667 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1668 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1669 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1670 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1671 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1672 | String label = null; |
| 1673 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1674 | List<String> supportedUriSchemes = null; |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1675 | Icon icon = null; |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1676 | boolean enabled = false; |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1677 | Bundle extras = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1678 | |
| 1679 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1680 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1681 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1682 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1683 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1684 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1685 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1686 | address = Uri.parse(parser.getText()); |
| 1687 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1688 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1689 | String nextText = parser.getText(); |
| 1690 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1691 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1692 | parser.next(); |
| 1693 | capabilities = Integer.parseInt(parser.getText()); |
| 1694 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1695 | parser.next(); |
| 1696 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1697 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1698 | parser.next(); |
| 1699 | iconPackageName = parser.getText(); |
| 1700 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1701 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1702 | iconBitmap = readBitmap(parser); |
| 1703 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1704 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1705 | iconTint = Integer.parseInt(parser.getText()); |
| 1706 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1707 | parser.next(); |
| 1708 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1709 | } else if (parser.getName().equals(LABEL)) { |
| 1710 | parser.next(); |
| 1711 | label = parser.getText(); |
| 1712 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1713 | parser.next(); |
| 1714 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1715 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1716 | supportedUriSchemes = readStringList(parser); |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1717 | } else if (parser.getName().equals(ICON)) { |
| 1718 | parser.next(); |
| 1719 | icon = readIcon(parser); |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1720 | } else if (parser.getName().equals(ENABLED)) { |
| 1721 | parser.next(); |
| 1722 | enabled = "true".equalsIgnoreCase(parser.getText()); |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1723 | } else if (parser.getName().equals(EXTRAS)) { |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1724 | extras = readBundle(parser); |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1725 | } else if (parser.getName().equals(SUPPORTED_AUDIO_ROUTES)) { |
| 1726 | parser.next(); |
| 1727 | supportedAudioRoutes = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1728 | } |
| 1729 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1730 | |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1731 | ComponentName pstnComponentName = new ComponentName("com.android.phone", |
| 1732 | "com.android.services.telephony.TelephonyConnectionService"); |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1733 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1734 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1735 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1736 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1737 | if (version < 2) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1738 | supportedUriSchemes = new ArrayList<>(); |
| 1739 | |
| 1740 | // Handle the SIP connection service. |
| 1741 | // Check the system settings to see if it also should handle "tel" calls. |
| 1742 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1743 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1744 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1745 | if (useSipForPstn) { |
| 1746 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1747 | } |
| 1748 | } else { |
| 1749 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1750 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1751 | } |
| 1752 | } |
| 1753 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1754 | // Upgrade older phone accounts with explicit package name |
| 1755 | if (version < 5) { |
| 1756 | if (iconBitmap == null) { |
| 1757 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1758 | } |
| 1759 | } |
| 1760 | |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1761 | if (version < 6) { |
| 1762 | // Always enable all SIP accounts on upgrade to version 6 |
| 1763 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1764 | enabled = true; |
| 1765 | } |
| 1766 | } |
Santos Cordon | a9eebe4 | 2015-06-11 14:07:44 -0700 | [diff] [blame] | 1767 | if (version < 7) { |
| 1768 | // Always enabled all PSTN acocunts on upgrade to version 7 |
| 1769 | if (accountHandle.getComponentName().equals(pstnComponentName)) { |
| 1770 | enabled = true; |
| 1771 | } |
| 1772 | } |
Roshan Pius | 6f752c8 | 2015-08-10 10:42:06 -0700 | [diff] [blame] | 1773 | if (version < 8) { |
| 1774 | // Migrate the SIP account handle ids to use SIP username instead of SIP URI. |
| 1775 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1776 | Uri accountUri = Uri.parse(accountHandle.getId()); |
| 1777 | if (accountUri.getScheme() != null && |
| 1778 | accountUri.getScheme().equals(PhoneAccount.SCHEME_SIP)) { |
| 1779 | accountHandle = new PhoneAccountHandle(accountHandle.getComponentName(), |
| 1780 | accountUri.getSchemeSpecificPart(), |
| 1781 | accountHandle.getUserHandle()); |
| 1782 | } |
| 1783 | } |
| 1784 | } |
Santos Cordon | a82aed8 | 2015-05-26 10:43:56 -0700 | [diff] [blame] | 1785 | |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1786 | if (version < 9) { |
| 1787 | // Set supported audio routes to all by default |
| 1788 | supportedAudioRoutes = CallAudioState.ROUTE_ALL; |
| 1789 | } |
| 1790 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1791 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1792 | .setAddress(address) |
| 1793 | .setSubscriptionAddress(subscriptionAddress) |
| 1794 | .setCapabilities(capabilities) |
Christine Hallstrom | ffe558c | 2016-11-30 16:05:13 -0800 | [diff] [blame] | 1795 | .setSupportedAudioRoutes(supportedAudioRoutes) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1796 | .setShortDescription(shortDescription) |
| 1797 | .setSupportedUriSchemes(supportedUriSchemes) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1798 | .setHighlightColor(highlightColor) |
Tyler Gunn | 369c874 | 2015-10-07 15:39:28 -0700 | [diff] [blame] | 1799 | .setExtras(extras) |
Santos Cordon | ea5cb93 | 2015-05-07 16:28:38 -0700 | [diff] [blame] | 1800 | .setIsEnabled(enabled); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1801 | |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1802 | if (icon != null) { |
| 1803 | builder.setIcon(icon); |
| 1804 | } else if (iconBitmap != null) { |
| 1805 | builder.setIcon(Icon.createWithBitmap(iconBitmap)); |
| 1806 | } else if (!TextUtils.isEmpty(iconPackageName)) { |
| 1807 | builder.setIcon(Icon.createWithResource(iconPackageName, iconResId)); |
| 1808 | // TODO: Need to set tint. |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1809 | } |
| 1810 | |
Ihab Awad | 0a4b95f | 2015-05-18 10:15:38 -0700 | [diff] [blame] | 1811 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1812 | } |
| 1813 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1814 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1815 | |
| 1816 | /** |
Santos Cordon | 9c30c28 | 2015-05-13 16:28:27 -0700 | [diff] [blame] | 1817 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN |
| 1818 | * calls. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1819 | * |
| 1820 | * @param context The context. |
| 1821 | * @return {@code True} if SIP should be used for all calls. |
| 1822 | */ |
| 1823 | private boolean useSipForPstnCalls(Context context) { |
| 1824 | String option = Settings.System.getString(context.getContentResolver(), |
| 1825 | Settings.System.SIP_CALL_OPTIONS); |
| 1826 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1827 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1828 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1829 | }; |
| 1830 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1831 | @VisibleForTesting |
| 1832 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1833 | new XmlSerialization<PhoneAccountHandle>() { |
| 1834 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1835 | private static final String COMPONENT_NAME = "component_name"; |
| 1836 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1837 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1838 | |
| 1839 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1840 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1841 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1842 | if (o != null) { |
| 1843 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1844 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1845 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1846 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1847 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1848 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1849 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1850 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1851 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1852 | if (o.getUserHandle() != null && context != null) { |
| 1853 | UserManager userManager = UserManager.get(context); |
| 1854 | writeLong(USER_SERIAL_NUMBER, |
| 1855 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1856 | } |
| 1857 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1858 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1859 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1863 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1864 | throws IOException, XmlPullParserException { |
| 1865 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1866 | String componentNameString = null; |
| 1867 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1868 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1869 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1870 | |
| 1871 | UserManager userManager = UserManager.get(context); |
| 1872 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1873 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1874 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1875 | parser.next(); |
| 1876 | componentNameString = parser.getText(); |
| 1877 | } else if (parser.getName().equals(ID)) { |
| 1878 | parser.next(); |
| 1879 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1880 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1881 | parser.next(); |
| 1882 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1883 | } |
| 1884 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1885 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1886 | UserHandle userHandle = null; |
| 1887 | if (userSerialNumberString != null) { |
| 1888 | try { |
| 1889 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1890 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1891 | } catch (NumberFormatException e) { |
| 1892 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1893 | } |
| 1894 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1895 | return new PhoneAccountHandle( |
| 1896 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1897 | idString, |
| 1898 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1899 | } |
| 1900 | } |
| 1901 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1902 | } |
| 1903 | }; |
Srikanth Chintala | d5d2dd9 | 2016-06-23 16:02:33 +0530 | [diff] [blame^] | 1904 | |
| 1905 | private String nullToEmpty(String str) { |
| 1906 | return str == null ? "" : str; |
| 1907 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1908 | } |