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