From 2cdae95de88bdabf69686f623483577fddef89f7 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 27 May 2018 23:35:40 +0200 Subject: [PATCH] create a helper function for getting the proper camera impl --- .../camera/activities/MainActivity.kt | 5 +- .../camera/dialogs/ChangeResolutionDialog.kt | 3 +- .../camera/extensions/Activity.kt | 2 +- .../camera/extensions/Context.kt | 14 ++++++ .../camera/extensions/Int.kt | 12 ----- .../camera/helpers/Config.kt | 4 +- .../camera/helpers/Constants.kt | 48 +++++++++---------- .../camera/helpers/PhotoProcessor.kt | 10 ++-- .../camera/views/PreviewCameraOne.kt | 2 +- 9 files changed, 49 insertions(+), 51 deletions(-) delete mode 100644 app/src/main/kotlin/com/simplemobiletools/camera/extensions/Int.kt diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt index 4191c478..50071405 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt @@ -16,10 +16,9 @@ import com.bumptech.glide.request.RequestOptions import com.simplemobiletools.camera.BuildConfig import com.simplemobiletools.camera.R import com.simplemobiletools.camera.extensions.config +import com.simplemobiletools.camera.extensions.getMyCamera import com.simplemobiletools.camera.extensions.navBarHeight import com.simplemobiletools.camera.helpers.* -import com.simplemobiletools.camera.implementations.MyCameraOneImpl -import com.simplemobiletools.camera.implementations.MyCameraTwoImpl import com.simplemobiletools.camera.interfaces.MyCamera import com.simplemobiletools.camera.views.FocusCircleView import com.simplemobiletools.camera.views.PreviewCameraOne @@ -121,7 +120,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave mCurrVideoRecTimer = 0 mCurrCameraId = 0 mLastHandledOrientation = 0 - mCameraImpl = if (isLollipopPlus()) MyCameraTwoImpl(applicationContext) else MyCameraOneImpl(applicationContext) + mCameraImpl = getMyCamera() if (config.alwaysOpenBackCamera) { config.lastUsedCamera = mCameraImpl.getBackCameraId() diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt index 8b1d4a51..60270e35 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/dialogs/ChangeResolutionDialog.kt @@ -8,6 +8,7 @@ import com.simplemobiletools.camera.R import com.simplemobiletools.camera.activities.SimpleActivity import com.simplemobiletools.camera.extensions.config import com.simplemobiletools.camera.extensions.getAspectRatio +import com.simplemobiletools.camera.extensions.getMyCamera import com.simplemobiletools.camera.helpers.Config import com.simplemobiletools.commons.dialogs.RadioGroupDialog import com.simplemobiletools.commons.extensions.setupDialogStuff @@ -16,7 +17,7 @@ import kotlinx.android.synthetic.main.dialog_change_resolution.view.* class ChangeResolutionDialog(val activity: SimpleActivity, val config: Config, val camera: Camera, val callback: () -> Unit) { var dialog: AlertDialog - private val isBackCamera = activity.config.lastUsedCamera == Camera.CameraInfo.CAMERA_FACING_BACK + private val isBackCamera = activity.config.lastUsedCamera == activity.getMyCamera().getBackCameraId() init { val view = LayoutInflater.from(activity).inflate(R.layout.dialog_change_resolution, null).apply { diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Activity.kt index 9a0d442f..0d853493 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Activity.kt @@ -9,7 +9,7 @@ fun Activity.getPreviewRotation(cameraId: Int): Int { val degrees = getDeviceRotationDegrees() var result: Int - if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { + if (info.facing == getMyCamera().getFrontCameraId()) { result = (info.orientation + degrees) % 360 result = 360 - result } else { diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt index fd5b027e..8a4f40bd 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Context.kt @@ -4,6 +4,11 @@ import android.content.Context import android.graphics.Point import android.view.WindowManager import com.simplemobiletools.camera.helpers.Config +import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_LEFT +import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_RIGHT +import com.simplemobiletools.camera.implementations.MyCameraOneImpl +import com.simplemobiletools.camera.implementations.MyCameraTwoImpl +import com.simplemobiletools.commons.helpers.isLollipopPlus import java.io.File import java.text.SimpleDateFormat import java.util.* @@ -44,3 +49,12 @@ val Context.realScreenSize: Point } val Context.navBarHeight: Int get() = realScreenSize.y - usableScreenSize.y + +fun Context.getMyCamera() = if (isLollipopPlus()) MyCameraTwoImpl(applicationContext) else MyCameraOneImpl(applicationContext) + +fun Context.compensateDeviceRotation(orientation: Int, currCameraId: Int) = when { + orientation == ORIENT_LANDSCAPE_LEFT -> 270 + orientation == ORIENT_LANDSCAPE_RIGHT -> 90 + currCameraId == getMyCamera().getFrontCameraId() -> 180 + else -> 0 +} diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Int.kt b/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Int.kt deleted file mode 100644 index 84f9edc3..00000000 --- a/app/src/main/kotlin/com/simplemobiletools/camera/extensions/Int.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.simplemobiletools.camera.extensions - -import android.hardware.Camera -import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_LEFT -import com.simplemobiletools.camera.helpers.ORIENT_LANDSCAPE_RIGHT - -fun Int.compensateDeviceRotation(currCameraId: Int) = when { - this == ORIENT_LANDSCAPE_LEFT -> 270 - this == ORIENT_LANDSCAPE_RIGHT -> 90 - currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT -> 180 - else -> 0 -} diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Config.kt index 21438cae..8d6bfc7b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Config.kt @@ -1,8 +1,8 @@ package com.simplemobiletools.camera.helpers import android.content.Context -import android.hardware.Camera import android.os.Environment +import com.simplemobiletools.camera.extensions.getMyCamera import com.simplemobiletools.commons.helpers.BaseConfig import java.io.File @@ -47,7 +47,7 @@ class Config(context: Context) : BaseConfig(context) { set(flipPhotos) = prefs.edit().putBoolean(FLIP_PHOTOS, flipPhotos).apply() var lastUsedCamera: Int - get() = prefs.getInt(LAST_USED_CAMERA, Camera.CameraInfo.CAMERA_FACING_BACK) + get() = prefs.getInt(LAST_USED_CAMERA, context.getMyCamera().getBackCameraId()) set(cameraId) = prefs.edit().putInt(LAST_USED_CAMERA, cameraId).apply() var flashlightState: Int diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt index 5d75310f..43ad2cee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/Constants.kt @@ -1,29 +1,29 @@ package com.simplemobiletools.camera.helpers -val ORIENT_PORTRAIT = 0 -val ORIENT_LANDSCAPE_LEFT = 1 -val ORIENT_LANDSCAPE_RIGHT = 2 +const val ORIENT_PORTRAIT = 0 +const val ORIENT_LANDSCAPE_LEFT = 1 +const val ORIENT_LANDSCAPE_RIGHT = 2 // shared preferences -val SAVE_PHOTOS = "save_photos" -val SHOW_PREVIEW = "show_preview" -val SOUND = "sound" -val FOCUS_BEFORE_CAPTURE = "focus_before_capture" -val VOLUME_BUTTONS_AS_SHUTTER = "volume_buttons_as_shutter" -val TURN_FLASH_OFF_AT_STARTUP = "turn_flash_off_at_startup" -val FLIP_PHOTOS = "flip_photos" -val LAST_USED_CAMERA = "last_used_camera" -val FLASHLIGHT_STATE = "flashlight_state" -val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index" -val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index" -val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index" -val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index" -val PHOTO_PREVIEW_HINT_SHOWN = "photo_preview_hint_shown" -val KEEP_SETTINGS_VISIBLE = "keep_settings_visible" -val ALWAYS_OPEN_BACK_CAMERA = "always_open_back_camera" -val SAVE_PHOTO_METADATA = "save_photo_metadata" -val PHOTO_QUALITY = "photo_quality" +const val SAVE_PHOTOS = "save_photos" +const val SHOW_PREVIEW = "show_preview" +const val SOUND = "sound" +const val FOCUS_BEFORE_CAPTURE = "focus_before_capture" +const val VOLUME_BUTTONS_AS_SHUTTER = "volume_buttons_as_shutter" +const val TURN_FLASH_OFF_AT_STARTUP = "turn_flash_off_at_startup" +const val FLIP_PHOTOS = "flip_photos" +const val LAST_USED_CAMERA = "last_used_camera" +const val FLASHLIGHT_STATE = "flashlight_state" +const val BACK_PHOTO_RESOLUTION_INDEX = "back_photo_resolution_index" +const val BACK_VIDEO_RESOLUTION_INDEX = "back_video_resolution_index" +const val FRONT_PHOTO_RESOLUTION_INDEX = "front_photo_resolution_index" +const val FRONT_VIDEO_RESOLUTION_INDEX = "front_video_resolution_index" +const val PHOTO_PREVIEW_HINT_SHOWN = "photo_preview_hint_shown" +const val KEEP_SETTINGS_VISIBLE = "keep_settings_visible" +const val ALWAYS_OPEN_BACK_CAMERA = "always_open_back_camera" +const val SAVE_PHOTO_METADATA = "save_photo_metadata" +const val PHOTO_QUALITY = "photo_quality" -val FLASH_OFF = 0 -val FLASH_ON = 1 -val FLASH_AUTO = 2 +const val FLASH_OFF = 0 +const val FLASH_ON = 1 +const val FLASH_AUTO = 2 diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/PhotoProcessor.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/PhotoProcessor.kt index 13f61f56..a0d5b2dc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/PhotoProcessor.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/PhotoProcessor.kt @@ -3,17 +3,13 @@ package com.simplemobiletools.camera.helpers import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Matrix -import android.hardware.Camera import android.media.ExifInterface import android.net.Uri import android.os.AsyncTask import android.os.Environment import com.simplemobiletools.camera.R import com.simplemobiletools.camera.activities.MainActivity -import com.simplemobiletools.camera.extensions.compensateDeviceRotation -import com.simplemobiletools.camera.extensions.config -import com.simplemobiletools.camera.extensions.getOutputMediaFile -import com.simplemobiletools.camera.extensions.getPreviewRotation +import com.simplemobiletools.camera.extensions.* import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.isNougatPlus import java.io.File @@ -72,7 +68,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId var image = BitmapFactory.decodeByteArray(data, 0, data.size) val exif = ExifInterface(photoFile.toString()) - val deviceRot = deviceOrientation.compensateDeviceRotation(currCameraId) + val deviceRot = activity.compensateDeviceRotation(deviceOrientation, currCameraId) val previewRot = activity.getPreviewRotation(currCameraId) val orient = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED) val imageRot = orient.degreesFromOrientation() @@ -82,7 +78,7 @@ class PhotoProcessor(val activity: MainActivity, val uri: Uri?, val currCameraId image = rotate(image, totalRotation) } - if (currCameraId == Camera.CameraInfo.CAMERA_FACING_FRONT && !activity.config.flipPhotos) { + if (currCameraId == activity.getMyCamera().getFrontCameraId() && !activity.config.flipPhotos) { val matrix = Matrix() if (path.startsWith(activity.internalStoragePath)) { matrix.preScale(1f, -1f) diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt index bd9c38bf..e8db984b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/views/PreviewCameraOne.kt @@ -735,7 +735,7 @@ class PreviewCameraOne : ViewGroup, SurfaceHolder.Callback, MediaScannerConnecti } private fun getVideoRotation(): Int { - val deviceRot = mActivity!!.mLastHandledOrientation.compensateDeviceRotation(mCurrCameraId) + val deviceRot = mActivity!!.compensateDeviceRotation(mActivity!!.mLastHandledOrientation, mCurrCameraId) val previewRot = mActivity!!.getPreviewRotation(mCurrCameraId) return (deviceRot + previewRot) % 360 }