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 | |
| 17 | package com.android.telecomm; |
| 18 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 19 | import org.json.JSONArray; |
| 20 | import org.json.JSONException; |
| 21 | import org.json.JSONObject; |
| 22 | import org.json.JSONTokener; |
| 23 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
| 25 | import android.content.Context; |
| 26 | |
| 27 | import android.content.SharedPreferences; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 28 | import android.net.Uri; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 29 | import android.telecomm.PhoneAccount; |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 30 | import android.telecomm.PhoneAccountMetadata; |
| 31 | import android.telecomm.TelecommManager; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 32 | |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
| 35 | import java.util.Objects; |
| 36 | |
| 37 | /** |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 38 | * Handles writing and reading PhoneAccount registration entries. This is a simple verbatim |
| 39 | * delegate for all the account handling methods on {@link TelecommManager} as implemented in |
| 40 | * {@link TelecommServiceImpl}, with the notable exception that {@link TelecommServiceImpl} is |
| 41 | * responsible for security checking to make sure that the caller has proper authority over |
| 42 | * the {@code ComponentName}s they are declaring in their {@code PhoneAccount}s. |
| 43 | * |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 44 | * TODO(santoscordon): Replace this implementation with a proper database stored in a Telecomm |
| 45 | * provider. |
| 46 | */ |
| 47 | final class PhoneAccountRegistrar { |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 48 | private static final String TELECOMM_PREFERENCES = "telecomm_prefs"; |
| 49 | private static final String PREFERENCE_PHONE_ACCOUNTS = "phone_accounts"; |
| 50 | |
| 51 | private final Context mContext; |
| 52 | |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 53 | PhoneAccountRegistrar(Context context) { |
| 54 | mContext = context; |
| 55 | } |
| 56 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 57 | public PhoneAccount getDefaultOutgoingPhoneAccount() { |
| 58 | State s = read(); |
| 59 | return s.mDefaultOutgoing; |
| 60 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 61 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 62 | public void setDefaultOutgoingPhoneAccount(PhoneAccount account) { |
| 63 | State s = read(); |
| 64 | |
| 65 | if (account == null) { |
| 66 | // Asking to clear the default outgoing is a valid request |
| 67 | s.mDefaultOutgoing = null; |
| 68 | } else { |
| 69 | boolean found = false; |
| 70 | for (PhoneAccountMetadata m : s.mAccounts) { |
| 71 | if (Objects.equals(account, m.getAccount())) { |
| 72 | found = true; |
| 73 | break; |
| 74 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 75 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 76 | |
| 77 | if (!found) { |
| 78 | Log.d(this, "Trying to set nonexistent default outgoing phone account %s", account); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | s.mDefaultOutgoing = account; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 85 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 88 | public List<PhoneAccount> getEnabledPhoneAccounts() { |
| 89 | State s = read(); |
| 90 | return accountsOnly(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 93 | public PhoneAccountMetadata getPhoneAccountMetadata(PhoneAccount account) { |
| 94 | State s = read(); |
| 95 | for (PhoneAccountMetadata m : s.mAccounts) { |
| 96 | if (Objects.equals(account, m.getAccount())) { |
| 97 | return m; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | return null; |
| 101 | } |
| 102 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 103 | // TODO: Should we implement an artificial limit for # of accounts associated with a single |
| 104 | // ComponentName? |
| 105 | public void registerPhoneAccount(PhoneAccountMetadata metadata) { |
| 106 | State s = read(); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 107 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 108 | s.mAccounts.add(metadata); |
| 109 | // Search for duplicates and remove any that are found. |
| 110 | for (int i = 0; i < s.mAccounts.size() - 1; i++) { |
| 111 | if (Objects.equals(metadata.getAccount(), s.mAccounts.get(i).getAccount())) { |
| 112 | // replace existing entry. |
| 113 | s.mAccounts.remove(i); |
| 114 | break; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 118 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 121 | public void unregisterPhoneAccount(PhoneAccount account) { |
| 122 | State s = read(); |
| 123 | |
| 124 | for (int i = 0; i < s.mAccounts.size(); i++) { |
| 125 | if (Objects.equals(account, s.mAccounts.get(i).getAccount())) { |
| 126 | s.mAccounts.remove(i); |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | checkDefaultOutgoing(s); |
| 132 | |
| 133 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 136 | public void clearAccounts(String packageName) { |
| 137 | State s = read(); |
| 138 | |
| 139 | for (int i = 0; i < s.mAccounts.size(); i++) { |
| 140 | if (Objects.equals( |
| 141 | packageName, |
| 142 | s.mAccounts.get(i).getAccount().getComponentName().getPackageName())) { |
| 143 | s.mAccounts.remove(i); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | checkDefaultOutgoing(s); |
| 148 | |
| 149 | write(s); |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 152 | private void checkDefaultOutgoing(State s) { |
| 153 | // Check that, after an operation that removes accounts, the account set up as the "default |
| 154 | // outgoing" has not been deleted. If it has, then clear out the setting. |
| 155 | for (PhoneAccountMetadata m : s.mAccounts) { |
| 156 | if (Objects.equals(s.mDefaultOutgoing, m.getAccount())) { |
| 157 | return; |
| 158 | } |
| 159 | } |
| 160 | s.mDefaultOutgoing = null; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 163 | private List<PhoneAccount> accountsOnly(State s) { |
| 164 | List<PhoneAccount> result = new ArrayList<>(); |
| 165 | for (PhoneAccountMetadata m : s.mAccounts) { |
| 166 | result.add(m.getAccount()); |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | |
| 171 | private State read() { |
| 172 | try { |
| 173 | String serialized = getPreferences().getString(PREFERENCE_PHONE_ACCOUNTS, null); |
| 174 | Log.d(this, "read() obtained serialized state: %s", serialized); |
| 175 | State state = serialized == null |
| 176 | ? new State() |
| 177 | : deserializeState(serialized); |
| 178 | Log.d(this, "read() obtained state: %s", state); |
| 179 | return state; |
| 180 | } catch (Exception e) { |
| 181 | Log.e(this, e, "read"); |
| 182 | throw new RuntimeException(e); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private boolean write(State state) { |
| 187 | try { |
| 188 | Log.d(this, "write() writing state: %s", state); |
| 189 | String serialized = serializeState(state); |
| 190 | Log.d(this, "write() writing serialized state: %s", serialized); |
| 191 | boolean success = getPreferences() |
| 192 | .edit() |
| 193 | .putString(PREFERENCE_PHONE_ACCOUNTS, serialized) |
| 194 | .commit(); |
| 195 | Log.d(this, "serialized state was written with succcess = %b", success); |
| 196 | return success; |
| 197 | } catch (Exception e) { |
| 198 | Log.e(this, e, "write"); |
| 199 | throw new RuntimeException(e); |
| 200 | } |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private SharedPreferences getPreferences() { |
| 204 | return mContext.getSharedPreferences(TELECOMM_PREFERENCES, Context.MODE_PRIVATE); |
| 205 | } |
Ihab Awad | 104f806 | 2014-07-17 11:29:35 -0700 | [diff] [blame^] | 206 | |
| 207 | private String serializeState(State s) throws JSONException { |
| 208 | // TODO: If this is used in production, remove the indent (=> do not pretty print) |
| 209 | return sStateJson.toJson(s).toString(2); |
| 210 | } |
| 211 | |
| 212 | private State deserializeState(String s) throws JSONException { |
| 213 | return sStateJson.fromJson(new JSONObject(new JSONTokener(s))); |
| 214 | } |
| 215 | |
| 216 | private static class State { |
| 217 | PhoneAccount mDefaultOutgoing = null; |
| 218 | final List<PhoneAccountMetadata> mAccounts = new ArrayList<>(); |
| 219 | } |
| 220 | |
| 221 | // |
| 222 | // JSON serialization |
| 223 | // |
| 224 | |
| 225 | private interface Json<T> { |
| 226 | JSONObject toJson(T o) throws JSONException; |
| 227 | T fromJson(JSONObject json) throws JSONException; |
| 228 | } |
| 229 | |
| 230 | private static final Json<State> sStateJson = |
| 231 | new Json<State>() { |
| 232 | private static final String DEFAULT_OUTGOING = "default_outgoing"; |
| 233 | private static final String ACCOUNTS = "accounts"; |
| 234 | |
| 235 | @Override |
| 236 | public JSONObject toJson(State o) throws JSONException { |
| 237 | JSONObject json = new JSONObject(); |
| 238 | if (o.mDefaultOutgoing != null) { |
| 239 | json.put(DEFAULT_OUTGOING, sPhoneAccountJson.toJson(o.mDefaultOutgoing)); |
| 240 | } |
| 241 | JSONArray accounts = new JSONArray(); |
| 242 | for (PhoneAccountMetadata m : o.mAccounts) { |
| 243 | accounts.put(sPhoneAccountMetadataJson.toJson(m)); |
| 244 | } |
| 245 | json.put(ACCOUNTS, accounts); |
| 246 | return json; |
| 247 | } |
| 248 | |
| 249 | @Override |
| 250 | public State fromJson(JSONObject json) throws JSONException { |
| 251 | State s = new State(); |
| 252 | if (json.has(DEFAULT_OUTGOING)) { |
| 253 | s.mDefaultOutgoing = sPhoneAccountJson.fromJson( |
| 254 | (JSONObject) json.get(DEFAULT_OUTGOING)); |
| 255 | } |
| 256 | if (json.has(ACCOUNTS)) { |
| 257 | JSONArray accounts = (JSONArray) json.get(ACCOUNTS); |
| 258 | for (int i = 0; i < accounts.length(); i++) { |
| 259 | try { |
| 260 | s.mAccounts.add(sPhoneAccountMetadataJson.fromJson( |
| 261 | (JSONObject) accounts.get(i))); |
| 262 | } catch (Exception e) { |
| 263 | Log.e(this, e, "Extracting phone account"); |
| 264 | } |
| 265 | } |
| 266 | } |
| 267 | return s; |
| 268 | } |
| 269 | }; |
| 270 | |
| 271 | private static final Json<PhoneAccountMetadata> sPhoneAccountMetadataJson = |
| 272 | new Json<PhoneAccountMetadata>() { |
| 273 | private static final String ACCOUNT = "account"; |
| 274 | private static final String HANDLE = "handle"; |
| 275 | private static final String CAPABILITIES = "capabilities"; |
| 276 | private static final String ICON_RES_ID = "icon_res_id"; |
| 277 | private static final String LABEL = "label"; |
| 278 | private static final String SHORT_DESCRIPTION = "short_description"; |
| 279 | private static final String VIDEO_CALLING_SUPPORTED = "video_calling_supported"; |
| 280 | |
| 281 | @Override |
| 282 | public JSONObject toJson(PhoneAccountMetadata o) throws JSONException { |
| 283 | return new JSONObject() |
| 284 | .put(ACCOUNT, sPhoneAccountJson.toJson(o.getAccount())) |
| 285 | .put(HANDLE, o.getHandle().toString()) |
| 286 | .put(CAPABILITIES, o.getCapabilities()) |
| 287 | .put(ICON_RES_ID, o.getIconResId()) |
| 288 | .put(LABEL, o.getLabel()) |
| 289 | .put(SHORT_DESCRIPTION, o.getShortDescription()) |
| 290 | .put(VIDEO_CALLING_SUPPORTED, (Boolean) o.isVideoCallingSupported()); |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public PhoneAccountMetadata fromJson(JSONObject json) throws JSONException { |
| 295 | return new PhoneAccountMetadata( |
| 296 | sPhoneAccountJson.fromJson((JSONObject) json.get(ACCOUNT)), |
| 297 | Uri.parse((String) json.get(HANDLE)), |
| 298 | (int) json.get(CAPABILITIES), |
| 299 | (int) json.get(ICON_RES_ID), |
| 300 | (String) json.get(LABEL), |
| 301 | (String) json.get(SHORT_DESCRIPTION), |
| 302 | (Boolean) json.get(VIDEO_CALLING_SUPPORTED)); |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | private static final Json<PhoneAccount> sPhoneAccountJson = |
| 307 | new Json<PhoneAccount>() { |
| 308 | private static final String COMPONENT_NAME = "component_name"; |
| 309 | private static final String ID = "id"; |
| 310 | |
| 311 | @Override |
| 312 | public JSONObject toJson(PhoneAccount o) throws JSONException { |
| 313 | return new JSONObject() |
| 314 | .put(COMPONENT_NAME, o.getComponentName().flattenToString()) |
| 315 | .put(ID, o.getId()); |
| 316 | } |
| 317 | |
| 318 | @Override |
| 319 | public PhoneAccount fromJson(JSONObject json) throws JSONException { |
| 320 | return new PhoneAccount( |
| 321 | ComponentName.unflattenFromString((String) json.get(COMPONENT_NAME)), |
| 322 | (String) json.get(ID)); |
| 323 | } |
| 324 | }; |
Santos Cordon | 176ae28 | 2014-07-14 02:02:14 -0700 | [diff] [blame] | 325 | } |