Fix DisplayMetrics KeyboardListener dependency

Summary:
public
KeyboardListener needs DisplayMetrics to be initialized when it is attached. At
the moment, this breaks easily whenever we change these components, since DisplayMetrics are intialized
in a module and KeyboardListener is created eagerly in ReactRootView, whereas
ReactRootView can exist without the instance.
This changes to create DisplayMetrics as soon as possible, when the react
instance is built. The KeyboardListener is created and attached after the ReactRootView is
attached to an existing instance, point at which DisplayMetrics have to be
initialized.

Reviewed By: dmmiller

Differential Revision: D2911351

fb-gh-sync-id: 64d1805c5d5b2f6876adb694b565a2df059b381d
This commit is contained in:
Andrei Coman
2016-02-08 11:45:30 -08:00
committed by facebook-github-bot-5
parent 6b74535e97
commit 4254e8a0a9
11 changed files with 86 additions and 77 deletions

View File

@@ -55,11 +55,10 @@ import com.facebook.react.uimanager.events.TouchEventType;
*/
public class ReactRootView extends SizeMonitoringFrameLayout implements RootView {
private final KeyboardListener mKeyboardListener = new KeyboardListener();
private @Nullable ReactInstanceManager mReactInstanceManager;
private @Nullable String mJSModuleName;
private @Nullable Bundle mLaunchOptions;
private @Nullable KeyboardListener mKeyboardListener;
private int mTargetTag = -1;
private final float[] mTargetCoordinates = new float[2];
private boolean mChildIsHandlingNativeGesture = false;
@@ -107,7 +106,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
Assertions.assertNotNull(mReactInstanceManager)
.attachMeasuredRootView(ReactRootView.this);
mIsAttachedToInstance = true;
getViewTreeObserver().addOnGlobalLayoutListener(mKeyboardListener);
getViewTreeObserver().addOnGlobalLayoutListener(getKeyboardListener());
}
});
}
@@ -298,7 +297,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
if (mReactInstanceManager != null && !mAttachScheduled) {
mReactInstanceManager.detachRootView(this);
mIsAttachedToInstance = false;
getViewTreeObserver().removeOnGlobalLayoutListener(mKeyboardListener);
getViewTreeObserver().removeOnGlobalLayoutListener(getKeyboardListener());
}
}
@@ -356,7 +355,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
if (mWasMeasured && mIsAttachedToWindow) {
mReactInstanceManager.attachMeasuredRootView(this);
mIsAttachedToInstance = true;
getViewTreeObserver().addOnGlobalLayoutListener(mKeyboardListener);
getViewTreeObserver().addOnGlobalLayoutListener(getKeyboardListener());
} else {
mAttachScheduled = true;
}
@@ -381,9 +380,21 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
mWasMeasured = true;
}
private KeyboardListener getKeyboardListener() {
if (mKeyboardListener == null) {
mKeyboardListener = new KeyboardListener();
}
return mKeyboardListener;
}
private class KeyboardListener implements ViewTreeObserver.OnGlobalLayoutListener {
private final Rect mVisibleViewArea;
private int mKeyboardHeight = 0;
private final Rect mVisibleViewArea = new Rect();
/* package */ KeyboardListener() {
mVisibleViewArea = new Rect();
}
@Override
public void onGlobalLayout() {