Skip to content

Commit

Permalink
[feature] Support import external subtitles (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyD666 committed May 19, 2024
1 parent 13d13aa commit 2b08882
Show file tree
Hide file tree
Showing 10 changed files with 475 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ android {
minSdk = 24
targetSdk = 34
versionCode = 16
versionName = "1.1-beta28"
versionName = "1.1-beta29"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/ContextExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.content.pm.PackageManager
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.view.Window
import androidx.core.content.ContextCompat
import androidx.core.content.pm.PackageInfoCompat

Expand All @@ -29,6 +30,18 @@ val Context.tryActivity: Activity?
return null
}

val Context.tryWindow: Window?
get() {
var ctx = this
while (ctx is ContextWrapper) {
if (ctx is Activity) {
return ctx.window
}
ctx = ctx.baseContext
}
return null
}

val Context.screenIsLand: Boolean
get() = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE

Expand Down
4 changes: 4 additions & 0 deletions app/src/main/java/com/skyd/anivu/ext/ViewExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.view.ViewGroup
import android.view.Window
import android.view.animation.AlphaAnimation
import android.view.inputmethod.InputMethodManager
import androidx.compose.ui.window.DialogWindowProvider
import androidx.core.graphics.Insets
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
Expand Down Expand Up @@ -67,6 +68,9 @@ val View.activity: Activity
val View.tryActivity: Activity?
get() = context.tryActivity

val View.tryWindow: Window?
get() = (parent as? DialogWindowProvider)?.window ?: context.tryWindow

/**
* 判断View和给定的Rect是否重叠(边和点不计入)
* @return true if overlap
Expand Down
13 changes: 0 additions & 13 deletions app/src/main/java/com/skyd/anivu/ui/activity/PlayActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.content.IntentCompat
import androidx.core.util.Consumer
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.skyd.anivu.base.BaseComposeActivity
import com.skyd.anivu.ext.savePictureToMediaStore
import com.skyd.anivu.ui.component.showToast
Expand Down Expand Up @@ -46,16 +43,6 @@ class PlayActivity : BaseComposeActivity() {

super.onCreate(savedInstanceState)

val windowInsetsController =
WindowCompat.getInsetsController(window, window.decorView)
// Configure the behavior of the hidden system bars.
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE

window.decorView.setOnApplyWindowInsetsListener { view, windowInsets ->
windowInsetsController.hide(WindowInsetsCompat.Type.systemBars())
view.onApplyWindowInsets(windowInsets)
}
// Keep screen on
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

Expand Down
279 changes: 279 additions & 0 deletions app/src/main/java/com/skyd/anivu/ui/component/SystemUiController.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,279 @@
package com.skyd.anivu.ui.component

import android.app.Activity
import android.os.Build
import android.view.View
import android.view.Window
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.luminance
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.window.DialogWindowProvider
import androidx.core.view.ViewCompat
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import com.skyd.anivu.ext.tryWindow


@Stable
interface SystemUiController {

/**
* Control for the behavior of the system bars. This value should be one of the
* [WindowInsetsControllerCompat] behavior constants:
* [WindowInsetsControllerCompat.BEHAVIOR_SHOW_BARS_BY_TOUCH] (Deprecated),
* [WindowInsetsControllerCompat.BEHAVIOR_DEFAULT] and
* [WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE].
*/
var systemBarsBehavior: Int

/**
* Property which holds the status bar visibility. If set to true, show the status bar,
* otherwise hide the status bar.
*/
var isStatusBarVisible: Boolean

/**
* Property which holds the navigation bar visibility. If set to true, show the navigation bar,
* otherwise hide the navigation bar.
*/
var isNavigationBarVisible: Boolean

/**
* Property which holds the status & navigation bar visibility. If set to true, show both bars,
* otherwise hide both bars.
*/
var isSystemBarsVisible: Boolean
get() = isNavigationBarVisible && isStatusBarVisible
set(value) {
isStatusBarVisible = value
isNavigationBarVisible = value
}

/**
* Set the status bar color.
*
* @param color The **desired** [Color] to set. This may require modification if running on an
* API level that only supports white status bar icons.
* @param darkIcons Whether dark status bar icons would be preferable.
* @param transformColorForLightContent A lambda which will be invoked to transform [color] if
* dark icons were requested but are not available. Defaults to applying a black scrim.
*
* @see statusBarDarkContentEnabled
*/
fun setStatusBarColor(
color: Color,
darkIcons: Boolean = color.luminance() > 0.5f,
transformColorForLightContent: (Color) -> Color = BlackScrimmed
)

/**
* Set the navigation bar color.
*
* @param color The **desired** [Color] to set. This may require modification if running on an
* API level that only supports white navigation bar icons. Additionally this will be ignored
* and [Color.Transparent] will be used on API 29+ where gesture navigation is preferred or the
* system UI automatically applies background protection in other navigation modes.
* @param darkIcons Whether dark navigation bar icons would be preferable.
* @param navigationBarContrastEnforced Whether the system should ensure that the navigation
* bar has enough contrast when a fully transparent background is requested. Only supported on
* API 29+.
* @param transformColorForLightContent A lambda which will be invoked to transform [color] if
* dark icons were requested but are not available. Defaults to applying a black scrim.
*
* @see navigationBarDarkContentEnabled
* @see navigationBarContrastEnforced
*/
fun setNavigationBarColor(
color: Color,
darkIcons: Boolean = color.luminance() > 0.5f,
navigationBarContrastEnforced: Boolean = true,
transformColorForLightContent: (Color) -> Color = BlackScrimmed
)

/**
* Set the status and navigation bars to [color].
*
* @see setStatusBarColor
* @see setNavigationBarColor
*/
fun setSystemBarsColor(
color: Color,
darkIcons: Boolean = color.luminance() > 0.5f,
isNavigationBarContrastEnforced: Boolean = true,
transformColorForLightContent: (Color) -> Color = BlackScrimmed
) {
setStatusBarColor(color, darkIcons, transformColorForLightContent)
setNavigationBarColor(
color,
darkIcons,
isNavigationBarContrastEnforced,
transformColorForLightContent
)
}

/**
* Property which holds whether the status bar icons + content are 'dark' or not.
*/
var statusBarDarkContentEnabled: Boolean

/**
* Property which holds whether the navigation bar icons + content are 'dark' or not.
*/
var navigationBarDarkContentEnabled: Boolean

/**
* Property which holds whether the status & navigation bar icons + content are 'dark' or not.
*/
var systemBarsDarkContentEnabled: Boolean
get() = statusBarDarkContentEnabled && navigationBarDarkContentEnabled
set(value) {
statusBarDarkContentEnabled = value
navigationBarDarkContentEnabled = value
}

/**
* Property which holds whether the system is ensuring that the navigation bar has enough
* contrast when a fully transparent background is requested. Only has an affect when running
* on Android API 29+ devices.
*/
var isNavigationBarContrastEnforced: Boolean
}

