OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.webapps; | |
6 | |
7 import android.content.Context; | |
8 import android.content.SharedPreferences; | |
9 import android.graphics.Bitmap; | |
10 import android.os.AsyncTask; | |
11 | |
12 import org.chromium.chrome.browser.ShortcutHelper; | |
13 | |
14 /** | |
15 * This is a class used to store data about an installed webapp. It uses SharedP references | |
16 * to persist the data to disk. | |
mlamouri (slow - plz ping)
2015/08/27 16:18:23
nit: I would add a comment explaining that every t
| |
17 */ | |
18 public class WebappDataStorage { | |
19 | |
20 static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; | |
21 static final String KEY_SPLASH_ICON = "splash_icon"; | |
22 static final String KEY_LAST_USED = "last_used"; | |
23 | |
24 private final SharedPreferences mPreferences; | |
25 | |
26 /** | |
27 * Opens an instance of WebappDataStorage for the webapp specified. | |
28 * @param context The context to open the SharedPreferences. | |
29 * @param webappId The ID of the webapp which is being opened. | |
30 */ | |
31 public static WebappDataStorage open(Context context, String webappId) { | |
32 WebappDataStorage storage = new WebappDataStorage( | |
33 context.getApplicationContext(), webappId); | |
34 storage.updateLastUsedTime(); | |
35 return storage; | |
36 } | |
37 | |
38 /** | |
39 * Asynchronously retrieves the time which this WebappDataStorage was last | |
40 * opened using {@link WebappDataStorage#open(Context, String)}. | |
41 * @param context The context to read the SharedPreferences file. | |
42 * @param webappId The ID of the webapp the used time is being read for. | |
43 * @param callback Called when the last used time has been retrieved. | |
44 */ | |
45 public static void getLastUsedTime(Context context, String webappId, | |
46 final FetchCallback<Long> callback) { | |
47 new WebappDataStorage(context.getApplicationContext(), webappId) | |
48 .getLastUsedTime(callback); | |
49 } | |
50 | |
51 private WebappDataStorage(Context context, String webappId) { | |
52 mPreferences = context.getSharedPreferences( | |
53 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); | |
54 } | |
55 | |
56 /* | |
57 * Asynchronously retrieves the splash screen image associated with the | |
58 * current webapp. | |
59 * @param callback Called when the splash screen image has been retrieved. | |
60 * May be null if no image was found. | |
61 */ | |
62 public void getSplashScreenImage(final FetchCallback<Bitmap> callback) { | |
63 new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute(); | |
64 } | |
65 | |
66 /* | |
67 * Update the information associated with the webapp with the specified data . | |
68 * @param splashScreenImage The image which should be shown on the splash sc reen of the webapp. | |
69 */ | |
70 public void updateSplashScreenImage(final Bitmap splashScreenImage) { | |
71 new UpdateTask(splashScreenImage).execute(); | |
72 } | |
73 | |
74 private void getLastUsedTime(final FetchCallback<Long> callback) { | |
75 new AsyncTask<Void, Void, Long>() { | |
76 @Override | |
77 protected final Long doInBackground(Void... nothing) { | |
78 return mPreferences.getLong(KEY_LAST_USED, -1L); | |
79 } | |
80 | |
81 @Override | |
82 protected final void onPostExecute(Long result) { | |
83 callback.onDataRetrieved(result == -1L ? null : result); | |
mlamouri (slow - plz ping)
2015/08/27 16:18:23
-1 shouldn't happen, right? Should that assert?
Lalit Maganti
2015/08/27 16:24:34
-1 will be returned if there is no last used time.
mlamouri (slow - plz ping)
2015/08/27 16:29:30
That's the point. Can you add an assert?
Lalit Maganti
2015/08/27 16:40:30
OK done.
| |
84 } | |
85 }.execute(); | |
86 } | |
87 | |
88 private WebappDataStorage updateLastUsedTime() { | |
89 new AsyncTask<Void, Void, Void>() { | |
90 @Override | |
91 protected final Void doInBackground(Void... nothing) { | |
92 mPreferences.edit() | |
93 .putLong(KEY_LAST_USED, System.currentTimeMillis()) | |
94 .commit(); | |
95 return null; | |
96 } | |
97 }.execute(); | |
98 return this; | |
99 } | |
100 | |
101 /** | |
102 * Called after data has been retrieved from storage. | |
103 */ | |
104 public interface FetchCallback<T> { | |
105 public void onDataRetrieved(T readObject); | |
106 } | |
107 | |
108 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> { | |
109 | |
110 private final String mKey; | |
111 private final FetchCallback<Bitmap> mCallback; | |
112 | |
113 public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) { | |
114 mKey = key; | |
115 mCallback = callback; | |
116 } | |
117 | |
118 @Override | |
119 protected final Bitmap doInBackground(Void... nothing) { | |
120 System.out.println(mPreferences.getString(mKey, null)); | |
mlamouri (slow - plz ping)
2015/08/27 16:18:23
nit: remove?
Lalit Maganti
2015/08/27 16:24:34
Whoops missed removing this one.
Done.
| |
121 return ShortcutHelper.decodeBitmapFromString(mPreferences.getString( mKey, null)); | |
122 } | |
123 | |
124 @Override | |
125 protected final void onPostExecute(Bitmap result) { | |
126 mCallback.onDataRetrieved(result); | |
127 } | |
128 } | |
129 | |
130 private final class UpdateTask extends AsyncTask<Void, Void, Void> { | |
131 | |
132 private final Bitmap mSplashImage; | |
133 | |
134 public UpdateTask(Bitmap splashImage) { | |
135 mSplashImage = splashImage; | |
136 } | |
137 | |
138 @Override | |
139 protected Void doInBackground(Void... nothing) { | |
140 mPreferences.edit() | |
141 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr ing(mSplashImage)) | |
142 .commit(); | |
143 return null; | |
144 } | |
145 } | |
146 } | |
OLD | NEW |