Skip to content

Commit

Permalink
Chore: clean compat code
Browse files Browse the repository at this point in the history
  • Loading branch information
Kr328 committed Nov 14, 2021
1 parent 7355016 commit 5224fa6
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 53 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/github/kr328/clash/BaseActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
17 changes: 6 additions & 11 deletions app/src/main/java/com/github/kr328/clash/LogcatService.kt
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
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
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
Expand Down Expand Up @@ -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()
)
}

Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
)
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down

0 comments on commit 5224fa6

Please sign in to comment.