From 5224fa656cb8802c12959e1135c8275adab57099 Mon Sep 17 00:00:00 2001 From: kr328 Date: Sun, 14 Nov 2021 20:33:37 +0800 Subject: [PATCH] Chore: clean compat code --- .../com/github/kr328/clash/BaseActivity.kt | 4 +-- .../com/github/kr328/clash/LogcatService.kt | 17 ++++------- .../kr328/clash/common/compat/Context.kt | 7 +---- .../github/kr328/clash/common/compat/Html.kt | 2 +- .../kr328/clash/common/compat/Intents.kt | 2 +- .../kr328/clash/common/compat/Package.kt | 2 +- .../kr328/clash/common/compat/Resource.kt | 2 +- .../kr328/clash/common/compat/Services.kt | 2 +- .../kr328/clash/service/ProfileWorker.kt | 30 +++++++------------ .../clash/module/StaticNotificationModule.kt | 13 +++----- .../clash/service/clash/module/TunModule.kt | 2 +- 11 files changed, 30 insertions(+), 53 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt index 4347756a..1671004b 100644 --- a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt @@ -234,12 +234,12 @@ abstract class BaseActivity> : window.statusBarColor = resolveThemedColor(android.R.attr.statusBarColor) window.navigationBarColor = resolveThemedColor(android.R.attr.navigationBarColor) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (Build.VERSION.SDK_INT >= 23) { window.isLightStatusBarsCompat = resolveThemedBoolean(android.R.attr.windowLightStatusBar) } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { + if (Build.VERSION.SDK_INT >= 27) { window.isLightNavigationBarCompat = resolveThemedBoolean(android.R.attr.windowLightNavigationBar) } diff --git a/app/src/main/java/com/github/kr328/clash/LogcatService.kt b/app/src/main/java/com/github/kr328/clash/LogcatService.kt index 4484f86a..da0673d3 100644 --- a/app/src/main/java/com/github/kr328/clash/LogcatService.kt +++ b/app/src/main/java/com/github/kr328/clash/LogcatService.kt @@ -1,7 +1,5 @@ package com.github.kr328.clash -import android.app.NotificationChannel -import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.content.ComponentName @@ -9,12 +7,13 @@ import android.content.Context import android.content.Intent import android.content.ServiceConnection import android.os.Binder -import android.os.Build import android.os.IBinder import android.os.IInterface +import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.github.kr328.clash.common.compat.getColorCompat +import com.github.kr328.clash.common.compat.pendingIntentFlags import com.github.kr328.clash.common.log.Log import com.github.kr328.clash.common.util.intent import com.github.kr328.clash.core.model.LogMessage @@ -126,16 +125,12 @@ class LogcatService : Service(), CoroutineScope by CoroutineScope(Dispatchers.De } private fun createNotificationChannel() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - return - NotificationManagerCompat.from(this) .createNotificationChannel( - NotificationChannel( + NotificationChannelCompat.Builder( CHANNEL_ID, - getString(R.string.clash_logcat), - NotificationManager.IMPORTANCE_DEFAULT - ) + NotificationManagerCompat.IMPORTANCE_DEFAULT + ).setName(getString(R.string.clash_logcat)).build() ) } @@ -152,7 +147,7 @@ class LogcatService : Service(), CoroutineScope by CoroutineScope(Dispatchers.De R.id.nf_logcat_status, LogcatActivity::class.intent .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP), - PendingIntent.FLAG_UPDATE_CURRENT + pendingIntentFlags(PendingIntent.FLAG_UPDATE_CURRENT) ) ) .build() diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Context.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Context.kt index 2015f05e..cc4b7c1d 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Context.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Context.kt @@ -4,17 +4,12 @@ package com.github.kr328.clash.common.compat import android.content.Context import android.graphics.drawable.Drawable -import android.os.Build import androidx.annotation.ColorRes import androidx.annotation.DrawableRes import androidx.core.content.ContextCompat fun Context.getColorCompat(@ColorRes id: Int): Int { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - this.getColor(id) - } else { - resources.getColor(id) - } + return ContextCompat.getColor(this, id) } fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable? { diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Html.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Html.kt index 2e88fe31..deb2cb12 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Html.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Html.kt @@ -7,7 +7,7 @@ import android.text.Html import android.text.Spanned fun fromHtmlCompat(content: String): Spanned { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return if (Build.VERSION.SDK_INT >= 24) { Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT) } else { Html.fromHtml(content) diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Intents.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Intents.kt index 8319542f..dae74630 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Intents.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Intents.kt @@ -4,7 +4,7 @@ import android.app.PendingIntent import android.os.Build fun pendingIntentFlags(flags: Int, mutable: Boolean = false): Int { - return if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) { + return if (Build.VERSION.SDK_INT >= 24) { if (Build.VERSION.SDK_INT > 30 && mutable) { flags or PendingIntent.FLAG_MUTABLE } else { diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Package.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Package.kt index 70f26b03..f1d119a9 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Package.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Package.kt @@ -6,7 +6,7 @@ import android.content.pm.PackageInfo val PackageInfo.versionCodeCompat: Long get() { - return if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) { + return if (android.os.Build.VERSION.SDK_INT >= 28) { longVersionCode } else { versionCode.toLong() diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Resource.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Resource.kt index a8b76fb1..a478f856 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Resource.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Resource.kt @@ -8,7 +8,7 @@ import java.util.* val Configuration.preferredLocale: Locale get() { - return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { + return if (Build.VERSION.SDK_INT >= 24) { locales[0] } else { locale diff --git a/common/src/main/java/com/github/kr328/clash/common/compat/Services.kt b/common/src/main/java/com/github/kr328/clash/common/compat/Services.kt index 340846e9..5eded773 100644 --- a/common/src/main/java/com/github/kr328/clash/common/compat/Services.kt +++ b/common/src/main/java/com/github/kr328/clash/common/compat/Services.kt @@ -5,7 +5,7 @@ import android.content.Intent import android.os.Build fun Context.startForegroundServiceCompat(intent: Intent) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= 26) { startForegroundService(intent) } else { startService(intent) diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt index ce52fdd8..e810b315 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt @@ -1,12 +1,10 @@ package com.github.kr328.clash.service -import android.app.NotificationChannel -import android.app.NotificationManager import android.app.PendingIntent import android.content.Intent import android.os.Binder -import android.os.Build import android.os.IBinder +import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.github.kr328.clash.common.compat.getColorCompat @@ -95,26 +93,20 @@ class ProfileWorker : BaseService() { } private fun createChannels() { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - return - - NotificationManagerCompat.from(this).createNotificationChannels( + NotificationManagerCompat.from(this).createNotificationChannelsCompat( listOf( - NotificationChannel( + NotificationChannelCompat.Builder( SERVICE_CHANNEL, - getString(R.string.profile_service_status), - NotificationManager.IMPORTANCE_LOW - ), - NotificationChannel( + NotificationManagerCompat.IMPORTANCE_LOW + ).setName(getString(R.string.profile_service_status)).build(), + NotificationChannelCompat.Builder( STATUS_CHANNEL, - getString(R.string.profile_process_status), - NotificationManager.IMPORTANCE_LOW - ), - NotificationChannel( + NotificationManagerCompat.IMPORTANCE_LOW + ).setName(getString(R.string.profile_process_status)).build(), + NotificationChannelCompat.Builder( RESULT_CHANNEL, - getString(R.string.profile_process_result), - NotificationManager.IMPORTANCE_DEFAULT - ) + NotificationManagerCompat.IMPORTANCE_DEFAULT + ).setName(getString(R.string.profile_process_result)).build() ) ) } diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt index 01ec3be2..78120e89 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/StaticNotificationModule.kt @@ -1,11 +1,9 @@ package com.github.kr328.clash.service.clash.module -import android.app.NotificationChannel -import android.app.NotificationManager import android.app.PendingIntent import android.app.Service import android.content.Intent -import android.os.Build +import androidx.core.app.NotificationChannelCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.github.kr328.clash.common.compat.getColorCompat @@ -57,14 +55,11 @@ class StaticNotificationModule(service: Service) : Module(service) { const val CHANNEL_ID = "clash_status_channel" fun createNotificationChannel(service: Service) { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) - return NotificationManagerCompat.from(service).createNotificationChannel( - NotificationChannel( + NotificationChannelCompat.Builder( CHANNEL_ID, - service.getText(R.string.clash_service_status_channel), - NotificationManager.IMPORTANCE_LOW - ) + NotificationManagerCompat.IMPORTANCE_LOW + ).setName(service.getText(R.string.clash_service_status_channel)).build() ) } diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt index 2be2413f..e2b38f59 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt @@ -28,7 +28,7 @@ class TunModule(private val vpn: VpnService) : Module(vpn) { source: InetSocketAddress, target: InetSocketAddress, ): Int { - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) + if (Build.VERSION.SDK_INT < 29) return -1 return runCatching { connectivity.getConnectionOwnerUid(protocol, source, target) }