From bf314ecf2267a0f655d47ebece1c4a451c1eacbc Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 30 May 2018 23:10:26 +0200 Subject: [PATCH] add a helper function for getting the camer amanager --- .../camera/views/PreviewCameraTwo.kt | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt index 61680e4a..b36da98f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraTwo.kt @@ -122,12 +122,11 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie private fun openCamera(width: Int, height: Int) { setupCameraOutputs(width, height) - val manager = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager try { if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) { throw RuntimeException("Time out waiting to lock camera opening.") } - manager.openCamera(mCameraId, cameraStateCallback, mBackgroundHandler) + getCameraManager().openCamera(mCameraId, cameraStateCallback, mBackgroundHandler) } catch (e: InterruptedException) { } catch (e: SecurityException) { } @@ -156,14 +155,13 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie } private fun getIsUsingFrontCamera(): Boolean { - val manager = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager - val characteristics = manager.getCameraCharacteristics(mCameraId) + val characteristics = getCameraManager().getCameraCharacteristics(mCameraId) val facing = characteristics.get(CameraCharacteristics.LENS_FACING) return facing == CameraCharacteristics.LENS_FACING_FRONT } private fun setupCameraOutputs(width: Int, height: Int) { - val manager = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager + val manager = getCameraManager() try { for (cameraId in manager.cameraIdList) { val characteristics = manager.getCameraCharacteristics(cameraId) @@ -381,8 +379,7 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie private fun focusArea() { mActivity.drawFocusCircle(mLastClickX, mLastClickY) - val manager = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager - val characteristics = manager.getCameraCharacteristics(mCameraId) + val characteristics = getCameraManager().getCameraCharacteristics(mCameraId) val sensorArraySize = characteristics.get(CameraCharacteristics.SENSOR_INFO_ACTIVE_ARRAY_SIZE) val captureCallbackHandler = object : CameraCaptureSession.CaptureCallback() { @@ -444,6 +441,8 @@ class PreviewCameraTwo : ViewGroup, TextureView.SurfaceTextureListener, MyPrevie } } + private fun getCameraManager() = mActivity.getSystemService(Context.CAMERA_SERVICE) as CameraManager + private fun takePicture() { lockFocus() }