From 559c831d0823f163d0b781d81436766ece520bcb Mon Sep 17 00:00:00 2001 From: "Daniele Verducci (Slimpenguin)" Date: Wed, 2 Feb 2022 09:16:47 +0100 Subject: [PATCH] Fixed back button exiting from app in full screen mode --- .../it/danieleverducci/ojo/ui/MainActivity.java | 13 +++++++++++++ .../ojo/ui/OnBackButtonPressedListener.java | 5 +++++ .../ojo/ui/SurveillanceFragment.java | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/it/danieleverducci/ojo/ui/OnBackButtonPressedListener.java diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/MainActivity.java b/app/src/main/java/it/danieleverducci/ojo/ui/MainActivity.java index 50d4d8b..dedddc1 100644 --- a/app/src/main/java/it/danieleverducci/ojo/ui/MainActivity.java +++ b/app/src/main/java/it/danieleverducci/ojo/ui/MainActivity.java @@ -8,6 +8,7 @@ import androidx.appcompat.app.AppCompatActivity; import android.util.Log; import android.view.View; +import androidx.fragment.app.Fragment; import androidx.navigation.NavController; import androidx.navigation.Navigation; @@ -21,6 +22,7 @@ public class MainActivity extends AppCompatActivity { private ActivityMainBinding binding; private NavController navController; private boolean rotationEnabledSetting; + private OnBackButtonPressedListener onBackButtonPressedListener; @Override protected void onCreate(Bundle savedInstanceState) { @@ -49,6 +51,17 @@ public class MainActivity extends AppCompatActivity { }); } + public void setOnBackButtonPressedListener(OnBackButtonPressedListener onBackButtonPressedListener) { + this.onBackButtonPressedListener = onBackButtonPressedListener; + } + + @Override + public void onBackPressed() { + if (this.onBackButtonPressedListener != null && this.onBackButtonPressedListener.onBackPressed()) + return; + super.onBackPressed(); + } + public void navigateToFragment(int actionId) { navigateToFragment(actionId, null); } diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/OnBackButtonPressedListener.java b/app/src/main/java/it/danieleverducci/ojo/ui/OnBackButtonPressedListener.java new file mode 100644 index 0000000..984c76f --- /dev/null +++ b/app/src/main/java/it/danieleverducci/ojo/ui/OnBackButtonPressedListener.java @@ -0,0 +1,5 @@ +package it.danieleverducci.ojo.ui; + +public interface OnBackButtonPressedListener { + public boolean onBackPressed(); +} diff --git a/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java b/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java index a96ee8b..dac17fa 100644 --- a/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java +++ b/app/src/main/java/it/danieleverducci/ojo/ui/SurveillanceFragment.java @@ -80,7 +80,6 @@ public class SurveillanceFragment extends Fragment { binding = FragmentSurveillanceBinding.inflate(inflater, container, false); return binding.getRoot(); - } @Override @@ -106,12 +105,26 @@ public class SurveillanceFragment extends Fragment { } } + fullscreenCameraView = false; addAllCameras(); // Start playback for all streams for (CameraView cv : cameraViews) { cv.startPlayback(); } + + // Register for back pressed events + ((MainActivity)getActivity()).setOnBackButtonPressedListener(new OnBackButtonPressedListener() { + @Override + public boolean onBackPressed() { + if(fullscreenCameraView && cameraViews.size() > 1) { + fullscreenCameraView = false; + showAllCameras(); + return true; + } + return false; + } + }); } @Override