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; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [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; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 30 | import android.os.Binder; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 31 | import android.os.Process; |
| 32 | import android.os.UserHandle; |
| 33 | import android.os.UserManager; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 34 | import android.provider.Settings; |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 35 | import android.telecom.ConnectionService; |
| 36 | import android.telecom.PhoneAccount; |
| 37 | import android.telecom.PhoneAccountHandle; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 38 | import android.telephony.PhoneNumberUtils; |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 39 | import android.telephony.SubscriptionManager; |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 40 | import android.telephony.TelephonyManager; |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 41 | import android.text.TextUtils; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 42 | import android.util.AtomicFile; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 43 | import android.util.Base64; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 44 | import android.util.Xml; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 45 | |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 46 | |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 47 | // TODO: Needed for move to system service: import com.android.internal.R; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 48 | import com.android.internal.annotations.VisibleForTesting; |
| 49 | import com.android.internal.util.FastXmlSerializer; |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 50 | import com.android.internal.util.IndentingPrintWriter; |
Sailesh Nepal | 0e1dc5a | 2014-07-30 11:08:54 -0700 | [diff] [blame] | 51 | import com.android.internal.util.XmlUtils; |
| 52 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 53 | import org.xmlpull.v1.XmlPullParser; |
| 54 | import org.xmlpull.v1.XmlPullParserException; |
| 55 | import org.xmlpull.v1.XmlSerializer; |
| 56 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 57 | import java.io.BufferedInputStream; |
| 58 | import java.io.BufferedOutputStream; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 59 | import java.io.ByteArrayOutputStream; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 60 | import java.io.File; |
| 61 | import java.io.FileNotFoundException; |
| 62 | import java.io.FileOutputStream; |
| 63 | import java.io.IOException; |
| 64 | import java.io.InputStream; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 65 | import java.lang.Integer; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 66 | import java.lang.SecurityException; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 67 | import java.lang.String; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 69 | import java.util.Collections; |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 70 | import java.util.Iterator; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 71 | import java.util.List; |
| 72 | import java.util.Objects; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 73 | import java.util.concurrent.CopyOnWriteArrayList; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 74 | |
| 75 | /** |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 76 | * Handles writing and reading PhoneAccountHandle registration entries. This is a simple verbatim |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 77 | * delegate for all the account handling methods on {@link android.telecom.TelecomManager} as implemented in |
| 78 | * {@link TelecomServiceImpl}, with the notable exception that {@link TelecomServiceImpl} is |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 79 | * responsible for security checking to make sure that the caller has proper authority over |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 80 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccountHandle}s. |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 81 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 82 | public final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 83 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 84 | public static final PhoneAccountHandle NO_ACCOUNT_SELECTED = |
| 85 | new PhoneAccountHandle(new ComponentName("null", "null"), "NO_ACCOUNT_SELECTED"); |
| 86 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 87 | public abstract static class Listener { |
| 88 | public void onAccountsChanged(PhoneAccountRegistrar registrar) {} |
| 89 | public void onDefaultOutgoingChanged(PhoneAccountRegistrar registrar) {} |
| 90 | public void onSimCallManagerChanged(PhoneAccountRegistrar registrar) {} |
| 91 | } |
| 92 | |
| 93 | private static final String FILE_NAME = "phone-account-registrar-state.xml"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 94 | @VisibleForTesting |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 95 | public static final int EXPECTED_STATE_VERSION = 5; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 96 | |
| 97 | /** Keep in sync with the same in SipSettings.java */ |
| 98 | private static final String SIP_SHARED_PREFERENCES = "SIP_PREFERENCES"; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 99 | |
| 100 | private final List<Listener> mListeners = new CopyOnWriteArrayList<>(); |
| 101 | private final AtomicFile mAtomicFile; |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 102 | private final Context mContext; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 103 | private final UserManager mUserManager; |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 104 | private final SubscriptionManager mSubscriptionManager; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 105 | private State mState; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 106 | private UserHandle mCurrentUserHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 107 | |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 108 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 109 | public PhoneAccountRegistrar(Context context) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 110 | this(context, FILE_NAME); |
| 111 | } |
| 112 | |
| 113 | @VisibleForTesting |
| 114 | public PhoneAccountRegistrar(Context context, String fileName) { |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 115 | // TODO: This file path is subject to change -- it is storing the phone account registry |
| 116 | // state file in the path /data/system/users/0/, which is likely not correct in a |
| 117 | // multi-user setting. |
| 118 | /** UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE |
| 119 | String filePath = Environment.getUserSystemDirectory(UserHandle.myUserId()). |
| 120 | getAbsolutePath(); |
| 121 | mAtomicFile = new AtomicFile(new File(filePath, fileName)); |
| 122 | UNCOMMENT_FOR_MOVE_TO_SYSTEM_SERVICE */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 123 | mAtomicFile = new AtomicFile(new File(context.getFilesDir(), fileName)); |
Tyler Gunn | 91d43cf | 2014-09-17 12:19:39 -0700 | [diff] [blame] | 124 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 125 | mState = new State(); |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 126 | mContext = context; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 127 | mUserManager = UserManager.get(context); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 128 | mSubscriptionManager = SubscriptionManager.from(mContext); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 129 | mCurrentUserHandle = Process.myUserHandle(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 130 | read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 133 | /** |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 134 | * Retrieves the subscription id for a given phone account if it exists. Subscription ids |
| 135 | * apply only to PSTN/SIM card phone accounts so all other accounts should not have a |
| 136 | * subscription id. |
| 137 | * @param accountHandle The handle for the phone account for which to retrieve the |
| 138 | * subscription id. |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 139 | * @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] | 140 | */ |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 141 | public int getSubscriptionIdForPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 142 | PhoneAccount account = getPhoneAccountInternal(accountHandle); |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 143 | |
| 144 | if (account != null |
| 145 | && account.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION) |
| 146 | && isVisibleForUser(accountHandle)) { |
| 147 | TelephonyManager tm = |
| 148 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 149 | return tm.getSubIdForPhoneAccount(account); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 150 | } |
Santos Cordon | 479b302 | 2015-02-06 04:27:15 -0800 | [diff] [blame] | 151 | return SubscriptionManager.INVALID_SUBSCRIPTION_ID; |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | /** |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 155 | * Retrieves the default outgoing phone account supporting the specified uriScheme. Note that if |
| 156 | * {@link #mCurrentUserHandle} does not have visibility into the current default, {@code null} |
| 157 | * will be returned. |
| 158 | * |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 159 | * @param uriScheme The URI scheme for the outgoing call. |
| 160 | * @return The {@link PhoneAccountHandle} to use. |
| 161 | */ |
| 162 | public PhoneAccountHandle getDefaultOutgoingPhoneAccount(String uriScheme) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 163 | final PhoneAccountHandle userSelected = getUserSelectedOutgoingPhoneAccount(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 164 | |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 165 | if (userSelected != null) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 166 | // If there is a default PhoneAccount, ensure it supports calls to handles with the |
| 167 | // specified uriScheme. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 168 | final PhoneAccount userSelectedAccount = getPhoneAccountInternal(userSelected); |
| 169 | if (userSelectedAccount.supportsUriScheme(uriScheme) |
| 170 | && isVisibleForUser(userSelected)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 171 | return userSelected; |
| 172 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 175 | List<PhoneAccountHandle> outgoing = getCallCapablePhoneAccounts(uriScheme); |
Ihab Awad | 6fb37c8 | 2014-08-07 19:48:57 -0700 | [diff] [blame] | 176 | switch (outgoing.size()) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 177 | case 0: |
| 178 | // There are no accounts, so there can be no default |
| 179 | return null; |
| 180 | case 1: |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 181 | // There is only one account, which is by definition the default. |
| 182 | PhoneAccountHandle onlyHandle = outgoing.get(0); |
| 183 | if (isVisibleForUser(onlyHandle)) { |
| 184 | return outgoing.get(0); |
| 185 | } |
| 186 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 187 | default: |
| 188 | // There are multiple accounts with no selected default |
| 189 | return null; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 190 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 191 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 192 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 193 | /** |
| 194 | * @return The user-selected outgoing {@link PhoneAccount}, or null if it hasn't been set (or |
| 195 | * if it was set by another user). |
| 196 | */ |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 197 | PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() { |
| 198 | if (mState.defaultOutgoing != null) { |
| 199 | // Return the registered outgoing default iff it still exists (we keep a sticky |
| 200 | // default to survive account deletion and re-addition) |
| 201 | for (int i = 0; i < mState.accounts.size(); i++) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 202 | if (mState.accounts.get(i).getAccountHandle().equals(mState.defaultOutgoing) |
| 203 | && isVisibleForUser(mState.defaultOutgoing)) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 204 | return mState.defaultOutgoing; |
| 205 | } |
| 206 | } |
| 207 | // At this point, there was a registered default but it has been deleted; proceed |
| 208 | // as though there were no default |
| 209 | } |
| 210 | return null; |
| 211 | } |
| 212 | |
Andrew Lee | a51a386 | 2014-09-03 14:58:45 -0700 | [diff] [blame] | 213 | public void setUserSelectedOutgoingPhoneAccount(PhoneAccountHandle accountHandle) { |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 214 | if (accountHandle == null) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 215 | // Asking to clear the default outgoing is a valid request |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 216 | mState.defaultOutgoing = null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 217 | } else { |
| 218 | boolean found = false; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 219 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 220 | if (Objects.equals(accountHandle, m.getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 221 | found = true; |
| 222 | break; |
| 223 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 224 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 225 | |
| 226 | if (!found) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 227 | Log.w(this, "Trying to set nonexistent default outgoing %s", |
| 228 | accountHandle); |
| 229 | return; |
| 230 | } |
| 231 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 232 | if (!getPhoneAccountInternal(accountHandle).hasCapabilities( |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 233 | PhoneAccount.CAPABILITY_CALL_PROVIDER)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 234 | Log.w(this, "Trying to set non-call-provider default outgoing %s", |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 235 | accountHandle); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 236 | return; |
| 237 | } |
| 238 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 239 | if (getPhoneAccountInternal(accountHandle).hasCapabilities( |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 240 | PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION)) { |
| 241 | // If the account selected is a SIM account, propagate down to the subscription |
| 242 | // record. |
Wink Saville | 7ce6e78 | 2014-10-27 10:56:46 -0700 | [diff] [blame] | 243 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Wink Saville | 33c05d3 | 2014-11-20 13:04:17 -0800 | [diff] [blame] | 244 | mSubscriptionManager.setDefaultVoiceSubId(subId); |
Nancy Chen | 5a36b6e | 2014-10-23 17:42:42 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 247 | mState.defaultOutgoing = accountHandle; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 250 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 251 | fireDefaultOutgoingChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Nancy Chen | 668dee0 | 2014-11-19 15:31:31 -0800 | [diff] [blame] | 254 | boolean isUserSelectedSmsPhoneAccount(PhoneAccountHandle accountHandle) { |
| 255 | return getSubscriptionIdForPhoneAccount(accountHandle) == |
| 256 | SubscriptionManager.getDefaultSmsSubId(); |
| 257 | } |
| 258 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 259 | public void setSimCallManager(PhoneAccountHandle callManager) { |
| 260 | if (callManager != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 261 | PhoneAccount callManagerAccount = getPhoneAccountInternal(callManager); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 262 | if (callManagerAccount == null) { |
| 263 | Log.d(this, "setSimCallManager: Nonexistent call manager: %s", callManager); |
| 264 | return; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 265 | } else if (!callManagerAccount.hasCapabilities( |
| 266 | PhoneAccount.CAPABILITY_CONNECTION_MANAGER)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 267 | Log.d(this, "setSimCallManager: Not a call manager: %s", callManagerAccount); |
| 268 | return; |
| 269 | } |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 270 | } else { |
| 271 | callManager = NO_ACCOUNT_SELECTED; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 272 | } |
| 273 | mState.simCallManager = callManager; |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 274 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 275 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 276 | fireSimCallManagerChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 279 | /** |
| 280 | * @return The {@link PhoneAccount}s which are visible to {@link #mCurrentUserHandle}. |
| 281 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 282 | public PhoneAccountHandle getSimCallManager() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 283 | if (mState.simCallManager != null) { |
Yorke Lee | 5e8836a | 2014-08-22 15:25:18 -0700 | [diff] [blame] | 284 | if (NO_ACCOUNT_SELECTED.equals(mState.simCallManager)) { |
| 285 | return null; |
| 286 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 287 | // Return the registered sim call manager iff it still exists (we keep a sticky |
| 288 | // setting to survive account deletion and re-addition) |
| 289 | for (int i = 0; i < mState.accounts.size(); i++) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 290 | if (mState.accounts.get(i).getAccountHandle().equals(mState.simCallManager) |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 291 | && !resolveComponent(mState.simCallManager).isEmpty() |
| 292 | && isVisibleForUser(mState.simCallManager)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 293 | return mState.simCallManager; |
| 294 | } |
| 295 | } |
| 296 | } |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 297 | |
| 298 | // See if the OEM has specified a default one. |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 299 | String defaultConnectionMgr = |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 300 | mContext.getResources().getString(R.string.default_connection_manager_component); |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 301 | if (!TextUtils.isEmpty(defaultConnectionMgr)) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 302 | ComponentName componentName = ComponentName.unflattenFromString(defaultConnectionMgr); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 303 | if (componentName == null) { |
| 304 | return null; |
| 305 | } |
| 306 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 307 | // Make sure that the component can be resolved. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 308 | List<ResolveInfo> resolveInfos = resolveComponent(componentName, null); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 309 | if (resolveInfos.isEmpty()) { |
| 310 | resolveInfos = resolveComponent(componentName, Binder.getCallingUserHandle()); |
| 311 | } |
| 312 | |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 313 | if (!resolveInfos.isEmpty()) { |
| 314 | // See if there is registered PhoneAccount by this component. |
| 315 | List<PhoneAccountHandle> handles = getAllPhoneAccountHandles(); |
| 316 | for (PhoneAccountHandle handle : handles) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 317 | if (componentName.equals(handle.getComponentName()) |
| 318 | && isVisibleForUser(handle)) { |
Evan Charlton | af51ceb | 2014-07-30 11:56:36 -0700 | [diff] [blame] | 319 | return handle; |
| 320 | } |
| 321 | } |
| 322 | Log.d(this, "%s does not have a PhoneAccount; not using as default", componentName); |
| 323 | } else { |
| 324 | Log.d(this, "%s could not be resolved; not using as default", componentName); |
| 325 | } |
| 326 | } else { |
| 327 | Log.v(this, "No default connection manager specified"); |
| 328 | } |
| 329 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 330 | return null; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 333 | /** |
| 334 | * A version of {@link #getPhoneAccount} which does not guard for the current user. |
| 335 | * |
| 336 | * @param handle |
| 337 | * @return |
| 338 | */ |
| 339 | PhoneAccount getPhoneAccountInternal(PhoneAccountHandle handle) { |
| 340 | for (PhoneAccount m : mState.accounts) { |
| 341 | if (Objects.equals(handle, m.getAccountHandle())) { |
| 342 | return m; |
| 343 | } |
| 344 | } |
| 345 | return null; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Update the current UserHandle to track when users are switched. This will allow the |
| 350 | * PhoneAccountRegistar to self-filter the PhoneAccounts to make sure we don't leak anything |
| 351 | * across users. |
| 352 | * |
| 353 | * @param userHandle The {@link UserHandle}, as delivered by |
| 354 | * {@link Intent#ACTION_USER_SWITCHED}. |
| 355 | */ |
| 356 | public void setCurrentUserHandle(UserHandle userHandle) { |
| 357 | if (userHandle == null) { |
| 358 | Log.d(this, "setCurrentUserHandle, userHandle = null"); |
| 359 | userHandle = Process.myUserHandle(); |
| 360 | } |
| 361 | Log.d(this, "setCurrentUserHandle, %s", userHandle); |
| 362 | mCurrentUserHandle = userHandle; |
| 363 | } |
| 364 | |
| 365 | private boolean isVisibleForUser(PhoneAccountHandle accountHandle) { |
| 366 | if (accountHandle == null) { |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | return isVisibleForUser(getPhoneAccountInternal(accountHandle)); |
| 371 | } |
| 372 | |
| 373 | private boolean isVisibleForUser(PhoneAccount account) { |
| 374 | if (account == null) { |
| 375 | return false; |
| 376 | } |
| 377 | |
| 378 | // If this PhoneAccount has CAPABILITY_MULTI_USER, it should be visible to all users and |
| 379 | // all profiles. Only Telephony and SIP accounts should have this capability. |
| 380 | if (account.hasCapabilities(PhoneAccount.CAPABILITY_MULTI_USER)) { |
| 381 | return true; |
| 382 | } |
| 383 | |
| 384 | UserHandle phoneAccountUserHandle = account.getAccountHandle().getUserHandle(); |
| 385 | if (phoneAccountUserHandle == null) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | if (mCurrentUserHandle == null) { |
| 390 | Log.d(this, "Current user is null; assuming true"); |
| 391 | return true; |
| 392 | } |
| 393 | |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 394 | if (phoneAccountUserHandle.equals(Binder.getCallingUserHandle())) { |
| 395 | return true; |
| 396 | } |
| 397 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 398 | // Unlike in TelecomServiceImpl, we only care about *profiles* here. We want to make sure |
| 399 | // that we don't resolve PhoneAccount across *users*, but resolving across *profiles* is |
| 400 | // fine. |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 401 | if (UserHandle.getCallingUserId() == UserHandle.USER_OWNER) { |
| 402 | List<UserInfo> profileUsers = |
| 403 | mUserManager.getProfiles(mCurrentUserHandle.getIdentifier()); |
| 404 | for (UserInfo profileInfo : profileUsers) { |
| 405 | if (profileInfo.getUserHandle().equals(phoneAccountUserHandle)) { |
| 406 | return true; |
| 407 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 408 | } |
| 409 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 410 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 411 | return false; |
| 412 | } |
| 413 | |
| 414 | private List<ResolveInfo> resolveComponent(PhoneAccountHandle phoneAccountHandle) { |
| 415 | return resolveComponent(phoneAccountHandle.getComponentName(), |
| 416 | phoneAccountHandle.getUserHandle()); |
| 417 | } |
| 418 | |
| 419 | private List<ResolveInfo> resolveComponent(ComponentName componentName, |
| 420 | UserHandle userHandle) { |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 421 | PackageManager pm = mContext.getPackageManager(); |
| 422 | Intent intent = new Intent(ConnectionService.SERVICE_INTERFACE); |
| 423 | intent.setComponent(componentName); |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 424 | try { |
| 425 | if (userHandle != null) { |
| 426 | return pm.queryIntentServicesAsUser(intent, 0, userHandle.getIdentifier()); |
| 427 | } else { |
| 428 | return pm.queryIntentServices(intent, 0); |
| 429 | } |
| 430 | } catch (SecurityException e) { |
| 431 | Log.v(this, "%s is not visible for the calling user", componentName); |
| 432 | return Collections.EMPTY_LIST; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 433 | } |
mike dooley | 10a5831 | 2014-11-06 13:46:19 -0800 | [diff] [blame] | 434 | } |
| 435 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 436 | /** |
| 437 | * Retrieves a list of all {@link PhoneAccountHandle}s registered. |
| 438 | * |
| 439 | * @return The list of {@link PhoneAccountHandle}s. |
| 440 | */ |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 441 | public List<PhoneAccountHandle> getAllPhoneAccountHandles() { |
| 442 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 443 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 444 | if (isVisibleForUser(m)) { |
| 445 | accountHandles.add(m.getAccountHandle()); |
| 446 | } |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 447 | } |
| 448 | return accountHandles; |
| 449 | } |
| 450 | |
| 451 | public List<PhoneAccount> getAllPhoneAccounts() { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 452 | List<PhoneAccount> accounts = new ArrayList<>(mState.accounts.size()); |
| 453 | for (PhoneAccount account : mState.accounts) { |
| 454 | if (isVisibleForUser(account)) { |
| 455 | accounts.add(account); |
| 456 | } |
| 457 | } |
| 458 | return accounts; |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 462 | * Retrieves a list of all call provider phone accounts. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 463 | * |
| 464 | * @return The phone account handles. |
| 465 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 466 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts() { |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 467 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER); |
| 468 | } |
| 469 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 470 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 471 | * 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] | 472 | * specified URI scheme. |
| 473 | * |
| 474 | * @param uriScheme The URI scheme. |
| 475 | * @return The phone account handles. |
| 476 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 477 | public List<PhoneAccountHandle> getCallCapablePhoneAccounts(String uriScheme) { |
| 478 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CALL_PROVIDER, uriScheme); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 481 | /** |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 482 | * Retrieves a list of all phone accounts registered by a specified package. |
| 483 | * |
| 484 | * @param packageName The name of the package that registered the phone accounts. |
| 485 | * @return The phone account handles. |
| 486 | */ |
| 487 | public List<PhoneAccountHandle> getPhoneAccountsForPackage(String packageName) { |
| 488 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
| 489 | for (PhoneAccount m : mState.accounts) { |
| 490 | if (Objects.equals( |
| 491 | packageName, |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 492 | m.getAccountHandle().getComponentName().getPackageName()) |
| 493 | && isVisibleForUser(m)) { |
Nancy Chen | 1c5926f | 2014-09-17 14:44:14 -0700 | [diff] [blame] | 494 | accountHandles.add(m.getAccountHandle()); |
| 495 | } |
| 496 | } |
| 497 | return accountHandles; |
| 498 | } |
| 499 | |
| 500 | /** |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 501 | * Retrieves a list of all phone account handles with the connection manager capability. |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 502 | * |
| 503 | * @return The phone account handles. |
| 504 | */ |
| 505 | public List<PhoneAccountHandle> getConnectionManagerPhoneAccounts() { |
Santos Cordon | e3d8245 | 2014-09-15 13:44:29 -0700 | [diff] [blame] | 506 | return getPhoneAccountHandles(PhoneAccount.CAPABILITY_CONNECTION_MANAGER, |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 507 | null /* supportedUriScheme */); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 510 | public PhoneAccount getPhoneAccount(PhoneAccountHandle handle) { |
| 511 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 512 | if (Objects.equals(handle, m.getAccountHandle()) |
| 513 | && isVisibleForUser(m)) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 514 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | return null; |
| 518 | } |
| 519 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 520 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 521 | // ComponentName? |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 522 | public void registerPhoneAccount(PhoneAccount account) { |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 523 | // Enforce the requirement that a connection service for a phone account has the correct |
| 524 | // permission. |
| 525 | if (!phoneAccountHasPermission(account.getAccountHandle())) { |
| 526 | Log.w(this, "Phone account %s does not have BIND_CONNECTION_SERVICE permission.", |
| 527 | account.getAccountHandle()); |
| 528 | throw new SecurityException( |
| 529 | "PhoneAccount connection service requires BIND_CONNECTION_SERVICE permission."); |
| 530 | } |
| 531 | |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 532 | addOrReplacePhoneAccount(account); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * Adds a {@code PhoneAccount}, replacing an existing one if found. |
| 537 | * |
| 538 | * @param account The {@code PhoneAccount} to add or replace. |
| 539 | */ |
| 540 | private void addOrReplacePhoneAccount(PhoneAccount account) { |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 541 | Log.d(this, "addOrReplacePhoneAccount(%s -> %s)", |
| 542 | account.getAccountHandle(), account); |
| 543 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 544 | mState.accounts.add(account); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 545 | // Search for duplicates and remove any that are found. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 546 | for (int i = 0; i < mState.accounts.size() - 1; i++) { |
| 547 | if (Objects.equals( |
| 548 | account.getAccountHandle(), mState.accounts.get(i).getAccountHandle())) { |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 549 | // replace existing entry. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 550 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 551 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 555 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 556 | fireAccountsChanged(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Evan Charlton | 8917637 | 2014-07-19 18:23:09 -0700 | [diff] [blame] | 559 | public void unregisterPhoneAccount(PhoneAccountHandle accountHandle) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 560 | for (int i = 0; i < mState.accounts.size(); i++) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 561 | PhoneAccountHandle handle = mState.accounts.get(i).getAccountHandle(); |
| 562 | if (Objects.equals(accountHandle, handle)) { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 563 | mState.accounts.remove(i); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 564 | break; |
| 565 | } |
| 566 | } |
| 567 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 568 | write(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 569 | fireAccountsChanged(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 572 | /** |
| 573 | * Un-registers all phone accounts associated with a specified package. |
| 574 | * |
| 575 | * @param packageName The package for which phone accounts will be removed. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 576 | * @param userHandle The {@link UserHandle} the package is running under. |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 577 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 578 | public void clearAccounts(String packageName, UserHandle userHandle) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 579 | boolean accountsRemoved = false; |
| 580 | Iterator<PhoneAccount> it = mState.accounts.iterator(); |
| 581 | while (it.hasNext()) { |
| 582 | PhoneAccount phoneAccount = it.next(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 583 | PhoneAccountHandle handle = phoneAccount.getAccountHandle(); |
| 584 | if (Objects.equals(packageName, handle.getComponentName().getPackageName()) |
| 585 | && Objects.equals(userHandle, handle.getUserHandle())) { |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 586 | Log.i(this, "Removing phone account " + phoneAccount.getLabel()); |
| 587 | it.remove(); |
| 588 | accountsRemoved = true; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Tyler Gunn | d900ce6 | 2014-08-13 11:40:59 -0700 | [diff] [blame] | 592 | if (accountsRemoved) { |
| 593 | write(); |
| 594 | fireAccountsChanged(); |
| 595 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 598 | public boolean isVoiceMailNumber(PhoneAccountHandle accountHandle, String number) { |
Wink Saville | 3585060 | 2014-10-23 15:57:21 -0700 | [diff] [blame] | 599 | int subId = getSubscriptionIdForPhoneAccount(accountHandle); |
Nancy Chen | 140004a | 2014-10-15 15:48:38 -0700 | [diff] [blame] | 600 | return PhoneNumberUtils.isVoiceMailNumber(subId, number); |
| 601 | } |
| 602 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 603 | public void addListener(Listener l) { |
| 604 | mListeners.add(l); |
| 605 | } |
| 606 | |
| 607 | public void removeListener(Listener l) { |
Jay Shrauner | a82c8f7 | 2014-08-14 15:49:16 -0700 | [diff] [blame] | 608 | if (l != null) { |
| 609 | mListeners.remove(l); |
| 610 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | private void fireAccountsChanged() { |
| 614 | for (Listener l : mListeners) { |
| 615 | l.onAccountsChanged(this); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | private void fireDefaultOutgoingChanged() { |
| 620 | for (Listener l : mListeners) { |
| 621 | l.onDefaultOutgoingChanged(this); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | private void fireSimCallManagerChanged() { |
| 626 | for (Listener l : mListeners) { |
| 627 | l.onSimCallManagerChanged(this); |
| 628 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 631 | /** |
| 632 | * Determines if the connection service specified by a {@link PhoneAccountHandle} has the |
| 633 | * {@link Manifest.permission#BIND_CONNECTION_SERVICE} permission. |
| 634 | * |
| 635 | * @param phoneAccountHandle The phone account to check. |
| 636 | * @return {@code True} if the phone account has permission. |
| 637 | */ |
| 638 | public boolean phoneAccountHasPermission(PhoneAccountHandle phoneAccountHandle) { |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 639 | List<ResolveInfo> resolveInfos = resolveComponent(phoneAccountHandle); |
| 640 | if (resolveInfos.isEmpty()) { |
| 641 | Log.w(this, "phoneAccount %s not found", phoneAccountHandle.getComponentName()); |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 642 | return false; |
| 643 | } |
Sailesh Nepal | 91fc809 | 2015-02-14 15:44:55 -0800 | [diff] [blame^] | 644 | for (ResolveInfo resolveInfo : resolveInfos) { |
| 645 | ServiceInfo serviceInfo = resolveInfo.serviceInfo; |
| 646 | if (serviceInfo == null || !Objects.equals(serviceInfo.permission, |
| 647 | Manifest.permission.BIND_CONNECTION_SERVICE)) { |
| 648 | return false; |
| 649 | } |
| 650 | } |
| 651 | return true; |
Tyler Gunn | cb59b67 | 2014-08-20 09:02:11 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 654 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 655 | |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 656 | /** |
| 657 | * Returns a list of phone account handles with the specified flag. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 658 | * |
| 659 | * @param flags Flags which the {@code PhoneAccount} must have. |
Santos Cordon | afe59e5 | 2014-08-22 16:48:43 -0700 | [diff] [blame] | 660 | */ |
| 661 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags) { |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 662 | return getPhoneAccountHandles(flags, null); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 663 | } |
| 664 | |
| 665 | /** |
| 666 | * Returns a list of phone account handles with the specified flag, supporting the specified |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 667 | * URI scheme. |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 668 | * |
| 669 | * @param flags Flags which the {@code PhoneAccount} must have. |
| 670 | * @param uriScheme URI schemes the PhoneAccount must handle. {@code Null} bypasses the |
| 671 | * URI scheme check. |
| 672 | */ |
Nancy Chen | 309198e | 2014-09-15 18:02:49 -0700 | [diff] [blame] | 673 | private List<PhoneAccountHandle> getPhoneAccountHandles(int flags, String uriScheme) { |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 674 | List<PhoneAccountHandle> accountHandles = new ArrayList<>(); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 675 | for (PhoneAccount m : mState.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 676 | if (!m.hasCapabilities(flags)) { |
| 677 | // Account doesn't have the right capabilities; skip this one. |
| 678 | continue; |
Ihab Awad | f2a8491 | 2014-07-22 21:09:25 -0700 | [diff] [blame] | 679 | } |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 680 | if (uriScheme != null && !m.supportsUriScheme(uriScheme)) { |
| 681 | // Account doesn't support this URI scheme; skip this one. |
| 682 | continue; |
| 683 | } |
| 684 | if (resolveComponent(m.getAccountHandle()).isEmpty()) { |
| 685 | // This component cannot be resolved anymore; skip this one. |
| 686 | continue; |
| 687 | } |
| 688 | if (!isVisibleForUser(m)) { |
| 689 | // Account is not visible for the current user; skip this one. |
| 690 | continue; |
| 691 | } |
| 692 | accountHandles.add(m.getAccountHandle()); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 693 | } |
Evan Charlton | 94d0162 | 2014-07-20 12:32:05 -0700 | [diff] [blame] | 694 | return accountHandles; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 695 | } |
| 696 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 697 | /** |
| 698 | * The state of this {@code PhoneAccountRegistrar}. |
| 699 | */ |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 700 | @VisibleForTesting |
| 701 | public static class State { |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 702 | /** |
| 703 | * The account selected by the user to be employed by default for making outgoing calls. |
| 704 | * If the user has not made such a selection, then this is null. |
| 705 | */ |
| 706 | public PhoneAccountHandle defaultOutgoing = null; |
| 707 | |
| 708 | /** |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 709 | * A {@code PhoneAccount} having {@link PhoneAccount#CAPABILITY_CONNECTION_MANAGER} which |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 710 | * manages and optimizes a user's PSTN SIM connections. |
| 711 | */ |
| 712 | public PhoneAccountHandle simCallManager; |
| 713 | |
| 714 | /** |
Tyler Gunn | 7cc70b4 | 2014-09-12 22:17:27 -0700 | [diff] [blame] | 715 | * The complete list of {@code PhoneAccount}s known to the Telecom subsystem. |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 716 | */ |
| 717 | public final List<PhoneAccount> accounts = new ArrayList<>(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 718 | |
| 719 | /** |
| 720 | * The version number of the State data. |
| 721 | */ |
| 722 | public int versionNumber; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Tyler Gunn | 9787e0e | 2014-10-14 14:36:12 -0700 | [diff] [blame] | 725 | /** |
| 726 | * Dumps the state of the {@link CallsManager}. |
| 727 | * |
| 728 | * @param pw The {@code IndentingPrintWriter} to write the state to. |
| 729 | */ |
| 730 | public void dump(IndentingPrintWriter pw) { |
| 731 | if (mState != null) { |
| 732 | pw.println("xmlVersion: " + mState.versionNumber); |
| 733 | pw.println("defaultOutgoing: " + (mState.defaultOutgoing == null ? "none" : |
| 734 | mState.defaultOutgoing)); |
| 735 | pw.println("simCallManager: " + (mState.simCallManager == null ? "none" : |
| 736 | mState.simCallManager)); |
| 737 | pw.println("phoneAccounts:"); |
| 738 | pw.increaseIndent(); |
| 739 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 740 | pw.println(phoneAccount); |
| 741 | } |
| 742 | pw.decreaseIndent(); |
| 743 | } |
| 744 | } |
| 745 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 746 | //////////////////////////////////////////////////////////////////////////////////////////////// |
| 747 | // |
| 748 | // State management |
| 749 | // |
| 750 | |
| 751 | private void write() { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 752 | final FileOutputStream os; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 753 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 754 | os = mAtomicFile.startWrite(); |
| 755 | boolean success = false; |
| 756 | try { |
| 757 | XmlSerializer serializer = new FastXmlSerializer(); |
| 758 | serializer.setOutput(new BufferedOutputStream(os), "utf-8"); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 759 | writeToXml(mState, serializer, mContext); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 760 | serializer.flush(); |
| 761 | success = true; |
| 762 | } finally { |
| 763 | if (success) { |
| 764 | mAtomicFile.finishWrite(os); |
| 765 | } else { |
| 766 | mAtomicFile.failWrite(os); |
| 767 | } |
| 768 | } |
| 769 | } catch (IOException e) { |
| 770 | Log.e(this, e, "Writing state to XML file"); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 771 | } |
| 772 | } |
| 773 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 774 | private void read() { |
| 775 | final InputStream is; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 776 | try { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 777 | is = mAtomicFile.openRead(); |
| 778 | } catch (FileNotFoundException ex) { |
| 779 | return; |
| 780 | } |
| 781 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 782 | boolean versionChanged = false; |
| 783 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 784 | XmlPullParser parser; |
| 785 | try { |
| 786 | parser = Xml.newPullParser(); |
| 787 | parser.setInput(new BufferedInputStream(is), null); |
| 788 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 789 | mState = readFromXml(parser, mContext); |
| 790 | versionChanged = mState.versionNumber < EXPECTED_STATE_VERSION; |
| 791 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 792 | } catch (IOException | XmlPullParserException e) { |
| 793 | Log.e(this, e, "Reading state from XML file"); |
| 794 | mState = new State(); |
| 795 | } finally { |
| 796 | try { |
| 797 | is.close(); |
| 798 | } catch (IOException e) { |
| 799 | Log.e(this, e, "Closing InputStream"); |
| 800 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 801 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 802 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 803 | // Verify all of the UserHandles. |
| 804 | List<PhoneAccount> badAccounts = new ArrayList<>(); |
| 805 | for (PhoneAccount phoneAccount : mState.accounts) { |
| 806 | UserHandle userHandle = phoneAccount.getAccountHandle().getUserHandle(); |
| 807 | if (userHandle == null) { |
| 808 | Log.w(this, "Missing UserHandle for %s", phoneAccount); |
| 809 | badAccounts.add(phoneAccount); |
| 810 | } else if (mUserManager.getSerialNumberForUser(userHandle) == -1) { |
| 811 | Log.w(this, "User does not exist for %s", phoneAccount); |
| 812 | badAccounts.add(phoneAccount); |
| 813 | } |
| 814 | } |
| 815 | mState.accounts.removeAll(badAccounts); |
| 816 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 817 | // If an upgrade occurred, write out the changed data. |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 818 | if (versionChanged || !badAccounts.isEmpty()) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 819 | write(); |
| 820 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 823 | private static void writeToXml(State state, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 824 | throws IOException { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 825 | sStateXml.writeToXml(state, serializer, context); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 826 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 827 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 828 | private static State readFromXml(XmlPullParser parser, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 829 | throws IOException, XmlPullParserException { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 830 | State s = sStateXml.readFromXml(parser, 0, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 831 | return s != null ? s : new State(); |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 832 | } |
| 833 | |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 834 | //////////////////////////////////////////////////////////////////////////////////////////////// |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 835 | // |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 836 | // XML serialization |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 837 | // |
| 838 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 839 | @VisibleForTesting |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 840 | public abstract static class XmlSerialization<T> { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 841 | private static final String LENGTH_ATTRIBUTE = "length"; |
| 842 | private static final String VALUE_TAG = "value"; |
| 843 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 844 | /** |
| 845 | * Write the supplied object to XML |
| 846 | */ |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 847 | public abstract void writeToXml(T o, XmlSerializer serializer, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 848 | throws IOException; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 849 | |
| 850 | /** |
| 851 | * Read from the supplied XML into a new object, returning null in case of an |
| 852 | * unrecoverable schema mismatch or other data error. 'parser' must be already |
| 853 | * positioned at the first tag that is expected to have been emitted by this |
| 854 | * object's writeToXml(). This object tries to fail early without modifying |
| 855 | * 'parser' if it does not recognize the data it sees. |
| 856 | */ |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 857 | public abstract T readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 858 | throws IOException, XmlPullParserException; |
| 859 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 860 | protected void writeTextIfNonNull(String tagName, Object value, XmlSerializer serializer) |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 861 | throws IOException { |
| 862 | if (value != null) { |
| 863 | serializer.startTag(null, tagName); |
| 864 | serializer.text(Objects.toString(value)); |
| 865 | serializer.endTag(null, tagName); |
| 866 | } |
| 867 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 868 | |
| 869 | /** |
| 870 | * Serializes a string array. |
| 871 | * |
| 872 | * @param tagName The tag name for the string array. |
| 873 | * @param values The string values to serialize. |
| 874 | * @param serializer The serializer. |
| 875 | * @throws IOException |
| 876 | */ |
| 877 | protected void writeStringList(String tagName, List<String> values, |
| 878 | XmlSerializer serializer) |
| 879 | throws IOException { |
| 880 | |
| 881 | serializer.startTag(null, tagName); |
| 882 | if (values != null) { |
| 883 | serializer.attribute(null, LENGTH_ATTRIBUTE, Objects.toString(values.size())); |
| 884 | for (String toSerialize : values) { |
| 885 | serializer.startTag(null, VALUE_TAG); |
| 886 | if (toSerialize != null ){ |
| 887 | serializer.text(toSerialize); |
| 888 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 889 | serializer.endTag(null, VALUE_TAG); |
| 890 | } |
| 891 | } else { |
| 892 | serializer.attribute(null, LENGTH_ATTRIBUTE, "0"); |
| 893 | } |
| 894 | serializer.endTag(null, tagName); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 895 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 896 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 897 | protected void writeBitmapIfNonNull(String tagName, Bitmap value, XmlSerializer serializer) |
| 898 | throws IOException { |
| 899 | if (value != null && value.getByteCount() > 0) { |
| 900 | ByteArrayOutputStream stream = new ByteArrayOutputStream(); |
| 901 | value.compress(Bitmap.CompressFormat.PNG, 100, stream); |
| 902 | byte[] imageByteArray = stream.toByteArray(); |
| 903 | String text = Base64.encodeToString(imageByteArray, 0, imageByteArray.length, 0); |
| 904 | |
| 905 | serializer.startTag(null, tagName); |
| 906 | serializer.text(text); |
| 907 | serializer.endTag(null, tagName); |
| 908 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 911 | protected void writeLong(String tagName, long value, XmlSerializer serializer) |
| 912 | throws IOException { |
| 913 | serializer.startTag(null, tagName); |
| 914 | serializer.text(Long.valueOf(value).toString()); |
| 915 | serializer.endTag(null, tagName); |
| 916 | } |
| 917 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 918 | /** |
| 919 | * Reads a string array from the XML parser. |
| 920 | * |
| 921 | * @param parser The XML parser. |
| 922 | * @return String array containing the parsed values. |
| 923 | * @throws IOException Exception related to IO. |
| 924 | * @throws XmlPullParserException Exception related to parsing. |
| 925 | */ |
| 926 | protected List<String> readStringList(XmlPullParser parser) |
| 927 | throws IOException, XmlPullParserException { |
| 928 | |
| 929 | int length = Integer.parseInt(parser.getAttributeValue(null, LENGTH_ATTRIBUTE)); |
| 930 | List<String> arrayEntries = new ArrayList<String>(length); |
| 931 | String value = null; |
| 932 | |
| 933 | if (length == 0) { |
| 934 | return arrayEntries; |
| 935 | } |
| 936 | |
| 937 | int outerDepth = parser.getDepth(); |
| 938 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 939 | if (parser.getName().equals(VALUE_TAG)) { |
Tyler Gunn | 8e0fef4 | 2014-09-08 18:34:44 -0700 | [diff] [blame] | 940 | parser.next(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 941 | value = parser.getText(); |
| 942 | arrayEntries.add(value); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | return arrayEntries; |
| 947 | } |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 948 | |
| 949 | protected Bitmap readBitmap(XmlPullParser parser) |
| 950 | throws IOException, XmlPullParserException { |
| 951 | byte[] imageByteArray = Base64.decode(parser.getText(), 0); |
| 952 | return BitmapFactory.decodeByteArray(imageByteArray, 0, imageByteArray.length); |
| 953 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 954 | } |
| 955 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 956 | @VisibleForTesting |
| 957 | public static final XmlSerialization<State> sStateXml = |
| 958 | new XmlSerialization<State>() { |
| 959 | private static final String CLASS_STATE = "phone_account_registrar_state"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 960 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 961 | private static final String SIM_CALL_MANAGER = "sim_call_manager"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 962 | private static final String ACCOUNTS = "accounts"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 963 | private static final String VERSION = "version"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 964 | |
| 965 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 966 | public void writeToXml(State o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 967 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 968 | if (o != null) { |
| 969 | serializer.startTag(null, CLASS_STATE); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 970 | serializer.attribute(null, VERSION, Objects.toString(EXPECTED_STATE_VERSION)); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 971 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 972 | if (o.defaultOutgoing != null) { |
| 973 | serializer.startTag(null, DEFAULT_OUTGOING); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 974 | sPhoneAccountHandleXml.writeToXml(o.defaultOutgoing, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 975 | serializer.endTag(null, DEFAULT_OUTGOING); |
| 976 | } |
| 977 | |
| 978 | if (o.simCallManager != null) { |
| 979 | serializer.startTag(null, SIM_CALL_MANAGER); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 980 | sPhoneAccountHandleXml.writeToXml(o.simCallManager, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 981 | serializer.endTag(null, SIM_CALL_MANAGER); |
| 982 | } |
| 983 | |
| 984 | serializer.startTag(null, ACCOUNTS); |
| 985 | for (PhoneAccount m : o.accounts) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 986 | sPhoneAccountXml.writeToXml(m, serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 987 | } |
| 988 | serializer.endTag(null, ACCOUNTS); |
| 989 | |
| 990 | serializer.endTag(null, CLASS_STATE); |
Ihab Awad | 293edf2 | 2014-07-24 17:52:29 -0700 | [diff] [blame] | 991 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 995 | public State readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 996 | throws IOException, XmlPullParserException { |
| 997 | if (parser.getName().equals(CLASS_STATE)) { |
| 998 | State s = new State(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 999 | |
| 1000 | String rawVersion = parser.getAttributeValue(null, VERSION); |
| 1001 | s.versionNumber = TextUtils.isEmpty(rawVersion) ? 1 : |
| 1002 | Integer.parseInt(rawVersion); |
| 1003 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1004 | int outerDepth = parser.getDepth(); |
| 1005 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1006 | if (parser.getName().equals(DEFAULT_OUTGOING)) { |
| 1007 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1008 | s.defaultOutgoing = sPhoneAccountHandleXml.readFromXml(parser, |
| 1009 | s.versionNumber, context); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1010 | } else if (parser.getName().equals(SIM_CALL_MANAGER)) { |
| 1011 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1012 | s.simCallManager = sPhoneAccountHandleXml.readFromXml(parser, |
| 1013 | s.versionNumber, context); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1014 | if (s.simCallManager.getUserHandle() == null) { |
| 1015 | // This should never happen, but handle the upgrade case. |
| 1016 | s.simCallManager = new PhoneAccountHandle( |
| 1017 | s.simCallManager.getComponentName(), |
| 1018 | s.simCallManager.getId(), |
| 1019 | Process.myUserHandle()); |
| 1020 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1021 | } else if (parser.getName().equals(ACCOUNTS)) { |
| 1022 | int accountsDepth = parser.getDepth(); |
| 1023 | while (XmlUtils.nextElementWithin(parser, accountsDepth)) { |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1024 | PhoneAccount account = sPhoneAccountXml.readFromXml(parser, |
| 1025 | s.versionNumber, context); |
| 1026 | |
| 1027 | if (account != null && s.accounts != null) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1028 | s.accounts.add(account); |
| 1029 | } |
| 1030 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1031 | } |
| 1032 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1033 | return s; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1034 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1035 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1036 | } |
| 1037 | }; |
| 1038 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1039 | @VisibleForTesting |
| 1040 | public static final XmlSerialization<PhoneAccount> sPhoneAccountXml = |
| 1041 | new XmlSerialization<PhoneAccount>() { |
| 1042 | private static final String CLASS_PHONE_ACCOUNT = "phone_account"; |
| 1043 | private static final String ACCOUNT_HANDLE = "account_handle"; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1044 | private static final String ADDRESS = "handle"; |
| 1045 | private static final String SUBSCRIPTION_ADDRESS = "subscription_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1046 | private static final String CAPABILITIES = "capabilities"; |
| 1047 | private static final String ICON_RES_ID = "icon_res_id"; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1048 | private static final String ICON_PACKAGE_NAME = "icon_package_name"; |
| 1049 | private static final String ICON_BITMAP = "icon_bitmap"; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1050 | private static final String ICON_TINT = "icon_tint"; |
| 1051 | private static final String HIGHLIGHT_COLOR = "highlight_color"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1052 | private static final String LABEL = "label"; |
| 1053 | private static final String SHORT_DESCRIPTION = "short_description"; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1054 | private static final String SUPPORTED_URI_SCHEMES = "supported_uri_schemes"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1055 | |
| 1056 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1057 | public void writeToXml(PhoneAccount o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1058 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1059 | if (o != null) { |
| 1060 | serializer.startTag(null, CLASS_PHONE_ACCOUNT); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1061 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1062 | if (o.getAccountHandle() != null) { |
| 1063 | serializer.startTag(null, ACCOUNT_HANDLE); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1064 | sPhoneAccountHandleXml.writeToXml(o.getAccountHandle(), serializer, context); |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1065 | serializer.endTag(null, ACCOUNT_HANDLE); |
| 1066 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1067 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1068 | writeTextIfNonNull(ADDRESS, o.getAddress(), serializer); |
| 1069 | writeTextIfNonNull(SUBSCRIPTION_ADDRESS, o.getSubscriptionAddress(), serializer); |
| 1070 | writeTextIfNonNull(CAPABILITIES, Integer.toString(o.getCapabilities()), serializer); |
| 1071 | writeTextIfNonNull(ICON_RES_ID, Integer.toString(o.getIconResId()), serializer); |
| 1072 | writeTextIfNonNull(ICON_PACKAGE_NAME, o.getIconPackageName(), serializer); |
| 1073 | writeBitmapIfNonNull(ICON_BITMAP, o.getIconBitmap(), serializer); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1074 | writeTextIfNonNull(ICON_TINT, Integer.toString(o.getIconTint()), serializer); |
| 1075 | writeTextIfNonNull(HIGHLIGHT_COLOR, |
| 1076 | Integer.toString(o.getHighlightColor()), serializer); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1077 | writeTextIfNonNull(LABEL, o.getLabel(), serializer); |
| 1078 | writeTextIfNonNull(SHORT_DESCRIPTION, o.getShortDescription(), serializer); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1079 | writeStringList(SUPPORTED_URI_SCHEMES, o.getSupportedUriSchemes(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1080 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1081 | serializer.endTag(null, CLASS_PHONE_ACCOUNT); |
| 1082 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1085 | public PhoneAccount readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1086 | throws IOException, XmlPullParserException { |
| 1087 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT)) { |
| 1088 | int outerDepth = parser.getDepth(); |
| 1089 | PhoneAccountHandle accountHandle = null; |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1090 | Uri address = null; |
| 1091 | Uri subscriptionAddress = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1092 | int capabilities = 0; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1093 | int iconResId = PhoneAccount.NO_RESOURCE_ID; |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1094 | String iconPackageName = null; |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1095 | Bitmap iconBitmap = null; |
Ihab Awad | 07bc5ee | 2014-11-12 13:42:52 -0800 | [diff] [blame] | 1096 | int iconTint = PhoneAccount.NO_ICON_TINT; |
| 1097 | int highlightColor = PhoneAccount.NO_HIGHLIGHT_COLOR; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1098 | String label = null; |
| 1099 | String shortDescription = null; |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1100 | List<String> supportedUriSchemes = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1101 | |
| 1102 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1103 | if (parser.getName().equals(ACCOUNT_HANDLE)) { |
| 1104 | parser.nextTag(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1105 | accountHandle = sPhoneAccountHandleXml.readFromXml(parser, version, |
| 1106 | context); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1107 | } else if (parser.getName().equals(ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1108 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1109 | address = Uri.parse(parser.getText()); |
| 1110 | } else if (parser.getName().equals(SUBSCRIPTION_ADDRESS)) { |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1111 | parser.next(); |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1112 | String nextText = parser.getText(); |
| 1113 | subscriptionAddress = nextText == null ? null : Uri.parse(nextText); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1114 | } else if (parser.getName().equals(CAPABILITIES)) { |
| 1115 | parser.next(); |
| 1116 | capabilities = Integer.parseInt(parser.getText()); |
| 1117 | } else if (parser.getName().equals(ICON_RES_ID)) { |
| 1118 | parser.next(); |
| 1119 | iconResId = Integer.parseInt(parser.getText()); |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1120 | } else if (parser.getName().equals(ICON_PACKAGE_NAME)) { |
| 1121 | parser.next(); |
| 1122 | iconPackageName = parser.getText(); |
| 1123 | } else if (parser.getName().equals(ICON_BITMAP)) { |
| 1124 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1125 | iconBitmap = readBitmap(parser); |
| 1126 | } else if (parser.getName().equals(ICON_TINT)) { |
Nancy Chen | 06ce062 | 2014-10-23 01:17:35 +0000 | [diff] [blame] | 1127 | parser.next(); |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1128 | iconTint = Integer.parseInt(parser.getText()); |
| 1129 | } else if (parser.getName().equals(HIGHLIGHT_COLOR)) { |
| 1130 | parser.next(); |
| 1131 | highlightColor = Integer.parseInt(parser.getText()); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1132 | } else if (parser.getName().equals(LABEL)) { |
| 1133 | parser.next(); |
| 1134 | label = parser.getText(); |
| 1135 | } else if (parser.getName().equals(SHORT_DESCRIPTION)) { |
| 1136 | parser.next(); |
| 1137 | shortDescription = parser.getText(); |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1138 | } else if (parser.getName().equals(SUPPORTED_URI_SCHEMES)) { |
| 1139 | supportedUriSchemes = readStringList(parser); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1142 | |
| 1143 | // Upgrade older phone accounts to specify the supported URI schemes. |
| 1144 | if (version < 2) { |
| 1145 | ComponentName sipComponentName = new ComponentName("com.android.phone", |
| 1146 | "com.android.services.telephony.sip.SipConnectionService"); |
| 1147 | |
| 1148 | supportedUriSchemes = new ArrayList<>(); |
| 1149 | |
| 1150 | // Handle the SIP connection service. |
| 1151 | // Check the system settings to see if it also should handle "tel" calls. |
| 1152 | if (accountHandle.getComponentName().equals(sipComponentName)) { |
| 1153 | boolean useSipForPstn = useSipForPstnCalls(context); |
| 1154 | supportedUriSchemes.add(PhoneAccount.SCHEME_SIP); |
| 1155 | if (useSipForPstn) { |
| 1156 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1157 | } |
| 1158 | } else { |
| 1159 | supportedUriSchemes.add(PhoneAccount.SCHEME_TEL); |
| 1160 | supportedUriSchemes.add(PhoneAccount.SCHEME_VOICEMAIL); |
| 1161 | } |
| 1162 | } |
| 1163 | |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1164 | // Upgrade older phone accounts with explicit package name |
| 1165 | if (version < 5) { |
| 1166 | if (iconBitmap == null) { |
| 1167 | iconPackageName = accountHandle.getComponentName().getPackageName(); |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | PhoneAccount.Builder builder = PhoneAccount.builder(accountHandle, label) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1172 | .setAddress(address) |
| 1173 | .setSubscriptionAddress(subscriptionAddress) |
| 1174 | .setCapabilities(capabilities) |
Andrew Lee | 7129f1c | 2014-09-04 11:55:07 -0700 | [diff] [blame] | 1175 | .setShortDescription(shortDescription) |
| 1176 | .setSupportedUriSchemes(supportedUriSchemes) |
Ihab Awad | 7e2c7f3 | 2014-11-03 09:49:45 -0800 | [diff] [blame] | 1177 | .setHighlightColor(highlightColor); |
| 1178 | |
| 1179 | if (iconBitmap == null) { |
| 1180 | builder.setIcon(iconPackageName, iconResId, iconTint); |
| 1181 | } else { |
| 1182 | builder.setIcon(iconBitmap); |
| 1183 | } |
| 1184 | |
| 1185 | return builder.build(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1186 | } |
| 1187 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1188 | } |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1189 | |
| 1190 | /** |
| 1191 | * Determines if the SIP call settings specify to use SIP for all calls, including PSTN calls. |
| 1192 | * |
| 1193 | * @param context The context. |
| 1194 | * @return {@code True} if SIP should be used for all calls. |
| 1195 | */ |
| 1196 | private boolean useSipForPstnCalls(Context context) { |
| 1197 | String option = Settings.System.getString(context.getContentResolver(), |
| 1198 | Settings.System.SIP_CALL_OPTIONS); |
| 1199 | option = (option != null) ? option : Settings.System.SIP_ADDRESS_ONLY; |
| 1200 | return option.equals(Settings.System.SIP_ALWAYS); |
| 1201 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1202 | }; |
| 1203 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1204 | @VisibleForTesting |
| 1205 | public static final XmlSerialization<PhoneAccountHandle> sPhoneAccountHandleXml = |
| 1206 | new XmlSerialization<PhoneAccountHandle>() { |
| 1207 | private static final String CLASS_PHONE_ACCOUNT_HANDLE = "phone_account_handle"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1208 | private static final String COMPONENT_NAME = "component_name"; |
| 1209 | private static final String ID = "id"; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1210 | private static final String USER_SERIAL_NUMBER = "user_serial_number"; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1211 | |
| 1212 | @Override |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1213 | public void writeToXml(PhoneAccountHandle o, XmlSerializer serializer, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1214 | throws IOException { |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1215 | if (o != null) { |
| 1216 | serializer.startTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1217 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1218 | if (o.getComponentName() != null) { |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1219 | writeTextIfNonNull( |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1220 | COMPONENT_NAME, o.getComponentName().flattenToString(), serializer); |
| 1221 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1222 | |
Ihab Awad | d9f5438 | 2014-10-24 11:44:47 -0700 | [diff] [blame] | 1223 | writeTextIfNonNull(ID, o.getId(), serializer); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1224 | |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1225 | if (o.getUserHandle() != null && context != null) { |
| 1226 | UserManager userManager = UserManager.get(context); |
| 1227 | writeLong(USER_SERIAL_NUMBER, |
| 1228 | userManager.getSerialNumberForUser(o.getUserHandle()), serializer); |
| 1229 | } |
| 1230 | |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1231 | serializer.endTag(null, CLASS_PHONE_ACCOUNT_HANDLE); |
| 1232 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | @Override |
Tyler Gunn | 8425357 | 2014-09-02 14:50:05 -0700 | [diff] [blame] | 1236 | public PhoneAccountHandle readFromXml(XmlPullParser parser, int version, Context context) |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1237 | throws IOException, XmlPullParserException { |
| 1238 | if (parser.getName().equals(CLASS_PHONE_ACCOUNT_HANDLE)) { |
| 1239 | String componentNameString = null; |
| 1240 | String idString = null; |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1241 | String userSerialNumberString = null; |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1242 | int outerDepth = parser.getDepth(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1243 | |
| 1244 | UserManager userManager = UserManager.get(context); |
| 1245 | |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1246 | while (XmlUtils.nextElementWithin(parser, outerDepth)) { |
| 1247 | if (parser.getName().equals(COMPONENT_NAME)) { |
| 1248 | parser.next(); |
| 1249 | componentNameString = parser.getText(); |
| 1250 | } else if (parser.getName().equals(ID)) { |
| 1251 | parser.next(); |
| 1252 | idString = parser.getText(); |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1253 | } else if (parser.getName().equals(USER_SERIAL_NUMBER)) { |
| 1254 | parser.next(); |
| 1255 | userSerialNumberString = parser.getText(); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } |
Ihab Awad | 2692322 | 2014-07-30 10:54:35 -0700 | [diff] [blame] | 1258 | if (componentNameString != null) { |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1259 | UserHandle userHandle = null; |
| 1260 | if (userSerialNumberString != null) { |
| 1261 | try { |
| 1262 | long serialNumber = Long.parseLong(userSerialNumberString); |
| 1263 | userHandle = userManager.getUserForSerialNumber(serialNumber); |
| 1264 | } catch (NumberFormatException e) { |
| 1265 | Log.e(this, e, "Could not parse UserHandle " + userSerialNumberString); |
| 1266 | } |
| 1267 | } |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1268 | return new PhoneAccountHandle( |
| 1269 | ComponentName.unflattenFromString(componentNameString), |
Evan Charlton | 105d977 | 2014-11-25 14:08:53 -0800 | [diff] [blame] | 1270 | idString, |
| 1271 | userHandle); |
Ihab Awad | b78b276 | 2014-07-25 15:16:23 -0700 | [diff] [blame] | 1272 | } |
| 1273 | } |
| 1274 | return null; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame] | 1275 | } |
| 1276 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 1277 | } |