From 865aff4fdee677b6e0b4fd301feda51a9385106b Mon Sep 17 00:00:00 2001 From: tehcneko <7741672-NekoInverter@users.noreply.gitlab.com> Date: Wed, 5 Jan 2022 19:39:35 +0800 Subject: [PATCH] Fix fullscreen on Android 12 --- .../hippo/ehviewer/ui/GalleryActivity.java | 72 ++++++++----------- .../hippo/ehviewer/widget/GalleryHeader.java | 19 +++-- .../hippo/ehviewer/widget/SeekBarPanel.java | 6 -- .../java/com/hippo/util/SystemUiHelper.java | 46 ------------ app/src/main/res/layout/activity_gallery.xml | 8 ++- 5 files changed, 49 insertions(+), 102 deletions(-) delete mode 100644 app/src/main/java/com/hippo/util/SystemUiHelper.java diff --git a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java index d5bfd23c6..a455ba719 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/GalleryActivity.java @@ -31,7 +31,6 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.PackageManager; -import android.content.res.Configuration; import android.content.res.Resources; import android.graphics.Typeface; import android.net.Uri; @@ -61,6 +60,10 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.AlertDialog; import androidx.core.content.ContextCompat; import androidx.core.content.FileProvider; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.core.view.WindowInsetsControllerCompat; import com.hippo.ehviewer.AppConfig; import com.hippo.ehviewer.BuildConfig; @@ -82,7 +85,6 @@ import com.hippo.glview.view.GLRootView; import com.hippo.unifile.UniFile; import com.hippo.util.ExceptionUtils; import com.hippo.util.IoThreadPoolExecutor; -import com.hippo.util.SystemUiHelper; import com.hippo.widget.ColorView; import com.hippo.yorozuya.AnimationUtils; import com.hippo.yorozuya.ConcurrentPool; @@ -100,6 +102,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import rikka.core.res.ConfigurationKt; import rikka.core.res.ResourcesKt; import rikka.material.app.DayNightDelegate; @@ -135,8 +138,7 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan @Nullable private GalleryAdapter mGalleryAdapter; @Nullable - private SystemUiHelper mSystemUiHelper; - private boolean mShowSystemUi; + private WindowInsetsControllerCompat insetsController; @Nullable private ColorView mMaskView; @Nullable @@ -328,8 +330,6 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan protected void onCreate(@Nullable Bundle savedInstanceState) { if (Settings.getReadingFullscreen()) { Window w = getWindow(); - w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION, - WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); } @@ -386,22 +386,18 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan mGalleryProvider.setListener(mGalleryAdapter); mGalleryProvider.setGLRoot(mGLRootView); - // System UI helper - if (Settings.getReadingFullscreen()) { - mSystemUiHelper = new SystemUiHelper(this); - mSystemUiHelper.hide(); - mShowSystemUi = false; - } else { - Window window = getWindow(); - View decorView = window.getDecorView(); - int flags = decorView.getSystemUiVisibility(); - if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_YES) <= 0) { - flags |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; + WindowCompat.setDecorFitsSystemWindows(getWindow(), false); + insetsController = ViewCompat.getWindowInsetsController(getWindow().getDecorView()); + if (insetsController != null) { + if (Settings.getReadingFullscreen()) { + insetsController.setSystemBarsBehavior(WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE); + insetsController.hide(WindowInsetsCompat.Type.systemBars()); } else { - flags &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR; + insetsController.show(WindowInsetsCompat.Type.systemBars()); } - decorView.setSystemUiVisibility(flags); - window.setStatusBarColor(ResourcesKt.resolveColor(getTheme(), android.R.attr.colorBackground)); + boolean night = ConfigurationKt.isNight(getResources().getConfiguration()); + insetsController.setAppearanceLightStatusBars(!night); + insetsController.setAppearanceLightNavigationBars(!night); } mMaskView = (ColorView) ViewUtils.$$(this, R.id.mask); @@ -479,13 +475,16 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan // Cutout if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) { getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES; - - GalleryHeader galleryHeader = findViewById(R.id.gallery_header); - galleryHeader.setOnApplyWindowInsetsListener((v, insets) -> { - galleryHeader.setDisplayCutout(insets.getDisplayCutout()); - return insets; - }); } + GalleryHeader galleryHeader = findViewById(R.id.gallery_header); + ViewCompat.setOnApplyWindowInsetsListener(galleryHeader, (v, insets) -> { + if (!Settings.getReadingFullscreen()) { + galleryHeader.setTopInsets(insets.getInsets(WindowInsetsCompat.Type.statusBars()).top); + } else { + galleryHeader.setDisplayCutout(insets.getDisplayCutout()); + } + return WindowInsetsCompat.CONSUMED; + }); if (Settings.getGuideGallery()) { FrameLayout mainLayout = (FrameLayout) ViewUtils.$$(this, R.id.main); @@ -542,15 +541,6 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan public void onWindowFocusChanged(boolean hasFocus) { super.onWindowFocusChanged(hasFocus); - SimpleHandler.getInstance().postDelayed(() -> { - if (hasFocus && mSystemUiHelper != null) { - if (mShowSystemUi) { - mSystemUiHelper.show(); - } else { - mSystemUiHelper.hide(); - } - } - }, 300); } @Override @@ -778,9 +768,9 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan mSeekBarPanelAnimator.addListener(mShowSliderListener); mSeekBarPanelAnimator.start(); - if (null != mSystemUiHelper) { - mSystemUiHelper.show(); - mShowSystemUi = true; + if (insetsController != null) { + if (Settings.getReadingFullscreen()) + insetsController.show(WindowInsetsCompat.Type.systemBars()); } } @@ -797,9 +787,9 @@ public class GalleryActivity extends EhActivity implements SeekBar.OnSeekBarChan mSeekBarPanelAnimator.addListener(mHideSliderListener); mSeekBarPanelAnimator.start(); - if (null != mSystemUiHelper) { - mSystemUiHelper.hide(); - mShowSystemUi = false; + if (insetsController != null) { + if (Settings.getReadingFullscreen()) + insetsController.hide(WindowInsetsCompat.Type.systemBars()); } } diff --git a/app/src/main/java/com/hippo/ehviewer/widget/GalleryHeader.java b/app/src/main/java/com/hippo/ehviewer/widget/GalleryHeader.java index dd33a6399..35bb8b763 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/GalleryHeader.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/GalleryHeader.java @@ -20,19 +20,20 @@ import android.content.Context; import android.graphics.Rect; import android.os.Build; import android.util.AttributeSet; -import android.view.DisplayCutout; import android.view.View; import android.view.ViewGroup; import androidx.annotation.Nullable; import androidx.annotation.RequiresApi; +import androidx.core.view.DisplayCutoutCompat; import com.hippo.ehviewer.R; import com.hippo.yorozuya.ObjectUtils; public class GalleryHeader extends ViewGroup { - private DisplayCutout displayCutout; + private DisplayCutoutCompat displayCutout; + private int topInsets = 0; private View battery; private View progress; @@ -51,14 +52,20 @@ public class GalleryHeader extends ViewGroup { super(context, attrs); } - @RequiresApi(api = Build.VERSION_CODES.P) - public void setDisplayCutout(@Nullable DisplayCutout displayCutout) { + public void setDisplayCutout(@Nullable DisplayCutoutCompat displayCutout) { if (!ObjectUtils.equal(this.displayCutout, displayCutout)) { this.displayCutout = displayCutout; requestLayout(); } } + public void setTopInsets(int topInsets) { + if (this.topInsets != topInsets) { + this.topInsets = topInsets; + requestLayout(); + } + } + @Override protected void onFinishInflate() { super.onFinishInflate(); @@ -78,7 +85,7 @@ public class GalleryHeader extends ViewGroup { } else { left = width - paddingRight - lp.rightMargin - view.getMeasuredWidth(); } - rect.set(left, lp.topMargin, left + view.getMeasuredWidth(), lp.topMargin + view.getMeasuredHeight()); + rect.set(left, lp.topMargin + topInsets, left + view.getMeasuredWidth(), lp.topMargin + topInsets + view.getMeasuredHeight()); } @RequiresApi(api = Build.VERSION_CODES.P) @@ -147,7 +154,7 @@ public class GalleryHeader extends ViewGroup { View child = getChildAt(i); measureChild(child, widthMeasureSpec, heightMeasureSpec); MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); - height = Math.max(height, child.getMeasuredHeight() + lp.topMargin); + height = Math.max(height, child.getMeasuredHeight() + lp.topMargin + topInsets); } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && displayCutout != null) { diff --git a/app/src/main/java/com/hippo/ehviewer/widget/SeekBarPanel.java b/app/src/main/java/com/hippo/ehviewer/widget/SeekBarPanel.java index 5f5f666db..2df3541f1 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/SeekBarPanel.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/SeekBarPanel.java @@ -20,7 +20,6 @@ import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; -import android.view.WindowInsets; import android.widget.LinearLayout; import android.widget.SeekBar; @@ -67,9 +66,4 @@ public class SeekBarPanel extends LinearLayout { return true; } } - - @Override - public WindowInsets onApplyWindowInsets(WindowInsets insets) { - return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(insets.getSystemWindowInsetLeft(), 0, insets.getSystemWindowInsetRight(), insets.getSystemWindowInsetBottom())); - } } diff --git a/app/src/main/java/com/hippo/util/SystemUiHelper.java b/app/src/main/java/com/hippo/util/SystemUiHelper.java deleted file mode 100644 index 2d727c3b0..000000000 --- a/app/src/main/java/com/hippo/util/SystemUiHelper.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2014 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.hippo.util; - -import android.app.Activity; -import android.view.View; - -public final class SystemUiHelper { - final View mDecorView; - - public SystemUiHelper(Activity activity) { - mDecorView = activity.getWindow().getDecorView(); - } - - public void show() { - mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); - } - - public void hide() { - mDecorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY - // Set the content to appear under the system bars so that the - // content doesn't resize when the system bars hide and show. - | View.SYSTEM_UI_FLAG_LAYOUT_STABLE - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN - // Hide the nav bar and status bar - | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION - | View.SYSTEM_UI_FLAG_FULLSCREEN); - } -} diff --git a/app/src/main/res/layout/activity_gallery.xml b/app/src/main/res/layout/activity_gallery.xml index 6030066c5..ec37e607e 100644 --- a/app/src/main/res/layout/activity_gallery.xml +++ b/app/src/main/res/layout/activity_gallery.xml @@ -15,9 +15,11 @@ --> + android:layout_height="match_parent" + app:fitsSystemWindowsInsets="start|end"> + android:visibility="invisible" + app:fitsSystemWindowsInsets="bottom">