Skip to content

Commit

Permalink
Get FragmentActivity from ContextWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
WMariusz committed Jun 8, 2021
1 parent e8c9ae4 commit def0721
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 4 additions & 4 deletions cropper/src/main/java/com/canhub/cropper/CropImageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.ProgressBar
import androidx.exifinterface.media.ExifInterface
import androidx.fragment.app.FragmentActivity
import com.canhub.cropper.CropOverlayView.CropWindowChangeListener
import com.canhub.cropper.utils.getFilePathFromUri
import com.canhub.cropper.utils.getFragmentActivityFrom
import java.lang.ref.WeakReference
import java.util.UUID
import kotlin.math.max
Expand Down Expand Up @@ -731,7 +731,7 @@ class CropImageView @JvmOverloads constructor(context: Context, attrs: Attribute
clearImageInt()
mCropOverlayView!!.initialCropWindowRect = null
mBitmapLoadingWorkerJob =
WeakReference(BitmapLoadingWorkerJob((context as FragmentActivity), this, uri))
WeakReference(BitmapLoadingWorkerJob(getFragmentActivityFrom(context), this, uri))
mBitmapLoadingWorkerJob!!.get()!!.start()
setProgressBarVisibility()
}
Expand Down Expand Up @@ -999,7 +999,7 @@ class CropImageView @JvmOverloads constructor(context: Context, attrs: Attribute
) {
WeakReference(
BitmapCroppingWorkerJob(
(context as FragmentActivity),
getFragmentActivityFrom(context),
this,
imageUri,
cropPoints,
Expand All @@ -1022,7 +1022,7 @@ class CropImageView @JvmOverloads constructor(context: Context, attrs: Attribute
} else {
WeakReference(
BitmapCroppingWorkerJob(
(context as FragmentActivity),
getFragmentActivityFrom(context),
this,
bitmap,
cropPoints,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.canhub.cropper.utils

import android.content.Context
import android.content.ContextWrapper
import androidx.fragment.app.FragmentActivity

internal fun getFragmentActivityFrom(context: Context): FragmentActivity {
var unpackContext = context
while (unpackContext !is FragmentActivity && unpackContext is ContextWrapper) {
unpackContext = unpackContext.baseContext
}

return unpackContext as FragmentActivity
}

0 comments on commit def0721

Please sign in to comment.