Upstream version 9.38.198.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.res.Resources;
8 import android.content.res.TypedArray;
9 import android.content.res.XmlResourceParser;
10 import android.util.TypedValue;
11
12 /**
13  * XWalkMixedResources is used to combine the resources
14  * from two different packages.
15  *
16  * Chromium uses resources through:
17  *   R.layout; R.id; R.string; R.dimen; R.drawable;
18  *   R.attr; R.style; R.menu ; R.color
19  * R.layout and R.menu is covered by Resources.getLayout()
20  * R.string is covered by Resources.getText()
21  * R.dimen, R.drawable and R.color is covered by getValue()
22  *
23  * For R.id, if it's used like findViewById(R.id.xxx), R.id.xxx
24  * is const at compile time within library context. It works
25  * if the view in hierachy has the same id, which needs inflate
26  * with layout resource from library context. Layout is covered
27  * by getLayout(), so R.id is OK.
28  *
29  * TODO(wang16):
30  * For R.attr and R.style, I have no confidence that it's covered.
31  * But the only place use this R.attr and R.style is "select" tag
32  * which is verified working well with this MixedResources.
33  */
34 public class XWalkMixedResources extends Resources {
35
36     private Resources mExtend;
37
38     XWalkMixedResources(Resources base, Resources extend) {
39         super(base.getAssets(), base.getDisplayMetrics(),
40                 base.getConfiguration());
41         mExtend = extend;
42     }
43
44     @Override
45     public CharSequence getText(int id) throws NotFoundException {
46         try {
47             return mExtend.getText(id);
48         } catch (NotFoundException e) {
49             return super.getText(id);
50         }
51     }
52
53     @Override
54     public XmlResourceParser getLayout(int id) throws NotFoundException {
55         try {
56             return mExtend.getLayout(id);
57         } catch (NotFoundException e) {
58             return super.getLayout(id);
59         }
60     }
61
62     @Override
63     public void getValue(int id, TypedValue outValue, boolean resolveRefs) {
64         try {
65             mExtend.getValue(id, outValue, resolveRefs);
66         } catch (NotFoundException e) {
67             super.getValue(id, outValue, resolveRefs);
68         }
69     }
70
71     @Override
72     public void getValueForDensity(int id, int density, TypedValue outValue, boolean resolveRefs) {
73         try {
74             mExtend.getValueForDensity(id, density, outValue, resolveRefs);
75         } catch (NotFoundException e) {
76             super.getValueForDensity(id, density, outValue, resolveRefs);
77         }
78     }
79
80     @Override
81     public int getIdentifier(String name, String defType, String defPackage) {
82         int id = mExtend.getIdentifier(name, defType, defPackage);
83         return id != 0 ? id : super.getIdentifier(name, defType, defPackage);
84     }
85 }