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