Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package stuff.mykolamiroshnychenko.accessibilityapp.services;
- import android.app.Service;
- import android.content.Intent;
- import android.graphics.PixelFormat;
- import android.os.Build;
- import android.os.IBinder;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.ImageView;
- import kotlin.mykolamiroshnychenko.accessibilityapp.R;
- import stuff.mykolamiroshnychenko.accessibilityapp.ui.MainActivity;
- public class ChatHeadService extends Service {
- private WindowManager mWindowManager;
- private View mChatHeadView;
- public ChatHeadService() {
- }
- @Override
- public IBinder onBind(Intent intent) {
- return null;
- }
- @Override
- public void onCreate() {
- super.onCreate();
- //Inflate the chat head layout we created
- mChatHeadView = LayoutInflater.from(this).inflate(R.layout.bubble_layout, null);
- int flags = WindowManager.LayoutParams.TYPE_PHONE;
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
- flags = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
- }
- //Add the view to the window.
- final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
- WindowManager.LayoutParams.WRAP_CONTENT,
- WindowManager.LayoutParams.WRAP_CONTENT,
- flags,
- WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
- PixelFormat.TRANSLUCENT);
- //Specify the chat head position
- params.gravity = Gravity.TOP | Gravity.LEFT; //Initially view will be added to top-left corner
- params.x = 0;
- params.y = 100;
- //Add the view to the window
- mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
- mWindowManager.addView(mChatHeadView, params);
- //Set the close button.
- ImageView closeButton = (ImageView) mChatHeadView.findViewById(R.id.close_btn);
- closeButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- //close the service and remove the chat head from the window
- stopSelf();
- }
- });
- //Drag and move chat head using user's touch action.
- final ImageView chatHeadImage = (ImageView) mChatHeadView.findViewById(R.id.chat_head_profile_iv);
- chatHeadImage.setOnTouchListener(new View.OnTouchListener() {
- private int lastAction;
- private int initialX;
- private int initialY;
- private float initialTouchX;
- private float initialTouchY;
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- //remember the initial position.
- initialX = params.x;
- initialY = params.y;
- //get the touch location
- initialTouchX = event.getRawX();
- initialTouchY = event.getRawY();
- lastAction = event.getAction();
- return true;
- case MotionEvent.ACTION_UP:
- //As we implemented on touch listener with ACTION_MOVE,
- //we have to check if the previous action was ACTION_DOWN
- //to identify if the user clicked the view or not.
- if (lastAction == MotionEvent.ACTION_DOWN) {
- //Open the chat conversation click.
- Intent intent = new Intent(ChatHeadService.this, MainActivity.class);
- intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
- //close the service and remove the chat heads
- stopSelf();
- }
- lastAction = event.getAction();
- return true;
- case MotionEvent.ACTION_MOVE:
- //Calculate the X and Y coordinates of the view.
- params.x = initialX + (int) (event.getRawX() - initialTouchX);
- params.y = initialY + (int) (event.getRawY() - initialTouchY);
- //Update the layout with new X & Y coordinate
- mWindowManager.updateViewLayout(mChatHeadView, params);
- lastAction = event.getAction();
- return true;
- }
- return false;
- }
- });
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement