Refactor(minSdk): Upgrade minSdk from 21 to 24

This commit is contained in:
2024-10-11 09:43:54 +08:00
parent 3979027471
commit cd97f6156f
9 changed files with 17 additions and 43 deletions

View File

@@ -35,12 +35,12 @@ plugins {
android { android {
namespace = "top.fatweb.oxygen.toolbox" namespace = "top.fatweb.oxygen.toolbox"
compileSdk = 34 compileSdk = 35
defaultConfig { defaultConfig {
applicationId = "top.fatweb.oxygen.toolbox" applicationId = "top.fatweb.oxygen.toolbox"
minSdk = 21 minSdk = 24
targetSdk = 34 targetSdk = 35
versionCode = baseVersionCode versionCode = baseVersionCode
versionName = "$baseVersionName${ versionName = "$baseVersionName${
if (baseVersionCode % 100 != 0) ".${ if (baseVersionCode % 100 != 0) ".${

View File

@@ -57,14 +57,8 @@ internal class ConnectivityManagerNetworkMonitor @Inject constructor(
} }
.conflate() .conflate()
@Suppress("DEPRECATION")
private fun ConnectivityManager.isCurrentlyConnected() = private fun ConnectivityManager.isCurrentlyConnected() =
when {
VERSION.SDK_INT >= VERSION_CODES.M ->
activeNetwork activeNetwork
?.let(::getNetworkCapabilities) ?.let(::getNetworkCapabilities)
?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) ?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true
else -> activeNetworkInfo?.isConnected
} ?: false
} }

View File

@@ -293,5 +293,5 @@ private fun OxygenNavRail(
private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) = private fun NavDestination?.isTopLevelDestinationInHierarchy(destination: TopLevelDestination) =
this?.hierarchy?.any { this?.hierarchy?.any {
it.route?.equals(destination.route) ?: false it.route?.equals(destination.route) == true
} ?: false } == true

View File

@@ -221,7 +221,7 @@ fun Scrollbar(
withTimeout(viewConfiguration.longPressTimeoutMillis) { withTimeout(viewConfiguration.longPressTimeoutMillis) {
tryAwaitRelease() tryAwaitRelease()
} }
} catch (e: TimeoutCancellationException) { } catch (_: TimeoutCancellationException) {
// Start the press triggered scroll // Start the press triggered scroll
val initialPress = PressInteraction.Press(offset) val initialPress = PressInteraction.Press(offset)
interactionSource?.tryEmit(initialPress) interactionSource?.tryEmit(initialPress)

View File

@@ -2,9 +2,7 @@ package top.fatweb.oxygen.toolbox.ui.util
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.os.Build
import android.os.LocaleList import android.os.LocaleList
import androidx.annotation.RequiresApi
import top.fatweb.oxygen.toolbox.model.userdata.LanguageConfig import top.fatweb.oxygen.toolbox.model.userdata.LanguageConfig
import java.util.Locale import java.util.Locale
@@ -24,11 +22,7 @@ object LocaleUtils {
fun attachBaseContext(context: Context, languageConfig: LanguageConfig): Context { fun attachBaseContext(context: Context, languageConfig: LanguageConfig): Context {
val locale: Locale = getLocaleFromLanguageConfig(languageConfig) val locale: Locale = getLocaleFromLanguageConfig(languageConfig)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return createConfigurationContext(context, locale)
createConfigurationContext(context, locale)
} else {
updateConfiguration(context, locale)
}
} }
private fun getLocaleFromLanguageConfig(languageConfig: LanguageConfig): Locale = private fun getLocaleFromLanguageConfig(languageConfig: LanguageConfig): Locale =
@@ -38,21 +32,10 @@ object LocaleUtils {
LanguageConfig.English -> Locale("en") LanguageConfig.English -> Locale("en")
} }
@RequiresApi(Build.VERSION_CODES.N)
private fun createConfigurationContext(context: Context, locale: Locale): Context { private fun createConfigurationContext(context: Context, locale: Locale): Context {
val configuration = context.resources.configuration val configuration = context.resources.configuration
configuration.setLocales(LocaleList(locale)) configuration.setLocales(LocaleList(locale))
return context.createConfigurationContext(configuration) return context.createConfigurationContext(configuration)
} }
@Suppress("DEPRECATION")
private fun updateConfiguration(context: Context, locale: Locale): Context {
val resources = context.resources
val configuration = resources.configuration
configuration.locale = locale
resources.updateConfiguration(configuration, resources.displayMetrics)
return context
}
} }

View File

@@ -13,10 +13,7 @@ import java.util.Locale
object ResourcesUtils { object ResourcesUtils {
private fun getConfiguration(context: Context): Configuration = context.resources.configuration private fun getConfiguration(context: Context): Configuration = context.resources.configuration
@Suppress("DEPRECATION") fun getAppLocale(context: Context): Locale = getConfiguration(context).locales.get(0)
fun getAppLocale(context: Context): Locale =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) getConfiguration(context).locales.get(0)
else getConfiguration(context).locale
fun getSystemLocale(): LocaleListCompat = fun getSystemLocale(): LocaleListCompat =
ConfigurationCompat.getLocales(Resources.getSystem().configuration) ConfigurationCompat.getLocales(Resources.getSystem().configuration)
@@ -24,7 +21,7 @@ object ResourcesUtils {
fun getAppVersionName(context: Context): String = fun getAppVersionName(context: Context): String =
try { try {
context.packageManager.getPackageInfo(context.packageName, 0)?.versionName ?: "Unknown" context.packageManager.getPackageInfo(context.packageName, 0)?.versionName ?: "Unknown"
} catch (e: PackageManager.NameNotFoundException) { } catch (_: PackageManager.NameNotFoundException) {
"Unknown" "Unknown"
} }
@@ -37,7 +34,7 @@ object ResourcesUtils {
context.packageName, context.packageName,
0 0
)?.versionCode?.toLong() ?: -1 )?.versionCode?.toLong() ?: -1
} catch (e: PackageManager.NameNotFoundException) { } catch (_: PackageManager.NameNotFoundException) {
-1 -1
} }

View File

@@ -121,7 +121,7 @@ private suspend fun FlowCollector<ToolViewUiState>.emitResult(
is Result.Loading -> ToolViewUiState.Loading is Result.Loading -> ToolViewUiState.Loading
is Result.Error -> { is Result.Error -> {
Timber.e("Can not load tool", result.exception) Timber.e(result.exception, "Can not load tool")
ToolViewUiState.Error ToolViewUiState.Error
} }

View File

@@ -30,7 +30,7 @@ class NativeWebApi(
return clipboardManager?.let { return clipboardManager?.let {
it.setPrimaryClip(ClipData.newPlainText("copy", text)) it.setPrimaryClip(ClipData.newPlainText("copy", text))
true true
} ?: false } == true
} }
@JavascriptInterface @JavascriptInterface
@@ -95,7 +95,7 @@ class NativeWebApi(
} }
true true
} catch (e: IOException) { } catch (e: IOException) {
Timber.e("Could not save file $fileName to ${file.absolutePath}", e) Timber.e(e, "Could not save file $fileName to ${file.absolutePath}")
false false
} }
} }

View File

@@ -17,7 +17,7 @@ object Permissions {
context: Context, context: Context,
permissionLauncher: ManagedActivityResultLauncher<String, Boolean> permissionLauncher: ManagedActivityResultLauncher<String, Boolean>
): Boolean { ): Boolean {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return true return true
} }