diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/Server2Activity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/Server2Activity.kt index 137840b4..23d995a1 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/Server2Activity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/Server2Activity.kt @@ -4,7 +4,6 @@ import android.os.Bundle import android.support.v7.app.AlertDialog import android.text.Editable import android.text.TextUtils -import android.util.Log import android.view.Menu import android.view.MenuItem import com.google.gson.Gson @@ -70,45 +69,32 @@ class Server2Activity : BaseActivity() { * save server config */ fun saveServer(): Boolean { - var saveSuccess: Boolean val vmess = configs.vmess[edit_index] vmess.remarks = et_remarks.text.toString() if (TextUtils.isEmpty(vmess.remarks)) { toast(R.string.server_lab_remarks) - saveSuccess = false + return false } - - if (AngConfigManager.addCustomServer(vmess, edit_index) == 0) { - toast(R.string.toast_success) - saveSuccess = true - } else { - toast(R.string.toast_failure) - saveSuccess = false - } - - try { Gson().fromJson(tv_content.text.toString(), Object::class.java) } catch (e: Exception) { e.printStackTrace() toast(R.string.toast_malformed_josn) - saveSuccess = false + return false } - if (saveSuccess) { + if (AngConfigManager.addCustomServer(vmess, edit_index) == 0) { //update config defaultDPreference.setPrefString(AppConfig.ANG_CONFIG + edit_guid, tv_content.text.toString()) - if (edit_index == configs.index) { - if (!AngConfigManager.genStoreV2rayConfig(edit_index)) { - Log.d(AppConfig.ANG_PACKAGE, "update custom config $edit_index but generate full configuration failed!") - } - } + AngConfigManager.genStoreV2rayConfigIfActive(edit_index) + toast(R.string.toast_success) finish() return true } else { + toast(R.string.toast_failure) return false } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt index a166d505..aabc013b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt @@ -155,7 +155,7 @@ object AngConfigManager { angConfig.index = index app.curIndex = index storeConfigFile() - if (!genStoreV2rayConfig(index)) { + if (!genStoreV2rayConfig()) { Log.d(AppConfig.ANG_PACKAGE, "set active index $index but generate full configuration failed!") return -1 } @@ -179,35 +179,32 @@ object AngConfigManager { } } + fun genStoreV2rayConfigIfActive(index: Int) { + if (index == configs.index) { + if (!genStoreV2rayConfig()) { + Log.d(AppConfig.ANG_PACKAGE, "update config $index but generate full configuration failed!") + } + } + } + /** * gen and store v2ray config file */ - fun genStoreV2rayConfig(index: Int): Boolean { + fun genStoreV2rayConfig(): Boolean { try { - if (angConfig.index < 0 - || angConfig.vmess.count() <= 0 - || angConfig.index > angConfig.vmess.count() - 1 - ) { - return false - } - var index2 = angConfig.index - if (index >= 0) { - index2 = index - } - - val result = V2rayConfigUtil.getV2rayConfig(app, angConfig.vmess[index2]) - if (result.status) { - app.defaultDPreference.setPrefString(PREF_CURR_CONFIG, result.content) - app.defaultDPreference.setPrefString(PREF_CURR_CONFIG_GUID, currConfigGuid()) - app.defaultDPreference.setPrefString(PREF_CURR_CONFIG_NAME, currConfigName()) - return true - } else { - return false + angConfig.vmess.getOrNull(angConfig.index)?.let { + val result = V2rayConfigUtil.getV2rayConfig(app, it) + if (result.status) { + app.defaultDPreference.setPrefString(PREF_CURR_CONFIG, result.content) + app.defaultDPreference.setPrefString(PREF_CURR_CONFIG_GUID, currConfigGuid()) + app.defaultDPreference.setPrefString(PREF_CURR_CONFIG_NAME, currConfigName()) + return true + } } } catch (e: Exception) { e.printStackTrace() - return false } + return false } fun currGeneratedV2rayConfig(): String { diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt index 5f278a60..95beb8cc 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/SettingsViewModel.kt @@ -36,7 +36,7 @@ class SettingsViewModel(application: Application) : AndroidViewModel(application AppConfig.PREF_V2RAY_ROUTING_BLOCKED, AppConfig.PREF_V2RAY_ROUTING_DIRECT -> { GlobalScope.launch { - if (!AngConfigManager.genStoreV2rayConfig(AngConfigManager.configs.index)) { + if (!AngConfigManager.genStoreV2rayConfig()) { Log.d(AppConfig.ANG_PACKAGE, "$key changed but generate full configuration failed!") } }