Use ContextCompat.registerReceiver with explicit RECEIVER_EXPORTED to fix crash on API 34.

java.lang.SecurityException: im.vector.app.debug: One of RECEIVER_EXPORTED or RECEIVER_NOT_EXPORTED should be specified when a receiver isn't being registered exclusively for system broadcasts
This commit is contained in:
Benoit Marty 2024-07-18 16:59:05 +02:00
parent 10e79849a5
commit 1e32ebde41
7 changed files with 53 additions and 17 deletions

View file

@ -22,6 +22,7 @@ import android.content.IntentFilter
import android.net.ConnectivityManager import android.net.ConnectivityManager
import android.net.Network import android.net.Network
import android.os.Build import android.os.Build
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject import javax.inject.Inject
@ -43,7 +44,12 @@ internal class FallbackNetworkCallbackStrategy @Inject constructor(
networkInfoReceiver.isConnectedCallback = { networkInfoReceiver.isConnectedCallback = {
hasChanged() hasChanged()
} }
context.registerReceiver(networkInfoReceiver, filter) ContextCompat.registerReceiver(
context,
networkInfoReceiver,
filter,
ContextCompat.RECEIVER_NOT_EXPORTED,
)
} }
override fun unregister() { override fun unregister() {

View file

@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.SharedPreferences import android.content.SharedPreferences
import androidx.core.content.ContextCompat
import androidx.core.content.edit import androidx.core.content.edit
import im.vector.app.core.debug.DebugReceiver import im.vector.app.core.debug.DebugReceiver
import im.vector.app.core.di.DefaultPreferences import im.vector.app.core.di.DefaultPreferences
@ -37,7 +38,12 @@ class VectorDebugReceiver @Inject constructor(
) : BroadcastReceiver(), DebugReceiver { ) : BroadcastReceiver(), DebugReceiver {
override fun register(context: Context) { override fun register(context: Context) {
context.registerReceiver(this, getIntentFilter(context)) ContextCompat.registerReceiver(
context,
this,
getIntentFilter(context),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
} }
override fun unregister(context: Context) { override fun unregister(context: Context) {

View file

@ -27,6 +27,7 @@ import android.os.HandlerThread
import android.os.StrictMode import android.os.StrictMode
import android.util.Log import android.util.Log
import android.view.Gravity import android.view.Gravity
import androidx.core.content.ContextCompat
import androidx.core.provider.FontRequest import androidx.core.provider.FontRequest
import androidx.core.provider.FontsContractCompat import androidx.core.provider.FontsContractCompat
import androidx.lifecycle.DefaultLifecycleObserver import androidx.lifecycle.DefaultLifecycleObserver
@ -217,13 +218,16 @@ class VectorApplication :
ProcessLifecycleOwner.get().lifecycle.addObserver(callManager) ProcessLifecycleOwner.get().lifecycle.addObserver(callManager)
// This should be done as early as possible // This should be done as early as possible
// initKnownEmojiHashSet(appContext) // initKnownEmojiHashSet(appContext)
ContextCompat.registerReceiver(
applicationContext.registerReceiver(powerKeyReceiver, IntentFilter().apply { applicationContext,
// Looks like i cannot receive OFF, if i don't have both ON and OFF powerKeyReceiver,
addAction(Intent.ACTION_SCREEN_OFF) IntentFilter().apply {
addAction(Intent.ACTION_SCREEN_ON) // Looks like i cannot receive OFF, if i don't have both ON and OFF
}) addAction(Intent.ACTION_SCREEN_OFF)
addAction(Intent.ACTION_SCREEN_ON)
},
ContextCompat.RECEIVER_NOT_EXPORTED,
)
EmojiManager.install(GoogleEmojiProvider()) EmojiManager.install(GoogleEmojiProvider())
// Initialize Mapbox before inflating mapViews // Initialize Mapbox before inflating mapViews

View file

@ -23,6 +23,7 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import androidx.core.content.ContextCompat
import im.vector.lib.core.utils.compat.getParcelableExtraCompat import im.vector.lib.core.utils.compat.getParcelableExtraCompat
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -83,7 +84,12 @@ class BluetoothHeadsetReceiver : BroadcastReceiver() {
fun createAndRegister(context: Context, listener: EventListener): BluetoothHeadsetReceiver { fun createAndRegister(context: Context, listener: EventListener): BluetoothHeadsetReceiver {
val receiver = BluetoothHeadsetReceiver() val receiver = BluetoothHeadsetReceiver()
receiver.delegate = WeakReference(listener) receiver.delegate = WeakReference(listener)
context.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED)) ContextCompat.registerReceiver(
context,
receiver,
IntentFilter(BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
return receiver return receiver
} }

View file

@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.media.AudioManager import android.media.AudioManager
import androidx.core.content.ContextCompat
import timber.log.Timber import timber.log.Timber
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
@ -69,7 +70,12 @@ class WiredHeadsetStateReceiver : BroadcastReceiver() {
val receiver = WiredHeadsetStateReceiver() val receiver = WiredHeadsetStateReceiver()
receiver.delegate = WeakReference(listener) receiver.delegate = WeakReference(listener)
val action = AudioManager.ACTION_HEADSET_PLUG val action = AudioManager.ACTION_HEADSET_PLUG
context.registerReceiver(receiver, IntentFilter(action)) ContextCompat.registerReceiver(
context,
receiver,
IntentFilter(action),
ContextCompat.RECEIVER_NOT_EXPORTED,
)
return receiver return receiver
} }

View file

@ -21,6 +21,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.IntentFilter import android.content.IntentFilter
import android.content.RestrictionsManager import android.content.RestrictionsManager
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService import androidx.core.content.getSystemService
import dagger.hilt.android.qualifiers.ApplicationContext import dagger.hilt.android.qualifiers.ApplicationContext
import timber.log.Timber import timber.log.Timber
@ -44,7 +45,12 @@ class DefaultMdmService @Inject constructor(
override fun registerListener(context: Context, onChangedListener: () -> Unit) { override fun registerListener(context: Context, onChangedListener: () -> Unit) {
val restrictionsFilter = IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED) val restrictionsFilter = IntentFilter(Intent.ACTION_APPLICATION_RESTRICTIONS_CHANGED)
this.onChangedListener = onChangedListener this.onChangedListener = onChangedListener
context.registerReceiver(restrictionsReceiver, restrictionsFilter) ContextCompat.registerReceiver(
context,
restrictionsReceiver,
restrictionsFilter,
ContextCompat.RECEIVER_NOT_EXPORTED
)
} }
override fun unregisterListener(context: Context) { override fun unregisterListener(context: Context) {

View file

@ -31,6 +31,7 @@ import android.os.Build
import android.util.Rational import android.util.Rational
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.PictureInPictureModeChangedInfo import androidx.core.app.PictureInPictureModeChangedInfo
import androidx.core.content.ContextCompat
import androidx.core.util.Consumer import androidx.core.util.Consumer
import androidx.core.view.isVisible import androidx.core.view.isVisible
import com.airbnb.mvrx.Mavericks import com.airbnb.mvrx.Mavericks
@ -201,11 +202,12 @@ class WidgetActivity : VectorBaseActivity<ActivityWidgetBinding>() {
} }
} }
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { ContextCompat.registerReceiver(
registerReceiver(hangupBroadcastReceiver, IntentFilter(ACTION_MEDIA_CONTROL), RECEIVER_NOT_EXPORTED) this,
} else { hangupBroadcastReceiver,
registerReceiver(hangupBroadcastReceiver, IntentFilter(ACTION_MEDIA_CONTROL)) IntentFilter(ACTION_MEDIA_CONTROL),
} ContextCompat.RECEIVER_NOT_EXPORTED,
)
} else { } else {
unregisterReceiver(hangupBroadcastReceiver) unregisterReceiver(hangupBroadcastReceiver)
} }