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,46 @@ 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
+ //{"method":"Runtime.consoleAPICalled","params":{"type":"log","args":["asdjasdkljasd"],"executionContextId":0,"timestamp":0.000000000000000}}
64
+ try {
65
+ JSONObject consoleMessage = new JSONObject ();
66
+
67
+ JSONObject params = new JSONObject ();
68
+ params .put ("type" , level );
69
+ params .put ("executionContextId" , 0 );
70
+ params .put ("timestamp" , 0.000000000000000 );
71
+
72
+ JSONArray args = new JSONArray ();
73
+ args .put (message );
74
+ params .put ("args" , args );
75
+
76
+ consoleMessage .put ("method" , "Runtime.consoleAPICalled" );
77
+ consoleMessage .put ("params" , params );
78
+
79
+ String sendingText = consoleMessage .toString ();
80
+ AndroidJsV8Inspector .send (connection , sendingText );
81
+
82
+ } catch (JSONException e ) {
83
+ e .printStackTrace ();
84
+ } catch (IOException e ) {
85
+ e .printStackTrace ();
86
+ }
87
+ }
88
+
89
+ @ RuntimeCallable
90
+ private static void send (Object connection , String payload ) throws IOException {
61
91
((JsV8InspectorWebSocket ) connection ).send (payload );
62
92
}
63
93
64
- private static String getInspectorMessage ( Object connection )
65
- {
94
+ @ RuntimeCallable
95
+ private static String getInspectorMessage ( Object connection ) {
66
96
return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
67
97
}
68
98
69
- class JsV8InspectorServer extends NanoWSD
70
- {
71
- public JsV8InspectorServer (String name )
72
- {
99
+ class JsV8InspectorServer extends NanoWSD {
100
+ public JsV8InspectorServer (String name ) {
73
101
super (name );
74
102
}
75
103
@@ -84,17 +112,14 @@ protected Response serveHttp(IHTTPSession session)
84
112
}
85
113
86
114
@ Override
87
- protected WebSocket openWebSocket (IHTTPSession handshake )
88
- {
115
+ protected WebSocket openWebSocket (IHTTPSession handshake ) {
89
116
return new JsV8InspectorWebSocket (handshake );
90
117
}
91
118
}
92
119
93
- class JsV8InspectorWebSocket extends NanoWSD .WebSocket
94
- {
120
+ class JsV8InspectorWebSocket extends NanoWSD .WebSocket {
95
121
96
- public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest )
97
- {
122
+ public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest ) {
98
123
super (handshakeRequest );
99
124
}
100
125
@@ -106,8 +131,7 @@ protected void onOpen()
106
131
Log .d ("V8Inspector" , "onOpen: ThreadID: " + Thread .currentThread ().getId ());
107
132
}
108
133
109
- mainHandler .post (new Runnable ()
110
- {
134
+ mainHandler .post (new Runnable () {
111
135
@ Override
112
136
public void run ()
113
137
{
@@ -153,14 +177,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153
177
154
178
inspectorMessages .offer (message .getTextPayload ());
155
179
156
- mainHandler .post (new Runnable ()
157
- {
180
+ mainHandler .post (new Runnable () {
158
181
@ Override
159
- public void run ()
160
- {
182
+ public void run () {
161
183
String nextMessage = inspectorMessages .poll ();
162
- while (nextMessage != null )
163
- {
184
+ while (nextMessage != null ) {
164
185
dispatchMessage (nextMessage );
165
186
nextMessage = inspectorMessages .poll ();
166
187
}
@@ -179,15 +200,11 @@ public void send(String payload) throws IOException
179
200
super .send (payload );
180
201
}
181
202
182
- public String getInspectorMessage ()
183
- {
184
- try
185
- {
203
+ public String getInspectorMessage () {
204
+ try {
186
205
String message = inspectorMessages .take ();
187
206
return message ;
188
- }
189
- catch (InterruptedException e )
190
- {
207
+ } catch (InterruptedException e ) {
191
208
e .printStackTrace ();
192
209
}
193
210
@@ -200,10 +217,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200
217
}
201
218
202
219
@ Override
203
- protected void onException (IOException exception )
204
- {
220
+ protected void onException (IOException exception ) {
205
221
exception .printStackTrace ();
206
222
disconnect ();
207
223
}
208
224
}
209
- }
225
+ }
0 commit comments