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.Intent; | 8 import android.content.Intent; |
9 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
(...skipping 24 matching lines...) Expand all Loading... |
35 static final String KEY_ICON = "icon"; | 35 static final String KEY_ICON = "icon"; |
36 static final String KEY_NAME = "name"; | 36 static final String KEY_NAME = "name"; |
37 static final String KEY_SHORT_NAME = "short_name"; | 37 static final String KEY_SHORT_NAME = "short_name"; |
38 static final String KEY_ORIENTATION = "orientation"; | 38 static final String KEY_ORIENTATION = "orientation"; |
39 static final String KEY_THEME_COLOR = "theme_color"; | 39 static final String KEY_THEME_COLOR = "theme_color"; |
40 static final String KEY_BACKGROUND_COLOR = "background_color"; | 40 static final String KEY_BACKGROUND_COLOR = "background_color"; |
41 static final String KEY_SOURCE = "source"; | 41 static final String KEY_SOURCE = "source"; |
42 static final String KEY_ACTION = "action"; | 42 static final String KEY_ACTION = "action"; |
43 static final String KEY_IS_ICON_GENERATED = "is_icon_generated"; | 43 static final String KEY_IS_ICON_GENERATED = "is_icon_generated"; |
44 static final String KEY_VERSION = "version"; | 44 static final String KEY_VERSION = "version"; |
45 static final String KEY_LAUNCHED = "launched"; | |
46 | 45 |
47 // Unset/invalid constants for last used times and URLs. 0 is used as the nu
ll last | 46 // Unset/invalid constants for last used times and URLs. 0 is used as the nu
ll last |
48 // used time as WebappRegistry assumes that this is always a valid timestamp
. | 47 // used time as WebappRegistry assumes that this is always a valid timestamp
. |
49 static final long LAST_USED_UNSET = 0; | 48 static final long LAST_USED_UNSET = 0; |
50 static final long LAST_USED_INVALID = -1; | 49 static final long LAST_USED_INVALID = -1; |
51 static final String URL_INVALID = ""; | 50 static final String URL_INVALID = ""; |
52 static final int VERSION_INVALID = 0; | 51 static final int VERSION_INVALID = 0; |
53 | 52 |
54 // We use a heuristic to determine whether a web app is still installed on t
he home screen, as | 53 // We use a heuristic to determine whether a web app is still installed on t
he home screen, as |
55 // there is no way to do so directly. Any web app which has been opened in t
he last ten days | 54 // there is no way to do so directly. Any web app which has been opened in t
he last ten days |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 * file. This does NOT delete the file itself but the file is left empty. | 156 * file. This does NOT delete the file itself but the file is left empty. |
158 * @param context The context to read the SharedPreferences file. | 157 * @param context The context to read the SharedPreferences file. |
159 * @param webappId The ID of the web app being deleted. | 158 * @param webappId The ID of the web app being deleted. |
160 */ | 159 */ |
161 static void deleteDataForWebapp(final Context context, final String webappId
) { | 160 static void deleteDataForWebapp(final Context context, final String webappId
) { |
162 assert !ThreadUtils.runningOnUiThread(); | 161 assert !ThreadUtils.runningOnUiThread(); |
163 openSharedPreferences(context, webappId).edit().clear().apply(); | 162 openSharedPreferences(context, webappId).edit().clear().apply(); |
164 } | 163 } |
165 | 164 |
166 /** | 165 /** |
167 * Deletes the launched flag, URL and scope, and sets last used time to 0 in
SharedPreferences. | 166 * Deletes the URL and scope, and sets last used time to 0 in SharedPreferen
ces. |
168 * This does not remove the stored splash screen image (if any) for the app. | 167 * This does not remove the stored splash screen image (if any) for the app. |
169 * @param context The context to read the SharedPreferences file. | 168 * @param context The context to read the SharedPreferences file. |
170 * @param webappId The ID of the web app for which history is being cleared. | 169 * @param webappId The ID of the web app for which history is being cleared. |
171 */ | 170 */ |
172 static void clearHistory(final Context context, final String webappId) { | 171 static void clearHistory(final Context context, final String webappId) { |
173 assert !ThreadUtils.runningOnUiThread(); | 172 assert !ThreadUtils.runningOnUiThread(); |
174 SharedPreferences.Editor editor = openSharedPreferences(context, webappI
d).edit(); | 173 SharedPreferences.Editor editor = openSharedPreferences(context, webappI
d).edit(); |
175 | 174 |
176 // The last used time is set to 0 to ensure that a valid value is always
present. | 175 // The last used time is set to 0 to ensure that a valid value is always
present. |
177 // If the web app is not launched prior to the next cleanup, then its re
maining data will be | 176 // If the web app is not launched prior to the next cleanup, then its re
maining data will be |
178 // removed. Otherwise, the next launch from home screen will update the
last used time. | 177 // removed. Otherwise, the next launch from home screen will update the
last used time. |
179 editor.putLong(KEY_LAST_USED, LAST_USED_UNSET); | 178 editor.putLong(KEY_LAST_USED, LAST_USED_UNSET); |
180 editor.remove(KEY_LAUNCHED); | |
181 editor.remove(KEY_URL); | 179 editor.remove(KEY_URL); |
182 editor.remove(KEY_SCOPE); | 180 editor.remove(KEY_SCOPE); |
183 editor.apply(); | 181 editor.apply(); |
184 } | 182 } |
185 | 183 |
186 /** | 184 /** |
187 * Sets the clock used to get the current time. | 185 * Sets the clock used to get the current time. |
188 */ | 186 */ |
189 @VisibleForTesting | 187 @VisibleForTesting |
190 public static void setClockForTests(Clock clock) { | 188 public static void setClockForTests(Clock clock) { |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
363 */ | 361 */ |
364 long getLastUsedTime() { | 362 long getLastUsedTime() { |
365 return mPreferences.getLong(KEY_LAST_USED, LAST_USED_INVALID); | 363 return mPreferences.getLong(KEY_LAST_USED, LAST_USED_INVALID); |
366 } | 364 } |
367 | 365 |
368 /** | 366 /** |
369 * Returns true if this web app has been launched from home screen recently
(within | 367 * Returns true if this web app has been launched from home screen recently
(within |
370 * WEBAPP_LAST_OPEN_MAX_TIME milliseconds). | 368 * WEBAPP_LAST_OPEN_MAX_TIME milliseconds). |
371 */ | 369 */ |
372 public boolean wasLaunchedRecently() { | 370 public boolean wasLaunchedRecently() { |
373 // Registering the web app sets the last used time, so we must also ensu
re that the web app | 371 // Registering the web app sets the last used time, so that counts as a
'launch'. |
374 // has actually been launched. Otherwise, launches from home screen are
the only occasion | 372 return (sClock.currentTimeMillis() - getLastUsedTime() < WEBAPP_LAST_OPE
N_MAX_TIME); |
375 // when last used time is updated. | |
376 return getLaunched() | |
377 && (sClock.currentTimeMillis() - getLastUsedTime() < WEBAPP_LAST
_OPEN_MAX_TIME); | |
378 } | |
379 | |
380 /** | |
381 * Returns true if this web app has been launched from home screen. | |
382 */ | |
383 boolean getLaunched() { | |
384 return mPreferences.getBoolean(KEY_LAUNCHED, false); | |
385 } | |
386 | |
387 /** | |
388 * Marks this web app as having been launched from home screen. | |
389 */ | |
390 void setLaunched() { | |
391 mPreferences.edit().putBoolean(KEY_LAUNCHED, true).apply(); | |
392 } | 373 } |
393 | 374 |
394 private Map<String, ?> getAllData() { | 375 private Map<String, ?> getAllData() { |
395 return mPreferences.getAll(); | 376 return mPreferences.getAll(); |
396 } | 377 } |
397 | 378 |
398 /** | 379 /** |
399 * Called after data has been retrieved from storage. | 380 * Called after data has been retrieved from storage. |
400 */ | 381 */ |
401 public interface FetchCallback<T> { | 382 public interface FetchCallback<T> { |
(...skipping 21 matching lines...) Expand all Loading... |
423 */ | 404 */ |
424 public static class Clock { | 405 public static class Clock { |
425 /** | 406 /** |
426 * Returns the current time in milliseconds. | 407 * Returns the current time in milliseconds. |
427 */ | 408 */ |
428 public long currentTimeMillis() { | 409 public long currentTimeMillis() { |
429 return System.currentTimeMillis(); | 410 return System.currentTimeMillis(); |
430 } | 411 } |
431 } | 412 } |
432 } | 413 } |
OLD | NEW |