/**
* Remembers a [SystemUiController] for the given [window].
*
* If no [window] is provided, an attempt to find the correct [Window] is made.
*
* First, if the [LocalView]'s parent is a [DialogWindowProvider], then that dialog's [Window] will
* be used.
*
* Second, we attempt to find [Window] for the [Activity] containing the [LocalView].
*
* If none of these are found (such as may happen in a preview), then the functionality of the
* returned [SystemUiController] will be degraded, but won't throw an exception.
*/
@Composable
fun rememberSystemUiController(
window: Window? = LocalView.current.tryWindow,
): SystemUiController {
val view = LocalView.current
return remember(view, window) { AndroidSystemUiController(view, window) }
}

/**
* A helper class for setting the navigation and status bar colors for a [View], gracefully
* degrading behavior based upon API level.
*
* Typically you would use [rememberSystemUiController] to remember an instance of this.
*/
internal class AndroidSystemUiController(
private val view: View,
private val window: Window?
) : SystemUiController {
private val windowInsetsController = window?.let {
WindowCompat.getInsetsController(it, view)
}

override fun setStatusBarColor(
color: Color,
darkIcons: Boolean,
transformColorForLightContent: (Color) -> Color
) {
statusBarDarkContentEnabled = darkIcons

window?.statusBarColor = when {
darkIcons && windowInsetsController?.isAppearanceLightStatusBars != true -> {
// If we're set to use dark icons, but our windowInsetsController call didn't
// succeed (usually due to API level), we instead transform the color to maintain
// contrast
transformColorForLightContent(color)
}

else -> color
}.toArgb()
}

override fun setNavigationBarColor(
color: Color,
darkIcons: Boolean,
navigationBarContrastEnforced: Boolean,
transformColorForLightContent: (Color) -> Color
) {
navigationBarDarkContentEnabled = darkIcons
isNavigationBarContrastEnforced = navigationBarContrastEnforced

window?.navigationBarColor = when {
darkIcons && windowInsetsController?.isAppearanceLightNavigationBars != true -> {
// If we're set to use dark icons, but our windowInsetsController call didn't
// succeed (usually due to API level), we instead transform the color to maintain
// contrast
transformColorForLightContent(color)
}

else -> color
}.toArgb()
}

override var systemBarsBehavior: Int
get() = windowInsetsController?.systemBarsBehavior ?: 0
set(value) {
windowInsetsController?.systemBarsBehavior = value
}

override var isStatusBarVisible: Boolean
get() {
return ViewCompat.getRootWindowInsets(view)
?.isVisible(WindowInsetsCompat.Type.statusBars()) == true
}
set(value) {
if (value) {
windowInsetsController?.show(WindowInsetsCompat.Type.statusBars())
} else {
windowInsetsController?.hide(WindowInsetsCompat.Type.statusBars())
}
}

override var isNavigationBarVisible: Boolean
get() {
return ViewCompat.getRootWindowInsets(view)
?.isVisible(WindowInsetsCompat.Type.navigationBars()) == true
}
set(value) {
if (value) {
windowInsetsController?.show(WindowInsetsCompat.Type.navigationBars())
} else {
windowInsetsController?.hide(WindowInsetsCompat.Type.navigationBars())
}
}

override var statusBarDarkContentEnabled: Boolean
get() = windowInsetsController?.isAppearanceLightStatusBars == true
set(value) {
windowInsetsController?.isAppearanceLightStatusBars = value
}

override var navigationBarDarkContentEnabled: Boolean
get() = windowInsetsController?.isAppearanceLightNavigationBars == true
set(value) {
windowInsetsController?.isAppearanceLightNavigationBars = value
}

override var isNavigationBarContrastEnforced: Boolean
get() = Build.VERSION.SDK_INT >= 29 && window?.isNavigationBarContrastEnforced == true
set(value) {
if (Build.VERSION.SDK_INT >= 29) {
window?.isNavigationBarContrastEnforced = value
}
}
}

private val BlackScrim = Color(0f, 0f, 0f, 0.3f) // 30% opaque black
private val BlackScrimmed: (Color) -> Color = { original ->
BlackScrim.compositeOver(original)
}
Loading

0 comments on commit 2b08882

Please sign in to comment.