add notifcaiton support with unmount button
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
package sh.sar.isodroid.notification
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.widget.Toast
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.launch
|
||||
import sh.sar.isodroid.isodrive.IsoDriveManager
|
||||
import sh.sar.isodroid.isodrive.MountEventBus
|
||||
|
||||
class UnmountReceiver : BroadcastReceiver() {
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action == NotificationHelper.ACTION_UNMOUNT) {
|
||||
val pendingResult = goAsync()
|
||||
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
try {
|
||||
val isoDriveManager = IsoDriveManager.getInstance(context)
|
||||
val result = isoDriveManager.unmount()
|
||||
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
if (result.success) {
|
||||
Toast.makeText(context, "Unmounted successfully", Toast.LENGTH_SHORT).show()
|
||||
NotificationHelper.getInstance(context).hideNotification()
|
||||
MountEventBus.emitUnmounted()
|
||||
} else {
|
||||
Toast.makeText(context, "Unmount failed: ${result.message}", Toast.LENGTH_LONG).show()
|
||||
}
|
||||
pendingResult.finish()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
CoroutineScope(Dispatchers.Main).launch {
|
||||
Toast.makeText(context, "Error: ${e.message}", Toast.LENGTH_LONG).show()
|
||||
pendingResult.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user