Skip to content

Commit

Permalink
Add cancelMediaNotification function which is called on app close o…
Browse files Browse the repository at this point in the history
…r on unmount of `Watch`
  • Loading branch information
MarmadileManteater committed Feb 4, 2024
1 parent edd3d3f commit 3669a05
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import androidx.core.app.NotificationManagerCompat
import java.io.File
import java.io.FileInputStream
import java.net.URL
import java.util.UUID.*

class FreeTubeJavaScriptInterface {
private var context: MainActivity
private var mediaSession: MediaSession?
Expand All @@ -27,7 +29,8 @@ class FreeTubeJavaScriptInterface {
companion object {
private const val DATA_DIRECTORY = "data://"
private const val CHANNEL_ID = "media_controls"
private const val NOTIFICATION_ID = 1
private val NOTIFICATION_ID = (2..1000).random()
private val NOTIFICATION_TAG = String.format("%s", randomUUID())
}

constructor(main: MainActivity) {
Expand Down Expand Up @@ -135,7 +138,10 @@ class FreeTubeJavaScriptInterface {
*/
@SuppressLint("MissingPermission")
private fun pushNotification(notification: Notification) {
NotificationManagerCompat.from(context).notify(NOTIFICATION_ID, notification)
val manager = NotificationManagerCompat.from(context)
// cancel any existing notifications
manager.cancel(NOTIFICATION_ID)
manager.notify(NOTIFICATION_TAG, NOTIFICATION_ID, notification)
lastNotification = notification
}

Expand Down Expand Up @@ -322,6 +328,12 @@ class FreeTubeJavaScriptInterface {
setMetadata(mediaSession!!, trackName, artist, duration, art)
}

@JavascriptInterface
fun cancelMediaNotification() {
val manager = NotificationManagerCompat.from(context)
manager.cancel(NOTIFICATION_TAG, NOTIFICATION_ID)
}

@JavascriptInterface
fun readFile(basedir: String, filename: String): String {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,7 @@ class MainActivity : AppCompatActivity(), OnRequestPermissionsResultCallback {
override fun onDestroy() {
super.onDestroy()
stopService(keepAliveIntent)
jsInterface.cancelMediaNotification()
webView.destroy()
}
}
2 changes: 2 additions & 0 deletions src/renderer/views/Watch/Watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
import {
createMediaSession
} from '../../helpers/android'
import android from 'android'

/**
* @typedef {object} AudioSource
Expand Down Expand Up @@ -340,6 +341,7 @@ export default defineComponent({
},
beforeDestroy() {
window.clearAllMediaSessionEventListeners()
android.cancelMediaNotification()
},
methods: {
onMountedDependOnLocalStateLoading() {
Expand Down

0 comments on commit 3669a05

Please sign in to comment.