From 4959b21290b0718b04d885052aad266d2713490b Mon Sep 17 00:00:00 2001 From: Siqi Liu Date: Tue, 7 Jun 2016 06:56:52 -0700 Subject: [PATCH] Enable Double R shortcut to reload JS when redbox is shown on Android Summary: Make "double tap R" shortcut enabled when redbox is shown in RN Android, consistent with that in iOS. Reviewed By: mkonicek Differential Revision: D3390132 fbshipit-source-id: 48fc40c2ba371a34abcac42a077359d11e907dfc --- .../com/facebook/react/ReactActivity.java | 31 ++++++------- .../devsupport/DoubleTapReloadRecognizer.java | 44 +++++++++++++++++++ .../react/devsupport/RedBoxDialog.java | 6 ++- 3 files changed, 63 insertions(+), 18 deletions(-) create mode 100644 ReactAndroid/src/main/java/com/facebook/react/devsupport/DoubleTapReloadRecognizer.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java b/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java index 4befbd3df..805130120 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java @@ -1,3 +1,12 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + package com.facebook.react; import android.app.Activity; @@ -12,6 +21,7 @@ import android.widget.Toast; import com.facebook.common.logging.FLog; import com.facebook.react.common.ReactConstants; +import com.facebook.react.devsupport.DoubleTapReloadRecognizer; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; import java.util.List; @@ -29,7 +39,7 @@ public abstract class ReactActivity extends Activity implements DefaultHardwareB private @Nullable ReactInstanceManager mReactInstanceManager; private @Nullable ReactRootView mReactRootView; private LifecycleState mLifecycleState = LifecycleState.BEFORE_RESUME; - private boolean mDoRefresh = false; + private DoubleTapReloadRecognizer mDoubleTapReloadRecognizer; /** * Returns the name of the bundle in assets. If this is null, and no file path is specified for @@ -142,6 +152,7 @@ public abstract class ReactActivity extends Activity implements DefaultHardwareB mReactRootView = createRootView(); mReactRootView.startReactApplication(mReactInstanceManager, getMainComponentName(), getLaunchOptions()); setContentView(mReactRootView); + mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); } @Override @@ -193,22 +204,8 @@ public abstract class ReactActivity extends Activity implements DefaultHardwareB mReactInstanceManager.showDevOptionsDialog(); return true; } - if (keyCode == KeyEvent.KEYCODE_R && !(getCurrentFocus() instanceof EditText)) { - // Enable double-tap-R-to-reload - if (mDoRefresh) { - mReactInstanceManager.getDevSupportManager().handleReloadJS(); - mDoRefresh = false; - } else { - mDoRefresh = true; - new Handler().postDelayed( - new Runnable() { - @Override - public void run() { - mDoRefresh = false; - } - }, - 200); - } + if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, getCurrentFocus())) { + mReactInstanceManager.getDevSupportManager().handleReloadJS(); } } return super.onKeyUp(keyCode, event); diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/DoubleTapReloadRecognizer.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DoubleTapReloadRecognizer.java new file mode 100644 index 000000000..71742026b --- /dev/null +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/DoubleTapReloadRecognizer.java @@ -0,0 +1,44 @@ +/** + * Copyright (c) 2015-present, Facebook, Inc. + * All rights reserved. + * + * This source code is licensed under the BSD-style license found in the + * LICENSE file in the root directory of this source tree. An additional grant + * of patent rights can be found in the PATENTS file in the same directory. + */ + +package com.facebook.react.devsupport; + +import android.os.Handler; +import android.view.KeyEvent; +import android.view.View; +import android.widget.EditText; + +/** + * A class allows recognizing double key tap of "R", used to reload JS in + * {@link AbstractReactActivity}, {@link RedBoxDialog} and {@link ReactActivity}. + */ +public class DoubleTapReloadRecognizer { + private boolean mDoRefresh = false; + private static final long DOUBLE_TAP_DELAY = 200; + + public boolean didDoubleTapR(int keyCode, View view) { + if (keyCode == KeyEvent.KEYCODE_R && !(view instanceof EditText)) { + if (mDoRefresh) { + mDoRefresh = false; + return true; + } else { + mDoRefresh = true; + new Handler().postDelayed( + new Runnable() { + @Override + public void run() { + mDoRefresh = false; + } + }, + DOUBLE_TAP_DELAY); + } + } + return false; + } +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java index 914f8db57..7b9cc1eb4 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java +++ b/ReactAndroid/src/main/java/com/facebook/react/devsupport/RedBoxDialog.java @@ -42,6 +42,7 @@ import org.json.JSONObject; /* package */ class RedBoxDialog extends Dialog implements AdapterView.OnItemClickListener { private final DevSupportManager mDevSupportManager; + private final DoubleTapReloadRecognizer mDoubleTapReloadRecognizer; private ListView mStackView; private Button mReloadJs; @@ -182,6 +183,7 @@ import org.json.JSONObject; setContentView(R.layout.redbox_view); mDevSupportManager = devSupportManager; + mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer(); mStackView = (ListView) findViewById(R.id.rn_redbox_stack); mStackView.setOnItemClickListener(this); @@ -219,7 +221,9 @@ import org.json.JSONObject; mDevSupportManager.showDevOptionsDialog(); return true; } - + if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, getCurrentFocus())) { + mDevSupportManager.handleReloadJS(); + } return super.onKeyUp(keyCode, event); } }