forked from rovo89/XposedBridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAndroidAppHelper.java
More file actions
154 lines (132 loc) · 5.14 KB
/
Copy pathAndroidAppHelper.java
File metadata and controls
154 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package android.app;
import static de.robv.android.xposed.XposedHelpers.findClass;
import static de.robv.android.xposed.XposedHelpers.findField;
import static de.robv.android.xposed.XposedHelpers.getObjectField;
import static de.robv.android.xposed.XposedHelpers.newInstance;
import java.lang.ref.WeakReference;
import java.util.Map;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.IBinder;
import android.view.Display;
import de.robv.android.xposed.XSharedPreferences;
import de.robv.android.xposed.XposedBridge;
/**
* Accessor for package level methods/fields in package android.app
*/
public class AndroidAppHelper {
private static Class<?> CLASS_RESOURCES_KEY;
private static boolean HAS_IS_THEMEABLE = false;
static {
CLASS_RESOURCES_KEY = (Build.VERSION.SDK_INT < 19) ?
findClass("android.app.ActivityThread$ResourcesKey", null)
: findClass("android.content.res.ResourcesKey", null);
try {
// T-Mobile theming engine (CyanogenMod etc.)
findField(CLASS_RESOURCES_KEY, "mIsThemeable");
HAS_IS_THEMEABLE = true;
} catch (NoSuchFieldError ignored) {
} catch (Throwable t) { XposedBridge.log(t); }
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map<Object, WeakReference<Resources>> getActiveResources(ActivityThread activityThread) {
if (Build.VERSION.SDK_INT <= 18) {
return (Map) getObjectField(activityThread, "mActiveResources");
} else {
Object resourcesManager = getObjectField(activityThread, "mResourcesManager");
return (Map) getObjectField(resourcesManager, "mActiveResources");
}
}
/* For SDK 15 & 16 */
private static Object createResourcesKey(String resDir, float scale, boolean isThemeable) {
try {
if (HAS_IS_THEMEABLE)
return newInstance(CLASS_RESOURCES_KEY, resDir, scale, isThemeable);
else
return newInstance(CLASS_RESOURCES_KEY, resDir, scale);
} catch (Throwable t) {
XposedBridge.log(t);
return null;
}
}
/* For SDK 17 & 18 */
private static Object createResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale, boolean isThemeable) {
try {
if (HAS_IS_THEMEABLE)
return newInstance(CLASS_RESOURCES_KEY, resDir, displayId, overrideConfiguration, scale, isThemeable);
else
return newInstance(CLASS_RESOURCES_KEY, resDir, displayId, overrideConfiguration, scale);
} catch (Throwable t) {
XposedBridge.log(t);
return null;
}
}
/* For SDK 19+ */
private static Object createResourcesKey(String resDir, int displayId, Configuration overrideConfiguration, float scale, IBinder token, boolean isThemeable) {
try {
if (HAS_IS_THEMEABLE)
return newInstance(CLASS_RESOURCES_KEY, resDir, displayId, overrideConfiguration, scale, isThemeable, token);
else
return newInstance(CLASS_RESOURCES_KEY, resDir, displayId, overrideConfiguration, scale, token);
} catch (Throwable t) {
XposedBridge.log(t);
return null;
}
}
public static void addActiveResource(String resDir, float scale, boolean isThemeable, Resources resources) {
ActivityThread thread = ActivityThread.currentActivityThread();
if (thread == null)
return;
Object resourcesKey;
if (Build.VERSION.SDK_INT <= 16)
resourcesKey = createResourcesKey(resDir, scale, isThemeable);
else if (Build.VERSION.SDK_INT <= 18)
resourcesKey = createResourcesKey(resDir, Display.DEFAULT_DISPLAY, null, scale, isThemeable);
else
resourcesKey = createResourcesKey(resDir, Display.DEFAULT_DISPLAY, null, scale, null, isThemeable);
if (resourcesKey != null)
getActiveResources(thread).put(resourcesKey, new WeakReference<Resources>(resources));
}
public static String currentProcessName() {
String processName = ActivityThread.currentPackageName();
if (processName == null)
return "android";
return processName;
}
public static ApplicationInfo currentApplicationInfo() {
ActivityThread am = ActivityThread.currentActivityThread();
if (am == null)
return null;
Object boundApplication = getObjectField(am, "mBoundApplication");
if (boundApplication == null)
return null;
return (ApplicationInfo) getObjectField(boundApplication, "appInfo");
}
public static String currentPackageName() {
ApplicationInfo ai = currentApplicationInfo();
return (ai != null) ? ai.packageName : "android";
}
public static Application currentApplication() {
return ActivityThread.currentApplication();
}
/** use class {@link XSharedPreferences} instead */
@Deprecated
public static SharedPreferences getSharedPreferencesForPackage(String packageName, String prefFileName, int mode) {
return new XSharedPreferences(packageName, prefFileName);
}
/** use class {@link XSharedPreferences} instead */
@Deprecated
public static SharedPreferences getDefaultSharedPreferencesForPackage(String packageName) {
return new XSharedPreferences(packageName);
}
/** use {@link XSharedPreferences#reload()}instead */
@Deprecated
public static void reloadSharedPreferencesIfNeeded(SharedPreferences pref) {
if (pref instanceof XSharedPreferences) {
((XSharedPreferences) pref).reload();
}
}
}