Commit a2409db8 authored by aldrin's avatar aldrin

feat(Application.java): Load Dreamland

在 attach 方法中加入dreamland 框架注入逻辑
parent ab72fb60
......@@ -29,7 +29,12 @@ import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.autofill.AutofillManager;
import java.io.File;
import dalvik.system.DexClassLoader;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.ArrayList;
/**
......@@ -346,12 +351,63 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
/**
* @hide
*/
private static AtomicBoolean hasInited = new AtomicBoolean(false);
private static final TAG = "LOAD_DREAMLAND";
@UnsupportedAppUsage
/* package */ final void attach(Context context) {
if (hasInited.compareAndSet(false, true)) {
Log.i(TAG, "attach: " + context.getPackageName());
PackageInfo runtimePkg = findDreamlandApkFromInstalled(context);
if (runtimePkg == null){
Log.w(TAG, "未找到 dreamland 运行时apk,请安装!");
}else{
String targetPackageSourceDir = runtimePkg.applicationInfo.publicSourceDir;
String nativeLibraryDir = runtimePkg.applicationInfo.nativeLibraryDir;
Log.i(TAG, "load apk path: " + targetPackageSourceDir);
Log.i(TAG, "load native path: " + nativeLibraryDir);
String dexDir = dexDir(context).getAbsolutePath();
try {
ClassLoader classLoader = new DexClassLoader(targetPackageSourceDir, dexDir, nativeLibraryDir, context.getClassLoader());
Class<?> runtimeClass = classLoader.loadClass("com.dreamland.runtime.DreamlandRuntime");
Method method = runtimeClass.getDeclaredMethod("callApplicationAttach", Context.class);
method.invoke(null, context);
Log.i(TAG, "加载 runtime 成功");
}catch(Exception e){
Log.e(TAG, "加载模块发生异常", e);
}
}
}
attachBaseContext(context);
mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
}
private static PackageInfo findDreamlandApkFromInstalled(Context context) {
PackageManager packageManager = context.getPackageManager();
if (packageManager == null){
Log.w(TAG, "packageManager 为 null");
return null;
}
for (PackageInfo pkg : packageManager.getInstalledPackages(PackageManager.GET_META_DATA)) {
if ("com.dreamland.runtime".equals(pkg.packageName))
return pkg;
}
return null;
}
private static File dexDir(Context context) {
return makeSureDirExist(new File(makeSureDirExist(context.getDir("dreamland_resource", Context.MODE_PRIVATE)), "dreamland_runtime_dex"));
}
private static File makeSureDirExist(File file) {
if (file.exists() && file.isFile()) {
return file;
}
file.mkdirs();
return file;
}
@UnsupportedAppUsage
/* package */ void dispatchActivityPreCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment