Android sendevent is really slow - how to speed it up? - android

I am doing some ui automation, and I am able to store screen touches using getevent, but when I try to send this using sendevent, it takes a really long time, making it hard to actually replay the inputs.
I have already trying loading the script onto the device and running the script locally on the device (a script with a bunch of sendevent commands). But this only imporved this slightly. Is there some other way to inject these commands in a quicker way?

The handler for touch is implemented differently across devices. You should cat /proc/bus/input/devices to see where the touch handler is implemented.
You can also do adb shell getevent, interact with the device and see the output for the interface name.
The reason why your replay takes a long time is because the sendevent binary opens the interface file, writes data to it and closes it for every call to sendevent. So in theory, if you have a bunch of sendevent commands, the binary is opening the interface file, writing data and closing it for every command.
The way I've solved this issue is by re-writing the sendevent.c file under /system/core/toolbox to open the file only once during replay, writing all the data and closing it at the end of the replay. It works perfectly for me!

OK.
Instead of using the getevent/sendevent you can try direct reading from the event interface
inside adb shell try:
dd if=/dev/input/event6 of=record1 # to record
dd if=./record1 of=/dev/input/event6 #to play
However, this may run too fast...

Related

Run multiple commands at once in RootTools/RootShell?

I'm using the RootTools library, and I need to execute two commands. The first one runs a binary, the second sends SIGINT to it, to kill it.
RootTools (as far as I know) can only have one root shell open at a time, so commands can only be executed one by one. This is a problem, because I have no way to stop my binary after I've ran it.
How can I do any of the following things?
Execute two commands at once, so I can run my kill command when the binary is running
Send SIGINT to my native process some other way (e.g. with a RootTools function)
I need to use RootTools because it's the only way for me to read standard output from my program. If there's another way to do that, though, please comment.
Do you think you can concat the commands?
Let's say I want to launch a find command, but if it takes 5 seconds, I want it to stop:
find / & sleep 5 && kill $!
We can get a better suited one liner, too (i.e. ignore standard error, kill only if needed etc.).
You could also just store the PID and kill it later (be careful, if the daemon stopped to run, his PID can be reused by the OS):
run the daemon in a root shell
my-daemon >/dev/null & echo "PID: $!"
parse the output in Java and store the PID (SharedPreferences?)
var pid = outputLine.split(" ")[1]
later on, stop the daemon with a root shell
kill <pid>

Android - Delay When Answering Calls Programmatically on Lollipop

I'm working on an app that can answer phone calls programmatically. Iv'e read much about it, and tried many methods to get it to work.
The only true solution that works for me in versions 5.0 and up is as followed -
Runtime.getRuntime().exec("input keyevent " + Integer.toString(KeyEvent.KEYCODE_HEADSETHOOK));
Although this method works, the actual answering time is delayed by 1.5 to 5 seconds. This delay is a real deal breaker, since my app needs to intercept all incoming calls, and I can't have it delaying the answering process.
Is there any other method that works faster?
Thanks
According to another answer[1] this depends on the Super user client used. Chainfire's SuperSU[2] is said to be faster than Superuser
[1] Need root to execute shell command "input keyevent <#>" through an app at runtime?
[2] https://play.google.com/store/apps/details?id=eu.chainfire.supersu
Depending on your situation, if only the root method works for you, one way is to keep the root shell open with your background service, and pipe the input keyevent command to it when needed. This would save the startup time of the process and the shell every time the call comes in. You can do this easily with libraries such as RootShell.

How to get CPU usage of certain function in android app?

I'm trying to get CPU usage in some point of running app. I need something like i used for time measurement.
Before I called the function (witch I want to measure) I used System.currentTimeMillis() to get the start time and the difference with the same value after function ended.
Running time of this function could be from 1 to 1000ms.
Mine solutions:
I can use adb top command triggered every millisecond (but i don't think it is working properly) adb shell top -m 15 -d 0.001 > C:\something\something\results.txt
Or, I was thinking about to call this command from running app in another thread (if the function will end so the thread would). If you think this could be the right way, may I still send results of command to some file in phone?Or should I use adb shell top -m 15 -d 0.001 -n 1 and call it in while cycle until thread will end?
If by function you mean literally java function then why dont you measure CPU time of its execution (difference of end and start measurements)? You can use System.currentTimeMillis() but this will measure also time of other threads that got CPU quantum. So I believe you are after Debug.threadCpuTimeNanos() which will measure only time CPU was executing your function code, you can investigate how it works by looking into sources:
http://androidxref.com/5.1.0_r1/xref/art/runtime/utils.cc#177
I'm not sure if this is what you're looking for, I've been looking into Android debugging recently, but I haven't tried this myself.
Here's the link: Traceview War Story, from the Android Developer's blog.
It describes using the Traceview tool to analyze functions and how much time the system is devoting to each process within that function.

Testing an Activity which uses a ContentResolver

In my app, I have an Activity, which is basically a form for the user to enter data which is then inserted into a database table via a ContentResolver. How do I test this Activity?
My first attempt was to use ActivityInstrumentationTestCase2 which gives me full instrumentation to simulate entering data. However, the underlying ContentProvider is not closed and destroyed between each test, which leaves the database in an unknown state at the beginning of subsequent tests.
My second attempt was to use ActivityUnitTestCase and inject a mock context that can clean up the database for each test. However, this doesn't allow me to enter text or click on buttons in the activity as it is never actually drawn on the test device.
Does anyone have any suggestions about what else I can try?
it seems that what you've been using is intended for library development
You should look at the monkey binary here , which works great for me.
If you're not satisfied with it you could use monkeyRunner which provides more control over the tests you're running.
Edit :
As far as the database testing goes , cant you use the sqlite3 binary for a simple query after each test?
Edit2:
I am thinking of a .sh script that does the following :
Runs monkey for a while - you can specify the number of events for the monkey to send
Invoke sqlite3 with a query that would check the database integrity into a log file (sqlite3 command can take sql query as a second parameter, and you can use ">" to write the output into some file)
Repeat.
There are tons of examples for .sh scripting on the net so you shouldn't have problem with that.
I am assuming you're doing all this in adb shell, but if you're not, make sure to set all your environment variables correctly. Particularly ANDROID_ROOT, ANDROID_ASSETS and ANDROID_DATA should be set to "/system","/system/app" and "/data" accordingly . Also don't forget to "chmod" the .sh file to be executable ( chmod 777 file.sh ).
Another suggestion is to generate and keep track of the monkey random seeds so you can repeat certain inputs that are causing you problems. You can specify a seed with -s parameter.

init, cron, and am interaction

first time poster here. I have a problem with android. I have cron daemon set up to run a script every once and a while. This script has alarms built in to it that trigger when the script fails or succeeds. The alarms use "am" commands to activate actions on some APKs (one wakes the device, the other plays music and or vibrates).
The script works fine. However, when it is called from the crond it behaves abnormally. dmesg shows init killed the process as per this bit of code:
svc = service_find_by_pid(pid);
if (!svc) {
ERROR("untracked pid %d exited\n", pid);
Obviously my script is not in the init.rc file, nor is crond for that matter, as a service (my phone is locked/reloads init.rc from ramdisk every boot). Is there a manner of fixing this problem without editing the init.rc?
this walkthrough seems like it had you in mind:
http://howto.ccroms.net/howto/cron
As I have always wanted an actual cron installation on my phone, I
worked on patching vixie-cron for just that. Below are the tales of my
adventures:

Categories

Resources