Added RECEIVER_NOT_EXPORTED to all broadcasts to make the application compatiable with Android 14. Also, Fixed the clicking is not working in notification

This commit is contained in:
MohitMaliFtechiz 2024-07-26 16:45:16 +05:30
parent 8405c7c6d7
commit 28472c0988
7 changed files with 35 additions and 18 deletions

View File

@ -19,6 +19,7 @@ package org.kiwix.kiwixmobile.localFileTransfer
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Context.RECEIVER_NOT_EXPORTED
import android.content.IntentFilter
import android.net.Uri
import android.net.wifi.WpsInfo
@ -32,10 +33,10 @@ import android.net.wifi.p2p.WifiP2pManager.Channel
import android.net.wifi.p2p.WifiP2pManager.ChannelListener
import android.net.wifi.p2p.WifiP2pManager.ConnectionInfoListener
import android.net.wifi.p2p.WifiP2pManager.PeerListListener
import android.os.Build
import android.os.Build.VERSION
import android.os.Build.VERSION_CODES
import android.os.Looper.getMainLooper
import org.kiwix.kiwixmobile.core.utils.files.Log
import android.widget.Toast
import androidx.lifecycle.LifecycleCoroutineScope
import kotlinx.coroutines.launch
@ -44,6 +45,7 @@ import org.kiwix.kiwixmobile.core.extensions.toast
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil
import org.kiwix.kiwixmobile.core.utils.dialog.AlertDialogShower
import org.kiwix.kiwixmobile.core.utils.dialog.KiwixDialog.FileTransferConfirmation
import org.kiwix.kiwixmobile.core.utils.files.Log
import org.kiwix.kiwixmobile.localFileTransfer.FileItem.FileStatus
import org.kiwix.kiwixmobile.localFileTransfer.KiwixWifiP2pBroadcastReceiver.P2pEventListener
import java.io.IOException
@ -105,7 +107,11 @@ class WifiDirectManager @Inject constructor(
addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION)
addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION)
}
context.registerReceiver(receiver, intentFilter)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.registerReceiver(receiver, intentFilter, RECEIVER_NOT_EXPORTED)
} else {
context.registerReceiver(receiver, intentFilter)
}
}
private fun unregisterWifiDirectBroadcastReceiver() = context.unregisterReceiver(receiver)

View File

