Enable media gallery fullscreen mode (#813)

* feat: Enable fullscreen mode on the media gallery screen

* Move `fitsSystemWindows` attribute to root view

* Rename for clarity

* Update fullscreen flag

* Set system bars behaviour for api >= 30
This commit is contained in:
ceokot 2021-12-20 09:38:27 +02:00 committed by GitHub
parent 8ea0690e3a
commit 366b5abdc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 3 deletions

View File

@ -27,6 +27,8 @@ import android.os.Build;
import android.os.Build.VERSION; import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES; import android.os.Build.VERSION_CODES;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
@ -36,6 +38,8 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.Window; import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
@ -92,6 +96,8 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
private final static String TAG = MediaPreviewActivity.class.getSimpleName(); private final static String TAG = MediaPreviewActivity.class.getSimpleName();
private static final int UI_ANIMATION_DELAY = 300;
public static final String ADDRESS_EXTRA = "address"; public static final String ADDRESS_EXTRA = "address";
public static final String DATE_EXTRA = "date"; public static final String DATE_EXTRA = "date";
public static final String SIZE_EXTRA = "size"; public static final String SIZE_EXTRA = "size";
@ -99,6 +105,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
public static final String OUTGOING_EXTRA = "outgoing"; public static final String OUTGOING_EXTRA = "outgoing";
public static final String LEFT_IS_RECENT_EXTRA = "left_is_recent"; public static final String LEFT_IS_RECENT_EXTRA = "left_is_recent";
private View rootContainer;
private ViewPager mediaPager; private ViewPager mediaPager;
private View detailsContainer; private View detailsContainer;
private TextView caption; private TextView caption;
@ -118,6 +125,26 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
private int restartItem = -1; private int restartItem = -1;
private boolean isFullscreen = false;
private final Handler hideHandler = new Handler(Looper.myLooper());
private final Runnable showRunnable = () -> {
getSupportActionBar().show();
};
private final Runnable hideRunnable = () -> {
if (VERSION.SDK_INT >= 30) {
rootContainer.getWindowInsetsController().hide(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
rootContainer.getWindowInsetsController().setSystemBarsBehavior(WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE);
} else {
rootContainer.setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LOW_PROFILE |
View.SYSTEM_UI_FLAG_FULLSCREEN |
View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY |
View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION |
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
}
};
public static Intent getPreviewIntent(Context context, Slide slide, MmsMessageRecord mms, Recipient threadRecipient) { public static Intent getPreviewIntent(Context context, Slide slide, MmsMessageRecord mms, Recipient threadRecipient) {
Intent previewIntent = null; Intent previewIntent = null;
if (MediaPreviewActivity.isContentTypeSupported(slide.getContentType()) && slide.getUri() != null) { if (MediaPreviewActivity.isContentTypeSupported(slide.getContentType()) && slide.getUri() != null) {
@ -147,6 +174,32 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
initializeObservers(); initializeObservers();
} }
private void toggleFullscreen() {
if (isFullscreen) {
exitFullscreen();
} else {
enterFullscreen();
}
}
private void enterFullscreen() {
getSupportActionBar().hide();
isFullscreen = true;
hideHandler.removeCallbacks(showRunnable);
hideHandler.postDelayed(hideRunnable, UI_ANIMATION_DELAY);
}
private void exitFullscreen() {
if (Build.VERSION.SDK_INT >= 30) {
rootContainer.getWindowInsetsController().show(WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
} else {
rootContainer.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
}
isFullscreen = false;
hideHandler.removeCallbacks(hideRunnable);
hideHandler.postDelayed(showRunnable, UI_ANIMATION_DELAY);
}
@Override @Override
public boolean dispatchTouchEvent(MotionEvent ev) { public boolean dispatchTouchEvent(MotionEvent ev) {
clickDetector.onTouchEvent(ev); clickDetector.onTouchEvent(ev);
@ -223,6 +276,7 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
} }
private void initializeViews() { private void initializeViews() {
rootContainer = findViewById(R.id.media_preview_root);
mediaPager = findViewById(R.id.media_pager); mediaPager = findViewById(R.id.media_pager);
mediaPager.setOffscreenPageLimit(1); mediaPager.setOffscreenPageLimit(1);
@ -295,12 +349,13 @@ public class MediaPreviewActivity extends PassphraseRequiredActionBarActivity im
} }
}); });
clickDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { clickDetector = new GestureDetector(this, new GestureDetector.SimpleOnGestureListener() {
@Override @Override
public boolean onSingleTapUp(MotionEvent e) { public boolean onSingleTapUp(MotionEvent e) {
if (e.getY() < detailsContainer.getTop()) { if (e.getY() < detailsContainer.getTop()) {
detailsContainer.setVisibility(detailsContainer.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE); detailsContainer.setVisibility(detailsContainer.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
} }
toggleFullscreen();
return super.onSingleTapUp(e); return super.onSingleTapUp(e);
} }
}); });

View File

@ -4,8 +4,10 @@
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/media_preview_root"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout <com.google.android.material.appbar.AppBarLayout
style="@style/Widget.Session.AppBarLayout" style="@style/Widget.Session.AppBarLayout"
@ -24,7 +26,7 @@
android:id="@+id/media_pager" android:id="@+id/media_pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:clickable="true"/> android:clickable="true" />
<LinearLayout <LinearLayout
android:id="@+id/media_preview_details_container" android:id="@+id/media_preview_details_container"