From 40fd1862c4fee5b1261fddf7005b01a1ea1a0b73 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 20 Aug 2022 09:35:55 +0200 Subject: [PATCH 1/2] kill app when closing pip --- .../java/com/github/libretube/fragments/PlayerFragment.kt | 7 +++++++ 1 file changed, 7 insertions(+) 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 c727af0ae..aea79de24 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -29,6 +29,7 @@ import androidx.core.net.toUri import androidx.core.os.bundleOf import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels +import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.LinearLayoutManager @@ -1638,6 +1639,12 @@ class PlayerFragment : BaseFragment() { binding.linLayout.visibility = View.GONE viewModel.isFullscreen.value = false + } else if (lifecycle.currentState == Lifecycle.State.CREATED) { + // close button got clicked in PiP mode + // destroying the fragment, player and notification + onDestroy() + // finish the activity + activity?.finishAndRemoveTask() } else { // enable exoPlayer controls again exoPlayerView.useController = true From f5e83d7dd657f382f4158dcf67b4cc867cc03e34 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 20 Aug 2022 09:42:24 +0200 Subject: [PATCH 2/2] add documentation --- .../java/com/github/libretube/Constants.kt | 5 ++++ .../libretube/util/NotificationHelper.kt | 10 ++++--- .../github/libretube/util/PermissionHelper.kt | 4 ++- .../com/github/libretube/util/ThemeHelper.kt | 28 ++++++++++++++++--- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/github/libretube/Constants.kt b/app/src/main/java/com/github/libretube/Constants.kt index 06a6e3d3a..cc6ffe739 100644 --- a/app/src/main/java/com/github/libretube/Constants.kt +++ b/app/src/main/java/com/github/libretube/Constants.kt @@ -55,3 +55,8 @@ const val PUSH_CHANNEL_ID = "notification_worker" * Database */ const val DATABASE_NAME = "LibreTubeDatabase" + +/** + * New Streams notifications + */ +const val NOTIFICATION_WORK_NAME = "NotificationService" \ No newline at end of file diff --git a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt index b61087d07..890e4f8b8 100644 --- a/app/src/main/java/com/github/libretube/util/NotificationHelper.kt +++ b/app/src/main/java/com/github/libretube/util/NotificationHelper.kt @@ -10,6 +10,7 @@ import androidx.work.ExistingPeriodicWorkPolicy import androidx.work.NetworkType import androidx.work.PeriodicWorkRequest import androidx.work.WorkManager +import com.github.libretube.NOTIFICATION_WORK_NAME import com.github.libretube.PUSH_CHANNEL_ID import com.github.libretube.PUSH_NOTIFICATION_ID import com.github.libretube.R @@ -23,6 +24,9 @@ import kotlinx.coroutines.runBlocking import java.util.concurrent.TimeUnit object NotificationHelper { + /** + * Enqueue the work manager task + */ fun enqueueWork( context: Context, existingPeriodicWorkPolicy: ExistingPeriodicWorkPolicy @@ -39,8 +43,6 @@ object NotificationHelper { "60" ).toLong() - val uniqueWorkName = "NotificationService" - // schedule the work manager request if logged in and notifications enabled if (notificationsEnabled && PreferenceHelper.getToken() != "") { // required network type for the work @@ -71,14 +73,14 @@ object NotificationHelper { // enqueue the task WorkManager.getInstance(context) .enqueueUniquePeriodicWork( - uniqueWorkName, + NOTIFICATION_WORK_NAME, existingPeriodicWorkPolicy, notificationWorker ) } else { // cancel the work if notifications are disabled or the user is not logged in WorkManager.getInstance(context) - .cancelUniqueWork(uniqueWorkName) + .cancelUniqueWork(NOTIFICATION_WORK_NAME) } } diff --git a/app/src/main/java/com/github/libretube/util/PermissionHelper.kt b/app/src/main/java/com/github/libretube/util/PermissionHelper.kt index 3670a2d81..1df60a152 100644 --- a/app/src/main/java/com/github/libretube/util/PermissionHelper.kt +++ b/app/src/main/java/com/github/libretube/util/PermissionHelper.kt @@ -8,8 +8,10 @@ import android.os.Environment import androidx.core.app.ActivityCompat object PermissionHelper { + /** + * request storage permissions if not granted yet + */ fun requestReadWrite(activity: Activity): Boolean { - // request storage permissions if not granted yet if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (!Environment.isExternalStorageManager()) { ActivityCompat.requestPermissions( diff --git a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt index 952797a37..f3197cebe 100644 --- a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt @@ -17,6 +17,9 @@ import com.google.android.material.color.DynamicColors object ThemeHelper { + /** + * Set the theme, including accent color and night mode + */ fun updateTheme(activity: AppCompatActivity) { val themeMode = PreferenceHelper.getString(PreferenceKeys.THEME_MODE, "A") val pureThemeEnabled = PreferenceHelper.getBoolean(PreferenceKeys.PURE_THEME, false) @@ -25,6 +28,9 @@ object ThemeHelper { updateThemeMode(themeMode) } + /** + * Update the accent color of the app + */ private fun updateAccentColor( activity: AppCompatActivity, pureThemeEnabled: Boolean @@ -51,13 +57,16 @@ object ThemeHelper { activity.setTheme(theme) } + /** + * apply dynamic colors to the activity + */ private fun applyDynamicColors(activity: AppCompatActivity) { - /** - * apply dynamic colors to the activity - */ DynamicColors.applyToActivityIfAvailable(activity) } + /** + * set the theme mode (light, dark, auto) + */ private fun updateThemeMode(themeMode: String) { val mode = when (themeMode) { "A" -> AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM @@ -68,6 +77,9 @@ object ThemeHelper { AppCompatDelegate.setDefaultNightMode(mode) } + /** + * change the app icon + */ fun changeIcon(context: Context, newLogoActivityAlias: String) { val activityAliases = context.resources.getStringArray(R.array.iconsValue) // Disable Old Icon(s) @@ -96,7 +108,9 @@ object ThemeHelper { ) } - // Needed due to different MainActivity Aliases because of the app icons + /** + * Needed due to different MainActivity Aliases because of the app icons + */ fun restartMainActivity(context: Context) { // kill player notification val nManager = context @@ -111,12 +125,18 @@ object ThemeHelper { android.os.Process.killProcess(android.os.Process.myPid()) } + /** + * Get a color by a resource code + */ fun getThemeColor(context: Context, colorCode: Int): Int { val value = TypedValue() context.theme.resolveAttribute(colorCode, value, true) return value.data } + /** + * Get the styled app name + */ fun getStyledAppName(context: Context): Spanned { val colorPrimary = getThemeColor(context, R.attr.colorPrimaryDark) val hexColor = String.format("#%06X", (0xFFFFFF and colorPrimary))