Created
April 30, 2019 13:27
-
-
Save korakotlee/1da972f360fb75f559972c73d6789138 to your computer and use it in GitHub Desktop.
ChatHeadService for Flutter platform channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.korakotlee.korspeed; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.graphics.PixelFormat; | |
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 android.widget.TextView; | |
import android.os.Bundle; | |
import android.util.Log; | |
@SuppressWarnings("deprecation") | |
public class ChatHeadService extends Service { | |
private WindowManager mWindowManager; | |
private View mChatHeadView; | |
private String message = "init"; | |
public ChatHeadService() { | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
// public void onStart(Intent intent, int startId) { | |
// super.onStart(intent, startId); | |
Bundle extras = intent.getExtras(); | |
// message = "onStart"; | |
if(extras == null) { | |
// Log.d("Service","null"); | |
} else { | |
// Log.d("Service","not null"); | |
message = (String) extras.get("message"); | |
// Log.d("Service","message: "+message); | |
// if(from.equalsIgnoreCase("Main")) | |
// StartListenLocation(); | |
} | |
final WindowManager.LayoutParams params = new WindowManager.LayoutParams( | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.WRAP_CONTENT, | |
WindowManager.LayoutParams.TYPE_PHONE, | |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, | |
PixelFormat.TRANSLUCENT); | |
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(); | |
// } | |
// }); | |
final TextView textView = (TextView) mChatHeadView.findViewById(R.id.label); | |
textView.setText( message); | |
textView.setOnTouchListener(new View.OnTouchListener() { | |
// chatHeadImage.setOnTouchListener(new View.OnTouchListener() { | |
private int lastAction; | |
private int initialX; | |
private int initialY; | |
private float initialTouchX; | |
private float initialTouchY; | |
private long startTime; | |
private long duration; | |
private int clickCount; | |
private static final long MAX_DURATION = 500; | |
@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(); | |
// detect Double Tap | |
startTime = System.currentTimeMillis(); | |
clickCount++; | |
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) { | |
// Single Tap - > Change Unit | |
//close the service and remove the chat heads | |
// stopSelf(); | |
} | |
lastAction = event.getAction(); | |
// detect Double Tap | |
long time = System.currentTimeMillis() - startTime; | |
duration= duration + time; | |
if(clickCount == 2) | |
{ | |
if(duration<= MAX_DURATION) | |
{ | |
stopSelf(); | |
// Toast.makeText(captureActivity.this, "double tap",Toast.LENGTH_LONG).show(); | |
} | |
clickCount = 0; | |
duration = 0; | |
break; | |
} | |
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; | |
} | |
}); | |
return START_NOT_STICKY; | |
} | |
@Override | |
public void onCreate() { | |
super.onCreate(); | |
//Inflate the chat head layout we created | |
mChatHeadView = LayoutInflater.from(this).inflate(R.layout.chat_head, null); | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
if (mChatHeadView != null) mWindowManager.removeView(mChatHeadView); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment