Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. public class PointerService extends Service {
  2. private ImageView cursor;
  3. private CountDownTimer timer;
  4. private WindowManager.LayoutParams params;
  5.  
  6. public void Update(final int x, final int y) {
  7. Log.d("PointerService", "Updating cursor position. X = " + Integer.toString(x) +
  8. "nY = " + Integer.toString(y));
  9. //Packet received => timer reset
  10. cursor.setVisibility(View.VISIBLE);
  11. cursor.setX(x);
  12. cursor.setY(y);
  13. cursor.postInvalidate();
  14. timer.cancel();
  15. timer.start();
  16. }
  17.  
  18. @Override
  19. public IBinder onBind(Intent intent) {
  20. return null;
  21. }
  22.  
  23. @Override
  24. public void onCreate() {
  25. super.onCreate();
  26. Log.d("PointerService", "Creating service");
  27.  
  28. cursor = new ImageView(this);
  29. cursor.setImageResource(R.drawable.ic_cursor);
  30. cursor.setVisibility(View.INVISIBLE);
  31. params = new WindowManager.LayoutParams(
  32. 64, 64,
  33. WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
  34. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
  35. WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
  36. WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
  37. WindowManager.LayoutParams.FLAG_SECURE,
  38. PixelFormat.TRANSPARENT);
  39. params.setTitle("Cursor");
  40. params.gravity = Gravity.TOP | Gravity.LEFT;
  41. WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  42. wm.addView(cursor, params);
  43.  
  44. timer = new CountDownTimer(5000, 5000) {
  45. @Override public void onTick(long l) {}
  46. @Override public void onFinish() {
  47. cursor.setVisibility(View.INVISIBLE);
  48. cursor.postInvalidate();
  49. }
  50. };
  51. timer.start();
  52. }
  53.  
  54. @Override
  55. public void onDestroy() {
  56. super.onDestroy();
  57. Log.d("CursorService", "Service destroyed");
  58. Singleton.getInstance().pointerService = null;
  59. if(cursor != null) {
  60. ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(cursor);
  61. cursor = null;
  62. }
  63. }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement