Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.pavle.sensorscroll;
- import android.app.Service;
- import android.content.Context;
- import android.content.Intent;
- import android.graphics.Point;
- import android.hardware.Sensor;
- import android.hardware.SensorEvent;
- import android.hardware.SensorEventListener;
- import android.hardware.SensorManager;
- import android.os.AsyncTask;
- import android.os.Handler;
- import android.os.IBinder;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import android.view.Display;
- import android.view.WindowManager;
- import android.widget.Toast;
- import java.io.IOException;
- import java.util.List;
- import java.util.Timer;
- import java.util.TimerTask;
- import eu.chainfire.libsuperuser.Shell;
- import static android.content.ContentValues.TAG;
- /**
- * Created by pavle on 13-Feb-18.
- */
- public class ScrollService extends Service
- {
- SensorManager sManager;
- private float accelerometer[] = new float[3];
- private float magnetic[] = new float[3];
- private float[] rotationMatrix;
- private float[] inclinationMatrix;
- private Point centerPoint;
- private boolean isAutoScrolling = false;
- private boolean isScrollingDown = false;
- @Override
- public int onStartCommand(Intent intent, int flags, int startId)
- {
- sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
- sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
- sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL);
- centerPoint = getDisplayCenterCoordinates(getApplicationContext());
- return super.onStartCommand(intent, flags, startId);
- }
- @Nullable
- @Override
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- sManager.unregisterListener(mySensorEventListener);
- }
- private SensorEventListener mySensorEventListener = new SensorEventListener()
- {
- public void onAccuracyChanged(Sensor sensor, int accuracy)
- {
- }
- public void onSensorChanged(SensorEvent event)
- {
- switch (event.sensor.getType())
- {
- case Sensor.TYPE_MAGNETIC_FIELD:
- magnetic = event.values.clone();
- break;
- case Sensor.TYPE_ACCELEROMETER:
- accelerometer = event.values.clone();
- break;
- }
- if (magnetic != null && accelerometer != null)
- {
- rotationMatrix = new float[9];
- inclinationMatrix = new float[9];
- SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magnetic);
- int inclination = (int) Math.round(Math.toDegrees(Math.acos(rotationMatrix[8])));
- if (inclination <= 40)
- {
- Log.e("up", "up");
- isScrollingDown = true;
- isAutoScrolling = true;
- }
- if (inclination <= 60 && inclination >= 50)
- {
- Log.e("hello", "hello");
- isAutoScrolling = false;
- }
- if (inclination >= 90)
- {
- Log.e("down", "DOWN");
- isScrollingDown = false;
- isAutoScrolling = true;
- }
- }
- }
- };
- public static Point getDisplayCenterCoordinates(Context context) {
- WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
- Display display = wm.getDefaultDisplay();
- final Point size = new Point();
- final Point center = new Point();
- display.getSize(size);
- center.set(size.x/2, size.y/2);
- return center;
- }
- Handler handler = new Handler();
- final Runnable r = new Runnable()
- {
- public void run()
- {
- if (!isAutoScrolling)
- {
- Toast.makeText(ScrollService.this, "DONE", Toast.LENGTH_LONG).show();
- handler.removeCallbacks(this);
- }
- else
- {
- executeCommandWithRoot("input swipe "
- + centerPoint.x + " "
- + centerPoint.y + " "
- + centerPoint.x + " "
- + (isScrollingDown ? Integer.toString(centerPoint.y - 150) : Integer.toString(centerPoint.y + 150)));
- handler.postDelayed(this, 100);
- }
- }
- };
- private static Shell.Interactive rootSession;
- public void executeCommandWithRoot(final String command)
- {
- AsyncTask.execute(new Runnable()
- {
- @Override
- public void run()
- {
- if (rootSession != null)
- {
- rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
- {
- @Override
- public void onCommandResult(int commandCode, int exitCode, List<String> output)
- {
- printShellOutput(output);
- }
- });
- }
- else
- {
- rootSession = new Shell.Builder().
- useSU().
- setWantSTDERR(true).
- setWatchdogTimeout(5).
- setMinimalLogging(true).
- open(new Shell.OnCommandResultListener()
- {
- @Override
- public void onCommandResult(int commandCode, int exitCode, List<String> output)
- {
- if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING)
- {
- Log.i(TAG, "Error opening root shell: exitCode " + exitCode);
- } else
- {
- rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
- {
- @Override
- public void onCommandResult(int commandCode, int exitCode, List<String> output)
- {
- printShellOutput(output);
- }
- });
- }
- }
- });
- }
- }
- });
- }
- public void printShellOutput(List<String> output)
- {
- if (output != null && !output.isEmpty())
- {
- for (String s : output)
- {
- Log.i(TAG, s);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement