Skip to content

Commit 199eb7d

Browse files
committed
[SHARED] Fix #86
1 parent 7b3e848 commit 199eb7d

6 files changed

Lines changed: 173 additions & 11 deletions

File tree

shared/src/androidMain/kotlin/com/kgurgul/cpuinfo/data/provider/HardwareDataProvider.android.kt

Lines changed: 132 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,27 @@ import com.kgurgul.cpuinfo.shared.Res
3434
import com.kgurgul.cpuinfo.shared.amount
3535
import com.kgurgul.cpuinfo.shared.back
3636
import com.kgurgul.cpuinfo.shared.battery
37+
import com.kgurgul.cpuinfo.shared.battery_charge_counter
38+
import com.kgurgul.cpuinfo.shared.battery_charge_time_remaining
39+
import com.kgurgul.cpuinfo.shared.battery_charging
3740
import com.kgurgul.cpuinfo.shared.battery_cold
41+
import com.kgurgul.cpuinfo.shared.battery_current_avg
42+
import com.kgurgul.cpuinfo.shared.battery_current_now
43+
import com.kgurgul.cpuinfo.shared.battery_cycle_count
3844
import com.kgurgul.cpuinfo.shared.battery_dead
45+
import com.kgurgul.cpuinfo.shared.battery_dock
46+
import com.kgurgul.cpuinfo.shared.battery_full
3947
import com.kgurgul.cpuinfo.shared.battery_good
4048
import com.kgurgul.cpuinfo.shared.battery_health
49+
import com.kgurgul.cpuinfo.shared.battery_not_charging
4150
import com.kgurgul.cpuinfo.shared.battery_overheat
4251
import com.kgurgul.cpuinfo.shared.battery_overvoltage
52+
import com.kgurgul.cpuinfo.shared.battery_present
53+
import com.kgurgul.cpuinfo.shared.battery_state
4354
import com.kgurgul.cpuinfo.shared.battery_unknown
55+
import com.kgurgul.cpuinfo.shared.battery_unplugged
4456
import com.kgurgul.cpuinfo.shared.battery_unspecified_failure
57+
import com.kgurgul.cpuinfo.shared.battery_wireless
4558
import com.kgurgul.cpuinfo.shared.bluetooth
4659
import com.kgurgul.cpuinfo.shared.bluetooth_le
4760
import com.kgurgul.cpuinfo.shared.bluetooth_mac
@@ -191,30 +204,138 @@ actual class HardwareDataProvider actual constructor() : KoinComponent {
191204
functionsList.add(ItemValue.NameResource(Res.string.technology, technology))
192205
}
193206

194-
// Are we charging / is charged?
207+
// Battery present
208+
val isPresent = batteryStatus.getBooleanExtra(BatteryManager.EXTRA_PRESENT, true)
209+
functionsList.add(
210+
ItemValue.NameValueResource(
211+
Res.string.battery_present,
212+
getYesNoStringResource(isPresent),
213+
)
214+
)
215+
216+
// Battery status
195217
val status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1)
218+
if (status != -1) {
219+
val statusRes =
220+
when (status) {
221+
BatteryManager.BATTERY_STATUS_CHARGING -> Res.string.battery_charging
222+
BatteryManager.BATTERY_STATUS_DISCHARGING -> Res.string.battery_unplugged
223+
BatteryManager.BATTERY_STATUS_FULL -> Res.string.battery_full
224+
BatteryManager.BATTERY_STATUS_NOT_CHARGING ->
225+
Res.string.battery_not_charging
226+
else -> Res.string.battery_unknown
227+
}
228+
functionsList.add(ItemValue.NameValueResource(Res.string.battery_state, statusRes))
229+
}
230+
231+
// Are we charging / is charged?
196232
val isCharging =
197-
status == BatteryManager.BATTERY_STATUS_CHARGING ||
198-
status == BatteryManager.BATTERY_STATUS_FULL
233+
batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1).let {
234+
it == BatteryManager.BATTERY_STATUS_CHARGING ||
235+
it == BatteryManager.BATTERY_STATUS_FULL
236+
}
199237

200238
// How we charging?
201239
val chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
202-
val usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB
203-
val acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC
204240

