提交 a0fda07c 编写于 作者: S superq_sky

Refactored creating the instance of plugin manager.

上级 a76beb3b
......@@ -26,6 +26,7 @@ import android.content.ComponentName;
import android.content.Context;
import android.content.IContentProvider;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ProviderInfo;
import android.content.pm.ResolveInfo;
import android.net.Uri;
......@@ -77,15 +78,38 @@ public class PluginManager {
public static PluginManager getInstance(Context base) {
if (sInstance == null) {
synchronized (PluginManager.class) {
if (sInstance == null)
sInstance = new PluginManager(base);
if (sInstance == null) {
sInstance = createInstance(base);
}
}
}
return sInstance;
}
private static PluginManager createInstance(Context context) {
try {
String factoryClass = context.getPackageManager()
.getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA)
.metaData.getString("VA_FACTORY");
if (factoryClass == null) {
return new PluginManager(context);
}
PluginManager pluginManager = Reflector.on(factoryClass).method("create", Context.class).call(context);
if (pluginManager != null) {
Log.d(TAG, "Created a instance of " + pluginManager.getClass());
return pluginManager;
}
} catch (Exception e) {
Log.w(TAG, "Created the instance error!", e);
}
return new PluginManager(context);
}
private PluginManager(Context context) {
protected PluginManager(Context context) {
Context app = context.getApplicationContext();
if (app == null) {
this.mContext = context;
......
......@@ -36,6 +36,9 @@
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/HostTheme">
<!-- test -->
<meta-data android:name="VA_FACTORY" android:value="com.didi.virtualapk.DemoFactory" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
......
package com.didi.virtualapk;
import android.content.Context;
import android.support.annotation.Keep;
import android.util.Log;
/**
* Created by qiaopu on 2018/7/5.
*/
@Keep
public class DemoFactory {
public static PluginManager create(Context context) {
return new PluginManager(context) {
@Override
public void init() {
super.init();
Log.e(TAG, "example");
}
};
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册