Hello fellow devs
It's a well known fact that all modules are loaded into all processes, even if they have nothing to do with them, which makes a pretty big difference if you have ~20 modules active and ~30 processes running.
Would something like this prevent the situation from happening?
Code:
public final class XposedInit implements IXposedHookLoadPackage
{
// Only one field and one method which means small memory footprint?
private IXposedHookLoadPackage mainModule = null;
@Override
public final void handleLoadPackage(final LoadPackageParam lpp) throws Throwable
{
// Hardcode the packages we really use
if (lpp.packageName.equals("android") || lpp.packageName.equals(getClass().getPackage().getName()))
{
// Initialize the real part of the module only if it's really going to be used
if (mainModule == null) mainModule = new MyMainGrandModule();
mainModule.handleLoadPackage(lpp);
}
}
}
Any insight appreciated.
Related
Hello guys,
I want to share my library XClasses.
My idea was to see every XClass as an extension of the original class.
My aim was to improve the user experience during the development.
Also I wanted to increase the readability of xposed code.
So it's nothing special.
It's just my personal opinion how readable code should look like.
A small example:
Code:
public class XposedMain implements IXposedHookLoadPackage
{
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam)
throws Throwable {
if(lpparam.packageName.equals("com.android.systemui"))
{
XposedHelpers.findAndHookMethod(TextView.class, "setText", CharSequence.class, TextView.BufferType.class, boolean.class, int.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param)
throws Throwable {
TextView tv = (TextView)param.thisObject;
CharSequence text = (CharSequence)param.args[0];
TextView.BufferType type = (TextView.BufferType)param.args[1];
boolean notifyBefore = (Boolean)param.args[2];
int oldlen = (Integer)param.args[3];
XposedBridge.log("setText "+text+" with size "+tv.getTextSize());
}
});
}
}
}
The same code with XClasses:
Code:
// class 1
public class XposedMain implements IXposedHookLoadPackage
{
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam)
throws Throwable {
if(lpparam.packageName.equals("com.android.systemui"))
{
setup(lpparam.classLoader);
hook(XTextView.class);
}
}
}
// class 2
public class XTextView
extends AbstractXClass<TextView>
{
public static String getOriginalClassName() { return TextView.class.getCanonicalName(); }
public XTextView(TextView objectThis) { super(objectThis); }
@BeforeOriginalMethod
private void setText_Before(CharSequence text, TextView.BufferType type,
boolean notifyBefore, int oldlen) throws Throwable {
XposedBridge.log("setText "+text+" with size "+getThis().getTextSize());
}
}
If you would like to know more you should visit the following site: https://github.com/seebye/XClasses
Regards,
Seebye
Does anyone know where I can find code handling the current live wallpaper engine / view? I am trying to extract the live wallpaper for further processing (e.g. blurring).
I already found an entrance point: If the LWP used the standard com.android.internal.view.BaseSurfaceHolder supplied by the base class android.service.wallpaper.WallpaperService.Engine i can hook into lockCanvas and unlockCanvas to grab the image. But most LWPs do not use this, so I need another (more general) solution.
I thought about hooking all subclasses of android.service.wallpaper.WallpaperService, but AFAIK that's not possible without loading every single class and check if it's a subclass.
Any ideas?
Implementation for reference:
Code:
public class Hook implements IXposedHookLoadPackage {
@Override
public void handleLoadPackage(XC_LoadPackage.LoadPackageParam loadPackageParam) throws Throwable {
final Class<?> engingeClass = findClass("android.service.wallpaper.WallpaperService.Engine", loadPackageParam.classLoader);
final String packageName = loadPackageParam.packageName;
final Class<?> surfaceHolderClass = findClass("com.android.internal.view.BaseSurfaceHolder",loadPackageParam.classLoader);
findAndHookMethod(engingeClass, "getSurfaceHolder", new XC_MethodHook() {
private boolean isFirstCall = true;
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Field field = findField(param.thisObject.getClass(), "this$0");
WallpaperService service = (WallpaperService) field.get(param.thisObject);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(service);
if (isFirstCall && wallpaperManager.getWallpaperInfo().getPackageName().equals(packageName)) {
isFirstCall = false;
XposedBridge.log("Got context. Set up hooks...");
hook(surfaceHolderClass,service);
}
}
});
}
Bitmap bitmap;
Canvas internalCanvas;
Canvas originalCanvas;
private void hook(Class<?> clazz, final Context context) {
hookAllMethods(clazz, "lockCanvas", new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
originalCanvas = (Canvas) param.getResult();
bitmap = Bitmap.createBitmap(originalCanvas.getWidth(),originalCanvas.getHeight(), Bitmap.Config.ARGB_8888);
internalCanvas = new Canvas(bitmap);
param.setResult(internalCanvas);
XposedBridge.log("Locked Canvas");
}
});
findAndHookMethod(clazz, "unlockCanvasAndPost", Canvas.class, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
originalCanvas.drawBitmap(bitmap,0,0,null);
param.args[0] = originalCanvas;
Intent intent = new Intent("com.faendir.lwpextractor.WALLPAPER_CHANGE");
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG,100,bs);
intent.putExtra("bmp",bs.toByteArray());
context.sendBroadcast(intent, "com.faendir.lwpextractor.RECEIVE_WALLPAPER_CHANGE");
XposedBridge.log("Unlocked Canvas");
}
});
}
}
[this post is a copy of http://forum.xda-developers.com/xposed/modules/extract-live-wallpaper-t3260128, now posted here, because @Spott07 pointed out it would fit better]
So, I'm getting fairly good at hooking and modifying classes, but this one is so unique, I'm not quite sure how to approach hooking it to do what I want. Code for the method/class I want to attack:
Code:
private class CreateLaunchPointListTask
extends AsyncTask<Void, Void, List<LaunchPoint>>
{
private CreateLaunchPointListTask() {}
protected List<LaunchPoint> doInBackground(Void... paramVarArgs)
{
paramVarArgs = mContext.getString(2131558445);
Object localObject = new Intent("android.intent.action.MAIN");
((Intent)localObject).addCategory(paramVarArgs);
paramVarArgs = new LinkedList();
PackageManager localPackageManager = mContext.getPackageManager();
localObject = localPackageManager.queryIntentActivities((Intent)localObject, 129);
int j = ((List)localObject).size();
int i = 0;
while (i < j)
{
ResolveInfo localResolveInfo = (ResolveInfo)((List)localObject).get(i);
if (activityInfo != null) {
paramVarArgs.add(new LaunchPoint(mContext, localPackageManager, localResolveInfo));
}
i += 1;
}
return paramVarArgs;
}
public void onPostExecute(List<LaunchPoint> arg1)
{
synchronized (mLock)
{
mAllLaunchPoints.clear();
mAllLaunchPoints.addAll(???);
synchronized (mCachedActions)
{
LaunchPointListGenerator.access$502(LaunchPointListGenerator.this, true);
if (!mCachedActions.isEmpty()) {
((LaunchPointListGenerator.CachedAction)mCachedActions.remove()).apply();
}
}
}
LaunchPointListGenerator.access$602(LaunchPointListGenerator.this, true);
Iterator localIterator = mListeners.iterator();
while (localIterator.hasNext()) {
((LaunchPointListGenerator.Listener)localIterator.next()).onLaunchPointListGeneratorReady();
}
}
}
So, while this is a big chunk of code, everything I want to do is really in the first few lines:
Code:
paramVarArgs = mContext.getString(2131558445);
Object localObject = new Intent("android.intent.action.MAIN");
((Intent)localObject).addCategory(paramVarArgs);
So, string 2131558445 is a specific intent. What I would like to do is add *another* category after 2131558445 is added to localObject.
That would be the simplest implementation.
A more advanced implementation would be to actually and return a second LinkedList, paramVarArgs2, that only matches up to the second intent category that we're inserting.
Any help would be greatly appreciated.
Hello, I have made this code
Link : http://hastebin.com/hiyupafibi.java
Duplicated here :
Code:
//In my module, I have this activity MainActivity, which has a function to generate random number
private int randomNumber() {
return (new Random()).nextInt(3);
}
//Toast this random number somewhere in the main activity
Toast.makeText(MainActivity.this, " " + randomNumber(), Toast.LENGTH_LONG).show();
//In XposedMod, make a hook
public class XposedMod implements IXposedHookLoadPackage {
private TextView tv;
public static final String PACKAGE_NAME = ".......";
@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
if (lpparam.packageName.equals(PACKAGE_NAME)) {
Class<?> MainActivityClass = XposedHelpers.findClass(PACKAGE_NAME + ".MainActivity", lpparam.classLoader);
XposedHelpers.findAndHookMethod(MainActivityClass, "randomNumber", new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam methodHookParam) throws Throwable {
try {
tv.setText("No NPE");
return 45;
} catch (NullPointerException e) {
return 44;
}
}
});
} else if (lpparam.packageName.equals("com.android.systemui")) {
Class<?> someClass = XposedHelpers.findClass("com.android.systemui.SomeClass", lpparam.classLoader);
XposedHelpers.findAndHookMethod(someClass, "someMethod", Context.class, new XC_MethodHook() {
@Override
protected Object beforeHookedMethod(MethodHookParam methodHookParam) throws Throwable {
tv = new TextView((Context) param.args[0]);
if (tv==null)
XposedBridge.log("tv is null, apologies!");
}
});
}
}
}
Everytime that toast is supposed to be shown, I get the answer to be 44 (that is, tv is null), but that should not be the case, because the log statement when tv is null is not shown. What am I doing wrong?
Thanks for the help, I appreciate it.
Cheers!
Hi,
I have hooked into afterHookedMethod to read some fields from an object but I'm getting java.lang.NoSuchFieldError and I don't understand why.
Say I have these files:
Code:
package com.android.demo;
public class Bar {
public boolean mBoolean;
public Bar() {
mBoolean = false;
}
}
Code:
package com.android.sample;
public class Foo {
Bar getBar() {
Bar bar = new Bar();
bar.mBoolean = true;
return bar;
}
}
And my Xposed hook:
Code:
findAndHookMethod(
"com.android.sample",
lpparam.classLoader,
"getBar",
new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
// This is supposed to be `bar`, right?
Object result = param.getResult();
// Throws java.lang.NoSuchFieldError: com.android.demo.Bar#mBoolean
Log.d(TAG, "mBoolean: " + String.valueOf(getBooleanField(result, "mBoolean")));
// In the end this is what I'm trying to do...
//setBooleanField(result, "mBoolean", false);
//Log.d(TAG, "mBoolean: " + String.valueOf(getBooleanField(result, "mBoolean")));
//param.setResult(result);
}
}
);
One thing that I should mention is that the real code I'm trying to change is obfuscated with Proguard. But I decoded it and looked for the method name - which was just "f" and use that instead of "getBar". I know I got the method name right because the error outputs the package name and class name correctly for the result object which is not obfuscated. And I'm sure this object has many public fields, but I can't access (or change for that matter) any of them without an exception being thrown.
What am I missing?
Whenever I've used this method, the first parameter has been the instance of the class (param.thisObject). I'm curious to know if it's possible to make it work the way you're trying to do with param.getResult().
But if you hook the constructor of the Bar class and try getObjectField with (param.thisObject, mBoolean) it should work.
PunchUp said:
Whenever I've used this method, the first parameter has been the instance of the class (param.thisObject). I'm curious to know if it's possible to make it work the way you're trying to do with param.getResult().
But if you hook the constructor of the Bar class and try getObjectField with (param.thisObject, mBoolean) it should work.
Click to expand...
Click to collapse
Maybe, but I'm not sure that allows me to achieve my end goal...
To be more specific, I'm trying to change the result of this:
https://github.com/android/platform...rc/com/android/email/SecurityPolicy.java#L120
Tried something simpler:
Code:
findAndHookMethod(
"com.android.emailcommon.provider.Policy",
lpparam.classLoader,
"a",
"android.database.Cursor",
new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
Log.d(TAG, "mPasswordMode = " +
String.valueOf(XposedHelpers.getIntField(param.thisObject, "mPasswordMode")));
}
}
);
Got this:
Code:
06-28 21:45:11.133 7201-7824/? E/Xposed: java.lang.NoSuchFieldError: com.android.emailcommon.provider.Policy#mPasswordMode
I don't understand what I'm missing...
I feel like an idiot...
I've been focusing on the methods and the fact that they are obfuscated that I forgot that the instance variables also are obfuscated.
It makes perfect sense now why none of this was working... Back to the drawing board.
Great Your question made me learn something new
PunchUp said:
Great Your question made me learn something new
Click to expand...
Click to collapse
Cool But what was that?
Save