OLD | NEW |
(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.os.AsyncTask; |
| 11 |
| 12 import org.chromium.chrome.browser.ShortcutHelper; |
| 13 |
| 14 /** |
| 15 * This is a class used to store data about an installed webapp. It uses SharedP
references |
| 16 * to persist the data to disk. Every time {@link WebappDataStorage#open(Context
, String)} |
| 17 * is used, the last used time is updated. It is not updated however, if |
| 18 * {@link WebappDataStorage#getLastUsedTime(Context, String, FetchCallback<Long>
)} is used. |
| 19 */ |
| 20 public class WebappDataStorage { |
| 21 |
| 22 static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; |
| 23 static final String KEY_SPLASH_ICON = "splash_icon"; |
| 24 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.updateLastUsedTime(); |
| 37 return storage; |
| 38 } |
| 39 |
| 40 /** |
| 41 * Asynchronously retrieves the time which this WebappDataStorage was last |
| 42 * opened using {@link WebappDataStorage#open(Context, String)}. |
| 43 * @param context The context to read the SharedPreferences file. |
| 44 * @param webappId The ID of the webapp the used time is being read for. |
| 45 * @param callback Called when the last used time has been retrieved. |
| 46 */ |
| 47 public static void getLastUsedTime(Context context, String webappId, |
| 48 FetchCallback<Long> callback) { |
| 49 new WebappDataStorage(context.getApplicationContext(), webappId) |
| 50 .getLastUsedTime(callback); |
| 51 } |
| 52 |
| 53 private WebappDataStorage(Context context, String webappId) { |
| 54 mPreferences = context.getSharedPreferences( |
| 55 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); |
| 56 } |
| 57 |
| 58 /* |
| 59 * Asynchronously retrieves the splash screen image associated with the |
| 60 * current webapp. |
| 61 * @param callback Called when the splash screen image has been retrieved. |
| 62 * May be null if no image was found. |
| 63 */ |
| 64 public void getSplashScreenImage(FetchCallback<Bitmap> callback) { |
| 65 new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute(); |
| 66 } |
| 67 |
| 68 /* |
| 69 * Update the information associated with the webapp with the specified data
. |
| 70 * @param splashScreenImage The image which should be shown on the splash sc
reen of the webapp. |
| 71 */ |
| 72 public void updateSplashScreenImage(Bitmap splashScreenImage) { |
| 73 new UpdateTask(splashScreenImage).execute(); |
| 74 } |
| 75 |
| 76 private void getLastUsedTime(final FetchCallback<Long> callback) { |
| 77 new AsyncTask<Void, Void, Long>() { |
| 78 @Override |
| 79 protected final Long doInBackground(Void... nothing) { |
| 80 return mPreferences.getLong(KEY_LAST_USED, -1L); |
| 81 } |
| 82 |
| 83 @Override |
| 84 protected final void onPostExecute(Long result) { |
| 85 assert result != -1L; |
| 86 callback.onDataRetrieved(result); |
| 87 } |
| 88 }.execute(); |
| 89 } |
| 90 |
| 91 private WebappDataStorage updateLastUsedTime() { |
| 92 new AsyncTask<Void, Void, Void>() { |
| 93 @Override |
| 94 protected final Void doInBackground(Void... nothing) { |
| 95 mPreferences.edit() |
| 96 .putLong(KEY_LAST_USED, System.currentTimeMillis()) |
| 97 .commit(); |
| 98 return null; |
| 99 } |
| 100 }.execute(); |
| 101 return this; |
| 102 } |
| 103 |
| 104 /** |
| 105 * Called after data has been retrieved from storage. |
| 106 */ |
| 107 public interface FetchCallback<T> { |
| 108 public void onDataRetrieved(T readObject); |
| 109 } |
| 110 |
| 111 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> { |
| 112 |
| 113 private final String mKey; |
| 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 return ShortcutHelper.decodeBitmapFromString(mPreferences.getString(
mKey, null)); |
| 124 } |
| 125 |
| 126 @Override |
| 127 protected final void onPostExecute(Bitmap result) { |
| 128 mCallback.onDataRetrieved(result); |
| 129 } |
| 130 } |
| 131 |
| 132 private final class UpdateTask extends AsyncTask<Void, Void, Void> { |
| 133 |
| 134 private final Bitmap mSplashImage; |
| 135 |
| 136 public UpdateTask(Bitmap splashImage) { |
| 137 mSplashImage = splashImage; |
| 138 } |
| 139 |
| 140 @Override |
| 141 protected Void doInBackground(Void... nothing) { |
| 142 mPreferences.edit() |
| 143 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr
ing(mSplashImage)) |
| 144 .commit(); |
| 145 return null; |
| 146 } |
| 147 } |
| 148 } |
OLD | NEW |