diff --git a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt index 67535aa6da..51ddbd02da 100644 --- a/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt +++ b/app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt @@ -106,18 +106,24 @@ class Service(private val context: Context) { } fun checkVersion(callback: CheckVersionCallback, settings: SharedPreferences) { - if (settings.getString("couchdbURL", "")?.isEmpty() == true) { - callback.onError(context.getString(R.string.config_not_available), true) - return + Log.d("Service", "checkVersion: ${Utilities.getUpdateUrl(settings)}") + if (!settings.getBoolean("isAlternativeUrl", false)){ + if (settings.getString("couchdbURL", "")?.isEmpty() == true) { + callback.onError(context.getString(R.string.config_not_available), true) + return + } } + retrofitInterface?.checkVersion(Utilities.getUpdateUrl(settings))?.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { preferences.edit().putInt("LastWifiID", NetworkUtils.getCurrentNetworkId(context)).apply() if (response.body() != null) { val p = response.body() preferences.edit().putString("versionDetail", Gson().toJson(response.body())).apply() + Log.d("Service", "checkVersion onResponse: ${response.body()}") retrofitInterface.getApkVersion(Utilities.getApkVersionUrl(settings)).enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { + Log.d("Service", "getApkVersion onResponse: $response") val responses: String? try { responses = Gson().fromJson(response.body()?.string(), String::class.java) @@ -144,6 +150,7 @@ class Service(private val context: Context) { if (p != null) { if (currentVersion < p.minapkcode && apkVersion < p.minapkcode) { callback.onUpdateAvailable(p, true) + Log.d("Service", "checkVersion: Planet up to date") } else { callback.onError("Planet up to date", false) } @@ -168,12 +175,15 @@ class Service(private val context: Context) { } fun isPlanetAvailable(callback: PlanetAvailableListener?) { + Log.d("Service", "isPlanetAvailable: ${Utilities.getUpdateUrl(preferences)}") retrofitInterface?.isPlanetAvailable(Utilities.getUpdateUrl(preferences))?.enqueue(object : Callback { override fun onResponse(call: Call, response: Response) { if (callback != null && response.code() == 200) { callback.isAvailable() + Log.d("Service", "isAvailable: true") } else { callback?.notAvailable() + Log.d("Service", "isAvailable: false") } } diff --git a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt index 4766a88e3f..fa7c645fb8 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/SyncManager.kt @@ -5,6 +5,7 @@ import android.content.SharedPreferences import android.net.wifi.SupplicantState import android.net.wifi.WifiManager import android.text.TextUtils +import android.util.Log import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonNull @@ -76,6 +77,7 @@ class SyncManager private constructor(private val context: Context) { private fun authenticateAndSync() { td = Thread { + Log.d("authenticateAndSync", "${TransactionSyncManager.authenticate()}") if (TransactionSyncManager.authenticate()) { startSync() } else { diff --git a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt index b7284adc50..a61df45cde 100644 --- a/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt +++ b/app/src/main/java/org/ole/planet/myplanet/service/TransactionSyncManager.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.SharedPreferences import android.text.TextUtils import android.util.Base64 +import android.util.Log import com.google.gson.Gson import com.google.gson.JsonArray import com.google.gson.JsonObject @@ -50,13 +51,16 @@ object TransactionSyncManager { fun authenticate(): Boolean { val apiInterface = client?.create(ApiInterface::class.java) try { - val response: Response? = apiInterface?.getDocuments(Utilities.header, Utilities.getUrl() + "/tablet_users/_all_docs")?.execute() + Log.d("TransactionSyncManager", "header: ${Utilities.header}, authenticate: ${Utilities.getUrl()}/tablet_users/_all_docs") + val response: Response? = apiInterface?.getDocuments(Utilities.header, "${Utilities.getUrl()}/tablet_users/_all_docs")?.execute() + Log.d("TransactionSyncManager", "response: $response") if (response != null) { return response.code() == 200 } } catch (e: IOException) { e.printStackTrace() } + return false } @@ -64,7 +68,7 @@ object TransactionSyncManager { listener.onSyncStarted() val userName = settings.getString("loginUserName", "") val password = settings.getString("loginUserPassword", "") - val header = "Basic " + Base64.encodeToString("$userName:$password".toByteArray(), Base64.NO_WRAP) + val header = "Basic ${Base64.encodeToString("$userName:$password".toByteArray(), Base64.NO_WRAP)}" mRealm.executeTransactionAsync({ realm: Realm -> val users = realm.where(RealmUserModel::class.java).isNotEmpty("_id").findAll() for (userModel in users) { @@ -76,7 +80,7 @@ object TransactionSyncManager { } private fun syncHealthData(userModel: RealmUserModel?, header: String) { - val table = "userdb-" + userModel?.planetCode?.let { Utilities.toHex(it) } + "-" + userModel?.name?.let { Utilities.toHex(it) } + val table = "userdb-${userModel?.planetCode?.let { Utilities.toHex(it) }}-${userModel?.name?.let { Utilities.toHex(it) }}" val apiInterface = client?.create(ApiInterface::class.java) val response: Response? try { diff --git a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt index b02edea1c4..0085d260af 100644 --- a/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt +++ b/app/src/main/java/org/ole/planet/myplanet/ui/sync/SyncActivity.kt @@ -193,7 +193,12 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers return withContext(Dispatchers.IO) { val apiInterface = client?.create(ApiInterface::class.java) try { - val response = apiInterface?.isPlanetAvailable("$processedUrl/_all_dbs")?.execute() + Log.d("SyncActivity", "isServerReachable: $processedUrl") + val response = if (settings.getBoolean("isAlternativeUrl", false)){ + apiInterface?.isPlanetAvailable("$processedUrl/db/_all_dbs")?.execute() + } else { + apiInterface?.isPlanetAvailable("$processedUrl/_all_dbs")?.execute() + } when { response?.isSuccessful == true -> { @@ -763,9 +768,15 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers 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() + editor.putString("serverPin", password) + editor.putString("url_user", urlUser) + editor.putString("url_pwd", urlPwd) + editor.putString("url_Scheme", uri.scheme) + editor.putString("url_Host", uri.host) + editor.putString("alternativeUrl", url) + editor.putString("processedAlternativeUrl", couchdbURL) + editor.putBoolean("isAlternativeUrl", true) + editor.apply() processedUrl = couchdbURL } else { @@ -781,11 +792,13 @@ abstract class SyncActivity : ProcessUserDataActivity(), SyncListener, CheckVers Service(this).isPlanetAvailable(object : PlanetAvailableListener { override fun isAvailable() { Service(context).checkVersion(this@SyncActivity, settings) + Log.d("SyncActivity", "isAvailable: true") } override fun notAvailable() { if (!isFinishing) { syncFailed = true showAlert(context, "Error", getString(R.string.planet_server_not_reachable)) + Log.d("SyncActivity", "isAvailable: false") } } }) diff --git a/app/src/main/java/org/ole/planet/myplanet/utilities/Utilities.kt b/app/src/main/java/org/ole/planet/myplanet/utilities/Utilities.kt index 70d04a90da..fc3f5c7ea1 100644 --- a/app/src/main/java/org/ole/planet/myplanet/utilities/Utilities.kt +++ b/app/src/main/java/org/ole/planet/myplanet/utilities/Utilities.kt @@ -104,6 +104,7 @@ object Utilities { imageView.setImageResource(R.drawable.ole_logo) } } + fun handleCheck(b: Boolean, i: Int, selectedItems: MutableList, list: List) { if (b) { selectedItems.add(list[i]) @@ -115,20 +116,25 @@ object Utilities { val header: String get() { val settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) + Log.d("Basic auth", "${settings.getString("url_user", "")}:${ settings.getString("url_pwd", "") }") + return "Basic ${Base64.encodeToString(("${settings.getString("url_user", "")}:${ settings.getString("url_pwd", "") }").toByteArray(), Base64.NO_WRAP)}" } fun getUrl(): String { val settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) - if (settings.contains("couchdbURL")) { - var url = settings.getString("couchdbURL", "") + var url: String? = "" - if (!url?.endsWith("/db")!!) { - url += "/db" - } - return url + url = if (settings.getBoolean("isAlternativeUrl", false)) { + settings.getString("processedAlternativeUrl", "") + } else { + settings.getString("couchdbURL", "") } - return "" + + if (!url?.endsWith("/db")!!) { + url += "/db" + } + return url } val hostUrl: String @@ -149,12 +155,19 @@ object Utilities { } fun getUpdateUrl(settings: SharedPreferences): String { - var url = settings.getString("couchdbURL", "") + var url: String? = "" + url = if (settings.getBoolean("isAlternativeUrl", false)) { + settings.getString("processedAlternativeUrl", "") + } else { + settings.getString("couchdbURL", "") + } + if (url != null) { if (url.endsWith("/db")) { url = url.replace("/db", "") } } + return "$url/versions" } @@ -179,7 +192,13 @@ object Utilities { } fun getApkVersionUrl(settings: SharedPreferences): String { - var url = settings.getString("couchdbURL", "") + var url: String? = "" + url = if (settings.getBoolean("isAlternativeUrl", false)){ + settings.getString("processedAlternativeUrl", "") + } else { + settings.getString("couchdbURL", "") + } + if (url != null) { if (url.endsWith("/db")) { url = url.replace("/db", "")