Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. package stuff.mykolamiroshnychenko.accessibilityapp.services;
  2.  
  3. import android.app.Service;
  4. import android.content.Intent;
  5. import android.graphics.PixelFormat;
  6. import android.os.Build;
  7. import android.os.IBinder;
  8. import android.view.Gravity;
  9. import android.view.LayoutInflater;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. import android.view.WindowManager;
  13. import android.widget.ImageView;
  14.  
  15. import kotlin.mykolamiroshnychenko.accessibilityapp.R;
  16. import stuff.mykolamiroshnychenko.accessibilityapp.ui.MainActivity;
  17.  
  18. public class ChatHeadService extends Service {
  19.  
  20. private WindowManager mWindowManager;
  21. private View mChatHeadView;
  22.  
  23. public ChatHeadService() {
  24. }
  25.  
  26. @Override
  27. public IBinder onBind(Intent intent) {
  28. return null;
  29. }
  30.  
  31. @Override
  32. public void onCreate() {
  33. super.onCreate();
  34. //Inflate the chat head layout we created
  35. mChatHeadView = LayoutInflater.from(this).inflate(R.layout.bubble_layout, null);
  36.  
  37.  
  38. int flags = WindowManager.LayoutParams.TYPE_PHONE;
  39. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  40. flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
  41. }
  42. //Add the view to the window.
  43. final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
  44. WindowManager.LayoutParams.WRAP_CONTENT,
  45. WindowManager.LayoutParams.WRAP_CONTENT,
  46. flags,
  47. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
  48. PixelFormat.TRANSLUCENT);
  49.  
  50. //Specify the chat head position
  51. params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
  52. params.x = 0;
  53. params.y = 100;
  54.  
  55. //Add the view to the window
  56. mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  57. mWindowManager.addView(mChatHeadView, params);
  58.  
  59. //Set the close button.
  60. ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
  61. closeButton.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. //close the service and remove the chat head from the window
  65. stopSelf();
  66. }
  67. });
  68.  
  69. //Drag and move chat head using user's touch action.
  70. final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
  71. chatHeadImage.setOnTouchListener(new View.OnTouchListener() {
  72. private int lastAction;
  73. private int initialX;
  74. private int initialY;
  75. private float initialTouchX;
  76. private float initialTouchY;
  77.  
  78. @Override
  79. public boolean onTouch(View v, MotionEvent event) {
  80. switch (event.getAction()) {
  81. case MotionEvent.ACTION_DOWN:
  82.  
  83. //remember the initial position.
  84. initialX = params.x;
  85. initialY = params.y;
  86.  
  87. //get the touch location
  88. initialTouchX = event.getRawX();
  89. initialTouchY = event.getRawY();
  90.  
  91. lastAction = event.getAction();
  92. return true;
  93. case MotionEvent.ACTION_UP:
  94. //As we implemented on touch listener with ACTION_MOVE,
  95. //we have to check if the previous action was ACTION_DOWN
  96. //to identify if the user clicked the view or not.
  97. if (lastAction == MotionEvent.ACTION_DOWN) {
  98. //Open the chat conversation click.
  99. Intent intent = new Intent(ChatHeadService.this, MainActivity.class);
  100. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  101. startActivity(intent);
  102.  
  103. //close the service and remove the chat heads
  104. stopSelf();
  105. }
  106. lastAction = event.getAction();
  107. return true;
  108. case MotionEvent.ACTION_MOVE:
  109. //Calculate the X and Y coordinates of the view.
  110. params.x = initialX + (int) (event.getRawX() - initialTouchX);
  111. params.y = initialY + (int) (event.getRawY() - initialTouchY);
  112.  
  113. //Update the layout with new X & Y coordinate
  114. mWindowManager.updateViewLayout(mChatHeadView, params);
  115. lastAction = event.getAction();
  116. return true;
  117. }
  118. return false;
  119. }
  120. });
  121. }
  122.  
  123. @Override
  124. public void onDestroy() {
  125. super.onDestroy();
  126. if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement