Skip to content

Commit 7afc8b8

Browse files
EdmondChuiHWfacebook-github-bot
authored andcommitted
disable "Open Debugger" menu item if the bundler is disconnected (#42876)
Summary: Pull Request resolved: #42876 Changelog: [Android][Fixed] Disable the "Open Debugger" item from dev menu if the bundler is disconnected # Existing In our dev menu, the "Open Debugger" menu item is shown even if the packager isn't connected. {F1451833462} # In this PR The "Open Debugger" menu item is disabled when the packager is disconnected with the message "Connect to the bundler to debug JavaScript" {F1451834019} # Reference * There are existing checks, but they're async. We don't need the check to be accurate; and we don't want to delay the dev menu from opening * [isMetroRunning()](https://github.com/facebook/react-native/blob/db066acfe3994787d706ad082ce718a91b8249f5/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java#L1013-L1027) * [isPackagerRunning(…)](https://github.com/facebook/react-native/blob/db066acfe3994787d706ad082ce718a91b8249f5/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java#L798-L805) * Also on iOS: D53354110 Reviewed By: hoxyq Differential Revision: D53428914 fbshipit-source-id: 3e70d7fec9fc8fc63a8519c5385a91775febce9a
1 parent d7dce97 commit 7afc8b8

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerBase.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import android.util.Pair;
2424
import android.view.Gravity;
2525
import android.view.View;
26+
import android.view.ViewGroup;
27+
import android.widget.ArrayAdapter;
2628
import android.widget.EditText;
2729
import android.widget.LinearLayout;
30+
import android.widget.ListAdapter;
2831
import android.widget.TextView;
2932
import android.widget.Toast;
3033
import androidx.annotation.Nullable;
@@ -63,10 +66,12 @@
6366
import java.net.MalformedURLException;
6467
import java.net.URL;
6568
import java.util.ArrayList;
69+
import java.util.HashSet;
6670
import java.util.LinkedHashMap;
6771
import java.util.List;
6872
import java.util.Locale;
6973
import java.util.Map;
74+
import java.util.Set;
7075

7176
public abstract class DevSupportManagerBase implements DevSupportManager {
7277

@@ -104,6 +109,7 @@ public interface CallbackWithBundleLoader {
104109
private boolean mIsReceiverRegistered = false;
105110
private boolean mIsShakeDetectorStarted = false;
106111
private boolean mIsDevSupportEnabled = false;
112+
private boolean mIsPackagerConnected;
107113
private @Nullable final RedBoxHandler mRedBoxHandler;
108114
private @Nullable String mLastErrorTitle;
109115
private @Nullable StackFrame[] mLastErrorStack;
@@ -340,6 +346,7 @@ public void showDevOptionsDialog() {
340346
return;
341347
}
342348
LinkedHashMap<String, DevOptionHandler> options = new LinkedHashMap<>();
349+
Set<String> disabledItemKeys = new HashSet<>();
343350
/* register standard options */
344351
options.put(
345352
mApplicationContext.getString(R.string.catalyst_reload),
@@ -369,8 +376,15 @@ public void onOptionSelected() {
369376

370377
if (mDevSettings.isDeviceDebugEnabled() && !mDevSettings.isRemoteJSDebugEnabled()) {
371378
// On-device JS debugging (CDP). Render action to open debugger frontend.
379+
boolean isConnected = mIsPackagerConnected;
380+
String debuggerItemString =
381+
mApplicationContext.getString(
382+
isConnected ? R.string.catalyst_debug_open : R.string.catalyst_debug_open_disabled);
383+
if (!isConnected) {
384+
disabledItemKeys.add(debuggerItemString);
385+
}
372386
options.put(
373-
mApplicationContext.getString(R.string.catalyst_debug_open),
387+
debuggerItemString,
374388
() ->
375389
mDevServerHelper.openDebugger(
376390
mCurrentContext,
@@ -505,11 +519,33 @@ public void onOptionSelected() {
505519
header.addView(jsExecutorLabel);
506520
}
507521

522+
ListAdapter adapter =
523+
new ArrayAdapter<String>(
524+
context, android.R.layout.simple_list_item_1, options.keySet().toArray(new String[0])) {
525+
@Override
526+
public boolean areAllItemsEnabled() {
527+
return false;
528+
}
529+
530+
@Override
531+
public boolean isEnabled(int position) {
532+
return !disabledItemKeys.contains(getItem(position));
533+
}
534+
535+
@Override
536+
public View getView(int position, @Nullable View convertView, ViewGroup parent) {
537+
View view = super.getView(position, convertView, parent);
538+
view.setEnabled(isEnabled(position));
539+
540+
return view;
541+
}
542+
};
543+
508544
mDevOptionsDialog =
509545
new AlertDialog.Builder(context)
510546
.setCustomTitle(header)
511-
.setItems(
512-
options.keySet().toArray(new String[0]),
547+
.setAdapter(
548+
adapter,
513549
(dialog, which) -> {
514550
optionHandlers[which].onOptionSelected();
515551
mDevOptionsDialog = null;
@@ -1022,12 +1058,12 @@ private void reload() {
10221058
new PackagerCommandListener() {
10231059
@Override
10241060
public void onPackagerConnected() {
1025-
// No-op
1061+
mIsPackagerConnected = true;
10261062
}
10271063

10281064
@Override
10291065
public void onPackagerDisconnected() {
1030-
// No-op
1066+
mIsPackagerConnected = false;
10311067
}
10321068

10331069
@Override

packages/react-native/ReactAndroid/src/main/res/devsupport/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<string name="catalyst_change_bundle_location" project="catalyst" translatable="false">Change Bundle Location</string>
66
<string name="catalyst_open_debugger_error" project="catalyst" translatable="false">Failed to open debugger. Please check that the dev server is running and reload the app.</string>
77
<string name="catalyst_debug_open" project="catalyst" translatable="false">Open Debugger</string>
8+
<string name="catalyst_debug_open_disabled" project="catalyst" translatable="false">Connect to the bundler to debug JavaScript</string>
89
<string name="catalyst_debug_connecting" project="catalyst" translatable="false">Connecting to debugger...</string>
910
<string name="catalyst_debug_error" project="catalyst" translatable="false">Failed to connect to debugger!</string>
1011
<string name="catalyst_hot_reloading" project="catalyst" translatable="false">Enable Fast Refresh</string>

0 commit comments

Comments
 (0)