From f81446095daa16e7283075a8f65f48e0b1039611 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 28 Jun 2022 16:32:26 +0200 Subject: [PATCH] kill notifications on closure --- app/src/main/AndroidManifest.xml | 8 +++- .../java/com/github/libretube/MainActivity.kt | 4 ++ .../libretube/dialogs/DownloadDialog.kt | 2 +- .../libretube/fragments/PlayerFragment.kt | 24 ++-------- .../libretube/services/ClosingService.kt | 31 ++++++++++++ .../{ => services}/DownloadService.kt | 3 +- app/src/main/res/layout/fragment_player.xml | 48 ++++--------------- 7 files changed, 58 insertions(+), 62 deletions(-) create mode 100644 app/src/main/java/com/github/libretube/services/ClosingService.kt rename app/src/main/java/com/github/libretube/{ => services}/DownloadService.kt (99%) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8f5d7d3c2..e5baf8a8a 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -272,9 +272,15 @@ + + \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/MainActivity.kt b/app/src/main/java/com/github/libretube/MainActivity.kt index 357250e75..ee7126cae 100644 --- a/app/src/main/java/com/github/libretube/MainActivity.kt +++ b/app/src/main/java/com/github/libretube/MainActivity.kt @@ -34,6 +34,7 @@ import androidx.navigation.findNavController import androidx.navigation.ui.setupWithNavController import com.github.libretube.fragments.PlayerFragment import com.github.libretube.fragments.isFullScreen +import com.github.libretube.services.ClosingService import com.github.libretube.util.CronetHelper import com.github.libretube.util.LocaleHelper import com.github.libretube.util.PreferenceHelper @@ -52,6 +53,9 @@ class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { DynamicColors.applyToActivityIfAvailable(this) super.onCreate(savedInstanceState) + + startService(Intent(this, ClosingService::class.java)) + CronetHelper.initCronet(this.applicationContext) RetrofitInstance.url = diff --git a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt index 9fae8cd8b..cc467c313 100644 --- a/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/dialogs/DownloadDialog.kt @@ -17,10 +17,10 @@ import android.widget.TextView import androidx.core.app.ActivityCompat import androidx.core.text.HtmlCompat import androidx.fragment.app.DialogFragment -import com.github.libretube.DownloadService import com.github.libretube.MainActivity import com.github.libretube.R import com.github.libretube.obj.Streams +import com.github.libretube.services.DownloadService import com.google.android.material.dialog.MaterialAlertDialogBuilder class DownloadDialog : DialogFragment() { diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 54fe27fd7..d95443f9e 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -38,7 +38,7 @@ import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView -import com.github.libretube.IS_DOWNLOAD_RUNNING +import com.github.libretube.services.IS_DOWNLOAD_RUNNING import com.github.libretube.MainActivity import com.github.libretube.R import com.github.libretube.adapters.ChaptersAdapter @@ -447,11 +447,11 @@ class PlayerFragment : Fragment() { relatedStreams = response.relatedStreams runOnUiThread { createExoPlayer(view) - prepareExoPlayerView() if (response.chapters != null) initializeChapters(response.chapters) // set media sources for the player setResolutionAndSubtitles(view, response) exoPlayer.prepare() + prepareExoPlayerView() initializePlayerView(view, response) // support for time stamped links if (arguments?.getLong("timeStamp") != null) { @@ -717,25 +717,9 @@ class PlayerFragment : Fragment() { } private fun initializeChapters(chapters: List) { - val chaptersToggle = view?.findViewById(R.id.chapters_toggle) val chaptersRecView = view?.findViewById(R.id.chapters_recView) - val chaptersToggleText = view?.findViewById(R.id.chapters_toggle_text) - val chaptersToggleArrow = view?.findViewById(R.id.chapters_toggle_arrow) if (chapters.isNotEmpty()) { - chaptersToggle?.visibility = View.VISIBLE - - chaptersToggle?.setOnClickListener { - if (chaptersRecView?.isVisible!!) { - chaptersRecView?.visibility = View.GONE - chaptersToggleText?.text = getString(R.string.show_chapters) - } else { - chaptersRecView?.visibility = View.VISIBLE - chaptersToggleText?.text = getString(R.string.hide_chapters) - } - chaptersToggleArrow!!.animate().setDuration(100).rotationBy(180F).start() - } - chaptersRecView?.layoutManager = LinearLayoutManager(this.context, LinearLayoutManager.HORIZONTAL, false) chaptersRecView?.adapter = ChaptersAdapter(chapters, exoPlayer) @@ -1121,7 +1105,7 @@ class PlayerFragment : Fragment() { enableTransition(R.id.yt_transition, false) } view?.findViewById(R.id.main_container)?.isClickable = true - view?.findViewById(R.id.top_bar)?.visibility = View.GONE + view?.findViewById(R.id.top_bar)?.visibility = View.GONE val mainActivity = activity as MainActivity mainActivity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT isFullScreen = false @@ -1133,7 +1117,7 @@ class PlayerFragment : Fragment() { exoPlayerView.showController() exoPlayerView.useController = true view?.findViewById(R.id.main_container)?.isClickable = false - view?.findViewById(R.id.top_bar)?.visibility = View.VISIBLE + view?.findViewById(R.id.top_bar)?.visibility = View.VISIBLE } } diff --git a/app/src/main/java/com/github/libretube/services/ClosingService.kt b/app/src/main/java/com/github/libretube/services/ClosingService.kt new file mode 100644 index 000000000..716e6dff9 --- /dev/null +++ b/app/src/main/java/com/github/libretube/services/ClosingService.kt @@ -0,0 +1,31 @@ +package com.github.libretube.services + +import android.app.NotificationManager +import android.app.Service +import android.content.Context +import android.content.Intent +import android.os.IBinder +import android.util.Log +import androidx.annotation.Nullable + +class ClosingService : Service() { + private val TAG = "ClosingService" + + @Nullable + override fun onBind(intent: Intent?): IBinder? { + return null + } + + // Handle application closing + override fun onTaskRemoved(rootIntent: Intent?) { + super.onTaskRemoved(rootIntent) + + // destroy all notifications (especially the player notification) + val nManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + nManager.cancelAll() + Log.e(TAG, "closed") + + // Destroy the service + stopSelf() + } +} diff --git a/app/src/main/java/com/github/libretube/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt similarity index 99% rename from app/src/main/java/com/github/libretube/DownloadService.kt rename to app/src/main/java/com/github/libretube/services/DownloadService.kt index eb7b309aa..8f3c96e71 100644 --- a/app/src/main/java/com/github/libretube/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -1,4 +1,4 @@ -package com.github.libretube +package com.github.libretube.services import android.app.DownloadManager import android.app.PendingIntent @@ -18,6 +18,7 @@ import android.util.Log import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.arthenica.ffmpegkit.FFmpegKit +import com.github.libretube.R import com.github.libretube.util.PreferenceHelper import java.io.File diff --git a/app/src/main/res/layout/fragment_player.xml b/app/src/main/res/layout/fragment_player.xml index 930247359..05dda1320 100644 --- a/app/src/main/res/layout/fragment_player.xml +++ b/app/src/main/res/layout/fragment_player.xml @@ -55,21 +55,16 @@ - - + android:layout_marginHorizontal="10dp" + android:orientation="horizontal"> @@ -81,8 +76,8 @@ @@ -111,31 +106,6 @@ - - - - - - - - - -