|
4 | 4 | import android.content.Context; |
5 | 5 | import android.content.Intent; |
6 | 6 | import android.content.ServiceConnection; |
| 7 | +import android.os.Handler; |
7 | 8 | import android.os.IBinder; |
| 9 | +import android.os.Looper; |
8 | 10 | import android.support.annotation.NonNull; |
| 11 | + |
9 | 12 | import java.util.LinkedList; |
10 | 13 | import java.util.Queue; |
11 | 14 |
|
|
14 | 17 | */ |
15 | 18 | public class DebugOverlay { |
16 | 19 |
|
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 | + } |
34 | 49 |
|
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 | + } |
37 | 54 |
|
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; |
42 | 56 | } |
43 | | - } |
44 | 57 |
|
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; |
48 | 61 | } |
49 | 62 |
|
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; |
90 | 67 | } |
91 | 68 |
|
92 | 69 | /** |
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. |
98 | 73 | */ |
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 | + } |
106 | 96 |
|
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 | + } |
117 | 111 |
|
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 | + } |
119 | 135 | } |
120 | | - } |
121 | 136 | } |
0 commit comments