[Q] Hooking overriden methods in extended classes - Xposed General

So I'm (attempting) to write my first Xposed module. I'm a little hung up so I'm looking for some help.
I'm trying to hook into the onStartCommand method of the AlarmKlaxon class in com.android.deskclock (link). The AlarmKlaxon class extends "Service" and onStartCommand (the method I want to hook) has the @override identifier.
I would think, I would be able to do something like this:
public class AlarmHandler implements IXposedHookLoadPackage {
public void handleLoadPackage(final LoadPackageParam lpparam) throws Throwable {​
if (!lpparam.packageName.equals("com.android.deskclock"))​return;​
XposedBridge.log("Found Package!");​
findAndHookMethod("com.android.deskclock.AlarmKlaxon", lpparam.classLoader, "onStartCommand", new XC_MethodHook() {​@override​protected void beforeHookedMethod(MethodHookParam param) throws Throwable {​XposedBridge.log("Found Method!");​}​});​}​}
Click to expand...
Click to collapse
When I test out the application, I get "ClassNotFound" exceptions when I hit findAndHookMethod. What seems very strange is that if I try to hook a method in a class that does not extend another class, it seems to work... For example, if I hook AlarmAlertWakeLock class (link), I don't see the exceptions....
Is there something special with the extended classes? When defining the class string, do I need to state that AlarmKlaxon extends another (e.g. "com.android.deskclock.Service.AlarmKlaxon")

Figured it out. Apparently the Cyanogenmod source code I was looking at wasn't the correct version or something. I needed to call the class "com.android.deskclock.alarm.AlarmKlax on"
This thread can be marked as solved.

Related

[Q] findClass vs. static class specification

In all Xposed examples/mods that I've seen so far, the following pattern is used (Environment class used as example):
Code:
findAndHookMethod("android.os.Environment", lpparam.classLoader, <method>, <params>, <hook>);
or something like
Code:
Class<?> cls = findClass("android.os.Environment", lpparam.classLoader); findAndHookMethod(cls, <method>, <params>, <hook>);
But it seems that a direct static specification of the class works as well:
Code:
findAndHookMethod(Environment.class, <method>, <params>, <hook>);
Are there any cons to this approach?
You usually use Environment.class when you can, and "android.os.Environment" when the class is not in the SDK.

[Q] Changing a returned object's property

Hello,
Please consider the following scenario
Code:
public class Document {
...
protected boolean dirty;
...
void setDirty(boolean value) {...}
public boolean getDirty() {...}
}
public class DocumentManager {
...
public Document getDocument(String Id) {...}
...
}
Now I'm hooking getDocument method just fine, but I need to change the returned object's 'dirty' property.
How can I accomplish this?
Thanks in advance for your help.
Call XposedHelpers.setBooleanField(…) on a Document object for "dirty" or hook getDirty() and use param.setResult(…) (or the XC_MethodReplacement.returnConstant(…) shortcut) to set the result you want.
GermainZ said:
Call XposedHelpers.setBooleanField(…) on a Document object for "dirty" or hook getDirty() and use param.setResult(…) (or the XC_MethodReplacement.returnConstant(…) shortcut) to set the result you want.
Click to expand...
Click to collapse
XposedHelpers.setBooleanField did the trick. The class sometimes access the 'dirty' member without going through the getter. That's why I needed to change the field instead of hooking getDirty.
Thanks mate.
Cheers.

[Q] Is it Possible to implement reciever on the module class?

