Commit 064560ac authored by swift_gan's avatar swift_gan Committed by swift_gan

[XposedCompat]fix

parent a9b826e8
package com.swift.sandhook.xposedcompat_new; package com.swift.sandhook.xposedcompat_new;
import android.app.Application;
import android.content.Context; import android.content.Context;
import com.swift.sandhook.HookLog; import com.swift.sandhook.HookLog;
...@@ -7,6 +8,10 @@ import com.swift.sandhook.SandHook; ...@@ -7,6 +8,10 @@ import com.swift.sandhook.SandHook;
import com.swift.sandhook.wrapper.HookErrorException; import com.swift.sandhook.wrapper.HookErrorException;
import com.swift.sandhook.wrapper.HookWrapper; import com.swift.sandhook.wrapper.HookWrapper;
import com.swift.sandhook.wrapper.StubMethodsFactory; import com.swift.sandhook.wrapper.StubMethodsFactory;
import com.swift.sandhook.xposedcompat_new.utils.ApplicationUtils;
import com.swift.sandhook.xposedcompat_new.utils.ClassUtils;
import com.swift.sandhook.xposedcompat_new.utils.ComposeClassLoader;
import com.swift.sandhook.xposedcompat_new.utils.ProcessUtils;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Member; import java.lang.reflect.Member;
...@@ -14,8 +19,11 @@ import java.lang.reflect.Method; ...@@ -14,8 +19,11 @@ import java.lang.reflect.Method;
import java.lang.reflect.Modifier; import java.lang.reflect.Modifier;
import java.util.Arrays; import java.util.Arrays;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook; import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge; import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedInit;
import de.robv.android.xposed.callbacks.XC_LoadPackage;
/** /**
* @author Swift Gan * @author Swift Gan
...@@ -30,8 +38,9 @@ public class XposedCompat { ...@@ -30,8 +38,9 @@ public class XposedCompat {
public static String processName; public static String processName;
public static boolean isFirstApplication; public static boolean isFirstApplication;
private static volatile int curSlot = 0; private static ClassLoader sandHookXposedClassLoader;
private static volatile int curSlot = 0;
public static volatile HookInfo[] hookInfos = new HookInfo[100]; public static volatile HookInfo[] hookInfos = new HookInfo[100];
static { static {
...@@ -166,4 +175,47 @@ public class XposedCompat { ...@@ -166,4 +175,47 @@ public class XposedCompat {
} }
} }
public static void loadModule(String modulePath, String moduleOdexDir, String moduleSoPath,ClassLoader classLoader) {
XposedInit.loadModule(modulePath, moduleOdexDir, moduleSoPath, classLoader);
}
public static void addXposedModuleCallback(IXposedHookLoadPackage module) {
XposedBridge.hookLoadPackage(new IXposedHookLoadPackage.Wrapper(module));
}
public static void callXposedModuleInit() throws Throwable {
//prepare LoadPackageParam
XC_LoadPackage.LoadPackageParam packageParam = new XC_LoadPackage.LoadPackageParam(XposedBridge.sLoadedPackageCallbacks);
Application application = ApplicationUtils.currentApplication();
if (application != null) {
if (packageParam.packageName == null) {
packageParam.packageName = application.getPackageName();
}
if (packageParam.processName == null) {
packageParam.processName = ProcessUtils.getProcessName(application);
}
if (packageParam.classLoader == null) {
packageParam.classLoader = application.getClassLoader();
}
if (packageParam.appInfo == null) {
packageParam.appInfo = application.getApplicationInfo();
}
}
XC_LoadPackage.callAll(packageParam);
}
public static ClassLoader getSandHookXposedClassLoader(ClassLoader appOriginClassLoader, ClassLoader sandBoxHostClassLoader) {
if (sandHookXposedClassLoader != null) {
return sandHookXposedClassLoader;
} else {
sandHookXposedClassLoader = new ComposeClassLoader(sandBoxHostClassLoader, appOriginClassLoader);
return sandHookXposedClassLoader;
}
}
} }
package com.swift.sandhook.xposedcompat_new.utils;
import android.app.Application;
import java.lang.reflect.Method;
public class ApplicationUtils {
private static Class classActivityThread;
private static Method currentApplicationMethod;
static Application application;
public static Application currentApplication() {
if (application != null)
return application;
if (currentApplicationMethod == null) {
try {
classActivityThread = Class.forName("android.app.ActivityThread");
currentApplicationMethod = classActivityThread.getDeclaredMethod("currentApplication");
} catch (Exception e) {
e.printStackTrace();
}
}
if (currentApplicationMethod == null)
return null;
try {
application = (Application) currentApplicationMethod.invoke(null);
} catch (Exception e) {
}
return application;
}
}
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package com.swift.sandhook.xposedcompat_new; package com.swift.sandhook.xposedcompat_new.utils;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
......
package com.swift.sandhook.xposedcompat_new.utils;
public class ComposeClassLoader extends ClassLoader {
private final ClassLoader mAppClassLoader;
public ComposeClassLoader(ClassLoader parent, ClassLoader appClassLoader) {
super(parent);
mAppClassLoader = appClassLoader;
}
@Override
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
Class clazz = null;
try {
clazz = mAppClassLoader.loadClass(name);
} catch (ClassNotFoundException e) {
// IGNORE.
}
if (clazz == null) {
clazz = super.loadClass(name, resolve);
}
if (clazz == null) {
throw new ClassNotFoundException();
}
return clazz;
}
}
package com.swift.sandhook.xposedcompat_new.utils;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;
/**
* Created by swift_gan on 2017/11/23.
*/
public class ProcessUtils {
private static volatile String processName = null;
public static String getProcessName(Context context) {
if (!TextUtils.isEmpty(processName))
return processName;
processName = doGetProcessName(context);
return processName;
}
private static String doGetProcessName(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
if (runningApps == null) {
return null;
}
for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
if (proInfo.pid == android.os.Process.myPid()) {
if (proInfo.processName != null) {
return proInfo.processName;
}
}
}
return context.getPackageName();
}
public static boolean isMainProcess(Context context) {
String processName = getProcessName(context);
String pkgName = context.getPackageName();
if (!TextUtils.isEmpty(processName) && !TextUtils.equals(processName, pkgName)) {
return false;
} else {
return true;
}
}
public static List<ResolveInfo> findActivitiesForPackage(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mainIntent.setPackage(packageName);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
return apps != null ? apps : new ArrayList<ResolveInfo>();
}
}
...@@ -24,8 +24,6 @@ import java.util.Set; ...@@ -24,8 +24,6 @@ import java.util.Set;
import de.robv.android.xposed.callbacks.XC_InitPackageResources; import de.robv.android.xposed.callbacks.XC_InitPackageResources;
import de.robv.android.xposed.callbacks.XC_LoadPackage; import de.robv.android.xposed.callbacks.XC_LoadPackage;
import static de.robv.android.xposed.XposedHelpers.getIntField;
/** /**
* This class contains most of Xposed's central logic, such as initialization and callbacks used by * This class contains most of Xposed's central logic, such as initialization and callbacks used by
* the native side. It also includes methods to add new hooks. * the native side. It also includes methods to add new hooks.
......
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