Skip to content

Commit

Permalink
Update: Koin-inject PowerManager
Browse files Browse the repository at this point in the history
  • Loading branch information
machiav3lli committed Nov 8, 2024
1 parent 82cd135 commit 9b017d8
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/machiav3lli/backup/OABX.kt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import org.koin.android.ext.koin.androidContext
import org.koin.android.ext.koin.androidLogger
import org.koin.core.context.startKoin
import org.koin.dsl.module
import org.koin.java.KoinJavaComponent.get
import timber.log.Timber
import java.lang.ref.WeakReference
import java.util.concurrent.ConcurrentLinkedQueue
Expand Down Expand Up @@ -544,7 +545,7 @@ class OABX : Application() {
if (aquire) {
traceDebug { "%%%%% $wakeLockTag wakelock aquire (before: $wakeLockNested)" }
if (wakeLockNested.accumulateAndGet(+1, Int::plus) == 1) {
val pm = context.getSystemService(POWER_SERVICE) as PowerManager
val pm : PowerManager = get(PowerManager::class.java)
theWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, wakeLockTag)
theWakeLock?.acquire(60 * 60 * 1000L)
traceDebug { "%%%%% $wakeLockTag wakelock ACQUIRED" }
Expand Down Expand Up @@ -741,4 +742,5 @@ class OABX : Application() {
val handlersModule = module {
single { WorkHandler(get()) }
single { ExportsHandler(get()) }
single { get<Context>().getSystemService(Context.POWER_SERVICE) as PowerManager }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package com.machiav3lli.backup.activities
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.PowerManager
import androidx.activity.SystemBarStyle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
Expand Down Expand Up @@ -113,7 +112,6 @@ class MainActivityX : BaseActivity() {

private val mScope: CoroutineScope = MainScope()
lateinit var navController: NavHostController
private lateinit var powerManager: PowerManager

private lateinit var openDialog: MutableState<Boolean>
private lateinit var dialogKey: MutableState<DialogKey?>
Expand Down Expand Up @@ -199,8 +197,6 @@ class MainActivityX : BaseActivity() {
return
}

powerManager = this.getSystemService(POWER_SERVICE) as PowerManager

setContent {

navController = rememberNavController()
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/machiav3lli/backup/pages/PermissionsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,16 @@ import com.machiav3lli.backup.utils.setBackupDir
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.koin.compose.koinInject
import timber.log.Timber

// TODO use rememberPermissionState to manage more permissions
@OptIn(ExperimentalPermissionsApi::class)
@Composable
fun PermissionsPage() {
fun PermissionsPage(powerManager: PowerManager = koinInject()) {
val context = LocalContext.current
val mScope = CoroutineScope(Dispatchers.Main)
val mainActivity = OABX.main!!
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val openDialog = remember { mutableStateOf(false) }
val dialogProp: MutableState<DialogMode> = remember {
mutableStateOf(DialogMode.NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ import androidx.core.app.ActivityCompat
import com.machiav3lli.backup.BACKUP_DIRECTORY_INTENT
import com.machiav3lli.backup.OABX
import com.machiav3lli.backup.preferences.persist_ignoreBatteryOptimization
import org.koin.java.KoinJavaComponent.get

// Getters

val Context.allPermissionsGranted: Boolean
get() {
val powerManager = getSystemService(AppCompatActivity.POWER_SERVICE) as PowerManager
val powerManager: PowerManager = get(PowerManager::class.java)
return hasStoragePermissions &&
isStorageDirSetAndOk &&
checkSMSMMSPermission &&
Expand Down

0 comments on commit 9b017d8

Please sign in to comment.