Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity
- {
- private Switch switchService;
- private NotificationManager notificationManager;
- private static final int NOTIFY_ID = 100;
- private static final String PLAY_ACTION = "PLAY_ACTION";
- private boolean isStarted = false;
- Intent serviceIntent;
- private BroadcastReceiver broadcastReceiver;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- switchService = findViewById(R.id.switchService);
- notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
- switchService.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
- {
- @Override
- public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked)
- {
- serviceIntent = new Intent(MainActivity.this, ScrollService.class);
- if(isChecked)
- {
- showActionNotification();
- }
- else
- {
- notificationManager.cancel(NOTIFY_ID);
- }
- }
- });
- broadcastReceiver = new BroadcastReceiver()
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- if(isStarted)
- {
- stopService(serviceIntent);
- isStarted = false;
- }
- else
- {
- startService(serviceIntent);
- isStarted = true;
- }
- showActionNotification();
- }
- };
- registerReceiver(broadcastReceiver, new IntentFilter(PLAY_ACTION));
- }
- private void showActionNotification()
- {
- RemoteViews views = new RemoteViews(getPackageName(), R.layout.notificationbarlayout);
- if(isStarted)
- {
- views.setImageViewResource(R.id.play, R.drawable.ic_pause_white_48dp);
- }
- else
- {
- views.setImageViewResource(R.id.play, R.drawable.ic_play_arrow_white_48dp);
- }
- Intent play = new Intent(PLAY_ACTION);
- PendingIntent PlayPend = PendingIntent.getBroadcast(this, 0, play, PendingIntent.FLAG_CANCEL_CURRENT);
- views.setOnClickPendingIntent(R.id.play,PlayPend);
- Intent notificationIntent = new Intent(this, MainActivity.class);
- NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
- .setContentIntent(PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_ONE_SHOT))
- .setSmallIcon(R.mipmap.ic_launcher)
- .setTicker("hello")
- .setContentTitle("hello")
- .setContentText("hello world");
- notification.setContent(views);
- notification.setCustomBigContentView(views);
- notificationManager.notify(NOTIFY_ID, notification.build());
- }
- @Override
- public void onDestroy()
- {
- super.onDestroy();
- if(isStarted)
- {
- stopService(serviceIntent);
- }
- unregisterReceiver(broadcastReceiver);
- }
- }
- 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;
- private static Shell.Interactive rootSession;
- @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)
- {
- isScrollingDown = true;
- isAutoScrolling = true;
- handler.removeCallbacks(r);
- handler.postDelayed(r, 100);
- }
- if (inclination <= 60 && inclination >= 50)
- {
- isAutoScrolling = false;
- handler.removeCallbacks(r);
- }
- if (inclination >= 90)
- {
- isScrollingDown = false;
- isAutoScrolling = true;
- handler.removeCallbacks(r);
- handler.postDelayed(r, 100);
- }
- }
- }
- };
- 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)
- {
- 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);
- }
- }
- };
- 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)
- {
- }
- });
- }
- 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)
- {
- }
- });
- }
- }
- });
- }
- }
- });
- }
Add Comment
Please, Sign In to add comment