Browse Source

Chore: clean compat code

kr328 4 years ago
parent
commit
5224fa656c

+ 2 - 2
app/src/main/java/com/github/kr328/clash/BaseActivity.kt

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

+ 6 - 11
app/src/main/java/com/github/kr328/clash/LogcatService.kt

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

+ 1 - 6
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.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) {
-        this.getColor(id)
-    } else {
-        resources.getColor(id)
-    }
+    return ContextCompat.getColor(this, id)
 }
 }
 
 
 fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable? {
 fun Context.getDrawableCompat(@DrawableRes id: Int): Drawable? {

+ 1 - 1
common/src/main/java/com/github/kr328/clash/common/compat/Html.kt

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

+ 1 - 1
common/src/main/java/com/github/kr328/clash/common/compat/Intents.kt

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

+ 1 - 1
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
 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()

+ 1 - 1
common/src/main/java/com/github/kr328/clash/common/compat/Resource.kt

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

+ 1 - 1
common/src/main/java/com/github/kr328/clash/common/compat/Services.kt

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

+ 11 - 19
service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt

@@ -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)
-            return
-
-        NotificationManagerCompat.from(this).createNotificationChannels(
+        NotificationManagerCompat.from(this).createNotificationChannelsCompat(
             listOf(
             listOf(
-                NotificationChannel(
+                NotificationChannelCompat.Builder(
                     SERVICE_CHANNEL,
                     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,
                     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,
                     RESULT_CHANNEL,
-                    getString(R.string.profile_process_result),
-                    NotificationManager.IMPORTANCE_DEFAULT
-                )
+                    NotificationManagerCompat.IMPORTANCE_DEFAULT
+                ).setName(getString(R.string.profile_process_result)).build()
             )
             )
         )
         )
     }
     }

+ 4 - 9
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
 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),
-                    NotificationManager.IMPORTANCE_LOW
-                )
+                    NotificationManagerCompat.IMPORTANCE_LOW
+                ).setName(service.getText(R.string.clash_service_status_channel)).build()
             )
             )
         }
         }
 
 

+ 1 - 1
service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt

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