make mActivity nullable to avoid leaking

This commit is contained in:
tibbi 2017-12-01 14:27:15 +01:00
parent 9aa7935590
commit b94d7305ea
3 changed files with 29 additions and 28 deletions

View File

@ -46,7 +46,7 @@ ext {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:3.0.12' implementation 'com.simplemobiletools:commons:3.0.20'
debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion" debugImplementation "com.squareup.leakcanary:leakcanary-android:$leakCanaryVersion"
releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion" releaseImplementation "com.squareup.leakcanary:leakcanary-android-no-op:$leakCanaryVersion"

View File

@ -29,7 +29,6 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
lateinit var mSurfaceHolder: SurfaceHolder lateinit var mSurfaceHolder: SurfaceHolder
lateinit var mSurfaceView: SurfaceView lateinit var mSurfaceView: SurfaceView
lateinit var mActivity: MainActivity
lateinit var mCallback: PreviewListener lateinit var mCallback: PreviewListener
lateinit var mScreenSize: Point lateinit var mScreenSize: Point
lateinit var config: Config lateinit var config: Config
@ -59,6 +58,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private var mIsFocusingBeforeCapture = false private var mIsFocusingBeforeCapture = false
private var autoFocusHandler = Handler() private var autoFocusHandler = Handler()
var mActivity: MainActivity? = null
var isWaitingForTakePictureCallback = false var isWaitingForTakePictureCallback = false
var mTargetUri: Uri? = null var mTargetUri: Uri? = null
var isImageCaptureIntent = false var isImageCaptureIntent = false
@ -118,7 +118,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.showErrorToast(e) mActivity!!.showErrorToast(e)
mCallback.setIsCameraAvailable(false) mCallback.setIsCameraAvailable(false)
return false return false
} }
@ -157,14 +157,14 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE)) if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE mParameters!!.focusMode = Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
mCamera!!.setDisplayOrientation(mActivity.getPreviewRotation(mCurrCameraId)) mCamera!!.setDisplayOrientation(mActivity!!.getPreviewRotation(mCurrCameraId))
mCamera!!.parameters = mParameters mCamera!!.parameters = mParameters
if (mCanTakePicture) { if (mCanTakePicture) {
try { try {
mCamera!!.setPreviewDisplay(mSurfaceHolder) mCamera!!.setPreviewDisplay(mSurfaceHolder)
} catch (e: IOException) { } catch (e: IOException) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
return false return false
} }
} }
@ -209,7 +209,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
private fun getDefaultFullscreenResolution(resolutions: List<Camera.Size>): Int? { private fun getDefaultFullscreenResolution(resolutions: List<Camera.Size>): Int? {
val screenAspectRatio = mActivity.realScreenSize.y / mActivity.realScreenSize.x.toFloat() val screenAspectRatio = mActivity!!.realScreenSize.y / mActivity!!.realScreenSize.x.toFloat()
resolutions.forEachIndexed { index, size -> resolutions.forEachIndexed { index, size ->
val diff = screenAspectRatio - (size.width / size.height.toFloat()) val diff = screenAspectRatio - (size.width / size.height.toFloat())
if (Math.abs(diff) < RATIO_TOLERANCE) { if (Math.abs(diff) < RATIO_TOLERANCE) {
@ -319,7 +319,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (mTargetUri != null) { if (mTargetUri != null) {
storePhoto(data) storePhoto(data)
} else { } else {
mActivity.finishActivity() mActivity!!.finishActivity()
} }
} else { } else {
storePhoto(data) storePhoto(data)
@ -327,13 +327,13 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
private fun storePhoto(data: ByteArray) { private fun storePhoto(data: ByteArray) {
PhotoProcessor(mActivity, mTargetUri, mCurrCameraId, mRotationAtCapture).execute(data) PhotoProcessor(mActivity!!, mTargetUri, mCurrCameraId, mRotationAtCapture).execute(data)
} }
private fun handlePreview() { private fun handlePreview() {
if (config.isShowPreviewEnabled) { if (config.isShowPreviewEnabled) {
if (!config.wasPhotoPreviewHintShown) { if (!config.wasPhotoPreviewHintShown) {
mActivity.toast(R.string.click_to_resume_preview) mActivity!!.toast(R.string.click_to_resume_preview)
config.wasPhotoPreviewHintShown = true config.wasPhotoPreviewHintShown = true
} }
} else { } else {
@ -346,7 +346,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private fun resumePreview() { private fun resumePreview() {
mIsPreviewShown = false mIsPreviewShown = false
mActivity.toggleBottomButtons(false) mActivity!!.toggleBottomButtons(false)
try { try {
mCamera?.startPreview() mCamera?.startPreview()
} catch (ignored: Exception) { } catch (ignored: Exception) {
@ -434,7 +434,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
fun showChangeResolutionDialog() { fun showChangeResolutionDialog() {
if (mCamera != null) { if (mCamera != null) {
val oldResolution = getSelectedResolution() val oldResolution = getSelectedResolution()
ChangeResolutionDialog(mActivity, config, mCamera!!) { ChangeResolutionDialog(mActivity!!, config, mCamera!!) {
if (oldResolution != getSelectedResolution()) { if (oldResolution != getSelectedResolution()) {
refreshPreview() refreshPreview()
} }
@ -459,7 +459,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
if (mSwitchToVideoAsap) if (mSwitchToVideoAsap)
initRecorder() initRecorder()
} catch (e: IOException) { } catch (e: IOException) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
} }
} }
@ -488,7 +488,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
try { try {
mCamera!!.startPreview() mCamera!!.startPreview()
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
} }
} }
} }
@ -580,7 +580,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
mCamera!!.parameters = mParameters mCamera!!.parameters = mParameters
Handler().postDelayed({ Handler().postDelayed({
mActivity.runOnUiThread { mActivity!!.runOnUiThread {
mParameters?.flashMode = Camera.Parameters.FLASH_MODE_AUTO mParameters?.flashMode = Camera.Parameters.FLASH_MODE_AUTO
mCamera?.parameters = mParameters mCamera?.parameters = mParameters
} }
@ -611,9 +611,9 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
setAudioSource(MediaRecorder.AudioSource.DEFAULT) setAudioSource(MediaRecorder.AudioSource.DEFAULT)
} }
mCurrVideoPath = mActivity.getOutputMediaFile(false) mCurrVideoPath = mActivity!!.getOutputMediaFile(false)
if (mCurrVideoPath.isEmpty()) { if (mCurrVideoPath.isEmpty()) {
mActivity.toast(R.string.video_creating_error) mActivity!!.toast(R.string.video_creating_error)
return false return false
} }
@ -646,18 +646,18 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
private fun checkPermissions(): Boolean { private fun checkPermissions(): Boolean {
if (mActivity.needsStupidWritePermissions(mCurrVideoPath)) { if (mActivity!!.needsStupidWritePermissions(mCurrVideoPath)) {
if (config.treeUri.isEmpty()) { if (config.treeUri.isEmpty()) {
mActivity.toast(R.string.save_error_internal_storage) mActivity!!.toast(R.string.save_error_internal_storage)
config.savePhotosFolder = Environment.getExternalStorageDirectory().toString() config.savePhotosFolder = Environment.getExternalStorageDirectory().toString()
releaseCamera() releaseCamera()
return false return false
} }
try { try {
var document = mActivity.getFileDocument(mCurrVideoPath) var document = mActivity!!.getFileDocument(mCurrVideoPath)
if (document == null) { if (document == null) {
mActivity.toast(R.string.unknown_error_occurred) mActivity!!.toast(R.string.unknown_error_occurred)
return false return false
} }
@ -674,7 +674,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
private fun setupFailed(e: Exception) { private fun setupFailed(e: Exception) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
releaseCamera() releaseCamera()
} }
@ -690,7 +690,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private fun getVideoRotation(): Int { private fun getVideoRotation(): Int {
val deviceRot = MainActivity.mLastHandledOrientation.compensateDeviceRotation(mCurrCameraId) val deviceRot = MainActivity.mLastHandledOrientation.compensateDeviceRotation(mCurrCameraId)
val previewRot = mActivity.getPreviewRotation(mCurrCameraId) val previewRot = mActivity!!.getPreviewRotation(mCurrCameraId)
return (deviceRot + previewRot) % 360 return (deviceRot + previewRot) % 360
} }
@ -709,7 +709,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
toggleShutterSound(false) toggleShutterSound(false)
mIsRecording = true mIsRecording = true
} catch (e: Exception) { } catch (e: Exception) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
releaseCamera() releaseCamera()
} }
} }
@ -719,9 +719,9 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
try { try {
toggleShutterSound(true) toggleShutterSound(true)
mRecorder!!.stop() mRecorder!!.stop()
mActivity.scanPath(mCurrVideoPath) {} mActivity!!.scanPath(mCurrVideoPath) {}
} catch (e: RuntimeException) { } catch (e: RuntimeException) {
mActivity.showErrorToast(e) mActivity!!.showErrorToast(e)
toggleShutterSound(false) toggleShutterSound(false)
File(mCurrVideoPath).delete() File(mCurrVideoPath).delete()
mRecorder = null mRecorder = null
@ -742,7 +742,7 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
private fun toggleShutterSound(mute: Boolean?) { private fun toggleShutterSound(mute: Boolean?) {
if (!config.isSoundEnabled) { if (!config.isSoundEnabled) {
(mActivity.getSystemService(Context.AUDIO_SERVICE) as AudioManager).setStreamMute(AudioManager.STREAM_SYSTEM, mute!!) (mActivity!!.getSystemService(Context.AUDIO_SERVICE) as AudioManager).setStreamMute(AudioManager.STREAM_SYSTEM, mute!!)
} }
} }
@ -770,10 +770,10 @@ class Preview : ViewGroup, SurfaceHolder.Callback, MediaScannerConnection.OnScan
} }
private fun getScreenSize(): Point { private fun getScreenSize(): Point {
val display = mActivity.windowManager.defaultDisplay val display = mActivity!!.windowManager.defaultDisplay
val size = Point() val size = Point()
display.getSize(size) display.getSize(size)
size.y += mActivity.resources.getNavBarHeight() size.y += mActivity!!.resources.getNavBarHeight()
return size return size
} }

View File

@ -108,6 +108,7 @@ class MainActivity : SimpleActivity(), PreviewListener, PhotoProcessor.MediaSave
override fun onDestroy() { override fun onDestroy() {
super.onDestroy() super.onDestroy()
mPreview?.releaseCamera() mPreview?.releaseCamera()
mPreview?.mActivity = null
} }
private fun initVariables() { private fun initVariables() {