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-09-16 10:40:01 +00:00
|
|
|
import androidx.core.content.ContextCompat
|
2020-09-17 10:13:16 +00:00
|
|
|
import androidx.databinding.ObservableBoolean
|
|
|
|
import androidx.databinding.ObservableField
|
|
|
|
import androidx.databinding.ObservableInt
|
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?>,
|
|
|
|
private val appPkg: String,
|
2020-08-26 11:50:02 +00:00
|
|
|
private val context: Context
|
|
|
|
) {
|
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>()
|
|
|
|
val installedVersionName = ObservableField<String>()
|
|
|
|
val buttonTxt = ObservableField<String>()
|
|
|
|
val buttonIcon = ObservableField<Drawable>()
|
|
|
|
val changelog = ObservableField<String>()
|
|
|
|
|
2020-09-20 14:41:28 +00:00
|
|
|
fun fetch() = CoroutineScope(Dispatchers.IO).launch {
|
2020-09-17 10:13:16 +00:00
|
|
|
isAppInstalled.set(isPackageInstalled(appPkg, context.packageManager))
|
2020-09-29 10:47:38 +00:00
|
|
|
versionName.set(jsonObject.get()?.string("version")?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable))
|
2020-09-17 10:13:16 +00:00
|
|
|
installedVersionName.set(getPkgVersionName(isAppInstalled.get(), appPkg))
|
2020-09-29 10:47:38 +00:00
|
|
|
versionCode.set(jsonObject.get()?.int("versionCode") ?: 0)
|
2020-09-17 10:13:16 +00:00
|
|
|
installedVersionCode.set(getPkgVersionCode(isAppInstalled.get(), appPkg))
|
|
|
|
buttonTxt.set(compareInt(installedVersionCode.get(), versionCode.get()))
|
|
|
|
buttonIcon.set(compareIntDrawable(installedVersionCode.get(), versionCode.get()))
|
2020-09-29 10:47:38 +00:00
|
|
|
changelog.set(jsonObject.get()?.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-08-26 11:50:02 +00:00
|
|
|
|
2020-08-31 16:12:20 +00:00
|
|
|
private fun getPkgVersionName(toCheck: Boolean, pkg: String): String {
|
2020-08-26 11:50:02 +00:00
|
|
|
return if (toCheck) {
|
2020-08-31 16:12:20 +00:00
|
|
|
context.packageManager.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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun compareIntDrawable(int1: Int, int2: Int): Drawable? {
|
|
|
|
return when {
|
2020-09-16 10:40:01 +00:00
|
|
|
int1 == 0 -> ContextCompat.getDrawable(context, R.drawable.ic_download)
|
|
|
|
int2 > int1 -> ContextCompat.getDrawable(context, R.drawable.ic_update)
|
|
|
|
int2 == int1 -> ContextCompat.getDrawable(context, R.drawable.ic_done)
|
|
|
|
else -> ContextCompat.getDrawable(context, R.drawable.ic_download)
|
2020-08-26 11:50:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|