Skip to content

Commit

Permalink
* [android] refresh the instance when reload called by the debug server
Browse files Browse the repository at this point in the history
android/playground/app/src/main/java/com/alibaba/weex/WXPageActivity.java
  • Loading branch information
budao committed Jul 5, 2016
1 parent 5789700 commit fd2be06
Show file tree
Hide file tree
Showing 5 changed files with 291 additions and 233 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.taobao.weex.bridge.WXJSObject;
import com.taobao.weex.bridge.WXParams;
import com.taobao.weex.common.IWXBridge;
import com.taobao.weex.common.IWXDebugProxy;
import com.taobao.weex.devtools.websocket.SimpleSession;

import java.util.ArrayList;
Expand All @@ -27,7 +28,7 @@ public class DebugBridge implements IWXBridge {
private Object mLock = new Object();
private WXBridgeManager mJsManager;
private SimpleSession mSession;
private MyBroadcastReceiver mReceiver;
private ConnectionBroadcastReceiver mReceiver;

private DebugBridge() {

Expand Down Expand Up @@ -156,15 +157,15 @@ public void reportJSException(String instanceId, String func, String exception)
}
}

public class MyBroadcastReceiver extends BroadcastReceiver {
public class ConnectionBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (DebugServerProxy.ACTION_DEBUG_SERVER_CONNECTED.equals(intent.getAction())) {
if (IWXDebugProxy.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())) {
} else if (IWXDebugProxy.ACTION_DEBUG_SERVER_CONNECT_FAILED.equals(intent.getAction())) {
Log.v(TAG, "connect to debug server failed");
synchronized (mLock) {
mLock.notify();
Expand All @@ -174,10 +175,10 @@ public void onReceive(Context context, Intent intent) {
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@

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 @@ -225,5 +223,4 @@ private void handleRemoteResponse(JsonRpcPeer peer, JSONObject responseNode)
pendingRequest.callback.onResponse(peer, response);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.taobao.weex.devtools.inspector.protocol.module;

import android.content.Context;
import android.content.Intent;

import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKEngine;
import com.taobao.weex.common.IWXDebugProxy;
import com.taobao.weex.devtools.debug.DebugBridge;
import com.taobao.weex.devtools.inspector.jsonrpc.JsonRpcPeer;
import com.taobao.weex.devtools.inspector.protocol.ChromeDevtoolsDomain;
Expand Down Expand Up @@ -50,6 +55,10 @@ public void callNative(JsonRpcPeer peer, JSONObject params) {
@ChromeDevtoolsMethod
public void reload(JsonRpcPeer peer, JSONObject params) {
WXSDKEngine.reload();
Context context = WXEnvironment.getApplication();
if (context != null) {
context.sendBroadcast(new Intent(IWXDebugProxy.ACTION_DEBUG_INSTANCE_REFRESH));
}
}

public static class CallNative {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Bundle;
Expand All @@ -24,6 +28,7 @@
import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.common.IWXDebugProxy;
import com.taobao.weex.common.WXRenderStrategy;
import com.taobao.weex.utils.WXFileUtils;
import com.taobao.weex.utils.WXLogUtils;
Expand All @@ -44,6 +49,8 @@ public class WXPageActivity extends WXBaseNavActivity implements IWXRenderListen
private View mWAView;

private Handler mWXHandler;
private BroadcastReceiver mReceiver;


private Uri mUri;
private HashMap mConfigMap = new HashMap<String, Object>();
Expand All @@ -63,6 +70,7 @@ protected void onCreate(Bundle savedInstanceState) {
rendWXPage();
setCurrentWxPageActivity(this);
mInstance.onActivityCreate();
registerBroadcastReceiver();
}

private void getUri() {
Expand Down Expand Up @@ -108,28 +116,35 @@ private void rendWXPage() {
loadWXfromService(url);
startHotRefresh();
} else {
if (mInstance == null) {
mInstance = new WXSDKInstance(this);
loadWXfromLocal(false);
}
}

// mInstance.setImgLoaderAdapter(new ImageAdapter(this));
mInstance.registerRenderListener(this);
}
mContainer.post(new Runnable() {
@Override
public void run() {
Activity ctx = WXPageActivity.this;
Rect outRect = new Rect();
ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
mConfigMap.put("bundleUrl", mUri.toString());
String path = mUri.getScheme().equals("file") ? assembleFilePath(mUri) : mUri.toString();
mInstance.render(TAG, WXFileUtils.loadFileContent(path, WXPageActivity.this),
mConfigMap, null,
ScreenUtil.getDisplayWidth(WXPageActivity.this), ScreenUtil
.getDisplayHeight(WXPageActivity.this),
WXRenderStrategy.APPEND_ASYNC);
}
});
private void loadWXfromLocal(boolean reload) {
if (reload && mInstance != null) {
mInstance.destroy();
mInstance = null;
}
if (mInstance == null) {
mInstance = new WXSDKInstance(this);
// mInstance.setImgLoaderAdapter(new ImageAdapter(this));
mInstance.registerRenderListener(this);
}
mContainer.post(new Runnable() {
@Override
public void run() {
Activity ctx = WXPageActivity.this;
Rect outRect = new Rect();
ctx.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
mConfigMap.put("bundleUrl", mUri.toString());
String path = mUri.getScheme().equals("file") ? assembleFilePath(mUri) : mUri.toString();
mInstance.render(TAG, WXFileUtils.loadFileContent(path, WXPageActivity.this),
mConfigMap, null,
ScreenUtil.getDisplayWidth(WXPageActivity.this), ScreenUtil
.getDisplayHeight(WXPageActivity.this),
WXRenderStrategy.APPEND_ASYNC);
}
});
}

private String assembleFilePath(Uri uri) {
Expand Down Expand Up @@ -204,6 +219,8 @@ private void addOnListener() {
@Override
protected void onDestroy() {
super.onDestroy();
unregisterBroadcastReceiver();

}

@Override
Expand Down Expand Up @@ -292,4 +309,34 @@ private void degradeAlert(String errMsg) {
.show();

}

public class RefreshBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (IWXDebugProxy.ACTION_DEBUG_INSTANCE_REFRESH.equals(intent.getAction())) {
Log.v(TAG, "connect to debug server success");
if (mUri != null) {
if (TextUtils.equals(mUri.getScheme(), "http") || TextUtils.equals(mUri.getScheme(), "https")) {
loadWXfromService(mUri.toString());
} else {
loadWXfromLocal(true);
}
}
}
}
}

private void registerBroadcastReceiver() {
mReceiver = new RefreshBroadcastReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(IWXDebugProxy.ACTION_DEBUG_INSTANCE_REFRESH);
registerReceiver(mReceiver, filter);
}

private void unregisterBroadcastReceiver() {
if (mReceiver != null) {
unregisterReceiver(mReceiver);
}
}

}
Loading

0 comments on commit fd2be06

Please sign in to comment.