管灌系统巡查员智能手机App
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
package cc.shinichi.library
 
import android.app.Application
import android.content.Context
import androidx.media3.common.util.UnstableApi
import androidx.media3.datasource.cache.CacheDataSource
 
/**
 * 文件名: GlobalContext.java
 * 作者: kirito
 * 描述: 全局
 * 创建时间: 2024/11/27
 */
@UnstableApi
object GlobalContext {
    private var application: Application? = null
    private var cacheDataSourceFactory: CacheDataSource.Factory? = null
 
    fun init(app: Application, cacheDataSourceFactory: CacheDataSource.Factory) {
        if (application == null) {
            application = app
        }
        if (this.cacheDataSourceFactory == null) {
            this.cacheDataSourceFactory = cacheDataSourceFactory
        }
    }
 
    fun getApplication(): Application {
        return application ?: throw IllegalStateException("Application is not initialized")
    }
 
    fun getCacheDataSourceFactory(): CacheDataSource.Factory {
        return cacheDataSourceFactory ?: throw IllegalStateException("CacheDataSourceFactory is not initialized")
    }
 
    fun getContext(): Context {
        return getApplication().applicationContext
    }
}