Skip to content

Commit

Permalink
Use PopupWindow instead of adding loading view directly to window's d…
Browse files Browse the repository at this point in the history
…ecor view
  • Loading branch information
kmagiera committed Nov 15, 2017
1 parent d1c28fc commit 62367f6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -569,11 +569,43 @@ public void onHostResume(Activity activity, DefaultHardwareBackBtnHandler defaul
UiThreadUtil.assertOnUiThread();

mDefaultBackButtonImpl = defaultBackButtonImpl;
mCurrentActivity = activity;

if (mUseDeveloperSupport) {
mDevSupportManager.setDevSupportEnabled(true);
// Resume can be called from one of two different states:
// a) when activity was paused
// b) when activity has just been created
// In case of (a) the activity is attached to window and it is ok to add new views to it or
// open dialogs. In case of (b) there is often a slight delay before such a thing happens.
// As dev support manager can add views or open dialogs immediately after it gets enabled
// (e.g. in the case when JS bundle is being fetched in background) we only want to enable
// it once we know for sure the current activity is attached.

// We check if activity is attached to window by checking if decor view is attached
final View decorView = mCurrentActivity.getWindow().getDecorView();
if (decorView.getWindowToken() == null) {
// window token isn't set which means the view is not attached. We could use
// View#isAttchedToWindow but it is only available since API 19

decorView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
@Override
public void onViewAttachedToWindow(View v) {
// we can drop listener now that we know the view is attached
decorView.removeOnAttachStateChangeListener(this);
mDevSupportManager.setDevSupportEnabled(true);
}

@Override
public void onViewDetachedFromWindow(View v) {
// do nothing
}
});
} else {
// activity is attached to window, we can enable dev support immediately
mDevSupportManager.setDevSupportEnabled(true);
}
}

mCurrentActivity = activity;
moveToResumedLifecycleState(false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.facebook.common.logging.FLog;
Expand All @@ -37,7 +40,8 @@ public class DevLoadingViewController {
private static boolean sEnabled = true;
private final Context mContext;
private final ReactInstanceManager mReactInstanceManager;
private TextView mDevLoadingView;
private final TextView mDevLoadingView;
private @Nullable PopupWindow mDevLoadingPopup;

public static void setDevLoadingEnabled(boolean enabled) {
sEnabled = enabled;
Expand Down Expand Up @@ -133,8 +137,8 @@ public void run() {
}

private void showInternal() {
if (mDevLoadingView.getParent() != null) {
// already attached
if (mDevLoadingPopup != null && mDevLoadingPopup.isShowing()) {
// already showing
return;
}

Expand All @@ -145,17 +149,23 @@ private void showInternal() {
return;
}

ViewGroup parent = (ViewGroup) currentActivity.getWindow().getDecorView().getRootView();
parent.addView(
mDevLoadingPopup = new PopupWindow(
mDevLoadingView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mDevLoadingPopup.setTouchable(false);

mDevLoadingPopup.showAtLocation(
currentActivity.getWindow().getDecorView(),
Gravity.NO_GRAVITY,
0,
0);
}

private void hideInternal() {
ViewGroup parent = (ViewGroup) mDevLoadingView.getParent();
if (parent != null) {
parent.removeView(mDevLoadingView);
if (mDevLoadingPopup != null && mDevLoadingPopup.isShowing()) {
mDevLoadingPopup.dismiss();
mDevLoadingPopup = null;
}
}
}

0 comments on commit 62367f6

Please sign in to comment.