I have been trying to pass FragmentActivity from React Native to Kotlin Library.
This is the code from RN side.
public class RNSecureKeystoreModule extends ReactContextBaseJavaModule implements ActivityEventListener {
private final KeyGeneratorImpl keyGenerator = new KeyGeneratorImpl();
private final CipherBoxImpl cipherBox = new CipherBoxImpl();
private final Biometrics biometrics;
private final SecureKeystoreImpl keystore;
private final DeviceCapability deviceCapability;
private final String logTag;
@ReactMethod
public RNSecureKeystoreModule(ReactApplicationContext reactContext) {
super(reactContext);
FragmentActivity activity=(FragmentActivity)getCurrentActivity();
this.biometrics = new Biometrics(activity);
this.keystore = new SecureKeystoreImpl(keyGenerator, cipherBox, biometrics);
this.deviceCapability = new DeviceCapability(keystore, keyGenerator, biometrics);
this.logTag = Util.Companion.getLogTag(getClass().getSimpleName());
}
My activity is null always, how can I solve this?
Here’s how I receive my Fragment activity.
`class Biometrics(
private val context: Context,
) {
And here’s why fragment activity is needed.
private suspend fun authenticate(
createCryptoObject: () -> CryptoObject,
action: (CryptoObject) -> Unit,
createCryptoObjectSuccess: Boolean,
) {
return suspendCoroutine { continuation ->
val fragmentActivity = context as? FragmentActivity
?: throw NullPointerException("Context is not a FragmentActivity")
fragmentActivity.runOnUiThread {
try {
val onAuthSuccess: (CryptoObject?) -> Unit =
{ cryptoObject -> action(cryptoObject ?: createCryptoObject()) }
val authCallback: BiometricPrompt.AuthenticationCallback =
BiometricPromptAuthCallback(continuation, onAuthSuccess)
val executor: Executor = Executors.newSingleThreadExecutor()
val promptInfo = createPromptInfo()
val biometricPrompt =
**BiometricPrompt(context as FragmentActivity, executor, authCallback)**
if (createCryptoObjectSuccess) {
biometricPrompt.authenticate(promptInfo, createCryptoObject())
} else {
try {
biometricPrompt.authenticate(promptInfo)
} catch (e: Exception) {
Log.e(logTag, e.toString())
}
}
} catch (e: Exception) {
Log.e(logTag, "exception in creating auth prompt: ", e)
continuation.resumeWithException(RuntimeException("exception in creating auth prompt"))
}
}
}
}
`
Please help me bring this to a closure