Skip to content

Commit

Permalink
LS Clock: Add lottie animation view support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahmud0808 committed Apr 11, 2024
1 parent 79f47b5 commit 0a22e90
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,5 @@ object Resources {
// Resource names
const val HEADER_CLOCK_LAYOUT = "preview_header_clock_"
const val LOCKSCREEN_CLOCK_LAYOUT = "preview_lockscreen_clock_"
const val LOCKSCREEN_CLOCK_LOTTIE = "lottie_lockscreen_clock_"
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.drdisagree.iconify.config.RPrefs
import com.drdisagree.iconify.ui.models.ClockModel
import com.drdisagree.iconify.ui.utils.ViewBindingHelpers.setBitmapWithAnimation
import com.drdisagree.iconify.utils.WallpaperUtil
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper
import com.google.android.material.button.MaterialButton
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -90,15 +91,6 @@ class ClockPreviewAdapter(
refreshLayout(this)
}

val adapterPosition = adapterPosition
if (RPrefs.getInt(prefStyle, 0) != adapterPosition) {
checkIcon.setVisibility(View.GONE)
button.setEnabled(true)
} else {
checkIcon.setVisibility(View.VISIBLE)
button.setEnabled(false)
}

clockContainer.removeAllViews()

val viewStub = ViewStub(context)
Expand All @@ -124,6 +116,12 @@ class ClockPreviewAdapter(
if (wallpaperBitmap != null) {
setBitmapWithAnimation(wallpaperView, wallpaperBitmap)
}

ViewHelper.loadLottieAnimationView(
appContext = context,
parent = container,
styleIndex = adapterPosition
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.applyFontRecursive
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.applyTextMarginRecursively
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.applyTextScalingRecursively
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.findViewWithTagAndChangeColor
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.loadLottieAnimationView
import com.drdisagree.iconify.xposed.modules.utils.ViewHelper.setMargins
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XposedBridge.hookAllMethods
import de.robv.android.xposed.XposedBridge.log
import de.robv.android.xposed.XposedHelpers.findClass
import de.robv.android.xposed.XposedHelpers.findClassIfExists
import de.robv.android.xposed.XposedHelpers.getObjectField
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam
import java.io.File
Expand All @@ -92,6 +94,7 @@ class LockscreenClock(context: Context?) : ModPack(context!!) {
private var mBatteryPercentage = 1
private var mVolumeLevelArcProgress: ImageView? = null
private var mRamUsageArcProgress: ImageView? = null
private lateinit var lottieAnimationViewClass: Class<*>
private val mBatteryReceiver: BroadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action != null && intent.action == Intent.ACTION_BATTERY_CHANGED) {
Expand Down Expand Up @@ -141,6 +144,11 @@ class LockscreenClock(context: Context?) : ModPack(context!!) {
override fun handleLoadPackage(loadPackageParam: LoadPackageParam) {
initResources(mContext)

lottieAnimationViewClass = findClassIfExists(
"com.airbnb.lottie.LottieAnimationView",
loadPackageParam.classLoader
)

val keyguardStatusViewClass = findClass(
"com.android.keyguard.KeyguardStatusView",
loadPackageParam.classLoader
Expand Down Expand Up @@ -378,14 +386,24 @@ class LockscreenClock(context: Context?) : ModPack(context!!) {
val inflater = LayoutInflater.from(appContext)
val clockStyle: Int = Xprefs!!.getInt(LSCLOCK_STYLE, 0)

return inflater.inflate(
val view: View = inflater.inflate(
appContext!!.resources.getIdentifier(
LOCKSCREEN_CLOCK_LAYOUT + clockStyle,
"layout",
BuildConfig.APPLICATION_ID
),
null
)

if (::lottieAnimationViewClass.isInitialized) {
loadLottieAnimationView(
appContext = appContext!!,
lottieAnimationViewClass = lottieAnimationViewClass,
parent = view
)
}

return view
}

private fun modifyClockView(clockView: View) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.drdisagree.iconify.xposed.modules.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.PorterDuff
import android.graphics.PorterDuffColorFilter
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams
Expand All @@ -16,9 +19,19 @@ import android.widget.LinearLayout
import android.widget.ProgressBar
import android.widget.RelativeLayout
import android.widget.TextView
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieDrawable
import com.airbnb.lottie.RenderMode
import com.drdisagree.iconify.BuildConfig
import com.drdisagree.iconify.common.Preferences
import com.drdisagree.iconify.common.Resources.LOCKSCREEN_CLOCK_LOTTIE
import com.drdisagree.iconify.config.XPrefs.Xprefs
import de.robv.android.xposed.XposedBridge
import de.robv.android.xposed.XposedHelpers.callMethod
import java.io.InputStream
import java.util.Locale


object ViewHelper {

fun setMargins(viewGroup: Any, context: Context, left: Int, top: Int, right: Int, bottom: Int) {
Expand Down Expand Up @@ -252,4 +265,99 @@ object ViewHelper {
val newSize = originalSize * scaleFactor
view.setTextSize(TypedValue.COMPLEX_UNIT_PX, newSize)
}

fun loadLottieAnimationView(
appContext: Context,
lottieAnimationViewClass: Class<*>? = null,
parent: View,
styleIndex: Int? = null
) {
if (parent !is ViewGroup ||
parent.findViewWithTag<LinearLayout>("lottie") == null ||
(lottieAnimationViewClass == null && styleIndex == null)
) return

var isXposedMode = true
val currentStyleIndex: Int? = try {
Xprefs!!.getInt(Preferences.LSCLOCK_STYLE, 0)
} catch (ignored: Throwable) {
if (styleIndex == null) {
throw IllegalStateException("Parameter \"styleIndex\" cannot be null")
}

isXposedMode = false
styleIndex
}
val rawResName = LOCKSCREEN_CLOCK_LOTTIE + currentStyleIndex

val lottieAnimView: Any = if (isXposedMode) {
if (lottieAnimationViewClass == null) {
throw IllegalStateException("Parameter \"lottieAnimationViewClass\" cannot be null")
}

lottieAnimationViewClass
.getConstructor(Context::class.java)
.newInstance(appContext)
} else {
LottieAnimationView(appContext)
}

val animationParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT
).apply {
gravity = Gravity.CENTER
}

@SuppressLint("DiscouragedApi")
val anim: Int = appContext.resources.getIdentifier(
rawResName,
"raw",
BuildConfig.APPLICATION_ID
)

if (anim == 0x0) {
if (isXposedMode) {
XposedBridge.log("Iconify - ${ViewHelper::class.simpleName}: $rawResName not found")
} else {
Log.w(ViewHelper::class.simpleName, "$rawResName not found")
}
return
}

val rawRes: InputStream = appContext.resources.openRawResource(anim)

if (isXposedMode) {
lottieAnimView.let {
callMethod(it, "setLayoutParams", animationParams)
callMethod(it, "setAnimation", rawRes, "cacheKey")
callMethod(it, "setRepeatCount", LottieDrawable.INFINITE)
callMethod(it, "setScaleType", ImageView.ScaleType.FIT_CENTER)
callMethod(it, "setAdjustViewBounds", true)
callMethod(it, "enableMergePathsForKitKatAndAbove", true)
callMethod(it, "playAnimation")
}
} else {
(lottieAnimView as LottieAnimationView).apply {
layoutParams = animationParams
setAnimation(rawRes, "cacheKey")
repeatCount = LottieDrawable.INFINITE
renderMode = RenderMode.HARDWARE
scaleType = ImageView.ScaleType.FIT_CENTER
adjustViewBounds = true
enableMergePathsForKitKatAndAbove(true)
playAnimation()
}
}

parent.findViewWithTag<LinearLayout>("lottie").let {
it.gravity = Gravity.CENTER

if (isXposedMode) {
callMethod(it, "addView", lottieAnimView);
} else {
it.addView(lottieAnimView as LottieAnimationView)
}
}
}
}

0 comments on commit 0a22e90

Please sign in to comment.