Guest User

Untitled

a guest
Oct 17th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. package com.mustafa.shakir.swipegyro;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.IOException;
  5.  
  6. import android.app.Service;
  7. import android.content.Context;
  8. import android.content.Intent;
  9. import android.content.res.Resources;
  10. import android.hardware.Sensor;
  11. import android.hardware.SensorEvent;
  12. import android.hardware.SensorEventListener;
  13. import android.hardware.SensorManager;
  14. import android.os.IBinder;
  15. import android.os.Vibrator;
  16. import android.support.annotation.Nullable;
  17. import android.util.Log;
  18.  
  19. public class GyroService extends Service implements SensorEventListener {
  20. Process m_process = null;
  21. DataOutputStream m_dataOut = null;
  22.  
  23. public final String TAG = GyroService.class.getSimpleName();
  24. private SensorManager sensorManager;
  25. private Sensor sensor;
  26. private long lastUpdate = 0;
  27. private float lastX,lastY,lastZ;
  28. private static final int SHAKE_THRESHOLD = 600;
  29. @Nullable
  30. @Override
  31. public IBinder onBind(Intent intent) {
  32. return null;
  33. }
  34.  
  35. @Override
  36. public void onSensorChanged(SensorEvent sensorEvent) {
  37. if (sensorEvent.sensor.getType() == sensor.TYPE_ACCELEROMETER) {
  38. float x = sensorEvent.values[0];
  39. float y = sensorEvent.values[1];
  40. float z = sensorEvent.values[2];
  41. long curTime = System.currentTimeMillis();
  42.  
  43. if ((curTime - lastUpdate) > 100) {
  44.  
  45. long diffTime = (curTime - lastUpdate);
  46. lastUpdate = curTime;
  47. float speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000;
  48. if (speed > SHAKE_THRESHOLD) {
  49. Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
  50. vib.vibrate(200);
  51. drag(250,300,-800,300,100);
  52.  
  53. }
  54. lastX = x;
  55. lastY = y;
  56. lastZ = z;
  57. }
  58. }
  59. }
  60.  
  61. @Override
  62. public void onAccuracyChanged(Sensor sensor, int i) {
  63.  
  64. }
  65.  
  66. @Override
  67. public int onStartCommand(Intent intent, int flags, int startId) {
  68. askForRoot();
  69. sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
  70. sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
  71. sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);
  72. return START_STICKY;
  73. }
  74. private void drag(float fromX, float toX, float fromY, float toY, int stepCount){
  75. runSwipeCommand((int)fromX, (int)toX, (int)fromY, (int)toY, stepCount);
  76. }
  77.  
  78. private void askForRoot(){
  79. try{
  80. m_process = Runtime.getRuntime().exec("su");
  81. m_dataOut = new DataOutputStream(m_process.getOutputStream());
  82. } catch (Exception e) {e.printStackTrace();}
  83. }
  84. private void runSwipeCommand(final int fromX, final int toX, final int fromY, final int toY, final int duration){
  85. Thread t1 = new Thread(new Runnable(){
  86. public void run(){
  87. try {
  88. if(m_process != null && m_dataOut != null){
  89. String cmd = "/system/bin/input swipe "+fromX+" "+fromY+" "+toX+" "+toY+" "+ duration+"n";
  90. m_dataOut.writeBytes(cmd);
  91. Log.e(TAG, "Command executed: " + cmd);
  92. }
  93. } catch (IOException e) { e.printStackTrace();}
  94. }
  95. });
  96. t1.start();
  97. }
  98. }
Add Comment
Please, Sign In to add comment