I need to implement a broadcast reciever that will fill variables for the methods my module will hook up.
But every thing that I tried out didnt work.
I tried to implement the reciever in the module class:
public class LogginExtra extends BroadcastReceiver implements IXposedHookLoadPackage {
But it says ClassNotFound.
Tried to make something with sharedPreferences, but the XSharedPreferences doesnt reads the prefs.
And I dont know how to solve this, if someone can help me I need to update 2 variables at the Module class (IXposedHookLoadPackage) from a broadcast reciever.
thanks in advanced.
caioketo said:
I need to implement a broadcast reciever that will fill variables for the methods my module will hook up.
But every thing that I tried out didnt work.
I tried to implement the reciever in the module class:
public class LogginExtra extends BroadcastReceiver implements IXposedHookLoadPackage {
But it says ClassNotFound.
Tried to make something with sharedPreferences, but the XSharedPreferences doesnt reads the prefs.
And I dont know how to solve this, if someone can help me I need to update 2 variables at the Module class (IXposedHookLoadPackage) from a broadcast reciever.
thanks in advanced.
Click to expand...
Click to collapse
The key to the solution is to realize the core of the module (the Xposed hooks) is not an application in itself. Instead its code runs as part of another application or as part of an Android system component. This is why XSharedPreferences is called 'shared'.
It depends on what your module is doing and when/where/how the code of you module runs, which solution is best.
Note that using XSharedPreferences isn't the holy grail either, as this solution might stop working in Android "L", because of new SELinux rules (which could affect quite some modules).
M66B said:
The key to the solution is to realize the core of the module (the Xposed hooks) is not an application in itself. Instead its code runs as part of another application or as part of an Android system component. This is why XSharedPreferences is called 'shared'.
It depends on what your module is doing and when/where/how the code of you module runs, which solution is best.
Note that using XSharedPreferences isn't the holy grail either, as this solution might stop working in Android "L", because of new SELinux rules (which could affect quite some modules).
Click to expand...
Click to collapse
Basically my module hook an Intent method, but I need a way to pass for the module the package name that it should look, and the package Ill get in a broadcast reciever, so, how can I pass a simple String variable for the module?? I understand what you said but still no clue to how to pass a variable from a reciever to the module itself.
caioketo said:
Basically my module hook an Intent method, but I need a way to pass for the module the package name that it should look, and the package Ill get in a broadcast reciever, so, how can I pass a simple String variable for the module?? I understand what you said but still no clue to how to pass a variable from a reciever to the module itself.
Click to expand...
Click to collapse
You can hook Intents at different places, which makes a difference, since this could be either within an application or within an Android component. Hooking within an application makes this simpler, since you will probably be able to use the application context to communicate.
Basically you can use some form of shared storage to pass information (which is what XSharedPreferences does) or inter process calls. Example of IPCs are intents and services, both if which require a proper Context instance to work.
The bottom line is that you first need to understand from where to where you need to communicate.
I am aware that I am not very concrete about what to do. You could take a look at the implementation of XSharedPreferences, which isn't very difficult to understand, or how for example XPrivacy does this, which is more difficult to understand: https://github.com/M66B/XPrivacy (basically the PrivacyManager class is used to fetch settings from a service implemented in PrivacyService class and running within Android).
But what you said about the XSharedPreferences, that it could possible stop working on L, makes me not wanting to use it, so here is my hook code:
findAndHookMethod("android.os.Bundle", loadPackageParam.classLoader, "getString", String.class, new XC_MethodHook() {
@override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (getExtra(loadParam.packageName)) {
String key = (String) param.args[0];
String value = (String) param.getResult();
getExtrasToSend().putString(key, value);
}
}
});
Basically it will grab every call to getExtras, of all packages, and when it get called it should check if the package that called, is the one I'm looking for (from the reciever part), I got 2 ideas, first one I'm testing right now, is I get a class wich holds a static field and method to the packageName variable, and I'm calling it with: (String)XposedHelpers.callMethod(Util.class, "getPackageName", null);
probably wont work, and I was thinking about hooking the method of my own reciever to get the variable in the module class, and use as static variable there.
Any of these should work? Is there a better way to make it??
caioketo said:
But what you said about the XSharedPreferences, that it could possible stop working on L, makes me not wanting to use it, so here is my hook code:
findAndHookMethod("android.os.Bundle", loadPackageParam.classLoader, "getString", String.class, new XC_MethodHook() {
@override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (getExtra(loadParam.packageName)) {
String key = (String) param.args[0];
String value = (String) param.getResult();
getExtrasToSend().putString(key, value);
}
}
});
Basically it will grab every call to getExtras, of all packages, and when it get called it should check if the package that called, is the one I'm looking for (from the reciever part), I got 2 ideas, first one I'm testing right now, is I get a class wich holds a static field and method to the packageName variable, and I'm calling it with: (String)XposedHelpers.callMethod(Util.class, "getPackageName", null);
probably wont work, and I was thinking about hooking the method of my own reciever to get the variable in the module class, and use as static variable there.
Any of these should work? Is there a better way to make it??
Click to expand...
Click to collapse
Static variables probably won't work, since the settings holder and the settings user probably run in different process spaces. You need either some kind of shared storage or an IPC mechanism to communicate the settings.
Is it possible to use files for that? or database? Im really lost, never work with that yet, and seems that there isnt any explanations on google.
caioketo said:
Is it possible to use files for that? or database? Im really lost, never work with that yet, and seems that there isnt any explanations on google.
Click to expand...
Click to collapse
Shared preferences does use an XML file, so yes. A database is possible too, but you need to make sure both the settings manager and the hook do have access to it, same as with using (XML) files. You'll need to take care of locking too, since multiple process will access the file or database.
M66B said:
Shared preferences does use an XML file, so yes. A database is possible too, but you need to make sure both the settings manager and the hook do have access to it, same as with using (XML) files. You'll need to take care of locking too, since multiple process will access the file or database.
Click to expand...
Click to collapse
I have broadcast receivers in my settings activity and my xposed module so they can communicate with each other. You need to setup your receiver in the xposed side programatically:
Code:
Context context = (Context)XposedHelpers.getObjectField(param.thisObject, "mContext");
IntentFilter filter = new IntentFilter();
filter.addAction(XposedReceiver.RESET_ACTION);
filter.addAction(XposedReceiver.REFRESH_ACTION);
context.registerReceiver(mBroadcastReceiver, filter);
The problem is that I'm hooking the Intent class, and it dont have a context field for me to register the broadcast.
EDIT: I don't think Im being clear of what I need, I need to be able to set someway to send the package name to the module, I'll hook this methods:
findAndHookMethod("android.os.Bundle", loadPackageParam.classLoader, "getString", String.class, new XC_MethodHook() { @override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (loadParam.packageName.equals(PACKAGE_NAME_FROM_BROADCAST) {
String key = (String) param.args[0];
String value = (String) param.getResult();
getExtrasToSend().putString(key, value);
}
}
});
and I need to pass that PACKAGE_NAME_FROM_BROADCAST from a broadcast receiver, dont know the way to make it, tried static variables, xsharedpreferences, etc.
Cant get it working. and I dont know more what to try.
caioketo said:
The problem is that I'm hooking the Intent class, and it dont have a context field for me to register the broadcast.
EDIT: I don't think Im being clear of what I need, I need to be able to set someway to send the package name to the module, I'll hook this methods:
Code:
findAndHookMethod("android.os.Bundle", loadPackageParam.classLoader, "getString", String.class, new XC_MethodHook() {
@override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (getExtra(loadParam.packageName)) {
String key = (String) param.args[0];
String value = (String) param.getResult();
getExtrasToSend().putString(key, value);
}
}
});
and I need to pass that PACKAGE_NAME_FROM_BROADCAST from a broadcast receiver, dont know the way to make it, tried static variables, xsharedpreferences, etc.
Cant get it working. and I dont know more what to try.
Click to expand...
Click to collapse
First of all it is not a good idea to hook into android.os.Bundle, since this will impact the system performance significantly.
Moreover android.os.Bundle is used very frequently in applications and various Android components, meaning your hook will run in a variety of process spaces. Getting a Context in this Xposed hook will therefore often not be possible. If you really want to go this way, a service directly registered with the service manager is your only option, but my advice is to find another, less intrusive, way to do what you want.
Ok, the problem is that, my module needs to grabs all calls to "getExtras" ("getStringExtra", "getBooleanExtra", etc) to know all extras that the application will expect, than I'll pass it to AutoShare app, so you can customize and start an intent with all possible extras.
Is there any other way to grab the extras without hooking the Bundle or Intent? About the perform, its doing nothing if its without the right package, so will it still slow the performance???
Thanks for the repply.
caioketo said:
...
About the perform, its doing nothing if its without the right package, so will it still slow the performance???
Thanks for the repply.
Click to expand...
Click to collapse
A hook never does nothing, since there will always be code executed. The hook you are trying to use will be executed really a lot, so there will be significant performance problem.
The 'bundle' is a general 'message', which is used for a lot of things. You should find a way not to hook into the 'bundle', but into a higher level function.

