new feature: create custom images
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package sh.sar.isodroid.notification
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import sh.sar.isodroid.isodrive.CreateImgEventBus
|
||||
|
||||
class CreateImgReceiver : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == NotificationHelper.ACTION_CANCEL_CREATE) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
CreateImgEventBus.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,11 @@ class NotificationHelper(private val context: Context) {
|
||||
|
||||
companion object {
|
||||
const val CHANNEL_ID = "iso_drive_mount_status"
|
||||
const val CHANNEL_ID_PROGRESS = "iso_drive_progress"
|
||||
const val NOTIFICATION_ID = 1001
|
||||
const val NOTIFICATION_ID_PROGRESS = 1002
|
||||
const val ACTION_UNMOUNT = "sh.sar.isodroid.ACTION_UNMOUNT"
|
||||
const val ACTION_CANCEL_CREATE = "sh.sar.isodroid.ACTION_CANCEL_CREATE"
|
||||
|
||||
@Volatile
|
||||
private var instance: NotificationHelper? = null
|
||||
@@ -32,11 +35,11 @@ class NotificationHelper(private val context: Context) {
|
||||
private val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
||||
|
||||
init {
|
||||
createNotificationChannel()
|
||||
createNotificationChannels()
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
val channel = NotificationChannel(
|
||||
private fun createNotificationChannels() {
|
||||
val mountChannel = NotificationChannel(
|
||||
CHANNEL_ID,
|
||||
"Mount Status",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
@@ -44,7 +47,18 @@ class NotificationHelper(private val context: Context) {
|
||||
description = "Shows when an ISO/IMG file is mounted"
|
||||
setShowBadge(false)
|
||||
}
|
||||
notificationManager.createNotificationChannel(channel)
|
||||
|
||||
val progressChannel = NotificationChannel(
|
||||
CHANNEL_ID_PROGRESS,
|
||||
"Progress",
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
).apply {
|
||||
description = "Shows progress for file operations"
|
||||
setShowBadge(false)
|
||||
}
|
||||
|
||||
notificationManager.createNotificationChannel(mountChannel)
|
||||
notificationManager.createNotificationChannel(progressChannel)
|
||||
}
|
||||
|
||||
fun showMountedNotification(mountStatus: MountStatus) {
|
||||
@@ -106,4 +120,68 @@ class NotificationHelper(private val context: Context) {
|
||||
fun hideNotification() {
|
||||
notificationManager.cancel(NOTIFICATION_ID)
|
||||
}
|
||||
|
||||
fun showCreateProgressNotification(fileName: String, progress: Int, bytesWritten: Long, totalBytes: Long) {
|
||||
val writtenMB = bytesWritten / (1024 * 1024)
|
||||
val totalMB = totalBytes / (1024 * 1024)
|
||||
|
||||
// Intent to open the app
|
||||
val openIntent = Intent(context, MainActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP
|
||||
}
|
||||
val openPendingIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
2,
|
||||
openIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
// Cancel action intent
|
||||
val cancelIntent = Intent(context, CreateImgReceiver::class.java).apply {
|
||||
action = ACTION_CANCEL_CREATE
|
||||
}
|
||||
val cancelPendingIntent = PendingIntent.getBroadcast(
|
||||
context,
|
||||
3,
|
||||
cancelIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
val notification = NotificationCompat.Builder(context, CHANNEL_ID_PROGRESS)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle("Creating $fileName")
|
||||
.setContentText("$writtenMB MB / $totalMB MB")
|
||||
.setProgress(100, progress, false)
|
||||
.setOngoing(true)
|
||||
.setShowWhen(false)
|
||||
.setContentIntent(openPendingIntent)
|
||||
.addAction(
|
||||
R.drawable.ic_eject,
|
||||
"Cancel",
|
||||
cancelPendingIntent
|
||||
)
|
||||
.setPriority(NotificationCompat.PRIORITY_LOW)
|
||||
.setCategory(NotificationCompat.CATEGORY_PROGRESS)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(NOTIFICATION_ID_PROGRESS, notification)
|
||||
}
|
||||
|
||||
fun showCreateCompleteNotification(fileName: String, success: Boolean) {
|
||||
hideProgressNotification()
|
||||
|
||||
val notification = NotificationCompat.Builder(context, CHANNEL_ID_PROGRESS)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setContentTitle(if (success) "Image Created" else "Creation Failed")
|
||||
.setContentText(fileName)
|
||||
.setAutoCancel(true)
|
||||
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
|
||||
.build()
|
||||
|
||||
notificationManager.notify(NOTIFICATION_ID_PROGRESS, notification)
|
||||
}
|
||||
|
||||
fun hideProgressNotification() {
|
||||
notificationManager.cancel(NOTIFICATION_ID_PROGRESS)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user