2 * Copyright 2011 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.ipc.invalidation.ticl.android.c2dm;
20 import com.google.ipc.invalidation.external.client.SystemResources.Logger;
21 import com.google.ipc.invalidation.external.client.android.service.AndroidLogger;
23 import android.content.Context;
24 import android.content.SharedPreferences;
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
30 import java.util.HashSet;
34 * Stores and provides access to private settings used by the C2DM manager.
36 public class C2DMSettings {
38 private static final Logger logger = AndroidLogger.forTag("C2DMSettings");
41 static final String PREFERENCE_PACKAGE = "com.google.android.c2dm.manager";
44 static final String REGISTRATION_ID = "registrationId";
46 private static final String APPLICATION_VERSION = "applicationVersion";
48 private static final String BACKOFF = "c2dm_backoff";
50 private static final long BACKOFF_DEFAULT = 30000;
52 private static final String OBSERVERS = "observers";
54 private static final String REGISTERING = "registering";
56 private static final String UNREGISTERING = "unregistering";
59 * Sets the C2DM registration ID.
61 static void setC2DMRegistrationId(Context context, String registrationId) {
62 storeField(context, REGISTRATION_ID, registrationId);
66 * Clears the C2DM registration ID.
68 static void clearC2DMRegistrationId(Context context) {
69 storeField(context, REGISTRATION_ID, null);
73 * Retrieves the C2DM registration ID (or {@code null} if not stored).
75 static String getC2DMRegistrationId(Context context) {
76 return retrieveField(context, REGISTRATION_ID, null);
80 * Returns {@code true} if there is a C2DM registration ID stored.
82 static boolean hasC2DMRegistrationId(Context context) {
83 return getC2DMRegistrationId(context) != null;
87 * Sets the application version.
89 static void setApplicationVersion(Context context, String applicationVersion) {
90 storeField(context, APPLICATION_VERSION, applicationVersion);
94 * Retrieves the application version (or {@code null} if not stored).
96 static String getApplicationVersion(Context context) {
97 return retrieveField(context, APPLICATION_VERSION, null);
101 * Returns the backoff setting.
103 static long getBackoff(Context context) {
104 return retrieveField(context, BACKOFF, BACKOFF_DEFAULT);
108 * Sets the backoff setting.
112 static void setBackoff(Context context, long backoff) {
113 storeField(context, BACKOFF, backoff);
117 * Resets the backoff setting to the default value.
119 static void resetBackoff(Context context) {
120 setBackoff(context, BACKOFF_DEFAULT);
124 * Sets the boolean flag indicating C2DM registration is in process.
126 static void setRegistering(Context context, boolean registering) {
127 storeField(context, REGISTERING, registering);
131 * Returns {@code true} if C2DM registration is in process.
133 static boolean isRegistering(Context context) {
134 return retrieveField(context, REGISTERING, false);
138 * Sets the boolean flag indicating C2DM unregistration is in process.
140 static void setUnregistering(Context context, boolean registering) {
141 storeField(context, UNREGISTERING, registering);
145 * Returns the boolean flag indicating C2DM unregistration is in process.
147 static boolean isUnregistering(Context context) {
148 return retrieveField(context, UNREGISTERING, false);
152 * Returns the set of stored observers.
154 static Set<C2DMObserver> getObservers(Context context) {
155 return createC2DMObserversFromJSON(retrieveField(context, OBSERVERS, null));
159 * Sets the set of stored observers.
161 static void setObservers(Context context, Set<C2DMObserver> observers) {
162 storeField(context, OBSERVERS, createJsonObserversFromC2DMObservers(observers));
165 private static Set<C2DMObserver> createC2DMObserversFromJSON(String jsonString) {
166 // The observer set is stored in a json array of objects that contain the
167 // observer json representation produced by C2DMObserver.toJSON. Iterate over
168 // this array and recreate observers from the objects.
169 Set<C2DMObserver> observers = new HashSet<C2DMObserver>();
170 if (jsonString == null) {
174 JSONArray array = new JSONArray(jsonString);
175 for (int i = 0; i < array.length(); i++) {
176 JSONObject jsonObserver = array.getJSONObject(i);
177 C2DMObserver observer = C2DMObserver.createFromJSON(jsonObserver);
178 if (observer != null) {
179 observers.add(observer);
182 } catch (JSONException e) {
183 logger.severe("Unable to parse observers. Source: %s", jsonString);
184 observers.clear(); // No partial result
189 private static String createJsonObserversFromC2DMObservers(Set<C2DMObserver> observers) {
190 // Stores the observers as an array of json objects in the format produced by
191 // C2DMObserver.toJSON
192 JSONArray array = new JSONArray();
193 for (C2DMObserver observer : observers) {
194 JSONObject json = observer.toJSON();
199 return array.toString();
202 private static boolean retrieveField(Context context, String field, boolean defaultValue) {
203 SharedPreferences preferences = getPreferences(context);
204 return preferences.getBoolean(field, defaultValue);
207 private static void storeField(Context context, String field, boolean value) {
208 SharedPreferences preferences = getPreferences(context);
209 SharedPreferences.Editor editor = preferences.edit();
210 editor.putBoolean(field, value);
214 private static long retrieveField(Context context, String field, long defaultValue) {
215 SharedPreferences preferences = getPreferences(context);
216 return preferences.getLong(field, defaultValue);
219 private static void storeField(Context context, String field, long value) {
220 SharedPreferences preferences = getPreferences(context);
221 SharedPreferences.Editor editor = preferences.edit();
222 editor.putLong(field, value);
226 private static String retrieveField(Context context, String field, String defaultValue) {
227 SharedPreferences preferences = getPreferences(context);
228 return preferences.getString(field, defaultValue);
231 private static void storeField(Context context, String field, String value) {
232 SharedPreferences preferences = getPreferences(context);
233 SharedPreferences.Editor editor = preferences.edit();
234 editor.putString(field, value);
237 logger.fine("Cleared field %s", field);
241 private static SharedPreferences getPreferences(Context context) {
242 return context.getSharedPreferences(PREFERENCE_PACKAGE, Context.MODE_PRIVATE);
245 /** Sets the C2DM registration id to {@code registrationId}. */
246 public static void setC2DMRegistrationIdForTest(Context context, String registrationId) {
247 setC2DMRegistrationId(context, registrationId);
250 /** Sets the C2DM application version to {@code version}. */
251 public static void setApplicationVersionForTest(Context context, String version) {
252 setApplicationVersion(context, version);