Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / ticl / android / c2dm / C2DMSettings.java
1 /*
2  * Copyright 2011 Google Inc.
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.google.ipc.invalidation.ticl.android.c2dm;
18
19
20 import com.google.ipc.invalidation.external.client.SystemResources.Logger;
21 import com.google.ipc.invalidation.external.client.android.service.AndroidLogger;
22
23 import android.content.Context;
24 import android.content.SharedPreferences;
25
26 import org.json.JSONArray;
27 import org.json.JSONException;
28 import org.json.JSONObject;
29
30 import java.util.HashSet;
31 import java.util.Set;
32
33 /**
34  * Stores and provides access to private settings used by the C2DM manager.
35  */
36 public class C2DMSettings {
37
38   private static final Logger logger = AndroidLogger.forTag("C2DMSettings");
39
40   
41   static final String PREFERENCE_PACKAGE = "com.google.android.c2dm.manager";
42
43   
44   static final String REGISTRATION_ID = "registrationId";
45
46   private static final String APPLICATION_VERSION = "applicationVersion";
47
48   private static final String BACKOFF = "c2dm_backoff";
49
50   private static final long BACKOFF_DEFAULT = 30000;
51
52   private static final String OBSERVERS = "observers";
53
54   private static final String REGISTERING = "registering";
55
56   private static final String UNREGISTERING = "unregistering";
57
58   /**
59    * Sets the C2DM registration ID.
60    */
61   static void setC2DMRegistrationId(Context context, String registrationId) {
62     storeField(context, REGISTRATION_ID, registrationId);
63   }
64
65   /**
66    * Clears the C2DM registration ID.
67    */
68   static void clearC2DMRegistrationId(Context context) {
69     storeField(context, REGISTRATION_ID, null);
70   }
71
72   /**
73    * Retrieves the C2DM registration ID (or {@code null} if not stored).
74    */
75   static String getC2DMRegistrationId(Context context) {
76     return retrieveField(context, REGISTRATION_ID, null);
77   }
78
79   /**
80    * Returns {@code true} if there is a C2DM registration ID stored.
81    */
82   static boolean hasC2DMRegistrationId(Context context) {
83     return getC2DMRegistrationId(context) != null;
84   }
85
86   /**
87    * Sets the application version.
88    */
89   static void setApplicationVersion(Context context, String applicationVersion) {
90     storeField(context, APPLICATION_VERSION, applicationVersion);
91   }
92
93   /**
94    * Retrieves the application version (or {@code null} if not stored).
95    */
96   static String getApplicationVersion(Context context) {
97     return retrieveField(context, APPLICATION_VERSION, null);
98   }
99
100   /**
101    * Returns the backoff setting.
102    */
103   static long getBackoff(Context context) {
104     return retrieveField(context, BACKOFF, BACKOFF_DEFAULT);
105   }
106
107   /**
108    * Sets the backoff setting.
109    * @param context
110    * @param backoff
111    */
112   static void setBackoff(Context context, long backoff) {
113     storeField(context, BACKOFF, backoff);
114   }
115
116   /**
117    * Resets the backoff setting to the default value.
118    */
119   static void resetBackoff(Context context) {
120     setBackoff(context, BACKOFF_DEFAULT);
121   }
122
123   /**
124    * Sets the boolean flag indicating C2DM registration is in process.
125    */
126   static void setRegistering(Context context, boolean registering) {
127     storeField(context, REGISTERING, registering);
128   }
129
130   /**
131    * Returns {@code true} if C2DM registration is in process.
132    */
133   static boolean isRegistering(Context context) {
134     return retrieveField(context, REGISTERING, false);
135   }
136
137   /**
138    * Sets the boolean flag indicating C2DM unregistration is in process.
139    */
140   static void setUnregistering(Context context, boolean registering) {
141     storeField(context, UNREGISTERING, registering);
142   }
143
144   /**
145    * Returns the boolean flag indicating C2DM unregistration is in process.
146    */
147   static boolean isUnregistering(Context context) {
148     return retrieveField(context, UNREGISTERING, false);
149   }
150
151   /**
152    * Returns the set of stored observers.
153    */
154   static Set<C2DMObserver> getObservers(Context context) {
155     return createC2DMObserversFromJSON(retrieveField(context, OBSERVERS, null));
156   }
157
158   /**
159    * Sets the set of stored observers.
160    */
161   static void setObservers(Context context, Set<C2DMObserver> observers) {
162     storeField(context, OBSERVERS, createJsonObserversFromC2DMObservers(observers));
163   }
164
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) {
171       return observers;
172     }
173     try {
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);
180         }
181       }
182     } catch (JSONException e) {
183       logger.severe("Unable to parse observers. Source: %s", jsonString);
184       observers.clear(); // No partial result
185     }
186     return observers;
187   }
188
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();
195       if (json != null) {
196         array.put(json);
197       }
198     }
199     return array.toString();
200   }
201
202   private static boolean retrieveField(Context context, String field, boolean defaultValue) {
203     SharedPreferences preferences = getPreferences(context);
204     return preferences.getBoolean(field, defaultValue);
205   }
206
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);
211     editor.commit();
212   }
213
214   private static long retrieveField(Context context, String field, long defaultValue) {
215     SharedPreferences preferences = getPreferences(context);
216     return preferences.getLong(field, defaultValue);
217   }
218
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);
223     editor.commit();
224   }
225
226   private static String retrieveField(Context context, String field, String defaultValue) {
227     SharedPreferences preferences = getPreferences(context);
228     return preferences.getString(field, defaultValue);
229   }
230
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);
235     editor.commit();
236     if (value == null) {
237       logger.fine("Cleared field %s", field);
238     }
239   }
240
241   private static SharedPreferences getPreferences(Context context) {
242     return context.getSharedPreferences(PREFERENCE_PACKAGE, Context.MODE_PRIVATE);
243   }
244
245   /** Sets the C2DM registration id to {@code registrationId}. */
246   public static void setC2DMRegistrationIdForTest(Context context, String registrationId) {
247     setC2DMRegistrationId(context, registrationId);
248   }
249
250   /** Sets the C2DM application version to {@code version}. */
251   public static void setApplicationVersionForTest(Context context, String version) {
252     setApplicationVersion(context, version);
253   }
254 }