improve code by Inspect Code
This commit is contained in:
20 files changed
+23
-274
No files matched your search
@@ -9,7 +9,6 @@ import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.common.util.componentName
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.common.util.startForegroundServiceCompat
|
||||
import com.github.kr328.clash.common.utils.Log
|
||||
import com.github.kr328.clash.remote.withProfile
|
||||
import com.github.kr328.clash.service.ProfileBackgroundService
|
||||
import com.github.kr328.clash.service.ProfileProvider
|
||||
|
||||
@@ -154,6 +154,12 @@ class ProfileEditFragment(
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
|
||||
override fun onSaveInstanceState(outState: Bundle) {
|
||||
super.onSaveInstanceState(outState)
|
||||
|
||||
(view as CommonUiLayout?)?.screen?.saveState(outState)
|
||||
}
|
||||
|
||||
private fun openUrlProvider(): Boolean {
|
||||
try {
|
||||
when (type) {
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.os.Bundle
|
||||
import com.github.kr328.clash.PackagesActivity
|
||||
import com.github.kr328.clash.R
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.preference.UiSettings
|
||||
import com.github.kr328.clash.remote.Broadcasts
|
||||
import com.github.kr328.clash.service.settings.ServiceSettings
|
||||
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.github.kr328.clash.utils
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import com.github.kr328.clash.common.ids.Intents
|
||||
import com.github.kr328.clash.common.util.intent
|
||||
import com.github.kr328.clash.common.util.startForegroundServiceCompat
|
||||
import com.github.kr328.clash.preference.UiSettings
|
||||
import com.github.kr328.clash.service.ClashService
|
||||
import com.github.kr328.clash.service.TunService
|
||||
import com.github.kr328.clash.service.util.sendBroadcastSelf
|
||||
|
||||
@@ -22,8 +22,6 @@ object Intents {
|
||||
|
||||
const val INTENT_EXTRA_CLASH_STOP_REASON =
|
||||
"${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.clash.stop.reason"
|
||||
const val INTENT_EXTRA_PROFILE_REQUEST =
|
||||
"${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile.request"
|
||||
const val INTENT_EXTRA_PROFILE_ID =
|
||||
"${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile.id"
|
||||
}
|
||||
@@ -2,8 +2,7 @@ package com.github.kr328.clash.common.ids
|
||||
|
||||
object NotificationIds {
|
||||
const val CLASH_STATUS = 1
|
||||
const val CLASH_VPN = 2
|
||||
const val PROFILE_STATUS = 3
|
||||
const val PROFILE_STATUS = 2
|
||||
private val PROFILE_RESULT = 10000..20000
|
||||
|
||||
fun generateProfileResultId(profileId: Long): Int {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package com.github.kr328.clash.common.ids
|
||||
|
||||
object PendingIds {
|
||||
const val CLASH_VPN = 1
|
||||
|
||||
fun generateProfileResultId(profileId: Long): Int {
|
||||
return NotificationIds.generateProfileResultId(profileId)
|
||||
}
|
||||
|
||||
@@ -1,55 +0,0 @@
|
||||
package com.github.kr328.clash.common.time
|
||||
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.channels.Channel
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
|
||||
class Ticker(private val timeMillis: Long) {
|
||||
val c: Channel<Int> = Channel()
|
||||
|
||||
private var tickerJob: Job? = null
|
||||
private val locker: Mutex = Mutex()
|
||||
|
||||
suspend fun resume() {
|
||||
locker.lock()
|
||||
|
||||
if ( tickerJob == null ) {
|
||||
start()
|
||||
}
|
||||
|
||||
locker.unlock()
|
||||
}
|
||||
|
||||
suspend fun pause() {
|
||||
locker.lock()
|
||||
|
||||
if ( tickerJob != null ) {
|
||||
tickerJob!!.cancel()
|
||||
}
|
||||
|
||||
locker.unlock()
|
||||
}
|
||||
|
||||
private suspend fun start() {
|
||||
withContext(Dispatchers.Unconfined) {
|
||||
tickerJob = launch {
|
||||
try {
|
||||
var count = 0
|
||||
|
||||
while (isActive) {
|
||||
c.send(count++)
|
||||
|
||||
delay(timeMillis)
|
||||
}
|
||||
}
|
||||
finally {
|
||||
locker.lock()
|
||||
|
||||
tickerJob = null
|
||||
|
||||
locker.unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.github.kr328.clash.common.utils
|
||||
|
||||
data class PrefixTree<T>(val separator: String) {
|
||||
companion object {
|
||||
const val WILDCARD = "*"
|
||||
}
|
||||
|
||||
private data class Node<T>(val children: MutableMap<String, Node<T>>, var data: T?)
|
||||
private val root = Node<T>(mutableMapOf(), null)
|
||||
|
||||
fun insert(path: String, data: T) {
|
||||
val segment = path.split(separator).filter(String::isNotBlank)
|
||||
var current = root
|
||||
|
||||
for ( s in segment ) {
|
||||
current = current.children.getOrPut(s) {
|
||||
Node(mutableMapOf(), null)
|
||||
}
|
||||
}
|
||||
|
||||
current.data = data
|
||||
}
|
||||
|
||||
fun find(path: String): T? {
|
||||
val segment = path.split(separator).filter(String::isNotBlank)
|
||||
var current = root
|
||||
|
||||
for (s in segment) {
|
||||
current = when {
|
||||
current.children.containsKey(s) ->
|
||||
current.children[s]!!
|
||||
current.children.containsKey(WILDCARD) ->
|
||||
current.children[WILDCARD]!!
|
||||
else -> return null
|
||||
}
|
||||
}
|
||||
|
||||
return current.data
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<string name="receive_clash_broadcasts">接收 Clash 广播</string>
|
||||
<string name="receive_broadcasts_of_clash">接收来自 Clash 内部的广播</string>
|
||||
</resources>
|
||||
@@ -1,5 +0,0 @@
|
||||
package com.github.kr328.clash.core.event
|
||||
|
||||
import android.os.Parcelable
|
||||
|
||||
interface Event : Parcelable
|
||||
@@ -1,19 +0,0 @@
|
||||
package com.github.kr328.clash.core.event
|
||||
|
||||
abstract class EventStream<T : Event> {
|
||||
private var on: (T) -> Unit = {}
|
||||
|
||||
fun onEvent(callback: (T) -> Unit) {
|
||||
on = callback
|
||||
}
|
||||
|
||||
fun close() {
|
||||
onClose()
|
||||
}
|
||||
|
||||
fun send(event: T) {
|
||||
on(event)
|
||||
}
|
||||
|
||||
abstract fun onClose()
|
||||
}
|
||||
@@ -6,11 +6,12 @@ import com.github.kr328.clash.common.serialization.Parcels
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
@Suppress("UNUSED")
|
||||
data class LogEvent(
|
||||
val level: Level,
|
||||
val message: String,
|
||||
val time: Long = System.currentTimeMillis()
|
||||
) : Event {
|
||||
) : Parcelable {
|
||||
companion object {
|
||||
const val DEBUG_VALUE = "debug"
|
||||
const val INFO_VALUE = "info"
|
||||
|
||||
@@ -4,6 +4,7 @@ import android.content.Context
|
||||
import android.os.Bundle
|
||||
import android.view.View
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
abstract class Base(val screen: CommonUiScreen) {
|
||||
val context: Context
|
||||
get() = screen.layout.context
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.github.kr328.clash.service
|
||||
|
||||
import android.content.Context
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.core.model.General
|
||||
import com.github.kr328.clash.core.model.ProxyGroupList
|
||||
@@ -12,9 +11,8 @@ import com.github.kr328.clash.service.transact.ParcelableContainer
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
class ClashManager(context: Context, parent: CoroutineScope) :
|
||||
class ClashManager(parent: CoroutineScope) :
|
||||
IClashManager.Stub(), CoroutineScope by parent {
|
||||
private val settings = context.getSharedPreferences("service", Context.MODE_PRIVATE)
|
||||
|
||||
override fun setProxyMode(mode: String?) {
|
||||
Clash.setProxyMode(requireNotNull(mode))
|
||||
|
||||
@@ -5,6 +5,6 @@ import android.os.IBinder
|
||||
|
||||
class ClashManagerService : BaseService() {
|
||||
override fun onBind(intent: Intent?): IBinder? {
|
||||
return ClashManager(this, this)
|
||||
return ClashManager(this)
|
||||
}
|
||||
}
|
||||
@@ -1,127 +0,0 @@
|
||||
package com.github.kr328.clash.service
|
||||
|
||||
import android.app.*
|
||||
import android.content.Intent
|
||||
import android.os.Build
|
||||
import androidx.core.app.NotificationCompat
|
||||
import androidx.core.app.NotificationManagerCompat
|
||||
import com.github.kr328.clash.common.utils.asBytesString
|
||||
import com.github.kr328.clash.common.utils.asSpeedString
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
class ClashNotification(
|
||||
private val context: Service
|
||||
) {
|
||||
companion object {
|
||||
private const val CLASH_STATUS_NOTIFICATION_CHANNEL = "clash_status_channel"
|
||||
private const val CLASH_STATUS_NOTIFICATION_ID = 413
|
||||
}
|
||||
|
||||
private val contentIntent = Intent(Intent.ACTION_MAIN)
|
||||
.addCategory(Intent.CATEGORY_DEFAULT)
|
||||
.addCategory(Intent.CATEGORY_LAUNCHER)
|
||||
.setPackage(context.packageName)
|
||||
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
|
||||
private val baseBuilder = NotificationCompat.Builder(context, CLASH_STATUS_NOTIFICATION_CHANNEL)
|
||||
.setSmallIcon(R.drawable.ic_notification)
|
||||
.setOngoing(true)
|
||||
.setColor(context.getColor(R.color.colorAccentService))
|
||||
.setOnlyAlertOnce(true)
|
||||
.setShowWhen(false)
|
||||
.setGroup(CLASH_STATUS_NOTIFICATION_CHANNEL)
|
||||
.setContentIntent(
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
CLASH_STATUS_NOTIFICATION_ID,
|
||||
contentIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
)
|
||||
private var currentProfile = "None"
|
||||
|
||||
init {
|
||||
createNotificationChannel()
|
||||
|
||||
updateBase()
|
||||
}
|
||||
|
||||
fun destroy() {
|
||||
context.stopForeground(true)
|
||||
|
||||
updateDestroy()
|
||||
}
|
||||
|
||||
fun setProfile(profile: String) {
|
||||
currentProfile = profile
|
||||
}
|
||||
|
||||
suspend fun update() {
|
||||
val notification = withContext(Dispatchers.Default) {
|
||||
createNotification()
|
||||
}
|
||||
|
||||
context.startForeground(CLASH_STATUS_NOTIFICATION_ID, notification)
|
||||
}
|
||||
|
||||
fun updateBase() {
|
||||
val notification = baseBuilder
|
||||
.setContentTitle(currentProfile)
|
||||
.setContentText(context.getText(R.string.running))
|
||||
.build()
|
||||
|
||||
context.startForeground(CLASH_STATUS_NOTIFICATION_ID, notification)
|
||||
}
|
||||
|
||||
private fun updateDestroy() {
|
||||
// just waiting system cancel our notification :)
|
||||
// fxxking google
|
||||
|
||||
val notification = baseBuilder
|
||||
.setContentTitle(context.getText(R.string.destroying))
|
||||
.setContentText(context.getText(R.string.recycling_resources))
|
||||
.setSubText(null)
|
||||
.build()
|
||||
|
||||
NotificationManagerCompat.from(context).apply {
|
||||
notify(CLASH_STATUS_NOTIFICATION_ID, notification)
|
||||
cancel(CLASH_STATUS_NOTIFICATION_ID)
|
||||
}
|
||||
}
|
||||
|
||||
private fun createNotification(): Notification {
|
||||
val traffic = Clash.queryTraffic()
|
||||
val bandwidth = Clash.queryBandwidth()
|
||||
|
||||
return baseBuilder
|
||||
.setContentTitle(currentProfile)
|
||||
.setContentText(
|
||||
context.getString(
|
||||
R.string.clash_notification_content,
|
||||
traffic.upload.asSpeedString(),
|
||||
traffic.download.asSpeedString()
|
||||
)
|
||||
)
|
||||
.setSubText(
|
||||
context.getString(
|
||||
R.string.clash_notification_content,
|
||||
bandwidth.upload.asBytesString(),
|
||||
bandwidth.download.asBytesString()
|
||||
)
|
||||
)
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun createNotificationChannel() {
|
||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
||||
return
|
||||
NotificationManagerCompat.from(context).createNotificationChannel(
|
||||
NotificationChannel(
|
||||
CLASH_STATUS_NOTIFICATION_CHANNEL,
|
||||
context.getText(R.string.clash_service_status_channel),
|
||||
NotificationManager.IMPORTANCE_LOW
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -173,7 +173,7 @@ class ProfileBackgroundService : BaseService() {
|
||||
.setGroup(NotificationChannels.PROFILE_STATUS)
|
||||
.build()
|
||||
|
||||
startForeground(NotificationIds.CLASH_VPN, notification)
|
||||
startForeground(NotificationIds.PROFILE_STATUS, notification)
|
||||
}
|
||||
|
||||
private suspend fun sendUpdateCompleted(id: Long) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.net.VpnService
|
||||
import android.os.Build
|
||||
import com.github.kr328.clash.common.ids.NotificationIds
|
||||
import com.github.kr328.clash.common.ids.PendingIds
|
||||
import com.github.kr328.clash.core.Clash
|
||||
import com.github.kr328.clash.service.util.parseCIDR
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
@@ -57,7 +57,7 @@ class TunModule(private val service: VpnService) : Module() {
|
||||
builder.setConfigureIntent(
|
||||
PendingIntent.getActivity(
|
||||
service,
|
||||
NotificationIds.CLASH_VPN,
|
||||
PendingIds.CLASH_VPN,
|
||||
contentIntent,
|
||||
PendingIntent.FLAG_UPDATE_CURRENT
|
||||
)
|
||||
|
||||
@@ -18,7 +18,7 @@ abstract class Database : RoomDatabase() {
|
||||
companion object {
|
||||
val database = open(Global.application)
|
||||
|
||||
fun open(context: Context): Database {
|
||||
private fun open(context: Context): Database {
|
||||
return Room.databaseBuilder(
|
||||
context.applicationContext,
|
||||
Database::class.java,
|
||||
|
||||
Reference in new issue
Block a user