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 1671004b..77d3f6ac 100644 --- a/app/src/main/java/com/github/kr328/clash/BaseActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/BaseActivity.kt @@ -24,6 +24,7 @@ import com.github.kr328.clash.util.ActivityResultLifecycle import com.github.kr328.clash.util.ApplicationObserver import kotlinx.coroutines.* import kotlinx.coroutines.channels.Channel +import java.util.* import java.util.concurrent.atomic.AtomicInteger import kotlin.coroutines.resume import kotlin.coroutines.suspendCoroutine @@ -39,7 +40,9 @@ abstract class BaseActivity> : ClashStop, ClashStart, ProfileLoaded, - ProfileChanged + ProfileChanged, + ProfileUpdateCompleted, + ProfileUpdateFailed } @@ -177,6 +180,14 @@ abstract class BaseActivity> : events.trySend(Event.ProfileChanged) } + override fun onProfileUpdateCompleted(uuid: UUID?) { + events.trySend(Event.ProfileUpdateCompleted) + } + + override fun onProfileUpdateFailed(uuid: UUID?, reason: String?) { + events.trySend(Event.ProfileUpdateFailed) + } + override fun onProfileLoaded() { events.trySend(Event.ProfileLoaded) } 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 aa6b677a..75943dd2 100644 --- a/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt +++ b/app/src/main/java/com/github/kr328/clash/ProfilesActivity.kt @@ -1,15 +1,23 @@ package com.github.kr328.clash +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.content.IntentFilter import com.github.kr328.clash.common.util.intent import com.github.kr328.clash.common.util.setUUID import com.github.kr328.clash.common.util.ticker import com.github.kr328.clash.design.ProfilesDesign +import com.github.kr328.clash.design.ui.ToastDuration +import com.github.kr328.clash.R import com.github.kr328.clash.service.model.Profile import com.github.kr328.clash.util.withProfile import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch import kotlinx.coroutines.selects.select import kotlinx.coroutines.withContext +import java.util.* import java.util.concurrent.TimeUnit class ProfilesActivity : BaseActivity() { @@ -83,4 +91,37 @@ class ProfilesActivity : BaseActivity() { patchProfiles(queryAll()) } } + + override fun onProfileUpdateCompleted(uuid: UUID?) { + if(uuid == null) + return; + launch { + var name: String? = null; + withProfile { + name = queryByUUID(uuid)?.name + } + design?.showToast( + getString(R.string.toast_profile_updated_complete, name), + ToastDuration.Long + ) + } + } + override fun onProfileUpdateFailed(uuid: UUID?, reason: String?) { + if(uuid == null) + return; + launch { + var name: String? = null; + withProfile { + name = queryByUUID(uuid)?.name + } + design?.showToast( + getString(R.string.toast_profile_updated_failed, name, reason), + ToastDuration.Long + ){ + setAction(R.string.edit) { + startActivity(PropertiesActivity::class.intent.setUUID(uuid)) + } + } + } + } } \ No newline at end of file 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 d2b4ea11..51d06927 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 @@ -7,6 +7,7 @@ import android.content.Intent import android.content.IntentFilter import com.github.kr328.clash.common.constants.Intents import com.github.kr328.clash.common.log.Log +import java.util.* class Broadcasts(private val context: Application) { interface Observer { @@ -14,6 +15,8 @@ class Broadcasts(private val context: Application) { fun onStarted() fun onStopped(cause: String?) fun onProfileChanged() + fun onProfileUpdateCompleted(uuid: UUID?) + fun onProfileUpdateFailed(uuid: UUID?, reason: String?) fun onProfileLoaded() } @@ -52,6 +55,17 @@ class Broadcasts(private val context: Application) { receivers.forEach { it.onProfileChanged() } + Intents.ACTION_PROFILE_UPDATE_COMPLETED -> + receivers.forEach { + it.onProfileUpdateCompleted( + UUID.fromString(intent.getStringExtra(Intents.EXTRA_UUID))) + } + Intents.ACTION_PROFILE_UPDATE_FAILED -> + receivers.forEach { + it.onProfileUpdateFailed( + UUID.fromString(intent.getStringExtra(Intents.EXTRA_UUID)), + intent.getStringExtra(Intents.EXTRA_FAIL_REASON)) + } Intents.ACTION_PROFILE_LOADED -> { receivers.forEach { it.onProfileLoaded() @@ -79,6 +93,8 @@ class Broadcasts(private val context: Application) { addAction(Intents.ACTION_CLASH_STARTED) addAction(Intents.ACTION_CLASH_STOPPED) addAction(Intents.ACTION_PROFILE_CHANGED) + addAction(Intents.ACTION_PROFILE_UPDATE_COMPLETED) + addAction(Intents.ACTION_PROFILE_UPDATE_FAILED) addAction(Intents.ACTION_PROFILE_LOADED) }) diff --git a/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt b/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt index 16bef4f3..73236841 100644 --- a/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt +++ b/common/src/main/java/com/github/kr328/clash/common/constants/Intents.kt @@ -14,6 +14,8 @@ object Intents { val ACTION_CLASH_STOPPED = "$packageName.intent.action.CLASH_STOPPED" val ACTION_CLASH_REQUEST_STOP = "$packageName.intent.action.CLASH_REQUEST_STOP" val ACTION_PROFILE_CHANGED = "$packageName.intent.action.PROFILE_CHANGED" + val ACTION_PROFILE_UPDATE_COMPLETED = "$packageName.intent.action.PROFILE_UPDATE_COMPLETED" + val ACTION_PROFILE_UPDATE_FAILED = "$packageName.intent.action.PROFILE_UPDATE_FAILED" val ACTION_PROFILE_REQUEST_UPDATE = "$packageName.intent.action.REQUEST_UPDATE" val ACTION_PROFILE_SCHEDULE_UPDATES = "$packageName.intent.action.SCHEDULE_UPDATES" val ACTION_PROFILE_LOADED = "$packageName.intent.action.PROFILE_LOADED" @@ -21,4 +23,5 @@ object Intents { const val EXTRA_STOP_REASON = "stop_reason" const val EXTRA_UUID = "uuid" + const val EXTRA_FAIL_REASON = "fail_reason" } \ No newline at end of file diff --git a/design/src/main/res/values-ja-rJP/strings.xml b/design/src/main/res/values-ja-rJP/strings.xml index 1fe271ab..9adaccc6 100644 --- a/design/src/main/res/values-ja-rJP/strings.xml +++ b/design/src/main/res/values-ja-rJP/strings.xml @@ -243,4 +243,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s \ No newline at end of file diff --git a/design/src/main/res/values-ko-rKR/strings.xml b/design/src/main/res/values-ko-rKR/strings.xml index c2bfe116..17de7d43 100644 --- a/design/src/main/res/values-ko-rKR/strings.xml +++ b/design/src/main/res/values-ko-rKR/strings.xml @@ -243,4 +243,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s \ No newline at end of file diff --git a/design/src/main/res/values-ru/strings.xml b/design/src/main/res/values-ru/strings.xml index f3dd8e12..decc348e 100644 --- a/design/src/main/res/values-ru/strings.xml +++ b/design/src/main/res/values-ru/strings.xml @@ -308,4 +308,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s diff --git a/design/src/main/res/values-zh-rHK/strings.xml b/design/src/main/res/values-zh-rHK/strings.xml index f7cd5c8c..596a66c9 100644 --- a/design/src/main/res/values-zh-rHK/strings.xml +++ b/design/src/main/res/values-zh-rHK/strings.xml @@ -240,4 +240,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s \ No newline at end of file diff --git a/design/src/main/res/values-zh-rTW/strings.xml b/design/src/main/res/values-zh-rTW/strings.xml index 78b26c6c..d4cc0879 100644 --- a/design/src/main/res/values-zh-rTW/strings.xml +++ b/design/src/main/res/values-zh-rTW/strings.xml @@ -240,4 +240,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s diff --git a/design/src/main/res/values-zh/strings.xml b/design/src/main/res/values-zh/strings.xml index 92437b8c..fd6743d3 100644 --- a/design/src/main/res/values-zh/strings.xml +++ b/design/src/main/res/values-zh/strings.xml @@ -243,4 +243,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s \ No newline at end of file diff --git a/design/src/main/res/values/strings.xml b/design/src/main/res/values/strings.xml index bfca9884..91ad0907 100644 --- a/design/src/main/res/values/strings.xml +++ b/design/src/main/res/values/strings.xml @@ -308,4 +308,6 @@ Unknown Database format Only %1$s are supported %1$s imported + Update profile %s completed + Update profile %1$s failed: %2$s diff --git a/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt b/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt index e810b315..612d108a 100644 --- a/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt +++ b/service/src/main/java/com/github/kr328/clash/service/ProfileWorker.kt @@ -15,6 +15,8 @@ import com.github.kr328.clash.common.id.UndefinedIds import com.github.kr328.clash.common.util.setUUID import com.github.kr328.clash.common.util.uuid import com.github.kr328.clash.service.data.ImportedDao +import com.github.kr328.clash.service.util.sendProfileUpdateCompleted +import com.github.kr328.clash.service.util.sendProfileUpdateFailed import kotlinx.coroutines.* import java.util.* import java.util.concurrent.TimeUnit @@ -176,6 +178,8 @@ class ProfileWorker : BaseService() { NotificationManagerCompat.from(this) .notify(id, notification) + + sendProfileUpdateCompleted(uuid) } private fun failed(uuid: UUID, name: String, reason: String) { @@ -191,6 +195,8 @@ class ProfileWorker : BaseService() { NotificationManagerCompat.from(this) .notify(id, notification) + + sendProfileUpdateFailed(uuid, reason) } companion object { diff --git a/service/src/main/java/com/github/kr328/clash/service/util/Broadcast.kt b/service/src/main/java/com/github/kr328/clash/service/util/Broadcast.kt index 02b3a4f3..8cbe278e 100644 --- a/service/src/main/java/com/github/kr328/clash/service/util/Broadcast.kt +++ b/service/src/main/java/com/github/kr328/clash/service/util/Broadcast.kt @@ -27,6 +27,21 @@ fun Context.sendProfileLoaded(uuid: UUID) { sendBroadcastSelf(intent) } +fun Context.sendProfileUpdateCompleted(uuid: UUID) { + val intent = Intent(Intents.ACTION_PROFILE_UPDATE_COMPLETED) + .putExtra(Intents.EXTRA_UUID, uuid.toString()) + + sendBroadcastSelf(intent) +} + +fun Context.sendProfileUpdateFailed(uuid: UUID, reason: String) { + val intent = Intent(Intents.ACTION_PROFILE_UPDATE_FAILED) + .putExtra(Intents.EXTRA_UUID, uuid.toString()) + .putExtra(Intents.EXTRA_FAIL_REASON, reason) + + sendBroadcastSelf(intent) +} + fun Context.sendOverrideChanged() { val intent = Intent(Intents.ACTION_OVERRIDE_CHANGED)