mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-09-08 14:52:13 -04:00
Refactored code to remove lint errors
* Now our minimum SDK version is 24 and we are using some conditions placed on behalf of this api level which are unused now, so we have removed those conditions. * Removed `CompatV21` file as now it is unused. * Renamed `CompatV23` to `CompatV24` and refactored the code to support our new minimum api level. * Refactored `NetworkUtilsTest` to support api level 24.
This commit is contained in:
parent
0e6300f20c
commit
6655ad6c69
@ -18,8 +18,6 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.zimManager.fileselectView.effects
|
package org.kiwix.kiwixmobile.zimManager.fileselectView.effects
|
||||||
|
|
||||||
import android.net.Uri
|
|
||||||
import android.os.Build
|
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.core.content.FileProvider
|
import androidx.core.content.FileProvider
|
||||||
import androidx.core.os.bundleOf
|
import androidx.core.os.bundleOf
|
||||||
@ -33,15 +31,11 @@ data class ShareFiles(private val selectedBooks: List<BookOnDisk>) :
|
|||||||
SideEffect<Unit> {
|
SideEffect<Unit> {
|
||||||
override fun invokeWith(activity: AppCompatActivity) {
|
override fun invokeWith(activity: AppCompatActivity) {
|
||||||
val selectedFileContentURIs = selectedBooks.mapNotNull {
|
val selectedFileContentURIs = selectedBooks.mapNotNull {
|
||||||
if (Build.VERSION.SDK_INT >= 24) {
|
|
||||||
FileProvider.getUriForFile(
|
FileProvider.getUriForFile(
|
||||||
activity,
|
activity,
|
||||||
activity.packageName + ".fileprovider",
|
activity.packageName + ".fileprovider",
|
||||||
it.file
|
it.file
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
Uri.fromFile(it.file)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
activity.navigate(
|
activity.navigate(
|
||||||
R.id.localFileTransferFragment,
|
R.id.localFileTransferFragment,
|
||||||
|
@ -90,9 +90,7 @@ abstract class CoreApp : Application() {
|
|||||||
if (BuildConfig.DEBUG) {
|
if (BuildConfig.DEBUG) {
|
||||||
StrictMode.setThreadPolicy(
|
StrictMode.setThreadPolicy(
|
||||||
StrictMode.ThreadPolicy.Builder().apply {
|
StrictMode.ThreadPolicy.Builder().apply {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
detectResourceMismatches()
|
detectResourceMismatches()
|
||||||
}
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
detectUnbufferedIo()
|
detectUnbufferedIo()
|
||||||
}
|
}
|
||||||
@ -106,9 +104,7 @@ abstract class CoreApp : Application() {
|
|||||||
)
|
)
|
||||||
StrictMode.setVmPolicy(
|
StrictMode.setVmPolicy(
|
||||||
VmPolicy.Builder().apply {
|
VmPolicy.Builder().apply {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
detectCleartextNetwork()
|
detectCleartextNetwork()
|
||||||
}
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
detectContentUriWithoutPermission()
|
detectContentUriWithoutPermission()
|
||||||
}
|
}
|
||||||
|
@ -32,7 +32,7 @@ import android.net.ConnectivityManager
|
|||||||
*
|
*
|
||||||
*
|
*
|
||||||
* Each implementation ends with a `V<n>` suffix, identifying the minimum API version on which this implementation
|
* Each implementation ends with a `V<n>` suffix, identifying the minimum API version on which this implementation
|
||||||
* can be used. For example, see [CompatV21].
|
* can be used. For example, see [CompatV24].
|
||||||
*
|
*
|
||||||
*
|
*
|
||||||
* Each implementation `CompatVn` should extend the implementation `CompatVm` for the greatest m<n such that `CompatVm`
|
* Each implementation `CompatVn` should extend the implementation `CompatVm` for the greatest m<n such that `CompatVm`
|
||||||
|
@ -29,8 +29,7 @@ class CompatHelper private constructor() {
|
|||||||
// Note: Needs ": Compat" or the type system assumes `Compat21`
|
// Note: Needs ": Compat" or the type system assumes `Compat21`
|
||||||
private val compatValue: Compat = when {
|
private val compatValue: Compat = when {
|
||||||
sdkVersion >= Build.VERSION_CODES.TIRAMISU -> CompatV33()
|
sdkVersion >= Build.VERSION_CODES.TIRAMISU -> CompatV33()
|
||||||
sdkVersion >= Build.VERSION_CODES.M -> CompatV23()
|
else -> CompatV24()
|
||||||
else -> CompatV21()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -1,60 +0,0 @@
|
|||||||
/*
|
|
||||||
* Kiwix Android
|
|
||||||
* Copyright (c) 2023 Kiwix <android.kiwix.org>
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@file:Suppress("DEPRECATION")
|
|
||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.compat
|
|
||||||
|
|
||||||
import android.content.Intent
|
|
||||||
import android.content.pm.PackageInfo
|
|
||||||
import android.content.pm.PackageManager
|
|
||||||
import android.content.pm.ResolveInfo
|
|
||||||
import android.net.ConnectivityManager
|
|
||||||
import android.net.ConnectivityManager.TYPE_WIFI
|
|
||||||
import android.net.NetworkInfo.State.CONNECTED
|
|
||||||
|
|
||||||
open class CompatV21 : Compat {
|
|
||||||
override fun queryIntentActivities(
|
|
||||||
packageManager: PackageManager,
|
|
||||||
intent: Intent,
|
|
||||||
flags: ResolveInfoFlagsCompat
|
|
||||||
): List<ResolveInfo> = packageManager.queryIntentActivities(intent, flags.value.toInt())
|
|
||||||
|
|
||||||
override fun getPackageInformation(
|
|
||||||
packageName: String,
|
|
||||||
packageManager: PackageManager,
|
|
||||||
flag: Int
|
|
||||||
): PackageInfo = packageManager.getPackageInfo(packageName, 0)
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the device has a network connection with internet access.
|
|
||||||
*
|
|
||||||
* @param connectivity The ConnectivityManager instance.
|
|
||||||
* @return True if a network connection with internet access is available, false otherwise.
|
|
||||||
*/
|
|
||||||
override fun isNetworkAvailable(connectivity: ConnectivityManager): Boolean =
|
|
||||||
connectivity.allNetworkInfo.any { it.state == CONNECTED }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Checks if the device is connected to a Wi-Fi network.
|
|
||||||
*
|
|
||||||
* @param connectivity The ConnectivityManager instance.
|
|
||||||
* @return True if connected to a Wi-Fi network, false otherwise.
|
|
||||||
*/
|
|
||||||
override fun isWifi(connectivity: ConnectivityManager): Boolean =
|
|
||||||
connectivity.getNetworkInfo(TYPE_WIFI)?.isConnected == true
|
|
||||||
}
|
|
@ -15,10 +15,10 @@
|
|||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@file:Suppress("DEPRECATION")
|
||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.compat
|
package org.kiwix.kiwixmobile.core.compat
|
||||||
|
|
||||||
import android.annotation.TargetApi
|
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.content.pm.PackageInfo
|
import android.content.pm.PackageInfo
|
||||||
import android.content.pm.PackageManager
|
import android.content.pm.PackageManager
|
||||||
@ -27,24 +27,19 @@ import android.net.ConnectivityManager
|
|||||||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
||||||
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
||||||
|
|
||||||
const val API_23 = 23
|
open class CompatV24 : Compat {
|
||||||
|
|
||||||
@TargetApi(API_23)
|
|
||||||
open class CompatV23 : Compat {
|
|
||||||
private val compatV21 = CompatV21()
|
|
||||||
|
|
||||||
override fun queryIntentActivities(
|
override fun queryIntentActivities(
|
||||||
packageManager: PackageManager,
|
packageManager: PackageManager,
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
flags: ResolveInfoFlagsCompat
|
flags: ResolveInfoFlagsCompat
|
||||||
): List<ResolveInfo> =
|
): List<ResolveInfo> = packageManager.queryIntentActivities(intent, flags.value.toInt())
|
||||||
compatV21.queryIntentActivities(packageManager, intent, flags)
|
|
||||||
|
|
||||||
override fun getPackageInformation(
|
override fun getPackageInformation(
|
||||||
packageName: String,
|
packageName: String,
|
||||||
packageManager: PackageManager,
|
packageManager: PackageManager,
|
||||||
flag: Int
|
flag: Int
|
||||||
): PackageInfo = compatV21.getPackageInformation(packageName, packageManager, flag)
|
): PackageInfo = packageManager.getPackageInfo(packageName, 0)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the device has a network connection with internet access.
|
* Checks if the device has a network connection with internet access.
|
@ -30,7 +30,7 @@ const val API_33 = 33
|
|||||||
|
|
||||||
@TargetApi(API_33)
|
@TargetApi(API_33)
|
||||||
open class CompatV33 : Compat {
|
open class CompatV33 : Compat {
|
||||||
private val compatV23 = CompatV23()
|
private val compatV24 = CompatV24()
|
||||||
override fun queryIntentActivities(
|
override fun queryIntentActivities(
|
||||||
packageManager: PackageManager,
|
packageManager: PackageManager,
|
||||||
intent: Intent,
|
intent: Intent,
|
||||||
@ -48,8 +48,8 @@ open class CompatV33 : Compat {
|
|||||||
packageManager.getPackageInfo(packageName, PackageInfoFlags.of(flag.toLong()))
|
packageManager.getPackageInfo(packageName, PackageInfoFlags.of(flag.toLong()))
|
||||||
|
|
||||||
override fun isNetworkAvailable(connectivity: ConnectivityManager): Boolean =
|
override fun isNetworkAvailable(connectivity: ConnectivityManager): Boolean =
|
||||||
compatV23.isNetworkAvailable(connectivity)
|
compatV24.isNetworkAvailable(connectivity)
|
||||||
|
|
||||||
override fun isWifi(connectivity: ConnectivityManager): Boolean =
|
override fun isWifi(connectivity: ConnectivityManager): Boolean =
|
||||||
compatV23.isWifi(connectivity)
|
compatV24.isWifi(connectivity)
|
||||||
}
|
}
|
||||||
|
@ -142,11 +142,7 @@ class FetchDownloadNotificationManager(private val context: Context) :
|
|||||||
else -> ACTION_TYPE_INVALID
|
else -> ACTION_TYPE_INVALID
|
||||||
}
|
}
|
||||||
intent.putExtra(EXTRA_ACTION_TYPE, action)
|
intent.putExtra(EXTRA_ACTION_TYPE, action)
|
||||||
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
val flags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
|
||||||
} else {
|
|
||||||
PendingIntent.FLAG_UPDATE_CURRENT
|
|
||||||
}
|
|
||||||
return PendingIntent.getBroadcast(
|
return PendingIntent.getBroadcast(
|
||||||
context,
|
context,
|
||||||
downloadNotification.notificationId + action,
|
downloadNotification.notificationId + action,
|
||||||
@ -166,11 +162,8 @@ class FetchDownloadNotificationManager(private val context: Context) :
|
|||||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||||
putExtra(DOWNLOAD_NOTIFICATION_TITLE, downloadNotification.title)
|
putExtra(DOWNLOAD_NOTIFICATION_TITLE, downloadNotification.title)
|
||||||
}
|
}
|
||||||
val pendingIntent = if (Build.VERSION.SDK_INT >= VERSION_CODES.M) {
|
val pendingIntent =
|
||||||
getActivity(context, 0, internal, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)
|
getActivity(context, 0, internal, FLAG_IMMUTABLE or FLAG_UPDATE_CURRENT)
|
||||||
} else {
|
|
||||||
getActivity(context, 0, internal, FLAG_UPDATE_CURRENT)
|
|
||||||
}
|
|
||||||
notificationBuilder.setContentIntent(pendingIntent)
|
notificationBuilder.setContentIntent(pendingIntent)
|
||||||
notificationBuilder.setAutoCancel(true)
|
notificationBuilder.setAutoCancel(true)
|
||||||
}
|
}
|
||||||
|
@ -21,8 +21,6 @@ package org.kiwix.kiwixmobile.core.extensions
|
|||||||
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 android.os.Build.VERSION
|
|
||||||
import android.os.Build.VERSION_CODES
|
|
||||||
import android.util.TypedValue
|
import android.util.TypedValue
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.annotation.AttrRes
|
import androidx.annotation.AttrRes
|
||||||
@ -53,11 +51,7 @@ fun Context.registerReceiver(baseBroadcastReceiver: BaseBroadcastReceiver): Inte
|
|||||||
registerReceiver(baseBroadcastReceiver, IntentFilter(baseBroadcastReceiver.action))
|
registerReceiver(baseBroadcastReceiver, IntentFilter(baseBroadcastReceiver.action))
|
||||||
|
|
||||||
val Context.locale: Locale
|
val Context.locale: Locale
|
||||||
get() =
|
get() = resources.configuration.locales.get(0)
|
||||||
if (VERSION.SDK_INT >= VERSION_CODES.N) resources.configuration.locales.get(0)
|
|
||||||
else
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
resources.configuration.locale
|
|
||||||
|
|
||||||
fun Context.getAttribute(@AttrRes attributeRes: Int) = with(TypedValue()) {
|
fun Context.getAttribute(@AttrRes attributeRes: Int) = with(TypedValue()) {
|
||||||
if (theme.resolveAttribute(attributeRes, this, true))
|
if (theme.resolveAttribute(attributeRes, this, true))
|
||||||
|
@ -1315,7 +1315,6 @@ abstract class CoreReaderFragment :
|
|||||||
if (sharedPreferenceUtil?.isPlayStoreBuildWithAndroid11OrAbove() == false &&
|
if (sharedPreferenceUtil?.isPlayStoreBuildWithAndroid11OrAbove() == false &&
|
||||||
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
|
Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU
|
||||||
) {
|
) {
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // For Marshmallow & higher API levels
|
|
||||||
if (requireActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
if (requireActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
== PackageManager.PERMISSION_GRANTED
|
== PackageManager.PERMISSION_GRANTED
|
||||||
) {
|
) {
|
||||||
@ -1323,9 +1322,6 @@ abstract class CoreReaderFragment :
|
|||||||
} else {
|
} else {
|
||||||
storagePermissionForNotesLauncher?.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
storagePermissionForNotesLauncher?.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||||
}
|
}
|
||||||
} else { // For Android versions below Marshmallow 6.0 (API 23)
|
|
||||||
isPermissionGranted = true // As already requested at install time
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
isPermissionGranted = true
|
isPermissionGranted = true
|
||||||
}
|
}
|
||||||
@ -1661,10 +1657,7 @@ abstract class CoreReaderFragment :
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun goToSearchWithText(intent: Intent) {
|
private fun goToSearchWithText(intent: Intent) {
|
||||||
val searchString =
|
val searchString = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
|
||||||
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
|
|
||||||
else ""
|
|
||||||
openSearch(
|
openSearch(
|
||||||
searchString,
|
searchString,
|
||||||
isOpenedFromTabView = false,
|
isOpenedFromTabView = false,
|
||||||
|
@ -23,7 +23,6 @@ import android.appwidget.AppWidgetManager
|
|||||||
import android.appwidget.AppWidgetProvider
|
import android.appwidget.AppWidgetProvider
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.os.Build
|
|
||||||
import android.widget.RemoteViews
|
import android.widget.RemoteViews
|
||||||
import org.kiwix.kiwixmobile.core.R
|
import org.kiwix.kiwixmobile.core.R
|
||||||
import kotlin.reflect.KClass
|
import kotlin.reflect.KClass
|
||||||
@ -50,21 +49,12 @@ abstract class CoreSearchWidget : AppWidgetProvider() {
|
|||||||
|
|
||||||
@SuppressLint("UnspecifiedImmutableFlag")
|
@SuppressLint("UnspecifiedImmutableFlag")
|
||||||
private fun pendingIntent(context: Context, action: String) =
|
private fun pendingIntent(context: Context, action: String) =
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
||||||
PendingIntent.getActivity(
|
PendingIntent.getActivity(
|
||||||
context,
|
context,
|
||||||
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
||||||
Intent(context, activityKClass.java).setAction(action),
|
Intent(context, activityKClass.java).setAction(action),
|
||||||
0 or PendingIntent.FLAG_IMMUTABLE
|
0 or PendingIntent.FLAG_IMMUTABLE
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
PendingIntent.getActivity(
|
|
||||||
context,
|
|
||||||
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
|
||||||
Intent(context, activityKClass.java).setAction(action),
|
|
||||||
0
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TEXT_CLICKED = "KiwixSearchWidget.TEXT_CLICKED"
|
const val TEXT_CLICKED = "KiwixSearchWidget.TEXT_CLICKED"
|
||||||
|
@ -18,8 +18,6 @@
|
|||||||
|
|
||||||
package org.kiwix.kiwixmobile.core.search.viewmodel.effects
|
package org.kiwix.kiwixmobile.core.search.viewmodel.effects
|
||||||
|
|
||||||
import android.annotation.TargetApi
|
|
||||||
import android.os.Build.VERSION_CODES
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import kotlinx.coroutines.channels.Channel
|
import kotlinx.coroutines.channels.Channel
|
||||||
@ -38,7 +36,6 @@ data class SearchArgumentProcessing(
|
|||||||
private val bundle: Bundle?,
|
private val bundle: Bundle?,
|
||||||
private val actions: Channel<Action>
|
private val actions: Channel<Action>
|
||||||
) : SideEffect<Unit> {
|
) : SideEffect<Unit> {
|
||||||
@TargetApi(VERSION_CODES.M)
|
|
||||||
override fun invokeWith(activity: AppCompatActivity) {
|
override fun invokeWith(activity: AppCompatActivity) {
|
||||||
bundle?.let {
|
bundle?.let {
|
||||||
actions.trySend(
|
actions.trySend(
|
||||||
|
@ -18,8 +18,6 @@
|
|||||||
package org.kiwix.kiwixmobile.core.utils
|
package org.kiwix.kiwixmobile.core.utils
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.os.Build.VERSION
|
|
||||||
import android.os.Build.VERSION_CODES
|
|
||||||
import android.text.Html
|
import android.text.Html
|
||||||
import android.text.Spanned
|
import android.text.Spanned
|
||||||
import android.util.AttributeSet
|
import android.util.AttributeSet
|
||||||
@ -43,14 +41,12 @@ object StyleUtils {
|
|||||||
return Xml.asAttributeSet(parser)
|
return Xml.asAttributeSet(parser)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("DEPRECATION")
|
|
||||||
@JvmStatic fun String?.fromHtml(): Spanned {
|
@JvmStatic fun String?.fromHtml(): Spanned {
|
||||||
return (this ?: "").let {
|
return (this ?: "").let {
|
||||||
if (VERSION.SDK_INT >= VERSION_CODES.N) Html.fromHtml(
|
Html.fromHtml(
|
||||||
this,
|
this,
|
||||||
Html.FROM_HTML_MODE_LEGACY
|
Html.FROM_HTML_MODE_LEGACY
|
||||||
)
|
)
|
||||||
else Html.fromHtml(this)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,12 +20,10 @@ package org.kiwix.kiwixmobile.core.utils
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.ConnectivityManager
|
import android.net.ConnectivityManager
|
||||||
import android.net.ConnectivityManager.TYPE_WIFI
|
|
||||||
import android.net.Network
|
import android.net.Network
|
||||||
import android.net.NetworkCapabilities
|
import android.net.NetworkCapabilities
|
||||||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
||||||
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
||||||
import android.net.NetworkInfo
|
|
||||||
import io.mockk.every
|
import io.mockk.every
|
||||||
import io.mockk.mockk
|
import io.mockk.mockk
|
||||||
import org.junit.Assert.assertEquals
|
import org.junit.Assert.assertEquals
|
||||||
@ -33,8 +31,7 @@ import org.junit.Assert.assertFalse
|
|||||||
import org.junit.Assert.assertTrue
|
import org.junit.Assert.assertTrue
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.kiwix.kiwixmobile.core.R
|
import org.kiwix.kiwixmobile.core.R
|
||||||
import org.kiwix.kiwixmobile.core.compat.CompatV21
|
import org.kiwix.kiwixmobile.core.compat.CompatV24
|
||||||
import org.kiwix.kiwixmobile.core.compat.CompatV23
|
|
||||||
import java.util.regex.Pattern
|
import java.util.regex.Pattern
|
||||||
|
|
||||||
class NetworkUtilsTest {
|
class NetworkUtilsTest {
|
||||||
@ -43,26 +40,10 @@ class NetworkUtilsTest {
|
|||||||
private val connectivity: ConnectivityManager = mockk()
|
private val connectivity: ConnectivityManager = mockk()
|
||||||
private val networkCapabilities: NetworkCapabilities = mockk()
|
private val networkCapabilities: NetworkCapabilities = mockk()
|
||||||
|
|
||||||
@Suppress("Deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
fun testNetworkAvailability_CompatV21() {
|
fun testNetworkAvailability_CompatV24() {
|
||||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
||||||
val compatV21 = CompatV21()
|
val compatV23 = CompatV24()
|
||||||
val networkInfo: NetworkInfo = mockk()
|
|
||||||
|
|
||||||
every { connectivity.allNetworkInfo } returns arrayOf(networkInfo)
|
|
||||||
every { networkInfo.state } returns NetworkInfo.State.CONNECTED
|
|
||||||
|
|
||||||
assertTrue(compatV21.isNetworkAvailable(connectivity))
|
|
||||||
|
|
||||||
every { networkInfo.state } returns NetworkInfo.State.DISCONNECTED
|
|
||||||
assertFalse(compatV21.isNetworkAvailable(connectivity))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testNetworkAvailability_CompatV23() {
|
|
||||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
|
||||||
val compatV23 = CompatV23()
|
|
||||||
val network: Network = mockk()
|
val network: Network = mockk()
|
||||||
|
|
||||||
every { connectivity.activeNetwork } returns network
|
every { connectivity.activeNetwork } returns network
|
||||||
@ -74,25 +55,9 @@ class NetworkUtilsTest {
|
|||||||
assertFalse(compatV23.isNetworkAvailable(connectivity))
|
assertFalse(compatV23.isNetworkAvailable(connectivity))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("Deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
fun test_isWifi_CompatV21() {
|
fun test_isWifi_CompatV24() {
|
||||||
val compatV21 = CompatV21()
|
val compatV23 = CompatV24()
|
||||||
val networkInfo: NetworkInfo = mockk()
|
|
||||||
|
|
||||||
every { connectivity.getNetworkInfo(TYPE_WIFI) } returns networkInfo
|
|
||||||
every { networkInfo.type } returns ConnectivityManager.TYPE_WIFI
|
|
||||||
every { networkInfo.isConnected } returns true
|
|
||||||
|
|
||||||
assertTrue(compatV21.isWifi(connectivity))
|
|
||||||
|
|
||||||
every { networkInfo.isConnected } returns false
|
|
||||||
assertFalse(compatV21.isWifi(connectivity))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun test_isWifi_CompatV23() {
|
|
||||||
val compatV23 = CompatV23()
|
|
||||||
val network: Network = mockk()
|
val network: Network = mockk()
|
||||||
|
|
||||||
every { connectivity.activeNetwork } returns network
|
every { connectivity.activeNetwork } returns network
|
||||||
@ -104,21 +69,10 @@ class NetworkUtilsTest {
|
|||||||
assertFalse(compatV23.isWifi(connectivity))
|
assertFalse(compatV23.isWifi(connectivity))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Suppress("Deprecation")
|
|
||||||
@Test
|
@Test
|
||||||
fun testNetworkAvailability_NoNetwork_CompatV21() {
|
fun testNetworkAvailability_NoNetwork_CompatV24() {
|
||||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
||||||
val compatV21 = CompatV21()
|
val compatV23 = CompatV24()
|
||||||
|
|
||||||
every { connectivity.allNetworkInfo } returns arrayOf()
|
|
||||||
|
|
||||||
assertFalse(compatV21.isNetworkAvailable(connectivity))
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun testNetworkAvailability_NoNetwork_CompatV23() {
|
|
||||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
|
||||||
val compatV23 = CompatV23()
|
|
||||||
val network: Network = mockk()
|
val network: Network = mockk()
|
||||||
|
|
||||||
every { connectivity.activeNetwork } returns network
|
every { connectivity.activeNetwork } returns network
|
||||||
|
@ -46,4 +46,5 @@
|
|||||||
</issue>
|
</issue>
|
||||||
<issue id="UnusedIds" severity="warning" />
|
<issue id="UnusedIds" severity="warning" />
|
||||||
<issue id="DataExtractionRules" severity="warning" />
|
<issue id="DataExtractionRules" severity="warning" />
|
||||||
|
<issue id="ObsoleteSdkInt" severity="warning" />
|
||||||
</lint>
|
</lint>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user