205-
val charging = getYesNoStringResource(isCharging)
206-
functionsList.add(ItemValue.NameValueResource(Res.string.is_charging, charging))
241+
functionsList.add(
242+
ItemValue.NameValueResource(
243+
Res.string.is_charging,
244+
getYesNoStringResource(isCharging),
245+
)
246+
)
207247
if (isCharging) {
208248
val chargingType =
209-
when {
210-
usbCharge -> Res.string.hardware_usb
211-
acCharge -> Res.string.hardware_ac
212-
else -> Res.string.unknown
249+
when (chargePlug) {
250+
BatteryManager.BATTERY_PLUGGED_USB -> Res.string.hardware_usb
251+
BatteryManager.BATTERY_PLUGGED_AC -> Res.string.hardware_ac
252+
BatteryManager.BATTERY_PLUGGED_WIRELESS -> Res.string.battery_wireless
253+
else -> {
254+
if (
255+
Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE &&
256+
chargePlug == BatteryManager.BATTERY_PLUGGED_DOCK
257+
) {
258+
Res.string.battery_dock
259+
} else {
260+
Res.string.unknown
261+
}
262+
}
213263
}
214264
functionsList.add(
215265
ItemValue.NameValueResource(Res.string.charging_type, chargingType)
216266
)
217267
}
268+
269+
// Cycle count (API 34+)
270+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
271+
val cycleCount = batteryStatus.getIntExtra(BatteryManager.EXTRA_CYCLE_COUNT, -1)
272+
if (cycleCount >= 0) {
273+
functionsList.add(
274+
ItemValue.NameResource(
275+
Res.string.battery_cycle_count,
276+
cycleCount.toString(),
277+
)
278+
)
279+
}
280+
}
281+
}
282+
283+
val batteryManager = appContext.getSystemService(Context.BATTERY_SERVICE) as? BatteryManager
284+
if (batteryManager != null) {
285+
// Current now
286+
val currentNow =
287+
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_NOW)
288+
if (currentNow != Int.MIN_VALUE && currentNow != 0) {
289+
functionsList.add(
290+
ItemValue.NameResource(
291+
Res.string.battery_current_now,
292+
"${currentNow / 1000.0}mA",
293+
)
294+
)
295+
}
296+
297+
// Current average
298+
val currentAvg =
299+
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CURRENT_AVERAGE)
300+
if (currentAvg != Int.MIN_VALUE && currentAvg != 0) {
301+
functionsList.add(
302+
ItemValue.NameResource(
303+
Res.string.battery_current_avg,
304+
"${currentAvg / 1000.0}mA",
305+
)
306+
)
307+
}
308+
309+
// Charge counter (remaining charge)
310+
val chargeCounter =
311+
batteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CHARGE_COUNTER)
312+
if (chargeCounter != Int.MIN_VALUE && chargeCounter > 0) {
313+
functionsList.add(
314+
ItemValue.NameResource(
315+
Res.string.battery_charge_counter,
316+
"${chargeCounter / 1000.0}mAh",
317+
)
318+
)
319+
}
320+
321+
// Charge time remaining (API 28+)
322+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
323+
val chargeTimeRemaining = batteryManager.computeChargeTimeRemaining()
324+
if (chargeTimeRemaining > 0) {
325+
val minutes = chargeTimeRemaining / 1000 / 60
326+
val hours = minutes / 60
327+
val remainingMinutes = minutes % 60
328+
val timeString =
329+
if (hours > 0) {
330+
"${hours}h ${remainingMinutes}m"
331+
} else {
332+
"${remainingMinutes}m"
333+
}
334+
functionsList.add(
335+
ItemValue.NameResource(Res.string.battery_charge_time_remaining, timeString)
336+
)
337+
}
338+
}
218339
}
219340

220341
return functionsList

shared/src/commonMain/composeResources/values-cs/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@
159159
<string name="battery_unplugged">Odpojeno</string>
160160
<string name="battery_charging">Nabíjení</string>
161161
<string name="battery_full">Plně nabitá</string>
162+
<string name="battery_not_charging">Nenabíjí se</string>
163+
<string name="battery_present">Přítomna</string>
164+
<string name="battery_dock">Dok</string>
165+
<string name="battery_current_now">Aktuální proud</string>
166+
<string name="battery_current_avg">Průměrný proud</string>
167+
<string name="battery_charge_counter">Zbývající náboj</string>
168+
<string name="battery_charge_time_remaining">Čas do nabití</string>
169+
<string name="battery_cycle_count">Počet cyklů</string>
162170
<string name="ir_emitter">IR vysílač</string>
163171
<string name="hardware_microphones">Mikrofony</string>
164172
<string name="hardware_computer_system">Počítačový systém</string>

