Skip to content

Commit

Permalink
Merge pull request #4 from Atwa/enhancement/avoid_leaking_activity_re…
Browse files Browse the repository at this point in the history
…ference

Avoid possible activity refrence leaking
  • Loading branch information
Atwa authored Oct 30, 2022
2 parents 7b7160d + 5d08ae8 commit 1104c08
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.atwa.filepicker.core
import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import java.io.File
import java.lang.ref.WeakReference

interface FilePicker {

Expand Down Expand Up @@ -35,6 +36,7 @@ interface FilePicker {

companion object {
@JvmStatic
fun getInstance(activity: AppCompatActivity): FilePicker = StorageFilePicker(activity)
fun getInstance(activity: AppCompatActivity): FilePicker =
StorageFilePicker(WeakReference(activity))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import com.atwa.filepicker.request.PdfPickerRequest
import com.atwa.filepicker.request.PickerRequest
import kotlinx.coroutines.launch
import java.io.File
import java.lang.ref.WeakReference

internal class StorageFilePicker(private val activity: AppCompatActivity) : FilePicker {
internal class StorageFilePicker(private val activity: WeakReference<AppCompatActivity>) : FilePicker {

private lateinit var pickerRequest: PickerRequest
private val decoder: Decoder by lazy { UriDecoder(activity.baseContext) }
private val decoder: Decoder by lazy { UriDecoder(activity.get()?.applicationContext) }

private val filePickerLauncher =
activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
activity.get()?.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
result?.data?.data?.let { processFile(it) }
}

Expand All @@ -40,11 +41,11 @@ internal class StorageFilePicker(private val activity: AppCompatActivity) : File
}

private fun initialize() {
filePickerLauncher.launch(pickerRequest.intent)
filePickerLauncher?.launch(pickerRequest.intent)
}

private fun processFile(uri: Uri) {
activity.lifecycleScope.launch {
activity.get()?.lifecycleScope?.launchWhenResumed {
pickerRequest.invokeCallback(uri)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import java.io.FileOutputStream
import java.io.IOException

internal class UriDecoder(
private val context: Context,
private val context: Context?,
private val streamer: Streamer = FileStreamer()
) : Decoder {

Expand All @@ -25,7 +25,7 @@ internal class UriDecoder(

override fun getStorageImage(imageUri: Uri?) = flow {
this@UriDecoder.uri = imageUri
this@UriDecoder.contentResolver = context.contentResolver
this@UriDecoder.contentResolver = context?.contentResolver
val result = try {
getBitMap()
} catch (ex: IOException) {
Expand All @@ -37,7 +37,7 @@ internal class UriDecoder(

override fun getStoragePDF(pdfUri: Uri?) = flow {
this@UriDecoder.uri = pdfUri
this@UriDecoder.contentResolver = context.contentResolver
this@UriDecoder.contentResolver = context?.contentResolver
val result = try {
getFile()
} catch (ex: IOException) {
Expand All @@ -49,7 +49,7 @@ internal class UriDecoder(

override fun getStorageFile(pdfUri: Uri?) = flow {
this@UriDecoder.uri = pdfUri
this@UriDecoder.contentResolver = context.contentResolver
this@UriDecoder.contentResolver = context?.contentResolver
val result = try {
getFile()
} catch (ex: IOException) {
Expand All @@ -66,7 +66,7 @@ internal class UriDecoder(
val inputStream = FileInputStream(pfd.fileDescriptor)
val bitmap = decodeFileDescriptor(pfd.fileDescriptor)
val name = getFileName() ?: "file"
val imageFile = File(context.cacheDir, name)
val imageFile = File(context?.cacheDir, name)
val outputStream = FileOutputStream(imageFile)
streamer.copyFile(inputStream, outputStream)
Pair(bitmap, imageFile)
Expand All @@ -80,7 +80,7 @@ internal class UriDecoder(
val pfd = contentResolver?.openFileDescriptor(pdfUri, "r")
val inputStream = FileInputStream(pfd?.fileDescriptor)
val name = getFileName() ?: "file"
val pdfFile = File(context.cacheDir, name)
val pdfFile = File(context?.cacheDir, name)
val outputStream = FileOutputStream(pdfFile)
streamer.copyFile(inputStream, outputStream)
Pair(name, pdfFile)
Expand Down

0 comments on commit 1104c08

Please sign in to comment.