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
This commit is contained in:
Siqi Liu 2016-06-07 06:56:52 -07:00 коммит произвёл Facebook Github Bot 9
Родитель 748a507861
Коммит 4959b21290
3 изменённых файлов: 63 добавлений и 18 удалений

Просмотреть файл

@ -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);

Просмотреть файл

@ -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 @@ 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);
}
}