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

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: Add accessed date to storage 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..77e9c4ae073d6cfa313514a9b5592ae66e26c2ce
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java
@@ -0,0 +1,115 @@
+// 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.
+ */
+public class WebappDataStorage {
+
+ private final SharedPreferences mPreferences;
+
+ private WebappDataStorage(Context context, String webappId) {
+ mPreferences = context.getSharedPreferences("webapp_" + webappId,
+ Context.MODE_PRIVATE);
+ updateLastAccessedDate();
+ }
+
+ public static WebappDataStorage open(Context context, String webappId) {
+ return new WebappDataStorage(context.getApplicationContext(), webappId);
+ }
+
+ public void getSplashIcon(final Callback<Bitmap> callback) {
+ new FetchTask<Bitmap>(callback) {
+ @Override
+ protected Bitmap fetchSharedPreference() {
+ String icon = mPreferences.getString("splash_icon", "");
gone 2015/08/24 21:15:16 Pull these keys into private static final Strings.
Lalit Maganti 2015/08/25 13:35:17 Done.
+ Bitmap decodedIcon = null;
+ if (!TextUtils.isEmpty(icon)) {
+ byte[] decoded = Base64.decode(icon, Base64.DEFAULT);
+ decodedIcon = BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
+ }
+ return decodedIcon;
+ }
+ }.execute();
+ }
+
+ public WebappDataStorage putSplashIcon(final Bitmap icon) {
+ new PutTask() {
+ @Override
+ protected void putSharedPreference() {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ icon.compress(Bitmap.CompressFormat.PNG, 100, output);
+ String splash_icon_base64 = Base64.encodeToString(
+ output.toByteArray(), Base64.DEFAULT);
+ mPreferences.edit()
+ .putString("splash_icon", splash_icon_base64)
+ .commit();
+ }
+ }.execute();
+ return this;
+ }
+
+ public WebappDataStorage updateLastAccessedDate() {
+ new PutTask() {
+ @Override
+ protected void putSharedPreference() {
+ mPreferences.edit()
+ .putLong("last_accessed", System.currentTimeMillis())
+ .commit();
+ }
+ }.execute();
+ return this;
+ }
+
+ /**
+ * Callback class used when items are requested from the storage.
+ */
+ public interface Callback<T> {
+ public void run(T readObject);
+ }
+
+ private abstract static class FetchTask<T> extends AsyncTask<Void, Void, T> {
+
+ private final Callback<T> mCallback;
+
+ public FetchTask(Callback<T> callback) {
+ mCallback = callback;
+ }
+
+ @Override
+ protected final T doInBackground(Void... nothing) {
+ return fetchSharedPreference();
gone 2015/08/24 20:22:46 Is there any reason why you set these AsyncTasks t
Lalit Maganti 2015/08/24 20:34:23 Right now this doesn't make any sense to add this
gone 2015/08/24 20:37:33 I'd suggest dealing with abstracting it away when
Lalit Maganti 2015/08/24 20:43:20 The eternal struggle between abstraction and compl
gone 2015/08/24 20:57:36 Do you have a concrete second use case for abstrac
Lalit Maganti 2015/08/24 21:00:16 I'd imagine we'd use this for name/short name as w
gone 2015/08/24 21:15:16 I'd still suggest separating the image and string
Lalit Maganti 2015/08/25 13:35:17 OK I've split off the icon saving/loading logic in
+ }
+
+ @Override
+ protected final void onPostExecute(T result) {
+ mCallback.run(result);
+ }
+
+ protected abstract T fetchSharedPreference();
+ }
+
+ private abstract static class PutTask extends AsyncTask<Void, Void, Void> {
+
+ @Override
+ protected final Void doInBackground(Void... nothing) {
+ putSharedPreference();
+ return null;
+ }
+
+ protected abstract void putSharedPreference();
+ }
+}
« 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