Skip to content

Commit

Permalink
sync to cloud url
Browse files Browse the repository at this point in the history
  • Loading branch information
Okuro3499 committed Dec 4, 2024
1 parent 6a486ed commit 3d5039d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 19 deletions.
16 changes: 13 additions & 3 deletions app/src/main/java/org/ole/planet/myplanet/datamanager/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyPlanet?> {
override fun onResponse(call: Call<MyPlanet?>, response: Response<MyPlanet?>) {
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<ResponseBody> {
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
Log.d("Service", "getApkVersion onResponse: $response")
val responses: String?
try {
responses = Gson().fromJson(response.body()?.string(), String::class.java)
Expand All @@ -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)
}
Expand All @@ -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<ResponseBody?> {
override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) {
if (callback != null && response.code() == 200) {
callback.isAvailable()
Log.d("Service", "isAvailable: true")
} else {
callback?.notAvailable()
Log.d("Service", "isAvailable: false")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -50,21 +51,24 @@ object TransactionSyncManager {
fun authenticate(): Boolean {
val apiInterface = client?.create(ApiInterface::class.java)
try {
val response: Response<DocumentResponse>? = 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<DocumentResponse>? = 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
}

fun syncAllHealthData(mRealm: Realm, settings: SharedPreferences, listener: SyncListener) {
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) {
Expand All @@ -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<DocumentResponse>?
try {
Expand Down
21 changes: 17 additions & 4 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 @@ -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 -> {
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
}
}
})
Expand Down
37 changes: 28 additions & 9 deletions app/src/main/java/org/ole/planet/myplanet/utilities/Utilities.kt
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ object Utilities {
imageView.setImageResource(R.drawable.ole_logo)
}
}

fun <T> handleCheck(b: Boolean, i: Int, selectedItems: MutableList<T?>, list: List<T?>) {
if (b) {
selectedItems.add(list[i])
Expand All @@ -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
Expand All @@ -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"
}

Expand All @@ -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", "")
Expand Down

0 comments on commit 3d5039d

Please sign in to comment.