Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mustafa.shakir.swipegyro;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.content.res.Resources;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.IBinder;
- import android.os.Vibrator;
- import android.support.annotation.Nullable;
- import android.util.Log;
- public class GyroService extends Service implements SensorEventListener {
- Process m_process = null;
- DataOutputStream m_dataOut = null;
- public final String TAG = GyroService.class.getSimpleName();
- private SensorManager sensorManager;
- private Sensor sensor;
- private long lastUpdate = 0;
- private float lastX,lastY,lastZ;
- private static final int SHAKE_THRESHOLD = 600;
- @Nullable
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onSensorChanged(SensorEvent sensorEvent) {
- if (sensorEvent.sensor.getType() == sensor.TYPE_ACCELEROMETER) {
- float x = sensorEvent.values[0];
- float y = sensorEvent.values[1];
- float z = sensorEvent.values[2];
- long curTime = System.currentTimeMillis();
- if ((curTime - lastUpdate) > 100) {
- long diffTime = (curTime - lastUpdate);
- lastUpdate = curTime;
- float speed = Math.abs(x + y + z - lastX - lastY - lastZ) / diffTime * 10000;
- if (speed > SHAKE_THRESHOLD) {
- Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
- vib.vibrate(200);
- drag(250,300,-800,300,100);
- }
- lastX = x;
- lastY = y;
- lastZ = z;
- }
- }
- }
- @Override
- public void onAccuracyChanged(Sensor sensor, int i) {
- }
- @Override
- public int onStartCommand(Intent intent, int flags, int startId) {
- askForRoot();
- sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
- sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
- sensorManager.registerListener(this,sensor,SensorManager.SENSOR_DELAY_NORMAL);
- return START_STICKY;
- }
- private void drag(float fromX, float toX, float fromY, float toY, int stepCount){
- runSwipeCommand((int)fromX, (int)toX, (int)fromY, (int)toY, stepCount);
- }
- private void askForRoot(){
- try{
- m_process = Runtime.getRuntime().exec("su");
- m_dataOut = new DataOutputStream(m_process.getOutputStream());
- } catch (Exception e) {e.printStackTrace();}
- }
- private void runSwipeCommand(final int fromX, final int toX, final int fromY, final int toY, final int duration){
- Thread t1 = new Thread(new Runnable(){
- public void run(){
- try {
- if(m_process != null && m_dataOut != null){
- String cmd = "/system/bin/input swipe "+fromX+" "+fromY+" "+toX+" "+toY+" "+ duration+"n";
- m_dataOut.writeBytes(cmd);
- Log.e(TAG, "Command executed: " + cmd);
- }
- } catch (IOException e) { e.printStackTrace();}
- }
- });
- t1.start();
- }
- }
Add Comment
Please, Sign In to add comment