-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMainActivity.kt
More file actions
212 lines (176 loc) · 7.3 KB
/
MainActivity.kt
File metadata and controls
212 lines (176 loc) · 7.3 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright 2017 - 2025 Riigi Infosüsteemi Amet
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
@file:Suppress("PackageName", "FunctionName")
package ee.ria.DigiDoc
import android.content.Context
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.google.firebase.Firebase
import com.google.firebase.crashlytics.crashlytics
import dagger.hilt.android.AndroidEntryPoint
import ee.ria.DigiDoc.common.model.AppState
import ee.ria.DigiDoc.domain.model.theme.ThemeSetting
import ee.ria.DigiDoc.domain.preferences.DataStore
import ee.ria.DigiDoc.fragment.RootFragment
import ee.ria.DigiDoc.init.LibrarySetup
import ee.ria.DigiDoc.manager.ActivityManager
import ee.ria.DigiDoc.root.RootChecker
import ee.ria.DigiDoc.ui.theme.RIADigiDocTheme
import ee.ria.DigiDoc.utils.locale.LocaleUtil
import ee.ria.DigiDoc.utils.locale.LocaleUtilImpl
import ee.ria.DigiDoc.utils.secure.SecureUtil
import ee.ria.DigiDoc.utilsLib.R.string.main_diagnostics_logging_key
import ee.ria.DigiDoc.utilsLib.R.string.main_diagnostics_logging_running_key
import ee.ria.DigiDoc.utilsLib.file.FileUtil.getExternalFileUris
import ee.ria.DigiDoc.utilsLib.file.FileUtil.getLogsDirectory
import ee.ria.DigiDoc.utilsLib.locale.LocaleUtil.getLocale
import ee.ria.DigiDoc.utilsLib.logging.LoggingUtil
import kotlinx.coroutines.launch
import java.util.logging.Logger
import javax.inject.Inject
@AndroidEntryPoint
class MainActivity :
ComponentActivity(),
DefaultLifecycleObserver {
@Inject
lateinit var librarySetup: LibrarySetup
@Inject
lateinit var dataStore: DataStore
@Inject
lateinit var fileTypeSetup: FileTypeSetup
@Inject
lateinit var activityManager: ActivityManager
@Inject
lateinit var rootChecker: RootChecker
@Inject
lateinit var localeUtil: LocaleUtil
@Inject
lateinit var loggingUtil: LoggingUtil
@Inject
lateinit var secureUtil: SecureUtil
private var isAppReady = false
override fun onCreate(savedInstanceState: Bundle?) {
super<ComponentActivity>.onCreate(savedInstanceState)
val isDebug = BuildConfig.BUILD_TYPE.contentEquals("debug")
if (!isDebug) {
secureUtil.markAsSecure(this)
}
installSplashScreen().setKeepOnScreenCondition {
!isAppReady
}
enableEdgeToEdge()
val useDarkMode =
if (isSystemModeEnabled(dataStore) == true) {
null
} else {
isDarkModeEnabled(dataStore)
}
if (rootChecker.isRooted()) {
setContent {
RIADigiDocTheme(darkTheme = useDarkMode) {
RootFragment()
}
}
return
}
lifecycle.addObserver(this)
val componentClassName = this.javaClass.name
val locale = dataStore.getLocale() ?: getLocale("en")
val webEidUri = intent.data?.takeIf { it.scheme == "web-eid-mobile" }
if (webEidUri != null) {
val browserPackage =
intent
.getStringExtra("com.android.browser.application_id")
?.takeIf { it.isNotEmpty() }
dataStore.setWebEidBrowserPackage(browserPackage)
}
val externalFileUris =
if (webEidUri != null) {
listOf()
} else {
getExternalFileUris(intent)
}
localeUtil.updateLocale(applicationContext, locale)
// Observe if activity needs to be recreated for changes to take effect (eg. Settings)
activityManager.shouldRecreateActivity.observe(this) { shouldRecreate ->
if (shouldRecreate) {
activityManager.setShouldRecreateActivity(false)
activityManager.recreateActivity(this)
}
}
Firebase.crashlytics.isCrashlyticsCollectionEnabled = false
activityManager.shouldResetLogging.observe(this) { shouldResetLogging ->
if (shouldResetLogging && !isDebug) {
activityManager.setShouldResetLogging(false)
loggingUtil.handleOneTimeLogging(this)
LoggingUtil.resetLogs(getLogsDirectory(this))
}
}
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
val isDiagnosticsLoggingEnabled = sharedPreferences.getBoolean(getString(main_diagnostics_logging_key), false)
val isDiagnosticsLoggingRunning =
sharedPreferences.getBoolean(
getString(main_diagnostics_logging_running_key),
false,
)
val isLoggingEnabled = isDebug || (isDiagnosticsLoggingEnabled && isDiagnosticsLoggingRunning)
lifecycleScope.launch {
LoggingUtil.initialize(
applicationContext,
Logger.getLogger(MainActivity::class.java.name),
isLoggingEnabled,
)
fileTypeSetup.initializeApplicationFileTypesAssociation(componentClassName)
librarySetup.setupLibraries(applicationContext, isLoggingEnabled)
isAppReady = true
setContent {
RIADigiDocTheme(darkTheme = useDarkMode) {
RIADigiDocAppScreen(
externalFileUris = externalFileUris,
webEidUri = webEidUri,
)
}
}
}
}
override fun attachBaseContext(newBase: Context?) {
// Instantiating here manually, as "attachBaseContext" runs before Hilt
val localeUtilImpl = LocaleUtilImpl()
val language = localeUtilImpl.getPreferredLanguage(newBase)
val localizedContext = newBase?.let { localeUtilImpl.updateLocale(it, getLocale(language)) }
super.attachBaseContext(localizedContext)
}
override fun onStart(owner: LifecycleOwner) {
super<DefaultLifecycleObserver>.onStart(owner)
AppState.isAppInForeground = true
}
override fun onStop(owner: LifecycleOwner) {
super<DefaultLifecycleObserver>.onStop(owner)
AppState.isAppInForeground = false
}
private fun isSystemModeEnabled(dataStore: DataStore): Boolean = dataStore.getThemeSetting() == ThemeSetting.SYSTEM
private fun isDarkModeEnabled(dataStore: DataStore): Boolean = dataStore.getThemeSetting() == ThemeSetting.DARK
}