OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.content.Context; | 7 import android.content.Context; |
8 import android.content.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.graphics.Bitmap; | 9 import android.graphics.Bitmap; |
10 import android.os.AsyncTask; | 10 import android.os.AsyncTask; |
(...skipping 13 matching lines...) Expand all Loading... | |
24 * (1) UPDATING/RETRIEVING THE ICON (web app MUST have been registered in Webapp Registry) | 24 * (1) UPDATING/RETRIEVING THE ICON (web app MUST have been registered in Webapp Registry) |
25 * WebappDataStorage storage = WebappDataStorage.open(context, id); | 25 * WebappDataStorage storage = WebappDataStorage.open(context, id); |
26 * storage.updateSplashScreenImage(bitmap); | 26 * storage.updateSplashScreenImage(bitmap); |
27 * storage.getSplashScreenImage(callback); | 27 * storage.getSplashScreenImage(callback); |
28 */ | 28 */ |
29 public class WebappDataStorage { | 29 public class WebappDataStorage { |
30 | 30 |
31 static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; | 31 static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; |
32 static final String KEY_SPLASH_ICON = "splash_icon"; | 32 static final String KEY_SPLASH_ICON = "splash_icon"; |
33 static final String KEY_LAST_USED = "last_used"; | 33 static final String KEY_LAST_USED = "last_used"; |
34 static final String KEY_ORIGIN_URL = "origin_url"; | |
35 | |
36 // Null/invalid constants for last used times and origins. 0 is used as the null last used time | |
37 // as WebappRegistry assumes that this is always a valid timestamp. | |
38 static final long NULL_LAST_USED = 0; | |
gone
2016/03/08 22:40:39
It's kind of weird to have both an INVALID and a N
| |
34 static final long INVALID_LAST_USED = -1; | 39 static final long INVALID_LAST_USED = -1; |
40 static final String INVALID_ORIGIN_URL = ""; | |
35 | 41 |
36 private static Factory sFactory = new Factory(); | 42 private static Factory sFactory = new Factory(); |
37 | 43 |
38 private final SharedPreferences mPreferences; | 44 private final SharedPreferences mPreferences; |
39 | 45 |
40 /** | 46 /** |
41 * Opens an instance of WebappDataStorage for the web app specified. | 47 * Opens an instance of WebappDataStorage for the web app specified. |
42 * @param context The context to open the SharedPreferences. | 48 * @param context The context to open the SharedPreferences. |
43 * @param webappId The ID of the web app which is being opened. | 49 * @param webappId The ID of the web app which is being opened. |
44 */ | 50 */ |
45 public static WebappDataStorage open(final Context context, final String web appId) { | 51 public static WebappDataStorage open(final Context context, final String web appId) { |
46 final WebappDataStorage storage = sFactory.create(context, webappId); | 52 final WebappDataStorage storage = sFactory.create(context, webappId); |
47 new AsyncTask<Void, Void, Void>() { | 53 new AsyncTask<Void, Void, Void>() { |
48 @Override | 54 @Override |
49 protected final Void doInBackground(Void... nothing) { | 55 protected final Void doInBackground(Void... nothing) { |
50 if (storage.getLastUsedTime() == INVALID_LAST_USED) { | 56 // The last used time may be invalid if the user has cleared the ir history. |
51 // If the last used time is invalid then assert that there i s no data | 57 // The next time the webapp is opened, set it to a valid value a gain. |
52 // in the WebappDataStorage which needs to be cleaned up. | 58 storage.updateLastUsedTime(System.currentTimeMillis()); |
53 assert storage.getAllData().isEmpty(); | |
54 } else { | |
55 storage.updateLastUsedTime(); | |
56 } | |
57 return null; | 59 return null; |
58 } | 60 } |
59 }.execute(); | 61 }.execute(); |
60 return storage; | 62 return storage; |
61 } | 63 } |
62 | 64 |
63 /** | 65 /** |
64 * Asynchronously retrieves the time which this WebappDataStorage was last | 66 * Asynchronously retrieves the time which this WebappDataStorage was last |
65 * opened using {@link WebappDataStorage#open(Context, String)}. | 67 * opened using {@link WebappDataStorage#open(Context, String)}. |
66 * @param context The context to read the SharedPreferences file. | 68 * @param context The context to read the SharedPreferences file. |
(...skipping 12 matching lines...) Expand all Loading... | |
79 } | 81 } |
80 | 82 |
81 @Override | 83 @Override |
82 protected final void onPostExecute(Long lastUsed) { | 84 protected final void onPostExecute(Long lastUsed) { |
83 callback.onDataRetrieved(lastUsed); | 85 callback.onDataRetrieved(lastUsed); |
84 } | 86 } |
85 }.execute(); | 87 }.execute(); |
86 } | 88 } |
87 | 89 |
88 /** | 90 /** |
91 * Asynchronously retrieves the origin URL stored in this WebappDataStorage. | |
92 * @param context The context to read the SharedPreferences file. | |
93 * @param webappId The ID of the web app the used time is being read for. | |
94 * @param callback Called when the origin has been retrieved. | |
95 */ | |
96 public static void getOriginUrl(final Context context, final String webappId , | |
97 final FetchCallback<String> callback) { | |
98 new AsyncTask<Void, Void, String>() { | |
99 @Override | |
100 protected final String doInBackground(Void... nothing) { | |
101 String originUrl = new WebappDataStorage(context.getApplicationC ontext(), webappId) | |
102 .getOriginUrl(); | |
103 return originUrl; | |
104 } | |
105 | |
106 @Override | |
107 protected final void onPostExecute(String originUrl) { | |
108 callback.onDataRetrieved(originUrl); | |
109 } | |
110 }.execute(); | |
111 } | |
112 | |
113 /** | |
114 * Asynchronously updates the origin URL stored in this WebappDataStorage. | |
115 * @param context The context to read the SharedPreferences file. | |
116 * @param webappId The ID of the web app the used time is being read for. | |
117 * @param origin The origin to set for the web app. | |
118 */ | |
119 public static void updateOriginUrl(final Context context, final String webap pId, | |
120 final String originUrl) { | |
121 new AsyncTask<Void, Void, Void>() { | |
122 @Override | |
123 protected final Void doInBackground(Void... nothing) { | |
124 new WebappDataStorage(context.getApplicationContext(), webappId) | |
125 .updateOriginUrl(originUrl); | |
126 return null; | |
127 } | |
128 }.execute(); | |
129 } | |
130 | |
131 /** | |
89 * Deletes the data for a web app by clearing all the information inside the SharedPreferences | 132 * Deletes the data for a web app by clearing all the information inside the SharedPreferences |
90 * file. This does NOT delete the file itself but the file is left empty. | 133 * file. This does NOT delete the file itself but the file is left empty. |
91 * @param context The context to read the SharedPreferences file. | 134 * @param context The context to read the SharedPreferences file. |
92 * @param webappId The ID of the web app being deleted. | 135 * @param webappId The ID of the web app being deleted. |
93 */ | 136 */ |
94 static void deleteDataForWebapp(final Context context, final String webappId ) { | 137 static void deleteDataForWebapp(final Context context, final String webappId ) { |
95 assert !ThreadUtils.runningOnUiThread(); | 138 assert !ThreadUtils.runningOnUiThread(); |
96 openSharedPreferences(context, webappId).edit().clear().apply(); | 139 openSharedPreferences(context, webappId).edit().clear().apply(); |
97 } | 140 } |
98 | 141 |
99 /** | 142 /** |
143 * Deletes the origin URL and sets last used time to 0 this web app in Share dPreferences. | |
144 * This does not remove the stored splash screen image (if any) for the app. | |
145 * @param context The context to read the SharedPreferences file. | |
146 * @param webappId The ID of the web app being deleted. | |
147 */ | |
148 static void clearHistory(final Context context, final String webappId) { | |
149 // The last used time is set to 0 to ensure that a valid value is always present. | |
150 // If the webapp is not launched prior to the next cleanup, then its rem aining data will be | |
151 // removed. Otherwise, the next launch will update the last used time. | |
152 assert !ThreadUtils.runningOnUiThread(); | |
153 openSharedPreferences(context, webappId) | |
154 .edit().putLong(KEY_LAST_USED, NULL_LAST_USED).remove(KEY_ORIGIN_URL ).apply(); | |
gone
2016/03/08 22:40:39
nit: indent by 8
| |
155 } | |
156 | |
157 /** | |
100 * Sets the factory used to generate WebappDataStorage objects. | 158 * Sets the factory used to generate WebappDataStorage objects. |
101 */ | 159 */ |
102 @VisibleForTesting | 160 @VisibleForTesting |
103 public static void setFactoryForTests(Factory factory) { | 161 public static void setFactoryForTests(Factory factory) { |
104 sFactory = factory; | 162 sFactory = factory; |
105 } | 163 } |
106 | 164 |
107 private static SharedPreferences openSharedPreferences(Context context, Stri ng webappId) { | 165 private static SharedPreferences openSharedPreferences(Context context, Stri ng webappId) { |
108 return context.getApplicationContext().getSharedPreferences( | 166 return context.getApplicationContext().getSharedPreferences( |
109 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); | 167 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); |
(...skipping 14 matching lines...) Expand all Loading... | |
124 } | 182 } |
125 | 183 |
126 /* | 184 /* |
127 * Update the information associated with the web app with the specified dat a. | 185 * Update the information associated with the web app with the specified dat a. |
128 * @param splashScreenImage The image which should be shown on the splash sc reen of the web app. | 186 * @param splashScreenImage The image which should be shown on the splash sc reen of the web app. |
129 */ | 187 */ |
130 public void updateSplashScreenImage(Bitmap splashScreenImage) { | 188 public void updateSplashScreenImage(Bitmap splashScreenImage) { |
131 new UpdateTask(splashScreenImage).execute(); | 189 new UpdateTask(splashScreenImage).execute(); |
132 } | 190 } |
133 | 191 |
134 void updateLastUsedTime() { | 192 void updateOriginUrl(String originUrl) { |
135 assert !ThreadUtils.runningOnUiThread(); | 193 assert !ThreadUtils.runningOnUiThread(); |
136 mPreferences.edit().putLong(KEY_LAST_USED, System.currentTimeMillis()).a pply(); | 194 mPreferences.edit().putString(KEY_ORIGIN_URL, originUrl).apply(); |
195 } | |
196 | |
197 String getOriginUrl() { | |
198 assert !ThreadUtils.runningOnUiThread(); | |
199 return mPreferences.getString(KEY_ORIGIN_URL, INVALID_ORIGIN_URL); | |
200 } | |
201 | |
202 void updateLastUsedTime(long lastUsedTime) { | |
203 assert !ThreadUtils.runningOnUiThread(); | |
204 mPreferences.edit().putLong(KEY_LAST_USED, lastUsedTime).apply(); | |
137 } | 205 } |
138 | 206 |
139 long getLastUsedTime() { | 207 long getLastUsedTime() { |
140 assert !ThreadUtils.runningOnUiThread(); | 208 assert !ThreadUtils.runningOnUiThread(); |
141 return mPreferences.getLong(KEY_LAST_USED, INVALID_LAST_USED); | 209 return mPreferences.getLong(KEY_LAST_USED, INVALID_LAST_USED); |
142 } | 210 } |
143 | 211 |
144 private Map<String, ?> getAllData() { | 212 private Map<String, ?> getAllData() { |
145 return mPreferences.getAll(); | 213 return mPreferences.getAll(); |
146 } | 214 } |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
197 } | 265 } |
198 | 266 |
199 @Override | 267 @Override |
200 protected Void doInBackground(Void... nothing) { | 268 protected Void doInBackground(Void... nothing) { |
201 mPreferences.edit() | 269 mPreferences.edit() |
202 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr ing(mSplashImage)) | 270 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr ing(mSplashImage)) |
203 .apply(); | 271 .apply(); |
204 return null; | 272 return null; |
205 } | 273 } |
206 } | 274 } |
207 } | 275 } |
OLD | NEW |