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 a84a5e9f..bfcdeb3d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/activities/MainActivity.kt @@ -895,9 +895,10 @@ class MainActivity : SimpleActivity(), PhotoProcessor.MediaSavedListener, Camera private fun resetViewsOnTimerFinish() { arrayOf(top_options, toggle_camera, last_photo_video_preview, camera_mode_holder).forEach { - it.fadeIn() - it.beVisible() + it?.fadeIn() + it?.beVisible() } + timer_text.beGone() shutter.setImageState(intArrayOf(-R.attr.state_timer_cancel), true) } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt index 3792d261..accff2ca 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/ImageQualityManager.kt @@ -11,10 +11,7 @@ import com.simplemobiletools.camera.models.CameraSelectorImageQualities import com.simplemobiletools.camera.models.MySize import com.simplemobiletools.commons.extensions.showErrorToast -class ImageQualityManager( - private val activity: AppCompatActivity, -) { - +class ImageQualityManager(private val activity: AppCompatActivity) { companion object { private val CAMERA_LENS = arrayOf(CameraCharacteristics.LENS_FACING_FRONT, CameraCharacteristics.LENS_FACING_BACK) } @@ -59,7 +56,7 @@ class ImageQualityManager( } fun getSupportedResolutions(cameraSelector: CameraSelector): List { - val fullScreenSize = getFullScreenResolution(cameraSelector) + val fullScreenSize = getFullScreenResolution(cameraSelector) ?: return ArrayList() return listOf(fullScreenSize) + imageQualities.filter { it.camSelector == cameraSelector } .flatMap { it.qualities } .sortedByDescending { it.pixels } @@ -68,11 +65,11 @@ class ImageQualityManager( .filter { it.isSupported(fullScreenSize.isSixteenToNine()) } } - private fun getFullScreenResolution(cameraSelector: CameraSelector): MySize { + private fun getFullScreenResolution(cameraSelector: CameraSelector): MySize? { return imageQualities.filter { it.camSelector == cameraSelector } .flatMap { it.qualities } .sortedByDescending { it.width } - .first { it.isSupported(false) } - .copy(isFullScreen = true) + .firstOrNull { it.isSupported(false) } + ?.copy(isFullScreen = true) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaActionSound.kt b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaActionSound.kt index dafa9907..a115a235 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaActionSound.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/helpers/MediaActionSound.kt @@ -40,7 +40,7 @@ class MediaActionSound(private val context: Context) { } private class SoundState( - val mediaSound: MediaSound, + val mediaSound: MediaSound?, // 0 is an invalid sample ID. var loadId: Int = 0, var streamId: Int = 0, @@ -99,7 +99,11 @@ class MediaActionSound(private val context: Context) { private fun loadSound(sound: SoundState): Int { var id = 0 - when (val mediaSound = sound.mediaSound) { + if (sound.mediaSound == null) { + return 0 + } + + when (val mediaSound = sound.mediaSound!!) { is MediaSound.ManufacturerSound -> { for (soundDir in SOUND_DIRS) { val soundPath = soundDir + mediaSound.fileName @@ -167,7 +171,7 @@ class MediaActionSound(private val context: Context) { } private fun playWithSoundPool(sound: SoundState) { - if (playCompletionRunnable != null) { + if (playCompletionRunnable != null && sound.mediaSound != null) { val duration = getSoundDuration(sound.mediaSound) playTimeHandler.postDelayed(playCompletionRunnable!!, duration) } diff --git a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt index fdee5c25..b4ad662a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt +++ b/app/src/main/kotlin/com/simplemobiletools/camera/implementations/CameraXPreview.kt @@ -10,6 +10,7 @@ import android.util.Rational import android.util.Size import android.view.* import android.view.GestureDetector.SimpleOnGestureListener +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.camera.core.* import androidx.camera.core.ImageCapture.* @@ -29,8 +30,10 @@ import com.simplemobiletools.camera.R import com.simplemobiletools.camera.extensions.* import com.simplemobiletools.camera.helpers.* import com.simplemobiletools.camera.interfaces.MyPreview -import com.simplemobiletools.camera.models.* import com.simplemobiletools.camera.models.CaptureMode +import com.simplemobiletools.camera.models.MediaOutput +import com.simplemobiletools.camera.models.MySize +import com.simplemobiletools.camera.models.ResolutionOption import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.ensureBackgroundThread @@ -461,14 +464,19 @@ class CameraXPreview( } override fun tryTakePicture() { - val imageCapture = imageCapture ?: throw IllegalStateException("Camera initialization failed.") + if (imageCapture == null) { + activity.toast(R.string.camera_open_error) + return + } + + val imageCapture = imageCapture val metadata = Metadata().apply { isReversedHorizontal = isFrontCameraInUse() && config.flipPhotos } val mediaOutput = mediaOutputHelper.getImageMediaOutput() - imageCapture.takePicture(mainExecutor, object : OnImageCapturedCallback() { + imageCapture!!.takePicture(mainExecutor, object : OnImageCapturedCallback() { override fun onCaptureSuccess(image: ImageProxy) { listener.shutterAnimation() playShutterSoundIfEnabled() @@ -555,20 +563,25 @@ class CameraXPreview( @SuppressLint("MissingPermission", "NewApi") private fun startRecording() { - val videoCapture = videoCapture ?: throw IllegalStateException("Camera initialization failed.") + if (videoCapture == null) { + activity.toast(R.string.camera_open_error) + return + } + + val videoCapture = videoCapture val recording = when (val mediaOutput = mediaOutputHelper.getVideoMediaOutput()) { is MediaOutput.FileDescriptorMediaOutput -> { FileDescriptorOutputOptions.Builder(mediaOutput.fileDescriptor).build() - .let { videoCapture.output.prepareRecording(activity, it) } + .let { videoCapture!!.output.prepareRecording(activity, it) } } is MediaOutput.FileMediaOutput -> { FileOutputOptions.Builder(mediaOutput.file).build() - .let { videoCapture.output.prepareRecording(activity, it) } + .let { videoCapture!!.output.prepareRecording(activity, it) } } is MediaOutput.MediaStoreOutput -> { MediaStoreOutputOptions.Builder(contentResolver, mediaOutput.contentUri).setContentValues(mediaOutput.contentValues).build() - .let { videoCapture.output.prepareRecording(activity, it) } + .let { videoCapture!!.output.prepareRecording(activity, it) } } } diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 8a524510..e188b876 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -3,14 +3,14 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:ignore="AppCompatResource"> - +