Skip to content

Commit

Permalink
Fix memory leak (#344)
Browse files Browse the repository at this point in the history
* Refactor launcher

* fix memory leak in image picker and sample app
  • Loading branch information
esafirm authored Mar 17, 2021
1 parent a55cd29 commit 721813a
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 45 deletions.
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
android.enableJetifier=true
android.useAndroidX=true
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ContentObserverTrigger(
Lifecycle.Event.ON_CREATE -> onCreate()
Lifecycle.Event.ON_DESTROY -> onDestroy()
else -> {
// Ignore others event
}
}
}
Expand All @@ -30,7 +31,7 @@ class ContentObserverTrigger(
handler = Handler()
}

val contentObserver = object : ContentObserver(handler) {
observer = object : ContentObserver(handler) {
override fun onChange(selfChange: Boolean) {
loadData()
}
Expand All @@ -39,7 +40,7 @@ class ContentObserverTrigger(
contentResolver.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
false,
contentObserver
observer!!
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface ImagePickerComponents {

open class DefaultImagePickerComponents(context: Context) : ImagePickerComponents {
override val imageLoader: ImageLoader by lazy { DefaultImageLoader() }
override val imageFileLoader: ImageFileLoader by lazy { DefaultImageFileLoader(context) }
override val imageFileLoader: ImageFileLoader by lazy { DefaultImageFileLoader(context.applicationContext) }
override val cameraModule: CameraModule by lazy { DefaultCameraModule() }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.android.parcel.IgnoredOnParcel
import kotlinx.android.parcel.Parcelize

@Parcelize
open class Image(
class Image(
val id: Long,
val name: String,
val path: String,
Expand Down
3 changes: 1 addition & 2 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ dependencies {
/* Development */
implementation project(':imagepicker')

debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.6.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-support-fragment:1.6.2'
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.6'
implementation "androidx.core:core-ktx:$core_ktx_version"

/* UI Test */
Expand Down
19 changes: 0 additions & 19 deletions sample/src/debug/java/sample/SampleApplication.kt

This file was deleted.

1 change: 0 additions & 1 deletion sample/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package="com.esafirm.sample">

<application
android:name=".SampleApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.esafirm.sample

import android.content.Context
import com.esafirm.imagepicker.features.DefaultImagePickerComponents
import com.esafirm.imagepicker.features.imageloader.DefaultImageLoader
import com.esafirm.imagepicker.features.imageloader.ImageLoader

class CustomImagePickerComponents(
context: Context,
private val useCustomImageLoader: Boolean
) : DefaultImagePickerComponents(context.applicationContext) {
override val imageLoader: ImageLoader
get() = if (useCustomImageLoader) {
GrayscaleImageLoader()
} else {
DefaultImageLoader()
}
}
11 changes: 3 additions & 8 deletions sample/src/main/java/com/esafirm/sample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,9 @@ class MainActivity : AppCompatActivity() {
val onlyVideo = binding.switchOnlyVideo.isChecked
val isExclude = binding.switchIncludeExclude.isChecked

ImagePickerComponentsHolder.setInternalComponent(object : DefaultImagePickerComponents(this) {
override val imageLoader: ImageLoader
get() = if (useCustomImageLoader) {
GrayscaleImageLoader()
} else {
DefaultImageLoader()
}
})
ImagePickerComponentsHolder.setInternalComponent(
CustomImagePickerComponents(this, useCustomImageLoader)
)

return ImagePickerConfig {

Expand Down
10 changes: 0 additions & 10 deletions sample/src/main/java/com/esafirm/sample/SampleApplication.kt

This file was deleted.

0 comments on commit 721813a

Please sign in to comment.