Pavle_nis

java

Feb 14th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.19 KB | None | 0 0
  1. public class MainActivity extends AppCompatActivity
  2. {
  3.     private Switch switchService;
  4.     private NotificationManager notificationManager;
  5.     private static final int NOTIFY_ID = 100;
  6.     private static final String PLAY_ACTION = "PLAY_ACTION";
  7.     private boolean isStarted = false;
  8.     Intent serviceIntent;
  9.     private BroadcastReceiver broadcastReceiver;
  10.  
  11.     @Override
  12.     protected void onCreate(Bundle savedInstanceState)
  13.     {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.activity_main);
  16.  
  17.         switchService = findViewById(R.id.switchService);
  18.  
  19.         notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  20.  
  21.         switchService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
  22.         {
  23.             @Override
  24.             public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked)
  25.             {
  26.                 serviceIntent = new Intent(MainActivity.this, ScrollService.class);
  27.  
  28.                 if(isChecked)
  29.                 {
  30.                     showActionNotification();
  31.                 }
  32.                 else
  33.                 {
  34.                     notificationManager.cancel(NOTIFY_ID);
  35.                 }
  36.             }
  37.         });
  38.  
  39.         broadcastReceiver = new BroadcastReceiver()
  40.         {
  41.             @Override
  42.             public void onReceive(Context context, Intent intent)
  43.             {
  44.                 if(isStarted)
  45.                 {
  46.                     stopService(serviceIntent);
  47.                     isStarted = false;
  48.                 }
  49.                 else
  50.                 {
  51.                     startService(serviceIntent);
  52.                     isStarted = true;
  53.                 }
  54.                 showActionNotification();
  55.             }
  56.         };
  57.         registerReceiver(broadcastReceiver, new IntentFilter(PLAY_ACTION));
  58.     }
  59.  
  60.     private void showActionNotification()
  61.     {
  62.         RemoteViews views = new RemoteViews(getPackageName(), R.layout.notificationbarlayout);
  63.  
  64.         if(isStarted)
  65.         {
  66.             views.setImageViewResource(R.id.play, R.drawable.ic_pause_white_48dp);
  67.         }
  68.         else
  69.         {
  70.             views.setImageViewResource(R.id.play, R.drawable.ic_play_arrow_white_48dp);
  71.         }
  72.  
  73.         Intent play = new Intent(PLAY_ACTION);
  74.         PendingIntent PlayPend = PendingIntent.getBroadcast(this, 0, play, PendingIntent.FLAG_CANCEL_CURRENT);
  75.         views.setOnClickPendingIntent(R.id.play,PlayPend);
  76.  
  77.         Intent notificationIntent = new Intent(this, MainActivity.class);
  78.  
  79.         NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
  80.                 .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT))
  81.                 .setSmallIcon(R.mipmap.ic_launcher)
  82.                 .setTicker("hello")
  83.                 .setContentTitle("hello")
  84.                 .setContentText("hello world");
  85.  
  86.         notification.setContent(views);
  87.         notification.setCustomBigContentView(views);
  88.         notificationManager.notify(NOTIFY_ID, notification.build());
  89.     }
  90.  
  91.     @Override
  92.     public void onDestroy()
  93.     {
  94.         super.onDestroy();
  95.  
  96.         if(isStarted)
  97.         {
  98.             stopService(serviceIntent);
  99.         }
  100.  
  101.         unregisterReceiver(broadcastReceiver);
  102.     }
  103. }
  104.  
  105. public class ScrollService extends Service
  106. {
  107.     SensorManager sManager;
  108.     private float accelerometer[] = new float[3];
  109.     private float magnetic[] = new float[3];
  110.     private float[] rotationMatrix;
  111.     private float[] inclinationMatrix;
  112.  
  113.     private Point centerPoint;
  114.  
  115.     private boolean isAutoScrolling = false;
  116.     private boolean isScrollingDown = false;
  117.  
  118.     private static Shell.Interactive rootSession;
  119.  
  120.     @Override
  121.     public int onStartCommand(Intent intent, int flags, int startId)
  122.     {
  123.         sManager = (SensorManager) getSystemService(SENSOR_SERVICE);
  124.         sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),SensorManager.SENSOR_DELAY_NORMAL);
  125.         sManager.registerListener(mySensorEventListener, sManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD),SensorManager.SENSOR_DELAY_NORMAL);
  126.  
  127.         centerPoint = getDisplayCenterCoordinates(getApplicationContext());
  128.  
  129.         return super.onStartCommand(intent, flags, startId);
  130.     }
  131.  
  132.     @Nullable
  133.     @Override
  134.     public IBinder onBind(Intent intent)
  135.     {
  136.         return null;
  137.     }
  138.  
  139.     @Override
  140.     public void onDestroy()
  141.     {
  142.         super.onDestroy();
  143.  
  144.         sManager.unregisterListener(mySensorEventListener);
  145.     }
  146.  
  147.     private SensorEventListener mySensorEventListener = new SensorEventListener()
  148.     {
  149.         public void onAccuracyChanged(Sensor sensor, int accuracy)
  150.         {
  151.         }
  152.  
  153.         public void onSensorChanged(SensorEvent event)
  154.         {
  155.             switch (event.sensor.getType())
  156.             {
  157.                 case Sensor.TYPE_MAGNETIC_FIELD:
  158.                     magnetic = event.values.clone();
  159.                     break;
  160.                 case Sensor.TYPE_ACCELEROMETER:
  161.                     accelerometer = event.values.clone();
  162.                     break;
  163.             }
  164.  
  165.             if (magnetic != null && accelerometer != null)
  166.             {
  167.                 rotationMatrix = new float[9];
  168.                 inclinationMatrix = new float[9];
  169.                 SensorManager.getRotationMatrix(rotationMatrix, inclinationMatrix, accelerometer, magnetic);
  170.                 int inclination = (int) Math.round(Math.toDegrees(Math.acos(rotationMatrix[8])));
  171.  
  172.                 if (inclination <= 40)
  173.                 {
  174.                     isScrollingDown = true;
  175.                     isAutoScrolling = true;
  176.  
  177.                     handler.removeCallbacks(r);
  178.                     handler.postDelayed(r, 100);
  179.                 }
  180.                 if (inclination <= 60 && inclination >= 50)
  181.                 {
  182.                     isAutoScrolling = false;
  183.  
  184.                     handler.removeCallbacks(r);
  185.                 }
  186.  
  187.                 if (inclination >= 90)
  188.                 {
  189.                     isScrollingDown = false;
  190.                     isAutoScrolling = true;
  191.  
  192.                     handler.removeCallbacks(r);
  193.                     handler.postDelayed(r, 100);
  194.                 }
  195.             }
  196.         }
  197.     };
  198.  
  199. public static Point getDisplayCenterCoordinates(Context context)
  200. {
  201.     WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  202.     Display display = wm.getDefaultDisplay();
  203.     final Point size = new Point();
  204.     final Point center = new Point();
  205.     display.getSize(size);
  206.     center.set(size.x/2, size.y/2);
  207.     return center;
  208. }
  209.  
  210. Handler handler = new Handler();
  211.  
  212. final Runnable r = new Runnable()
  213. {
  214.     public void run()
  215.     {
  216.         if (!isAutoScrolling)
  217.         {
  218.             handler.removeCallbacks(this);
  219.         }
  220.         else
  221.         {
  222.             executeCommandWithRoot("input swipe "
  223.                     + centerPoint.x + " "
  224.                     + centerPoint.y + " "
  225.                     + centerPoint.x + " "
  226.                     + (isScrollingDown ? Integer.toString(centerPoint.y - 150) : Integer.toString(centerPoint.y + 150)));
  227.  
  228.             handler.postDelayed(this, 100);
  229.         }
  230.     }
  231. };
  232.  
  233. public void executeCommandWithRoot(final String command)
  234. {
  235.     AsyncTask.execute(new Runnable()
  236.     {
  237.         @Override
  238.         public void run()
  239.         {
  240.             if (rootSession != null)
  241.             {
  242.                 rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
  243.                 {
  244.                     @Override
  245.                     public void onCommandResult(int commandCode, int exitCode, List<String> output)
  246.                     {
  247.  
  248.                     }
  249.                 });
  250.             }
  251.             else
  252.             {
  253.                 rootSession = new Shell.Builder().
  254.                         useSU().
  255.                         setWantSTDERR(true).
  256.                         setWatchdogTimeout(5).
  257.                         setMinimalLogging(true).
  258.                         open(new Shell.OnCommandResultListener()
  259.                         {
  260.                             @Override
  261.                             public void onCommandResult(int commandCode, int exitCode, List<String> output)
  262.                             {
  263.                                 if (exitCode != Shell.OnCommandResultListener.SHELL_RUNNING)
  264.                                 {
  265.                                     Log.i(TAG, "Error opening root shell: exitCode " + exitCode);
  266.                                 } else
  267.                                 {
  268.                                     rootSession.addCommand(command, 0, new Shell.OnCommandResultListener()
  269.                                     {
  270.                                         @Override
  271.                                         public void onCommandResult(int commandCode, int exitCode, List<String> output)
  272.                                         {
  273.  
  274.                                         }
  275.                                     });
  276.                                 }
  277.                             }
  278.                         });
  279.             }
  280.         }
  281.     });
  282. }
Add Comment
Please, Sign In to add comment