Commit 02a91ec6 authored by aldrin's avatar aldrin

优化注入逻辑,修复应用在32模式时下So不匹配问题

parent 71abb014
......@@ -29,14 +29,19 @@ 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;
import android.content.pm.ApplicationInfo;
/**
* Base class for maintaining global application state. You can provide your own
* implementation by creating a subclass and specifying the fully-qualified name
......@@ -65,7 +70,9 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
@UnsupportedAppUsage
private ArrayList<OnProvideAssistDataListener> mAssistCallbacks = null;
/** @hide */
/**
* @hide
*/
@UnsupportedAppUsage
public LoadedApk mLoadedApk;
......@@ -76,7 +83,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
* {@link Activity#onCreate}.
*/
default void onActivityPreCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
}
/**
......@@ -89,7 +96,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
* {@link Activity#onCreate}.
*/
default void onActivityPostCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
}
/**
......@@ -173,7 +180,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
* called before {@link Activity#onSaveInstanceState}.
*/
default void onActivityPreSaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
@NonNull Bundle outState) {
}
/**
......@@ -187,7 +194,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
* called after{@link Activity#onSaveInstanceState}.
*/
default void onActivityPostSaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
@NonNull Bundle outState) {
}
/**
......@@ -266,8 +273,8 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
public void onConfigurationChanged(@NonNull Configuration newConfig) {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ComponentCallbacks)callbacks[i]).onConfigurationChanged(newConfig);
for (int i = 0; i < callbacks.length; i++) {
((ComponentCallbacks) callbacks[i]).onConfigurationChanged(newConfig);
}
}
}
......@@ -276,8 +283,8 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
public void onLowMemory() {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ComponentCallbacks)callbacks[i]).onLowMemory();
for (int i = 0; i < callbacks.length; i++) {
((ComponentCallbacks) callbacks[i]).onLowMemory();
}
}
}
......@@ -286,10 +293,10 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
public void onTrimMemory(int level) {
Object[] callbacks = collectComponentCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
for (int i = 0; i < callbacks.length; i++) {
Object c = callbacks[i];
if (c instanceof ComponentCallbacks2) {
((ComponentCallbacks2)c).onTrimMemory(level);
((ComponentCallbacks2) c).onTrimMemory(level);
}
}
}
......@@ -352,48 +359,63 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
* @hide
*/
private static AtomicBoolean hasInited = new AtomicBoolean(false);
private static AtomicBoolean hasInjected = new AtomicBoolean(false);
private static final String DREAMLAND_TAG = "LOAD_DREAMLAND";
@UnsupportedAppUsage
/* package */ final void attach(Context context) {
if (hasInited.compareAndSet(false, true)) {
Log.i(DREAMLAND_TAG, "attach: " + context.getPackageName());
PackageInfo runtimePkg = findDreamlandApkFromInstalled(context);
if (runtimePkg == null){
Log.w(DREAMLAND_TAG, "未找到 dreamland 运行时apk,请安装!");
}else{
String targetPackageSourceDir = runtimePkg.applicationInfo.publicSourceDir;
String nativeLibraryDir = runtimePkg.applicationInfo.nativeLibraryDir;
Log.i(DREAMLAND_TAG, "load apk path: " + targetPackageSourceDir);
Log.i(DREAMLAND_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(DREAMLAND_TAG, "加载 runtime 成功");
}catch(Exception e){
Log.e(DREAMLAND_TAG, "加载模块发生异常", e);
}
if (hasInjected.compareAndSet(false, true)) {
Log.i(DREAMLAND_TAG, "attach: " + context.getApplicationInfo().processName);
String packageName = context.getPackageName();
if (packageName.contains("dreamland")) {
// dreamland runtime 和 dreamland manager 不进行注入
Log.i(DREAMLAND_TAG, "Don't inject into: " + packageName)
} else {
inject(context, "com.dreamland.runtime",
"com.dreamland.runtime.DreamlandRuntime", "callApplicationAttach");
}
}
}
attachBaseContext(context);
mLoadedApk = ContextImpl.getImpl(context).mPackageInfo;
}
private static PackageInfo findDreamlandApkFromInstalled(Context context) {
private static void inject(Context context, String packageName, String className, String methodName) {
ApplicationInfo runtimeInfo = getApplicationInfo(context, packageName);
if (runtimeInfo == null) {
Log.w(DREAMLAND_TAG, "Don't find inject runtime or packageManager is null: " + packageName);
} else {
String targetPackageSourceDir = runtimeInfo.publicSourceDir;
String nativeLibraryDir = runtimeInfo.nativeLibraryDir
if (!android.os.Process.is64Bit()) {
nativeLibraryDir = nativeLibraryDir.substring(0, nativeLibraryDir.lastIndexOf("/") + 1) + "arm";
}
Log.i(DREAMLAND_TAG, "inject apk path: " + targetPackageSourceDir);
Log.i(DREAMLAND_TAG, "inject native path: " + nativeLibraryDir);
String dexDir = dexDir(context).getAbsolutePath();
try {
ClassLoader classLoader = new DexClassLoader(targetPackageSourceDir, dexDir, nativeLibraryDir, context.getClassLoader());
Class<?> runtimeClass = classLoader.loadClass(className);
Method method = runtimeClass.getDeclaredMethod(methodName, Context.class);
method.invoke(null, context);
Log.i(DREAMLAND_TAG, "inject sucessed -> " + packageName);
} catch (Exception e) {
Log.e(DREAMLAND_TAG, "inject failed -> " + packageName, e);
}
}
}
private static ApplicationInfo getApplicationInfo(Context context, String packageName) {
PackageManager packageManager = context.getPackageManager();
if (packageManager == null){
Log.w(DREAMLAND_TAG, "packageManager null");
if (packageManager == null) {
Log.w(DREAMLAND_TAG, "packageManager is null");
return null;
}
for (PackageInfo pkg : packageManager.getInstalledPackages(PackageManager.GET_META_DATA)) {
if ("com.dreamland.runtime".equals(pkg.packageName))
return pkg;
try {
return context.getPackageManager().getApplicationInfo(packageName, PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
Log.w(DREAMLAND_TAG, "Don't find: " + packageName);
return null;
}
return null;
}
private static File dexDir(Context context) {
......@@ -410,7 +432,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
@UnsupportedAppUsage
/* package */ void dispatchActivityPreCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
......@@ -421,12 +443,12 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
/* package */ void dispatchActivityCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityCreated(activity,
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityCreated(activity,
savedInstanceState);
}
}
......@@ -434,7 +456,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
@UnsupportedAppUsage
/* package */ void dispatchActivityPostCreated(@NonNull Activity activity,
@Nullable Bundle savedInstanceState) {
@Nullable Bundle savedInstanceState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
......@@ -455,11 +477,11 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityStarted(@NonNull Activity activity) {
/* package */ void dispatchActivityStarted(@NonNull Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityStarted(activity);
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityStarted(activity);
}
}
}
......@@ -485,11 +507,11 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityResumed(@NonNull Activity activity) {
/* package */ void dispatchActivityResumed(@NonNull Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityResumed(activity);
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityResumed(activity);
}
}
}
......@@ -515,11 +537,11 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityPaused(@NonNull Activity activity) {
/* package */ void dispatchActivityPaused(@NonNull Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity);
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityPaused(activity);
}
}
}
......@@ -545,11 +567,11 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityStopped(@NonNull Activity activity) {
/* package */ void dispatchActivityStopped(@NonNull Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityStopped(activity);
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityStopped(activity);
}
}
}
......@@ -566,7 +588,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
@UnsupportedAppUsage
/* package */ void dispatchActivityPreSaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
@NonNull Bundle outState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
......@@ -577,12 +599,12 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivitySaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
/* package */ void dispatchActivitySaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivitySaveInstanceState(activity,
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivitySaveInstanceState(activity,
outState);
}
}
......@@ -590,7 +612,7 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
@UnsupportedAppUsage
/* package */ void dispatchActivityPostSaveInstanceState(@NonNull Activity activity,
@NonNull Bundle outState) {
@NonNull Bundle outState) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i = 0; i < callbacks.length; i++) {
......@@ -611,11 +633,11 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
}
@UnsupportedAppUsage
/* package */ void dispatchActivityDestroyed(@NonNull Activity activity) {
/* package */ void dispatchActivityDestroyed(@NonNull Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
for (int i = 0; i < callbacks.length; i++) {
((ActivityLifecycleCallbacks) callbacks[i]).onActivityDestroyed(activity);
}
}
}
......@@ -660,13 +682,15 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
callbacks = mAssistCallbacks.toArray();
}
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((OnProvideAssistDataListener)callbacks[i]).onProvideAssistData(activity, data);
for (int i = 0; i < callbacks.length; i++) {
((OnProvideAssistDataListener) callbacks[i]).onProvideAssistData(activity, data);
}
}
}
/** @hide */
/**
* @hide
*/
@Override
public AutofillManager.AutofillClient getAutofillClient() {
final AutofillManager.AutofillClient client = super.getAutofillClient();
......
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