Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java

Issue 1286973003: webapps: introduce helper class to store extended set of data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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.graphics.BitmapFactory;
11 import android.os.AsyncTask;
12 import android.text.TextUtils;
13 import android.util.Base64;
14
15 import java.io.ByteArrayOutputStream;
16
17 /**
18 * This is a class used to store data about an installed webapp.
19 */
20 public class WebappDataStorage {
21
22 private static final String KEY_SPLASH_ICON = "splash_icon";
23 private static final String KEY_LAST_ACCESSED = "last_accessed";
mlamouri (slow - plz ping) 2015/08/26 13:41:04 nit: last_used. "used" is a more common term. For
Lalit Maganti 2015/08/26 21:20:41 Done.
24
25 private final SharedPreferences mPreferences;
26
27 private WebappDataStorage(Context context, String webappId) {
mlamouri (slow - plz ping) 2015/08/26 13:41:04 style: unless the coding style says otherwise, I w
Lalit Maganti 2015/08/26 21:20:41 Moved to be after the public static methods simila
28 mPreferences = context.getSharedPreferences("webapp_" + webappId,
29 Context.MODE_PRIVATE);
30 }
31
32 /**
33 * Opens an instance of WebappDataStorage for the webapp specified.
34 * Implementation detail: This WebappDataStorage object returned utilizes
gone 2015/08/26 21:03:09 nits: (1) Don't bother saying "Implementation det
Lalit Maganti 2015/08/26 21:20:41 (1) Ack. (2) Done.
35 * SharedPreferences to persist data.
36 * @param context The context to open the SharedPreferences.
37 * @param webappId The ID of the webapp which is being opened.
38 */
39 public static WebappDataStorage open(Context context, String webappId) {
40 WebappDataStorage storage = new WebappDataStorage(
41 context.getApplicationContext(), webappId);
42 storage.updateLastAccessedTime();
43 return storage;
44 }
45
46 /**
47 * Retrieve the time which this WebappDataStorage was last accessed (i.e. la st opened using
48 * the open function) asynchrously.
gone 2015/08/26 21:03:09 nit: Be explicit, especially since this is a comme
gone 2015/08/26 21:04:57 er @link
Lalit Maganti 2015/08/26 21:20:41 Done.
49 * @param context The context to read the SharedPreferences file.
50 * @param webappId The ID of the webapp the accessed time is being read for.
51 * @param callback Called when the last accessed time has been retrieved.
52 */
53 public static WebappDataStorage getLastAccessedTime(Context context, String webappId,
54 final FetchCallback<String> callback) {
55 return new WebappDataStorage(context.getApplicationContext(), webappId);
56 }
57
58 /*
59 * Retrieve the splashcreen image associated with with the current webapp
gone 2015/08/26 21:03:09 nit: If it's not too late, we should standardize o
Lalit Maganti 2015/08/26 21:20:41 Changed but see comment below about camel casing.
60 * asynchrously.
61 * @param callback Called when the splashscreen image has been retrieved.
62 * May be null if no image was found.
63 */
64 public void getSplashImage(final FetchCallback<Bitmap> callback) {
65 new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute();
66 }
67
68 /*
69 * Update the information associated with the webapp with the specified
70 * data.
71 * @param splashscreenImage The image which should be shown on the splashcre en of the webapp.
gone 2015/08/26 21:03:09 nit: splashcreen?
Lalit Maganti 2015/08/26 21:20:41 Fixed.
72 */
73 public void update(final Bitmap splashscreenImage) {
gone 2015/08/26 21:03:09 update() is vague. Use updateSplashscreenImage()
Lalit Maganti 2015/08/26 21:20:41 Changed to updateSplashscreenImage. However, if we
gone 2015/08/26 21:56:14 Ideally, yes. That's one reason why we got stuck
Lalit Maganti 2015/08/26 22:33:53 It's OK as this is the first patch that actually u
74 new UpdateTask(splashscreenImage).execute();
75 }
76
77 private void getLastAccessedTime(final FetchCallback<String> callback) {
78 new AsyncTask<Void, Void, String>() {
79 @Override
80 protected final String doInBackground(Void... nothing) {
81 return mPreferences.getString(KEY_LAST_ACCESSED, null);
82 }
83
84 @Override
85 protected final void onPostExecute(String result) {
86 callback.run(result);
87 }
88 }.execute();
89 }
90
91 private WebappDataStorage updateLastAccessedTime() {
92 new AsyncTask<Void, Void, Void>() {
93 @Override
94 protected final Void doInBackground(Void... nothing) {
95 mPreferences.edit()
96 .putLong(KEY_LAST_ACCESSED, System.currentTimeMillis())
97 .commit();
98 return null;
99 }
100 }.execute();
101 return this;
102 }
103
104 /**
105 * Callback class used when items are requested from the storage.
106 */
107 public interface FetchCallback<T> {
108 public void run(T readObject);
109 }
110
111 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> {
112
113 private final String mKey;
114
115 private final FetchCallback<Bitmap> mCallback;
116
117 public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) {
118 mKey = key;
119 mCallback = callback;
120 }
121
122 @Override
123 protected final Bitmap doInBackground(Void... nothing) {
124 String icon = mPreferences.getString(mKey, null);
125 if (TextUtils.isEmpty(icon)) return null;
126
127 byte[] decoded = Base64.decode(icon, Base64.DEFAULT);
128 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
129 }
130
131 @Override
132 protected final void onPostExecute(Bitmap result) {
133 mCallback.run(result);
134 }
135 }
136
137 private final class UpdateTask extends AsyncTask<Void, Void, Void> {
138
139 private final Bitmap mSplashImage;
140
141 public UpdateTask(Bitmap splashImage) {
142 mSplashImage = splashImage;
143 }
144
145 @Override
146 protected Void doInBackground(Void... nothing) {
147 ByteArrayOutputStream output = new ByteArrayOutputStream();
148 mSplashImage.compress(Bitmap.CompressFormat.PNG, 100, output);
149 String splashImageString = Base64.encodeToString(
150 output.toByteArray(), Base64.DEFAULT);
151 mPreferences.edit()
152 .putString(KEY_SPLASH_ICON, splashImageString)
153 .commit();
154 return null;
155 }
156 }
157 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698