Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkMixedResources.java
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.
4
5 package org.xwalk.core;
6
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;
13
14 /**
15  * XWalkMixedResources is used to combine the resources
16  * from two different packages.
17  *
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()
24  *
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.
30  *
31  * TODO(wang16):
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.
35  */
36 public class XWalkMixedResources extends Resources {
37
38     private Resources mLibraryResource;
39
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")) {
46                 return true;
47             } else if (className.startsWith("org.xwalk.core") &&
48                     !className.endsWith("XWalkMixedResources")) {
49                 return false;
50             }
51         }
52         return false;
53     }
54
55     XWalkMixedResources(Resources base, Resources libraryResources) {
56         super(base.getAssets(), base.getDisplayMetrics(),
57                 base.getConfiguration());
58         mLibraryResource = libraryResources;
59     }
60
61     @Override
62     public CharSequence getText(int id) throws NotFoundException {
63         boolean calledInLibrary = isCalledInLibrary();
64         try {
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);
70         }
71     }
72
73     @Override
74     public XmlResourceParser getLayout(int id) throws NotFoundException {
75         boolean calledInLibrary = isCalledInLibrary();
76         try {
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);
82         }
83     }
84
85     @Override
86     public void getValue(int id, TypedValue outValue, boolean resolveRefs) {
87         boolean calledInLibrary = isCalledInLibrary();
88         try {
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);
94         }
95     }
96
97     @Override
98     public void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs) {
99         boolean calledInLibrary = isCalledInLibrary();
100         try {
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);
106         }
107     }
108
109     @Override
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);
115         } else {
116             int id = super.getIdentifier(name, defType, defPackage);
117             return id != 0 ? id : mLibraryResource.getIdentifier(name, defType, defPackage);
118         }
119     }
120
121     @Override
122     public Drawable getDrawable(int id) {
123         boolean calledInLibrary = isCalledInLibrary();
124         try {
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);
130         }
131     }
132 }