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,46 @@ 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+ //{"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 {
6191 ((JsV8InspectorWebSocket ) connection ).send (payload );
6292 }
6393
64- private static String getInspectorMessage ( Object connection )
65- {
94+ @ RuntimeCallable
95+ private static String getInspectorMessage ( Object connection ) {
6696 return ((JsV8InspectorWebSocket ) connection ).getInspectorMessage ();
6797 }
6898
69- class JsV8InspectorServer extends NanoWSD
70- {
71- public JsV8InspectorServer (String name )
72- {
99+ class JsV8InspectorServer extends NanoWSD {
100+ public JsV8InspectorServer (String name ) {
73101 super (name );
74102 }
75103
@@ -84,17 +112,14 @@ protected Response serveHttp(IHTTPSession session)
84112 }
85113
86114 @ Override
87- protected WebSocket openWebSocket (IHTTPSession handshake )
88- {
115+ protected WebSocket openWebSocket (IHTTPSession handshake ) {
89116 return new JsV8InspectorWebSocket (handshake );
90117 }
91118 }
92119
93- class JsV8InspectorWebSocket extends NanoWSD .WebSocket
94- {
120+ class JsV8InspectorWebSocket extends NanoWSD .WebSocket {
95121
96- public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest )
97- {
122+ public JsV8InspectorWebSocket (NanoHTTPD .IHTTPSession handshakeRequest ) {
98123 super (handshakeRequest );
99124 }
100125
@@ -106,8 +131,7 @@ protected void onOpen()
106131 Log .d ("V8Inspector" , "onOpen: ThreadID: " + Thread .currentThread ().getId ());
107132 }
108133
109- mainHandler .post (new Runnable ()
110- {
134+ mainHandler .post (new Runnable () {
111135 @ Override
112136 public void run ()
113137 {
@@ -153,14 +177,11 @@ protected void onMessage(final NanoWSD.WebSocketFrame message)
153177
154178 inspectorMessages .offer (message .getTextPayload ());
155179
156- mainHandler .post (new Runnable ()
157- {
180+ mainHandler .post (new Runnable () {
158181 @ Override
159- public void run ()
160- {
182+ public void run () {
161183 String nextMessage = inspectorMessages .poll ();
162- while (nextMessage != null )
163- {
184+ while (nextMessage != null ) {
164185 dispatchMessage (nextMessage );
165186 nextMessage = inspectorMessages .poll ();
166187 }
@@ -179,15 +200,11 @@ public void send(String payload) throws IOException
179200 super .send (payload );
180201 }
181202
182- public String getInspectorMessage ()
183- {
184- try
185- {
203+ public String getInspectorMessage () {
204+ try {
186205 String message = inspectorMessages .take ();
187206 return message ;
188- }
189- catch (InterruptedException e )
190- {
207+ } catch (InterruptedException e ) {
191208 e .printStackTrace ();
192209 }
193210
@@ -200,10 +217,9 @@ protected void onPong(NanoWSD.WebSocketFrame pong)
200217 }
201218
202219 @ Override
203- protected void onException (IOException exception )
204- {
220+ protected void onException (IOException exception ) {
205221 exception .printStackTrace ();
206222 disconnect ();
207223 }
208224 }
209- }
225+ }
0 commit comments