summaryrefslogtreecommitdiff
path: root/app/src/main/java/com/draco/buoy/utils/BatterySaverManager.kt
blob: 8bf76a6f963179659eeba1c1b424599c10e4be83 (plain)
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
package com.draco.buoy.utils

import android.content.ContentResolver
import android.provider.Settings
import com.draco.buoy.models.BatterySaverConstantsConfig
import com.draco.buoy.repositories.constants.BatterySaverSecureSettings

class BatterySaverManager(private val contentResolver: ContentResolver) {
    /**
     * Enable or disable low power mode
     */
    fun setLowPower(state: Boolean) {
        val intBool = if (state) 1 else 0
        Settings.Global.putInt(
            contentResolver,
            BatterySaverSecureSettings.LOW_POWER,
            intBool
        )
    }

    /**
     * Return true if low power mode is enabled
     */
    fun getLowPower(): Boolean {
        return Settings.Global.getInt(
            contentResolver,
            BatterySaverSecureSettings.LOW_POWER,
            0
        ) == 1
    }

    /**
     * Enable or disable low power sticky mode
     */
    fun setLowPowerSticky(state: Boolean) {
        val intBool = if (state) 1 else 0
        Settings.Global.putInt(
            contentResolver,
            BatterySaverSecureSettings.LOW_POWER_STICKY,
            intBool
        )
    }

    /**
     * Enable or disable low power sticky auto disable mode
     */
    fun setLowPowerStickyAutoDisableEnabled(state: Boolean) {
        val intBool = if (state) 1 else 0
        Settings.Global.putInt(
            contentResolver,
            BatterySaverSecureSettings.LOW_POWER_STICKY_AUTO_DISABLE_ENABLED,
            intBool
        )
    }

    /**
     * Set the raw battery saver constants secure setting
     */
    fun setConstantsString(constants: String?) {
        Settings.Global.putString(
            contentResolver,
            BatterySaverSecureSettings.BATTERY_SAVER_CONSTANTS,
            constants
        )
    }

    /**
     * Get the raw battery saver constants secure setting
     */
    fun getConstantsString(): String? {
        return Settings.Global.getString(
            contentResolver,
            BatterySaverSecureSettings.BATTERY_SAVER_CONSTANTS
        )
    }

    /**
     * Set the battery saver constants secure setting via a config
     */
    fun setConstantsConfig(config: BatterySaverConstantsConfig) {
        setConstantsString(config.toString())
    }

    /**
     * Quick way to apply either type of config
     */
    fun apply(config: Any?) {
        when (config) {
            is String? -> setConstantsString(config)
            is BatterySaverConstantsConfig -> setConstantsConfig(config)
        }

        //setLowPower(true)
        setLowPowerSticky(true)
        setLowPowerStickyAutoDisableEnabled(false)
    }

    /**
     * Reset constants to default values
     */
    fun resetToDefault() {
        apply(null)

        setLowPowerSticky(false)
        setLowPowerStickyAutoDisableEnabled(true)
    }
}