Help needed with a View class.

Hi, this is my first post here so please forgive me if I have put this in the wrong section, it was a bit confusing.
I have an application that takes the touch coordinates from onTouchEvent and and puts them in to an SQLite Database.
My three relevant classes:
Main class: TouchDetector.
Custom View class: TouchDetectorView.
Database class: TouchDatabaseHandler.
TouchDetectorView captures the touches and stores the x, y values in variables. I need to access the TouchDatabaseHandler class from this view class in order to send them to the database.
Constructor inside TouchDatabaseHandler class where data is sent to database:
Code:
public TouchDatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
View class constructor.
Code:
public TouchDetectorView(Context context, AttributeSet attrs) {
super(context, attrs);
dbHandler = new TouchDatabaseHandler([COLOR="Red"][U]this[/U][/COLOR], null, null, 1);
}
What do I replace the this with to make my code work? The tutorial I was following had the dbHandler inside the onCreate but since I am in a View class I do not have on. I apologise if this is very vague, I am a beginner and would appreciate any help and/or feedback.

[HELP]failed hooking GL10?

Hello Everyone:
I am trying to hook GL10 class, but somehow it wouldn't work.
the class is javax.microedition.khronos.opengles.GL10, the method is String glGetString(int name)
here is my code:
Code:
try{
XposedHelpers.findAndHookMethod("javax.microedition.khronos.opengles.GL10", lpparam.classLoader, "glGetString", int.class, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log(">>>>>>>>>>>>>>>>>>>> IN OPENGL GL10 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
XposedBridge.log(param.getResult().toString());
}
});
}catch (Exception e){e.printStackTrace();}
the code above seems did't do anything, I never see the debug print. I also tryied Integer.class as param, but I got nosuchmethod exception.
is there anything I can do to make it work? any hints will be welcome, thank you very much.
All Best
Blueskined
If you don't get any errors from findAndHookMethod it means hook is placed correctly.
The reason why you are not getting any output is most likely this function is never called within package (process) your hook is active in.

Categories

Resources