-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathApp.kt
More file actions
52 lines (44 loc) · 1.99 KB
/
App.kt
File metadata and controls
52 lines (44 loc) · 1.99 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
package to.bitkit
import android.annotation.SuppressLint
import android.app.Activity
import android.app.Application
import android.app.Application.ActivityLifecycleCallbacks
import android.os.Bundle
import androidx.hilt.work.HiltWorkerFactory
import androidx.work.Configuration
import dagger.hilt.android.HiltAndroidApp
import to.bitkit.env.Env
import to.bitkit.services.BluetoothInit
import javax.inject.Inject
@HiltAndroidApp
internal open class App : Application(), Configuration.Provider {
@Inject
lateinit var workerFactory: HiltWorkerFactory
override val workManagerConfiguration
get() = Configuration.Builder()
.setWorkerFactory(workerFactory)
.build()
override fun onCreate() {
super.onCreate()
currentActivity = CurrentActivity().also { registerActivityLifecycleCallbacks(it) }
Env.initAppStoragePath(filesDir.absolutePath)
// Initialize btleplug for Bluetooth support (required before any BLE usage)
BluetoothInit.ensureInitialized()
}
companion object {
@SuppressLint("StaticFieldLeak") // Should be safe given its manual memory management
internal var currentActivity: CurrentActivity? = null
}
}
class CurrentActivity : ActivityLifecycleCallbacks {
var value: Activity? = null
private set
override fun onActivityCreated(activity: Activity, bundle: Bundle?) = Unit
override fun onActivityStarted(activity: Activity) = run { this.value = activity }
override fun onActivityResumed(activity: Activity) = run { this.value = activity }
override fun onActivityPaused(activity: Activity) = clearIfCurrent(activity)
override fun onActivityStopped(activity: Activity) = clearIfCurrent(activity)
override fun onActivitySaveInstanceState(activity: Activity, bundle: Bundle) = Unit
override fun onActivityDestroyed(activity: Activity) = clearIfCurrent(activity)
private fun clearIfCurrent(activity: Activity) = run { if (this.value == activity) this.value = null }
}