Welcome to XDA

Search to go directly to your device's forum

Register an account

Unlock full posting privileges

Ask a question

No registration required
Post Reply

[Q] problem with class static value

OP Falseclock

8th July 2014, 09:40 PM   |  #1  
Falseclock's Avatar
OP Senior Member
Flag Almaty
Thanks Meter: 319
 
517 posts
Join Date:Joined: Jun 2012
Donate to Me
More
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

			}
		});
	}
}
Last edited by Falseclock; 8th July 2014 at 10:09 PM.
9th July 2014, 12:30 AM   |  #2  
GermainZ's Avatar
Forum Moderator / Recognized Developer / XDA Portal Team
Thanks Meter: 7,337
 
5,911 posts
Join Date:Joined: Aug 2012
More
Why do you think that using a broadcast receiver is not battery efficient?
9th July 2014, 07:13 AM   |  #3  
Falseclock's Avatar
OP Senior Member
Flag Almaty
Thanks Meter: 319
 
517 posts
Join Date:Joined: Jun 2012
Donate to Me
More
Quote:
Originally Posted by GermainZ

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.
9th July 2014, 10:58 AM   |  #4  
Tungstwenty's Avatar
Recognized Contributor
Thanks Meter: 4,408
 
1,824 posts
Join Date:Joined: Nov 2011
Donate to Me
More
Quote:
Originally Posted by Falseclock

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.
The Following User Says Thank You to Tungstwenty For This Useful Post: [ View ]
9th July 2014, 11:06 AM   |  #5  
Falseclock's Avatar
OP Senior Member
Flag Almaty
Thanks Meter: 319
 
517 posts
Join Date:Joined: Jun 2012
Donate to Me
More
Quote:
Originally Posted by Tungstwenty

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?
9th July 2014, 05:48 PM   |  #6  
rovo89's Avatar
Senior Recognized Developer
Thanks Meter: 15,342
 
2,386 posts
Join Date:Joined: Jan 2012
More
Quote:
Originally Posted by Falseclock

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.
The Following User Says Thank You to rovo89 For This Useful Post: [ View ]
Post Reply Subscribe to Thread
Previous Thread Next Thread
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes