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

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: Fix minor issues 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. It uses SharedP references
19 * to persist the data to disk.
20 */
21 public class WebappDataStorage {
22
23 private static final String KEY_SPLASH_ICON = "splash_icon";
24 private static final String KEY_LAST_USED = "last_used";
25
26 private final SharedPreferences mPreferences;
27
28 /**
29 * Opens an instance of WebappDataStorage for the webapp specified.
30 * @param context The context to open the SharedPreferences.
31 * @param webappId The ID of the webapp which is being opened.
32 */
33 public static WebappDataStorage open(Context context, String webappId) {
34 WebappDataStorage storage = new WebappDataStorage(
35 context.getApplicationContext(), webappId);
36 storage.updateLastAccessedTime();
37 return storage;
38 }
39
40 /**
41 * Retrieve the time which this WebappDataStorage was last opened using
gone 2015/08/26 21:56:14 nit: Asynchronously retrieves the time which this
Lalit Maganti 2015/08/26 22:33:53 Done.
42 * {@link WebappDataStorage#open(Context, String)} asynchrously.
43 * @param context The context to read the SharedPreferences file.
44 * @param webappId The ID of the webapp the accessed time is being read for.
45 * @param callback Called when the last accessed time has been retrieved.
46 */
47 public static WebappDataStorage getLastUsedTime(Context context, String weba ppId,
48 final FetchCallback<String> callback) {
49 return new WebappDataStorage(context.getApplicationContext(), webappId);
50 }
51
52 private WebappDataStorage(Context context, String webappId) {
53 mPreferences = context.getSharedPreferences("webapp_" + webappId,
54 Context.MODE_PRIVATE);
55 }
56
57 /*
58 * Retrieve the splash screen image associated with with the current webapp
gone 2015/08/26 21:56:14 nit: Asynchronously retrieves the splash screen im
Lalit Maganti 2015/08/26 22:33:53 Done.
59 * asynchrously.
60 * @param callback Called when the splash screen image has been retrieved.
61 * May be null if no image was found.
62 */
63 public void getSplashscreenImage(final FetchCallback<Bitmap> callback) {
64 new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute();
65 }
66
67 /*
68 * Update the information associated with the webapp with the specified
69 * data.
gone 2015/08/26 21:56:14 nit: wrap at 100.
Lalit Maganti 2015/08/26 22:33:54 Done.
70 * @param splashscreenImage The image which should be shown on the splash sc reen of the webapp.
71 */
72 public void updateSplashscreenImage(final Bitmap splashscreenImage) {
73 new UpdateTask(splashscreenImage).execute();
74 }
75
76 private void getLastAccessedTime(final FetchCallback<String> callback) {
77 new AsyncTask<Void, Void, String>() {
78 @Override
79 protected final String doInBackground(Void... nothing) {
80 return mPreferences.getString(KEY_LAST_USED, null);
81 }
82
83 @Override
84 protected final void onPostExecute(String result) {
85 callback.run(result);
86 }
87 }.execute();
88 }
89
90 private WebappDataStorage updateLastAccessedTime() {
91 new AsyncTask<Void, Void, Void>() {
92 @Override
93 protected final Void doInBackground(Void... nothing) {
94 mPreferences.edit()
95 .putLong(KEY_LAST_USED, System.currentTimeMillis())
gone 2015/08/26 21:56:14 nit: indent by 8.
Lalit Maganti 2015/08/26 22:33:53 Done.
96 .commit();
97 return null;
98 }
99 }.execute();
100 return this;
101 }
102
103 /**
104 * Callback class used when items are requested from the storage.
gone 2015/08/26 21:56:14 nit: Called after data has been retrieved from sto
Lalit Maganti 2015/08/26 22:33:53 Done.
105 */
106 public interface FetchCallback<T> {
107 public void run(T readObject);
gone 2015/08/26 21:56:14 run() -> onDataRetrieved().
Lalit Maganti 2015/08/26 22:33:54 Done.
108 }
109
110 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> {
111
112 private final String mKey;
113
gone 2015/08/26 21:56:14 nit: remove newline?
Lalit Maganti 2015/08/26 22:33:54 Done.
114 private final FetchCallback<Bitmap> mCallback;
115
116 public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) {
117 mKey = key;
118 mCallback = callback;
119 }
120
121 @Override
122 protected final Bitmap doInBackground(Void... nothing) {
123 String icon = mPreferences.getString(mKey, null);
124 if (TextUtils.isEmpty(icon)) return null;
125
126 byte[] decoded = Base64.decode(icon, Base64.DEFAULT);
127 return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
128 }
129
130 @Override
131 protected final void onPostExecute(Bitmap result) {
132 mCallback.run(result);
133 }
134 }
135
136 private final class UpdateTask extends AsyncTask<Void, Void, Void> {
137
138 private final Bitmap mSplashImage;
139
140 public UpdateTask(Bitmap splashImage) {
141 mSplashImage = splashImage;
142 }
143
144 @Override
145 protected Void doInBackground(Void... nothing) {
146 ByteArrayOutputStream output = new ByteArrayOutputStream();
147 mSplashImage.compress(Bitmap.CompressFormat.PNG, 100, output);
148 String splashImageString = Base64.encodeToString(
149 output.toByteArray(), Base64.DEFAULT);
150 mPreferences.edit()
151 .putString(KEY_SPLASH_ICON, splashImageString)
Lalit Maganti 2015/08/26 22:33:53 Also indented at 8 to match above.
152 .commit();
153 return null;
154 }
155 }
156 }
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