Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core / src / org / xwalk / core / XWalkInternalResources.java
1 // Copyright (c) 2014 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.util.Log;
9
10 import java.lang.reflect.Field;
11
12 public class XWalkInternalResources {
13     private static final String TAG = "XWalkInternalResources";
14     
15     private static boolean loaded = false;
16     private final static String INTERNAL_RESOURCE_CLASSES[] = {
17         "org.chromium.content.R",
18         "org.chromium.ui.R"
19     };
20     private final static String GENERATED_RESOURCE_CLASS = "org.xwalk.core.R";
21
22     // Doing org.chromium.content.R.<class>.<name> = org.xwalk.core.R.<class>.<name>
23     // Use reflection to iterate over the target class is to avoid hardcode.
24     private static void doResetIds(Context context) {
25         ClassLoader classLoader = context.getClassLoader();
26         for (String resourceClass : INTERNAL_RESOURCE_CLASSES) {
27             try {
28                 Class<?> internalResource = classLoader.loadClass(resourceClass);
29                 Class<?>[] innerClazzs = internalResource.getClasses();
30                 for (Class<?> innerClazz : innerClazzs) {
31                     Class<?> generatedInnerClazz;
32                     String generatedInnerClassName = innerClazz.getName().replace(
33                             resourceClass, GENERATED_RESOURCE_CLASS);
34                     try {
35                         generatedInnerClazz = classLoader.loadClass(generatedInnerClassName);
36                     } catch (ClassNotFoundException e) {
37                         Log.w(TAG, generatedInnerClassName + "is not found.");
38                         continue;
39                     }
40                     Field[] fields = innerClazz.getFields();
41                     for (Field field : fields) {
42                         // It's final means we are probably not used as library project.
43                         if (!field.isAccessible()) continue;
44                         try {
45                             int value = generatedInnerClazz.getField(field.getName()).getInt(null);
46                             field.setInt(null, value);
47                         } catch (IllegalAccessException e) {
48                             Log.w(TAG, generatedInnerClazz.getName() + "." +
49                                     field.getName() + " is not accessable.");
50                         } catch (IllegalArgumentException e) {
51                             Log.w(TAG, generatedInnerClazz.getName() + "." + 
52                                     field.getName() + " is not int.");
53                         } catch (NoSuchFieldException e) {
54                             Log.w(TAG, generatedInnerClazz.getName() + "." + 
55                                     field.getName() + " is not found.");
56                         }
57                     }
58                 }
59             } catch (ClassNotFoundException e) {
60                 Log.w(TAG, resourceClass + "is not found.");
61             }
62         }
63     }
64
65     static void resetIds(Context context) {
66         if (!loaded) {
67             doResetIds(context);
68             loaded = true;
69         }
70     }
71 }