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.statusBarColor = resolveThemedColor(android.R.attr.statusBarColor)
window.navigationBarColor = resolveThemedColor(android.R.attr.navigationBarColor) window.navigationBarColor = resolveThemedColor(android.R.attr.navigationBarColor)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= 23) {
window.isLightStatusBarsCompat = window.isLightStatusBarsCompat =
resolveThemedBoolean(android.R.attr.windowLightStatusBar) resolveThemedBoolean(android.R.attr.windowLightStatusBar)
} }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { if (Build.VERSION.SDK_INT >= 27) {
window.isLightNavigationBarCompat = window.isLightNavigationBarCompat =
resolveThemedBoolean(android.R.attr.windowLightNavigationBar) resolveThemedBoolean(android.R.attr.windowLightNavigationBar)
} }

View File

@ -1,7 +1,5 @@
package com.github.kr328.clash package com.github.kr328.clash
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.ComponentName import android.content.ComponentName
@ -9,12 +7,13 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.content.ServiceConnection import android.content.ServiceConnection
import android.os.Binder import android.os.Binder
import android.os.Build
import android.os.IBinder import android.os.IBinder
import android.os.IInterface import android.os.IInterface
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import com.github.kr328.clash.common.compat.getColorCompat 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.log.Log
import com.github.kr328.clash.common.util.intent import com.github.kr328.clash.common.util.intent
import com.github.kr328.clash.core.model.LogMessage import com.github.kr328.clash.core.model.LogMessage
@ -126,16 +125,12 @@ class LogcatService : Service(), CoroutineScope by CoroutineScope(Dispatchers.De
} }
private fun createNotificationChannel() { private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return
NotificationManagerCompat.from(this) NotificationManagerCompat.from(this)
.createNotificationChannel( .createNotificationChannel(
NotificationChannel( NotificationChannelCompat.Builder(
CHANNEL_ID, CHANNEL_ID,
getString(R.string.clash_logcat), NotificationManagerCompat.IMPORTANCE_DEFAULT
NotificationManager.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, R.id.nf_logcat_status,
LogcatActivity::class.intent LogcatActivity::class.intent
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP), .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() .build()

View File

@ -4,17 +4,12 @@ package com.github.kr328.clash.common.compat
import android.content.Context import android.content.Context
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.os.Build
import androidx.annotation.ColorRes import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
fun Context.getColorCompat(@ColorRes id: Int): Int { fun Context.getColorCompat(@ColorRes id: Int): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { return ContextCompat.getColor(this, id)
this.getColor(id)
} else {
resources.getColor(id)
}
} }
fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable? { fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable? {

View File

@ -7,7 +7,7 @@ import android.text.Html
import android.text.Spanned import android.text.Spanned
fun fromHtmlCompat(content: String): 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) Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT)
} else { } else {
Html.fromHtml(content) Html.fromHtml(content)

View File

@ -4,7 +4,7 @@ import android.app.PendingIntent
import android.os.Build import android.os.Build
fun pendingIntentFlags(flags: Int, mutable: Boolean = false): Int { 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) { if (Build.VERSION.SDK_INT > 30 && mutable) {
flags or PendingIntent.FLAG_MUTABLE flags or PendingIntent.FLAG_MUTABLE
} else { } else {

View File

@ -6,7 +6,7 @@ import android.content.pm.PackageInfo
val PackageInfo.versionCodeCompat: Long val PackageInfo.versionCodeCompat: Long
get() { 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 longVersionCode
} else { } else {
versionCode.toLong() versionCode.toLong()

View File

@ -8,7 +8,7 @@ import java.util.*
val Configuration.preferredLocale: Locale val Configuration.preferredLocale: Locale
get() { get() {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { return if (Build.VERSION.SDK_INT >= 24) {
locales[0] locales[0]
} else { } else {
locale locale

View File

@ -5,7 +5,7 @@ import android.content.Intent
import android.os.Build import android.os.Build
fun Context.startForegroundServiceCompat(intent: Intent) { fun Context.startForegroundServiceCompat(intent: Intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= 26) {
startForegroundService(intent) startForegroundService(intent)
} else { } else {
startService(intent) startService(intent)

View File

@ -1,12 +1,10 @@
package com.github.kr328.clash.service package com.github.kr328.clash.service
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.content.Intent import android.content.Intent
import android.os.Binder import android.os.Binder
import android.os.Build
import android.os.IBinder import android.os.IBinder
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import com.github.kr328.clash.common.compat.getColorCompat import com.github.kr328.clash.common.compat.getColorCompat
@ -95,26 +93,20 @@ class ProfileWorker : BaseService() {
} }
private fun createChannels() { private fun createChannels() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) NotificationManagerCompat.from(this).createNotificationChannelsCompat(
return
NotificationManagerCompat.from(this).createNotificationChannels(
listOf( listOf(
NotificationChannel( NotificationChannelCompat.Builder(
SERVICE_CHANNEL, SERVICE_CHANNEL,
getString(R.string.profile_service_status), NotificationManagerCompat.IMPORTANCE_LOW
NotificationManager.IMPORTANCE_LOW ).setName(getString(R.string.profile_service_status)).build(),
), NotificationChannelCompat.Builder(
NotificationChannel(
STATUS_CHANNEL, STATUS_CHANNEL,
getString(R.string.profile_process_status), NotificationManagerCompat.IMPORTANCE_LOW
NotificationManager.IMPORTANCE_LOW ).setName(getString(R.string.profile_process_status)).build(),
), NotificationChannelCompat.Builder(
NotificationChannel(
RESULT_CHANNEL, RESULT_CHANNEL,
getString(R.string.profile_process_result), NotificationManagerCompat.IMPORTANCE_DEFAULT
NotificationManager.IMPORTANCE_DEFAULT ).setName(getString(R.string.profile_process_result)).build()
)
) )
) )
} }

View File

@ -1,11 +1,9 @@
package com.github.kr328.clash.service.clash.module package com.github.kr328.clash.service.clash.module
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent import android.app.PendingIntent
import android.app.Service import android.app.Service
import android.content.Intent import android.content.Intent
import android.os.Build import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import com.github.kr328.clash.common.compat.getColorCompat 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" const val CHANNEL_ID = "clash_status_channel"
fun createNotificationChannel(service: Service) { fun createNotificationChannel(service: Service) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return
NotificationManagerCompat.from(service).createNotificationChannel( NotificationManagerCompat.from(service).createNotificationChannel(
NotificationChannel( NotificationChannelCompat.Builder(
CHANNEL_ID, CHANNEL_ID,
service.getText(R.string.clash_service_status_channel), NotificationManagerCompat.IMPORTANCE_LOW
NotificationManager.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, source: InetSocketAddress,
target: InetSocketAddress, target: InetSocketAddress,
): Int { ): Int {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) if (Build.VERSION.SDK_INT < 29)
return -1 return -1
return runCatching { connectivity.getConnectionOwnerUid(protocol, source, target) } return runCatching { connectivity.getConnectionOwnerUid(protocol, source, target) }