shared/src/commonMain/composeResources/values-de/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@
159159
<string name="battery_unplugged">Abgesteckt</string>
160160
<string name="battery_charging">Lädt</string>
161161
<string name="battery_full">Voll</string>
162+
<string name="battery_not_charging">Lädt nicht</string>
163+
<string name="battery_present">Vorhanden</string>
164+
<string name="battery_dock">Dock</string>
165+
<string name="battery_current_now">Aktueller Strom</string>
166+
<string name="battery_current_avg">Durchschnittlicher Strom</string>
167+
<string name="battery_charge_counter">Verbleibende Ladung</string>
168+
<string name="battery_charge_time_remaining">Zeit bis voll</string>
169+
<string name="battery_cycle_count">Ladezyklen</string>
162170
<string name="ir_emitter">IR-Sender</string>
163171
<string name="hardware_microphones">Mikrofone</string>
164172
<string name="hardware_computer_system">Computersystem</string>

shared/src/commonMain/composeResources/values-es/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@
165165
<string name="battery_unplugged">Desconectada</string>
166166
<string name="battery_charging">Cargando</string>
167167
<string name="battery_full">Completa</string>
168+
<string name="battery_not_charging">Sin carga</string>
169+
<string name="battery_present">Presente</string>
170+
<string name="battery_dock">Base</string>
171+
<string name="battery_current_now">Corriente actual</string>
172+
<string name="battery_current_avg">Corriente promedio</string>
173+
<string name="battery_charge_counter">Carga restante</string>
174+
<string name="battery_charge_time_remaining">Tiempo hasta carga completa</string>
175+
<string name="battery_cycle_count">Ciclos de carga</string>
168176
<string name="ir_emitter">Emisor IR</string>
169177
<string name="hardware_microphones">Micrófonos</string>
170178
<string name="hardware_computer_system">Sistema informático</string>

shared/src/commonMain/composeResources/values-pl/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@
165165
<string name="battery_unplugged">Odłączona</string>
166166
<string name="battery_charging">Ładowanie</string>
167167
<string name="battery_full">Pełna</string>
168+
<string name="battery_not_charging">Nie ładuje</string>
169+
<string name="battery_present">Obecna</string>
170+
<string name="battery_dock">Stacja dokująca</string>
171+
<string name="battery_current_now">Prąd chwilowy</string>
172+
<string name="battery_current_avg">Prąd średni</string>
173+
<string name="battery_charge_counter">Pozostały ładunek</string>
174+
<string name="battery_charge_time_remaining">Czas do naładowania</string>
175+
<string name="battery_cycle_count">Liczba cykli</string>
168176
<string name="ir_emitter">Nadajnik IR</string>
169177
<string name="camera_lens_number">Liczba soczewek</string>
170178
<string name="hardware_microphones">Mikrofony</string>

shared/src/commonMain/composeResources/values/strings.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@
197197
<string name="battery_unplugged">Unplugged</string>
198198
<string name="battery_charging">Charging</string>
199199
<string name="battery_full">Full</string>
200+
<string name="battery_not_charging">Not charging</string>
201+
<string name="battery_present">Present</string>
202+
<string name="battery_wireless" translatable="false">Wireless</string>
203+
<string name="battery_dock">Dock</string>
204+
<string name="battery_current_now">Current now</string>
205+
<string name="battery_current_avg">Current average</string>
206+
<string name="battery_charge_counter">Remaining charge</string>
207+
<string name="battery_charge_time_remaining">Time until full</string>
208+
<string name="battery_cycle_count">Cycle count</string>
200209
<string name="ir_emitter">IR Emitter</string>
201210
<string name="hardware_microphones">Microphones</string>
202211
<string name="hardware_computer_system">Computer system</string>

0 commit comments

Comments
 (0)