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(); |
+ } |
+} |