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

Unified Diff: content/public/android/java/src/org/chromium/content_public/resources/BitmapResources.java

Issue 731133002: Upstream ResourceManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add generated enum class to BUILD.gn Created 6 years, 1 month 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
Index: content/public/android/java/src/org/chromium/content_public/resources/BitmapResources.java
diff --git a/content/public/android/java/src/org/chromium/content_public/resources/BitmapResources.java b/content/public/android/java/src/org/chromium/content_public/resources/BitmapResources.java
new file mode 100644
index 0000000000000000000000000000000000000000..1b65964e7b49eb0763f5b45480c988031a54b0b3
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content_public/resources/BitmapResources.java
@@ -0,0 +1,129 @@
+// Copyright 2013 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.content_public.resources;
+
+import android.content.Context;
+import android.content.res.Resources;
+import android.graphics.Bitmap;
+import android.graphics.BitmapFactory;
+import android.graphics.Canvas;
+import android.os.AsyncTask;
+import android.util.Log;
+import android.util.SparseArray;
+import android.view.InflateException;
+
+/**
+ * This class handles loading some GL related resources.
+ */
+public class BitmapResources {
David Trainor- moved to gerrit 2014/11/18 18:53:53 Does anything actually use this file?
Jaekyun Seok (inactive) 2014/11/18 23:34:02 No, I will remove this file.
+
+ private static final String TAG = "BitmapResources";
+
+ private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;
+
+ /**
+ * Load an Android resource as a Bitmap.
+ *
+ * @param resources The resources to load the Bitmap from.
+ * @param resourceId The id of the resource to load.
+ * @return The Bitmap if it could be loaded, null otherwise.
+ */
+ public static Bitmap load(Resources resources, int resourceId) {
+ Bitmap bitmap = null;
+ try {
+ BitmapFactory.Options options = new BitmapFactory.Options();
+ options.inPreferredConfig = BITMAP_CONFIG;
+ Bitmap factoryBitmap = BitmapFactory.decodeResource(
+ resources, resourceId, options);
+
+ if (factoryBitmap.getConfig() == BITMAP_CONFIG) {
+ bitmap = factoryBitmap;
+ } else {
+ // The factory was unable to load in the correct format, so
+ // convert it.
+ bitmap = Bitmap.createBitmap(factoryBitmap.getWidth(),
+ factoryBitmap.getHeight(), BITMAP_CONFIG);
+ Canvas canvas = new Canvas(bitmap);
+ canvas.drawBitmap(factoryBitmap, 0, 0, null);
+ factoryBitmap.recycle();
+ }
+ } catch (OutOfMemoryError ex) {
+ Log.w(TAG, "OutOfMemoryError while trying to load resource.");
+ if (bitmap != null)
+ bitmap.recycle();
+ bitmap = null;
+ } catch (InflateException ex) {
+ Log.w(TAG, "InflateException while trying to load resource.");
+ if (bitmap != null)
+ bitmap.recycle();
+ bitmap = null;
+ } catch (Exception e) {
+ // Gracefully handle any other errors.
+ Log.e(TAG, "Unable to load bitmap", e);
+ if (bitmap != null)
+ bitmap.recycle();
+ }
+ return bitmap;
+ }
+
+ private final SparseArray<AsyncTask<Void, Void, Bitmap>> mBitmapLoaders;
+
+ public BitmapResources() {
+ mBitmapLoaders = new SparseArray<AsyncTask<Void, Void, Bitmap>>();
+ }
+
+ /**
+ * Attempt to preload a Bitmap in a background thread.
+ *
+ * @param context The Context to load the Bitmap from.
+ * @param id The id of the Bitmap to load.
+ */
+ public void preload(Context context, final int id) {
+ final Resources res = context.getResources();
+ if (mBitmapLoaders.get(id) == null) {
+ final AsyncTask<Void, Void, Bitmap> task = new AsyncTask<Void, Void, Bitmap>() {
+ @Override
+ protected Bitmap doInBackground(Void... params) {
+ return load(res, id);
+ }
+ };
+ task.execute();
+ mBitmapLoaders.put(id, task);
+ }
+ }
+
+ /**
+ * Get a Bitmap. If a preloaded Bitmap is available, it will be returned. Otherwise, the
+ * Bitmap will be loaded on demand. This method may block.
+ *
+ * @param context The Context to load the Bitmap from.
+ * @param id The id of the Bitmap to load.
+ *
+ * @return The Bitmap if it could be loaded, null otherwise.
+ */
+ public Bitmap get(Context context, int id) {
+ Bitmap bitmap = null;
+ AsyncTask<Void, Void, Bitmap> preloader = mBitmapLoaders.get(id);
+ if (preloader != null) {
+ mBitmapLoaders.remove(id);
+ try {
+ // Try to cancel the task. This should succeed only if the task hasn't
+ // started yet. If it hasn't started yet, we don't want to wait for it.
+ if (!preloader.cancel(false)) {
+ bitmap = preloader.get();
+ }
+ } catch (Exception e) {
+ bitmap = null;
+ }
+ }
+
+ if (bitmap == null) {
+ bitmap = load(context.getResources(), id);
+ }
+
+ return bitmap;
+ }
+
+}

Powered by Google App Engine
This is Rietveld 408576698