show error toasts in more exception cases

This commit is contained in:
tibbi 2017-10-21 20:35:35 +02:00
parent 15e88a8510
commit 4541dc6e7d
1 changed files with 15 additions and 21 deletions

View File

@ -9,7 +9,6 @@ import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Environment import android.os.Environment
import android.os.Handler import android.os.Handler
import android.util.Log
import android.view.ScaleGestureDetector import android.view.ScaleGestureDetector
import android.view.SurfaceHolder import android.view.SurfaceHolder
import android.view.SurfaceView import android.view.SurfaceView
@ -24,7 +23,6 @@ import java.util.*
class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener { class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScanCompletedListener {
var mCamera: Camera? = null var mCamera: Camera? = null
private val TAG = Preview::class.java.simpleName
private val FOCUS_AREA_SIZE = 100 private val FOCUS_AREA_SIZE = 100
private val PHOTO_PREVIEW_LENGTH = 500L private val PHOTO_PREVIEW_LENGTH = 500L
private val REFOCUS_PERIOD = 3000L private val REFOCUS_PERIOD = 3000L
@ -40,7 +38,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private var mParameters: Camera.Parameters? = null private var mParameters: Camera.Parameters? = null
private var mRecorder: MediaRecorder? = null private var mRecorder: MediaRecorder? = null
private var mScaleGestureDetector: ScaleGestureDetector? = null private var mScaleGestureDetector: ScaleGestureDetector? = null
private var mZoomRatios: List<Int>? = null private var mZoomRatios = ArrayList<Int>()
private var mCurrVideoPath = "" private var mCurrVideoPath = ""
private var mCanTakePicture = false private var mCanTakePicture = false
@ -119,8 +117,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
newCamera = Camera.open(cameraId) newCamera = Camera.open(cameraId)
mCallback.setIsCameraAvailable(true) mCallback.setIsCameraAvailable(true)
} catch (e: Exception) { } catch (e: Exception) {
mActivity.toast(R.string.camera_open_error) mActivity.showErrorToast(e)
Log.e(TAG, "setCamera open " + e.message)
mCallback.setIsCameraAvailable(false) mCallback.setIsCameraAvailable(false)
return false return false
} }
@ -144,7 +141,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mParameters = mCamera!!.parameters mParameters = mCamera!!.parameters
mMaxZoom = mParameters!!.maxZoom mMaxZoom = mParameters!!.maxZoom
mZoomRatios = mParameters!!.zoomRatios mZoomRatios = mParameters!!.zoomRatios as ArrayList<Int>
mSupportedPreviewSizes = mParameters!!.supportedPreviewSizes.sortedByDescending { it.width * it.height } mSupportedPreviewSizes = mParameters!!.supportedPreviewSizes.sortedByDescending { it.width * it.height }
refreshPreview() refreshPreview()
@ -159,7 +156,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
try { try {
mCamera!!.setPreviewDisplay(mSurfaceHolder) mCamera!!.setPreviewDisplay(mSurfaceHolder)
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "setCamera setPreviewDisplay " + e.message) mActivity.showErrorToast(e)
return false return false
} }
} }
@ -339,6 +336,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
try { try {
mCamera?.startPreview() mCamera?.startPreview()
} catch (e: Exception) { } catch (e: Exception) {
mActivity.showErrorToast(e)
} }
mCanTakePicture = true mCanTakePicture = true
focusArea(false, false) focusArea(false, false)
@ -385,8 +383,8 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
rescheduleAutofocus() rescheduleAutofocus()
} }
} }
} catch (ignored: RuntimeException) { } catch (e: RuntimeException) {
mActivity.showErrorToast(e)
} }
} }
@ -441,7 +439,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (mSwitchToVideoAsap) if (mSwitchToVideoAsap)
initRecorder() initRecorder()
} catch (e: IOException) { } catch (e: IOException) {
Log.e(TAG, "surfaceCreated IOException " + e.message) mActivity.showErrorToast(e)
} }
} }
@ -469,7 +467,8 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mCamera!!.parameters = mParameters mCamera!!.parameters = mParameters
try { try {
mCamera!!.startPreview() mCamera!!.startPreview()
} catch (ignored: RuntimeException) { } catch (e: RuntimeException) {
mActivity.showErrorToast(e)
} }
} }
} }
@ -485,9 +484,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
} }
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) { override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {}
}
private fun getOptimalPreviewSize(sizes: List<Camera.Size>, width: Int, height: Int): Camera.Size { private fun getOptimalPreviewSize(sizes: List<Camera.Size>, width: Int, height: Int): Camera.Size {
var result: Camera.Size? = null var result: Camera.Size? = null
@ -515,12 +512,12 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (mSupportedPreviewSizes != null) { if (mSupportedPreviewSizes != null) {
// for simplicity lets assume that most displays are 16:9 and the remaining ones are 4:3 // for simplicity lets assume that most displays are 16:9 and the remaining ones are 4:3
// always set 16:9 for videos as many devices support 4:3 only in low quality // always set 16:9 for videos as many devices support 4:3 only in low quality
if (mIsSixteenToNine || mIsVideoMode) { mPreviewSize = if (mIsSixteenToNine || mIsVideoMode) {
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes!!, mScreenSize.y, mScreenSize.x) getOptimalPreviewSize(mSupportedPreviewSizes!!, mScreenSize.y, mScreenSize.x)
} else { } else {
val newRatioHeight = (mScreenSize.x * (4.toDouble() / 3)).toInt() val newRatioHeight = (mScreenSize.x * (4.toDouble() / 3)).toInt()
setMeasuredDimension(mScreenSize.x, newRatioHeight) setMeasuredDimension(mScreenSize.x, newRatioHeight)
mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes!!, newRatioHeight, mScreenSize.x) getOptimalPreviewSize(mSupportedPreviewSizes!!, newRatioHeight, mScreenSize.x)
} }
val lp = mSurfaceView.layoutParams val lp = mSurfaceView.layoutParams
@ -653,7 +650,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private fun setupFailed(e: Exception) { private fun setupFailed(e: Exception) {
mActivity.showErrorToast(e) mActivity.showErrorToast(e)
Log.e(TAG, "initRecorder " + e.message)
releaseCamera() releaseCamera()
} }
@ -689,7 +685,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mIsRecording = true mIsRecording = true
} catch (e: Exception) { } catch (e: Exception) {
mActivity.showErrorToast(e) mActivity.showErrorToast(e)
Log.e(TAG, "toggleRecording " + e.message)
releaseCamera() releaseCamera()
} }
} }
@ -701,10 +696,9 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mRecorder!!.stop() mRecorder!!.stop()
mActivity.scanPath(mCurrVideoPath) {} mActivity.scanPath(mCurrVideoPath) {}
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
mActivity.showErrorToast(e)
toggleShutterSound(false) toggleShutterSound(false)
File(mCurrVideoPath).delete() File(mCurrVideoPath).delete()
mActivity.showErrorToast(e)
Log.e(TAG, "stopRecording " + e.message)
mRecorder = null mRecorder = null
mIsRecording = false mIsRecording = false
releaseCamera() releaseCamera()