Commit 02a91ec6 authored by aldrin's avatar aldrin

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

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