ref: 2f07c3711ce825bda121098aa646d5abfcfd0435
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 |
package xyz.apiote.bimba.czwek.settings import android.content.Context import android.content.Intent import android.content.SharedPreferences import android.net.ConnectivityManager import android.os.Bundle import android.util.Log 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.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.settings.feeds.FeedChooserActivity class ServerChooserActivity : AppCompatActivity() { private var _binding: ActivityServerChooserBinding? = null private val binding get() = _binding!! private val activityLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { if (!preferences.getBoolean("inFeedsTransaction", true)) { finish() } } private lateinit var preferences: SharedPreferences override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) preferences = getSharedPreferences("shp", MODE_PRIVATE) if (intent.getBooleanExtra("simple", false)) { setServer("bimba.apiote.xyz", "") checkServer(true) } else { _binding = ActivityServerChooserBinding.inflate(layoutInflater) setContentView(binding.root) preferences.edit(true) { putBoolean("inFeedsTransaction", true) } binding.button.isEnabled = false binding.serverField.editText!!.addTextChangedListener { editable -> binding.button.isEnabled = !editable.isNullOrBlank() } if (!preferences.getBoolean("firstRun", true)) { Server.get(this).let { server -> binding.serverField.editText!!.setText(server.host) binding.tokenField.editText!!.setText(server.token) } } binding.button.setOnClickListener { 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(resources.getString(R.string.cancel)) { _, _ -> }.apply { if (onPositive != null) { setPositiveButton(resources.getString(R.string.cont)) { _, _ -> onPositive() } } }.show() } private fun checkServer(isSimple: Boolean) { val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager MainScope().launch { val result = getBimba(cm, 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 } val token = preferences.getString("token", "") updateServer(bimba.servers[0]["url"]!!) if (bimba.isPrivate() && token == "") { showDialog(R.string.error, R.string.server_private_question, R.drawable.error_sec, null) return@launch } if (bimba.isRateLimited() && token == "" && !isSimple) { showDialog( R.string.rate_limit, R.string.server_rate_limited_question, R.drawable.error_limit ) { runFeedsActivity() } return@launch } runFeedsActivity() } } private fun setServer(hostname: String, token: String) { preferences.edit(true) { putString("host", hostname) putString("token", token) } } private fun updateServer(apiPath: String) { preferences.edit(true) { putString("apiPath", apiPath) } } private fun runFeedsActivity() { activityLauncher.launch(Intent(this, FeedChooserActivity::class.java)) if (intent.getBooleanExtra("simple", false)) { finish() } } } |