Skip to content

Commit b39e8a0

Browse files
Krzysztof Borowymichalchudziak
authored andcommitted
feat(android): add packager connection check (#26)
1 parent 2aecb60 commit b39e8a0

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

android/src/main/java/com/callstack/reactnativebrownfield/ReactNativeActivity.kt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.facebook.react.devsupport.DoubleTapReloadRecognizer
1313
import com.facebook.react.modules.core.PermissionListener
1414
import com.facebook.react.bridge.Callback
1515
import com.facebook.react.bridge.ReadableMap
16+
import com.facebook.react.common.DebugServerException
1617
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler
1718
import com.facebook.react.modules.core.PermissionAwareActivity
1819

@@ -39,10 +40,9 @@ class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, Perm
3940
)
4041

4142
supportActionBar?.hide()
42-
4343
setContentView(reactRootView)
44-
4544
doubleTapReloadRecognizer = DoubleTapReloadRecognizer()
45+
checkPackagerConnection()
4646
}
4747

4848
override fun onDestroy() {
@@ -145,6 +145,21 @@ class ReactNativeActivity : ReactActivity(), DefaultHardwareBackBtnHandler, Perm
145145
}
146146
}
147147

148+
private fun checkPackagerConnection() {
149+
if (ReactNativeBrownfield.shared.reactNativeHost.hasInstance() && ReactNativeBrownfield.shared.reactNativeHost.useDeveloperSupport) {
150+
val devSupportManager =
151+
ReactNativeBrownfield.shared.reactNativeHost.reactInstanceManager.devSupportManager
152+
val url = devSupportManager.sourceUrl
153+
devSupportManager?.isPackagerRunning { isRunning ->
154+
if (!isRunning) {
155+
val error = Error()
156+
val message = DebugServerException.makeGeneric(url, "Could not connect to development server.", "URL: $url", error).message
157+
devSupportManager.showNewJavaError(message, error)
158+
}
159+
}
160+
}
161+
}
162+
148163
companion object {
149164
@JvmStatic
150165
@JvmOverloads

0 commit comments

Comments
 (0)