mirror of
https://github.com/kiwix/kiwix-android.git
synced 2025-08-03 10:46:53 -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
f7517a2272
commit
632bb4a528
@ -18,8 +18,6 @@
|
||||
|
||||
package org.kiwix.kiwixmobile.zimManager.fileselectView.effects
|
||||
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.core.os.bundleOf
|
||||
@ -33,15 +31,11 @@ data class ShareFiles(private val selectedBooks: List<BookOnDisk>) :
|
||||
SideEffect<Unit> {
|
||||
override fun invokeWith(activity: AppCompatActivity) {
|
||||
val selectedFileContentURIs = selectedBooks.mapNotNull {
|
||||
if (Build.VERSION.SDK_INT >= 24) {
|
||||
FileProvider.getUriForFile(
|
||||
activity,
|
||||
activity.packageName + ".fileprovider",
|
||||
it.file
|
||||
)
|
||||
} else {
|
||||
Uri.fromFile(it.file)
|
||||
}
|
||||
FileProvider.getUriForFile(
|
||||
activity,
|
||||
activity.packageName + ".fileprovider",
|
||||
it.file
|
||||
)
|
||||
}
|
||||
activity.navigate(
|
||||
R.id.localFileTransferFragment,
|
||||
|
@ -90,9 +90,7 @@ abstract class CoreApp : Application() {
|
||||
if (BuildConfig.DEBUG) {
|
||||
StrictMode.setThreadPolicy(
|
||||
StrictMode.ThreadPolicy.Builder().apply {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
detectResourceMismatches()
|
||||
}
|
||||
detectResourceMismatches()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
detectUnbufferedIo()
|
||||
}
|
||||
@ -106,9 +104,7 @@ abstract class CoreApp : Application() {
|
||||
)
|
||||
StrictMode.setVmPolicy(
|
||||
VmPolicy.Builder().apply {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
detectCleartextNetwork()
|
||||
}
|
||||
detectCleartextNetwork()
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
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
|
||||
* 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`
|
||||
|
@ -29,8 +29,7 @@ class CompatHelper private constructor() {
|
||||
// Note: Needs ": Compat" or the type system assumes `Compat21`
|
||||
private val compatValue: Compat = when {
|
||||
sdkVersion >= Build.VERSION_CODES.TIRAMISU -> CompatV33()
|
||||
sdkVersion >= Build.VERSION_CODES.M -> CompatV23()
|
||||
else -> CompatV21()
|
||||
else -> CompatV24()
|
||||
}
|
||||
|
||||
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/>.
|
||||
*
|
||||
*/
|
||||
@file:Suppress("DEPRECATION")
|
||||
|
||||
package org.kiwix.kiwixmobile.core.compat
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInfo
|
||||
import android.content.pm.PackageManager
|
||||
@ -27,24 +27,19 @@ import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
||||
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
||||
|
||||
const val API_23 = 23
|
||||
|
||||
@TargetApi(API_23)
|
||||
open class CompatV23 : Compat {
|
||||
private val compatV21 = CompatV21()
|
||||
open class CompatV24 : Compat {
|
||||
|
||||
override fun queryIntentActivities(
|
||||
packageManager: PackageManager,
|
||||
intent: Intent,
|
||||
flags: ResolveInfoFlagsCompat
|
||||
): List<ResolveInfo> =
|
||||
compatV21.queryIntentActivities(packageManager, intent, flags)
|
||||
): List<ResolveInfo> = packageManager.queryIntentActivities(intent, flags.value.toInt())
|
||||
|
||||
override fun getPackageInformation(
|
||||
packageName: String,
|
||||
packageManager: PackageManager,
|
||||
flag: Int
|
||||
): PackageInfo = compatV21.getPackageInformation(packageName, packageManager, flag)
|
||||
): PackageInfo = packageManager.getPackageInfo(packageName, 0)
|
||||
|
||||
/**
|
||||
* Checks if the device has a network connection with internet access.
|
@ -30,7 +30,7 @@ const val API_33 = 33
|
||||
|
||||
@TargetApi(API_33)
|
||||
open class CompatV33 : Compat {
|
||||
private val compatV23 = CompatV23()
|
||||
private val compatV24 = CompatV24()
|
||||
override fun queryIntentActivities(
|
||||
packageManager: PackageManager,
|
||||
intent: Intent,
|
||||
@ -48,8 +48,8 @@ open class CompatV33 : Compat {
|
||||
packageManager.getPackageInfo(packageName, PackageInfoFlags.of(flag.toLong()))
|
||||
|
||||
override fun isNetworkAvailable(connectivity: ConnectivityManager): Boolean =
|
||||
compatV23.isNetworkAvailable(connectivity)
|
||||
compatV24.isNetworkAvailable(connectivity)
|
||||
|
||||
override fun isWifi(connectivity: ConnectivityManager): Boolean =
|
||||
compatV23.isWifi(connectivity)
|
||||
compatV24.isWifi(connectivity)
|
||||
}
|
||||
|
@ -146,11 +146,7 @@ class FetchDownloadNotificationManager(private val context: Context) :
|
||||
else -> ACTION_TYPE_INVALID
|
||||
}
|
||||
intent.putExtra(EXTRA_ACTION_TYPE, action)
|
||||
val flags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
} else {
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
}
|
||||
val flags = PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
|
||||
return PendingIntent.getBroadcast(
|
||||
context,
|
||||
downloadNotification.notificationId + action,
|
||||
@ -170,11 +166,8 @@ class FetchDownloadNotificationManager(private val context: Context) :
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
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)
|
||||
} else {
|
||||
getActivity(context, 0, internal, FLAG_UPDATE_CURRENT)
|
||||
}
|
||||
notificationBuilder.setContentIntent(pendingIntent)
|
||||
notificationBuilder.setAutoCancel(true)
|
||||
}
|
||||
|
@ -21,8 +21,6 @@ package org.kiwix.kiwixmobile.core.extensions
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.IntentFilter
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.util.TypedValue
|
||||
import android.widget.Toast
|
||||
import androidx.annotation.AttrRes
|
||||
@ -53,11 +51,7 @@ fun Context.registerReceiver(baseBroadcastReceiver: BaseBroadcastReceiver): Inte
|
||||
registerReceiver(baseBroadcastReceiver, IntentFilter(baseBroadcastReceiver.action))
|
||||
|
||||
val Context.locale: Locale
|
||||
get() =
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.N) resources.configuration.locales.get(0)
|
||||
else
|
||||
@Suppress("DEPRECATION")
|
||||
resources.configuration.locale
|
||||
get() = resources.configuration.locales.get(0)
|
||||
|
||||
fun Context.getAttribute(@AttrRes attributeRes: Int) = with(TypedValue()) {
|
||||
if (theme.resolveAttribute(attributeRes, this, true))
|
||||
|
@ -1315,16 +1315,12 @@ abstract class CoreReaderFragment :
|
||||
if (sharedPreferenceUtil?.isPlayStoreBuildWithAndroid11OrAbove() == false &&
|
||||
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)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
isPermissionGranted = true
|
||||
} else {
|
||||
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
|
||||
if (requireActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED
|
||||
) {
|
||||
isPermissionGranted = true
|
||||
} else {
|
||||
storagePermissionForNotesLauncher?.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
}
|
||||
} else {
|
||||
isPermissionGranted = true
|
||||
@ -1665,10 +1661,7 @@ abstract class CoreReaderFragment :
|
||||
}
|
||||
|
||||
private fun goToSearchWithText(intent: Intent) {
|
||||
val searchString =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
|
||||
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
|
||||
else ""
|
||||
val searchString = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
|
||||
openSearch(
|
||||
searchString,
|
||||
isOpenedFromTabView = false,
|
||||
|
@ -23,7 +23,6 @@ import android.appwidget.AppWidgetManager
|
||||
import android.appwidget.AppWidgetProvider
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.widget.RemoteViews
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import kotlin.reflect.KClass
|
||||
@ -50,21 +49,12 @@ abstract class CoreSearchWidget : AppWidgetProvider() {
|
||||
|
||||
@SuppressLint("UnspecifiedImmutableFlag")
|
||||
private fun pendingIntent(context: Context, action: String) =
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
||||
Intent(context, activityKClass.java).setAction(action),
|
||||
0 or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
} else {
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
||||
Intent(context, activityKClass.java).setAction(action),
|
||||
0
|
||||
)
|
||||
}
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
(System.currentTimeMillis() % Int.MAX_VALUE).toInt(),
|
||||
Intent(context, activityKClass.java).setAction(action),
|
||||
0 or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
companion object {
|
||||
const val TEXT_CLICKED = "KiwixSearchWidget.TEXT_CLICKED"
|
||||
|
@ -18,8 +18,6 @@
|
||||
|
||||
package org.kiwix.kiwixmobile.core.search.viewmodel.effects
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.os.Bundle
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
@ -38,7 +36,6 @@ data class SearchArgumentProcessing(
|
||||
private val bundle: Bundle?,
|
||||
private val actions: Channel<Action>
|
||||
) : SideEffect<Unit> {
|
||||
@TargetApi(VERSION_CODES.M)
|
||||
override fun invokeWith(activity: AppCompatActivity) {
|
||||
bundle?.let {
|
||||
actions.trySend(
|
||||
|
@ -18,8 +18,6 @@
|
||||
package org.kiwix.kiwixmobile.core.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Build.VERSION_CODES
|
||||
import android.text.Html
|
||||
import android.text.Spanned
|
||||
import android.util.AttributeSet
|
||||
@ -43,14 +41,12 @@ object StyleUtils {
|
||||
return Xml.asAttributeSet(parser)
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
@JvmStatic fun String?.fromHtml(): Spanned {
|
||||
return (this ?: "").let {
|
||||
if (VERSION.SDK_INT >= VERSION_CODES.N) Html.fromHtml(
|
||||
Html.fromHtml(
|
||||
this,
|
||||
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.net.ConnectivityManager
|
||||
import android.net.ConnectivityManager.TYPE_WIFI
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET
|
||||
import android.net.NetworkCapabilities.TRANSPORT_WIFI
|
||||
import android.net.NetworkInfo
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import org.junit.Assert.assertEquals
|
||||
@ -33,8 +31,7 @@ import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.kiwix.kiwixmobile.core.R
|
||||
import org.kiwix.kiwixmobile.core.compat.CompatV21
|
||||
import org.kiwix.kiwixmobile.core.compat.CompatV23
|
||||
import org.kiwix.kiwixmobile.core.compat.CompatV24
|
||||
import java.util.regex.Pattern
|
||||
|
||||
class NetworkUtilsTest {
|
||||
@ -43,26 +40,10 @@ class NetworkUtilsTest {
|
||||
private val connectivity: ConnectivityManager = mockk()
|
||||
private val networkCapabilities: NetworkCapabilities = mockk()
|
||||
|
||||
@Suppress("Deprecation")
|
||||
@Test
|
||||
fun testNetworkAvailability_CompatV21() {
|
||||
fun testNetworkAvailability_CompatV24() {
|
||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
||||
val compatV21 = CompatV21()
|
||||
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 compatV23 = CompatV24()
|
||||
val network: Network = mockk()
|
||||
|
||||
every { connectivity.activeNetwork } returns network
|
||||
@ -74,25 +55,9 @@ class NetworkUtilsTest {
|
||||
assertFalse(compatV23.isNetworkAvailable(connectivity))
|
||||
}
|
||||
|
||||
@Suppress("Deprecation")
|
||||
@Test
|
||||
fun test_isWifi_CompatV21() {
|
||||
val compatV21 = CompatV21()
|
||||
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()
|
||||
fun test_isWifi_CompatV24() {
|
||||
val compatV23 = CompatV24()
|
||||
val network: Network = mockk()
|
||||
|
||||
every { connectivity.activeNetwork } returns network
|
||||
@ -104,21 +69,10 @@ class NetworkUtilsTest {
|
||||
assertFalse(compatV23.isWifi(connectivity))
|
||||
}
|
||||
|
||||
@Suppress("Deprecation")
|
||||
@Test
|
||||
fun testNetworkAvailability_NoNetwork_CompatV21() {
|
||||
fun testNetworkAvailability_NoNetwork_CompatV24() {
|
||||
every { context.getSystemService(Context.CONNECTIVITY_SERVICE) } returns connectivity
|
||||
val compatV21 = CompatV21()
|
||||
|
||||
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 compatV23 = CompatV24()
|
||||
val network: Network = mockk()
|
||||
|
||||
every { connectivity.activeNetwork } returns network
|
||||
|
@ -46,4 +46,5 @@
|
||||
</issue>
|
||||
<issue id="UnusedIds" severity="warning" />
|
||||
<issue id="DataExtractionRules" severity="warning" />
|
||||
<issue id="ObsoleteSdkInt" severity="warning" />
|
||||
</lint>
|
||||
|
Loading…
x
Reference in New Issue
Block a user