Attend XDA's Second Annual Developer Conference, XDA:DevCon 2014!
5,729,690 Members 48,888 Now Online
XDA Developers Android and Mobile Development Forum

[Q] problem with class static value

Tip us?
 
Falseclock
Old
(Last edited by Falseclock; 8th July 2014 at 09:09 PM.)
#1  
Falseclock's Avatar
Senior Member - OP
Thanks Meter 239
Posts: 415
Join Date: Jun 2012
Location: Almaty

 
DONATE TO ME
Default [Q] problem with class static value

have some problems with static values used as flags.

for example I need to hook 2 methods in different FRAMEWORK classes during initZygote. (no problem with system apps during handleLoadPackage)
I hook method in first class, set static value and waiting when another method of another class will be triggered.
once another method is invoked, flag always has it's initial state.

it looks like each hook method processed in different class instances.

i tried volatile type, tried to synchronize - nothing help.

unreliable remedy:
1. register broadcast receiver inside hook method.
2. send action to own service
3. send broadcast from own service.
4. catch broadcast with receiver.

BUT it is not battery cost efficient solution in my case. And mostly I receive broadcast message when hooking method already processed and returned value. Otherwise I need to wait when service will process request and send broadcast. But it is not usable.

is there any solution?

Code:
public class XMain implements IXposedHookInitPackageResources, IXposedHookZygoteInit, IXposedHookLoadPackage
{
	private static boolean isNeedToRun = false;

	public void initZygote(StartupParam startupParam) throws Throwable
	{
		findAndHookMethod("com.class1", null, "method1", int.class, new XC_MethodHook()
		{
			@Override
			protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
			{			    
				isNeedToRun = true;
			}
		});

		findAndHookMethod("com.class2", null, "method2", int.class, new XC_MethodHook()
		{
			@Override
			protected void beforeHookedMethod(final MethodHookParam param) throws Throwable
			{			    
				if (isNeedToRun) param.setResult(null); // always FALSE even if previous hook set as TRUE

			}
		});
	}
}
 
Falseclock
Old
#3  
Falseclock's Avatar
Senior Member - OP
Thanks Meter 239
Posts: 415
Join Date: Jun 2012
Location: Almaty

 
DONATE TO ME
Quote:
Originally Posted by GermainZ View Post
Why do you think that using a broadcast receiver is not battery efficient?
I do not want to invoke service and send broadcast very often (several thousands times during normal battery one cycle charge)

I just want use easiest way by storing flag inside class and worried that users will find the module in the list of gluttonous applications.
 
Tungstwenty
Old
#4  
Tungstwenty's Avatar
Recognized Contributor
Thanks Meter 4166
Posts: 1,765
Join Date: Nov 2011

 
DONATE TO ME
Quote:
Originally Posted by Falseclock View Post
Code:
	if (isNeedToRun) param.setResult(null); // always FALSE even if previous hook set as TRUE
You're probably not considering the fact that different processes will each have its own "isNeededToRun" variable.
Whenever new processes (system_process, apps, etc.) are forked from zygote, each of them will have its own independent state which includes this static variable. From then on they will be completely separate things even if it's a variable with the same name, don't get confused by that.
Try adding a Log.i(...) call to write something in both methods, and then check the logcat for the pid in which the messages are logged. I bet you'll see that different pids are writing and reading, and you can't expect them to be doing it in the same global variable.

Device: Xperia Z (C6603)
ROM: Stock 4.4.2 10.5.A.0.230
Locked bootloader, rooted, XZDualRecovery
Mods (Xposed): App Settings, Advanced reboot menu
Retired device: Samsung Galaxy S2 (GT-I9100)

My threads that you might find useful:

SUPERCOMPUTER: what it sounded like before you bought it
The Following User Says Thank You to Tungstwenty For This Useful Post: [ Click to Expand ]
 
Falseclock
Old
#5  
Falseclock's Avatar
Senior Member - OP
Thanks Meter 239
Posts: 415
Join Date: Jun 2012
Location: Almaty

 
DONATE TO ME
Quote:
Originally Posted by Tungstwenty View Post
You're probably not considering the fact that different processes will each have its own "isNeededToRun" variable.
Whenever new processes (system_process, apps, etc.) are forked from zygote, each of them will have its own independent state which includes this static variable. From then on they will be completely separate things even if it's a variable with the same name, don't get confused by that.
Try adding a Log.i(...) call to write something in both methods, and then check the logcat for the pid in which the messages are logged. I bet you'll see that different pids are writing and reading, and you can't expect them to be doing it in the same global variable.
Yes, I realized this. But under handleLoadPackage everything work perfect.
Any suggestion to solve situation?
 
rovo89
Old
#6  
rovo89's Avatar
Senior Recognized Developer
Thanks Meter 13636
Posts: 2,367
Join Date: Jan 2012
Quote:
Originally Posted by Falseclock View Post
Yes, I realized this. But under handleLoadPackage everything work perfect.
Any suggestion to solve situation?
handleLoadPackage is executed after the fork, in the new process. Not sure what exactly you hooked when you tried that way, but probably both callbacks were executed in the same app/process.

You have the typical problem that requires some kind of inter-process communication (IPC). Most common examples for IPC in Android are broadcasts and services. Files might also be an option (especially with observers), but probably not when you need to change the value very frequently.
Tired of updating your mod for every new ROM release?
You want give users the possibility to combine different mods without creating tons of different files for all possible combinations?
Then have a look at my Xposed framework - modifications without APK changes!
The Following User Says Thank You to rovo89 For This Useful Post: [ Click to Expand ]
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes