44import android .os .Handler ;
55import android .util .Log ;
66
7+ import org .json .JSONArray ;
8+ import org .json .JSONException ;
9+ import org .json .JSONObject ;
10+
711import java .io .IOException ;
812import java .util .concurrent .ConcurrentLinkedQueue ;
913import java .util .concurrent .LinkedBlockingQueue ;
@@ -27,19 +31,17 @@ public class AndroidJsV8Inspector
2731 protected static native final void disconnect ();
2832
2933 protected native final void dispatchMessage (String message );
34+
3035 protected Handler mainHandler ;
3136 private LinkedBlockingQueue <String > inspectorMessages = new LinkedBlockingQueue <String >();
3237
33- public AndroidJsV8Inspector (Context context , Logger logger )
34- {
38+ public AndroidJsV8Inspector (Context context , Logger logger ) {
3539 this .context = context ;
3640 this .logger = logger ;
3741 }
3842
39- public void start () throws IOException
40- {
41- if (this .server == null )
42- {
43+ public void start () throws IOException {
44+ if (this .server == null ) {
4345 Runtime currentRuntime = Runtime .getCurrentRuntime ();
4446
4547 mainHandler = currentRuntime .getHandler ();
@@ -56,20 +58,45 @@ public void start() throws IOException
5658 }
5759 }
5860
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 {
6190 ((JsV8InspectorWebSocket ) connection ).send (payload );
6291 }
6392
64- private static String getInspectorMessage ( Object connection )
65- {
93+ @ RuntimeCallable
94+ private static String getInspectorMessage ( Object connection ) {
6695 return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
6796 }
6897
69- class JsV8InspectorServer extends NanoWSD
70- {
71- public JsV8InspectorServer (String name )
72- {
98+ class JsV8InspectorServer extends NanoWSD {
99+ public JsV8InspectorServer (String name ) {
73100 super (name );
74101 }
75102
@@ -84,17 +111,14 @@ protected Response serveHttp(IHTTPSession session)
84111 }
85112
86113 @ Override
87- protected WebSocket openWebSocket (IHTTPSession handshake )
88- {
114+ protected WebSocket openWebSocket (IHTTPSession handshake ) {
89115 return new JsV8InspectorWebSocket (handshake );
90116 }
91117 }
92118
93- class JsV8InspectorWebSocket extends NanoWSD .WebSocket
94- {
119+ class JsV8InspectorWebSocket extends NanoWSD .WebSocket {
95120
96- public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest )
97- {
121+ public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest ) {
98122 super (handshakeRequest );
99123 }
100124
@@ -106,8 +130,7 @@ protected void onOpen()
106130 Log .d ("V8Inspector" , "onOpen: ThreadID: " + Thread .currentThread ().getId ());
107131 }
108132
109- mainHandler .post (new Runnable ()
110- {
133+ mainHandler .post (new Runnable () {
111134 @ Override
112135 public void run ()
113136 {
@@ -153,14 +176,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153176
154177 inspectorMessages .offer (message .getTextPayload ());
155178
156- mainHandler .post (new Runnable ()
157- {
179+ mainHandler .post (new Runnable () {
158180 @ Override
159- public void run ()
160- {
181+ public void run () {
161182 String nextMessage = inspectorMessages .poll ();
162- while (nextMessage != null )
163- {
183+ while (nextMessage != null ) {
164184 dispatchMessage (nextMessage );
165185 nextMessage = inspectorMessages .poll ();
166186 }
@@ -179,15 +199,11 @@ public void send(String payload) throws IOException
179199 super .send (payload );
180200 }
181201
182- public String getInspectorMessage ()
183- {
184- try
185- {
202+ public String getInspectorMessage () {
203+ try {
186204 String message = inspectorMessages .take ();
187205 return message ;
188- }
189- catch (InterruptedException e )
190- {
206+ } catch (InterruptedException e ) {
191207 e .printStackTrace ();
192208 }
193209
@@ -200,10 +216,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200216 }
201217
202218 @ Override
203- protected void onException (IOException exception )
204- {
219+ protected void onException (IOException exception ) {
205220 exception .printStackTrace ();
206221 disconnect ();
207222 }
208223 }
209- }
224+ }
0 commit comments