Set brightness to 66% when taking a selfie.

This commit is contained in:
Alex Hart 2022-11-16 16:51:54 -04:00
parent 586339575f
commit 3469e8d0e0
4 changed files with 49 additions and 15 deletions

View file

@ -120,6 +120,10 @@ class Camera1Controller {
});
}
boolean isCameraFacingFront() {
return cameraId == Camera.CameraInfo.CAMERA_FACING_FRONT;
}
int flip() {
Log.d(TAG, "flip()");
SurfaceTexture surfaceTexture = previewSurface;

View file

@ -11,6 +11,7 @@ import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.Display;
import android.view.GestureDetector;
@ -123,7 +124,7 @@ public class Camera1Fragment extends LoggingFragment implements CameraFragment,
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
cameraScreenBrightnessController = new CameraScreenBrightnessController(requireActivity().getWindow());
cameraScreenBrightnessController = new CameraScreenBrightnessController(requireActivity().getWindow(), () -> camera.isCameraFacingFront());
getViewLifecycleOwner().getLifecycle().addObserver(cameraScreenBrightnessController);
rotationListener = new RotationListener(requireContext());
@ -336,6 +337,7 @@ public class Camera1Fragment extends LoggingFragment implements CameraFragment,
animation.setDuration(200);
animation.setInterpolator(new DecelerateInterpolator());
flipButton.startAnimation(animation);
cameraScreenBrightnessController.onCameraDirectionChanged(newCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT);
});
} else {
flipButton.setVisibility(View.GONE);

View file

@ -6,30 +6,53 @@ import androidx.lifecycle.LifecycleOwner
/**
* Modifies screen brightness to increase to a max of 66% if lower than that for optimal picture
* taking conditions.
* taking conditions. This brightness is only applied when the front-facing camera is selected.
*/
class CameraScreenBrightnessController(private val window: Window) : DefaultLifecycleObserver {
class CameraScreenBrightnessController(private val window: Window, private val cameraDirectionProvider: CameraDirectionProvider) : DefaultLifecycleObserver {
companion object {
private const val MIN_CAMERA_BRIGHTNESS = 0.66f
private const val FRONT_CAMERA_BRIGHTNESS = 0.66f
}
private var originalBrightness: Float = 0f
private val originalBrightness: Float by lazy { window.attributes.screenBrightness }
override fun onResume(owner: LifecycleOwner) {
val originalBrightness = window.attributes.screenBrightness
if (originalBrightness < MIN_CAMERA_BRIGHTNESS) {
onCameraDirectionChanged(cameraDirectionProvider.isFrontFacingCameraSelected())
}
override fun onPause(owner: LifecycleOwner) {
disableBrightness()
}
/**
* Because setting camera direction is an asynchronous action, we cannot rely on
* the `CameraDirectionProvider` at this point.
*/
fun onCameraDirectionChanged(isFrontFacing: Boolean) {
if (isFrontFacing) {
enableBrightness()
} else {
disableBrightness()
}
}
private fun enableBrightness() {
if (originalBrightness < FRONT_CAMERA_BRIGHTNESS) {
window.attributes = window.attributes.apply {
screenBrightness = MIN_CAMERA_BRIGHTNESS
screenBrightness = FRONT_CAMERA_BRIGHTNESS
}
}
}
override fun onPause(owner: LifecycleOwner) {
if (originalBrightness > 0f && window.attributes.screenBrightness == MIN_CAMERA_BRIGHTNESS) {
private fun disableBrightness() {
if (window.attributes.screenBrightness == FRONT_CAMERA_BRIGHTNESS) {
window.attributes = window.attributes.apply {
screenBrightness = originalBrightness
}
}
}
interface CameraDirectionProvider {
fun isFrontFacingCameraSelected(): Boolean
}
}

View file

@ -136,8 +136,10 @@ public class CameraXFragment extends LoggingFragment implements CameraFragment {
@SuppressLint("MissingPermission")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
cameraScreenBrightnessController = new CameraScreenBrightnessController(requireActivity().getWindow());
getViewLifecycleOwner().getLifecycle().addObserver(cameraScreenBrightnessController);
cameraScreenBrightnessController = new CameraScreenBrightnessController(
requireActivity().getWindow(),
() -> cameraController.getCameraSelector() == CameraSelector.DEFAULT_FRONT_CAMERA
);
ViewGroup cameraParent = view.findViewById(R.id.camerax_camera_parent);
@ -508,12 +510,14 @@ public class CameraXFragment extends LoggingFragment implements CameraFragment {
return;
}
getViewLifecycleOwner().getLifecycle().addObserver(cameraScreenBrightnessController);
if (cameraController.hasCamera(CameraSelector.DEFAULT_FRONT_CAMERA) && cameraController.hasCamera(CameraSelector.DEFAULT_BACK_CAMERA)) {
flipButton.setVisibility(View.VISIBLE);
flipButton.setOnClickListener(v -> {
cameraController.setCameraSelector(cameraController.getCameraSelector() == CameraSelector.DEFAULT_FRONT_CAMERA
? CameraSelector.DEFAULT_BACK_CAMERA
: CameraSelector.DEFAULT_FRONT_CAMERA);
CameraSelector cameraSelector = cameraController.getCameraSelector() == CameraSelector.DEFAULT_FRONT_CAMERA
? CameraSelector.DEFAULT_BACK_CAMERA
: CameraSelector.DEFAULT_FRONT_CAMERA;
cameraController.setCameraSelector(cameraSelector);
TextSecurePreferences.setDirectCaptureCameraId(getContext(), CameraXUtil.toCameraDirectionInt(cameraController.getCameraSelector()));
Animation animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
@ -522,6 +526,7 @@ public class CameraXFragment extends LoggingFragment implements CameraFragment {
flipButton.startAnimation(animation);
flashButton.setAutoFlashEnabled(cameraController.getImageCaptureFlashMode() >= ImageCapture.FLASH_MODE_AUTO);
flashButton.setFlash(cameraController.getImageCaptureFlashMode());
cameraScreenBrightnessController.onCameraDirectionChanged(cameraSelector == CameraSelector.DEFAULT_FRONT_CAMERA);
});
GestureDetector gestureDetector = new GestureDetector(requireContext(), new GestureDetector.SimpleOnGestureListener() {