diff --git a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt index d803cb6..3162706 100644 --- a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt @@ -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 diff --git a/app/src/main/java/com/github/kr328/clash/fragment/ProfileEditFragment.kt b/app/src/main/java/com/github/kr328/clash/fragment/ProfileEditFragment.kt index 0857d39..65ec74e 100644 --- a/app/src/main/java/com/github/kr328/clash/fragment/ProfileEditFragment.kt +++ b/app/src/main/java/com/github/kr328/clash/fragment/ProfileEditFragment.kt @@ -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) { diff --git a/app/src/main/java/com/github/kr328/clash/settings/NetworkFragment.kt b/app/src/main/java/com/github/kr328/clash/settings/NetworkFragment.kt index 49ef77b..238279d 100644 --- a/app/src/main/java/com/github/kr328/clash/settings/NetworkFragment.kt +++ b/app/src/main/java/com/github/kr328/clash/settings/NetworkFragment.kt @@ -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 diff --git a/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt b/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt deleted file mode 100644 index d71e8b2..0000000 --- a/app/src/main/java/com/github/kr328/clash/utils/ServiceUtils.kt +++ /dev/null @@ -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 - diff --git a/common/src/main/java/com/github/kr328/clash/common/ids/Intents.kt b/common/src/main/java/com/github/kr328/clash/common/ids/Intents.kt index b34689f..e88a03a 100644 --- a/common/src/main/java/com/github/kr328/clash/common/ids/Intents.kt +++ b/common/src/main/java/com/github/kr328/clash/common/ids/Intents.kt @@ -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" } \ No newline at end of file diff --git a/common/src/main/java/com/github/kr328/clash/common/ids/NotificationIds.kt b/common/src/main/java/com/github/kr328/clash/common/ids/NotificationIds.kt index d5710ef..cf38054 100644 --- a/common/src/main/java/com/github/kr328/clash/common/ids/NotificationIds.kt +++ b/common/src/main/java/com/github/kr328/clash/common/ids/NotificationIds.kt @@ -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 { diff --git a/common/src/main/java/com/github/kr328/clash/common/ids/PendingIds.kt b/common/src/main/java/com/github/kr328/clash/common/ids/PendingIds.kt index 7e8780c..c46cc53 100644 --- a/common/src/main/java/com/github/kr328/clash/common/ids/PendingIds.kt +++ b/common/src/main/java/com/github/kr328/clash/common/ids/PendingIds.kt @@ -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) } diff --git a/common/src/main/java/com/github/kr328/clash/common/time/Ticker.kt b/common/src/main/java/com/github/kr328/clash/common/time/Ticker.kt deleted file mode 100644 index ca57fc9..0000000 --- a/common/src/main/java/com/github/kr328/clash/common/time/Ticker.kt +++ /dev/null @@ -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 = 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() - } - } - } - } -} \ No newline at end of file diff --git a/common/src/main/java/com/github/kr328/clash/common/utils/PrefixTree.kt b/common/src/main/java/com/github/kr328/clash/common/utils/PrefixTree.kt deleted file mode 100644 index 01c4e51..0000000 --- a/common/src/main/java/com/github/kr328/clash/common/utils/PrefixTree.kt +++ /dev/null @@ -1,40 +0,0 @@ -package com.github.kr328.clash.common.utils - -data class PrefixTree(val separator: String) { - companion object { - const val WILDCARD = "*" - } - - private data class Node(val children: MutableMap>, var data: T?) - private val root = Node(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 - } -} \ No newline at end of file diff --git a/common/src/main/res/values-zh/strings.xml b/common/src/main/res/values-zh/strings.xml new file mode 100644 index 0000000..d516d6e --- /dev/null +++ b/common/src/main/res/values-zh/strings.xml @@ -0,0 +1,5 @@ + + + 接收 Clash 广播 + 接收来自 Clash 内部的广播 + \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/Event.kt b/core/src/main/java/com/github/kr328/clash/core/event/Event.kt deleted file mode 100644 index 31756e6..0000000 --- a/core/src/main/java/com/github/kr328/clash/core/event/Event.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.github.kr328.clash.core.event - -import android.os.Parcelable - -interface Event : Parcelable \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/EventStream.kt b/core/src/main/java/com/github/kr328/clash/core/event/EventStream.kt deleted file mode 100644 index f7a59dd..0000000 --- a/core/src/main/java/com/github/kr328/clash/core/event/EventStream.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.github.kr328.clash.core.event - -abstract class EventStream { - private var on: (T) -> Unit = {} - - fun onEvent(callback: (T) -> Unit) { - on = callback - } - - fun close() { - onClose() - } - - fun send(event: T) { - on(event) - } - - abstract fun onClose() -} \ No newline at end of file diff --git a/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt b/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt index d452a6c..63ff6af 100644 --- a/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt +++ b/core/src/main/java/com/github/kr328/clash/core/event/LogEvent.kt @@ -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" diff --git a/design/src/main/java/com/github/kr328/clash/design/common/Base.kt b/design/src/main/java/com/github/kr328/clash/design/common/Base.kt index bac13fa..8242b1b 100644 --- a/design/src/main/java/com/github/kr328/clash/design/common/Base.kt +++ b/design/src/main/java/com/github/kr328/clash/design/common/Base.kt @@ -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 diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt b/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt index 361a0ac..463b662 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashManager.kt @@ -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)) diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt index c1dc9a8..32e921e 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashManagerService.kt @@ -5,6 +5,6 @@ import android.os.IBinder class ClashManagerService : BaseService() { override fun onBind(intent: Intent?): IBinder? { - return ClashManager(this, this) + return ClashManager(this) } } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashNotification.kt b/service/src/main/java/com/github/kr328/clash/service/ClashNotification.kt deleted file mode 100644 index a81dfb8..0000000 --- a/service/src/main/java/com/github/kr328/clash/service/ClashNotification.kt +++ /dev/null @@ -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 - ) - ) - } -} \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileBackgroundService.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileBackgroundService.kt index 0cf699b..734ba1b 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileBackgroundService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileBackgroundService.kt @@ -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) { diff --git a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt index 2d69ae0..23eb405 100644 --- a/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt +++ b/service/src/main/java/com/github/kr328/clash/service/clash/module/TunModule.kt @@ -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 ) diff --git a/service/src/main/java/com/github/kr328/clash/service/data/Database.kt b/service/src/main/java/com/github/kr328/clash/service/data/Database.kt index 17e57bf..db989ac 100644 --- a/service/src/main/java/com/github/kr328/clash/service/data/Database.kt +++ b/service/src/main/java/com/github/kr328/clash/service/data/Database.kt @@ -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,