Chore: clean compat code

This commit is contained in:
kr328 2021-11-14 20:33:37 +08:00
parent 73550164a8
commit 5224fa656c
11 changed files with 30 additions and 53 deletions

View File

@ -234,12 +234,12 @@ abstract class BaseActivity<D : Design<*>> :
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)
}

View File

@ -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()

View File

@ -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? {

View File

@ -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)

View File

@ -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 {

View File

@ -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()

View File

@ -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

View File

@ -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)

View File

@ -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()
)
)
}

View File

@ -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<Unit>(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()
)
}

View File

@ -28,7 +28,7 @@ class TunModule(private val vpn: VpnService) : Module<Unit>(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) }