Skip to content

Commit ed2e81c

Browse files
author
Hannes Dorfmann
committed
Updated build tools and ensure that dispatching logging messages run on main ui thread; Fixes #7
1 parent 7408c7b commit ed2e81c

7 files changed

Lines changed: 118 additions & 98 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.application'
22

33
android {
44
compileSdkVersion 27
5-
buildToolsVersion "27.0.0"
5+
buildToolsVersion '28.0.3'
66

77
defaultConfig {
88
applicationId "com.hannesdorfmann.debugoverlay.sample"

app/src/main/java/com/hannesdorfmann/debugoverlay/sample/MainActivity.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@ public class MainActivity extends AppCompatActivity {
2727
@Override public void onClick(View view) {
2828
boolean showDebugOverlay = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(MainActivity.this);
2929
if (showDebugOverlay) {
30-
DebugOverlay.with(MainActivity.this).log("Message test " + i++);
30+
new Thread(){
31+
@Override
32+
public void run() {
33+
DebugOverlay.with(MainActivity.this).log("Message test " + i++);
34+
}
35+
}.start();
3136
} else {
3237
requestOverlayPermission();
3338
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
jcenter()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:3.0.0'
9+
classpath 'com.android.tools.build:gradle:3.2.1'
1010

1111
// NOTE: Do not place your application dependencies here; they belong
1212
// in the individual module build.gradle files
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Mon Oct 12 17:23:28 CEST 2015
1+
#Mon Jan 07 23:15:08 CET 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip

noop/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 27
5-
buildToolsVersion "27.0.0"
5+
buildToolsVersion '28.0.3'
66

77
defaultConfig {
88
minSdkVersion 14

overlay/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 27
5-
buildToolsVersion "27.0.0"
5+
buildToolsVersion '28.0.3'
66

77
defaultConfig {
88
minSdkVersion 14

overlay/src/main/java/com/hannesdorfmann/debugoverlay/DebugOverlay.java

Lines changed: 106 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.content.ServiceConnection;
7+
import android.os.Handler;
78
import android.os.IBinder;
9+
import android.os.Looper;
810
import android.support.annotation.NonNull;
11+
912
import java.util.LinkedList;
1013
import java.util.Queue;
1114

@@ -14,108 +17,120 @@
1417
*/
1518
public class DebugOverlay {
1619

17-
private static DebugOverlay INSTANCE;
18-
private MessageDispatcher messageDispatcher;
19-
20-
private DebugOverlay(Context context) {
21-
Intent intent = new Intent(context, DebugOverlayService.class);
22-
23-
ServiceConnection serviceConnection = new ServiceConnection() {
24-
@Override public void onServiceConnected(ComponentName name, IBinder binder) {
25-
DebugOverlayService service =
26-
((DebugOverlayService.DebugOverlayServiceBinder) binder).getService();
27-
messageDispatcher.setService(service);
28-
}
29-
30-
@Override public void onServiceDisconnected(ComponentName name) {
31-
INSTANCE = null;
32-
}
33-
};
20+
private static DebugOverlay INSTANCE;
21+
private MessageDispatcher messageDispatcher;
22+
23+
private DebugOverlay(Context context) {
24+
Intent intent = new Intent(context, DebugOverlayService.class);
25+
26+
ServiceConnection serviceConnection = new ServiceConnection() {
27+
@Override
28+
public void onServiceConnected(ComponentName name, IBinder binder) {
29+
DebugOverlayService service =
30+
((DebugOverlayService.DebugOverlayServiceBinder) binder).getService();
31+
messageDispatcher.setService(service);
32+
}
33+
34+
@Override
35+
public void onServiceDisconnected(ComponentName name) {
36+
INSTANCE = null;
37+
}
38+
};
39+
40+
messageDispatcher = new MessageDispatcher();
41+
boolean bound = context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
42+
43+
if (!bound) {
44+
throw new RuntimeException(
45+
"Could not bind the Service " + DebugOverlayService.class.getSimpleName()
46+
+ " - Is Service declared in Android manifest and is Permission SYSTEM_ALERT_WINDOW granted?");
47+
}
48+
}
3449

35-
messageDispatcher = new MessageDispatcher();
36-
boolean bound = context.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
50+
public static DebugOverlay with(Context context) {
51+
if (INSTANCE == null) {
52+
INSTANCE = new DebugOverlay(context.getApplicationContext());
53+
}
3754

38-
if (!bound) {
39-
throw new RuntimeException(
40-
"Could not bind the Service " + DebugOverlayService.class.getSimpleName()
41-
+ " - Is Service declared in Android manifest and is Permission SYSTEM_ALERT_WINDOW granted?");
55+
return INSTANCE;
4256
}
43-
}
4457

45-
public static DebugOverlay with(Context context) {
46-
if (INSTANCE == null) {
47-
INSTANCE = new DebugOverlay(context.getApplicationContext());
58+
public DebugOverlay log(String msg) {
59+
messageDispatcher.enqueueMessage(msg);
60+
return this;
4861
}
4962

50-
return INSTANCE;
51-
}
52-
53-
public DebugOverlay log(String msg) {
54-
messageDispatcher.enqueueMessage(msg);
55-
return this;
56-
}
57-
58-
public DebugOverlay log(String fortmatedMsg, Object... paramters) {
59-
String msg = String.format(fortmatedMsg, paramters);
60-
log(msg);
61-
return this;
62-
}
63-
64-
/**
65-
* Dispatch Messages to the {@link DebugOverlayService} or store them in a queue if the service
66-
* is
67-
* not bound yet. Dispatch the queued messages once we are bound.
68-
*/
69-
private static class MessageDispatcher {
70-
71-
private DebugOverlayService service;
72-
private Queue<String> messageQueue = new LinkedList<>();
73-
74-
/**
75-
* DO NOT INVOKE THIS DIRECTLY. Should only be invoked from internally to establish a
76-
* connection
77-
* to the service
78-
*/
79-
void setService(@NonNull DebugOverlayService service) {
80-
if (service == null) {
81-
throw new NullPointerException(
82-
DebugOverlayService.class.getSimpleName() + " is null! That's not allowed");
83-
}
84-
this.service = service;
85-
if (!messageQueue.isEmpty()) {
86-
for (String msg : messageQueue) {
87-
dispatch(msg);
88-
}
89-
}
63+
public DebugOverlay log(String fortmatedMsg, Object... paramters) {
64+
String msg = String.format(fortmatedMsg, paramters);
65+
log(msg);
66+
return this;
9067
}
9168

9269
/**
93-
* Enqueue a message. Will be dispatched directly to the {@link DebugOverlayService} if service
94-
* is already bound or enqueued into a message queue and be dispatched once the {@link
95-
* DebugOverlayService} is connected
96-
*
97-
* @param msg The message to dispatch
70+
* Dispatch Messages to the {@link DebugOverlayService} or store them in a queue if the service
71+
* is
72+
* not bound yet. Dispatch the queued messages once we are bound.
9873
*/
99-
public void enqueueMessage(@NonNull String msg) {
100-
if (service != null) {
101-
dispatch(msg);
102-
} else {
103-
messageQueue.add(msg);
104-
}
105-
}
74+
private static class MessageDispatcher {
75+
76+
private DebugOverlayService service;
77+
private Queue<String> messageQueue = new LinkedList<>();
78+
private Handler mainThreadHandler = new Handler(Looper.getMainLooper());
79+
80+
/**
81+
* DO NOT INVOKE THIS DIRECTLY. Should only be invoked from internally to establish a
82+
* connection to the service
83+
*/
84+
private void setService(@NonNull DebugOverlayService service) {
85+
if (service == null) {
86+
throw new NullPointerException(
87+
DebugOverlayService.class.getSimpleName() + " is null! That's not allowed");
88+
}
89+
this.service = service;
90+
if (!messageQueue.isEmpty()) {
91+
for (String msg : messageQueue) {
92+
dispatchOnMainUiThread(msg);
93+
}
94+
}
95+
}
10696

107-
/**
108-
* Dispatch a message directly
109-
*
110-
* @param message The message to dispatch
111-
*/
112-
private void dispatch(String message) {
113-
if (service == null) {
114-
throw new NullPointerException(DebugOverlayService.class.getSimpleName()
115-
+ " is null, but this should never be the case");
116-
}
97+
/**
98+
* Enqueue a message. Will be dispatched directly to the {@link DebugOverlayService} if service
99+
* is already bound or enqueued into a message queue and be dispatched once the {@link
100+
* DebugOverlayService} is connected
101+
*
102+
* @param msg The message to dispatchOnMainUiThread
103+
*/
104+
public void enqueueMessage(@NonNull String msg) {
105+
if (service != null) {
106+
dispatchOnMainUiThread(msg);
107+
} else {
108+
messageQueue.add(msg);
109+
}
110+
}
117111

118-
service.logMsg(message);
112+
/**
113+
* Dispatch a message directly if already running on main UI Thread or post it on main UI Looper
114+
*
115+
* @param message The message to dispatchOnMainUiThread
116+
*/
117+
private void dispatchOnMainUiThread(String message) {
118+
if (service == null) {
119+
throw new NullPointerException(DebugOverlayService.class.getSimpleName()
120+
+ " is null, but this should never be the case");
121+
}
122+
123+
boolean isExecutingOnMainThread = Looper.myLooper() == Looper.getMainLooper();
124+
if (isExecutingOnMainThread) {
125+
service.logMsg(message);
126+
} else {
127+
mainThreadHandler.post(new Runnable() {
128+
@Override
129+
public void run() {
130+
service.logMsg(message);
131+
}
132+
});
133+
}
134+
}
119135
}
120-
}
121136
}

0 commit comments

Comments
 (0)