Fix DevLoadingViewController

Reviewed By: mdvacca

Differential Revision: D7860219

fbshipit-source-id: 3770a2064808bf4ae4c61f15c6d68c3b93fe9f38
This commit is contained in:
Andrew Chen (Eng)
2018-05-03 13:01:28 -07:00
committed by Facebook Github Bot
parent 786c1ecc2a
commit 5a6147c4f8
3 changed files with 48 additions and 55 deletions

View File

@@ -7,28 +7,25 @@
package com.facebook.react.devsupport;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Build;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.PopupWindow;
import android.widget.TextView;
import com.facebook.common.logging.FLog;
import com.facebook.react.R;
import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.common.ReactConstants;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import javax.annotation.Nullable;
/**
@@ -36,12 +33,9 @@ import javax.annotation.Nullable;
*/
@TargetApi(Build.VERSION_CODES.CUPCAKE)
public class DevLoadingViewController {
private static final int COLOR_DARK_GREEN = Color.parseColor("#035900");
private static boolean sEnabled = true;
private final Context mContext;
private final ReactInstanceManagerDevHelper mReactInstanceManagerHelper;
private final TextView mDevLoadingView;
private @Nullable TextView mDevLoadingView;
private @Nullable PopupWindow mDevLoadingPopup;
public static void setDevLoadingEnabled(boolean enabled) {
@@ -49,13 +43,10 @@ public class DevLoadingViewController {
}
public DevLoadingViewController(Context context, ReactInstanceManagerDevHelper reactInstanceManagerHelper) {
mContext = context;
mReactInstanceManagerHelper = reactInstanceManagerHelper;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDevLoadingView = (TextView) inflater.inflate(R.layout.dev_loading_view, null);
}
public void showMessage(final String message, final int color, final int backgroundColor) {
public void showMessage(final String message) {
if (!sEnabled) {
return;
}
@@ -63,16 +54,17 @@ public class DevLoadingViewController {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
mDevLoadingView.setBackgroundColor(backgroundColor);
mDevLoadingView.setText(message);
mDevLoadingView.setTextColor(color);
showInternal();
showInternal(message);
}
});
}
public void showForUrl(String url) {
Context context = getContext();
if (context == null) {
return;
}
URL parsedURL;
try {
parsedURL = new URL(url);
@@ -82,13 +74,17 @@ public class DevLoadingViewController {
}
showMessage(
mContext.getString(R.string.catalyst_loading_from_url, parsedURL.getHost() + ":" + parsedURL.getPort()),
Color.WHITE,
COLOR_DARK_GREEN);
context.getString(R.string.catalyst_loading_from_url,
parsedURL.getHost() + ":" + parsedURL.getPort()));
}
public void showForRemoteJSEnabled() {
showMessage(mContext.getString(R.string.catalyst_remotedbg_message), Color.WHITE, COLOR_DARK_GREEN);
Context context = getContext();
if (context == null) {
return;
}
showMessage(context.getString(R.string.catalyst_remotedbg_message));
}
public void updateProgress(final @Nullable String status, final @Nullable Integer done, final @Nullable Integer total) {
@@ -105,21 +101,9 @@ public class DevLoadingViewController {
message.append(String.format(Locale.getDefault(), " %.1f%% (%d/%d)", (float) done / total * 100, done, total));
}
message.append("\u2026"); // `...` character
mDevLoadingView.setText(message);
}
});
}
public void show() {
if (!sEnabled) {
return;
}
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
showInternal();
if (mDevLoadingView != null) {
mDevLoadingView.setText(message);
}
}
});
}
@@ -137,7 +121,7 @@ public class DevLoadingViewController {
});
}
private void showInternal() {
private void showInternal(String message) {
if (mDevLoadingPopup != null && mDevLoadingPopup.isShowing()) {
// already showing
return;
@@ -150,20 +134,21 @@ public class DevLoadingViewController {
return;
}
int topOffset = 0;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
// On Android SDK <= 19 PopupWindow#showAtLocation uses absolute screen position. In order for
// loading view to be placed below status bar (if the status bar is present) we need to pass
// an appropriate Y offset.
Rect rectangle = new Rect();
currentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
topOffset = rectangle.top;
}
// PopupWindow#showAtLocation uses absolute screen position. In order for
// loading view to be placed below status bar (if the status bar is present) we need to pass
// an appropriate Y offset.
Rect rectangle = new Rect();
currentActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
int topOffset = rectangle.top;
mDevLoadingPopup = new PopupWindow(currentActivity);
LayoutInflater inflater =
(LayoutInflater) currentActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDevLoadingView = (TextView) inflater.inflate(R.layout.dev_loading_view, null);
mDevLoadingView.setText(message);
mDevLoadingPopup = new PopupWindow(mDevLoadingView, MATCH_PARENT, WRAP_CONTENT);
mDevLoadingPopup.setTouchable(false);
mDevLoadingPopup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
mDevLoadingPopup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
mDevLoadingPopup.showAtLocation(
currentActivity.getWindow().getDecorView(),
@@ -176,6 +161,11 @@ public class DevLoadingViewController {
if (mDevLoadingPopup != null && mDevLoadingPopup.isShowing()) {
mDevLoadingPopup.dismiss();
mDevLoadingPopup = null;
mDevLoadingView = null;
}
}
private @Nullable Context getContext() {
return mReactInstanceManagerHelper.getCurrentActivity();
}
}

View File

@@ -1073,7 +1073,7 @@ public class DevSupportManagerImpl implements
// show the dev loading if it should be
if (mDevLoadingViewVisible) {
mDevLoadingViewController.show();
mDevLoadingViewController.showMessage("Reloading...");
}
mDevServerHelper.openPackagerConnection(this.getClass().getSimpleName(), this);