0

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 SELinuxwhich 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 suand 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?

Dania
  • 1,648
  • 4
  • 31
  • 57

1 Answers1

1

What error are you seeing? logcat should tell you why it was denied. You may be missing some additional permissions. Also, permissions should be grouped together using curly braces, not parentheses.

The error should look something like:

04-12 19:57:31.501 4885-4885/? E/audit: type=1400 msg=audit(1460509051.501:7365): avc: denied { read } for pid=18086 comm="yourappname" name="lux" dev="sysfs" ino=19601 scontext=u:r:untrusted_app:s0:c512,c768 tcontext=u:object_r:sysfs_sensor_writable:s0 tclass=file permissive=0

You can then use this to fix/update your supolicy command. In this example, you would use "allow untrusted_app sysfs_sensor_writable file { read }" (braces aren't needed in this case, but you will probably be adding multiple permissions here).

Phong Tran
  • 11
  • 2