Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ad869d4b authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽 Committed by Mohammed Althaf T
Browse files

171-223-r-Fix_preview_not_drawn_under_navigation_bar

parent 0271d8c2
Loading
Loading
Loading
Loading
+58 −0
Original line number Diff line number Diff line
@@ -737,10 +737,41 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen
        this.hasOldSystemOrientation = true;
        this.oldSystemOrientation = getSystemOrientation();

        calculateNavigationGap();

        if( MyDebug.LOG )
            Log.d(TAG, "onCreate: total time for Activity startup: " + (System.currentTimeMillis() - debug_time));
    }

    /**
     * if navigationMode is no gesture, then retrieve navigationBar's height & update navigation_gap
     */
    private void calculateNavigationGap() {
        int resourceId = getResources().getIdentifier("config_navBarInteractionMode", "integer", "android");
        if (resourceId > 0) {
            int navType = getResources().getInteger(resourceId);
            if (navType != 2) { //gesture mode = 2
                resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    navigation_gap = getResources().getDimensionPixelSize(resourceId);
                }
            }
        }
    }

    public void handleDecorFitsSystemWindows() {
        setDecorFitsSystemWindows(!isInFullScreenMode());
    }

    /**
     * check is the preview size = maximise && is not in video mode, then return true
     */
    public boolean isInFullScreenMode() {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String preview_size = sharedPreferences.getString(PreferenceKeys.PreviewSizePreferenceKey, "preference_preview_size_display");
        return !(preview_size.equals("preference_preview_size_wysiwyg") || preview.isVideo());
    }

    /** Whether to use codepaths that are compatible with scoped storage.
     */
    public static boolean useScopedStorage() {
@@ -3866,18 +3897,32 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen
        }
    }

    /**
     * if sdk>=R & not in full screen, means the DecorFitsSystemWindows=true, then we can ignore navigationGap.
     */
    public int getNavigationGap() {
        if (isFullScreen()) return 0;
        return (want_no_limits || edge_to_edge_mode) ? navigation_gap : 0;
    }

    public int getNavigationGapLandscape() {
        if (isFullScreen()) return 0;
        return edge_to_edge_mode ? navigation_gap_landscape : 0;
    }

    public int getNavigationGapReverseLandscape() {
        if (isFullScreen()) return 0;
        return edge_to_edge_mode ? navigation_gap_reverse_landscape : 0;
    }

    private boolean isFullScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !isInFullScreenMode()) {
            return true;
        }

        return false;
    }

    /** The system is now such that we have entered or exited immersive mode. If visible is true,
     *  system UI is now visible such that we should exit immersive mode. If visible is false, the
     *  system has entered immersive mode.
@@ -4248,6 +4293,18 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen
        }
    }

    /**
     * setDecorFitsSystemWindows for build >=R
     * @return if the operation successful or not
     */
    public boolean setDecorFitsSystemWindows(boolean decorFitsSystemWindows) {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            getWindow().setDecorFitsSystemWindows(decorFitsSystemWindows);
            return true;
        }
        return false;
    }

    /** Sets the brightness level for normal operation (when camera preview is visible).
     *  If force_max is true, this always forces maximum brightness; otherwise this depends on user preference.
     */
@@ -4433,6 +4490,7 @@ public class MainActivity extends AppCompatActivity implements PreferenceFragmen
        }

        setImmersiveMode(false);
        setDecorFitsSystemWindows(true);
        camera_in_background = true;

        // we disable location listening when showing settings or a dialog etc - saves battery life, also better for privacy
+24 −2
Original line number Diff line number Diff line
@@ -2,8 +2,8 @@ package net.sourceforge.opencamera.preview;

import net.sourceforge.opencamera.JavaImageFunctions;
import net.sourceforge.opencamera.JavaImageProcessing;
import net.sourceforge.opencamera.MainActivity;
import net.sourceforge.opencamera.cameracontroller.RawImage;
//import net.sourceforge.opencamera.MainActivity;
import net.sourceforge.opencamera.MyDebug;
import net.sourceforge.opencamera.R;
import net.sourceforge.opencamera.TakePhoto;
@@ -3979,6 +3979,21 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        return optimalSize;
    }

    private static CameraController.Size getClosestSizeForFullScreen(List<CameraController.Size> sizes, double targetRatio) {
        if (MyDebug.LOG)
            Log.d(TAG, "getClosestSize()");
        CameraController.Size optimalSize = null;
        double minDiff = Double.MIN_VALUE;
        for (CameraController.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if ((ratio - targetRatio) > minDiff) {
                optimalSize = size;
                minDiff = ratio - targetRatio;
            }
        }
        return (optimalSize != null) ? optimalSize : getClosestSize(sizes, targetRatio, null);
    }

    public CameraController.Size getOptimalPreviewSize(List<CameraController.Size> sizes) {
        if( MyDebug.LOG )
            Log.d(TAG, "getOptimalPreviewSize()");
@@ -4012,6 +4027,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                    Log.d(TAG, "swapped display_size to: " + display_size.x + " x " + display_size.y);
            }
        }
        ((MainActivity)getContext()).handleDecorFitsSystemWindows();
        double targetRatio = calculateTargetRatioForPreview(display_size);
        int targetHeight = Math.min(display_size.y, display_size.x);
        if( targetHeight <= 0 ) {
@@ -4041,8 +4057,12 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            // can't find match for aspect ratio, so find closest one
            if( MyDebug.LOG )
                Log.d(TAG, "no preview size matches the aspect ratio");
            if (((MainActivity)getContext()).isInFullScreenMode()) {
                optimalSize = getClosestSizeForFullScreen(sizes, targetRatio);
            } else {
                optimalSize = getClosestSize(sizes, targetRatio, null);
            }
        }
        if( MyDebug.LOG ) {
            Log.d(TAG, "chose optimalSize: " + optimalSize.width + " x " + optimalSize.height);
            Log.d(TAG, "optimalSize ratio: " + ((double)optimalSize.width / optimalSize.height));
@@ -4977,6 +4997,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        else {
            if( this.isOnTimer() ) {
                cancelTimer();
                ((MainActivity)getContext()).setDecorFitsSystemWindows(true);
                this.is_video = true;
            }
            else if( this.phase == PHASE_TAKING_PHOTO ) {
@@ -4985,6 +5006,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                    Log.d(TAG, "wait until photo taken");
            }
            else {
                ((MainActivity)getContext()).setDecorFitsSystemWindows(true);
                this.is_video = true;
            }
        }