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

Commit

Permalink
Updater: Organize project structure
Browse files Browse the repository at this point in the history
* Optimize the method to determine whether to run Xiaomi system
  • Loading branch information
YuKongA committed Jan 17, 2024
1 parent 6842240 commit 7acd1a9
Show file tree
Hide file tree
Showing 29 changed files with 223 additions and 178 deletions.
5 changes: 3 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".MainApplication"
android:name=".application.MainApplication"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_launcher"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication">
<activity
android:name=".ui.MainActivity"
android:name=".activity.MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.yukonga.update.ui
package top.yukonga.update.activity

import android.content.ClipData
import android.content.ClipboardManager
Expand Down Expand Up @@ -40,28 +40,28 @@ import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import top.yukonga.update.BuildConfig
import top.yukonga.update.R
import top.yukonga.update.databinding.ActivityMainBinding
import top.yukonga.update.databinding.MainContentBinding
import top.yukonga.update.logic.adapter.CustomArrayAdapter
import top.yukonga.update.activity.adapter.CustomArrayAdapter
import top.yukonga.update.logic.data.DeviceInfoHelper
import top.yukonga.update.logic.data.RecoveryRomInfoHelper
import top.yukonga.update.logic.fadInAnimation
import top.yukonga.update.logic.fadOutAnimation
import top.yukonga.update.logic.setTextAnimation
import top.yukonga.update.logic.utils.AppUtils.atLeastAndroidP
import top.yukonga.update.logic.utils.AppUtils.atLeastAndroidQ
import top.yukonga.update.logic.utils.AppUtils.atLeastAndroidR
import top.yukonga.update.logic.data.RomInfoHelper
import top.yukonga.update.logic.utils.AndroidUtils.atLeastAndroidP
import top.yukonga.update.logic.utils.AndroidUtils.atLeastAndroidQ
import top.yukonga.update.logic.utils.AndroidUtils.atLeastAndroidR
import top.yukonga.update.logic.utils.AppUtils.dp
import top.yukonga.update.logic.utils.AppUtils.hapticConfirm
import top.yukonga.update.logic.utils.AppUtils.hapticReject
import top.yukonga.update.logic.utils.AppUtils.isLandscape
import top.yukonga.update.logic.utils.FileUtils
import top.yukonga.update.logic.utils.FileUtils.downloadRomFile
import top.yukonga.update.logic.utils.HapticUtils.hapticConfirm
import top.yukonga.update.logic.utils.HapticUtils.hapticReject
import top.yukonga.update.logic.utils.InfoUtils.getRecoveryRomInfo
import top.yukonga.update.logic.utils.JsonUtils.json
import top.yukonga.update.logic.utils.LoginUtils
import top.yukonga.update.logic.utils.miuiStringToast.MiuiStringToast.showStringToast
import top.yukonga.update.logic.viewModel.MainViewModel
import top.yukonga.update.miuiStringToast.MiuiStringToast.showStringToast
import top.yukonga.update.activity.viewModel.MainViewModel
import top.yukonga.update.databinding.ActivityMainBinding
import top.yukonga.update.databinding.MainContentBinding
import top.yukonga.update.logic.utils.AnimUtils.fadInAnimation
import top.yukonga.update.logic.utils.AnimUtils.fadOutAnimation
import top.yukonga.update.logic.utils.AnimUtils.setTextAnimation

