diff --git a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt index 5626723b9..ef9847ab5 100644 --- a/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt +++ b/app/src/main/java/org/kiwix/kiwixmobile/webserver/ZimHostFragment.kt @@ -35,6 +35,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.Toast +import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatActivity import androidx.core.app.ActivityCompat import androidx.core.content.ContextCompat @@ -62,7 +63,6 @@ import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_CHECK_IP_ADDRESS import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_START_SERVER import org.kiwix.kiwixmobile.webserver.wifi_hotspot.HotspotService.ACTION_STOP_SERVER -import java.util.ArrayList import javax.inject.Inject class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { @@ -140,6 +140,11 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { } startServerButton.setOnClickListener { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU || + checkNearbyWifiDevicesPermission() + ) { + startStopServer() + } if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P || checkCoarseLocationAccessPermission()) { startStopServer() } @@ -169,6 +174,33 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { true } + private fun checkNearbyWifiDevicesPermission(): Boolean = + if (ContextCompat.checkSelfPermission( + requireActivity(), + Manifest.permission.NEARBY_WIFI_DEVICES + ) == PackageManager.PERMISSION_DENIED + ) { + if (ActivityCompat.shouldShowRequestPermissionRationale( + requireActivity(), + Manifest.permission.NEARBY_WIFI_DEVICES + ) + ) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + alertDialogShower.show( + KiwixDialog.NearbyWifiPermissionRationaleOnHostZimFile, + ::askNearbyWifiDevicesPermission + ) + } + } else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + askNearbyWifiDevicesPermission() + } + } + false + } else { + true + } + override fun onRequestPermissionsResult( requestCode: Int, permissions: Array, @@ -189,6 +221,14 @@ class ZimHostFragment : BaseFragment(), ZimHostCallbacks, ZimHostContract.View { ) } + @RequiresApi(Build.VERSION_CODES.TIRAMISU) + private fun askNearbyWifiDevicesPermission() { + ActivityCompat.requestPermissions( + requireActivity(), arrayOf(Manifest.permission.NEARBY_WIFI_DEVICES), + PERMISSION_REQUEST_CODE_COARSE_LOCATION + ) + } + private fun startStopServer() { when { ServerUtils.isServerStarted -> stopServer() diff --git a/buildSrc/src/main/kotlin/Versions.kt b/buildSrc/src/main/kotlin/Versions.kt index dcd78bbf4..28b2ad6df 100644 --- a/buildSrc/src/main/kotlin/Versions.kt +++ b/buildSrc/src/main/kotlin/Versions.kt @@ -52,7 +52,7 @@ object Versions { const val javax_annotation_api: String = "1.3.2" - const val leakcanary_android: String = "2.5" + const val leakcanary_android: String = "2.9.1" const val constraintlayout: String = "2.0.4" diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadNotificationManager.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadNotificationManager.kt index 754bcff13..d37a82a90 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadNotificationManager.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/downloader/fetch/FetchDownloadNotificationManager.kt @@ -20,6 +20,7 @@ package org.kiwix.kiwixmobile.core.downloader.fetch import android.annotation.SuppressLint import android.app.NotificationChannel import android.app.NotificationManager +import android.app.PendingIntent.FLAG_IMMUTABLE import android.app.PendingIntent.FLAG_UPDATE_CURRENT import android.app.PendingIntent.getActivity import android.content.Context @@ -119,7 +120,12 @@ class FetchDownloadNotificationManager(context: Context) : addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) putExtra(DOWNLOAD_NOTIFICATION_TITLE, downloadNotification.title) } - notificationBuilder.setContentIntent(getActivity(context, 0, internal, FLAG_UPDATE_CURRENT)) + val pendingIntent = if (Build.VERSION.SDK_INT >= VERSION_CODES.S) { + getActivity(context, 0, internal, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT) + } else { + getActivity(context, 0, internal, FLAG_UPDATE_CURRENT) + } + notificationBuilder.setContentIntent(pendingIntent) notificationBuilder.setAutoCancel(true) } } diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt index fb2aac57b..e0dbe7595 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/utils/dialog/KiwixDialog.kt @@ -56,6 +56,13 @@ sealed class KiwixDialog( android.R.string.cancel ) + object NearbyWifiPermissionRationaleOnHostZimFile : KiwixDialog( + null, + R.string.permission_rationale_location_on_host_zim_file, + android.R.string.yes, + android.R.string.cancel + ) + object StoragePermissionRationale : KiwixDialog( null, R.string.request_storage, diff --git a/core/src/main/res/values/strings.xml b/core/src/main/res/values/strings.xml index 0a823b0c4..ebbad3353 100644 --- a/core/src/main/res/values/strings.xml +++ b/core/src/main/res/values/strings.xml @@ -236,6 +236,7 @@ Connection failed Location permission is required by Android to allow the app to detect peer devices Location permission is required by Android to allow the app to Host Zim files + Nearby wifi devices permission is required by Android to allow the app to Host Zim files Cannot locate peer devices without location permissions Cannot access zim files without storage permission Enable location to allow detection of peers