Skip to content

Commit 3b0a395

Browse files
committed
Improve memory and temperature tiile states
1 parent 98eb4ca commit 3b0a395

File tree

14 files changed

+65
-151
lines changed

14 files changed

+65
-151
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@
260260
<action android:name="android.service.quicksettings.action.QS_TILE" />
261261
</intent-filter>
262262
<meta-data
263-
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
263+
android:name="android.service.quicksettings.INACTIVE_TILE"
264264
android:value="true" />
265265
</service>
266266

@@ -318,14 +318,14 @@
318318
<service
319319
android:name=".tiles.temperature.TemperatureTileService"
320320
android:exported="true"
321-
android:icon="@drawable/ic_temperature_off"
321+
android:icon="@drawable/ic_temperature"
322322
android:label="@string/temperature_tile"
323323
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
324324
<intent-filter>
325325
<action android:name="android.service.quicksettings.action.QS_TILE" />
326326
</intent-filter>
327327
<meta-data
328-
android:name="android.service.quicksettings.TOGGLEABLE_TILE"
328+
android:name="android.service.quicksettings.INACTIVE_TILE"
329329
android:value="true" />
330330
</service>
331331
</application>

app/src/main/java/com/wstxda/toolkit/manager/memory/MemoryManager.kt

Lines changed: 36 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class MemoryManager(context: Context) {
2727
private val appContext = context.applicationContext
2828
private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
2929

30-
private val _currentState = MutableStateFlow(MemoryState.DISABLED)
30+
private val activityManager = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
31+
private val memoryInfo = ActivityManager.MemoryInfo()
32+
33+
private val _currentState = MutableStateFlow(MemoryState.RAM)
3134
val currentState = _currentState.asStateFlow()
3235

3336
private val _usedValue = MutableStateFlow("")
@@ -44,51 +47,39 @@ class MemoryManager(context: Context) {
4447

4548
init {
4649
val prefs = appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
47-
val savedState = prefs.getString(KEY_STATE, MemoryState.DISABLED.name)
48-
_currentState.value = runCatching {
49-
MemoryState.valueOf(savedState!!)
50-
}.getOrDefault(MemoryState.DISABLED)
50+
val savedStateName = prefs.getString(KEY_STATE, MemoryState.RAM.name)
51+
val savedState = runCatching { MemoryState.valueOf(savedStateName!!) }.getOrNull()
52+
_currentState.value = savedState ?: MemoryState.RAM
5153
}
5254

5355
fun toggle() {
54-
val nextState = when (_currentState.value) {
55-
MemoryState.DISABLED -> MemoryState.RAM
56-
MemoryState.RAM -> MemoryState.STORAGE
57-
MemoryState.STORAGE -> MemoryState.DISABLED
58-
}
59-
56+
val nextState =
57+
if (_currentState.value == MemoryState.RAM) MemoryState.STORAGE else MemoryState.RAM
6058
_currentState.value = nextState
61-
6259
appContext.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE).edit {
6360
putString(KEY_STATE, nextState.name)
6461
}
6562

66-
if (nextState != MemoryState.DISABLED) {
67-
updateData()
68-
} else {
69-
clearData()
70-
}
71-
72-
updatePollingState()
63+
updateData()
7364
}
7465

7566
fun setListening(listening: Boolean) {
67+
if (isPanelOpen == listening) return
7668
isPanelOpen = listening
7769
updatePollingState()
7870
}
7971

8072
private fun updatePollingState() {
81-
val shouldPoll = _currentState.value != MemoryState.DISABLED && isPanelOpen
82-
83-
if (shouldPoll) {
84-
if (pollingJob?.isActive != true) startPolling()
73+
if (isPanelOpen) {
74+
startPolling()
8575
} else {
8676
stopPolling()
8777
}
8878
}
8979

9080
private fun startPolling() {
91-
pollingJob?.cancel()
81+
if (pollingJob?.isActive == true) return
82+
9283
pollingJob = managerScope.launch {
9384
updateData()
9485
while (isActive) {
@@ -103,48 +94,39 @@ class MemoryManager(context: Context) {
10394
pollingJob = null
10495
}
10596

106-
private fun clearData() {
107-
_usedValue.value = ""
108-
_totalValue.value = ""
109-
_detailValue.value = ""
110-
}
111-
11297
private fun updateData() {
113-
when (_currentState.value) {
114-
MemoryState.RAM -> updateRamInfo()
115-
MemoryState.STORAGE -> updateStorageInfo()
116-
MemoryState.DISABLED -> {}
98+
try {
99+
when (_currentState.value) {
100+
MemoryState.RAM -> updateRamInfo()
101+
MemoryState.STORAGE -> updateStorageInfo()
102+
}
103+
} catch (e: Exception) {
104+
e.printStackTrace()
117105
}
118106
}
119107

120108
private fun updateRamInfo() {
121-
val am = appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
122-
val memInfo = ActivityManager.MemoryInfo()
123-
am.getMemoryInfo(memInfo)
109+
activityManager.getMemoryInfo(memoryInfo)
124110

125-
val usedBytes = memInfo.totalMem - memInfo.availMem
126-
val totalBytes = memInfo.totalMem
127-
val percent = (usedBytes.toDouble() / totalBytes.toDouble() * 100).toInt()
111+
val totalBytes = memoryInfo.totalMem
112+
val usedBytes = totalBytes - memoryInfo.availMem
113+
val percent = ((usedBytes * 100) / totalBytes).toInt()
128114

129115
_usedValue.value = Formatter.formatShortFileSize(appContext, usedBytes)
130116
_totalValue.value = Formatter.formatShortFileSize(appContext, totalBytes)
131117
_detailValue.value = "$percent%"
132118
}
133119

134120
private fun updateStorageInfo() {
135-
try {
136-
val path = Environment.getDataDirectory()
137-
val stat = StatFs(path.path)
138-
139-
val totalBytes = stat.blockCountLong * stat.blockSizeLong
140-
val freeBytes = stat.availableBlocksLong * stat.blockSizeLong
141-
val usedBytes = totalBytes - freeBytes
142-
143-
_usedValue.value = Formatter.formatShortFileSize(appContext, usedBytes)
144-
_totalValue.value = Formatter.formatShortFileSize(appContext, totalBytes)
145-
_detailValue.value = Formatter.formatShortFileSize(appContext, freeBytes)
146-
} catch (_: Exception) {
147-
clearData()
148-
}
121+
val path = Environment.getDataDirectory()
122+
val stat = StatFs(path.path)
123+
124+
val totalBytes = stat.blockCountLong * stat.blockSizeLong
125+
val freeBytes = stat.availableBlocksLong * stat.blockSizeLong
126+
val usedBytes = totalBytes - freeBytes
127+
128+
_usedValue.value = Formatter.formatShortFileSize(appContext, usedBytes)
129+
_totalValue.value = Formatter.formatShortFileSize(appContext, totalBytes)
130+
_detailValue.value = Formatter.formatShortFileSize(appContext, freeBytes)
149131
}
150132
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package com.wstxda.toolkit.manager.memory
22

33
enum class MemoryState {
4-
DISABLED, RAM, STORAGE
4+
RAM, STORAGE
55
}

app/src/main/java/com/wstxda/toolkit/manager/temperature/TemperatureManager.kt

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,21 @@ class TemperatureManager(context: Context) {
2222

2323
private val appContext = context.applicationContext
2424
private val managerScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
25-
26-
private val _isActive = MutableStateFlow(false)
27-
val isActive = _isActive.asStateFlow()
28-
2925
private val _temperature = MutableStateFlow(0f)
3026
val temperature = _temperature.asStateFlow()
31-
3227
private var pollingJob: Job? = null
33-
private var isPanelOpen = false
34-
35-
fun toggle() {
36-
_isActive.value = !_isActive.value
37-
38-
if (_isActive.value) {
39-
updateData()
40-
}
41-
42-
updatePollingState()
43-
}
4428

4529
fun setListening(listening: Boolean) {
46-
isPanelOpen = listening
47-
updatePollingState()
48-
}
49-
50-
private fun updatePollingState() {
51-
val shouldPoll = _isActive.value && isPanelOpen
52-
53-
if (shouldPoll) {
54-
if (pollingJob?.isActive != true) startPolling()
30+
if (listening) {
31+
startPolling()
5532
} else {
5633
stopPolling()
5734
}
5835
}
5936

6037
private fun startPolling() {
61-
pollingJob?.cancel()
38+
if (pollingJob?.isActive == true) return
39+
6240
pollingJob = managerScope.launch {
6341
updateData()
6442
while (isActive) {

app/src/main/java/com/wstxda/toolkit/tiles/memory/MemoryTileService.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import android.service.quicksettings.Tile
44
import com.wstxda.toolkit.base.BaseTileService
55
import com.wstxda.toolkit.manager.memory.MemoryManager
66
import com.wstxda.toolkit.manager.memory.MemoryModule
7-
import com.wstxda.toolkit.manager.memory.MemoryState
87
import com.wstxda.toolkit.ui.icon.MemoryIconProvider
98
import com.wstxda.toolkit.ui.label.MemoryLabelProvider
109
import kotlinx.coroutines.flow.Flow
@@ -45,9 +44,9 @@ class MemoryTileService : BaseTileService() {
4544
val detail = memoryManager.detailValue.value
4645

4746
setTileState(
48-
state = if (state == MemoryState.DISABLED) Tile.STATE_INACTIVE else Tile.STATE_ACTIVE,
47+
state = Tile.STATE_INACTIVE,
4948
label = memoryLabelProvider.getLabel(state, detail),
50-
subtitle = memoryLabelProvider.getSubtitle(state, used, total),
49+
subtitle = memoryLabelProvider.getSubtitle(used, total),
5150
icon = memoryIconProvider.getIcon(state)
5251
)
5352
}

app/src/main/java/com/wstxda/toolkit/tiles/temperature/TemperatureTileService.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ package com.wstxda.toolkit.tiles.temperature
22

33
import android.service.quicksettings.Tile
44
import com.wstxda.toolkit.base.BaseTileService
5-
import com.wstxda.toolkit.manager.temperature.TemperatureManager
65
import com.wstxda.toolkit.manager.temperature.TemperatureModule
76
import com.wstxda.toolkit.ui.icon.TemperatureIconProvider
87
import com.wstxda.toolkit.ui.label.TemperatureLabelProvider
98
import kotlinx.coroutines.flow.Flow
109

1110
class TemperatureTileService : BaseTileService() {
1211

13-
private val temperatureManager: TemperatureManager by lazy { TemperatureModule.getInstance(applicationContext) }
12+
private val temperatureManager by lazy { TemperatureModule.getInstance(applicationContext) }
1413
private val temperatureLabelProvider by lazy { TemperatureLabelProvider(applicationContext) }
1514
private val temperatureIconProvider by lazy { TemperatureIconProvider(applicationContext) }
1615

@@ -25,24 +24,20 @@ class TemperatureTileService : BaseTileService() {
2524
}
2625

2726
override fun onClick() {
28-
temperatureManager.toggle()
2927
}
3028

3129
override fun flowsToCollect(): List<Flow<*>> {
32-
return listOf(
33-
temperatureManager.isActive, temperatureManager.temperature
34-
)
30+
return listOf(temperatureManager.temperature)
3531
}
3632

3733
override fun updateTile() {
38-
val isActive = temperatureManager.isActive.value
3934
val temp = temperatureManager.temperature.value
4035

4136
setTileState(
42-
state = if (isActive) Tile.STATE_ACTIVE else Tile.STATE_INACTIVE,
43-
label = temperatureLabelProvider.getLabel(isActive, temp),
44-
subtitle = temperatureLabelProvider.getSubtitle(isActive),
45-
icon = temperatureIconProvider.getIcon(isActive)
37+
state = Tile.STATE_INACTIVE,
38+
label = temperatureLabelProvider.getLabel(temp),
39+
subtitle = temperatureLabelProvider.getSubtitle(),
40+
icon = temperatureIconProvider.getIcon()
4641
)
4742
}
4843
}

app/src/main/java/com/wstxda/toolkit/ui/icon/MemoryIconProvider.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class MemoryIconProvider(private val context: Context) {
99

1010
fun getIcon(state: MemoryState): Icon {
1111
val resId = when (state) {
12-
MemoryState.DISABLED -> R.drawable.ic_memory
1312
MemoryState.RAM -> R.drawable.ic_ram
1413
MemoryState.STORAGE -> R.drawable.ic_storage
1514
}

app/src/main/java/com/wstxda/toolkit/ui/icon/TemperatureIconProvider.kt

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import com.wstxda.toolkit.R
66

77
class TemperatureIconProvider(private val context: Context) {
88

9-
fun getIcon(isActive: Boolean): Icon {
10-
val iconRes = if (isActive) {
11-
R.drawable.ic_temperature_on
12-
} else {
13-
R.drawable.ic_temperature_off
14-
}
15-
return Icon.createWithResource(context, iconRes)
9+
fun getIcon(): Icon {
10+
return Icon.createWithResource(context, R.drawable.ic_temperature)
1611
}
1712
}

app/src/main/java/com/wstxda/toolkit/ui/label/MemoryLabelProvider.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,16 @@ import com.wstxda.toolkit.manager.memory.MemoryState
77
class MemoryLabelProvider(private val context: Context) {
88

99
fun getLabel(state: MemoryState, detail: String): CharSequence {
10-
return when (state) {
11-
MemoryState.DISABLED -> context.getString(R.string.memory_tile)
12-
13-
MemoryState.RAM -> {
14-
if (detail.isBlank()) return context.getString(R.string.memory_tile_empty)
15-
context.getString(R.string.memory_tile_ram, detail)
16-
}
10+
if (detail.isBlank()) return context.getString(R.string.memory_tile)
1711

18-
MemoryState.STORAGE -> {
19-
if (detail.isBlank()) return context.getString(R.string.memory_tile_empty)
20-
context.getString(R.string.memory_tile_storage, detail)
21-
}
12+
return when (state) {
13+
MemoryState.RAM -> context.getString(R.string.memory_tile_ram, detail)
14+
MemoryState.STORAGE -> context.getString(R.string.memory_tile_storage, detail)
2215
}
2316
}
2417

25-
fun getSubtitle(state: MemoryState, used: String, total: String): CharSequence? {
26-
return when (state) {
27-
MemoryState.DISABLED -> context.getString(R.string.tile_switch)
28-
29-
MemoryState.RAM, MemoryState.STORAGE -> {
30-
if (used.isBlank() || total.isBlank()) return null
31-
context.getString(R.string.memory_tile_format, used, total)
32-
}
33-
}
18+
fun getSubtitle(used: String, total: String): CharSequence? {
19+
if (used.isBlank() || total.isBlank()) return null
20+
return context.getString(R.string.memory_tile_format, used, total)
3421
}
3522
}

app/src/main/java/com/wstxda/toolkit/ui/label/TemperatureLabelProvider.kt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,11 @@ import java.util.Locale
66

77
class TemperatureLabelProvider(private val context: Context) {
88

9-
fun getLabel(isActive: Boolean, temp: Float): CharSequence {
10-
return if (isActive) {
11-
String.format(Locale.US, context.getString(R.string.temperature_tile_format), temp)
12-
} else {
13-
context.getString(R.string.temperature_tile)
14-
}
9+
fun getLabel(temp: Float): CharSequence {
10+
return String.format(Locale.US, context.getString(R.string.temperature_tile_format), temp)
1511
}
1612

17-
fun getSubtitle(isActive: Boolean): CharSequence? {
18-
return if (isActive) {
19-
context.getString(R.string.temperature_tile_source)
20-
} else {
21-
context.getString(R.string.tile_off)
22-
}
13+
fun getSubtitle(): CharSequence? {
14+
return context.getString(R.string.temperature_tile)
2315
}
2416
}

0 commit comments

Comments
 (0)