- handle top/button margin if there's a cutout with windowInsets.displayCutout
- handle nav bar color
- remove 16:9 size if the full screen size is already 16:9
- change layout to use Button instead of explicit MaterialButton
This commit is contained in:
darthpaul 2022-08-25 06:56:15 +01:00
parent e6a588a218
commit c220b478c0
8 changed files with 53 additions and 65 deletions

View File

@ -13,7 +13,8 @@ import android.view.*
import android.widget.LinearLayout
import androidx.appcompat.content.res.AppCompatResources
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.WindowCompat
import androidx.core.content.ContextCompat
import androidx.core.view.*
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
@ -81,29 +82,23 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
supportActionBar?.hide()
checkWhatsNewDialog()
setupOrientationEventListener()
if (isRPlus()) {
val windowInsetsController = ViewCompat.getWindowInsetsController(window.decorView)
windowInsetsController?.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
windowInsetsController?.hide(WindowInsetsCompat.Type.statusBars())
if (isOreoMr1Plus()) {
setShowWhenLocked(true)
setTurnScreenOn(true)
window.insetsController?.hide(WindowInsets.Type.statusBars())
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
} else if (isOreoMr1Plus()) {
setShowWhenLocked(true)
setTurnScreenOn(true)
window.addFlags(
WindowManager.LayoutParams.FLAG_FULLSCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD or
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED or
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON or
WindowManager.LayoutParams.FLAG_FULLSCREEN or
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
}
selectPhotoTab()
}
private fun selectPhotoTab(triggerListener: Boolean = false) {
@ -142,11 +137,14 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
checkButtons()
}
toggleBottomButtons(false)
}
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
if (hasStorageAndCameraPermissions()) {
mOrientationEventListener.enable()
}
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
ensureTransparentNavigationBar()
}
private fun ensureTransparentNavigationBar() {
window.navigationBarColor = ContextCompat.getColor(this, android.R.color.transparent)
}
override fun onPause() {
@ -274,36 +272,25 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
setContentView(R.layout.activity_main)
initButtons()
(toggle_flash.layoutParams as ConstraintLayout.LayoutParams).setMargins(
0,
statusBarHeight,
0,
0,
)
ViewCompat.setOnApplyWindowInsetsListener(view_holder) { _, windowInsets ->
val safeInsetBottom = windowInsets.displayCutout?.safeInsetBottom ?: 0
val safeInsetTop = windowInsets.displayCutout?.safeInsetTop ?: 0
(flash_toggle_group.layoutParams as ConstraintLayout.LayoutParams).setMargins(
0,
statusBarHeight,
0,
0,
)
listOf(toggle_flash, flash_toggle_group, media_size_toggle_group).forEach { view ->
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
topMargin = safeInsetTop
}
}
(media_size_toggle_group.layoutParams as ConstraintLayout.LayoutParams).setMargins(
0,
statusBarHeight,
0,
0,
)
val marginBottom = safeInsetBottom + navigationBarHeight + resources.getDimensionPixelSize(R.dimen.bigger_margin)
(shutter.layoutParams as ConstraintLayout.LayoutParams).goneBottomMargin = marginBottom
val goneMargin = (navigationBarHeight + resources.getDimension(R.dimen.bigger_margin)).toInt()
(shutter.layoutParams as ConstraintLayout.LayoutParams).goneBottomMargin = goneMargin
video_rec_curr_timer.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = marginBottom
}
(video_rec_curr_timer.layoutParams as ConstraintLayout.LayoutParams).setMargins(
0,
0,
0,
(navigationBarHeight + resources.getDimension(R.dimen.bigger_margin)).toInt()
)
WindowInsetsCompat.CONSUMED
}
checkVideoCaptureIntent()
if (mIsInPhotoMode) {
@ -372,7 +359,7 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
private fun toggleFlash() {
if (checkCameraAvailable()) {
showFlashOptions(mIsInPhotoMode)
showFlashOptions(mIsInPhotoMode)
}
}
@ -749,11 +736,11 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera
showResolutionOptions()
}
private fun createButton(resolutionOption: ResolutionOption): MaterialButton {
private fun createButton(resolutionOption: ResolutionOption): MaterialButton? {
val params = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT).apply {
weight = 1f
}
return (layoutInflater.inflate(R.layout.layout_button, null) as MaterialButton).apply {
return (layoutInflater.inflate(R.layout.layout_button, null) as? MaterialButton)?.apply {
layoutParams = params
icon = AppCompatResources.getDrawable(context, resolutionOption.imageDrawableResId)
id = resolutionOption.buttonViewId

View File

@ -63,14 +63,14 @@ class ImageQualityManager(
.flatMap { it.qualities }
.sortedByDescending { it.pixels }
.distinctBy { it.getAspectRatio(activity) }
.filter { it.isSupported() }
.filter { it.isSupported(fullScreenSize.isSixteenToNine()) }
}
private fun getFullScreenResolution(cameraSelector: CameraSelector): MySize {
return imageQualities.filter { it.camSelector == cameraSelector }
.flatMap { it.qualities }
.sortedByDescending { it.width }
.first { it.isSupported() }
.first { it.isSupported(false) }
.copy(isFullScreen = true)
}
}

View File

@ -18,7 +18,7 @@ data class MySize(val width: Int, val height: Int, val isFullScreen: Boolean = f
val megaPixels: String = String.format("%.1f", (width * height.toFloat()) / ONE_MEGA_PIXEL)
private fun isSixteenToNine() = ratio == 16 / 9f
fun isSixteenToNine() = ratio == 16 / 9f
private fun isFiveToThree() = ratio == 5 / 3f
@ -40,8 +40,12 @@ data class MySize(val width: Int, val height: Int, val isFullScreen: Boolean = f
private fun isSquare() = width == height
fun isSupported(): Boolean {
return (isFourToThree() || isSixteenToNine() || isSquare()) && megaPixels != ZERO_MEGA_PIXEL
fun isSupported(isFullScreenSize16x9: Boolean): Boolean {
return if (isFullScreenSize16x9) {
isFourToThree() || isSquare()
} else {
isFourToThree() || isSixteenToNine() || isSquare()
} && megaPixels != ZERO_MEGA_PIXEL
}
fun getAspectRatio(context: Context) = when {

View File

@ -6,7 +6,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:fitsSystemWindows="false">
android:fitsSystemWindows="true">
<androidx.camera.view.PreviewView
android:id="@+id/preview_view"
@ -18,7 +18,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0"
android:background="@color/md_grey_black" />
android:background="@android:color/black" />
<include
layout="@layout/layout_media_size"
@ -103,8 +103,7 @@
android:id="@+id/bottom_overlay"
android:layout_width="match_parent"
android:layout_height="0dp"
android:alpha="0.3"
android:background="@color/gradient_grey_start"
android:background="@color/overlay_color"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
@ -122,7 +121,7 @@
app:tabBackground="@drawable/tab_indicator"
app:tabIndicator="@null"
app:tabMode="auto"
app:tabRippleColor="@color/md_grey_600_dark"
app:tabRippleColor="@null"
app:tabSelectedTextColor="@color/md_grey_600_dark"
app:tabTextColor="@color/md_grey_white">

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton xmlns:android="http://schemas.android.com/apk/res/android"
<Button xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
android:layout_width="wrap_content"

View File

@ -8,7 +8,7 @@
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
<Button
android:id="@+id/flash_off"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
android:layout_width="0dp"
@ -16,7 +16,7 @@
android:layout_weight="1"
app:icon="@drawable/ic_flash_off_vector" />
<com.google.android.material.button.MaterialButton
<Button
android:id="@+id/flash_auto"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
android:layout_width="0dp"
@ -24,7 +24,7 @@
android:layout_weight="1"
app:icon="@drawable/ic_flash_auto_vector" />
<com.google.android.material.button.MaterialButton
<Button
android:id="@+id/flash_on"
style="@style/Widget.App.Button.OutlineButton.IconOnly"
android:layout_width="0dp"

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="overlay_color">#2e000000</color>
</resources>

View File

@ -4,12 +4,10 @@
<style name="AppTheme" parent="AppTheme.Base" />
<style name="FullScreenTheme" parent="AppTheme">
<item name="android:navigationBarColor">@android:color/transparent</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
<item name="android:enforceNavigationBarContrast">false</item>
</style>
<style name="Widget.App.Button.OutlineButton.IconOnly" parent="Widget.MaterialComponents.Button.TextButton">