Skip to content

Commit

Permalink
* [android] fix problem that session is null and init framework failed
Browse files Browse the repository at this point in the history
  • Loading branch information
budao committed Jun 27, 2016
1 parent 28741da commit b985d59
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 41 deletions.
@@ -1,7 +1,9 @@
package com.taobao.weex.devtools.debug;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

import com.alibaba.fastjson.JSON;
Expand All @@ -22,8 +24,10 @@
public class DebugBridge implements IWXBridge {
private static final String TAG = "DebugBridge";
private static volatile DebugBridge sInstance;
private Object mLock = new Object();
private WXBridgeManager mJsManager;
private SimpleSession mSession;
private MyBroadcastReceiver mReceiver;

private DebugBridge() {

Expand All @@ -43,6 +47,7 @@ public static DebugBridge getInstance() {

public void setSession(SimpleSession session) {
mSession = session;
registerBroadcastReceiver();
}

public void setBridgeManager(WXBridgeManager bridgeManager) {
Expand All @@ -51,31 +56,24 @@ public void setBridgeManager(WXBridgeManager bridgeManager) {

@Override
public int initFramework(String framework, WXParams params) {
if (mSession == null || (mSession != null && !mSession.isOpen())) {
return -1;
while (mSession == null || (mSession != null && !mSession.isOpen())) {
synchronized (mLock) {
try {
Log.v(TAG, "waiting for session now");
mLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
unregisterBroadcastReceiver();

mSession.sendText(getShakeHandsMessage());
Log.e(TAG, "ShakeHands");
mSession.sendText(getInitFrameworkMessage(framework));
Log.e(TAG, "InitFrameworkMessage");
return 1;
}

private String getShakeHandsMessage() {
Map<String, Object> func = new HashMap<>();
func.put("name", WXEnvironment.getApplication().getPackageName());
func.put("model", WXEnvironment.SYS_MODEL);
func.put("weexVersion", WXEnvironment.WXSDK_VERSION);
func.put("platform", WXEnvironment.OS);
func.put("deviceId",((TelephonyManager) WXEnvironment.getApplication().getSystemService(Context.TELEPHONY_SERVICE))
.getDeviceId());
if (mSession != null && mSession.isOpen()) {
mSession.sendText(getInitFrameworkMessage(framework));
return 1;
}

Map<String, Object> map = new HashMap<>();
map.put("id", "0");
map.put("method", "WxDebug.registerDevice");
map.put("params", func);
return JSON.toJSONString(map);
return 0;
}

private String getInitFrameworkMessage(String framework) {
Expand Down Expand Up @@ -134,4 +132,35 @@ public void reportJSException(String instanceId, String func, String exception)
mJsManager.reportJSException(instanceId, func, exception);
}
}

public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DebugServerProxy.ACTION_DEBUG_SERVER_CONNECTED.equals(intent.getAction())) {
Log.v(TAG, "connect to debug server success");
synchronized (mLock) {
mLock.notify();
}
} else if (DebugServerProxy.ACTION_DEBUG_SERVER_CONNECT_FAILED.equals(intent.getAction())) {
Log.v(TAG, "connect to debug server failed");
synchronized (mLock) {
mLock.notify();
}
}
}
}

private void registerBroadcastReceiver() {
mReceiver = new MyBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(DebugServerProxy.ACTION_DEBUG_SERVER_CONNECTED);
filter.addAction(DebugServerProxy.ACTION_DEBUG_SERVER_CONNECT_FAILED);
WXEnvironment.getApplication().registerReceiver(mReceiver, filter);
}

private void unregisterBroadcastReceiver() {
if (mReceiver != null) {
WXEnvironment.getApplication().unregisterReceiver(mReceiver);
}
}
}
@@ -1,10 +1,13 @@
package com.taobao.weex.devtools.debug;

import android.content.Context;
import android.content.Intent;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.squareup.okhttp.ws.WebSocket;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKManager;
Expand All @@ -31,11 +34,15 @@
import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okio.BufferedSource;

public class DebugServerProxy implements IWXDebugProxy {
private static final String TAG = "DebugServerProxy";
public static final String ACTION_DEBUG_SERVER_CONNECTED = "DEBUG_SERVER_CONNECTED";
public static final String ACTION_DEBUG_SERVER_CONNECT_FAILED = "DEBUG_SERVER_CONNECT_FAILED";
private DebugSocketClient mWebSocketClient;
private ObjectMapper mObjectMapper = new ObjectMapper();
private MethodDispatcher mMethodDispatcher;
Expand Down Expand Up @@ -71,11 +78,28 @@ public void start() {
mBridge.setBridgeManager(mJsManager);
mWebSocketClient.connect(mRemoteUrl, new DebugSocketClient.Callback() {

private String getShakeHandsMessage() {
Map<String, Object> func = new HashMap<>();
func.put("name", WXEnvironment.getApplication().getPackageName());
func.put("model", WXEnvironment.SYS_MODEL);
func.put("weexVersion", WXEnvironment.WXSDK_VERSION);
func.put("platform", WXEnvironment.OS);
func.put("deviceId", ((TelephonyManager) WXEnvironment.getApplication()
.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());

Map<String, Object> map = new HashMap<>();
map.put("id", "0");
map.put("method", "WxDebug.registerDevice");
map.put("params", func);
return JSON.toJSONString(map);
}

@Override
public void onSuccess(String response) {
mWebSocketClient.sendText(getShakeHandsMessage());
mContext.sendBroadcast(new Intent(ACTION_DEBUG_SERVER_CONNECTED));
mDomainModules = new WeexInspector.DefaultInspectorModulesBuilder(mContext).finish();
mMethodDispatcher = new MethodDispatcher(mObjectMapper, mDomainModules);

WXSDKManager.getInstance().postOnUiThread(
new Runnable() {

Expand All @@ -95,6 +119,7 @@ public void run() {

@Override
public void onFailure(Throwable cause) {
mContext.sendBroadcast(new Intent(ACTION_DEBUG_SERVER_CONNECT_FAILED));
WXLogUtils.d("connect debugger server failure!! " + cause.toString());
}

Expand Down Expand Up @@ -122,7 +147,6 @@ public void handleMessage(BufferedSource payload, WebSocket.PayloadType type) th
handleRemoteMessage(mPeer, message);
}
}
Log.v(TAG, "onMessage " + message);
} catch (Exception e) {

} finally {
Expand All @@ -137,7 +161,6 @@ public void handleMessage(BufferedSource payload, WebSocket.PayloadType type) th
}

private void handleDebugMessage(String message) {
Log.v(TAG, "onMessage " + message);
com.alibaba.fastjson.JSONObject jsonObject = com.alibaba.fastjson.JSONObject.parseObject(message);

String method = jsonObject.getString("method");
Expand All @@ -146,15 +169,13 @@ private void handleDebugMessage(String message) {
String instance = params.getString("instance");
String callback = params.getString("callback");
String tasks = params.getString("tasks");
Log.v(TAG, "WxDebug.callNative " + tasks);
mBridge.callNative(instance, tasks, callback);
}
}

private void handleRemoteMessage(JsonRpcPeer peer, String message)
throws IOException, MessageHandlingException, JSONException {
// Parse as a generic JSONObject first since we don't know if this is a request or response.
Log.v(TAG, "handleRemoteMessage : " + message);
JSONObject messageNode = new JSONObject(message);
if (messageNode.has("method")) {
handleRemoteRequest(peer, messageNode);
Expand Down Expand Up @@ -198,7 +219,6 @@ private void handleRemoteRequest(JsonRpcPeer peer, JSONObject requestNode)
jsonObject = mObjectMapper.convertValue(response, JSONObject.class);
responseString = jsonObject.toString();
}
Log.v(TAG, "handleRemoteRequest : " + responseString);
peer.getWebSocket().sendText(responseString);
}
}
Expand All @@ -215,7 +235,6 @@ private void handleRemoteResponse(JsonRpcPeer peer, JSONObject responseNode)
if (pendingRequest.callback != null) {
pendingRequest.callback.onResponse(peer, response);
}
Log.v(TAG, "handleRemoteResponse : " + responseNode.toString());
}

}
@@ -1,5 +1,6 @@
package com.taobao.weex.devtools.debug;

import android.text.TextUtils;
import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
Expand Down Expand Up @@ -43,9 +44,11 @@ public void connect(String url, Callback callback) {
// Disable timeouts for read
mHttpClient.setReadTimeout(0, TimeUnit.MINUTES);

Request request = new Request.Builder().url(url).build();
WebSocketCall call = WebSocketCall.create(mHttpClient, request);
call.enqueue(this);
if (!TextUtils.isEmpty(url)) {
Request request = new Request.Builder().url(url).build();
WebSocketCall call = WebSocketCall.create(mHttpClient, request);
call.enqueue(this);
}
}

public void closeQuietly() {
Expand Down Expand Up @@ -112,11 +115,6 @@ public void onFailure(IOException e) {
@Override
public void onOpen(WebSocket webSocket, Request arg1, Response arg2)
throws IOException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mWebSocket = webSocket;
if (mConnectCallback != null) {
mConnectCallback.onSuccess(null);
Expand Down
Expand Up @@ -11,7 +11,7 @@
import com.taobao.weex.common.WXException;

public class WXApplication extends Application {
public static String CURRENT_IP = "30.30.29.246"; // "your_current_IP"
public static String CURRENT_IP = "your_current_IP"; // "30.30.29.246";

@Override
public void onCreate() {
Expand All @@ -35,7 +35,10 @@ public void onCreate() {
}

private void initDebugEnv(boolean enable, String host) {
WXEnvironment.sDebugMode = enable;
WXEnvironment.sDebugWsUrl = "ws://" + host + ":8088/debugProxy/native";
if (!"your_current_IP".equals(host)) {
WXEnvironment.sDebugMode = enable;
WXEnvironment.sDebugWsUrl = "ws://" + host + ":8088/debugProxy/native";
}
}

}

0 comments on commit b985d59

Please sign in to comment.