Skip to content

Commit

Permalink
save alternative urls used and process it
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 4, 2024
1 parent 8153dec commit 6a486ed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ class Service(private val context: Context) {
// Handle the result
when (result) {
is UrlCheckResult.Success -> {
listener?.onConfigurationIdReceived(result.id, result.code)
val isAlternativeUrl = result.url != url
listener?.onConfigurationIdReceived(result.id, result.code, result.url, isAlternativeUrl)
activity.setSyncFailed(false)
Log.d("Service", "Successfully connected to URL: ${result.url}")
}
Expand Down Expand Up @@ -609,6 +610,6 @@ class Service(private val context: Context) {
}

interface ConfigurationIdListener {
fun onConfigurationIdReceived(id: String, code: String)
fun onConfigurationIdReceived(id: String, code: String, url: String, isAlternativeUrl: Boolean)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ abstract class ProcessUserDataActivity : PermissionActivity(), SuccessListener {
} else {
urlUser = "satellite"
urlPwd = password
couchdbURL = uri.scheme + "://" + urlUser + ":" + urlPwd + "@" + uri.host + ":" + if (uri.port == -1) (if (uri.scheme == "http") 80 else 443) else uri.port
couchdbURL = "${uri.scheme}://$urlUser:$urlPwd@${uri.host}:${if (uri.port == -1) (if (uri.scheme == "http") 80 else 443) else uri.port}"
}
editor.putString("serverPin", password)
saveUrlScheme(editor, uri, url, couchdbURL)
Expand Down Expand Up @@ -230,7 +230,7 @@ abstract class ProcessUserDataActivity : PermissionActivity(), SuccessListener {
alert11.show()
}

private fun getUserInfo(uri: Uri): Array<String> {
fun getUserInfo(uri: Uri): Array<String> {
val ar = arrayOf("", "")
val info = uri.userInfo?.split(":".toRegex())?.dropLastWhile { it.isEmpty() }?.toTypedArray()
if ((info?.size ?: 0) > 1) {
Expand Down
37 changes: 32 additions & 5 deletions app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.ole.planet.myplanet.ui.sync
import android.Manifest
import android.content.*
import android.graphics.drawable.AnimationDrawable
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.*
Expand Down Expand Up @@ -112,17 +113,19 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
processedUrl = Utilities.getUrl()
}

override fun onConfigurationIdReceived(id: String, code:String) {
override fun onConfigurationIdReceived(id: String, code: String, url: String, isAlternativeUrl: Boolean) {
Log.d("SyncActivity", "onConfigurationIdReceived: $id")
Log.d("SyncActivity", "onConfigurationIdReceived: $code")
Log.d("SyncActivity", "onConfigurationIdReceived: $url")
Log.d("SyncActivity", "onConfigurationIdReceived: $isAlternativeUrl")
val savedId = settings.getString("configurationId", null)
if (serverConfigAction == "sync") {
if (savedId == null) {
editor.putString("configurationId", id).apply()
editor.putString("communityName", code).apply()
currentDialog?.let { continueSync(it) }
currentDialog?.let { continueSync(it, url, isAlternativeUrl) }
} else if (id == savedId) {
currentDialog?.let { continueSync(it) }
currentDialog?.let { continueSync(it, url, isAlternativeUrl) }
} else {
clearDataDialog(getString(R.string.you_want_to_connect_to_a_different_server), false)
}
Expand Down Expand Up @@ -743,9 +746,33 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers
return modifiedUrl
}

private fun continueSync(dialog: MaterialDialog) {
private fun continueSync(dialog: MaterialDialog, url: String, isAlternativeUrl: Boolean) {
if (isAlternativeUrl) {
val password = "${(dialog.customView?.findViewById<View>(R.id.input_server_Password) as EditText).text}"
val uri = Uri.parse(url)
var couchdbURL: String
val urlUser: String
val urlPwd: String
if (url.contains("@")) {
val userinfo = getUserInfo(uri)
urlUser = userinfo[0]
urlPwd = userinfo[1]
couchdbURL = url
} else {
urlUser = "satellite"
urlPwd = password
couchdbURL = "${uri.scheme}://$urlUser:$urlPwd@${uri.host}:${if (uri.port == -1) (if (uri.scheme == "http") 80 else 443) else uri.port}"
}
editor.putString("alternativeUrl", url).apply()
editor.putString("processedAlternativeUrl", couchdbURL).apply()
editor.putBoolean("isAlternativeUrl", true).apply()

processedUrl = couchdbURL
} else {
processedUrl = saveConfigAndContinue(dialog)
}
Log.d("SyncActivity", "continueSync: $processedUrl")
processedUrl = saveConfigAndContinue(dialog)

if (TextUtils.isEmpty(processedUrl)) return
isSync = true
if (checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) && settings.getBoolean("firstRun", true)) {
Expand Down

0 comments on commit 6a486ed

Please sign in to comment.