Skip to content

Instantly share code, notes, and snippets.

@sontqq
Created January 26, 2020 15:19
Show Gist options
  • Save sontqq/d7821a2b8a4a75d5119664de13ae4bed to your computer and use it in GitHub Desktop.
Save sontqq/d7821a2b8a4a75d5119664de13ae4bed to your computer and use it in GitHub Desktop.
public static void initHeadOverlay(Context c) {
try {
if (headoverlay_mHeadView != null) {
headoverlay_mHeadView.setVisibility(View.GONE);
headoverlay_mHeadView = null;
}
headoverlay_mHeadView = LayoutInflater.from(c).inflate(R.layout.head_layout, null);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
params.x = 0;
params.y = 700;
headoverlay_mWindowManager = (WindowManager) c.getSystemService(WINDOW_SERVICE);
headoverlay_mWindowManager.addView(headoverlay_mHeadView, params);
headoverlay_lay_txt1 = headoverlay_mHeadView.findViewById(R.id.laytext);
headoverlay_lay_txt2 = headoverlay_mHeadView.findViewById(R.id.laytext2);
headoverlay_lay_btn1 = headoverlay_mHeadView.findViewById(R.id.lay_btn1);
headoverlay_lay_btn2 = headoverlay_mHeadView.findViewById(R.id.lay_btn2);
bllistview = headoverlay_mHeadView.findViewById(R.id.bllist);
headoverlay_lay_btn1.setText("Ping");
headoverlay_lay_btn2.setText("Hide");
headoverlay_lay_btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
headoverlay_lay_txt1.setText(String.valueOf(System.currentTimeMillis()));
}
});
headoverlay_lay_btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
headoverlay_mHeadView.setVisibility(View.GONE);
headoverlay_mHeadView = null;
}
});
headoverlay_lay_txt1.setText("SERVICE STARTED");
headoverlay_lay_txt1.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:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
lastAction = event.getAction();
return true;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
//OPEN MAIN ACTIVITY
Intent intent = new Intent(c, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intent);
//stopSelf();
}
lastAction = event.getAction();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
headoverlay_mWindowManager.updateViewLayout(headoverlay_mHeadView, params);
lastAction = event.getAction();
return true;
}
return false;
}
});
headoverlay_lay_txt2.setVisibility(View.GONE);
headoverlay_lay_txt2.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:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
lastAction = event.getAction();
return true;
case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN) {
//OPEN MAIN ACTIVITY
Intent intent = new Intent(c, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intent);
//stopSelf();
}
lastAction = event.getAction();
return true;
case MotionEvent.ACTION_MOVE:
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
headoverlay_mWindowManager.updateViewLayout(headoverlay_mHeadView, params);
lastAction = event.getAction();
return true;
}
return false;
}
});
} catch (Exception e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment