|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react |
| 9 | + |
| 10 | +import android.content.Intent |
| 11 | +import android.content.res.Configuration |
| 12 | +import android.os.Bundle |
| 13 | +import android.view.KeyEvent |
| 14 | +import androidx.appcompat.app.AppCompatActivity |
| 15 | +import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler |
| 16 | +import com.facebook.react.modules.core.PermissionAwareActivity |
| 17 | +import com.facebook.react.modules.core.PermissionListener |
| 18 | + |
| 19 | +/** Base Activity for React Native applications. */ |
| 20 | +public abstract class ReactActivity protected constructor() : |
| 21 | + AppCompatActivity(), DefaultHardwareBackBtnHandler, PermissionAwareActivity { |
| 22 | + |
| 23 | + public open val reactActivityDelegate: ReactActivityDelegate = createReactActivityDelegate() |
| 24 | + |
| 25 | + public open val reactDelegate: ReactDelegate? |
| 26 | + get() = reactActivityDelegate.reactDelegate |
| 27 | + |
| 28 | + protected open val reactHost: ReactHost? |
| 29 | + get() = reactActivityDelegate.reactHost |
| 30 | + |
| 31 | + protected val reactNativeHost: ReactNativeHost |
| 32 | + get() = reactActivityDelegate.reactNativeHost |
| 33 | + |
| 34 | + protected val reactInstanceManager: ReactInstanceManager |
| 35 | + get() = reactActivityDelegate.reactInstanceManager |
| 36 | + |
| 37 | + /** |
| 38 | + * Returns the name of the main component registered from JavaScript. This is used to schedule |
| 39 | + * rendering of the component. e.g. "MoviesApp" |
| 40 | + */ |
| 41 | + protected open val mainComponentName: String? |
| 42 | + get() = null |
| 43 | + |
| 44 | + /** Called at construction time, override if you have a custom delegate implementation. */ |
| 45 | + protected open fun createReactActivityDelegate(): ReactActivityDelegate { |
| 46 | + return ReactActivityDelegate(this, mainComponentName) |
| 47 | + } |
| 48 | + |
| 49 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 50 | + super.onCreate(savedInstanceState) |
| 51 | + reactActivityDelegate.onCreate(savedInstanceState) |
| 52 | + } |
| 53 | + |
| 54 | + override fun onPause() { |
| 55 | + super.onPause() |
| 56 | + reactActivityDelegate.onPause() |
| 57 | + } |
| 58 | + |
| 59 | + override fun onResume() { |
| 60 | + super.onResume() |
| 61 | + reactActivityDelegate.onResume() |
| 62 | + } |
| 63 | + |
| 64 | + override fun onDestroy() { |
| 65 | + super.onDestroy() |
| 66 | + reactActivityDelegate.onDestroy() |
| 67 | + } |
| 68 | + |
| 69 | + override public fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { |
| 70 | + super.onActivityResult(requestCode, resultCode, data) |
| 71 | + reactActivityDelegate.onActivityResult(requestCode, resultCode, data) |
| 72 | + } |
| 73 | + |
| 74 | + override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean = |
| 75 | + reactActivityDelegate.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event) |
| 76 | + |
| 77 | + override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean = |
| 78 | + reactActivityDelegate.onKeyUp(keyCode, event) || super.onKeyUp(keyCode, event) |
| 79 | + |
| 80 | + override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean = |
| 81 | + reactActivityDelegate.onKeyLongPress(keyCode, event) || super.onKeyLongPress(keyCode, event) |
| 82 | + |
| 83 | + @Suppress("DEPRECATION") |
| 84 | + @Deprecated("Deprecated in Java") |
| 85 | + override fun onBackPressed() { |
| 86 | + if (!reactActivityDelegate.onBackPressed()) { |
| 87 | + super.onBackPressed() |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + override fun invokeDefaultOnBackPressed() { |
| 92 | + @Suppress("DEPRECATION") super.onBackPressed() |
| 93 | + } |
| 94 | + |
| 95 | + override public fun onNewIntent(intent: Intent) { |
| 96 | + if (!reactActivityDelegate.onNewIntent(intent)) { |
| 97 | + super.onNewIntent(intent) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + override public fun onUserLeaveHint() { |
| 102 | + super.onUserLeaveHint() |
| 103 | + reactActivityDelegate.onUserLeaveHint() |
| 104 | + } |
| 105 | + |
| 106 | + override fun requestPermissions( |
| 107 | + permissions: Array<String>, |
| 108 | + requestCode: Int, |
| 109 | + listener: PermissionListener? |
| 110 | + ) { |
| 111 | + reactActivityDelegate.requestPermissions(permissions, requestCode, listener) |
| 112 | + } |
| 113 | + |
| 114 | + override fun onRequestPermissionsResult( |
| 115 | + requestCode: Int, |
| 116 | + permissions: Array<String>, |
| 117 | + grantResults: IntArray |
| 118 | + ) { |
| 119 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults) |
| 120 | + reactActivityDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults) |
| 121 | + } |
| 122 | + |
| 123 | + override fun onWindowFocusChanged(hasFocus: Boolean) { |
| 124 | + super.onWindowFocusChanged(hasFocus) |
| 125 | + reactActivityDelegate.onWindowFocusChanged(hasFocus) |
| 126 | + } |
| 127 | + |
| 128 | + override fun onConfigurationChanged(newConfig: Configuration) { |
| 129 | + super.onConfigurationChanged(newConfig) |
| 130 | + reactActivityDelegate.onConfigurationChanged(newConfig) |
| 131 | + } |
| 132 | + |
| 133 | + protected fun loadApp(appKey: String?) { |
| 134 | + reactActivityDelegate.loadApp(appKey) |
| 135 | + } |
| 136 | +} |
0 commit comments