Skip to content

Commit 7c7108a

Browse files
pakoitofacebook-github-bot
authored andcommitted
Add inspector attach to RN Dev Menu (Android)
Reviewed By: Hypuk Differential Revision: D6405828 fbshipit-source-id: 9274c3e8748e6ce259357836dde7d53c4fce9fbf
1 parent de424cc commit 7c7108a

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactSettingsForTests.java

+5
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public boolean isElementInspectorEnabled() {
3737
return false;
3838
}
3939

40+
@Override
41+
public boolean isNuclideJSDebugEnabled() {
42+
return false;
43+
}
44+
4045
@Override
4146
public boolean isRemoteJSDebugEnabled() {
4247
return false;

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevInternalSettings.java

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import android.content.SharedPreferences;
1515
import android.preference.PreferenceManager;
1616
import com.facebook.react.common.annotations.VisibleForTesting;
17+
import com.facebook.react.common.build.ReactBuildConfig;
1718
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
1819
import com.facebook.react.packagerconnection.PackagerConnectionSettings;
1920

@@ -124,6 +125,11 @@ public void setBundleDeltasEnabled(boolean enabled) {
124125
mPreferences.edit().putBoolean(PREFS_JS_BUNDLE_DELTAS_KEY, enabled).apply();
125126
}
126127

128+
@Override
129+
public boolean isNuclideJSDebugEnabled() {
130+
return ReactBuildConfig.IS_INTERNAL_BUILD && ReactBuildConfig.DEBUG;
131+
}
132+
127133
@Override
128134
public boolean isRemoteJSDebugEnabled() {
129135
return mPreferences.getBoolean(PREFS_REMOTE_JS_DEBUG_KEY, false);

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevServerHelper.java

+43
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
import android.content.Context;
1313
import android.os.AsyncTask;
1414
import android.os.Handler;
15+
import android.widget.Toast;
1516
import com.facebook.common.logging.FLog;
1617
import com.facebook.infer.annotation.Assertions;
18+
import com.facebook.react.R;
1719
import com.facebook.react.bridge.UiThreadUtil;
1820
import com.facebook.react.common.ReactConstants;
1921
import com.facebook.react.common.network.OkHttpCallUtil;
@@ -73,6 +75,7 @@ public class DevServerHelper {
7375
private static final String PACKAGER_STATUS_URL_FORMAT = "http://%s/status";
7476
private static final String HEAP_CAPTURE_UPLOAD_URL_FORMAT = "http://%s/jscheapcaptureupload";
7577
private static final String INSPECTOR_DEVICE_URL_FORMAT = "http://%s/inspector/device?name=%s&app=%s";
78+
private static final String INSPECTOR_ATTACH_URL_FORMAT = "http://%s/nuclide/attach-debugger-nuclide?title=%s&app=%s&device=%s";
7679
private static final String SYMBOLICATE_URL_FORMAT = "http://%s/symbolicate";
7780
private static final String OPEN_STACK_FRAME_URL_FORMAT = "http://%s/open-stack-frame";
7881

@@ -224,6 +227,36 @@ protected Void doInBackground(Void... params) {
224227
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
225228
}
226229

230+
public void attachDebugger(final Context context, final String title) {
231+
new AsyncTask<Void, String, Boolean>() {
232+
@Override
233+
protected Boolean doInBackground(Void... ignore) {
234+
return doSync();
235+
}
236+
237+
public boolean doSync() {
238+
try {
239+
String attachToNuclideUrl = getInspectorAttachUrl(title);
240+
OkHttpClient client = new OkHttpClient();
241+
Request request = new Request.Builder().url(attachToNuclideUrl).build();
242+
client.newCall(request).execute();
243+
return true;
244+
} catch (IOException e) {
245+
FLog.e(ReactConstants.TAG, "Failed to send attach request to Inspector", e);
246+
return false;
247+
}
248+
}
249+
250+
@Override
251+
protected void onPostExecute(Boolean result) {
252+
if (!result) {
253+
String message = context.getString(R.string.catalyst_debugjs_nuclide_failure);
254+
Toast.makeText(context, message, Toast.LENGTH_LONG).show();
255+
}
256+
}
257+
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
258+
}
259+
227260
public void symbolicateStackTrace(
228261
Iterable<StackFrame> stackFrames,
229262
final SymbolicationListener listener) {
@@ -321,6 +354,16 @@ public String getInspectorDeviceUrl() {
321354
mPackageName);
322355
}
323356

357+
public String getInspectorAttachUrl(String title) {
358+
return String.format(
359+
Locale.US,
360+
INSPECTOR_ATTACH_URL_FORMAT,
361+
AndroidInfoHelpers.getServerHost(),
362+
title,
363+
mPackageName,
364+
AndroidInfoHelpers.getFriendlyDeviceName());
365+
}
366+
324367
public BundleDownloader getBundleDownloader() {
325368
return mBundleDownloader;
326369
}

ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManagerImpl.java

+27-4
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ private static enum ErrorType {
110110
private static final String EXOPACKAGE_LOCATION_FORMAT
111111
= "/data/local/tmp/exopackage/%s//secondary-dex";
112112

113+
public static final String EMOJI_HUNDRED_POINTS_SYMBOL = " \uD83D\uDCAF";
114+
public static final String EMOJI_FACE_WITH_NO_GOOD_GESTURE = " \uD83D\uDE45";
115+
113116
private final Context mApplicationContext;
114117
private final ShakeDetector mShakeDetector;
115118
private final BroadcastReceiver mReloadAppBroadcastReceiver;
@@ -395,16 +398,36 @@ public void showDevOptionsDialog() {
395398
LinkedHashMap<String, DevOptionHandler> options = new LinkedHashMap<>();
396399
/* register standard options */
397400
options.put(
398-
mApplicationContext.getString(R.string.catalyst_reloadjs), new DevOptionHandler() {
401+
mApplicationContext.getString(R.string.catalyst_reloadjs),
402+
new DevOptionHandler() {
399403
@Override
400404
public void onOptionSelected() {
401405
handleReloadJS();
402406
}
403407
});
408+
if (mDevSettings.isNuclideJSDebugEnabled()) {
409+
// The concatenation is applied directly here because XML isn't emoji-friendly
410+
String nuclideJsDebugMenuItemTitle =
411+
mApplicationContext.getString(R.string.catalyst_debugjs_nuclide)
412+
+ EMOJI_HUNDRED_POINTS_SYMBOL;
413+
options.put(
414+
nuclideJsDebugMenuItemTitle,
415+
new DevOptionHandler() {
416+
@Override
417+
public void onOptionSelected() {
418+
mDevServerHelper.attachDebugger(mApplicationContext, "ReactNative");
419+
}
420+
});
421+
}
422+
String remoteJsDebugMenuItemTitle =
423+
mDevSettings.isRemoteJSDebugEnabled()
424+
? mApplicationContext.getString(R.string.catalyst_debugjs_off)
425+
: mApplicationContext.getString(R.string.catalyst_debugjs);
426+
if (mDevSettings.isNuclideJSDebugEnabled()) {
427+
remoteJsDebugMenuItemTitle += EMOJI_FACE_WITH_NO_GOOD_GESTURE;
428+
}
404429
options.put(
405-
mDevSettings.isRemoteJSDebugEnabled() ?
406-
mApplicationContext.getString(R.string.catalyst_debugjs_off) :
407-
mApplicationContext.getString(R.string.catalyst_debugjs),
430+
remoteJsDebugMenuItemTitle,
408431
new DevOptionHandler() {
409432
@Override
410433
public void onOptionSelected() {

ReactAndroid/src/main/java/com/facebook/react/modules/debug/interfaces/DeveloperSettings.java

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ public interface DeveloperSettings {
3939
*/
4040
boolean isElementInspectorEnabled();
4141

42+
/**
43+
* @return Whether Nuclide JS debugging is enabled.
44+
*/
45+
boolean isNuclideJSDebugEnabled();
46+
4247
/**
4348
* @return Whether remote JS debugging is enabled.
4449
*/

ReactAndroid/src/main/res/devsupport/values/strings.xml

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="catalyst_reloadjs" project="catalyst" translatable="false">Reload</string>
4+
<string name="catalyst_debugjs_nuclide" project="catalyst" translatable="false">Debug JS in Nuclide</string>
5+
<string name="catalyst_debugjs_nuclide_failure" project="catalyst" translatable="false">The request to attach Nuclide could not reach Metro Bundler!</string>
46
<string name="catalyst_debugjs" project="catalyst" translatable="false">Debug JS Remotely</string>
57
<string name="catalyst_debugjs_off" project="catalyst" translatable="false">Stop Remote JS Debugging</string>
68
<string name="catalyst_hot_module_replacement" project="catalyst" translatable="false">Enable Hot Reloading</string>

0 commit comments

Comments
 (0)