Skip to content

Commit a0fda07

Browse files
committed
Refactored creating the instance of plugin manager.
1 parent a76beb3 commit a0fda07

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import android.content.Context;
2727
import android.content.IContentProvider;
2828
import android.content.Intent;
29+
import android.content.pm.PackageManager;
2930
import android.content.pm.ProviderInfo;
3031
import android.content.pm.ResolveInfo;
3132
import android.net.Uri;
@@ -77,15 +78,38 @@ public class PluginManager {
7778
public static PluginManager getInstance(Context base) {
7879
if (sInstance == null) {
7980
synchronized (PluginManager.class) {
80-
if (sInstance == null)
81-
sInstance = new PluginManager(base);
81+
if (sInstance == null) {
82+
sInstance = createInstance(base);
83+
}
8284
}
8385
}
8486

8587
return sInstance;
8688
}
89+
90+
private static PluginManager createInstance(Context context) {
91+
try {
92+
String factoryClass = context.getPackageManager()
93+
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
94+
.metaData.getString("VA_FACTORY");
95+
if (factoryClass == null) {
96+
return new PluginManager(context);
97+
}
98+
99+
PluginManager pluginManager = Reflector.on(factoryClass).method("create", Context.class).call(context);
100+
if (pluginManager != null) {
101+
Log.d(TAG, "Created a instance of " + pluginManager.getClass());
102+
return pluginManager;
103+
}
104+
105+
} catch (Exception e) {
106+
Log.w(TAG, "Created the instance error!", e);
107+
}
108+
109+
return new PluginManager(context);
110+
}
87111

88-
private PluginManager(Context context) {
112+
protected PluginManager(Context context) {
89113
Context app = context.getApplicationContext();
90114
if (app == null) {
91115
this.mContext = context;

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
android:label="@string/app_name"
3737
android:supportsRtl="true"
3838
android:theme="@style/HostTheme">
39+
<!-- test -->
40+
<meta-data android:name="VA_FACTORY" android:value="com.didi.virtualapk.DemoFactory" />
41+
3942
<activity android:name=".MainActivity">
4043
<intent-filter>
4144
<action android:name="android.intent.action.MAIN" />
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.didi.virtualapk;
2+
3+
import android.content.Context;
4+
import android.support.annotation.Keep;
5+
import android.util.Log;
6+
7+
/**
8+
* Created by qiaopu on 2018/7/5.
9+
*/
10+
@Keep
11+
public class DemoFactory {
12+
public static PluginManager create(Context context) {
13+
return new PluginManager(context) {
14+
@Override
15+
public void init() {
16+
super.init();
17+
Log.e(TAG, "example");
18+
}
19+
};
20+
}
21+
}

0 commit comments

Comments
 (0)