/
MainService.java
129 lines (93 loc) · 3.09 KB
/
MainService.java
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package com.mattfenlon.ghost;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.FrameLayout;
/**
* Created by matt on 08/08/2016.
*/
public class MainService extends Service implements View.OnTouchListener {
private static final String TAG = MainService.class.getSimpleName();
private WindowManager windowManager;
private View floatyView;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
addOverlayView();
}
private void addOverlayView() {
final WindowManager.LayoutParams params;
int layoutParamsType;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
layoutParamsType = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
}
else {
layoutParamsType = LayoutParams.TYPE_PHONE;
}
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
layoutParamsType,
0,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.START;
params.x = 0;
params.y = 0;
FrameLayout interceptorLayout = new FrameLayout(this) {
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
// Only fire on the ACTION_DOWN event, or you'll get two events (one for _DOWN, one for _UP)
if (event.getAction() == KeyEvent.ACTION_DOWN) {
// Check if the HOME button is pressed
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.v(TAG, "BACK Button Pressed");
// As we've taken action, we'll return true to prevent other apps from consuming the event as well
return true;
}
}
// Otherwise don't intercept the event
return super.dispatchKeyEvent(event);
}
};
LayoutInflater inflater = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE));
if (inflater != null) {
floatyView = inflater.inflate(R.layout.floating_view, interceptorLayout);
floatyView.setOnTouchListener(this);
windowManager.addView(floatyView, params);
}
else {
Log.e("SAW-example", "Layout Inflater Service is null; can't inflate and display R.layout.floating_view");
}
}
@Override
public void onDestroy() {
super.onDestroy();
if (floatyView != null) {
windowManager.removeView(floatyView);
floatyView = null;
}
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
view.performClick();
Log.v(TAG, "onTouch...");
// Kill service
onDestroy();
return true;
}
}