2020-08-26 11:50:02 +00:00
|
|
|
package com.vanced.manager.model
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.graphics.drawable.Drawable
|
2020-08-26 17:21:37 +00:00
|
|
|
import android.os.Build
|
2020-11-05 18:26:27 +00:00
|
|
|
import androidx.databinding.*
|
|
|
|
import com.beust.klaxon.JsonArray
|
2020-09-16 10:40:01 +00:00
|
|
|
import com.beust.klaxon.JsonObject
|
2020-09-06 10:33:04 +00:00
|
|
|
import com.vanced.manager.R
|
2020-08-26 11:50:02 +00:00
|
|
|
import com.vanced.manager.utils.PackageHelper.isPackageInstalled
|
2020-09-20 14:41:28 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
2020-08-26 11:50:02 +00:00
|
|
|
|
|
|
|
open class DataModel(
|
2020-09-29 10:47:38 +00:00
|
|
|
private val jsonObject: ObservableField<JsonObject?>,
|
2020-10-31 19:45:39 +00:00
|
|
|
private val context: Context,
|
|
|
|
val appPkg: String,
|
|
|
|
val appName: String,
|
2020-11-05 18:26:27 +00:00
|
|
|
val appIcon: Drawable?,
|
|
|
|
appVersions: ObservableField<JsonArray<String>>? = null
|
2020-08-26 11:50:02 +00:00
|
|
|
) {
|
2020-08-31 16:12:20 +00:00
|
|
|
|
2020-09-17 10:13:16 +00:00
|
|
|
private val versionCode = ObservableInt()
|
|
|
|
private val installedVersionCode = ObservableInt()
|
|
|
|
|
|
|
|
val isAppInstalled = ObservableBoolean()
|
|
|
|
val versionName = ObservableField<String>()
|
2020-11-05 18:26:27 +00:00
|
|
|
//val versions = ObservableField<Array<AppVersionsModel>>()
|
2020-09-17 10:13:16 +00:00
|
|
|
val installedVersionName = ObservableField<String>()
|
|
|
|
val buttonTxt = ObservableField<String>()
|
|
|
|
val changelog = ObservableField<String>()
|
|
|
|
|
2020-09-20 14:41:28 +00:00
|
|
|
fun fetch() = CoroutineScope(Dispatchers.IO).launch {
|
2020-11-05 18:26:27 +00:00
|
|
|
val jobj = jsonObject.get()
|
2020-09-17 10:13:16 +00:00
|
|
|
isAppInstalled.set(isPackageInstalled(appPkg, context.packageManager))
|
2020-11-05 18:26:27 +00:00
|
|
|
versionName.set(jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable))
|
|
|
|
//versions.set(appVersions?.get()?.value?.convertToAppVersions())
|
2020-09-17 10:13:16 +00:00
|
|
|
installedVersionName.set(getPkgVersionName(isAppInstalled.get(), appPkg))
|
2020-11-05 18:26:27 +00:00
|
|
|
versionCode.set(jobj?.int("versionCode") ?: 0)
|
2020-09-17 10:13:16 +00:00
|
|
|
installedVersionCode.set(getPkgVersionCode(isAppInstalled.get(), appPkg))
|
|
|
|
buttonTxt.set(compareInt(installedVersionCode.get(), versionCode.get()))
|
2020-11-05 18:26:27 +00:00
|
|
|
changelog.set(jobj?.string("changelog") ?: context.getString(R.string.unavailable))
|
2020-08-31 16:12:20 +00:00
|
|
|
}
|
2020-09-17 19:25:44 +00:00
|
|
|
|
|
|
|
init {
|
|
|
|
fetch()
|
2020-11-05 18:26:27 +00:00
|
|
|
jsonObject.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
|
|
|
|
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
|
|
|
|
fetch()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
appVersions?.addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
|
2020-10-31 19:45:39 +00:00
|
|
|
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
|
|
|
|
fetch()
|
|
|
|
}
|
|
|
|
})
|
2020-09-17 19:25:44 +00:00
|
|
|
}
|
2020-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
private fun getPkgVersionName(toCheck: Boolean, pkg: String): String {
|
2020-10-01 10:39:01 +00:00
|
|
|
val pm = context.packageManager
|
2020-08-26 11:50:02 +00:00
|
|
|
return if (toCheck) {
|
2020-10-02 20:30:02 +00:00
|
|
|
pm.getPackageInfo(pkg, 0).versionName.removeSuffix("-vanced")
|
2020-08-26 11:50:02 +00:00
|
|
|
} else {
|
|
|
|
context.getString(R.string.unavailable)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Suppress("DEPRECATION")
|
2020-08-31 16:12:20 +00:00
|
|
|
private fun getPkgVersionCode(toCheck: Boolean, pkg: String): Int {
|
2020-08-26 11:50:02 +00:00
|
|
|
return if (toCheck) {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
|
2020-08-26 17:21:37 +00:00
|
|
|
context.packageManager.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF).toInt()
|
2020-08-26 11:50:02 +00:00
|
|
|
else
|
2020-08-26 17:21:37 +00:00
|
|
|
context.packageManager.getPackageInfo(pkg, 0).versionCode
|
2020-08-26 11:50:02 +00:00
|
|
|
} else 0
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun compareInt(int1: Int, int2: Int): String {
|
|
|
|
return when {
|
|
|
|
int1 == 0 -> context.getString(R.string.install)
|
|
|
|
int2 > int1 -> context.getString(R.string.update)
|
|
|
|
int2 == int1 || int1 > int2 -> context.getString(R.string.button_reinstall)
|
|
|
|
else -> context.getString(R.string.install)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|