React Native Kiosk Mode

Viewed 1761

I have a rather basic React Native app. I want to conditionally lock/unlock the app based on the component that is being rendered. For example, I want to lock the app (in kiosk mode) if the Player component is rendered and then unlock the app if it's not (or unmounting).

render(){
  return(
   <>
      {this.state.showPlayer === false && <Connect />}
      {this.state.showPlayer === true && <Player />}
   </>
  )
}

I have reviewed the documentation for setting the lock here: https://developer.android.com/work/dpc/dedicated-devices/lock-task-mode#start_lock_task_mode

Here is the code:

// Set an option to turn on lock task mode when starting the activity.
ActivityOptions options = ActivityOptions.makeBasic();
options.setLockTaskEnabled(true);

// Start our kiosk app's main activity with our lock task mode option.
PackageManager packageManager = context.getPackageManager();
Intent launchIntent = packageManager.getLaunchIntentForPackage(KIOSK_PACKAGE);
if (launchIntent != null) {
  context.startActivity(launchIntent, options.toBundle());
}

I am not sure how to configure this as I'm rather new to the React Native and Android space. Any help would be appreciated.

2 Answers

TLDR: If want to use any native code ( java, kotlin, objc, swift) you can use Native Modules.

By the looks of it, you want to execute native code ( java ) from React Native. If you wrap the whole thing in a function ( or divide it into modules ), then you can do it just by calling that function from the JavaScript side of React Native.

Here is a simple tutorial on how to use Native Modules in React native.

https://www.reactnative.guide/16-custom-native-modules/16.1-android-native-module.html

(note: I can make you a minimal project on using these Native Modules with your java function, if you want. In that case, I would need exact functions that would do what you want just by calling it.)

Related