Advertisement
Pavle_nis

Untitled

Jun 7th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.28 KB | None | 0 0
  1. package com.pavle.sensorscroll;
  2.  
  3. import android.app.Service;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.graphics.Point;
  7. import android.hardware.Sensor;
  8. import android.hardware.SensorEvent;
  9. import android.hardware.SensorEventListener;
  10. import android.hardware.SensorManager;
  11. import android.os.AsyncTask;
  12. import android.os.Handler;
  13. import android.os.IBinder;
  14. import android.support.annotation.Nullable;
  15. import android.util.Log;
  16. import android.view.Display;
  17. import android.view.WindowManager;
  18. import android.widget.Toast;
  19.  
  20. import java.io.IOException;
  21. import java.util.List;
  22. import java.util.Timer;
  23. import java.util.TimerTask;
  24.  
  25. import eu.chainfire.libsuperuser.Shell;
  26.  
  27. import static android.content.ContentValues.TAG;
  28.  
  29. /**
  30.  * Created by pavle on 13-Feb-18.
  31.  */
  32.  
  33. public class ScrollService extends Service
  34. {
  35.     SensorManager sManager;
  36.     private float accelerometer[] = new float[3];
  37.     private float magnetic[] = new float[3];
  38.     private float[] rotationMatrix;
  39.     private float[] inclinationMatrix;
  40.  
  41.     private Point centerPoint;
  42.  
  43.     private boolean isAutoScrolling = false;
  44.     private boolean isScrollingDown = false;
  45.  
  46.     @Override
  47.     public int onStartCommand(Intent intent, int flags, int startId)
  48.     {
  49.         sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  50.         sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
  51.         sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL);
  52.  
  53.         centerPoint = getDisplayCenterCoordinates(getApplicationContext());
  54.  
  55.         return super.onStartCommand(intent, flags, startId);
  56.     }
  57.  
  58.     @Nullable
  59.     @Override
  60.     public IBinder onBind(Intent intent)
  61.     {
  62.         return null;
  63.     }
  64.  
  65.     @Override
  66.     public void onDestroy()
  67.     {
  68.         super.onDestroy();
  69.  
  70.         sManager.unregisterListener(mySensorEventListener);
  71.     }
  72.  
  73.     private SensorEventListener mySensorEventListener = new SensorEventListener()
  74.     {
  75.         public void onAccuracyChanged(Sensor sensor, int accuracy)
  76.         {
  77.         }
  78.  
  79.         public void onSensorChanged(SensorEvent event)
  80.         {
  81.             switch (event.sensor.getType())
  82.             {
  83.                 case Sensor.TYPE_MAGNETIC_FIELD:
  84.                     magnetic = event.values.clone();
  85.                     break;
  86.                 case Sensor.TYPE_ACCELEROMETER:
  87.                     accelerometer = event.values.clone();
  88.                     break;
  89.             }
  90.  
  91.             if (magnetic != null && accelerometer != null)
  92.             {
  93.                 rotationMatrix = new float[9];
  94.                 inclinationMatrix = new float[9];
  95.                 SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magnetic);
  96.                 int inclination = (int) Math.round(Math.toDegrees(Math.acos(rotationMatrix[8])));
  97.  
  98.                 if (inclination <= 40)
  99.                 {
  100.                     Log.e("up", "up");
  101.  
  102.                     isScrollingDown = true;
  103.                     isAutoScrolling = true;
  104.                 }
  105.                 if (inclination <= 60 && inclination >= 50)
  106.                 {
  107.                     Log.e("hello", "hello");
  108.  
  109.                     isAutoScrolling = false;
  110.                 }
  111.  
  112.                 if (inclination >= 90)
  113.                 {
  114.                     Log.e("down", "DOWN");
  115.  
  116.                     isScrollingDown = false;
  117.                     isAutoScrolling = true;
  118.                 }
  119.             }
  120.         }
  121.     };
  122.  
  123.     public static Point getDisplayCenterCoordinates(Context context) {
  124.         WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  125.         Display display = wm.getDefaultDisplay();
  126.         final Point size = new Point();
  127.         final Point center = new Point();
  128.         display.getSize(size);
  129.         center.set(size.x/2, size.y/2);
  130.         return center;
  131.     }
  132.  
  133.     Handler handler = new Handler();
  134.  
  135.     final Runnable r = new Runnable()
  136.     {
  137.         public void run()
  138.         {
  139.             if (!isAutoScrolling)
  140.             {
  141.                 Toast.makeText(ScrollService.this, "DONE", Toast.LENGTH_LONG).show();
  142.                 handler.removeCallbacks(this);
  143.             }
  144.             else
  145.             {
  146.                 executeCommandWithRoot("input swipe "
  147.                         + centerPoint.x + " "
  148.                         + centerPoint.y + " "
  149.                         + centerPoint.x + " "
  150.                         + (isScrollingDown ? Integer.toString(centerPoint.y - 150) : Integer.toString(centerPoint.y + 150)));
  151.  
  152.                 handler.postDelayed(this, 100);
  153.             }
  154.         }
  155.     };
  156.  
  157.     private static Shell.Interactive rootSession;
  158.  
  159.     public void executeCommandWithRoot(final String command)
  160.     {
  161.         AsyncTask.execute(new Runnable()
  162.         {
  163.             @Override
  164.             public void run()
  165.             {
  166.                 if (rootSession != null)
  167.                 {
  168.                     rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
  169.                     {
  170.                         @Override
  171.                         public void onCommandResult(int commandCode, int exitCode, List<String> output)
  172.                         {
  173.                             printShellOutput(output);
  174.                         }
  175.                     });
  176.                 }
  177.                 else
  178.                 {
  179.                     rootSession = new Shell.Builder().
  180.                             useSU().
  181.                             setWantSTDERR(true).
  182.                             setWatchdogTimeout(5).
  183.                             setMinimalLogging(true).
  184.                             open(new Shell.OnCommandResultListener()
  185.                             {
  186.                                 @Override
  187.                                 public void onCommandResult(int commandCode, int exitCode, List<String> output)
  188.                                 {
  189.                                     if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING)
  190.                                     {
  191.                                         Log.i(TAG, "Error opening root shell: exitCode " + exitCode);
  192.                                     } else
  193.                                     {
  194.                                         rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
  195.                                         {
  196.                                             @Override
  197.                                             public void onCommandResult(int commandCode, int exitCode, List<String> output)
  198.                                             {
  199.                                                 printShellOutput(output);
  200.                                             }
  201.                                         });
  202.                                     }
  203.                                 }
  204.                             });
  205.                 }
  206.             }
  207.         });
  208.     }
  209.  
  210.     public void printShellOutput(List<String> output)
  211.     {
  212.         if (output != null && !output.isEmpty())
  213.         {
  214.             for (String s : output)
  215.             {
  216.                 Log.i(TAG, s);
  217.             }
  218.         }
  219.     }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement