diff --git a/pickcam/src/main/kotlin/com/lovoo/android/pickcam/view/PickPicCaptureFragment.kt b/pickcam/src/main/kotlin/com/lovoo/android/pickcam/view/PickPicCaptureFragment.kt index 6e33030..4d7bbdf 100644 --- a/pickcam/src/main/kotlin/com/lovoo/android/pickcam/view/PickPicCaptureFragment.kt +++ b/pickcam/src/main/kotlin/com/lovoo/android/pickcam/view/PickPicCaptureFragment.kt @@ -16,21 +16,12 @@ package com.lovoo.android.pickcam.view import android.app.Activity -import android.content.BroadcastReceiver -import android.content.Context -import android.content.Intent -import android.content.IntentFilter +import android.content.* import android.content.pm.PackageManager -import android.os.Build -import android.os.Bundle -import android.os.Environment -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.os.* +import android.view.* import androidx.core.content.ContextCompat -import androidx.fragment.app.DialogFragment -import androidx.fragment.app.Fragment -import androidx.fragment.app.FragmentManager +import androidx.fragment.app.* import com.lovoo.android.pickcam.R import com.lovoo.android.pickcam.worker.CaptureResultWorker import com.lovoo.android.pickcore.contract.CameraDestination @@ -40,6 +31,7 @@ import com.lovoo.android.pickcore.destination.PublicDirectory import com.lovoo.android.pickcore.loader.CameraLoader import com.lovoo.android.pickcore.permission.Permission import com.lovoo.android.pickcore.util.isMinimumR +import com.lovoo.android.pickcore.util.registerReceiverSafely /** * Ready to use solution to handle Android Camera capture. @@ -81,7 +73,10 @@ class PickPicCaptureFragment : DialogFragment() { throw (IllegalArgumentException("You have to implement CaptureCallback")) } } - context.registerReceiver(onWorkerDoneReceiver, IntentFilter().apply { addAction(CaptureResultWorker.INTENT_ACTION_ON_RESULT) }) + context.registerReceiverSafely( + receiver = onWorkerDoneReceiver, + filter = IntentFilter().apply { addAction(CaptureResultWorker.INTENT_ACTION_ON_RESULT) }, + ) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { @@ -131,7 +126,8 @@ class PickPicCaptureFragment : DialogFragment() { preferences.edit().putBoolean(PREF_KEY_PERMISSION, false).apply() } - val deniedPermissions = Permission.getDeniedPermissions(requireActivity(), Permission.cameraPermissions.plus(Permission.galleryPermissions)) + val deniedPermissions = + Permission.getDeniedPermissions(requireActivity(), Permission.cameraPermissions.plus(Permission.galleryPermissions)) if (deniedPermissions.isEmpty()) { startCamera() } else { diff --git a/pickcore/src/main/kotlin/com/lovoo/android/pickcore/util/ContextExtensions.kt b/pickcore/src/main/kotlin/com/lovoo/android/pickcore/util/ContextExtensions.kt index 2575b72..44bfd53 100644 --- a/pickcore/src/main/kotlin/com/lovoo/android/pickcore/util/ContextExtensions.kt +++ b/pickcore/src/main/kotlin/com/lovoo/android/pickcore/util/ContextExtensions.kt @@ -15,8 +15,30 @@ */ package com.lovoo.android.pickcore.util +import android.annotation.SuppressLint +import android.content.* import android.os.Build fun isMinimumQ() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q fun isMinimumR() = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R + +/** + * Registers given [receiver] for all Android versions, including Android 13+, where the "EXPORTED" nature of the receiver must be explicitly passed + * to the parameters. + * + * @param isExported indicates if the BroadcastReceiver is exported or not (i.e. available to external apps). False by default. + */ +@SuppressLint("UnspecifiedRegisterReceiverFlag") +fun Context.registerReceiverSafely( + receiver: BroadcastReceiver, + filter: IntentFilter, + isExported: Boolean = false +) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + val exportedFlag = if (isExported) Context.RECEIVER_EXPORTED else Context.RECEIVER_NOT_EXPORTED + registerReceiver(receiver, filter, exportedFlag) + } else { + registerReceiver(receiver, filter) + } +} \ No newline at end of file diff --git a/pickui/src/main/kotlin/com/lovoo/android/pickui/view/GalleryFragment.kt b/pickui/src/main/kotlin/com/lovoo/android/pickui/view/GalleryFragment.kt index 1b9cbed..cd2e2d3 100644 --- a/pickui/src/main/kotlin/com/lovoo/android/pickui/view/GalleryFragment.kt +++ b/pickui/src/main/kotlin/com/lovoo/android/pickui/view/GalleryFragment.kt @@ -15,12 +15,14 @@ */ package com.lovoo.android.pickui.view +import android.annotation.SuppressLint import android.app.Activity import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.content.IntentFilter import android.database.Cursor +import android.os.Build import android.os.Bundle import android.view.Gravity import android.view.LayoutInflater @@ -37,6 +39,7 @@ import com.lovoo.android.pickcore.model.Gallery import com.lovoo.android.pickcore.model.convertToUi import com.lovoo.android.pickcore.permission.Permission import com.lovoo.android.pickcore.presenter.GalleryPresenterImpl +import com.lovoo.android.pickcore.util.registerReceiverSafely import com.lovoo.android.pickui.R import com.lovoo.android.pickui.adapter.GalleryAdapter import com.lovoo.android.pickui.adapter.PopUpGalleryAdapter @@ -74,6 +77,8 @@ class GalleryFragment : Fragment(), GalleryView { presenter.onCreate(savedInstanceState) } + + @SuppressLint("UnspecifiedRegisterReceiverFlag") override fun onAttach(context: Context) { super.onAttach(context) activity?.let { @@ -82,7 +87,7 @@ class GalleryFragment : Fragment(), GalleryView { val filter = IntentFilter().apply { addAction(CameraLoader.INTENT_INVALIDATE_GALLERY) } - context.registerReceiver(invalidateReceiver, filter) + context.registerReceiverSafely(receiver = invalidateReceiver, filter = filter,) } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { diff --git a/pickui/src/main/kotlin/com/lovoo/android/pickui/view/PictureFragment.kt b/pickui/src/main/kotlin/com/lovoo/android/pickui/view/PictureFragment.kt index 200ba9d..deaa24b 100644 --- a/pickui/src/main/kotlin/com/lovoo/android/pickui/view/PictureFragment.kt +++ b/pickui/src/main/kotlin/com/lovoo/android/pickui/view/PictureFragment.kt @@ -38,6 +38,7 @@ import com.lovoo.android.pickcore.model.Gallery import com.lovoo.android.pickcore.model.Picture import com.lovoo.android.pickcore.model.convertToUi import com.lovoo.android.pickcore.presenter.PicturePresenterImpl +import com.lovoo.android.pickcore.util.registerReceiverSafely import com.lovoo.android.pickui.R import com.lovoo.android.pickui.adapter.PictureAdapter import com.lovoo.android.pickui.databinding.PickpicFragmentPictureBinding @@ -111,7 +112,7 @@ class PictureFragment : Fragment(), PictureView { val filter = IntentFilter().apply { addAction(CameraLoader.INTENT_INVALIDATE_GALLERY) } - context.registerReceiver(invalidateReceiver, filter) + context.registerReceiverSafely(invalidateReceiver, filter) } override fun onDetach() {