Skip to content

Commit 0f37c9c

Browse files
authored
Merge pull request #227 from sameerasw/develop
Develop - Critical bug fix with batteries notification
2 parents 34025a1 + 344562b commit 0f37c9c

3 files changed

Lines changed: 35 additions & 15 deletions

File tree

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ android {
2121
applicationId = "com.sameerasw.essentials"
2222
minSdk = 26
2323
targetSdk = 36
24-
versionCode = 27
25-
versionName = "11.3"
24+
versionCode = 28
25+
versionName = "11.4"
2626

2727
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2828
}

app/src/main/java/com/sameerasw/essentials/services/BatteryNotificationService.kt

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ class BatteryNotificationService : Service() {
4343
settingsRepository = SettingsRepository(this)
4444
createNotificationChannel()
4545
settingsRepository.registerOnSharedPreferenceChangeListener(preferenceChangeListener)
46+
47+
startForeground(NOTIF_ID, buildBaseNotification(getString(R.string.feat_batteries_title), ""))
48+
4649
updateNotification()
4750
}
4851

4952
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
53+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
54+
startForeground(NOTIF_ID, buildBaseNotification(getString(R.string.feat_batteries_title), ""))
55+
}
5056
updateNotification()
5157
return START_STICKY
5258
}
@@ -77,26 +83,39 @@ class BatteryNotificationService : Service() {
7783

7884
private fun updateNotification() {
7985
val batteryItems = fetchBatteryData()
80-
if (batteryItems.isEmpty()) {
81-
stopForeground(true)
82-
stopSelf()
83-
return
86+
87+
val notification = if (batteryItems.isEmpty()) {
88+
buildBaseNotification(
89+
getString(R.string.feat_batteries_title),
90+
getString(R.string.battery_notification_no_devices)
91+
)
92+
} else {
93+
val bitmap = createCompositeBitmap(batteryItems)
94+
NotificationCompat.Builder(this, CHANNEL_ID)
95+
.setSmallIcon(R.drawable.rounded_battery_charging_60_24)
96+
.setLargeIcon(bitmap)
97+
.setStyle(NotificationCompat.BigPictureStyle()
98+
.bigPicture(bitmap)
99+
.bigLargeIcon(null as Bitmap?))
100+
.setContentTitle(getString(R.string.feat_batteries_title))
101+
.setPriority(NotificationCompat.PRIORITY_LOW)
102+
.setOngoing(true)
103+
.setSilent(true)
104+
.build()
84105
}
85106

86-
val bitmap = createCompositeBitmap(batteryItems)
87-
val notification = NotificationCompat.Builder(this, CHANNEL_ID)
107+
startForeground(NOTIF_ID, notification)
108+
}
109+
110+
private fun buildBaseNotification(title: String, content: String): Notification {
111+
return NotificationCompat.Builder(this, CHANNEL_ID)
88112
.setSmallIcon(R.drawable.rounded_battery_charging_60_24)
89-
.setLargeIcon(bitmap)
90-
.setStyle(NotificationCompat.BigPictureStyle()
91-
.bigPicture(bitmap)
92-
.bigLargeIcon(null as Bitmap?))
93-
.setContentTitle(getString(R.string.feat_batteries_title))
113+
.setContentTitle(title)
114+
.setContentText(content)
94115
.setPriority(NotificationCompat.PRIORITY_LOW)
95116
.setOngoing(true)
96117
.setSilent(true)
97118
.build()
98-
99-
startForeground(NOTIF_ID, notification)
100119
}
101120

102121
private fun fetchBatteryData(): List<BatteryItemData> {

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@
690690
<string name="update_channel_name">App Updates</string>
691691
<string name="update_channel_desc">Notifications for new app updates</string>
692692
<string name="notification_update_available">Update available</string>
693+
<string name="battery_notification_no_devices">No devices connected</string>
693694

694695
<string name="label_unknown">Unknown</string>
695696
<string name="network_type_5g">5G</string>

0 commit comments

Comments
 (0)