forked from KhalisFoundation/sundar-gutka-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainActivity.kt
More file actions
98 lines (89 loc) · 3.57 KB
/
Copy pathMainActivity.kt
File metadata and controls
98 lines (89 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.WahegurooNetwork.SundarGutka
import com.facebook.react.ReactActivity
import com.facebook.react.ReactActivityDelegate
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.fabricEnabled
import com.facebook.react.defaults.DefaultReactActivityDelegate
import org.devio.rn.splashscreen.SplashScreen
import android.os.Bundle
import android.view.KeyEvent
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
class MainActivity : ReactActivity() {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
override fun getMainComponentName(): String = "SundarGutka"
override fun onCreate(savedInstanceState: Bundle?) {
SplashScreen.show(this)
super.onCreate(null)
// Lay out content edge-to-edge so the window draws behind system bars.
WindowCompat.setDecorFitsSystemWindows(window, false)
}
/**
* Called every time the window regains/loses focus. We re-apply sticky-immersive
* flags here so they survive notification shade pulls, lock/unlock cycles, etc.
*/
override fun onWindowFocusChanged(hasFocus: Boolean) {
super.onWindowFocusChanged(hasFocus)
if (hasFocus) {
hideSystemBars()
}
}
/**
* Set BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE so the system bars (status bar +
* gesture navigation bar) are hidden by default and reappear transiently when
* the user swipes from an edge, then auto-hide again without any app interaction.
*/
private fun hideSystemBars() {
val controller = WindowInsetsControllerCompat(window, window.decorView)
controller.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
controller.hide(WindowInsetsCompat.Type.systemBars())
}
/**
* Returns the instance of the [ReactActivityDelegate]. We use [DefaultReactActivityDelegate]
* which allows you to enable New Architecture with a single boolean flags [fabricEnabled]
*/
override fun createReactActivityDelegate(): ReactActivityDelegate =
DefaultReactActivityDelegate(this, mainComponentName, fabricEnabled)
/**
* Safely handle system dialog operations to prevent SecurityException
*/
private fun safeCloseSystemDialogs() {
try {
// Check if we have the permission and are on a compatible API level
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P &&
checkSelfPermission("android.permission.BROADCAST_CLOSE_SYSTEM_DIALOGS") == PackageManager.PERMISSION_GRANTED) {
val closeDialogIntent = Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)
sendBroadcast(closeDialogIntent)
}
} catch (e: SecurityException) {
// Log the exception but don't crash
e.printStackTrace()
} catch (e: Exception) {
// Handle any other exceptions
e.printStackTrace()
}
}
/**
* Override onKeyDown to safely handle system dialog operations
*/
override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
try {
return super.onKeyDown(keyCode, event)
} catch (e: SecurityException) {
// If we get a SecurityException, try to handle it gracefully
if (e.message?.contains("BROADCAST_CLOSE_SYSTEM_DIALOGS") == true) {
// Log the issue but don't crash
e.printStackTrace()
return true // Consume the event
}
throw e // Re-throw if it's not related to our permission
}
}
}