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

Side by Side Diff: ui/android/java/src/org/chromium/ui/resources/ResourceManager.java

Issue 731133002: Upstream ResourceManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rename namespace Created 6 years 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
OLDNEW
(Empty)
1 // Copyright 2014 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.ui.resources;
6
7 import android.content.Context;
8 import android.content.res.Resources;
9 import android.graphics.Bitmap;
10 import android.graphics.Rect;
11 import android.util.SparseArray;
12
13 import org.chromium.base.CalledByNative;
14 import org.chromium.base.JNINamespace;
15 import org.chromium.ui.resources.ResourceLoader.ResourceLoaderCallback;
16 import org.chromium.ui.resources.dynamics.DynamicResource;
17 import org.chromium.ui.resources.dynamics.DynamicResourceLoader;
18 import org.chromium.ui.resources.statics.StaticResourceLoader;
19 import org.chromium.ui.resources.system.SystemResourceLoader;
20
21 /**
22 * The Java component of a manager for all static resources to be loaded and use d by CC layers.
23 * This class does not hold any resource state, but passes it directly to native as they are loaded.
24 */
25 @JNINamespace("ui")
26 public class ResourceManager implements ResourceLoaderCallback {
27 private final SparseArray<ResourceLoader> mResourceLoaders = new SparseArray <ResourceLoader>();
28 private final SparseArray<SparseArray<LayoutResource>> mLoadedResources =
29 new SparseArray<SparseArray<LayoutResource>>();
30
31 private final float mPxToDp;
32
33 private long mNativeResourceManagerPtr;
34
35 private ResourceManager(Context context, long staticResourceManagerPtr) {
36 Resources resources = context.getResources();
37 mPxToDp = 1.f / resources.getDisplayMetrics().density;
38
39 // Register ResourceLoaders
40 registerResourceLoader(new StaticResourceLoader(
41 AndroidResourceType.STATIC, this, resources));
42 registerResourceLoader(new DynamicResourceLoader(
43 AndroidResourceType.DYNAMIC, this));
44 registerResourceLoader(new DynamicResourceLoader(
45 AndroidResourceType.DYNAMIC_BITMAP, this));
46 registerResourceLoader(new SystemResourceLoader(
47 AndroidResourceType.SYSTEM, this, context));
48
49 mNativeResourceManagerPtr = staticResourceManagerPtr;
50 }
51
52 /**
53 * Creates an instance of a {@link ResourceManager}. This will
54 * @param context A {@link Context} instance to grab {@link Resources} from.
55 * @param staticResourceManagerPtr A pointer to the native component of this class.
56 * @return A new instance of a {@link ResourceManage r}.
57 */
58 @CalledByNative
59 private static ResourceManager create(Context context, long staticResourceMa nagerPtr) {
60 return new ResourceManager(context, staticResourceManagerPtr);
61 }
62
63 /**
64 * @return A reference to the {@link DynamicResourceLoader} that provides
65 * {@link DynamicResource} objects to this class.
66 */
67 public DynamicResourceLoader getDynamicResourceLoader() {
68 return (DynamicResourceLoader) mResourceLoaders.get(
69 AndroidResourceType.DYNAMIC);
70 }
71
72 /**
73 * @return A reference to the {@link DynamicResourceLoader} for bitmaps that provides
74 * {@link BitmapDynamicResource} objects to this class.
75 */
76 public DynamicResourceLoader getBitmapDynamicResourceLoader() {
77 return (DynamicResourceLoader) mResourceLoaders.get(
78 AndroidResourceType.DYNAMIC_BITMAP);
79 }
80
81 /**
82 * Automatically loads any synchronous resources specified in |syncIds| and will start
83 * asynchronous reads for any asynchronous resources specified in |asyncIds| .
84 * @param type AndroidResourceType which will be loaded.
85 * @param syncIds Resource ids which will be loaded synchronously.
86 * @param asyncIds Resource ids which will be loaded asynchronously.
87 */
88 public void preloadResources(int type, int[] syncIds, int[] asyncIds) {
89 ResourceLoader loader = mResourceLoaders.get(type);
90 if (asyncIds != null) {
91 for (Integer resId : asyncIds) {
92 loader.preloadResource(resId);
93 }
94 }
95
96 if (syncIds != null) {
97 for (Integer resId : syncIds) {
98 loader.loadResource(resId);
99 }
100 }
101 }
102
103 /**
104 * @param resType The type of the Android resource.
105 * @param resId The id of the Android resource.
106 * @return The corresponding {@link LayoutResource}.
107 */
108 public LayoutResource getResource(int resType, int resId) {
109 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType);
110 return bucket != null ? bucket.get(resId) : null;
111 }
112
113 @Override
114 public void onResourceLoaded(int resType, int resId, Resource resource) {
115 if (resource == null) return;
116
117 saveMetadataForLoadedResource(resType, resId, resource);
118
119 if (mNativeResourceManagerPtr == 0) return;
120 Rect padding = resource.getPadding();
121 Rect aperture = resource.getAperture();
122
123 nativeOnResourceReady(mNativeResourceManagerPtr, resType, resId, resourc e.getBitmap(),
124 padding.left, padding.top, padding.right, padding.bottom,
125 aperture.left, aperture.top, aperture.right, aperture.bottom);
126 }
127
128 private void saveMetadataForLoadedResource(int resType, int resId, Resource resource) {
129 SparseArray<LayoutResource> bucket = mLoadedResources.get(resType);
130 if (bucket == null) {
131 bucket = new SparseArray<LayoutResource>();
132 mLoadedResources.put(resType, bucket);
133 }
134 bucket.put(resId, new LayoutResource(mPxToDp, resource));
135 }
136
137 @CalledByNative
138 private void destroy() {
139 assert mNativeResourceManagerPtr != 0;
140 mNativeResourceManagerPtr = 0;
141 }
142
143 @CalledByNative
144 private void resourceRequested(int resType, int resId) {
145 ResourceLoader loader = mResourceLoaders.get(resType);
146 if (loader != null) loader.loadResource(resId);
147 }
148
149 @CalledByNative
150 private void preloadResource(int resType, int resId) {
151 ResourceLoader loader = mResourceLoaders.get(resType);
152 if (loader != null) loader.preloadResource(resId);
153 }
154
155 @CalledByNative
156 private long getNativePtr() {
157 return mNativeResourceManagerPtr;
158 }
159
160 private void registerResourceLoader(ResourceLoader loader) {
161 mResourceLoaders.put(loader.getResourceType(), loader);
162 }
163
164 private native void nativeOnResourceReady(long nativeResourceManager, int re sType,
165 int resId, Bitmap bitmap, int paddingLeft, int paddingTop, int paddi ngRight,
166 int paddingBottom, int apertureLeft, int apertureTop, int apertureRi ght,
167 int apertureBottom);
168
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698