4
4
import android .os .Handler ;
5
5
import android .util .Log ;
6
6
7
+ import org .json .JSONArray ;
8
+ import org .json .JSONException ;
9
+ import org .json .JSONObject ;
10
+
7
11
import java .io .IOException ;
8
12
import java .util .concurrent .ConcurrentLinkedQueue ;
9
13
import java .util .concurrent .LinkedBlockingQueue ;
@@ -27,19 +31,17 @@ public class AndroidJsV8Inspector
27
31
protected static native final void disconnect ();
28
32
29
33
protected native final void dispatchMessage (String message );
34
+
30
35
protected Handler mainHandler ;
31
36
private LinkedBlockingQueue <String > inspectorMessages = new LinkedBlockingQueue <String >();
32
37
33
- public AndroidJsV8Inspector (Context context , Logger logger )
34
- {
38
+ public AndroidJsV8Inspector (Context context , Logger logger ) {
35
39
this .context = context ;
36
40
this .logger = logger ;
37
41
}
38
42
39
- public void start () throws IOException
40
- {
41
- if (this .server == null )
42
- {
43
+ public void start () throws IOException {
44
+ if (this .server == null ) {
43
45
Runtime currentRuntime = Runtime .getCurrentRuntime ();
44
46
45
47
mainHandler = currentRuntime .getHandler ();
@@ -56,20 +58,45 @@ public void start() throws IOException
56
58
}
57
59
}
58
60
59
- private static void send (Object connection , String payload ) throws IOException
60
- {
61
+ @ RuntimeCallable
62
+ private static void sendToDevToolsConsole (Object connection , String message , String level ) {
63
+ try {
64
+ JSONObject consoleMessage = new JSONObject ();
65
+
66
+ JSONObject params = new JSONObject ();
67
+ params .put ("type" , level );
68
+ params .put ("executionContextId" , 0 );
69
+ params .put ("timestamp" , 0.000000000000000 );
70
+
71
+ JSONArray args = new JSONArray ();
72
+ args .put (message );
73
+ params .put ("args" , args );
74
+
75
+ consoleMessage .put ("method" , "Runtime.consoleAPICalled" );
76
+ consoleMessage .put ("params" , params );
77
+
78
+ String sendingText = consoleMessage .toString ();
79
+ AndroidJsV8Inspector .send (connection , sendingText );
80
+
81
+ } catch (JSONException e ) {
82
+ e .printStackTrace ();
83
+ } catch (IOException e ) {
84
+ e .printStackTrace ();
85
+ }
86
+ }
87
+
88
+ @ RuntimeCallable
89
+ private static void send (Object connection , String payload ) throws IOException {
61
90
((JsV8InspectorWebSocket ) connection ).send (payload );
62
91
}
63
92
64
- private static String getInspectorMessage ( Object connection )
65
- {
93
+ @ RuntimeCallable
94
+ private static String getInspectorMessage ( Object connection ) {
66
95
return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
67
96
}
68
97
69
- class JsV8InspectorServer extends NanoWSD
70
- {
71
- public JsV8InspectorServer (String name )
72
- {
98
+ class JsV8InspectorServer extends NanoWSD {
99
+ public JsV8InspectorServer (String name ) {
73
100
super (name );
74
101
}
75
102
@@ -84,17 +111,14 @@ protected Response serveHttp(IHTTPSession session)
84
111
}
85
112
86
113
@ Override
87
- protected WebSocket openWebSocket (IHTTPSession handshake )
88
- {
114
+ protected WebSocket openWebSocket (IHTTPSession handshake ) {
89
115
return new JsV8InspectorWebSocket (handshake );
90
116
}
91
117
}
92
118
93
- class JsV8InspectorWebSocket extends NanoWSD .WebSocket
94
- {
119
+ class JsV8InspectorWebSocket extends NanoWSD .WebSocket {
95
120
96
- public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest )
97
- {
121
+ public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest ) {
98
122
super (handshakeRequest );
99
123
}
100
124
@@ -106,8 +130,7 @@ protected void onOpen()
106
130
Log .d ("V8Inspector" , "onOpen: ThreadID: " + Thread .currentThread ().getId ());
107
131
}
108
132
109
- mainHandler .post (new Runnable ()
110
- {
133
+ mainHandler .post (new Runnable () {
111
134
@ Override
112
135
public void run ()
113
136
{
@@ -153,14 +176,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153
176
154
177
inspectorMessages .offer (message .getTextPayload ());
155
178
156
- mainHandler .post (new Runnable ()
157
- {
179
+ mainHandler .post (new Runnable () {
158
180
@ Override
159
- public void run ()
160
- {
181
+ public void run () {
161
182
String nextMessage = inspectorMessages .poll ();
162
- while (nextMessage != null )
163
- {
183
+ while (nextMessage != null ) {
164
184
dispatchMessage (nextMessage );
165
185
nextMessage = inspectorMessages .poll ();
166
186
}
@@ -179,15 +199,11 @@ public void send(String payload) throws IOException
179
199
super .send (payload );
180
200
}
181
201
182
- public String getInspectorMessage ()
183
- {
184
- try
185
- {
202
+ public String getInspectorMessage () {
203
+ try {
186
204
String message = inspectorMessages .take ();
187
205
return message ;
188
- }
189
- catch (InterruptedException e )
190
- {
206
+ } catch (InterruptedException e ) {
191
207
e .printStackTrace ();
192
208
}
193
209
@@ -200,10 +216,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200
216
}
201
217
202
218
@ Override
203
- protected void onException (IOException exception )
204
- {
219
+ protected void onException (IOException exception ) {
205
220
exception .printStackTrace ();
206
221
disconnect ();
207
222
}
208
223
}
209
- }
224
+ }
0 commit comments