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

Commit 46cfe8dd authored by frankpreel's avatar frankpreel
Browse files

Replace boolean is_video()

As now the switch button supports 3 mode
enum FunctionalMode {
            PHOTO,
            VIDEO,
            QRCODE
    };
parent 9f3a3f57
Loading
Loading
Loading
Loading
+66 −55
Original line number Diff line number Diff line
@@ -197,7 +197,15 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    private AsyncTask<Void, Void, CameraController> open_camera_task; // background task used for opening camera
    private CloseCameraTask close_camera_task; // background task used for closing camera
    private boolean has_permissions = true; // whether we have permissions necessary to operate the camera (camera, storage); assume true until we've been denied one of them
    private boolean is_video;

    public static enum FunctionalMode {
            PHOTO,
            VIDEO,
            QRCODE
    };
    private FunctionalMode functionalMode;
    private boolean is_video() {return functionalMode == FunctionalMode.VIDEO;};

    private volatile MediaRecorder video_recorder; // must be volatile for test project reading the state
    private volatile boolean video_start_time_set; // must be volatile for test project reading the state
    private long video_start_time; // system time when the video recording was started, or last resumed if it was paused
@@ -686,14 +694,14 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        if( MyDebug.LOG )
            Log.d(TAG, "handleSingleTouch");

        if( !this.is_video && this.isTakingPhotoOrOnTimer() ) {
        if( !this.is_video() && this.isTakingPhotoOrOnTimer() ) {
            // if video, okay to refocus when recording
            return true;
        }

        // note, we always try to force start the preview (in case is_preview_paused has become false)
        // except if recording video (firstly, the preview should be running; secondly, we don't want to reset the phase!)
        if (!this.is_video) {
        if (!this.is_video()) {
            startCameraPreview();
        }
        cancelAutoFocus();
@@ -1263,7 +1271,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                }
            }
            if (due_to_max_filesize || remaining_restart_video > 0) {
                if (is_video) {
                if (is_video()) {
                    String toast = null;
                    if (!due_to_max_filesize)
                        toast = remaining_restart_video + " " + getContext().getResources().getString(R.string.repeats_to_go);
@@ -1639,7 +1647,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        current_focus_index = -1;
        max_num_focus_areas = 0;
        applicationInterface.cameraInOperation(false, false);
        if (is_video)
        if (is_video())
            applicationInterface.cameraInOperation(false, true);
        if (!this.has_surface) {
            if (MyDebug.LOG) {
@@ -2025,7 +2033,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            saved_is_video = false;
        }
        // must switch video before setupCameraParameters(), and starting preview
        if (saved_is_video != this.is_video) {
        if (saved_is_video != this.is_video()) {
            if (MyDebug.LOG)
                Log.d(TAG, "switch video mode as not in correct mode");
            this.switchVideo(true, false);
@@ -2076,7 +2084,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu

        updateFlashForVideo();
        if (take_photo) {
            if (this.is_video) {
            if (this.is_video()) {
                if (MyDebug.LOG)
                    Log.d(TAG, "switch to video for take_photo widget");
                this.switchVideo(true, true);
@@ -2085,8 +2093,8 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu

        // must be done after switching to video mode (so is_video is set correctly)
        if (MyDebug.LOG)
            Log.d(TAG, "is_video?: " + is_video);
        if (this.is_video) {
            Log.d(TAG, "is_video?: " + is_video());
        if (this.is_video()) {
            CameraController.TonemapProfile tonemap_profile = CameraController.TonemapProfile.TONEMAPPROFILE_OFF;
            if (supports_tonemap_curve) {
                tonemap_profile = applicationInterface.getVideoTonemapProfile();
@@ -2105,7 +2113,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        // Setup for high speed - must be done after setupCameraParameters() and switching to video mode, but before setPreviewSize() and startCameraPreview().
        // In theory it shouldn't matter if we call setVideoHighSpeed(true) if is_video==false, as it should only have an effect
        // when recording video; but don't set high speed mode in photo mode just to be safe.
        camera_controller.setVideoHighSpeed(is_video && video_high_speed);
        camera_controller.setVideoHighSpeed(is_video() && video_high_speed);

        if (do_startup_focus && using_android_l && camera_controller.supportsAutoFocus()) {
            // need to switch flash off for autofocus - and for Android L, need to do this before starting preview (otherwise it won't work in time); for old camera API, need to do this after starting preview!
@@ -2615,10 +2623,10 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        {
            if (MyDebug.LOG) {
                Log.d(TAG, "set up video stabilization");
                Log.d(TAG, "is_video?: " + is_video);
                Log.d(TAG, "is_video?: " + is_video());
            }
            if (this.supports_video_stabilization) {
                boolean using_video_stabilization = is_video && applicationInterface.getVideoStabilizationPref();
                boolean using_video_stabilization = is_video() && applicationInterface.getVideoStabilizationPref();
                if (MyDebug.LOG)
                    Log.d(TAG, "using_video_stabilization?: " + using_video_stabilization);
                camera_controller.setVideoStabilization(using_video_stabilization);
@@ -3107,7 +3115,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "video_high_speed?: " + video_high_speed);
        }

        if (is_video && video_high_speed && supports_iso_range && is_manual_iso) {
        if (is_video() && video_high_speed && supports_iso_range && is_manual_iso) {
            if (MyDebug.LOG)
                Log.d(TAG, "manual mode not supported for video_high_speed");
            camera_controller.setManualISO(false, 0);
@@ -3257,7 +3265,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        }
        // first set picture size (for photo mode, must be done now so we can set the picture size from this; for video, doesn't really matter when we set it)
        CameraController.Size new_size;
        if (this.is_video) {
        if (this.is_video()) {
            // see comments for getOptimalVideoPictureSize()
            VideoProfile profile = getVideoProfile();
            if (MyDebug.LOG)
@@ -3770,8 +3778,8 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        String preview_size = applicationInterface.getPreviewSizePref();
        // should always use wysiwig for video mode, otherwise we get incorrect aspect ratio shown when recording video (at least on Galaxy Nexus, e.g., at 640x480)
        // also not using wysiwyg mode with video caused corruption on Samsung cameras (tested with Samsung S3, Android 4.3, front camera, infinity focus)
        if (preview_size.equals("preference_preview_size_wysiwyg") || this.is_video) {
            if (this.is_video) {
        if (preview_size.equals("preference_preview_size_wysiwyg") || this.is_video()) {
            if (this.is_video()) {
                if (MyDebug.LOG)
                    Log.d(TAG, "set preview aspect ratio from video size (wysiwyg)");
                VideoProfile profile = getVideoProfile();
@@ -3828,7 +3836,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        final double ASPECT_TOLERANCE = 0.05;
        if (sizes == null)
            return null;
        if (is_video && video_high_speed) {
        if (is_video() && video_high_speed) {
            VideoProfile profile = getVideoProfile();
            if (MyDebug.LOG)
                Log.d(TAG, "video size: " + profile.videoFrameWidth + " x " + profile.videoFrameHeight);
@@ -4648,7 +4656,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            // (important not to return here however - still want to call
            // camera_controller.clearPreviewFpsRange() to clear a previously set fps)
        }
        else if( this.is_video ) {
        else if( this.is_video() ) {
            // For Nexus 5 and Nexus 6, we need to set the preview fps using matchPreviewFpsToVideo to avoid problem of dark preview in low light, as described above.
            // When the video recording starts, the preview automatically adjusts, but still good to avoid too-dark preview before the user starts recording.
            // However I'm wary of changing the behaviour for all devices at the moment, since some devices can be
@@ -4714,33 +4722,33 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "camera not opened!");
            return;
        }
        if (!is_video && !supports_video) {
        if (!is_video() && !supports_video) {
            if (MyDebug.LOG)
                Log.d(TAG, "video not supported");
            return;
        }
        boolean old_is_video = is_video;
        if (this.is_video) {
        boolean old_is_video = is_video();
        if (this.is_video()) {
            if (video_recorder != null) {
                stopVideo(false);
            }
            this.is_video = false;
            this.functionalMode = FunctionalMode.PHOTO;
        } else {
            if (this.isOnTimer()) {
                cancelTimer();
                ((MainActivity)getContext()).setDecorFitsSystemWindows(true);
                this.is_video = true;
                this.functionalMode = FunctionalMode.VIDEO;
            } else if (this.phase == PHASE_TAKING_PHOTO) {
                // wait until photo taken
                if (MyDebug.LOG)
                    Log.d(TAG, "wait until photo taken");
            } else {
                ((MainActivity)getContext()).setDecorFitsSystemWindows(true);
                this.is_video = true;
                this.functionalMode = FunctionalMode.VIDEO;
            }
        }

        if (is_video != old_is_video) {
        if (is_video() != old_is_video) {
            setFocusPref(false); // first restore the saved focus for the new photo/video mode; don't do autofocus, as it'll be cancelled when restarting preview
			/*if( !is_video ) {
				// changing from video to photo mode
@@ -4749,7 +4757,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu

            if (change_user_pref) {
                // now save
                applicationInterface.setVideoPref(is_video);
                applicationInterface.setVideoPref(is_video());
            }
            if (!during_startup) {
                // if during startup, updateFlashForVideo() needs to always be explicitly called anyway
@@ -4776,7 +4784,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
				// changing from photo to video mode
				setFocusPref(false);
			}*/
            if (is_video) {
            if (is_video()) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && applicationInterface.getRecordAudioPref()) {
                    // check for audio permission now, rather than when user starts video recording
                    // we restrict the checks to Android 6 or later just in case, see note in LocationSupplier.setupLocationListener()
@@ -4804,7 +4812,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    private void setFocusPref(boolean auto_focus) {
        if (MyDebug.LOG)
            Log.d(TAG, "setFocusPref()");
        String focus_value = applicationInterface.getFocusPref(is_video);
        String focus_value = applicationInterface.getFocusPref(is_video());
        if (focus_value.length() > 0) {
            if (MyDebug.LOG)
                Log.d(TAG, "found existing focus_value: " + focus_value);
@@ -4818,7 +4826,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "found no existing focus_value");
            // here we set the default values for focus mode
            // note if updating default focus value for photo mode, also update MainActivityTest.setToDefault()
            if( !updateFocus(is_video ? "focus_mode_continuous_video" : "focus_mode_continuous_picture", true, true, auto_focus) ) {
            if( !updateFocus(is_video() ? "focus_mode_continuous_video" : "focus_mode_continuous_picture", true, true, auto_focus) ) {
                if( MyDebug.LOG )
                    Log.d(TAG, "continuous focus not supported, so fall back to first");
                updateFocus(0, true, true, auto_focus);
@@ -4839,12 +4847,12 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        if (MyDebug.LOG)
            Log.d(TAG, "updateFocusForVideo()");
        String old_focus_mode = null;
        if (this.supported_focus_values != null && camera_controller != null && is_video) {
        if (this.supported_focus_values != null && camera_controller != null && is_video()) {
            boolean focus_is_video = focusIsVideo();
            if (MyDebug.LOG) {
                Log.d(TAG, "focus_is_video: " + focus_is_video + " , is_video: " + is_video);
                Log.d(TAG, "focus_is_video: " + focus_is_video + " , is_video: " + is_video());
            }
            if (focus_is_video != is_video) {
            if (focus_is_video != is_video()) {
                if (MyDebug.LOG)
                    Log.d(TAG, "need to change focus mode");
                old_focus_mode = this.getCurrentFocusValue();
@@ -4862,7 +4870,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    private void updateFlashForVideo() {
        if (MyDebug.LOG)
            Log.d(TAG, "updateFlashForVideo()");
        if (is_video) {
        if (is_video()) {
            // check flash is not auto or on
            String current_flash = getCurrentFlashValue();
            if (current_flash != null && !isFlashSupportedForVideo(current_flash)) {
@@ -4926,7 +4934,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    public void updateFlash(String flash_value) {
        if (MyDebug.LOG)
            Log.d(TAG, "updateFlash(): " + flash_value);
        if (this.phase == PHASE_TAKING_PHOTO && !is_video) {
        if (this.phase == PHASE_TAKING_PHOTO && !is_video()) {
            // just to be safe - risk of cancelling the autofocus before taking a photo, or otherwise messing things up
            if (MyDebug.LOG)
                Log.d(TAG, "currently taking a photo");
@@ -4967,7 +4975,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                    // don't bother setting done to false as we shouldn't have two torches in a row...
                }

                if (is_video) {
                if (is_video()) {
                    // check supported for video
                    String new_flash_value = supported_flash_values.get(new_flash_index);
                    if (!isFlashSupportedForVideo(new_flash_value)) {
@@ -5137,7 +5145,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu

            if (save) {
                // now save
                applicationInterface.setFocusPref(focus_value, is_video);
                applicationInterface.setFocusPref(focus_value, is_video());
            }
        }
    }
@@ -5187,7 +5195,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        String focus_value = current_focus_index != -1 ? supported_focus_values.get(current_focus_index) : null;
        if (MyDebug.LOG)
            Log.d(TAG, "focus_value is " + focus_value);
        if (camera_controller != null && focus_value != null && focus_value.equals("focus_mode_continuous_picture") && !this.is_video) {
        if (camera_controller != null && focus_value != null && focus_value.equals("focus_mode_continuous_picture") && !this.is_video()) {
            if (MyDebug.LOG)
                Log.d(TAG, "set continuous picture focus move callback");
            camera_controller.setContinuousFocusMoveCallback(new CameraController.ContinuousFocusMoveCallback() {
@@ -5272,7 +5280,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            this.phase = PHASE_NORMAL;
            return;
        }
        if (is_video && continuous_fast_burst) {
        if (is_video() && continuous_fast_burst) {
            Log.e(TAG, "continuous_fast_burst not supported for video mode");
            this.phase = PHASE_NORMAL;
            return;
@@ -5284,7 +5292,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        }
        //if( !photo_snapshot && this.phase == PHASE_TAKING_PHOTO ) {
        //if( (is_video && is_video_recording && !photo_snapshot) || this.phase == PHASE_TAKING_PHOTO ) {
        if (is_video && isVideoRecording() && !photo_snapshot) {
        if (is_video() && isVideoRecording() && !photo_snapshot) {
            // user requested stop video
            if (!video_start_time_set || System.currentTimeMillis() - video_start_time < 500) {
                // if user presses to stop too quickly, we ignore
@@ -5296,7 +5304,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                stopVideo(false);
            }
            return;
        } else if ((!is_video || photo_snapshot) && this.phase == PHASE_TAKING_PHOTO) {
        } else if ((!is_video() || photo_snapshot) && this.phase == PHASE_TAKING_PHOTO) {
            // user requested take photo while already taking photo
            if (MyDebug.LOG)
                Log.d(TAG, "already taking a photo");
@@ -5304,14 +5312,14 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                cancelRepeat();
                showToast(take_photo_toast, R.string.cancelled_repeat_mode, true);
            }
            else if( !is_video && camera_controller.getBurstType() == CameraController.BurstType.BURSTTYPE_FOCUS && camera_controller.isCapturingBurst() ) {
            else if( !is_video() && camera_controller.getBurstType() == CameraController.BurstType.BURSTTYPE_FOCUS && camera_controller.isCapturingBurst() ) {
                camera_controller.stopFocusBracketingBurst();
                showToast(take_photo_toast, R.string.cancelled_focus_bracketing, true);
            }
            return;
        }

        if (!is_video || photo_snapshot) {
        if (!is_video() || photo_snapshot) {
            // check it's okay to take a photo
            if (!applicationInterface.canTakeNewPhoto()) {
                if (MyDebug.LOG)
@@ -5584,7 +5592,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        if (MyDebug.LOG)
            Log.d(TAG, "takePicture");
        //this.thumbnail_anim = false;
        if (!is_video || photo_snapshot)
        if (!is_video() || photo_snapshot)
            this.phase = PHASE_TAKING_PHOTO;
        else {
            if (phase == PHASE_TIMER)
@@ -5599,7 +5607,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "camera not opened!");
            this.phase = PHASE_NORMAL;
            applicationInterface.cameraInOperation(false, false);
            if (is_video)
            if (is_video())
                applicationInterface.cameraInOperation(false, true);
            return;
        }
@@ -5608,7 +5616,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "preview surface not yet available");
            this.phase = PHASE_NORMAL;
            applicationInterface.cameraInOperation(false, false);
            if (is_video)
            if (is_video())
                applicationInterface.cameraInOperation(false, true);
            return;
        }
@@ -5623,17 +5631,17 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                    if (MyDebug.LOG)
                        Log.d(TAG, "location data required, but not available");
                    showToast(null, R.string.location_not_available, true);
                    if( !is_video || photo_snapshot )
                    if( !is_video() || photo_snapshot )
                        this.phase = PHASE_NORMAL;
                    applicationInterface.cameraInOperation(false, false);
                    if (is_video)
                    if (is_video())
                        applicationInterface.cameraInOperation(false, true);
                    return;
                }
            }
        }

        if (is_video && !photo_snapshot) {
        if (is_video() && !photo_snapshot) {
            if (MyDebug.LOG)
                Log.d(TAG, "start video recording");
            startVideoRecording(max_filesize_restart);
@@ -6465,7 +6473,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
            camera_controller.setRotation(getImageVideoRotation());

            boolean enable_sound = applicationInterface.getShutterSoundPref();
            if (is_video && isVideoRecording())
            if (is_video() && isVideoRecording())
                enable_sound = false; // always disable shutter sound if we're taking a photo while recording video
            if (MyDebug.LOG)
                Log.d(TAG, "enable_sound? " + enable_sound);
@@ -6564,7 +6572,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        } else if (!this.is_preview_started) {
            if (MyDebug.LOG)
                Log.d(TAG, "preview not yet started");
        } else if (!(manual && this.is_video) && (this.isVideoRecording() || this.isTakingPhotoOrOnTimer())) {
        } else if (!(manual && this.is_video()) && (this.isVideoRecording() || this.isTakingPhotoOrOnTimer())) {
            // if taking a video, we allow manual autofocuses
            // autofocus may cause problem if there is a video corruption problem, see testTakeVideoBitrate() on Nexus 7 at 30Mbs or 50Mbs, where the startup autofocus would cause a problem here
            if (MyDebug.LOG)
@@ -6574,7 +6582,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                // remove any previous request to switch back to continuous
                removePendingContinuousFocusReset();
            }
            if (manual && !is_video && camera_controller.focusIsContinuous() && supportedFocusValue("focus_mode_auto")) {
            if (manual && !is_video() && camera_controller.focusIsContinuous() && supportedFocusValue("focus_mode_auto")) {
                if (MyDebug.LOG)
                    Log.d(TAG, "switch from continuous to autofocus mode for touch focus");
                camera_controller.setFocusValue("focus_mode_auto"); // switch to autofocus
@@ -6755,8 +6763,8 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
                Log.d(TAG, "starting the camera preview");
            {
                if (MyDebug.LOG)
                    Log.d(TAG, "setRecordingHint: " + is_video);
                camera_controller.setRecordingHint(this.is_video);
                    Log.d(TAG, "setRecordingHint: " + is_video());
                camera_controller.setRecordingHint(this.is_video());
            }
            setPreviewFps();
            try {
@@ -7057,7 +7065,7 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
    public boolean isVideoHighSpeed() {
        if (MyDebug.LOG)
            Log.d(TAG, "isVideoHighSpeed");
        return is_video && video_high_speed;
        return is_video() && video_high_speed;
    }

    public boolean canDisableShutterSound() {
@@ -8583,8 +8591,11 @@ public class Preview implements SurfaceHolder.Callback, TextureView.SurfaceTextu
        }
    }

    public boolean isQRCode() {
        return functionalMode == FunctionalMode.QRCODE;
    }
    public boolean isVideo() {
        return is_video;
        return functionalMode == FunctionalMode.VIDEO;
    }

    public boolean isVideoRecording() {