ref: e4d7079147c16b0d67cd39e35f5d943644d24c68
app/src/main/java/xyz/apiote/bimba/czwek/settings/ServerChooserActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
// SPDX-FileCopyrightText: Adam Evyčędo // // SPDX-License-Identifier: GPL-3.0-or-later package xyz.apiote.bimba.czwek.settings import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.graphics.Color import android.os.Bundle import android.util.Log import androidx.activity.enableEdgeToEdge import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.content.res.AppCompatResources import androidx.core.content.edit import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat import androidx.core.view.updatePadding import androidx.core.widget.addTextChangedListener import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.MainScope import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.yaml.snakeyaml.error.YAMLException import xyz.apiote.bimba.czwek.R import xyz.apiote.bimba.czwek.api.Bimba import xyz.apiote.bimba.czwek.api.Server import xyz.apiote.bimba.czwek.api.TrafficFormatException import xyz.apiote.bimba.czwek.api.getBimba import xyz.apiote.bimba.czwek.databinding.ActivityServerChooserBinding import xyz.apiote.bimba.czwek.onboarding.FirstRunActivity import xyz.apiote.bimba.czwek.settings.feeds.FeedChooserActivity class ServerChooserActivity : AppCompatActivity() { companion object { const val PARAM_SIMPLE = "simple" const val IN_FEEDS_TRANSACTION = "inFeedsTransaction" const val PREFERENCES_NAME = "shp" fun getIntent(context: Context, simple: Boolean) = Intent(context, ServerChooserActivity::class.java).apply { putExtra(PARAM_SIMPLE, simple) } } private var _binding: ActivityServerChooserBinding? = null private val binding get() = _binding!! private val activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (!preferences.getBoolean(IN_FEEDS_TRANSACTION, true)) { finish() } } private lateinit var preferences: SharedPreferences override fun onCreate(savedInstanceState: Bundle?) { enableEdgeToEdge() super.onCreate(savedInstanceState) preferences = getSharedPreferences(PREFERENCES_NAME, MODE_PRIVATE) if (intent.getBooleanExtra(PARAM_SIMPLE, false)) { setServer(Server.DEFAULT, "") checkServer(true) } else { _binding = ActivityServerChooserBinding.inflate(layoutInflater) setContentView(binding.root) ViewCompat.setOnApplyWindowInsetsListener(binding.root) { v, windowInsets -> val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars()) v.updatePadding(right = insets.right, left = insets.left, top = insets.top) windowInsets } preferences.edit(true) { putBoolean(IN_FEEDS_TRANSACTION, true) } if (preferences.getBoolean("shibboleet", false)) { binding.button.setBackgroundColor(Color.rgb(35, 93, 121)) binding.button.setTextColor(Color.WHITE) } binding.button.isEnabled = false binding.serverField.editText!!.addTextChangedListener { editable -> binding.button.isEnabled = !editable.isNullOrBlank() } if (!FirstRunActivity.getFirstRun(this)) { Server.get(this).let { server -> binding.serverField.editText!!.setText(server.host) binding.tokenField.editText!!.setText(server.token) } } binding.button.setOnClickListener { when (binding.serverField.editText!!.text.toString()) { ":shibboleet" -> { binding.button.setBackgroundColor(Color.rgb(35, 93, 121)) binding.button.setTextColor(Color.WHITE) preferences.edit(true) { putBoolean("shibboleet", true) } if (!FirstRunActivity.getFirstRun(this)) { Server.get(this).let { server -> binding.serverField.editText!!.setText(server.host) binding.tokenField.editText!!.setText(server.token) } } } ";shibboleet" -> { _binding = ActivityServerChooserBinding.inflate(layoutInflater) setContentView(binding.root) preferences.edit(true) { putBoolean("shibboleet", false) } if (!FirstRunActivity.getFirstRun(this)) { Server.get(this).let { server -> binding.serverField.editText!!.setText(server.host) binding.tokenField.editText!!.setText(server.token) } } } else -> { setServer( binding.serverField.editText!!.text.toString(), binding.tokenField.editText!!.text.toString() ) checkServer(false) } } } } } private fun showDialog( title: Int, description: Int, icon: Int, onPositive: (() -> Unit)? ) { MaterialAlertDialogBuilder(this).setIcon(AppCompatResources.getDrawable(this, icon)) .setTitle(getString(title)).setMessage(getString(description)) .setNegativeButton(R.string.cancel) { _, _ -> }.apply { if (onPositive != null) { setPositiveButton(R.string.continue_) { _, _ -> onPositive() } } }.show() } private fun checkServer(isSimple: Boolean) { MainScope().launch { val result = getBimba(this@ServerChooserActivity, Server.get(this@ServerChooserActivity)) if (result.error != null) { showDialog(R.string.error, result.error.stringResource, result.error.imageResource, null) Log.w( "ServerChooser", "${result.error.statusCode}, ${getString(result.error.stringResource)}" ) return@launch } val bimba = try { withContext(Dispatchers.IO) { Bimba.unmarshal(result.stream!!) } } catch (e: YAMLException) { Log.w("ServerChooser", e.message ?: "YAML error") showDialog(R.string.error, R.string.error_traffic_spec, R.drawable.error_server, null) return@launch } catch (e: TrafficFormatException) { Log.w("ServerChooser", e.message) showDialog(R.string.error, R.string.error_traffic_spec, R.drawable.error_server, null) return@launch } if (preferences.getBoolean("shibboleet", false)) { val validServers = bimba.servers.filter { !it.getOrDefault("url", null).isNullOrBlank() } val servers = validServers.toTypedArray() MaterialAlertDialogBuilder(this@ServerChooserActivity) .setTitle(R.string.choose_server) .setItems(servers.map { it["description"] }.toTypedArray()) { _, i -> updateServer(servers[i]["url"].toString()) moveOn(bimba, false) } .show() return@launch } updateServer(bimba.servers[0]["url"]!!) moveOn(bimba, isSimple) return@launch } } private fun moveOn(bimba: Bimba, isSimple: Boolean) { val token = preferences.getString(Server.TOKEN_KEY, "") if (bimba.isPrivate() && token == "") { showDialog(R.string.error, R.string.server_private_question, R.drawable.error_sec, null) return } if (bimba.isRateLimited() && token == "" && !isSimple) { showDialog( R.string.rate_limit, R.string.server_rate_limited_question, R.drawable.error_limit ) { runFeedsActivity() } return } runFeedsActivity() } private fun setServer(hostname: String, token: String) { preferences.edit(true) { putString(Server.HOST_KEY, hostname) putString(Server.TOKEN_KEY, token) } } private fun updateServer(apiPath: String) { preferences.edit(true) { putString(Server.API_PATH_KEY, apiPath) } } private fun runFeedsActivity() { activityLauncher.launch(Intent(this, FeedChooserActivity::class.java)) if (intent.getBooleanExtra(PARAM_SIMPLE, false)) { finish() } } } |