Refactor edge-to-edge

Change edge-to-edge to auto-enable on 27+, along with relying on styles to make the systemui changes instead of code directly.
This commit is contained in:
OxygenCobalt
2020-12-18 10:45:42 -07:00
parent 0d32ab0f7e
commit 4413cb1499
20 changed files with 275 additions and 418 deletions
@@ -1,7 +1,6 @@
package org.oxycblt.auxio
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
@@ -13,13 +12,11 @@ import org.oxycblt.auxio.databinding.ActivityMainBinding
import org.oxycblt.auxio.playback.PlaybackService
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.accent
import org.oxycblt.auxio.ui.handleTransparentSystemBars
import org.oxycblt.auxio.ui.toColor
import org.oxycblt.auxio.ui.isEdgeOn
// FIXME: Fix bug where fast navigation will break the animations and
// lead to nothing being displayed [Possibly Un-fixable]
// FIXME: Compat issue with Versions 5 that leads to progress bar looking off
// TODO: Try to heavily refactor edge-to-edge
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -39,7 +36,7 @@ class MainActivity : AppCompatActivity() {
// Apply the theme
setTheme(accent.second)
if (settingsManager.edgeEnabled && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
if (isEdgeOn()) {
doEdgeToEdgeSetup(binding)
}
}
@@ -55,11 +52,6 @@ class MainActivity : AppCompatActivity() {
@Suppress("DEPRECATION")
private fun doEdgeToEdgeSetup(binding: ActivityMainBinding) {
window?.apply {
statusBarColor = Color.TRANSPARENT
// Use a heavily transparent scrim on the nav bar as full transparency is borked
navigationBarColor = R.color.nav_color.toColor(this@MainActivity)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// Do modern edge to edge [Which is really a shot in the dark tbh]
this@MainActivity.logD("Doing R+ edge-to-edge.")
@@ -81,8 +73,6 @@ class MainActivity : AppCompatActivity() {
binding.root.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
}
handleTransparentSystemBars(resources.configuration)
}
}
}
@@ -14,8 +14,8 @@ import org.oxycblt.auxio.R
import org.oxycblt.auxio.databinding.FragmentCompactPlaybackBinding
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.ui.memberBinding
import org.oxycblt.auxio.ui.isLandscape
import org.oxycblt.auxio.ui.memberBinding
/**
* A [Fragment] that displays the currently played song at a glance, with some basic controls.
@@ -17,7 +17,7 @@ import org.oxycblt.auxio.music.BaseModel
import org.oxycblt.auxio.music.Header
import org.oxycblt.auxio.playback.PlaybackViewModel
import org.oxycblt.auxio.playback.state.PlaybackMode
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.isEdgeOn
import org.oxycblt.auxio.ui.isIrregularLandscape
/**
@@ -50,8 +50,7 @@ class QueueFragment : Fragment() {
findNavController().navigateUp()
}
if (!requireActivity().isIrregularLandscape() &&
SettingsManager.getInstance().edgeEnabled) {
if (!requireActivity().isIrregularLandscape() && isEdgeOn()) {
setOnApplyWindowInsetsListener @Suppress("DEPRECATION") { _, insets ->
val top = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
insets.getInsets(WindowInsets.Type.systemBars()).top
@@ -42,9 +42,6 @@ class SettingsManager private constructor(context: Context) :
.apply()
}
val edgeEnabled: Boolean
get() = sharedPrefs.getBoolean(Keys.KEY_EDGE_TO_EDGE, false)
val colorizeNotif: Boolean
get() = sharedPrefs.getBoolean(Keys.KEY_COLORIZE_NOTIFICATION, true)
@@ -1,16 +1,15 @@
package org.oxycblt.auxio.settings.ui
import android.app.Dialog
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.browser.customtabs.CustomTabsIntent
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.net.toUri
import androidx.fragment.app.DialogFragment
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import org.oxycblt.auxio.BuildConfig
import org.oxycblt.auxio.R
@@ -20,8 +19,7 @@ import org.oxycblt.auxio.logE
import org.oxycblt.auxio.music.MusicStore
import org.oxycblt.auxio.settings.SettingsManager
import org.oxycblt.auxio.ui.createToast
import org.oxycblt.auxio.ui.handleTransparentSystemBars
import org.oxycblt.auxio.ui.handleTransparentSystemNavBar
import org.oxycblt.auxio.ui.isEdgeOn
import org.oxycblt.auxio.ui.isIrregularLandscape
import org.oxycblt.auxio.ui.isLandscape
import org.oxycblt.auxio.ui.toColor
@@ -40,12 +38,10 @@ class AboutDialog : BottomSheetDialogFragment() {
val settingsManager = SettingsManager.getInstance()
// --- UI SETUP ---
// Apply edge-to-edge to the dialog if supported/enabled.
if (settingsManager.edgeEnabled && !requireActivity().isIrregularLandscape()) {
if (isEdgeOn() && !requireActivity().isIrregularLandscape()) {
requireDialog().window?.apply {
navigationBarColor = R.color.background.toColor(requireContext())
handleTransparentSystemNavBar(resources.configuration)
// handleTransparentSystemNavBar(resources.configuration)
}
}
@@ -62,6 +58,15 @@ class AboutDialog : BottomSheetDialogFragment() {
R.string.format_author, getString(R.string.author_oxycblt)
)
if (isLandscape(resources)) {
val dialog = requireDialog() as BottomSheetDialog
dialog.findViewById<CoordinatorLayout>(
com.google.android.material.R.id.design_bottom_sheet
)?.let {
BottomSheetBehavior.from(it).state = BottomSheetBehavior.STATE_EXPANDED
}
}
logD("Dialog created.")
return binding.root
@@ -111,11 +116,10 @@ class AboutDialog : BottomSheetDialogFragment() {
}
companion object {
// TODO: Change dev to master
private const val LINK_CODEBASE = "https://github.com/oxygencobalt/Auxio"
private const val LINK_FAQ = "$LINK_CODEBASE/blob/master/FAQ.md"
private const val LINK_LICENSES = "$LINK_CODEBASE/blob/master/LICENSES.md"
val LINKS = arrayOf(LINK_CODEBASE, LINK_FAQ, LINK_LICENSES)
}
}
}
@@ -6,15 +6,10 @@ package org.oxycblt.auxio.ui
import android.annotation.TargetApi
import android.app.Activity
import android.content.Context
import android.content.res.Configuration
import android.graphics.Point
import android.os.Build
import android.util.DisplayMetrics
import android.view.View
import android.view.Window
import android.view.WindowInsetsController
import android.view.WindowManager
import org.oxycblt.auxio.settings.SettingsManager
/**
* Check if we are in the "Irregular" landscape mode [e.g landscape, but nav bar is on the sides]
@@ -27,6 +22,10 @@ fun Activity.isIrregularLandscape(): Boolean {
!isSystemBarOnBottom(this)
}
fun isEdgeOn(): Boolean {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1
}
/**
* Check if the system bars are on the bottom.
* @return If the system bars are on the bottom, false if no.
@@ -62,61 +61,3 @@ private fun isSystemBarOnBottom(activity: Activity): Boolean {
return (!canMove || width < height)
}
/**
* Handle transparent system bars. Adapted from Music Player GO
* (https://github.com/enricocid/Music-Player-GO)
*/
fun Window.handleTransparentSystemBars(config: Configuration) {
fun isNight() = config.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
insetsController?.let { controller ->
val appearance = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS or
WindowInsetsController.APPEARANCE_LIGHT_STATUS_BARS
val mask = if (isNight()) 0 else appearance
controller.setSystemBarsAppearance(appearance, mask)
}
} else {
val flags = decorView.systemUiVisibility
decorView.systemUiVisibility =
if (isNight()) {
flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() and
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
} else {
flags or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR or
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
}
/**
* Handle only the transparent navigation bar.
*/
fun Window.handleTransparentSystemNavBar(config: Configuration) {
fun isNight() = config.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
insetsController?.let { controller ->
val appearance = WindowInsetsController.APPEARANCE_LIGHT_NAVIGATION_BARS
val mask = if (isNight()) 0 else appearance
controller.setSystemBarsAppearance(appearance, mask)
}
} else {
val flags = decorView.systemUiVisibility
decorView.systemUiVisibility =
if (isNight()) {
flags and View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR.inv() and
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR.inv()
} else {
flags or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR or
View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
}
}
}
@@ -20,7 +20,8 @@ import kotlin.reflect.KProperty
* @param onDestroy Any code that should be run when the binding is destroyed.
*/
fun <T : ViewBinding> Fragment.memberBinding(
bindingFactory: (LayoutInflater) -> T, onDestroy: T.() -> Unit = {}
bindingFactory: (LayoutInflater) -> T,
onDestroy: T.() -> Unit = {}
) = FragmentBinderDelegate(this, bindingFactory, onDestroy)
/**