Skip to content

Commit 4b98b1d

Browse files
committed
Optimized some code extensibility.
1 parent a0fda07 commit 4b98b1d

18 files changed

Lines changed: 302 additions & 156 deletions

AndroidStub/src/main/java/android/content/res/Resources.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ public final AssetManager getAssets() {
1616
throw new RuntimeException("Stub!");
1717
}
1818

19+
public int getColor(int id) throws NotFoundException {
20+
throw new RuntimeException("Stub!");
21+
}
22+
1923
public Configuration getConfiguration() {
2024
throw new RuntimeException("Stub!");
2125
}
@@ -28,6 +32,10 @@ public Drawable getDrawable(int id) throws NotFoundException {
2832
throw new RuntimeException("Stub!");
2933
}
3034

35+
public String getString(int id) throws NotFoundException {
36+
throw new RuntimeException("Stub!");
37+
}
38+
3139
public CharSequence getText(int id) throws NotFoundException {
3240
throw new RuntimeException("Stub!");
3341
}
@@ -44,6 +52,10 @@ public final Theme newTheme() {
4452
throw new RuntimeException("Stub!");
4553
}
4654

55+
public void updateConfiguration(Configuration config, DisplayMetrics metrics) {
56+
throw new RuntimeException("Stub!");
57+
}
58+
4759
public final class Theme {
4860

4961
public void applyStyle(int resId, boolean force) {

CoreLibrary/src/main/java/android/content/ContentResolverWrapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ public void unstableProviderDied(IContentProvider icp) {
5252
@TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
5353
@Override
5454
public void appNotRespondingViaProvider(IContentProvider icp) {
55-
mBase.appNotRespondingViaProvider(icp);
55+
// dark greylist in Android P
56+
// mBase.appNotRespondingViaProvider(icp);
5657
}
5758

5859
}

CoreLibrary/src/main/java/com/didi/virtualapk/PluginManager.java

Lines changed: 63 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,18 @@
3131
import android.content.pm.ResolveInfo;
3232
import android.net.Uri;
3333
import android.os.Build;
34+
import android.os.Bundle;
3435
import android.os.Handler;
3536
import android.util.Log;
3637
import android.util.Singleton;
3738

3839
import com.didi.virtualapk.delegate.ActivityManagerProxy;
3940
import com.didi.virtualapk.delegate.IContentProviderProxy;
41+
import com.didi.virtualapk.delegate.RemoteContentProvider;
4042
import com.didi.virtualapk.internal.ComponentsHandler;
4143
import com.didi.virtualapk.internal.LoadedPlugin;
42-
import com.didi.virtualapk.internal.PluginContentResolver;
4344
import com.didi.virtualapk.internal.VAInstrumentation;
44-
import com.didi.virtualapk.utils.PluginUtil;
45+
import com.didi.virtualapk.internal.utils.PluginUtil;
4546
import com.didi.virtualapk.utils.Reflector;
4647
import com.didi.virtualapk.utils.RunUtil;
4748

@@ -66,14 +67,15 @@ public class PluginManager {
6667
private static volatile PluginManager sInstance = null;
6768

6869
// Context of host app
69-
private Context mContext;
70-
private ComponentsHandler mComponentsHandler;
71-
private Map<String, LoadedPlugin> mPlugins = new ConcurrentHashMap<>();
72-
private final List<Callback> mCallbacks = new ArrayList<>();
70+
protected final Context mContext;
71+
protected final Application mApplication;
72+
protected ComponentsHandler mComponentsHandler;
73+
protected final Map<String, LoadedPlugin> mPlugins = new ConcurrentHashMap<>();
74+
protected final List<Callback> mCallbacks = new ArrayList<>();
7375

74-
private VAInstrumentation mInstrumentation; // Hooked instrumentation
75-
private IActivityManager mActivityManager; // Hooked IActivityManager binder
76-
private IContentProvider mIContentProvider; // Hooked IContentProvider binder
76+
protected VAInstrumentation mInstrumentation; // Hooked instrumentation
77+
protected IActivityManager mActivityManager; // Hooked IActivityManager binder
78+
protected IContentProvider mIContentProvider; // Hooked IContentProvider binder
7779

7880
public static PluginManager getInstance(Context base) {
7981
if (sInstance == null) {
@@ -89,14 +91,22 @@ public static PluginManager getInstance(Context base) {
8991

9092
private static PluginManager createInstance(Context context) {
9193
try {
92-
String factoryClass = context.getPackageManager()
93-
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
94-
.metaData.getString("VA_FACTORY");
94+
Bundle metaData = context.getPackageManager()
95+
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
96+
.metaData;
97+
98+
if (metaData == null) {
99+
return new PluginManager(context);
100+
}
101+
102+
String factoryClass = metaData.getString("VA_FACTORY");
103+
95104
if (factoryClass == null) {
96105
return new PluginManager(context);
97106
}
98107

99108
PluginManager pluginManager = Reflector.on(factoryClass).method("create", Context.class).call(context);
109+
100110
if (pluginManager != null) {
101111
Log.d(TAG, "Created a instance of " + pluginManager.getClass());
102112
return pluginManager;
@@ -110,17 +120,25 @@ private static PluginManager createInstance(Context context) {
110120
}
111121

112122
protected PluginManager(Context context) {
113-
Context app = context.getApplicationContext();
114-
if (app == null) {
115-
this.mContext = context;
123+
if (context instanceof Application) {
124+
this.mApplication = (Application) context;
125+
this.mContext = mApplication.getBaseContext();
116126
} else {
117-
this.mContext = ((Application)app).getBaseContext();
127+
final Context app = context.getApplicationContext();
128+
if (app == null) {
129+
this.mContext = context;
130+
this.mApplication = ActivityThread.currentApplication();
131+
} else {
132+
this.mApplication = (Application) app;
133+
this.mContext = mApplication.getBaseContext();
134+
}
118135
}
119-
prepare();
136+
137+
mComponentsHandler = createComponentsHandler();
138+
hookCurrentProcess();
120139
}
121140

122-
private void prepare() {
123-
mComponentsHandler = createComponentsHandler();
141+
protected void hookCurrentProcess() {
124142
hookInstrumentationAndHandler();
125143
hookSystemServices();
126144
hookDataBindingUtil();
@@ -138,6 +156,10 @@ public void run() {
138156
protected void doInWorkThread() {
139157
}
140158

159+
public Application getHostApplication() {
160+
return this.mApplication;
161+
}
162+
141163
protected ComponentsHandler createComponentsHandler() {
142164
return new ComponentsHandler(this);
143165
}
@@ -208,10 +230,10 @@ protected void hookInstrumentationAndHandler() {
208230
try {
209231
ActivityThread activityThread = ActivityThread.currentActivityThread();
210232
Instrumentation baseInstrumentation = activityThread.getInstrumentation();
211-
if (baseInstrumentation.getClass().getName().contains("lbe")) {
212-
// reject executing in paralell space, for example, lbe.
213-
System.exit(0);
214-
}
233+
// if (baseInstrumentation.getClass().getName().contains("lbe")) {
234+
// // reject executing in paralell space, for example, lbe.
235+
// System.exit(0);
236+
// }
215237

216238
final VAInstrumentation instrumentation = createInstrumentation(baseInstrumentation);
217239

@@ -225,14 +247,14 @@ protected void hookInstrumentationAndHandler() {
225247
}
226248

227249
protected void hookIContentProviderAsNeeded() {
228-
Uri uri = Uri.parse(PluginContentResolver.getUri(mContext));
250+
Uri uri = Uri.parse(RemoteContentProvider.getUri(mContext));
229251
mContext.getContentResolver().call(uri, "wakeup", null, null);
230252
try {
231253
Field authority = null;
232-
Field mProvider = null;
254+
Field provider = null;
233255
ActivityThread activityThread = ActivityThread.currentActivityThread();
234-
Map mProviderMap = Reflector.with(activityThread).field("mProviderMap").get();
235-
Iterator iter = mProviderMap.entrySet().iterator();
256+
Map providerMap = Reflector.with(activityThread).field("mProviderMap").get();
257+
Iterator iter = providerMap.entrySet().iterator();
236258
while (iter.hasNext()) {
237259
Map.Entry entry = (Map.Entry) iter.next();
238260
Object key = entry.getKey();
@@ -247,12 +269,12 @@ protected void hookIContentProviderAsNeeded() {
247269
}
248270
auth = (String) authority.get(key);
249271
}
250-
if (auth.equals(PluginContentResolver.getAuthority(mContext))) {
251-
if (mProvider == null) {
252-
mProvider = val.getClass().getDeclaredField("mProvider");
253-
mProvider.setAccessible(true);
272+
if (auth.equals(RemoteContentProvider.getAuthority(mContext))) {
273+
if (provider == null) {
274+
provider = val.getClass().getDeclaredField("mProvider");
275+
provider.setAccessible(true);
254276
}
255-
IContentProvider rawProvider = (IContentProvider) mProvider.get(val);
277+
IContentProvider rawProvider = (IContentProvider) provider.get(val);
256278
IContentProvider proxy = IContentProviderProxy.newInstance(mContext, rawProvider);
257279
mIContentProvider = proxy;
258280
Log.d(TAG, "hookIContentProvider succeed : " + mIContentProvider);
@@ -281,17 +303,16 @@ public void loadPlugin(File apk) throws Exception {
281303
}
282304

283305
LoadedPlugin plugin = createLoadedPlugin(apk);
284-
if (null != plugin) {
285-
this.mPlugins.put(plugin.getPackageName(), plugin);
286-
synchronized (mCallbacks) {
287-
for (int i = 0; i < mCallbacks.size(); i++) {
288-
mCallbacks.get(i).onAddedLoadedPlugin(plugin);
289-
}
306+
307+
if (null == plugin) {
308+
throw new RuntimeException("Can't load plugin which is invalid: " + apk.getAbsolutePath());
309+
}
310+
311+
this.mPlugins.put(plugin.getPackageName(), plugin);
312+
synchronized (mCallbacks) {
313+
for (int i = 0; i < mCallbacks.size(); i++) {
314+
mCallbacks.get(i).onAddedLoadedPlugin(plugin);
290315
}
291-
// try to invoke plugin's application
292-
plugin.invokeApplication();
293-
} else {
294-
throw new RuntimeException("Can't load plugin which is invalid: " + apk.getAbsolutePath());
295316
}
296317
}
297318

CoreLibrary/src/main/java/com/didi/virtualapk/delegate/ActivityManagerProxy.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@
3333
import android.util.Log;
3434

3535
import com.didi.virtualapk.PluginManager;
36-
import com.didi.virtualapk.utils.PluginUtil;
36+
import com.didi.virtualapk.internal.utils.PluginUtil;
3737

3838
import java.lang.reflect.InvocationHandler;
3939
import java.lang.reflect.Method;
40-
import java.lang.reflect.Proxy;
4140

4241
/**
4342
* @author johnsonlee

CoreLibrary/src/main/java/com/didi/virtualapk/delegate/IContentProviderProxy.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import java.lang.reflect.Proxy;
3434
import java.util.Arrays;
3535

36-
import static com.didi.virtualapk.delegate.RemoteContentProvider.KEY_WRAPPER_URI;
37-
3836
/**
3937
* Created by renyugang on 16/12/8.
4038
*/
@@ -84,7 +82,7 @@ private void wrapperUri(Method method, Object[] args) {
8482
if (method.getName().equals("call")) {
8583
bundleInCallMethod = getBundleParameter(args);
8684
if (bundleInCallMethod != null) {
87-
String uriString = bundleInCallMethod.getString(KEY_WRAPPER_URI);
85+
String uriString = bundleInCallMethod.getString(RemoteContentProvider.KEY_WRAPPER_URI);
8886
if (uriString != null) {
8987
uri = Uri.parse(uriString);
9088
}
@@ -100,14 +98,9 @@ private void wrapperUri(Method method, Object[] args) {
10098
if (info != null) {
10199
String pkg = info.packageName;
102100
LoadedPlugin plugin = pluginManager.getLoadedPlugin(pkg);
103-
String pluginUri = Uri.encode(uri.toString());
104-
StringBuilder builder = new StringBuilder(PluginContentResolver.getUri(mContext));
105-
builder.append("/?plugin=" + plugin.getLocation());
106-
builder.append("&pkg=" + pkg);
107-
builder.append("&uri=" + pluginUri);
108-
Uri wrapperUri = Uri.parse(builder.toString());
101+
Uri wrapperUri = PluginContentResolver.wrapperUri(plugin, uri);
109102
if (method.getName().equals("call")) {
110-
bundleInCallMethod.putString(KEY_WRAPPER_URI, wrapperUri.toString());
103+
bundleInCallMethod.putString(RemoteContentProvider.KEY_WRAPPER_URI, wrapperUri.toString());
111104
} else {
112105
args[index] = wrapperUri;
113106
}

CoreLibrary/src/main/java/com/didi/virtualapk/delegate/LocalService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import com.didi.virtualapk.PluginManager;
3434
import com.didi.virtualapk.internal.LoadedPlugin;
35-
import com.didi.virtualapk.utils.PluginUtil;
35+
import com.didi.virtualapk.internal.utils.PluginUtil;
3636
import com.didi.virtualapk.utils.Reflector;
3737

3838
import java.lang.reflect.Method;

CoreLibrary/src/main/java/com/didi/virtualapk/delegate/RemoteContentProvider.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,12 @@ public Bundle call(String method, String arg, Bundle extras) {
209209
return null;
210210
}
211211

212+
public static String getAuthority(Context context) {
213+
return context.getPackageName() + ".VirtualAPK.Provider";
214+
}
215+
216+
public static String getUri(Context context) {
217+
return "content://" + getAuthority(context);
218+
}
219+
212220
}

0 commit comments

Comments
 (0)