-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kotlinify DefaultDevSupportManagerFactory (#43960)
Summary: Pull Request resolved: #43960 ## Changelog: [Internal] - As in the title. Differential Revision: D55865794
- Loading branch information
1 parent
01535c5
commit 70a738f
Showing
3 changed files
with
114 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 0 additions & 115 deletions
115
...tAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.java
This file was deleted.
Oops, something went wrong.
112 changes: 112 additions & 0 deletions
112
...actAndroid/src/main/java/com/facebook/react/devsupport/DefaultDevSupportManagerFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.devsupport | ||
|
||
import android.content.Context | ||
import com.facebook.react.common.SurfaceDelegateFactory | ||
import com.facebook.react.devsupport.interfaces.DevBundleDownloadListener | ||
import com.facebook.react.devsupport.interfaces.DevLoadingViewManager | ||
import com.facebook.react.devsupport.interfaces.DevSupportManager | ||
import com.facebook.react.devsupport.interfaces.RedBoxHandler | ||
import com.facebook.react.packagerconnection.RequestHandler | ||
|
||
/** | ||
* A simple factory that creates instances of [DevSupportManager] implementations. Uses reflection | ||
* to create BridgeDevSupportManager if it exists. This allows ProGuard to strip that class and its | ||
* dependencies in release builds. If the class isn't found, [ ] is returned instead. | ||
*/ | ||
public class DefaultDevSupportManagerFactory : DevSupportManagerFactory { | ||
|
||
@Deprecated( | ||
"in favor of the customisable create for DevSupportManagerFactory", | ||
ReplaceWith( | ||
"create(applicationContext, reactInstanceManagerHelper, packagerPathForJSBundleName, enableOnCreate, redBoxHandler, devBundleDownloadListener, minNumShakes, customPackagerCommandHandlers, surfaceDelegateFactory, devLoadingViewManager)")) | ||
public fun create( | ||
applicationContext: Context, | ||
reactInstanceDevHelper: ReactInstanceDevHelper, | ||
packagerPathForJSBundleName: String?, | ||
enableOnCreate: Boolean, | ||
minNumShakes: Int | ||
): DevSupportManager { | ||
return create( | ||
applicationContext, | ||
reactInstanceDevHelper, | ||
packagerPathForJSBundleName, | ||
enableOnCreate, | ||
null, | ||
null, | ||
minNumShakes, | ||
null, | ||
null, | ||
null) | ||
} | ||
|
||
public override fun create( | ||
applicationContext: Context, | ||
reactInstanceManagerHelper: ReactInstanceDevHelper, | ||
packagerPathForJSBundleName: String?, | ||
enableOnCreate: Boolean, | ||
redBoxHandler: RedBoxHandler?, | ||
devBundleDownloadListener: DevBundleDownloadListener?, | ||
minNumShakes: Int, | ||
customPackagerCommandHandlers: Map<String, RequestHandler>?, | ||
surfaceDelegateFactory: SurfaceDelegateFactory?, | ||
devLoadingViewManager: DevLoadingViewManager? | ||
): DevSupportManager { | ||
return if (!enableOnCreate) { | ||
ReleaseDevSupportManager() | ||
} else | ||
try { | ||
// Developer support is enabled, we now must choose whether to return a DevSupportManager, | ||
// or a more lean profiling-only PerftestDevSupportManager. We make the choice by first | ||
// trying to return the full support DevSupportManager and if it fails, then just | ||
// return PerftestDevSupportManager. | ||
|
||
// ProGuard is surprisingly smart in this case and will keep a class if it detects a call | ||
// to | ||
// Class.forName() with a static string. So instead we generate a quasi-dynamic string to | ||
// confuse it. | ||
val className = | ||
StringBuilder(DEVSUPPORT_IMPL_PACKAGE) | ||
.append(".") | ||
.append(DEVSUPPORT_IMPL_CLASS) | ||
.toString() | ||
val devSupportManagerClass = Class.forName(className) | ||
val constructor = | ||
devSupportManagerClass.getConstructor( | ||
Context::class.java, | ||
ReactInstanceDevHelper::class.java, | ||
String::class.java, | ||
Boolean::class.javaPrimitiveType, | ||
RedBoxHandler::class.java, | ||
DevBundleDownloadListener::class.java, | ||
Int::class.javaPrimitiveType, | ||
MutableMap::class.java, | ||
SurfaceDelegateFactory::class.java, | ||
DevLoadingViewManager::class.java) | ||
constructor.newInstance( | ||
applicationContext, | ||
reactInstanceManagerHelper, | ||
packagerPathForJSBundleName, | ||
true, | ||
redBoxHandler, | ||
devBundleDownloadListener, | ||
minNumShakes, | ||
customPackagerCommandHandlers, | ||
surfaceDelegateFactory, | ||
devLoadingViewManager) as DevSupportManager | ||
} catch (e: Exception) { | ||
PerftestDevSupportManager(applicationContext) | ||
} | ||
} | ||
|
||
private companion object { | ||
private const val DEVSUPPORT_IMPL_PACKAGE = "com.facebook.react.devsupport" | ||
private const val DEVSUPPORT_IMPL_CLASS = "BridgeDevSupportManager" | ||
} | ||
} |