I want to inject touch events into the device. I use instrumentation for that. The method works well on Jelly Bean
, but it doesn't inject nor does it give any error in Lollipop
.
When I searched I found that it might be due to the enforcement of SELinux
which prevents some actions from being executed for security purposes. I downloaded SELinux Mode Changer
and set SELinux
to permissive
, and I made sure that it was set to permissive
by checking its status in About phone
in the settings
. My device is rooted, and I have tried with su
and without it. But, really I don't know what is the problem.
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById (R.id.face);
try {
Runtime.getRuntime().exec("supoliciy –live \"allow appdomain input_device dir ( ioctl read getattr search open )\" \"allow appdomain input_device dir ( ioctl read write getattr lock append open )\"");
//supoliciy –live "allow appdomain input_device dir ( ioctl read getattr search open )" "allow appdomain input_device dir ( ioctl read write getattr lock append open )"
// Runtime.getRuntime().exec("reboot");
// Runtime.getRuntime().exec("su -c reboot");
/*
Process process = Runtime.getRuntime().exec("su");//supolicy --live \"allow appdomain input_device dir { ioctl read getattr search open }\" \"allow appdomain input_device chr_file { ioctl read write getattr lock append open }\"");
DataOutputStream os = new DataOutputStream(process.getOutputStream());
String cmd = "/system/bin/input tap 100 200\n";
os.writeBytes(cmd);
os.writeBytes("exit\n");
os.flush();
os.close();
process.waitFor();
*/
Process process = Runtime.getRuntime().exec("su");//supolicy --live \"allow appdomain input_device dir { ioctl read getattr search open }\" \"allow appdomain input_device chr_file { ioctl read write getattr lock append open }\"");
}
catch (IOException e) {
Toast toast = Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG);
toast.show();
e.printStackTrace();
// Log.e(" ", e.getStackTrace().toString());
}
/*
used in cmd case only
catch (InterruptedException e){
Toast toast = Toast.makeText(getApplicationContext(), "ERROR 2", Toast.LENGTH_LONG);
toast.show();
}
*/
final Instrumentation m_Instrumentation = new Instrumentation();
final MotionEvent down = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 400, 600, 0);
down.setSource(InputDevice.SOURCE_TOUCHSCREEN);
final MotionEvent up = MotionEvent.obtain(SystemClock.uptimeMillis(),
SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, 400, 600, 0);
up.setSource(InputDevice.SOURCE_TOUCHSCREEN);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
m_Instrumentation.sendPointerSync(down);
m_Instrumentation.sendPointerSync(up);
}
});
view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Toast toast = Toast.makeText(getApplicationContext(), "View touched", Toast.LENGTH_LONG);
toast.show();
return false;
}
});
t.start();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Toast toast = Toast.makeText(getApplicationContext(), "View touched", Toast.LENGTH_LONG);
toast.show();
return true;
}
}
I even tried to execute supoliciy
as you can see in the code above, but nothing worked.
How can I solve this problem?