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

Commit

Permalink
feat: Cleanup MiuiStringToast
Browse files Browse the repository at this point in the history
  • Loading branch information
YuKongA committed May 3, 2024
1 parent 249f8de commit 5cfbc73
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 119 deletions.
48 changes: 11 additions & 37 deletions app/src/main/kotlin/top/yukonga/miuiStringToast/MiuiStringToast.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package top.yukonga.miuiStringToast

import StringToastBundle
import android.annotation.SuppressLint
import android.app.PendingIntent
import android.content.Context
Expand All @@ -23,45 +24,25 @@ import top.yukonga.update.logic.utils.XiaomiUtils.isMiPad

object MiuiStringToast {

fun newIconParams(
category: String?,
iconResName: String?,
iconType: Int,
iconFormat: String?
): IconParams {
return IconParams(category, iconFormat, iconResName, iconType)
}

@SuppressLint("WrongConstant")
fun showStringToast(
context: Context,
text: String?,
colorType: Int?
) {
fun showStringToast(context: Context, text: String?, colorType: Int) {
if ((!isMiPad() && isLandscape()) || !atLeast(Build.VERSION_CODES.TIRAMISU) || !isHyperOS()) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
return
}
try {
val textParams = TextParams(text, if (colorType == 1) colorToInt("#4CAF50") else colorToInt("#E53935"))
val textParams = TextParams(text, if (colorType == 1) Color.parseColor("#4CAF50") else Color.parseColor("#E53935"))
val left = Left(textParams = textParams)
val iconParams: IconParams = newIconParams(Category.DRAWABLE, if (colorType == 1) "ic_update_toast" else "ic_update_toast_error", 1, FileType.SVG)
val iconParams = IconParams(Category.DRAWABLE, FileType.SVG, "ic_launcher", 1)
val right = Right(iconParams = iconParams)
val stringToastBean = StringToastBean(left, right)
val jsonStr = Json.encodeToString(StringToastBean.serializer(), stringToastBean)
val bundle = StringToastBundle.Builder()
.setPackageName(BuildConfig.APPLICATION_ID)
.setStrongToastCategory(StrongToastCategory.TEXT_BITMAP_INTENT.value)
.setStrongToastCategory(StrongToastCategory.TEXT_BITMAP_INTENT)
.setTarget(PendingIntent.getActivity(context, 0, Intent(context, MainActivity::class.java), PendingIntent.FLAG_IMMUTABLE))
.setDuration(2500L)
.setLevel(0.0f)
.setRapidRate(0.0f)
.setCharge(null as String?)
.setStringToastChargeFlag(0)
.setParam(jsonStr)
.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
Expand All @@ -71,13 +52,6 @@ object MiuiStringToast {
}
}

fun colorToInt(color: String?): Int {
val color1 = Color.parseColor(color)
val color2 = Color.parseColor("#FFFFFF")
val color3 = color1 xor color2
return color3.inv()
}

object Category {
const val RAW = "raw"
const val DRAWABLE = "drawable"
Expand All @@ -91,12 +65,12 @@ object MiuiStringToast {
const val SVG = "svg"
}

enum class StrongToastCategory(var value: String) {
VIDEO_TEXT("video_text"),
VIDEO_BITMAP_INTENT("video_bitmap_intent"),
TEXT_BITMAP("text_bitmap"),
TEXT_BITMAP_INTENT("text_bitmap_intent"),
VIDEO_TEXT_TEXT_VIDEO("video_text_text_video")
object StrongToastCategory {
const val VIDEO_TEXT = "video_text"
const val VIDEO_BITMAP_INTENT = "video_bitmap_intent"
const val TEXT_BITMAP = "text_bitmap"
const val TEXT_BITMAP_INTENT = "text_bitmap_intent"
const val VIDEO_TEXT_TEXT_VIDEO = "video_text_text_video"
}

}
Original file line number Diff line number Diff line change
@@ -1,95 +1,47 @@
package top.yukonga.miuiStringToast

import android.app.PendingIntent
import android.os.Bundle
import kotlinx.serialization.Contextual
import kotlinx.serialization.Serializable

@Serializable
class StringToastBundle {
class StringToastBundle private constructor() {

companion object {
private var mBundle: Bundle = Bundle()
}

@Serializable
class Builder {
private var packageName: String? = null
private var stringToastCategory: String? = null

@Contextual
private var target: PendingIntent? = null
private var duration: Long = 0
private var param: String? = null
private var duration: Long = 2500L
private var level: Float = 0f
private var rapidRate: Float = 0f
private var charge: String? = null
private var stringToastChargeFlag: Int = 0
private var param: String? = null
private var statusBarStrongToast: String? = null

fun setPackageName(name: String?): Builder {
packageName = name
return this
}

fun setStrongToastCategory(category: String?): Builder {
this.stringToastCategory = category
return this
}

fun setTarget(intent: PendingIntent?): Builder {
target = intent
return this
}

fun setDuration(duration: Long): Builder {
this.duration = duration
return this
}

fun setLevel(level: Float): Builder {
this.level = level
return this
}

fun setRapidRate(rate: Float): Builder {
this.rapidRate = rate
return this
}

fun setCharge(charge: String?): Builder {
this.charge = charge
return this
}

fun setStringToastChargeFlag(flag: Int): Builder {
this.stringToastChargeFlag = flag
return this
}

fun setParam(param: String?): Builder {
this.param = param
return this
}

fun setStatusBarStrongToast(status: String?): Builder {
this.statusBarStrongToast = status
return this
}
private var statusBarStrongToast: String? = "show_custom_strong_toast"

fun setPackageName(packageName: String?) = apply { this.packageName = packageName }
fun setStrongToastCategory(category: String) = apply { stringToastCategory = category }
fun setTarget(target: PendingIntent?) = apply { this.target = target }
fun setParam(param: String?) = apply { this.param = param }
fun setDuration(duration: Long) = apply { this.duration = duration }
fun setLevel(level: Float) = apply { this.level = level }
fun setRapidRate(rapidRate: Float) = apply { this.rapidRate = rapidRate }
fun setCharge(charge: String?) = apply { this.charge = charge }
fun setStringToastChargeFlag(stringToastChargeFlag: Int) = apply { this.stringToastChargeFlag = stringToastChargeFlag }
fun setStatusBarStrongToast(statusBarStrongToast: String?) = apply { this.statusBarStrongToast = statusBarStrongToast }

fun onCreate(): Bundle {
mBundle.putString("package_name", packageName)
mBundle.putString("strong_toast_category", stringToastCategory)
mBundle.putParcelable("target", target)
mBundle.putString("param", param)
mBundle.putLong("duration", duration)
mBundle.putFloat("level", level)
mBundle.putFloat("rapid_rate", rapidRate)
mBundle.putString("charge", charge)
mBundle.putInt("string_toast_charge_flag", stringToastChargeFlag)
mBundle.putString("param", param)
mBundle.putString("status_bar_strong_toast", statusBarStrongToast)
return mBundle
}
}

}
9 changes: 0 additions & 9 deletions app/src/main/res/drawable/ic_update_toast.xml

This file was deleted.

9 changes: 0 additions & 9 deletions app/src/main/res/drawable/ic_update_toast_error.xml

This file was deleted.

0 comments on commit 5cfbc73

Please sign in to comment.