From 8a4149c8000e7d8313780052f964955100102cf4 Mon Sep 17 00:00:00 2001 From: Kr328 <39107975+Kr328@users.noreply.github.com> Date: Tue, 18 Feb 2020 01:16:41 +0800 Subject: [PATCH] fix broadcast --- .../com/github/kr328/clash/BaseActivity.kt | 13 ++++++-- .../com/github/kr328/clash/MainActivity.kt | 31 +++++++++++++++++++ .../github/kr328/clash/ProfilesActivity.kt | 6 +--- .../github/kr328/clash/remote/Broadcasts.kt | 14 +++++++-- app/src/main/res/layout/activity_main.xml | 8 ++--- app/src/main/res/values/strings.xml | 3 ++ .../kr328/clash/service/ClashService.kt | 6 ++-- .../com/github/kr328/clash/service/Intents.kt | 8 ++--- .../clash/service/util/BroadcastUtils.kt | 14 ++++++--- 9 files changed, 79 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt index ce6cf86..ca4ca34 100644 --- a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt @@ -40,9 +40,15 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() } } - override fun onProfileChanged(active: ClashProfileEntity?) { + override fun onProfileChanged() { launch { - onClashProfileChanged(active) + onClashProfileChanged() + } + } + + override fun onProfileLoaded(profileEntity: ClashProfileEntity) { + launch { + onClashProfileLoaded(profileEntity) } } } @@ -60,7 +66,8 @@ abstract class BaseActivity : AppCompatActivity(), CoroutineScope by MainScope() open suspend fun onClashStarted() {} open suspend fun onClashStopped(reason: String?) {} - open suspend fun onClashProfileChanged(active: ClashProfileEntity?) {} + open suspend fun onClashProfileChanged() {} + open suspend fun onClashProfileLoaded(profile: ClashProfileEntity) {} override fun setContentView(layoutResID: Int) { val base = CoordinatorLayout(this).apply { diff --git a/app/src/main/java/com/github/kr328/clash/MainActivity.kt b/app/src/main/java/com/github/kr328/clash/MainActivity.kt index ee5ee5d..44e621a 100644 --- a/app/src/main/java/com/github/kr328/clash/MainActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/MainActivity.kt @@ -5,9 +5,12 @@ import android.content.Intent import android.net.VpnService import android.os.Bundle import android.view.View +import com.github.kr328.clash.core.model.General import com.github.kr328.clash.core.utils.asBytesString import com.github.kr328.clash.remote.withClash +import com.github.kr328.clash.remote.withProfile import com.github.kr328.clash.service.ClashService +import com.github.kr328.clash.service.data.ClashProfileEntity import com.github.kr328.clash.service.util.intent import com.github.kr328.clash.service.util.startForegroundServiceCompat import kotlinx.android.synthetic.main.activity_main.* @@ -86,6 +89,10 @@ class MainActivity : BaseActivity() { makeSnackbarException(getString(R.string.clash_start_failure), reason) } + override suspend fun onClashProfileLoaded(profile: ClashProfileEntity) { + updateClashStatus() + } + private fun startBandwidthPolling() { if (bandwidthJob != null) return @@ -131,5 +138,29 @@ class MainActivity : BaseActivity() { proxies.visibility = View.GONE } + + launch { + val general = withClash { + queryGeneral() + } + val active = withProfile { + queryActiveProfile() + } + + val modeResId = when ( general.mode ) { + General.Mode.DIRECT -> R.string.direct_mode + General.Mode.GLOBAL -> R.string.global_mode + General.Mode.RULE -> R.string.rule_mode + } + + val profileString = + if ( active == null ) + getText(R.string.not_selected) + else + getString(R.string.format_profile_activated, active.name) + + proxies.summary = getText(modeResId) + profiles.summary = profileString + } } } \ No newline at end of file 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 0519e17..2044432 100644 --- a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt @@ -79,11 +79,7 @@ class ProfilesActivity : BaseActivity(), ProfileAdapter.Callback, ProfilesMenu.C super.onActivityResult(requestCode, resultCode, data) } - override suspend fun onClashProfileChanged(active: ClashProfileEntity?) { - super.onClashProfileChanged(active) - - Log.d("Broadcast received") - + override suspend fun onClashProfileChanged() { reloadProfiles() } diff --git a/app/src/main/java/com/github/kr328/clash/remote/Broadcasts.kt b/app/src/main/java/com/github/kr328/clash/remote/Broadcasts.kt index d5b8a74..ac6e912 100644 --- a/app/src/main/java/com/github/kr328/clash/remote/Broadcasts.kt +++ b/app/src/main/java/com/github/kr328/clash/remote/Broadcasts.kt @@ -17,7 +17,8 @@ object Broadcasts { interface Receiver { fun onStarted() fun onStopped(cause: String?) - fun onProfileChanged(active: ClashProfileEntity?) + fun onProfileChanged() + fun onProfileLoaded(profileEntity: ClashProfileEntity) } var clashRunning: Boolean = false @@ -45,8 +46,16 @@ object Broadcasts { } Intents.INTENT_ACTION_PROFILE_CHANGED -> receivers.forEach { - it.onProfileChanged(intent.getParcelableExtra(Intents.INTENT_EXTRA_PROFILE_ACTIVE)) + it.onProfileChanged() } + Intents.INTENT_ACTION_PROFILE_LOADED -> { + val profile = intent + .getParcelableExtra(Intents.INTENT_EXTRA_PROFILE) ?: return + + receivers.forEach { + it.onProfileLoaded(profile) + } + } } } } @@ -66,6 +75,7 @@ object Broadcasts { addAction(Intents.INTENT_ACTION_PROFILE_CHANGED) addAction(Intents.INTENT_ACTION_CLASH_STOPPED) addAction(Intents.INTENT_ACTION_CLASH_STARTED) + addAction(Intents.INTENT_ACTION_PROFILE_LOADED) }) val pong = application.contentResolver.call( diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 30af450..cdf67a8 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -16,12 +16,12 @@ @@ -32,7 +32,7 @@ android:textColor="?attr/colorPrimary" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_marginStart="25dp" /> + android:layout_marginStart="22.5dp" /> Proxy Direct Mode + Rule Mode + Global Mode Profiles Not selected + %s Activated Logs Settings diff --git a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt index ef033c9..eca7856 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ClashService.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ClashService.kt @@ -92,9 +92,9 @@ class ClashService : BaseService() { super.onDestroy() } - private suspend fun reloadProfile() = withContext(Dispatchers.IO) { + private suspend fun reloadProfile() { val active = ClashDatabase.getInstance(service).openClashProfileDao() - .queryActiveProfile() ?: return@withContext stopSelf("Empty active profile") + .queryActiveProfile() ?: return stopSelf("Empty active profile") try { Clash.loadProfile( @@ -108,6 +108,8 @@ class ClashService : BaseService() { } notification.setProfile(active.name) + + broadcastProfileLoaded(this, active) } catch (e: Exception) { stopSelf("Load profile failure") } diff --git a/service/src/main/java/com/github/kr328/clash/service/Intents.kt b/service/src/main/java/com/github/kr328/clash/service/Intents.kt index e1d487d..626e815 100644 --- a/service/src/main/java/com/github/kr328/clash/service/Intents.kt +++ b/service/src/main/java/com/github/kr328/clash/service/Intents.kt @@ -1,8 +1,6 @@ package com.github.kr328.clash.service object Intents { - const val INTENT_ACTION_BIND_TUN_SERVICE = - "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.action.bind.tun" const val INTENT_ACTION_CLASH_STARTED = "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.action.clash.started" const val INTENT_ACTION_CLASH_STOPPED = @@ -13,11 +11,13 @@ object Intents { "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.action.profile.enqueue.request" const val INTENT_ACTION_PROFILE_SETUP = "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.action.profile.setup" + const val INTENT_ACTION_PROFILE_LOADED = + "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.action.profile.loaded" const val INTENT_EXTRA_CLASH_STOP_REASON = "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.clash.stop.reason" - const val INTENT_EXTRA_PROFILE_ACTIVE = - "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile.active.name" + const val INTENT_EXTRA_PROFILE = + "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile" const val INTENT_EXTRA_PROFILE_REQUEST = "${BuildConfig.LIBRARY_PACKAGE_NAME}.intent.extra.profile.request" } \ No newline at end of file diff --git a/service/src/main/java/com/github/kr328/clash/service/util/BroadcastUtils.kt b/service/src/main/java/com/github/kr328/clash/service/util/BroadcastUtils.kt index e9b5c2a..37e6899 100644 --- a/service/src/main/java/com/github/kr328/clash/service/util/BroadcastUtils.kt +++ b/service/src/main/java/com/github/kr328/clash/service/util/BroadcastUtils.kt @@ -2,18 +2,24 @@ package com.github.kr328.clash.service.util import android.content.Context import android.content.Intent -import com.github.kr328.clash.core.utils.Log import com.github.kr328.clash.service.Intents import com.github.kr328.clash.service.data.ClashDatabase +import com.github.kr328.clash.service.data.ClashProfileEntity +import com.github.kr328.clash.service.data.ClashProfileProxyEntity fun Context.sendBroadcastSelf(intent: Intent) { this.sendBroadcast(intent.setPackage(this.packageName)) } -suspend fun broadcastProfileChanged(context: Context) { - val active = ClashDatabase.getInstance(context).openClashProfileDao().queryActiveProfile() +fun broadcastProfileChanged(context: Context) { val intent = Intent(Intents.INTENT_ACTION_PROFILE_CHANGED) - .putExtra(Intents.INTENT_EXTRA_PROFILE_ACTIVE, active) + + context.sendBroadcastSelf(intent) +} + +fun broadcastProfileLoaded(context: Context, profileEntity: ClashProfileEntity) { + val intent = Intent(Intents.INTENT_ACTION_PROFILE_LOADED) + .putExtra(Intents.INTENT_EXTRA_PROFILE, profileEntity) context.sendBroadcastSelf(intent) }