Skip to content

Commit

Permalink
Only brighten screen when flash is ON and camera is FRONT.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-signal committed Dec 12, 2022
1 parent 3cc556d commit a6a1850
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
requireActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
cameraScreenBrightnessController = new CameraScreenBrightnessController(requireActivity().getWindow(), () -> camera.isCameraFacingFront());
cameraScreenBrightnessController = new CameraScreenBrightnessController(requireActivity().getWindow(), new CameraStateProvider(camera));
getViewLifecycleOwner().getLifecycle().addObserver(cameraScreenBrightnessController);

rotationListener = new RotationListener(requireContext());
Expand Down Expand Up @@ -477,4 +477,23 @@ public boolean onDoubleTap(MotionEvent e) {
private enum Stage {
SURFACE_AVAILABLE, CAMERA_PROPERTIES_AVAILABLE
}

private static class CameraStateProvider implements CameraScreenBrightnessController.CameraStateProvider {

private final Camera1Controller camera1Controller;

private CameraStateProvider(Camera1Controller camera1Controller) {
this.camera1Controller = camera1Controller;
}

@Override
public boolean isFrontFacingCameraSelected() {
return camera1Controller.isCameraFacingFront();
}

@Override
public boolean isFlashEnabled() {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import androidx.lifecycle.LifecycleOwner
* Modifies screen brightness to increase to a max of 66% if lower than that for optimal picture
* taking conditions. This brightness is only applied when the front-facing camera is selected.
*/
class CameraScreenBrightnessController(private val window: Window, private val cameraDirectionProvider: CameraDirectionProvider) : DefaultLifecycleObserver {
class CameraScreenBrightnessController(
private val window: Window,
private val cameraStateProvider: CameraStateProvider
) : DefaultLifecycleObserver {

companion object {
private const val FRONT_CAMERA_BRIGHTNESS = 0.66f
Expand All @@ -17,7 +20,8 @@ class CameraScreenBrightnessController(private val window: Window, private val c
private val originalBrightness: Float by lazy { window.attributes.screenBrightness }

override fun onResume(owner: LifecycleOwner) {
onCameraDirectionChanged(cameraDirectionProvider.isFrontFacingCameraSelected())
onCameraDirectionChanged(cameraStateProvider.isFrontFacingCameraSelected())
onCameraFlashChanged(cameraStateProvider.isFlashEnabled())
}

override fun onPause(owner: LifecycleOwner) {
Expand All @@ -29,7 +33,15 @@ class CameraScreenBrightnessController(private val window: Window, private val c
* the `CameraDirectionProvider` at this point.
*/
fun onCameraDirectionChanged(isFrontFacing: Boolean) {
if (isFrontFacing) {
if (isFrontFacing && cameraStateProvider.isFlashEnabled()) {
enableBrightness()
} else {
disableBrightness()
}
}

fun onCameraFlashChanged(isFlashEnabled: Boolean) {
if (isFlashEnabled && cameraStateProvider.isFrontFacingCameraSelected()) {
enableBrightness()
} else {
disableBrightness()
Expand All @@ -52,7 +64,8 @@ class CameraScreenBrightnessController(private val window: Window, private val c
}
}

interface CameraDirectionProvider {
interface CameraStateProvider {
fun isFrontFacingCameraSelected(): Boolean
fun isFlashEnabled(): Boolean
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ public void onAttach(@NonNull Context context) {
@SuppressLint("MissingPermission")
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
cameraScreenBrightnessController = new CameraScreenBrightnessController(
requireActivity().getWindow(),
() -> cameraController.getCameraSelector() == CameraSelector.DEFAULT_FRONT_CAMERA
);

ViewGroup cameraParent = view.findViewById(R.id.camerax_camera_parent);

this.previewView = view.findViewById(R.id.camerax_camera);
Expand All @@ -156,6 +151,11 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
cameraController.setImageCaptureMode(CameraXUtil.getOptimalCaptureMode());
cameraXModePolicy.initialize(cameraController);

cameraScreenBrightnessController = new CameraScreenBrightnessController(
requireActivity().getWindow(),
new CameraStateProvider(cameraController)
);

previewView.setScaleType(PREVIEW_SCALE_TYPE);
previewView.setController(cameraController);

Expand Down Expand Up @@ -344,7 +344,10 @@ private void initControls() {

flashButton.setAutoFlashEnabled(cameraController.getImageCaptureFlashMode() >= ImageCapture.FLASH_MODE_AUTO);
flashButton.setFlash(cameraController.getImageCaptureFlashMode());
flashButton.setOnFlashModeChangedListener(cameraController::setImageCaptureFlashMode);
flashButton.setOnFlashModeChangedListener(mode -> {
cameraController.setImageCaptureFlashMode(mode);
cameraScreenBrightnessController.onCameraFlashChanged(mode == ImageCapture.FLASH_MODE_ON);
});

galleryButton.setOnClickListener(v -> controller.onGalleryClicked());
countButton.setOnClickListener(v -> controller.onCameraCountButtonClicked());
Expand Down Expand Up @@ -556,4 +559,23 @@ public boolean onDoubleTap(MotionEvent e) {
flipButton.setVisibility(View.GONE);
}
}

private static class CameraStateProvider implements CameraScreenBrightnessController.CameraStateProvider {

private final CameraController cameraController;

private CameraStateProvider(CameraController cameraController) {
this.cameraController = cameraController;
}

@Override
public boolean isFrontFacingCameraSelected() {
return cameraController.getCameraSelector() == CameraSelector.DEFAULT_FRONT_CAMERA;
}

@Override
public boolean isFlashEnabled() {
return cameraController.getImageCaptureFlashMode() == ImageCapture.FLASH_MODE_ON;
}
}
}

0 comments on commit a6a1850

Please sign in to comment.