Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
Updater: Add ViewModel support
Browse files Browse the repository at this point in the history
* 此外,我们发现当运行 HyperOS 的手机设备处于横屏状态时,它不会显示灵动额头,所以此时我们回到显示普通 Toast 消息
  • Loading branch information
YuKongA committed Jan 4, 2024
1 parent a782171 commit 58e3b98
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 166 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package top.yukonga.update.logic.utils

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.content.res.Resources.getSystem
import android.os.Build
import android.os.Environment
Expand Down Expand Up @@ -35,6 +36,11 @@ object AppUtils {

fun isHyperOS(): Boolean = if (isXiaomi()) getProp("ro.miui.ui.version.code").toInt() >= 816 else false

fun isTablet(): Boolean =
getSystem().configuration.smallestScreenWidthDp >= 600 || getProp("ro.build.characteristics") == "tablet" || (getSystem().configuration.screenLayout and Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE

fun isLandscape(): Boolean = getSystem().configuration.orientation == Configuration.ORIENTATION_LANDSCAPE

val Int.dp: Int get() = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, this.toFloat(), getSystem().displayMetrics).toInt()

val Int.px: Int get() = (this / getSystem().displayMetrics.density + 0.5f).toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ object FileUtils {
return File(context.filesDir, "cookies.json")
}

fun cookiesFileExists(context: Context): Boolean {
return cookiesFile(context).exists()
}

fun saveCookiesFile(context: Context, data: String) {
cookiesFile(context).writeText(data)
}
Expand All @@ -27,6 +23,10 @@ object FileUtils {
cookiesFile(context).delete()
}

fun isCookiesFileExists(context: Context): Boolean {
return cookiesFile(context).exists()
}

fun downloadRomFile(context: Context, fileLink: String, fileName: String) {
DownloadManager.Request(fileLink.toUri()).apply {
setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ object InfoUtils {
var securityKey = securityKey.toByteArray(Charsets.UTF_8)
var serviceToken = ""
var port = "1"
if (FileUtils.cookiesFileExists(context)) {
if (FileUtils.isCookiesFileExists(context)) {
val cookiesFile = FileUtils.readCookiesFile(context)
val cookies = Gson().fromJson(cookiesFile, MutableMap::class.java)
userId = cookies["userId"].toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import com.google.gson.Gson
import top.yukonga.update.BuildConfig
import top.yukonga.update.logic.utils.AppUtils.atLeastAndroidT
import top.yukonga.update.logic.utils.AppUtils.isHyperOS
import top.yukonga.update.logic.utils.AppUtils.isLandscape
import top.yukonga.update.logic.utils.AppUtils.isTablet
import top.yukonga.update.logic.utils.miuiStringToast.res.IconParams
import top.yukonga.update.logic.utils.miuiStringToast.res.Left
import top.yukonga.update.logic.utils.miuiStringToast.res.Right
Expand All @@ -30,39 +32,38 @@ object MiuiStringToast {

@SuppressLint("WrongConstant")
fun showStringToast(context: Context, text: String?, colorType: Int?) {
if ((!isTablet() && isLandscape()) || !atLeastAndroidT() || !isHyperOS()) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
return
}
try {
if (atLeastAndroidT() && isHyperOS()) {
val textParams = TextParams()
textParams.setText(text)
textParams.setTextColor(if (colorType == 1) colorToInt("#4CAF50") else colorToInt("#E53935"))
val left = Left()
left.setTextParams(textParams)
val iconParams: IconParams = newIconParams(Category.DRAWABLE, if (colorType == 1) "ic_update_toast" else "ic_update_toast_error", 1, FileType.SVG)
val right = Right()
right.setIconParams(iconParams)
val stringToastBean = StringToastBean()
stringToastBean.setLeft(left)
stringToastBean.setRight(right)
val gson = Gson()
val str = gson.toJson(stringToastBean)
val bundle: Bundle = StringToastBundle.Builder()
.setPackageName(BuildConfig.APPLICATION_ID)
.setStrongToastCategory(StrongToastCategory.TEXT_BITMAP.value)
.setTarget(null as PendingIntent?)
.setDuration(2500L)
.setLevel(0.0f)
.setRapidRate(0.0f)
.setCharge(null as String?)
.setStringToastChargeFlag(0)
.setParam(str)
.setStatusBarStrongToast("show_custom_strong_toast")
.onCreate()
val service = context.getSystemService(Context.STATUS_BAR_SERVICE)
service.javaClass.getMethod("setStatus", Int::class.javaPrimitiveType, String::class.java, Bundle::class.java)
.invoke(service, 1, "strong_toast_action", bundle)
} else {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
val textParams = TextParams()
textParams.setText(text)
textParams.setTextColor(if (colorType == 1) colorToInt("#4CAF50") else colorToInt("#E53935"))
val left = Left()
left.setTextParams(textParams)
val iconParams: IconParams = newIconParams(Category.DRAWABLE, if (colorType == 1) "ic_update_toast" else "ic_update_toast_error", 1, FileType.SVG)
val right = Right()
right.setIconParams(iconParams)
val stringToastBean = StringToastBean()
stringToastBean.setLeft(left)
stringToastBean.setRight(right)
val gson = Gson()
val str = gson.toJson(stringToastBean)
val bundle: Bundle = StringToastBundle.Builder()
.setPackageName(BuildConfig.APPLICATION_ID)
.setStrongToastCategory(StrongToastCategory.TEXT_BITMAP.value)
.setTarget(null as PendingIntent?)
.setDuration(2500L)
.setLevel(0.0f)
.setRapidRate(0.0f)
.setCharge(null as String?)
.setStringToastChargeFlag(0)
.setParam(str)
.setStatusBarStrongToast("show_custom_strong_toast")
.onCreate()
val service = context.getSystemService(Context.STATUS_BAR_SERVICE)
service.javaClass.getMethod("setStatus", Int::class.javaPrimitiveType, String::class.java, Bundle::class.java).invoke(service, 1, "strong_toast_action", bundle)
} catch (e: IllegalAccessException) {
throw RuntimeException(e)
} catch (e: InvocationTargetException) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package top.yukonga.update.logic.viewModel

import androidx.lifecycle.ViewModel

class MainViewModel : ViewModel() {
var device: String? = null
var version: String? = null
var codebase: String? = null
var branch: String? = null
var filename: String? = null
var filesize: String? = null
var bigversion: String? = null
var officialDownload: String? = null
var officialText: String? = null
var cdnDownload: String? = null
var changelog: String? = null
}
Loading

0 comments on commit 58e3b98

Please sign in to comment.