Skip to content
Permalink
Browse files

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
  • Loading branch information
Siqi Liu Facebook Github Bot 9
Siqi Liu authored and Facebook Github Bot 9 committed Jun 7, 2016
1 parent 748a507 commit 4959b21290b0718b04d885052aad266d2713490b
@@ -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 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 @@
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 @@ protected void onCreate(Bundle savedInstanceState) {
mReactRootView = createRootView();
mReactRootView.startReactApplication(mReactInstanceManager, getMainComponentName(), getLaunchOptions());
setContentView(mReactRootView);
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
}

@Override
@@ -193,22 +204,8 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
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);
@@ -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;
}
}
@@ -42,6 +42,7 @@
/* 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 @@ protected RedBoxDialog(Context context, DevSupportManager devSupportManager) {
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 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {
mDevSupportManager.showDevOptionsDialog();
return true;
}

if (mDoubleTapReloadRecognizer.didDoubleTapR(keyCode, getCurrentFocus())) {
mDevSupportManager.handleReloadJS();
}
return super.onKeyUp(keyCode, event);
}
}

0 comments on commit 4959b21

Please sign in to comment.
You can’t perform that action at this time.