@ -54,6 +54,7 @@ import org.kiwix.kiwixmobile.core.di.modules.NetworkModule
import org.kiwix.kiwixmobile.core.di.modules.SearchModule
import org.kiwix.kiwixmobile.core.downloader.Downloader
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationActionsBroadcastReceiver
import org.kiwix.kiwixmobile.core.error.ErrorActivity
import org.kiwix.kiwixmobile.core.main.KiwixWebView
import org.kiwix.kiwixmobile.core.reader.ZimFileReader
@ -120,6 +121,7 @@ interface CoreComponent {
fun mutex(): Mutex
fun downloadManagerBroadCastReceiver(): DownloadManagerBroadcastReceiver
fun downloadNotificationActionBroadCastReceiver(): DownloadNotificationActionsBroadcastReceiver
fun inject(application: CoreApp)
fun inject(kiwixWebView: KiwixWebView)

View File

@ -24,8 +24,6 @@ import android.content.Context
import dagger.Module
import dagger.Provides
import org.kiwix.kiwixmobile.core.di.CoreServiceScope
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerBroadcastReceiver
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadManagerMonitor
import org.kiwix.kiwixmobile.core.qr.GenerateQR
import org.kiwix.kiwixmobile.core.read_aloud.ReadAloudNotificationManger
import org.kiwix.kiwixmobile.core.webserver.KiwixServer

View File

@ -357,6 +357,7 @@ class DownloadManagerMonitor @Inject constructor(
}
fun pauseDownload(downloadId: Long) {
Log.e("UPDATE_NOTIFICATION", "pauseDownload: $downloadId")
synchronized(lock) {
updater.onNext {
if (pauseResumeDownloadInDownloadManagerContentResolver(downloadId, STATUS_PAUSED)) {
@ -367,6 +368,7 @@ class DownloadManagerMonitor @Inject constructor(
}
fun resumeDownload(downloadId: Long) {
Log.e("UPDATE_NOTIFICATION", "resumeDownload: $downloadId")
synchronized(lock) {
updater.onNext {
if (pauseResumeDownloadInDownloadManagerContentResolver(downloadId, STATUS_RUNNING)) {
@ -377,6 +379,7 @@ class DownloadManagerMonitor @Inject constructor(
}
fun cancelDownload(downloadId: Long) {
Log.e("UPDATE_NOTIFICATION", "cancelDownload: $downloadId")
synchronized(lock) {
downloadManager.remove(downloadId)
handleCancelledDownload(downloadId)

View File

@ -29,7 +29,7 @@ import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificatio
import org.kiwix.kiwixmobile.core.downloader.downloadManager.DownloadNotificationManager.Companion.NOTIFICATION_ACTION
import javax.inject.Inject
const val DOWNLOAD_NOTIFICATION_ACTION = "download_notification_action"
const val DOWNLOAD_NOTIFICATION_ACTION = "org.kiwix.kiwixmobile.download_notification_action"
class DownloadNotificationActionsBroadcastReceiver @Inject constructor(
private val downloadManagerMonitor: DownloadManagerMonitor
@ -37,18 +37,14 @@ class DownloadNotificationActionsBroadcastReceiver @Inject constructor(
override val action: String = DOWNLOAD_NOTIFICATION_ACTION
override fun onIntentWithActionReceived(context: Context, intent: Intent) {
Log.e(
"UPDATED_NOTIFICATION",
"updateNotification: ${intent.getStringExtra(NOTIFICATION_ACTION)}\n" +
"${intent.getLongExtra(EXTRA_DOWNLOAD_ID, -1L)}"
)
val downloadId = intent.getLongExtra(EXTRA_DOWNLOAD_ID, -1L)
val downloadId = intent.getIntExtra(EXTRA_DOWNLOAD_ID, -1)
val notificationAction = intent.getStringExtra(NOTIFICATION_ACTION)
if (downloadId != -1L) {
Log.e("UPDATE_NOTIFICATION", "onIntentWithActionReceived: $downloadId , $notificationAction")
if (downloadId != -1) {
when (notificationAction) {
ACTION_PAUSE -> downloadManagerMonitor.pauseDownload(downloadId)
ACTION_RESUME -> downloadManagerMonitor.resumeDownload(downloadId)
ACTION_CANCEL -> downloadManagerMonitor.cancelDownload(downloadId)
ACTION_PAUSE -> downloadManagerMonitor.pauseDownload(downloadId.toLong())
ACTION_RESUME -> downloadManagerMonitor.resumeDownload(downloadId.toLong())
ACTION_CANCEL -> downloadManagerMonitor.cancelDownload(downloadId.toLong())
}
}
}

View File

@ -166,8 +166,7 @@ class DownloadNotificationManager @Inject constructor(
private fun getActionPendingIntent(action: String, downloadId: Int): PendingIntent {
val intent =
Intent(context, DownloadNotificationActionsBroadcastReceiver::class.java).apply {
this.action = DOWNLOAD_NOTIFICATION_ACTION
Intent(DOWNLOAD_NOTIFICATION_ACTION).apply {
putExtra(NOTIFICATION_ACTION, action)
putExtra(EXTRA_DOWNLOAD_ID, downloadId)
}

View File

@ -19,12 +19,14 @@
package org.kiwix.kiwixmobile.core.extensions
import android.content.Context
import android.content.Context.RECEIVER_NOT_EXPORTED
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Build
import android.util.TypedValue
import android.widget.Toast
import androidx.annotation.AttrRes
@ -53,7 +55,18 @@ fun Context?.toast(
}
fun Context.registerReceiver(baseBroadcastReceiver: BaseBroadcastReceiver): Intent? =
registerReceiver(baseBroadcastReceiver, IntentFilter(baseBroadcastReceiver.action))
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
registerReceiver(
baseBroadcastReceiver,
IntentFilter(baseBroadcastReceiver.action),
RECEIVER_NOT_EXPORTED
)
} else {
registerReceiver(
baseBroadcastReceiver,
IntentFilter(baseBroadcastReceiver.action)
)
}
val Context.locale: Locale
get() = resources.configuration.locales.get(0)