class MainActivity : AppCompatActivity() {

Expand Down Expand Up @@ -133,7 +133,7 @@ class MainActivity : AppCompatActivity() {
val systemVersionTextExt = systemVersion.replace("OS1", "V816").replace("AUTO", deviceCode)

// Acquire ROM info.
val recoveryRomInfo = json.decodeFromString<RecoveryRomInfoHelper.RomInfo>(
val recoveryRomInfo = json.decodeFromString<RomInfoHelper.RomInfo>(
getRecoveryRomInfo(
this@MainActivity, codeNameExt, regionCode, systemVersionTextExt, androidVersion
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.yukonga.update.logic.adapter
package top.yukonga.update.activity.adapter

import android.content.Context
import android.text.TextUtils
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.yukonga.update.logic.view
package top.yukonga.update.activity.view

import android.content.Context
import android.util.AttributeSet
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.yukonga.update.logic.viewModel
package top.yukonga.update.activity.viewModel

import androidx.lifecycle.ViewModel

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.yukonga.update
package top.yukonga.update.application

import android.app.Application
import com.google.android.material.color.DynamicColors
Expand Down
35 changes: 0 additions & 35 deletions app/src/main/kotlin/top/yukonga/update/logic/UpdaterExtensions.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package top.yukonga.update.logic.data
import kotlinx.serialization.Serializable

@Serializable
data class AuthorizeInfoHelper(
data class AuthorizeHelper(
val description: String? = null,
val location: String? = null,
val nonce: Long? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package top.yukonga.update.logic.data
import kotlinx.serialization.Serializable

@Serializable
data class LoginInfoHelper(
data class LoginHelper(
val accountType: String,
val authResult: String,
val description: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package top.yukonga.update.logic.data
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

object RecoveryRomInfoHelper {
object RomInfoHelper {

@Serializable
data class RomInfo(
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/AndroidUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package top.yukonga.update.logic.utils

import android.os.Build

object AndroidUtils {

fun atLeastAndroidP(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P

fun atLeastAndroidQ(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

fun atLeastAndroidR(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R

fun atLeastAndroidT(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU

}
38 changes: 38 additions & 0 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/AnimUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package top.yukonga.update.logic.utils

import android.graphics.Typeface
import android.view.View
import android.widget.TextView
import androidx.core.view.isVisible

object AnimUtils {
fun TextView.setTextAnimation(text: CharSequence?, duration: Long = 300, completion: (() -> Unit)? = null) {
if (!this.text.contentEquals(text) || !this.isVisible) {
fadOutAnimation(duration) {
this.text = text
fadInAnimation(duration) {
completion?.invoke()
}
}
this.typeface = Typeface.MONOSPACE
}
}

// ViewExtensions

fun View.fadOutAnimation(duration: Long = 300, visibility: Int = View.GONE, completion: (() -> Unit)? = null) {
animate().alpha(0f).setDuration(duration).withEndAction {
this.visibility = visibility
completion?.invoke()
}
}

fun View.fadInAnimation(duration: Long = 300, completion: (() -> Unit)? = null) {
alpha = 0f
visibility = View.VISIBLE
animate().alpha(1f).setDuration(duration).withEndAction {
completion?.invoke()
}

}
}
88 changes: 0 additions & 88 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/AppUtils.kt
Original file line number Diff line number Diff line change
@@ -1,102 +1,14 @@
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
import android.text.TextUtils
import android.util.TypedValue
import android.view.HapticFeedbackConstants
import android.view.View
import java.io.BufferedReader
import java.io.File
import java.io.FileInputStream
import java.io.IOException
import java.io.InputStreamReader
import java.util.Properties

object AppUtils {

fun hapticConfirm(view: View) {
if (atLeastAndroidR()) view.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
else view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}

fun hapticReject(view: View) {
if (atLeastAndroidR()) view.performHapticFeedback(HapticFeedbackConstants.REJECT)
else view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}

fun atLeastAndroidP(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P

fun atLeastAndroidQ(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q

fun atLeastAndroidR(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.R

fun atLeastAndroidT(): Boolean = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU

fun isXiaomi(): Boolean = getProp("ro.miui.ui.version.code").isNotEmpty() && getProp("ro.miui.ui.version.name").isNotEmpty()

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()

fun getProp(name: String): String {
var prop = getPropByShell(name)
if (!TextUtils.isEmpty(prop)) return prop
prop = getPropByStream(name)
if (!TextUtils.isEmpty(prop)) return prop
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) return getPropByReflect(name)
return prop
}

private fun getPropByShell(propName: String): String {
var input: BufferedReader? = null
try {
val p = Runtime.getRuntime().exec("getprop $propName")
input = BufferedReader(InputStreamReader(p.inputStream), 1024)
val ret = input.readLine()
if (ret != null) return ret
} catch (ignore: IOException) {
} finally {
if (input != null) {
try {
input.close()
} catch (_: IOException) {
}
}
}
return ""
}

private fun getPropByStream(key: String): String {
try {
val prop = Properties()
val iS = FileInputStream(
File(Environment.getRootDirectory(), "build.prop")
)
prop.load(iS)
return prop.getProperty(key, "")
} catch (_: Exception) {
}
return ""
}

private fun getPropByReflect(key: String): String {
try {
@SuppressLint("PrivateApi") val clz = Class.forName("android.os.SystemProperties")
val getMethod = clz.getMethod("get", String::class.java, String::class.java)
return getMethod.invoke(clz, key, "") as String
} catch (_: Exception) {
}
return ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import android.content.Context
import android.os.Environment
import androidx.core.net.toUri
import top.yukonga.update.R
import top.yukonga.update.logic.utils.miuiStringToast.MiuiStringToast.showStringToast
import top.yukonga.update.miuiStringToast.MiuiStringToast.showStringToast
import java.io.File

object FileUtils {

private fun cookiesFile(context: Context): File {
return File(context.filesDir, "cookies.json")
}
Expand Down Expand Up @@ -42,4 +43,5 @@ object FileUtils {
showStringToast(context, context.getString(R.string.download_start), 1)
}
}

}
18 changes: 18 additions & 0 deletions app/src/main/kotlin/top/yukonga/update/logic/utils/HapticUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package top.yukonga.update.logic.utils

import android.view.HapticFeedbackConstants
import android.view.View

object HapticUtils {

fun hapticConfirm(view: View) {
if (AndroidUtils.atLeastAndroidR()) view.performHapticFeedback(HapticFeedbackConstants.CONFIRM)
else view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}

fun hapticReject(view: View) {
if (AndroidUtils.atLeastAndroidR()) view.performHapticFeedback(HapticFeedbackConstants.REJECT)
else view.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.content.Context
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.FormBody
import top.yukonga.update.logic.data.LoginInfoHelper
import top.yukonga.update.logic.data.LoginHelper
import top.yukonga.update.logic.data.RequestParamHelper
import top.yukonga.update.logic.utils.CryptoUtils.miuiDecrypt
import top.yukonga.update.logic.utils.CryptoUtils.miuiEncrypt
Expand Down Expand Up @@ -40,7 +40,7 @@ object InfoUtils {
var port = "1"
if (FileUtils.isCookiesFileExists(context)) {
val cookiesFile = FileUtils.readCookiesFile(context)
val cookies = json.decodeFromString<LoginInfoHelper>(cookiesFile)
val cookies = json.decodeFromString<LoginHelper>(cookiesFile)
userId = cookies.userId
accountType = cookies.accountType.ifEmpty { "CN" }
securityKey = Base64.getDecoder().decode((cookies.ssecurity))
Expand All @@ -55,4 +55,5 @@ object InfoUtils {
val requestedEncryptedText = postRequest.body?.string() ?: ""
return miuiDecrypt(requestedEncryptedText, securityKey)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ package top.yukonga.update.logic.utils
import kotlinx.serialization.json.Json

object JsonUtils {

val json = Json { ignoreUnknownKeys = true }

}
Loading

0 comments on commit 7acd1a9

Please sign in to comment.