1 // Copyright (c) 2013 Intel Corporation. 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.
5 package org.xwalk.core;
7 import android.content.Context;
8 import android.content.res.Resources;
9 import android.content.res.TypedArray;
10 import android.content.res.XmlResourceParser;
11 import android.graphics.drawable.Drawable;
12 import android.util.TypedValue;
15 * XWalkMixedResources is used to combine the resources
16 * from two different packages.
18 * Chromium uses resources through:
19 * R.layout; R.id; R.string; R.dimen; R.drawable;
20 * R.attr; R.style; R.menu ; R.color
21 * R.layout and R.menu is covered by Resources.getLayout()
22 * R.string is covered by Resources.getText()
23 * R.dimen, R.drawable and R.color is covered by getValue()
25 * For R.id, if it's used like findViewById(R.id.xxx), R.id.xxx
26 * is const at compile time within library context. It works
27 * if the view in hierachy has the same id, which needs inflate
28 * with layout resource from library context. Layout is covered
29 * by getLayout(), so R.id is OK.
32 * For R.attr and R.style, I have no confidence that it's covered.
33 * But the only place use this R.attr and R.style is "select" tag
34 * which is verified working well with this MixedResources.
36 public class XWalkMixedResources extends Resources {
38 private Resources mLibraryResource;
40 private boolean isCalledInLibrary() {
41 StackTraceElement[] stacks = Thread.currentThread().getStackTrace();
42 for (StackTraceElement stack : stacks) {
43 String className = stack.getClassName();
44 if (className.startsWith("org.chromium") ||
45 className.startsWith("org.xwalk.core.internal")) {
47 } else if (className.startsWith("org.xwalk.core") &&
48 !className.endsWith("XWalkMixedResources")) {
55 XWalkMixedResources(Resources base, Resources libraryResources) {
56 super(base.getAssets(), base.getDisplayMetrics(),
57 base.getConfiguration());
58 mLibraryResource = libraryResources;
62 public CharSequence getText(int id) throws NotFoundException {
63 boolean calledInLibrary = isCalledInLibrary();
65 if (calledInLibrary) return mLibraryResource.getText(id);
66 else return super.getText(id);
67 } catch (NotFoundException e) {
68 if (calledInLibrary) return super.getText(id);
69 else return mLibraryResource.getText(id);
74 public XmlResourceParser getLayout(int id) throws NotFoundException {
75 boolean calledInLibrary = isCalledInLibrary();
77 if (calledInLibrary) return mLibraryResource.getLayout(id);
78 else return super.getLayout(id);
79 } catch (NotFoundException e) {
80 if (calledInLibrary) return super.getLayout(id);
81 else return mLibraryResource.getLayout(id);
86 public void getValue(int id, TypedValue outValue, boolean resolveRefs) {
87 boolean calledInLibrary = isCalledInLibrary();
89 if (calledInLibrary) mLibraryResource.getValue(id, outValue, resolveRefs);
90 else super.getValue(id, outValue, resolveRefs);
91 } catch (NotFoundException e) {
92 if (calledInLibrary) super.getValue(id, outValue, resolveRefs);
93 else mLibraryResource.getValue(id, outValue, resolveRefs);
98 public void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs) {
99 boolean calledInLibrary = isCalledInLibrary();
101 if (calledInLibrary) mLibraryResource.getValueForDensity(id, density, outValue, resolveRefs);
102 else super.getValueForDensity(id, density, outValue, resolveRefs);
103 } catch (NotFoundException e) {
104 if (calledInLibrary) super.getValueForDensity(id, density, outValue, resolveRefs);
105 else mLibraryResource.getValueForDensity(id, density, outValue, resolveRefs);
110 public int getIdentifier(String name, String defType, String defPackage) {
111 boolean calledInLibrary = isCalledInLibrary();
112 if (calledInLibrary) {
113 int id = mLibraryResource.getIdentifier(name, defType, defPackage);
114 return id != 0 ? id : super.getIdentifier(name, defType, defPackage);
116 int id = super.getIdentifier(name, defType, defPackage);
117 return id != 0 ? id : mLibraryResource.getIdentifier(name, defType, defPackage);
122 public Drawable getDrawable(int id) {
123 boolean calledInLibrary = isCalledInLibrary();
125 if (calledInLibrary) return mLibraryResource.getDrawable(id);
126 else return super.getDrawable(id);
127 } catch (NotFoundException e) {
128 if (calledInLibrary) return super.getDrawable(id);
129 else return mLibraryResource.getDrawable(id);