-
Notifications
You must be signed in to change notification settings - Fork 39
/
DebugOverlayService.java
64 lines (52 loc) · 1.46 KB
/
DebugOverlayService.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
package com.hannesdorfmann.debugoverlay;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.View;
/**
* @author Hannes Dorfmann
*/
public class DebugOverlayService extends Service {
public static class DebugOverlayServiceBinder extends Binder {
private DebugOverlayService service;
public DebugOverlayServiceBinder(DebugOverlayService service) {
this.service = service;
}
public DebugOverlayService getService() {
return service;
}
}
private DebugOverlayServiceBinder binder = new DebugOverlayServiceBinder(this);
private DebugOverlayView view;
@Nullable @Override public IBinder onBind(Intent intent) {
return binder;
}
public void logMsg(String msg) {
if (view == null) {
view = new DebugOverlayView(getApplicationContext());
view.getCloseButton().setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
destroyView();
stopSelf();
}
});
}
runOnUiThread(() -> view.addMessage(msg));
}
private void runOnUiThread(Runnable runnable) {
new Handler(getMainLooper()).post(runnable);
}
@Override public void onDestroy() {
super.onDestroy();
destroyView();
}
private void destroyView() {
if (view != null) {
view.hideView();
view = null;
}
}
}