Skip to content

Commit

Permalink
Fixed unexpected NPE happening in framework due to 'more keys' placer…
Browse files Browse the repository at this point in the history
… views
  • Loading branch information
MajeurAndroid authored and dslul committed Dec 13, 2020
1 parent 629fd56 commit fa66144
Showing 1 changed file with 33 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.accessibility.AccessibilityEvent;

import android.widget.FrameLayout;
Expand Down Expand Up @@ -57,7 +58,7 @@
final class EmojiPageKeyboardView extends KeyboardView implements
MoreKeysPanel.Controller {
private static final String TAG = "EmojiPageKeyboardView";
private static final boolean LOG = true;
private static final boolean LOG = false;
private static final long KEY_PRESS_DELAY_TIME = 250; // msec
private static final long KEY_RELEASE_DELAY_TIME = 30; // msec

Expand Down Expand Up @@ -121,13 +122,7 @@ public void setHardwareAcceleratedDrawingEnabled(final boolean enabled) {
mMoreKeysPlacerView.setLayerType(LAYER_TYPE_HARDWARE, layerPaint);
}

@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
installMoreKeysPlacerView();
}

private void installMoreKeysPlacerView() {
private void installMoreKeysPlacerView(final boolean uninstall) {
final View rootView = getRootView();
if (rootView == null) {
Log.w(TAG, "Cannot find root view");
Expand All @@ -140,30 +135,11 @@ private void installMoreKeysPlacerView() {
return;
}

windowContentView.addView(mMoreKeysPlacerView);
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mMoreKeysPlacerView.removeAllViews();
uninstallMoreKeysPlacerView();
}

private void uninstallMoreKeysPlacerView() {
final View rootView = getRootView();
if (rootView == null) {
Log.w(TAG, "Cannot find root view");
return;
}
final ViewGroup windowContentView = rootView.findViewById(android.R.id.content);
// Note: It'd be very weird if we get null by android.R.id.content.
if (windowContentView == null) {
Log.w(TAG, "Cannot find android.R.id.content view to add DrawingPreviewPlacerView");
return;
if (uninstall) {
windowContentView.removeView(mMoreKeysPlacerView);
} else {
windowContentView.addView(mMoreKeysPlacerView);
}

windowContentView.removeView(mMoreKeysPlacerView);
}

public void setOnKeyEventListener(final OnKeyEventListener listener) {
Expand Down Expand Up @@ -223,34 +199,38 @@ public MoreKeysPanel showMoreKeysKeyboard(@Nonnull final Key key, final int last
return moreKeysKeyboardView;
}

@Override
public void onShowMoreKeysPanel(final MoreKeysPanel panel) {
// Dismiss another {@link MoreKeysPanel} that may be being showed.
onDismissMoreKeysPanel();
panel.showInParent(mMoreKeysPlacerView);
mMoreKeysPanel = panel;
private void dismissMoreKeysPanel() {
if (isShowingMoreKeysPanel()) {
mMoreKeysPanel.dismissMoreKeysPanel();
}
}

public boolean isShowingMoreKeysPanel() {
return mMoreKeysPanel != null;
}

@Override
public void onCancelMoreKeysPanel() {
// Nothing to do
public void onShowMoreKeysPanel(final MoreKeysPanel panel) {
// install placer view only when needed instead of when this
// view is attached to window
installMoreKeysPlacerView(false /* uninstall */);
panel.showInParent(mMoreKeysPlacerView);
mMoreKeysPanel = panel;
}

@Override
public void onDismissMoreKeysPanel() {
if (isShowingMoreKeysPanel()) {
mMoreKeysPanel.removeFromParent();
mMoreKeysPanel = null;
installMoreKeysPlacerView(true /* uninstall */);
}
}

private void dismissMoreKeysPanel() {
@Override
public void onCancelMoreKeysPanel() {
if (isShowingMoreKeysPanel()) {
mMoreKeysPanel.dismissMoreKeysPanel();
dismissMoreKeysPanel();
}
}

Expand Down Expand Up @@ -319,6 +299,9 @@ private void onLongPressed(final Key key) {
final int translatedX = moreKeysPanel.translateX(x);
final int translatedY = moreKeysPanel.translateY(y);
moreKeysPanel.onDownEvent(translatedX, translatedY, mPointerId, 0 /* nor used for now */);
// No need of re-allowing parent later as we don't
// want any scroll to append during this entire input.
disallowParentInterceptTouchEvent(true);
}
}

Expand Down Expand Up @@ -465,4 +448,13 @@ public boolean onMove(final MotionEvent e) {
mLastY = y;
return true;
}

private void disallowParentInterceptTouchEvent(final boolean disallow) {
final ViewParent parent = getParent();
if (parent == null) {
Log.w(TAG, "Cannot disallow touch event interception, no parent found.");
return;
}
parent.requestDisallowInterceptTouchEvent(disallow);
}
}

0 comments on commit fa66144

Please sign in to comment.