blob: 4b6feca03dfd8b81753329be390edee852a173b6 [file] [log] [blame]
Santos Cordon176ae282014-07-14 02:02:14 -07001/*
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
17package com.android.telecomm;
18
Ihab Awad104f8062014-07-17 11:29:35 -070019import org.json.JSONArray;
20import org.json.JSONException;
21import org.json.JSONObject;
22import org.json.JSONTokener;
23
Santos Cordon176ae282014-07-14 02:02:14 -070024import android.content.ComponentName;
25import android.content.Context;
26
27import android.content.SharedPreferences;
Santos Cordon176ae282014-07-14 02:02:14 -070028import android.net.Uri;
Santos Cordon176ae282014-07-14 02:02:14 -070029import android.telecomm.PhoneAccount;
Ihab Awad104f8062014-07-17 11:29:35 -070030import android.telecomm.PhoneAccountMetadata;
31import android.telecomm.TelecommManager;
Santos Cordon176ae282014-07-14 02:02:14 -070032
33import java.util.ArrayList;
34import java.util.List;
35import java.util.Objects;
36
37/**
Ihab Awad104f8062014-07-17 11:29:35 -070038 * 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 Cordon176ae282014-07-14 02:02:14 -070044 * TODO(santoscordon): Replace this implementation with a proper database stored in a Telecomm
45 * provider.
46 */
47final class PhoneAccountRegistrar {
Santos Cordon176ae282014-07-14 02:02:14 -070048 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 Cordon176ae282014-07-14 02:02:14 -070053 PhoneAccountRegistrar(Context context) {
54 mContext = context;
55 }
56
Ihab Awad104f8062014-07-17 11:29:35 -070057 public PhoneAccount getDefaultOutgoingPhoneAccount() {
58 State s = read();
59 return s.mDefaultOutgoing;
60 }
Santos Cordon176ae282014-07-14 02:02:14 -070061
Ihab Awad104f8062014-07-17 11:29:35 -070062 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 Cordon176ae282014-07-14 02:02:14 -070075 }
Ihab Awad104f8062014-07-17 11:29:35 -070076
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 Cordon176ae282014-07-14 02:02:14 -070083 }
84
Ihab Awad104f8062014-07-17 11:29:35 -070085 write(s);
Santos Cordon176ae282014-07-14 02:02:14 -070086 }
87
Ihab Awad104f8062014-07-17 11:29:35 -070088 public List<PhoneAccount> getEnabledPhoneAccounts() {
89 State s = read();
90 return accountsOnly(s);
Santos Cordon176ae282014-07-14 02:02:14 -070091 }
92
Ihab Awad104f8062014-07-17 11:29:35 -070093 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 Cordon176ae282014-07-14 02:02:14 -070098 }
99 }
100 return null;
101 }
102
Ihab Awad104f8062014-07-17 11:29:35 -0700103 // 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 Cordon176ae282014-07-14 02:02:14 -0700107
Ihab Awad104f8062014-07-17 11:29:35 -0700108 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 Cordon176ae282014-07-14 02:02:14 -0700115 }
116 }
117
Ihab Awad104f8062014-07-17 11:29:35 -0700118 write(s);
Santos Cordon176ae282014-07-14 02:02:14 -0700119 }
120
Ihab Awad104f8062014-07-17 11:29:35 -0700121 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 Cordon176ae282014-07-14 02:02:14 -0700134 }
135
Ihab Awad104f8062014-07-17 11:29:35 -0700136 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 Cordon176ae282014-07-14 02:02:14 -0700150 }
151
Ihab Awad104f8062014-07-17 11:29:35 -0700152 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 Cordon176ae282014-07-14 02:02:14 -0700161 }
162
Ihab Awad104f8062014-07-17 11:29:35 -0700163 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 Cordon176ae282014-07-14 02:02:14 -0700201 }
202
203 private SharedPreferences getPreferences() {
204 return mContext.getSharedPreferences(TELECOMM_PREFERENCES, Context.MODE_PRIVATE);
205 }
Ihab Awad104f8062014-07-17 11:29:35 -0700206
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 Cordon176ae282014-07-14 02:02:14 -0700325}