EtchDroid/app/src/main/java/eu/depau/etchdroid/abc/UsbWriteService.kt

176 lines
6.4 KiB
Kotlin
Raw Normal View History

2018-08-15 17:04:45 +00:00
package eu.depau.etchdroid.abc
2018-08-13 23:32:02 +00:00
import android.app.IntentService
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
2018-08-14 16:49:22 +00:00
import android.content.Context
import android.content.Intent
2018-08-13 23:32:02 +00:00
import android.os.Build
2018-08-14 16:49:22 +00:00
import android.os.PowerManager
import android.support.v4.app.NotificationCompat
2018-08-15 17:04:45 +00:00
import eu.depau.etchdroid.R
import eu.depau.etchdroid.utils.toHRSize
import eu.depau.etchdroid.utils.toHRTime
2018-08-13 23:32:02 +00:00
abstract class UsbWriteService(name: String) : IntentService(name) {
val TAG = name
val FOREGROUND_ID = 1931
2018-08-14 16:49:22 +00:00
val RESULT_NOTIFICATION_ID = 3829
2018-08-15 17:04:45 +00:00
val WRITE_PROGRESS_CHANNEL_ID = "eu.depau.etchdroid.notifications.USB_WRITE_PROGRESS"
val WRITE_RESULT_CHANNEL_ID = "eu.depau.etchdroid.notifications.USB_WRITE_RESULT"
val WAKELOCK_TAG = "eu.depau.etchdroid.wakelocks.USB_WRITING"
2018-08-14 16:49:22 +00:00
2018-08-13 23:32:02 +00:00
private var prevTime = System.currentTimeMillis()
private var prevBytes = 0L
private var notifyChanRegistered = false
2018-08-14 16:49:22 +00:00
private var mWakeLock: PowerManager.WakeLock? = null
private var wlAcquireTime = -1L
private val WL_TIMEOUT = 10 * 60 * 1000L
override fun onHandleIntent(intent: Intent?) {
startForeground(FOREGROUND_ID, buildForegroundNotification(null, null, -1, -1))
try {
writeImage(intent!!)
} finally {
stopForeground(true)
}
}
abstract fun writeImage(intent: Intent): Long
2018-08-13 23:32:02 +00:00
2018-08-14 16:49:22 +00:00
fun getNotificationBuilder(channel: String = WRITE_PROGRESS_CHANNEL_ID): NotificationCompat.Builder {
2018-08-13 23:32:02 +00:00
if (!notifyChanRegistered) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
2018-08-14 16:49:22 +00:00
val statusChannel = NotificationChannel(
WRITE_PROGRESS_CHANNEL_ID,
getString(R.string.notchan_writestatus_title),
NotificationManager.IMPORTANCE_LOW
)
statusChannel.description = getString(R.string.notchan_writestatus_desc)
val resultChannel = NotificationChannel(
WRITE_RESULT_CHANNEL_ID,
"USB write result notifications",
NotificationManager.IMPORTANCE_DEFAULT
)
resultChannel.description = "Used to display the result of a finished write operation"
2018-08-13 23:32:02 +00:00
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
val notificationManager = getSystemService(NotificationManager::class.java)
2018-08-14 16:49:22 +00:00
notificationManager!!.createNotificationChannel(statusChannel)
notificationManager!!.createNotificationChannel(resultChannel)
2018-08-13 23:32:02 +00:00
}
notifyChanRegistered = true
}
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
2018-08-14 16:49:22 +00:00
NotificationCompat.Builder(this, channel)
2018-08-13 23:32:02 +00:00
else
NotificationCompat.Builder(this)
2018-08-13 23:32:02 +00:00
}
2018-08-14 16:49:22 +00:00
fun updateNotification(usbDevice: String, filename: String?, bytes: Long, total: Long) {
2018-08-13 23:32:02 +00:00
// Notification rate limiting
val time = System.currentTimeMillis()
if (time <= prevTime + 1000)
return
2018-08-15 16:10:39 +00:00
val speed = ((bytes - prevBytes).toDouble() / (time - prevTime).toDouble() * 1000).toHRSize()
2018-08-13 23:32:02 +00:00
prevTime = time
prevBytes = bytes
val perc: Int = (bytes.toDouble() / total * 100.0).toInt()
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
2018-08-14 16:49:22 +00:00
notificationManager.notify(FOREGROUND_ID, buildForegroundNotification(usbDevice, filename, bytes, total, "$perc% • $speed/s"))
2018-08-13 23:32:02 +00:00
}
2018-08-14 16:49:22 +00:00
fun resultNotification(usbDevice: String, filename: String, success: Boolean, bytes: Long = 0, startTime: Long = 0) {
2018-08-13 23:32:02 +00:00
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
2018-08-14 16:49:22 +00:00
val b = getNotificationBuilder(WRITE_RESULT_CHANNEL_ID)
2018-08-13 23:32:02 +00:00
.setOngoing(false)
2018-08-14 16:49:22 +00:00
val dt = System.currentTimeMillis() - startTime
if (!success)
b.setContentTitle("Write failed")
.setContentText("$usbDevice may have been unplugged while writing.")
.setSubText(dt.toHRTime())
else {
2018-08-15 16:10:39 +00:00
val speed = (bytes.toDouble() / dt.toDouble() * 1000).toHRSize() + "/s"
2018-08-14 16:49:22 +00:00
b.setContentTitle("Write finished")
.setContentText("$filename successfully written to $usbDevice")
.setSubText("${dt.toHRTime()}${bytes.toHRSize()}$speed")
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
b.setSmallIcon(R.drawable.ic_usb_white_24dp)
2018-08-13 23:32:02 +00:00
2018-08-14 16:49:22 +00:00
notificationManager.notify(RESULT_NOTIFICATION_ID, b.build())
2018-08-13 23:32:02 +00:00
}
2018-08-14 16:49:22 +00:00
fun buildForegroundNotification(usbDevice: String?, filename: String?, bytes: Long, total: Long, subText: String? = null): Notification {
2018-08-13 23:32:02 +00:00
val progr: Int
val indet: Boolean
if (total < 0) {
progr = 0
indet = true
} else {
2018-08-14 16:49:22 +00:00
progr = (bytes.toFloat() / total * 100).toInt()
2018-08-13 23:32:02 +00:00
indet = false
}
val b = getNotificationBuilder()
2018-08-14 16:49:22 +00:00
b.setContentTitle(getString(R.string.notif_writing_img))
2018-08-13 23:32:02 +00:00
.setOngoing(true)
.setProgress(100, progr, indet)
2018-08-14 16:49:22 +00:00
if (usbDevice != null && filename != null)
2018-08-15 17:06:50 +00:00
b.setContentText("${filename} to $usbDevice")
2018-08-14 16:49:22 +00:00
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)
2018-08-13 23:32:02 +00:00
if (subText != null)
b.setSubText(subText)
return b.build()
}
2018-08-14 16:49:22 +00:00
fun wakeLock(acquire: Boolean) {
// Do not reacquire wakelock if timeout not expired
if (acquire && mWakeLock != null && wlAcquireTime > 0 && System.currentTimeMillis() < wlAcquireTime + WL_TIMEOUT - 5000)
return
wlAcquireTime = if (acquire)
System.currentTimeMillis()
else
-1
val powerMgr = getSystemService(Context.POWER_SERVICE) as PowerManager
powerMgr.run {
if (mWakeLock == null)
mWakeLock = newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK_TAG)
mWakeLock.apply {
if (acquire)
this!!.acquire(WL_TIMEOUT /*10 minutes*/)
else
this!!.release()
}
}
}
2018-08-13 23:32:02 +00:00
}