Skip to content

Commit

Permalink
Added explicit RECEIVER_EXPORTED and RECEIVER_NOT_EXPORTED flags for …
Browse files Browse the repository at this point in the history
…registering broadcast receivers
  • Loading branch information
sherzodnosirov committed Oct 17, 2023
1 parent 9b0d31c commit 04a6f7a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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?) {
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -74,6 +77,8 @@ class GalleryFragment : Fragment(), GalleryView {
presenter.onCreate(savedInstanceState)
}


@SuppressLint("UnspecifiedRegisterReceiverFlag")
override fun onAttach(context: Context) {
super.onAttach(context)
activity?.let {
Expand All @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit 04a6f7a

Please sign in to comment.