fix broadcast

This commit is contained in:
Kr328 committed 2020-02-18 01:17:05 +08:00
1 parent 0d7650cbc0
commit 8a4149c800
9 files changed
+79 -24

No files matched your search

@@ -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 {
@@ -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
}
}
}
@@ -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()
}
@@ -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<ClashProfileEntity>(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(
+4 -4
View File
@@ -16,12 +16,12 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_height="70dp"
android:gravity="center_vertical">
<View
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginStart="40dp"
android:background="@drawable/ic_logo" />
@@ -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" />
</LinearLayout>
<com.github.kr328.clash.design.view.ColorfulTextCard
+3
View File
@@ -9,9 +9,12 @@
<string name="proxy">Proxy</string>
<string name="direct_mode">Direct Mode</string>
<string name="rule_mode">Rule Mode</string>
<string name="global_mode">Global Mode</string>
<string name="profiles">Profiles</string>
<string name="not_selected">Not selected</string>
<string name="format_profile_activated">%s Activated</string>
<string name="logs">Logs</string>
<string name="settings">Settings</string>
@@ -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")
}
@@ -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"
}
@@ -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)
}