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

Unified 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, 4 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java
new file mode 100644
index 0000000000000000000000000000000000000000..d72181c9303aeecd9cf8b07cf3865d2da0c7eee1
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java
@@ -0,0 +1,156 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.webapps;
+
+import android.content.Context;
+import android.content.SharedPreferences;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.os.AsyncTask;
+import android.text.TextUtils;
+import android.util.Base64;
+
+import java.io.ByteArrayOutputStream;
+
+/**
+ * This is a class used to store data about an installed webapp. It uses SharedPreferences
+ * to persist the data to disk.
+ */
+public class WebappDataStorage {
+
+ private static final String KEY_SPLASH_ICON = "splash_icon";
+ private static final String KEY_LAST_USED = "last_used";
+
+ private final SharedPreferences mPreferences;
+
+ /**
+ * Opens an instance of WebappDataStorage for the webapp specified.
+ * @param context The context to open the SharedPreferences.
+ * @param webappId The ID of the webapp which is being opened.
+ */
+ public static WebappDataStorage open(Context context, String webappId) {
+ WebappDataStorage storage = new WebappDataStorage(
+ context.getApplicationContext(), webappId);
+ storage.updateLastAccessedTime();
+ return storage;
+ }
+
+ /**
+ * 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.
+ * {@link WebappDataStorage#open(Context, String)} asynchrously.
+ * @param context The context to read the SharedPreferences file.
+ * @param webappId The ID of the webapp the accessed time is being read for.
+ * @param callback Called when the last accessed time has been retrieved.
+ */
+ public static WebappDataStorage getLastUsedTime(Context context, String webappId,
+ final FetchCallback<String> callback) {
+ return new WebappDataStorage(context.getApplicationContext(), webappId);
+ }
+
+ private WebappDataStorage(Context context, String webappId) {
+ mPreferences = context.getSharedPreferences("webapp_" + webappId,
+ Context.MODE_PRIVATE);
+ }
+
+ /*
+ * 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.
+ * asynchrously.
+ * @param callback Called when the splash screen image has been retrieved.
+ * May be null if no image was found.
+ */
+ public void getSplashscreenImage(final FetchCallback<Bitmap> callback) {
+ new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute();
+ }
+
+ /*
+ * Update the information associated with the webapp with the specified
+ * data.
gone 2015/08/26 21:56:14 nit: wrap at 100.
Lalit Maganti 2015/08/26 22:33:54 Done.
+ * @param splashscreenImage The image which should be shown on the splash screen of the webapp.
+ */
+ public void updateSplashscreenImage(final Bitmap splashscreenImage) {
+ new UpdateTask(splashscreenImage).execute();
+ }
+
+ private void getLastAccessedTime(final FetchCallback<String> callback) {
+ new AsyncTask<Void, Void, String>() {
+ @Override
+ protected final String doInBackground(Void... nothing) {
+ return mPreferences.getString(KEY_LAST_USED, null);
+ }
+
+ @Override
+ protected final void onPostExecute(String result) {
+ callback.run(result);
+ }
+ }.execute();
+ }
+
+ private WebappDataStorage updateLastAccessedTime() {
+ new AsyncTask<Void, Void, Void>() {
+ @Override
+ protected final Void doInBackground(Void... nothing) {
+ mPreferences.edit()
+ .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.
+ .commit();
+ return null;
+ }
+ }.execute();
+ return this;
+ }
+
+ /**
+ * 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.
+ */
+ public interface FetchCallback<T> {
+ public void run(T readObject);
gone 2015/08/26 21:56:14 run() -> onDataRetrieved().
Lalit Maganti 2015/08/26 22:33:54 Done.
+ }
+
+ private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> {
+
+ private final String mKey;
+
gone 2015/08/26 21:56:14 nit: remove newline?
Lalit Maganti 2015/08/26 22:33:54 Done.
+ private final FetchCallback<Bitmap> mCallback;
+
+ public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) {
+ mKey = key;
+ mCallback = callback;
+ }
+
+ @Override
+ protected final Bitmap doInBackground(Void... nothing) {
+ String icon = mPreferences.getString(mKey, null);
+ if (TextUtils.isEmpty(icon)) return null;
+
+ byte[] decoded = Base64.decode(icon, Base64.DEFAULT);
+ return BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
+ }
+
+ @Override
+ protected final void onPostExecute(Bitmap result) {
+ mCallback.run(result);
+ }
+ }
+
+ private final class UpdateTask extends AsyncTask<Void, Void, Void> {
+
+ private final Bitmap mSplashImage;
+
+ public UpdateTask(Bitmap splashImage) {
+ mSplashImage = splashImage;
+ }
+
+ @Override
+ protected Void doInBackground(Void... nothing) {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ mSplashImage.compress(Bitmap.CompressFormat.PNG, 100, output);
+ String splashImageString = Base64.encodeToString(
+ output.toByteArray(), Base64.DEFAULT);
+ mPreferences.edit()
+ .putString(KEY_SPLASH_ICON, splashImageString)
Lalit Maganti 2015/08/26 22:33:53 Also indented at 8 to match above.
+ .commit();
+ return null;
+ }
+ }
+}
« 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