Implement bundle sync status

Reviewed By: pakoito

Differential Revision: D6807480

fbshipit-source-id: d71f2cecd882c47e79bb71dfb9d03d3597fa4068
This commit is contained in:
Ray Shih
2018-02-01 06:13:19 -08:00
committed by Facebook Github Bot
parent 9f57dedc17
commit 88980f2ef7
7 changed files with 99 additions and 8 deletions

View File

@@ -116,9 +116,15 @@ public class DevServerHelper {
private @Nullable InspectorPackagerConnection mInspectorPackagerConnection;
private @Nullable OkHttpClient mOnChangePollingClient;
private @Nullable OnServerContentChangeListener mOnServerContentChangeListener;
private InspectorPackagerConnection.BundleStatusProvider mBundlerStatusProvider;
public DevServerHelper(DevInternalSettings settings, String packageName) {
public DevServerHelper(
DevInternalSettings settings,
String packageName,
InspectorPackagerConnection.BundleStatusProvider bundleStatusProvider
) {
mSettings = settings;
mBundlerStatusProvider = bundleStatusProvider;
mClient = new OkHttpClient.Builder()
.connectTimeout(HTTP_CONNECT_TIMEOUT_MS, TimeUnit.MILLISECONDS)
.readTimeout(0, TimeUnit.MILLISECONDS)
@@ -212,7 +218,11 @@ public class DevServerHelper {
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
mInspectorPackagerConnection = new InspectorPackagerConnection(getInspectorDeviceUrl(), mPackageName);
mInspectorPackagerConnection = new InspectorPackagerConnection(
getInspectorDeviceUrl(),
mPackageName,
mBundlerStatusProvider
);
mInspectorPackagerConnection.connect();
return null;
}

View File

@@ -47,6 +47,7 @@ import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.ShakeDetector;
import com.facebook.react.common.futures.SimpleSettableFuture;
import com.facebook.react.devsupport.DevServerHelper.PackagerCommandListener;
import com.facebook.react.devsupport.InspectorPackagerConnection;
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener;
import com.facebook.react.devsupport.interfaces.DevOptionHandler;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
@@ -156,6 +157,8 @@ public class DevSupportManagerImpl implements
private @Nullable DevBundleDownloadListener mBundleDownloadListener;
private @Nullable List<ErrorCustomizer> mErrorCustomizers;
private InspectorPackagerConnection.BundleStatus mBundleStatus;
private static class JscProfileTask extends AsyncTask<String, Void, Void> {
private static final MediaType JSON =
MediaType.parse("application/json; charset=utf-8");
@@ -217,7 +220,17 @@ public class DevSupportManagerImpl implements
mApplicationContext = applicationContext;
mJSAppBundleName = packagerPathForJSBundleName;
mDevSettings = new DevInternalSettings(applicationContext, this);
mDevServerHelper = new DevServerHelper(mDevSettings, mApplicationContext.getPackageName());
mBundleStatus = new InspectorPackagerConnection.BundleStatus();
mDevServerHelper = new DevServerHelper(
mDevSettings,
mApplicationContext.getPackageName(),
new InspectorPackagerConnection.BundleStatusProvider() {
@Override
public InspectorPackagerConnection.BundleStatus getBundleStatus() {
return mBundleStatus;
}
}
);
mBundleDownloadListener = devBundleDownloadListener;
// Prepare shake gesture detector (will be started/stopped from #reload)
@@ -1032,6 +1045,10 @@ public class DevSupportManagerImpl implements
public void onSuccess() {
mDevLoadingViewController.hide();
mDevLoadingViewVisible = false;
synchronized (DevSupportManagerImpl.this) {
mBundleStatus.isLastDownloadSucess = true;
mBundleStatus.updateTimestamp = System.currentTimeMillis();
}
if (mBundleDownloadListener != null) {
mBundleDownloadListener.onSuccess();
}
@@ -1057,6 +1074,9 @@ public class DevSupportManagerImpl implements
public void onFailure(final Exception cause) {
mDevLoadingViewController.hide();
mDevLoadingViewVisible = false;
synchronized (DevSupportManagerImpl.this) {
mBundleStatus.isLastDownloadSucess = false;
}
if (mBundleDownloadListener != null) {
mBundleDownloadListener.onFailure(cause);
}

View File

@@ -8,7 +8,6 @@ import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import android.os.AsyncTask;
@@ -33,11 +32,17 @@ public class InspectorPackagerConnection {
private final Connection mConnection;
private final Map<String, Inspector.LocalConnection> mInspectorConnections;
private final String mPackageName;
private BundleStatusProvider mBundleStatusProvider;
public InspectorPackagerConnection(String url, String packageName) {
public InspectorPackagerConnection(
String url,
String packageName,
BundleStatusProvider bundleStatusProvider
) {
mConnection = new Connection(url);
mInspectorConnections = new HashMap<>();
mPackageName = packageName;
mBundleStatusProvider = bundleStatusProvider;
}
public void connect() {
@@ -143,11 +148,14 @@ public class InspectorPackagerConnection {
private JSONArray getPages() throws JSONException {
List<Inspector.Page> pages = Inspector.getPages();
JSONArray array = new JSONArray();
BundleStatus bundleStatus = mBundleStatusProvider.getBundleStatus();
for (Inspector.Page page : pages) {
JSONObject jsonPage = new JSONObject();
jsonPage.put("id", String.valueOf(page.getId()));
jsonPage.put("title", page.getTitle());
jsonPage.put("app", mPackageName);
jsonPage.put("isLastBundleDownloadSuccess", bundleStatus.isLastDownloadSucess);
jsonPage.put("bundleUpdateTimestamp", bundleStatus.updateTimestamp);
array.put(jsonPage);
}
return array;
@@ -306,4 +314,25 @@ public class InspectorPackagerConnection {
}
}
}
static public class BundleStatus {
public Boolean isLastDownloadSucess;
public long updateTimestamp = -1;
public BundleStatus(
Boolean isLastDownloadSucess,
long updateTimestamp
) {
this.isLastDownloadSucess = isLastDownloadSucess;
this.updateTimestamp = updateTimestamp;
}
public BundleStatus() {
this(false, -1);
}
}
public interface BundleStatusProvider {
public BundleStatus getBundleStatus();
}
}