Files
ISODroid/app/src/main/java/sh/sar/isodroid/notification/UnmountReceiver.kt

49 lines
1.8 KiB
Kotlin

/*
* SPDX-FileCopyrightText: 2026 Shiham Abdul Rahman <shihaam@shihaam.dev>
* SPDX-License-Identifier: GPL-3.0-or-later
*/
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()
}
}
}
}
}
}