-
Notifications
You must be signed in to change notification settings - Fork 487
Expand file tree
/
Copy pathFloatBarManager.java
More file actions
80 lines (73 loc) · 2.99 KB
/
Copy pathFloatBarManager.java
File metadata and controls
80 lines (73 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package com.x8.launcher.floatview;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import com.x8.launcher.R;
public class FloatBarManager {
private Context mContext;
private WindowManager windowManager;
private View floatSideView;
private WindowManager.LayoutParams layoutParams;
public FloatBarManager(Context context) {
this.mContext = context;
windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
}
public void createSideFloatView() {
if (floatSideView != null) return;
floatSideView = LayoutInflater.from(mContext).inflate(R.layout.layout_float_sidebar, null);
initWindowParams();
try {
windowManager.addView(floatSideView, layoutParams);
} catch (Exception e) {
// ignore if cannot add (permission)
}
bindFloatEvent();
}
private void initWindowParams() {
layoutParams = new WindowManager.LayoutParams();
layoutParams.width = (int) (260 * mContext.getResources().getDisplayMetrics().density);
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
layoutParams.format = PixelFormat.TRANSLUCENT;
layoutParams.type = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) ? WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY : WindowManager.LayoutParams.TYPE_PHONE;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.gravity = Gravity.START | Gravity.TOP;
layoutParams.x = 0;
layoutParams.y = 0;
}
private void bindFloatEvent() {
floatSideView.setOnTouchListener(new View.OnTouchListener() {
private int lastX, lastY;
@Override
public boolean onTouch(View v, MotionEvent event) {
int rawX = (int) event.getRawX();
int rawY = (int) event.getRawY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = rawX;
lastY = rawY;
return true;
case MotionEvent.ACTION_MOVE:
int dx = rawX - lastX;
int dy = rawY - lastY;
layoutParams.x += dx;
layoutParams.y += dy;
try { windowManager.updateViewLayout(floatSideView, layoutParams); } catch (Exception ignored) {}
lastX = rawX; lastY = rawY;
return true;
}
return false;
}
});
}
public void destroyFloatView() {
if (floatSideView != null) {
try { windowManager.removeView(floatSideView); } catch (Exception ignored) {}
floatSideView = null;
}
}
}