akshay2211 / PixImagePicker

Pix is a Whatsapp image picker replica. with this, you can integrate an image picker just like WhatsApp.
Apache License 2.0
860 stars 217 forks source link

Handle thrown illegalStateException when cams are unavailable in CameraXManager #206

Closed 73k05 closed 2 years ago

73k05 commented 3 years ago

Hi,

when initialising camera hardware and binding, we check if camera are available and the throw exception if not, we can observe it in the following peace of code:

lensFacing = when {
            hasBackCamera() and !options.isFrontFacing -> CameraSelector.LENS_FACING_BACK
            hasFrontCamera() and options.isFrontFacing -> CameraSelector.LENS_FACING_FRONT
            else -> throw IllegalStateException("Back and front camera are unavailable")
        }

The main issue with this code is that this case actually happened on a moto g power (android 11) and the exception is thrown which is well anticipated code. But the exception is not handled and not delegated causing the application to crash. Here is what I propose in the PR:

  1. Delegate exception to fragment
@Throws(IllegalStateException::class)
    fun bindCameraUseCases(binding: FragmentPixBinding) {
  1. Handle exception in fragment by backpressing and return to the previous view
try {
            setUpCamera(binding)
            binding.gridLayout.controlsLayout.flashButton.show()
            binding.setDrawableIconForFlash(options)
        }catch (e:IllegalStateException){
            resultCallback?.invoke(PixEventCallback.Results())
            PixBus.returnObjects(
                event = PixEventCallback.Results(
                    ArrayList(),
                    PixEventCallback.Status.BACK_PRESSED
                )
            )
        }

Also work for another thrown in the same function.

Regards 73k05

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.