@@ -15,6 +15,7 @@ import androidx.lifecycle.LifecycleObserver
1515import androidx.lifecycle.OnLifecycleEvent
1616import androidx.lifecycle.ProcessLifecycleOwner
1717import androidx.multidex.MultiDexApplication
18+ import dagger.Lazy
1819import io.github.sds100.keymapper.base.expertmode.SystemBridgeAutoStarter
1920import io.github.sds100.keymapper.base.logging.KeyMapperLoggingTree
2021import io.github.sds100.keymapper.base.logging.SystemBridgeLogger
@@ -51,46 +52,46 @@ abstract class BaseKeyMapperApp : MultiDexApplication() {
5152 private val tag = BaseKeyMapperApp ::class .simpleName
5253
5354 @Inject
54- lateinit var appCoroutineScope: CoroutineScope
55+ lateinit var appCoroutineScope: Lazy < CoroutineScope >
5556
5657 @Inject
57- lateinit var notificationController: NotificationController
58+ lateinit var notificationController: Lazy < NotificationController >
5859
5960 @Inject
60- lateinit var packageManagerAdapter: AndroidPackageManagerAdapter
61+ lateinit var packageManagerAdapter: Lazy < AndroidPackageManagerAdapter >
6162
6263 @Inject
63- lateinit var devicesAdapter: AndroidDevicesAdapter
64+ lateinit var devicesAdapter: Lazy < AndroidDevicesAdapter >
6465
6566 @Inject
66- lateinit var permissionAdapter: AndroidPermissionAdapter
67+ lateinit var permissionAdapter: Lazy < AndroidPermissionAdapter >
6768
6869 @Inject
69- lateinit var accessibilityServiceAdapter: AccessibilityServiceAdapterImpl
70+ lateinit var accessibilityServiceAdapter: Lazy < AccessibilityServiceAdapterImpl >
7071
7172 @Inject
72- lateinit var autoGrantPermissionController: AutoGrantPermissionController
73+ lateinit var autoGrantPermissionController: Lazy < AutoGrantPermissionController >
7374
7475 @Inject
75- lateinit var loggingTree: KeyMapperLoggingTree
76+ lateinit var loggingTree: Lazy < KeyMapperLoggingTree >
7677
7778 @Inject
78- lateinit var settingsRepository: PreferenceRepositoryImpl
79+ lateinit var settingsRepository: Lazy < PreferenceRepositoryImpl >
7980
8081 @Inject
81- lateinit var logRepository: LogRepository
82+ lateinit var logRepository: Lazy < LogRepository >
8283
8384 @Inject
84- lateinit var keyEventRelayServiceWrapper: KeyEventRelayServiceWrapperImpl
85+ lateinit var keyEventRelayServiceWrapper: Lazy < KeyEventRelayServiceWrapperImpl >
8586
8687 @Inject
87- lateinit var systemBridgeAutoStarter: SystemBridgeAutoStarter
88+ lateinit var systemBridgeAutoStarter: Lazy < SystemBridgeAutoStarter >
8889
8990 @Inject
90- lateinit var systemBridgeConnectionManager: SystemBridgeConnectionManagerImpl
91+ lateinit var systemBridgeConnectionManager: Lazy < SystemBridgeConnectionManagerImpl >
9192
9293 @Inject
93- lateinit var systemBridgeLogger: SystemBridgeLogger
94+ lateinit var systemBridgeLogger: Lazy < SystemBridgeLogger >
9495
9596 private val processLifecycleOwner by lazy { ProcessLifecycleOwner .get() }
9697
@@ -118,16 +119,18 @@ abstract class BaseKeyMapperApp : MultiDexApplication() {
118119 Log .i(tag, " KeyMapperApp: OnCreate" )
119120
120121 Thread .setDefaultUncaughtExceptionHandler { thread, exception ->
121- // log in a blocking manner and always log regardless of whether the setting is turned on
122- val entry = LogEntryEntity (
123- id = 0 ,
124- time = Calendar .getInstance().timeInMillis,
125- severity = LogEntryEntity .SEVERITY_ERROR ,
126- message = exception.stackTraceToString(),
127- )
128-
129- runBlocking {
130- logRepository.insertSuspend(entry)
122+ if (userManager?.isUserUnlocked != false ) {
123+ // log in a blocking manner and always log regardless of whether the setting is turned on
124+ val entry = LogEntryEntity (
125+ id = 0 ,
126+ time = Calendar .getInstance().timeInMillis,
127+ severity = LogEntryEntity .SEVERITY_ERROR ,
128+ message = exception.stackTraceToString(),
129+ )
130+
131+ runBlocking {
132+ logRepository.get().insertSuspend(entry)
133+ }
131134 }
132135
133136 priorExceptionHandler?.uncaughtException(thread, exception)
@@ -168,7 +171,7 @@ abstract class BaseKeyMapperApp : MultiDexApplication() {
168171
169172 registerReceiver(broadcastReceiver, intentFilter)
170173
171- settingsRepository.get(Keys .darkTheme)
174+ settingsRepository.get().get( Keys .darkTheme)
172175 .map { it?.toIntOrNull() }
173176 .map {
174177 when (it) {
@@ -178,35 +181,35 @@ abstract class BaseKeyMapperApp : MultiDexApplication() {
178181 }
179182 }
180183 .onEach { mode -> AppCompatDelegate .setDefaultNightMode(mode) }
181- .launchIn(appCoroutineScope)
184+ .launchIn(appCoroutineScope.get() )
182185
183186 if (BuildConfig .BUILD_TYPE == " debug" || BuildConfig .BUILD_TYPE == " debug_release" ) {
184187 Timber .plant(Timber .DebugTree ())
185188 }
186189
187- Timber .plant(loggingTree)
190+ Timber .plant(loggingTree.get() )
188191
189- notificationController.init ()
192+ notificationController.get(). init ()
190193
191194 processLifecycleOwner.lifecycle.addObserver(
192195 object : LifecycleObserver {
193196 @Suppress(" DEPRECATION" )
194197 @OnLifecycleEvent(Lifecycle .Event .ON_RESUME )
195198 fun onResume () {
196199 // when the user returns to the app let everything know that the permissions could have changed
197- notificationController.onOpenApp()
200+ notificationController.get(). onOpenApp()
198201
199202 if (BuildConfig .DEBUG &&
200- permissionAdapter.isGranted(Permission .WRITE_SECURE_SETTINGS )
203+ permissionAdapter.get(). isGranted(Permission .WRITE_SECURE_SETTINGS )
201204 ) {
202- accessibilityServiceAdapter.start()
205+ accessibilityServiceAdapter.get(). start()
203206 }
204207 }
205208 },
206209 )
207210
208- appCoroutineScope.launch {
209- notificationController.openApp.collectLatest { intentAction ->
211+ appCoroutineScope.get(). launch {
212+ notificationController.get(). openApp.collectLatest { intentAction ->
210213 Intent (this @BaseKeyMapperApp, getMainActivityClass()).apply {
211214 action = intentAction
212215 flags = Intent .FLAG_ACTIVITY_NEW_TASK
@@ -216,38 +219,38 @@ abstract class BaseKeyMapperApp : MultiDexApplication() {
216219 }
217220 }
218221
219- notificationController.showToast.onEach { toast ->
222+ notificationController.get(). showToast.onEach { toast ->
220223 Toast .makeText(this , toast, Toast .LENGTH_SHORT ).show()
221- }.launchIn(appCoroutineScope)
224+ }.launchIn(appCoroutineScope.get() )
222225
223- autoGrantPermissionController.start()
224- keyEventRelayServiceWrapper.bind()
226+ autoGrantPermissionController.get(). start()
227+ keyEventRelayServiceWrapper.get(). bind()
225228
226- if (systemBridgeConnectionManager.isConnected()) {
229+ if (systemBridgeConnectionManager.get(). isConnected()) {
227230 Timber .i(" KeyMapperApp: System bridge is connected" )
228231 } else {
229232 Timber .i(" KeyMapperApp: System bridge is disconnected" )
230233 }
231234
232- systemBridgeAutoStarter.init ()
235+ systemBridgeAutoStarter.get(). init ()
233236
234237 // Initialize SystemBridgeLogger to start receiving log messages from SystemBridge.
235238 // Using Lazy<> to avoid circular dependency issues and ensure it's only created
236239 // when the API level requirement is met.
237- systemBridgeLogger.start()
240+ systemBridgeLogger.get(). start()
238241
239- appCoroutineScope.launch {
240- systemBridgeConnectionManager.connectionState.collect { state ->
242+ appCoroutineScope.get(). launch {
243+ systemBridgeConnectionManager.get(). connectionState.collect { state ->
241244 if (state is SystemBridgeConnectionState .Connected ) {
242245 val isUsed =
243- settingsRepository.get(Keys .isSystemBridgeUsed).first() ? : false
246+ settingsRepository.get().get( Keys .isSystemBridgeUsed).first() ? : false
244247
245248 // Enable the setting to use PRO mode for key event actions the first time they use PRO mode.
246249 if (! isUsed) {
247- settingsRepository.set(Keys .keyEventActionsUseSystemBridge, true )
250+ settingsRepository.get(). set(Keys .keyEventActionsUseSystemBridge, true )
248251 }
249252
250- settingsRepository.set(Keys .isSystemBridgeUsed, true )
253+ settingsRepository.get(). set(Keys .isSystemBridgeUsed, true )
251254 }
252255 }
253256 }
0 commit comments