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

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: Add accessed date to storage 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 final SharedPreferences mPreferences;
23
24 private WebappDataStorage(Context context, String webappId) {
25 mPreferences = context.getSharedPreferences("webapp_" + webappId,
26 Context.MODE_PRIVATE);
27 updateLastAccessedDate();
28 }
29
30 public static WebappDataStorage open(Context context, String webappId) {
31 return new WebappDataStorage(context.getApplicationContext(), webappId);
32 }
33
34 public void getSplashIcon(final Callback<Bitmap> callback) {
35 new FetchTask<Bitmap>(callback) {
36 @Override
37 protected Bitmap fetchSharedPreference() {
38 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.
39 Bitmap decodedIcon = null;
40 if (!TextUtils.isEmpty(icon)) {
41 byte[] decoded = Base64.decode(icon, Base64.DEFAULT);
42 decodedIcon = BitmapFactory.decodeByteArray(decoded, 0, deco ded.length);
43 }
44 return decodedIcon;
45 }
46 }.execute();
47 }
48
49 public WebappDataStorage putSplashIcon(final Bitmap icon) {
50 new PutTask() {
51 @Override
52 protected void putSharedPreference() {
53 ByteArrayOutputStream output = new ByteArrayOutputStream();
54 icon.compress(Bitmap.CompressFormat.PNG, 100, output);
55 String splash_icon_base64 = Base64.encodeToString(
56 output.toByteArray(), Base64.DEFAULT);
57 mPreferences.edit()
58 .putString("splash_icon", splash_icon_base64)
59 .commit();
60 }
61 }.execute();
62 return this;
63 }
64
65 public WebappDataStorage updateLastAccessedDate() {
66 new PutTask() {
67 @Override
68 protected void putSharedPreference() {
69 mPreferences.edit()
70 .putLong("last_accessed", System.currentTimeMillis())
71 .commit();
72 }
73 }.execute();
74 return this;
75 }
76
77 /**
78 * Callback class used when items are requested from the storage.
79 */
80 public interface Callback<T> {
81 public void run(T readObject);
82 }
83
84 private abstract static class FetchTask<T> extends AsyncTask<Void, Void, T> {
85
86 private final Callback<T> mCallback;
87
88 public FetchTask(Callback<T> callback) {
89 mCallback = callback;
90 }
91
92 @Override
93 protected final T doInBackground(Void... nothing) {
94 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
95 }
96
97 @Override
98 protected final void onPostExecute(T result) {
99 mCallback.run(result);
100 }
101
102 protected abstract T fetchSharedPreference();
103 }
104
105 private abstract static class PutTask extends AsyncTask<Void, Void, Void> {
106
107 @Override
108 protected final Void doInBackground(Void... nothing) {
109 putSharedPreference();
110 return null;
111 }
112
113 protected abstract void putSharedPreference();
114 }
115 }
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