-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathJsDevReloadHandler.java
More file actions
96 lines (78 loc) · 2.77 KB
/
JsDevReloadHandler.java
File metadata and controls
96 lines (78 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package com.reactnativenavigation.react;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.KeyEvent;
import android.widget.EditText;
import android.os.Build;
import com.facebook.react.devsupport.interfaces.DevSupportManager;
import com.reactnativenavigation.utils.UiUtils;
public class JsDevReloadHandler extends JsDevReloadHandlerFacade {
private static final String RELOAD_BROADCAST = "com.reactnativenavigation.broadcast.RELOAD";
public interface ReloadListener {
void onReload();
}
private final BroadcastReceiver reloadReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
reloadReactNative();
}
};
private final DevSupportManager devSupportManager;
private long firstRTimestamp = 0;
private ReloadListener reloadListener = () -> {};
JsDevReloadHandler(DevSupportManager devSupportManager) {
this.devSupportManager = devSupportManager;
}
@Override
public void onSuccess() {
UiUtils.runOnMainThread(reloadListener::onReload);
}
public void setReloadListener(ReloadListener listener) {
reloadListener = listener;
}
public void removeReloadListener(ReloadListener listener) {
if (reloadListener == listener) {
reloadListener = null;
}
}
public void onActivityResumed(Activity activity) {
if (Build.VERSION.SDK_INT >= 34 && activity.getApplicationInfo().targetSdkVersion >= 34) {
activity.registerReceiver(reloadReceiver, new IntentFilter(RELOAD_BROADCAST), Context.RECEIVER_EXPORTED);
} else {
activity.registerReceiver(reloadReceiver, new IntentFilter(RELOAD_BROADCAST));
}
}
public void onActivityPaused(Activity activity) {
activity.unregisterReceiver(reloadReceiver);
}
public boolean onKeyUp(Activity activity, int keyCode) {
if (!devSupportManager.getDevSupportEnabled()) {
return false;
}
if (keyCode == KeyEvent.KEYCODE_MENU) {
devSupportManager.showDevOptionsDialog();
return true;
}
if (keyCode == KeyEvent.KEYCODE_R) {
if (lessThan500MillisSinceLastR() && textViewIsNotFocused(activity)) {
reloadReactNative();
return true;
}
firstRTimestamp = System.currentTimeMillis();
}
return false;
}
private boolean lessThan500MillisSinceLastR() {
return firstRTimestamp != 0 && System.currentTimeMillis() - firstRTimestamp < 1000;
}
private boolean textViewIsNotFocused(Activity activity) {
return !(activity.getCurrentFocus() instanceof EditText);
}
private void reloadReactNative() {
reloadListener.onReload();
devSupportManager.handleReloadJS();
}
}