refactor notification
This commit is contained in:
1 parent
4a787eb780
commit
28d313ce10
1 file changed
+132
-81
@@ -6,6 +6,7 @@ import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.os.PowerManager
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import eu.depau.etchdroid.R
|
||||
import eu.depau.etchdroid.ui.activities.ErrorActivity
|
||||
import eu.depau.etchdroid.utils.ktexts.toHRSize
|
||||
@@ -15,8 +16,10 @@ import kotlin.math.max
|
||||
|
||||
|
||||
abstract class UsbWriteService(val usbWriteName: String) : IntentService(usbWriteName) {
|
||||
private var prevTime = System.currentTimeMillis()
|
||||
private var prevBytes = 0L
|
||||
private var time: Long = 0
|
||||
private var speed: Double = 0.0
|
||||
private var previousTime = System.currentTimeMillis()
|
||||
private var previousBytes = 0L
|
||||
private var notifyChanRegistered = false
|
||||
private var mWakeLock: PowerManager.WakeLock? = null
|
||||
private var wlAcquireTime = -1L
|
||||
@@ -42,120 +45,168 @@ abstract class UsbWriteService(val usbWriteName: String) : IntentService(usbWrit
|
||||
|
||||
abstract fun writeImage(intent: Intent): Long
|
||||
|
||||
private fun getNotificationBuilder(channel: String = WRITE_PROGRESS_CHANNEL_ID): NotificationCompat.Builder {
|
||||
if (!notifyChanRegistered) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val statusChannel = NotificationChannel(
|
||||
WRITE_PROGRESS_CHANNEL_ID,
|
||||
getString(R.string.notchan_writestatus_title),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
statusChannel.description = getString(R.string.notchan_writestatus_desc)
|
||||
private fun calculateWriteSpeed(bytes: Long): Double =
|
||||
max((bytes - previousBytes).toDouble() / (time - previousTime) * 1000, 0.0)
|
||||
|
||||
val resultChannel = NotificationChannel(
|
||||
WRITE_RESULT_CHANNEL_ID,
|
||||
getString(R.string.result_channel_name),
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
)
|
||||
resultChannel.description = getString(R.string.result_channel_desc)
|
||||
private fun calculateLastWriteSpeed(bytes: Long, deltaTime: Long): Double =
|
||||
max(bytes.toDouble() / deltaTime * 1000, 0.0)
|
||||
|
||||
// Register the channel with the system; you can't change the importance
|
||||
// or other notification behaviors after this
|
||||
val notificationManager = getSystemService(NotificationManager::class.java)
|
||||
notificationManager!!.createNotificationChannel(statusChannel)
|
||||
notificationManager.createNotificationChannel(resultChannel)
|
||||
// Notification rate limiting
|
||||
private fun shouldUpdateNotification(): Boolean {
|
||||
time = System.currentTimeMillis()
|
||||
return time <= previousTime + 1000
|
||||
}
|
||||
|
||||
private fun createNotificationChannels() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
val statusChannel = NotificationChannel(
|
||||
WRITE_PROGRESS_CHANNEL_ID,
|
||||
getString(R.string.notchan_writestatus_title),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
).apply {
|
||||
description = getString(R.string.notchan_writestatus_desc)
|
||||
}
|
||||
|
||||
val resultChannel = NotificationChannel(
|
||||
WRITE_RESULT_CHANNEL_ID,
|
||||
getString(R.string.result_channel_name),
|
||||
NotificationManager.IMPORTANCE_DEFAULT
|
||||
).apply {
|
||||
description = getString(R.string.result_channel_desc)
|
||||
}
|
||||
|
||||
// Register the channel with the system; you can't change the importance
|
||||
// or other notification behaviors after this
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.createNotificationChannel(statusChannel)
|
||||
notificationManager.createNotificationChannel(resultChannel)
|
||||
}
|
||||
}
|
||||
|
||||
private fun getNotificationBuilder(channel: String = WRITE_PROGRESS_CHANNEL_ID):
|
||||
NotificationCompat.Builder {
|
||||
if (!notifyChanRegistered) {
|
||||
createNotificationChannels()
|
||||
notifyChanRegistered = true
|
||||
}
|
||||
|
||||
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
|
||||
NotificationCompat.Builder(this, channel)
|
||||
else
|
||||
NotificationCompat.Builder(this)
|
||||
return NotificationCompat.Builder(this, channel)
|
||||
}
|
||||
|
||||
fun updateNotification(usbDevice: String, filename: String?, bytes: Long, progr: Int) {
|
||||
// Notification rate limiting
|
||||
val time = System.currentTimeMillis()
|
||||
if (time <= prevTime + 1000)
|
||||
if (shouldUpdateNotification())
|
||||
return
|
||||
|
||||
val speed = max((bytes - prevBytes).toDouble() / (time - prevTime).toDouble() * 1000, 0.0).toHRSize()
|
||||
prevTime = time
|
||||
prevBytes = bytes
|
||||
val speed = calculateWriteSpeed(bytes).toHRSize()
|
||||
previousTime = time
|
||||
previousBytes = bytes
|
||||
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
notificationManager.notify(FOREGROUND_ID, buildForegroundNotification(usbDevice, filename, progr, "$progr% • $speed/s"))
|
||||
with(NotificationManagerCompat.from(this)) {
|
||||
notify(FOREGROUND_ID, buildForegroundNotification(
|
||||
usbDevice,
|
||||
filename,
|
||||
progr,
|
||||
"$progr% • $speed/s"
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
fun resultNotification(usbDevice: String, filename: String, exception: Throwable?, bytes: Long = 0, startTime: Long = 0) {
|
||||
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
val b = getNotificationBuilder(WRITE_RESULT_CHANNEL_ID)
|
||||
fun resultNotification(
|
||||
usbDevice: String,
|
||||
filename: String,
|
||||
exception: Throwable?,
|
||||
bytes: Long = 0,
|
||||
startTime: Long = 0
|
||||
) {
|
||||
val notificationCompatBuilder = getNotificationBuilder(WRITE_RESULT_CHANNEL_ID)
|
||||
.setOngoing(false)
|
||||
|
||||
val dt = System.currentTimeMillis() - startTime
|
||||
val deltaTime = System.currentTimeMillis() - startTime
|
||||
|
||||
if (exception != null) {
|
||||
val intent = Intent(this, ErrorActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
|
||||
intent.putExtra("error", exception.message)
|
||||
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
|
||||
val pendingIntent = PendingIntent.getActivity(
|
||||
this,
|
||||
0,
|
||||
intent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
b.setContentTitle(getString(R.string.failed_tap_for_info))
|
||||
notificationCompatBuilder.setContentTitle(getString(R.string.failed_tap_for_info))
|
||||
.setContentText(getString(R.string.error_notif_content_text, usbDevice))
|
||||
.setStyle(NotificationCompat.BigTextStyle()
|
||||
.bigText(getString(R.string.error_notif_content_text, usbDevice)))
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
.setSubText(dt.toHRTime())
|
||||
.setSubText(deltaTime.toHRTime())
|
||||
} else {
|
||||
val speed = max(bytes.toDouble() / dt.toDouble() * 1000, 0.0).toHRSize() + "/s"
|
||||
b.setContentTitle(getString(R.string.write_finished))
|
||||
.setContentText(getString(R.string.success_notif_content_text, filename, usbDevice))
|
||||
.setSubText("${dt.toHRTime()} • ${bytes.toHRSize()} • $speed")
|
||||
val formattedLastWriteSpeed =
|
||||
calculateLastWriteSpeed(bytes, deltaTime).toHRSize() + "/s"
|
||||
|
||||
notificationCompatBuilder.setContentTitle(getString(R.string.write_finished))
|
||||
.setContentText(getString(
|
||||
R.string.success_notif_content_text,
|
||||
filename,
|
||||
usbDevice
|
||||
))
|
||||
.setSubText("%1s • %2s • %3s".format(
|
||||
deltaTime.toHRTime(),
|
||||
bytes.toHRSize(),
|
||||
formattedLastWriteSpeed
|
||||
))
|
||||
}
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
|
||||
b.setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
notificationCompatBuilder.setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
|
||||
notificationManager.notify(RESULT_NOTIFICATION_ID, b.build())
|
||||
with(NotificationManagerCompat.from(this)) {
|
||||
notify(RESULT_NOTIFICATION_ID, notificationCompatBuilder.build())
|
||||
}
|
||||
}
|
||||
|
||||
private fun buildForegroundNotification(usbDevice: String?, filename: String?, progr: Int, subText: String? = null, title: String = getString(R.string.notif_writing_img)): Notification {
|
||||
val indet: Boolean
|
||||
val prog: Int
|
||||
private fun buildForegroundNotification(
|
||||
usbDevice: String?,
|
||||
filename: String?,
|
||||
progress: Int,
|
||||
subText: String? = null,
|
||||
title: String = getString(R.string.notif_writing_img)
|
||||
): Notification {
|
||||
|
||||
if (progr < 0) {
|
||||
prog = 0
|
||||
indet = true
|
||||
} else {
|
||||
prog = progr
|
||||
indet = false
|
||||
val notificationBuilder = getNotificationBuilder()
|
||||
|
||||
with(notificationBuilder) {
|
||||
setContentTitle(title)
|
||||
setOngoing(true)
|
||||
|
||||
if (progress < 0) {
|
||||
setProgress(100, 0, true)
|
||||
} else {
|
||||
setProgress(100, progress, false)
|
||||
}
|
||||
|
||||
if (usbDevice != null && filename != null) {
|
||||
setContentText("$filename to $usbDevice")
|
||||
setStyle(NotificationCompat.BigTextStyle().bigText("$filename to $usbDevice"))
|
||||
} else {
|
||||
setContentText(getString(R.string.notif_initializing))
|
||||
}
|
||||
|
||||
setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
|
||||
if (subText != null)
|
||||
setSubText(subText)
|
||||
}
|
||||
|
||||
val b = getNotificationBuilder()
|
||||
|
||||
b.setContentTitle(title)
|
||||
.setOngoing(true)
|
||||
.setProgress(100, prog, indet)
|
||||
|
||||
if (usbDevice != null && filename != null)
|
||||
b.setContentText("$filename to $usbDevice")
|
||||
else
|
||||
b.setContentText(getString(R.string.notif_initializing))
|
||||
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
|
||||
b.setSmallIcon(R.drawable.ic_usb_white_24dp)
|
||||
|
||||
if (subText != null)
|
||||
b.setSubText(subText)
|
||||
|
||||
return b.build()
|
||||
return notificationBuilder.build()
|
||||
}
|
||||
|
||||
private fun istTimeoutExpired(acquire: Boolean): Boolean =
|
||||
acquire && mWakeLock != null && wlAcquireTime > 0 &&
|
||||
System.currentTimeMillis() < wlAcquireTime + WL_TIMEOUT - 5000
|
||||
|
||||
fun wakeLock(acquire: Boolean) {
|
||||
// Do not reacquire wakelock if timeout not expired
|
||||
if (acquire && mWakeLock != null && wlAcquireTime > 0 && System.currentTimeMillis() < wlAcquireTime + WL_TIMEOUT - 5000)
|
||||
if (istTimeoutExpired(acquire))
|
||||
return
|
||||
|
||||
wlAcquireTime = if (acquire)
|
||||
@@ -169,11 +220,11 @@ abstract class UsbWriteService(val usbWriteName: String) : IntentService(usbWrit
|
||||
if (mWakeLock == null)
|
||||
mWakeLock = newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG)
|
||||
|
||||
mWakeLock.apply {
|
||||
mWakeLock?.run {
|
||||
if (acquire)
|
||||
this!!.acquire(WL_TIMEOUT /*10 minutes*/)
|
||||
this.acquire(WL_TIMEOUT /*10 minutes*/)
|
||||
else
|
||||
this!!.release()
|
||||
this.release()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user