Skip to content

Commit

Permalink
Enable Double R shortcut to reload JS when redbox is shown on Android
Browse files Browse the repository at this point in the history
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 authored and Facebook Github Bot 9 committed Jun 7, 2016
1 parent 748a507 commit 4959b21
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 18 deletions.
31 changes: 14 additions & 17 deletions ReactAndroid/src/main/java/com/facebook/react/ReactActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -142,6 +152,7 @@ protected void onCreate(Bundle savedInstanceState) {
mReactRootView = createRootView();
mReactRootView.startReactApplication(mReactInstanceManager, getMainComponentName(), getLaunchOptions());
setContentView(mReactRootView);
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
}

@Override
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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.