Skip to content

Commit

Permalink
OP QS Header: Refactor and fix missing playback state method
Browse files Browse the repository at this point in the history
Signed-off-by: DrDisagree <[email protected]>
  • Loading branch information
Mahmud0808 committed Sep 5, 2024
1 parent 254576e commit c28397a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 115 deletions.
197 changes: 82 additions & 115 deletions app/src/main/java/com/drdisagree/iconify/xposed/modules/OpQsHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
private var mMediaArtwork: Bitmap? = null
private var mPreviousMediaArtwork: Bitmap? = null
private var mPreviousMediaProcessedArtwork: Bitmap? = null

private var mInternetEnabled = false
private var mBluetoothEnabled = false
private var mMediaIsPlaying = false

private lateinit var appContext: Context
Expand Down Expand Up @@ -153,12 +156,6 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
private lateinit var opMediaBackgroundDrawable: GradientDrawable
private lateinit var mediaSessionLegacyHelperClass: Class<*>

private var deferredInternetActiveColorAction: (() -> Unit)? = null
private var deferredInternetInactiveColorAction: (() -> Unit)? = null
private var deferredBluetoothActiveColorAction: (() -> Unit)? = null
private var deferredBluetoothInactiveColorAction: (() -> Unit)? = null
private var deferredMediaPlayerInactiveColorAction: (() -> Unit)? = null

private var lastUpdateTime = 0L
private var cooldownTime = 50 // milliseconds

Expand Down Expand Up @@ -205,10 +202,6 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
"$SYSTEMUI_PACKAGE.qs.QSPanelControllerBase",
loadPackageParam.classLoader
)
val qsPanelControllerClass = findClass(
"$SYSTEMUI_PACKAGE.qs.QSPanelController",
loadPackageParam.classLoader
)
val qsSecurityFooterUtilsClass = findClassIfExists(
"$SYSTEMUI_PACKAGE.qs.QSSecurityFooterUtils",
loadPackageParam.classLoader
Expand All @@ -217,9 +210,10 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
"$SYSTEMUI_PACKAGE.qs.QuickStatusBarHeader",
loadPackageParam.classLoader
)
val scrimControllerClass = findClass(
"$SYSTEMUI_PACKAGE.statusbar.phone.ScrimController",
loadPackageParam.classLoader
val shadeHeaderControllerClass = findClassInArray(
loadPackageParam.classLoader,
"$SYSTEMUI_PACKAGE.shade.ShadeHeaderController",
"$SYSTEMUI_PACKAGE.shade.LargeScreenShadeHeaderController",
)
val dependencyClass = findClass(
"$SYSTEMUI_PACKAGE.Dependency",
Expand Down Expand Up @@ -333,15 +327,41 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
}
})

hookAllConstructors(qsPanelControllerClass, object : XC_MethodHook() {
hookAllMethods(shadeHeaderControllerClass, "onInit", object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
onColorsInitialized()
}
})
val configurationControllerListener = getObjectField(
param.thisObject,
"configurationControllerListener"
)

hookAllMethods(scrimControllerClass, "updateThemeColors", object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
onColorsInitialized()
val updateColors = object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
if (qsTileViewImplInstance != null) {
initResources()

updateInternetTileColors()
updateBluetoothTileColors()
}
}
}

val methods = listOf(
"onConfigChanged",
"onDensityOrFontScaleChanged",
"onUiModeChanged",
"onThemeChanged"
)

for (method in methods) {
try {
hookAllMethods(
configurationControllerListener.javaClass,
method,
updateColors
)
} catch (ignored: Throwable) {
}
}
}
})

Expand Down Expand Up @@ -920,6 +940,7 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
private fun updateInternetState() {
val isWiFiConnected = isWiFiConnected
val isMobileDataConnected = isMobileDataConnected
mInternetEnabled = isWiFiConnected || isMobileDataConnected

val internetLabel: CharSequence = mContext.getString(
mContext.resources.getIdentifier(
Expand All @@ -934,7 +955,7 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
SYSTEMUI_PACKAGE
)

if (isWiFiConnected || isMobileDataConnected) {
if (mInternetEnabled) {
if (isWiFiConnected) {
val signalLevel = getWiFiSignalStrengthLevel()
val wifiIconResId = when (signalLevel) {
Expand Down Expand Up @@ -977,14 +998,12 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
}
mQsOpHeaderView?.setInternetIcon(mobileDataIconResId)
}

updateInternetActiveColors()
} else {
mQsOpHeaderView?.setInternetText(internetLabel)
mQsOpHeaderView?.setInternetIcon(noInternetIconResId)

updateInternetInactiveColors()
}

updateInternetTileColors()
}

private val isWiFiConnected: Boolean
Expand Down Expand Up @@ -1141,10 +1160,10 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
}

private fun updateBluetoothState(enabled: Boolean = isBluetoothEnabled) {
mBluetoothEnabled = enabled

if (enabled) {
mQsOpHeaderView?.setBlueToothText(getBluetoothConnectedDevice())

updateBluetoothActiveColors()
} else {
mQsOpHeaderView?.setBlueToothText(
mContext.resources.getIdentifier(
Expand All @@ -1153,9 +1172,9 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
SYSTEMUI_PACKAGE
)
)

updateBluetoothInactiveColors()
}

updateBluetoothTileColors()
}

private val isMediaControllerAvailable: Boolean
Expand Down Expand Up @@ -1272,13 +1291,12 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
}

private val isMediaPlaying: Boolean
get() {
return (isMediaControllerAvailable && PlaybackState.STATE_PLAYING == callMethod(
mNotificationMediaManager,
"getMediaControllerPlaybackState",
mMediaController
))
}
get() = (isMediaControllerAvailable
&& PlaybackState.STATE_PLAYING == getMediaControllerPlaybackState(mMediaController))

private fun getMediaControllerPlaybackState(controller: MediaController?): Int {
return controller?.playbackState?.state ?: PlaybackState.STATE_NONE
}

private fun updateMediaPlayerView() {
val currentTime = System.currentTimeMillis()
Expand Down Expand Up @@ -1427,7 +1445,10 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
)

if (processedArtwork == null || onDominantColor == null) {
updateMediaPlayerInactiveColors()
mQsOpHeaderView?.setMediaPlayerItemsColor(colorLabelInactive)
colorInactive?.let {
mQsOpHeaderView?.mediaPlayerBackground?.setColorFilter(it)
}
} else {
val derivedOnDominantColor = if (mediaFadeLevel > 20) {
colorLabelInactive ?: onDominantColor
Expand Down Expand Up @@ -1461,95 +1482,37 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {

if (mQsOpHeaderView == null) return

deferredInternetActiveColorAction?.invoke()
deferredInternetInactiveColorAction?.invoke()
deferredBluetoothActiveColorAction?.invoke()
deferredBluetoothInactiveColorAction?.invoke()
deferredMediaPlayerInactiveColorAction?.invoke()

updateInternetState()
updateBluetoothState()
updateMediaPlayer(force = true)
}

private fun updateInternetActiveColors() {
if (colorActive != null && colorLabelActive != null) {
applyInternetActiveColors()
} else {
deferredInternetActiveColorAction = { applyInternetActiveColors() }
}
}

private fun applyInternetActiveColors() {
mQsOpHeaderView?.setInternetTileColor(
tileColor = colorActive,
labelColor = colorLabelActive
)
deferredInternetActiveColorAction = null
}

private fun updateInternetInactiveColors() {
if (colorInactive != null && colorLabelInactive != null) {
applyInternetInactiveColors()
} else {
deferredInternetInactiveColorAction = { applyInternetInactiveColors() }
}
}

private fun applyInternetInactiveColors() {
mQsOpHeaderView?.setInternetTileColor(
tileColor = colorInactive,
labelColor = colorLabelInactive
)
deferredInternetInactiveColorAction = null
}

private fun updateBluetoothActiveColors() {
if (colorActive != null && colorLabelActive != null) {
applyBluetoothActiveColors()
} else {
deferredBluetoothActiveColorAction = { applyBluetoothActiveColors() }
}
}

private fun applyBluetoothActiveColors() {
mQsOpHeaderView?.setBluetoothTileColor(
tileColor = colorActive,
labelColor = colorLabelActive
)
deferredBluetoothActiveColorAction = null
}

private fun updateBluetoothInactiveColors() {
if (colorInactive != null && colorLabelInactive != null) {
applyBluetoothInactiveColors()
private fun updateInternetTileColors() {
if (mInternetEnabled) {
mQsOpHeaderView?.setInternetTileColor(
tileColor = colorActive,
labelColor = colorLabelActive
)
} else {
deferredBluetoothInactiveColorAction = { applyBluetoothInactiveColors() }
mQsOpHeaderView?.setInternetTileColor(
tileColor = colorInactive,
labelColor = colorLabelInactive
)
}
}

private fun applyBluetoothInactiveColors() {
mQsOpHeaderView?.setBluetoothTileColor(
tileColor = colorInactive,
labelColor = colorLabelInactive
)
deferredBluetoothInactiveColorAction = null
}

private fun updateMediaPlayerInactiveColors() {
if (colorLabelInactive != null) {
applyInactiveMediaPlayerColors()
private fun updateBluetoothTileColors() {
if (mBluetoothEnabled) {
mQsOpHeaderView?.setBluetoothTileColor(
tileColor = colorActive,
labelColor = colorLabelActive
)
} else {
deferredMediaPlayerInactiveColorAction = { applyInactiveMediaPlayerColors() }
}
}

private fun applyInactiveMediaPlayerColors() {
mQsOpHeaderView?.setMediaPlayerItemsColor(colorLabelInactive)
colorInactive?.let {
mQsOpHeaderView?.mediaPlayerBackground?.setColorFilter(it)
mQsOpHeaderView?.setBluetoothTileColor(
tileColor = colorInactive,
labelColor = colorLabelInactive
)
}
deferredMediaPlayerInactiveColorAction = null
}

private suspend fun processArtwork(
Expand Down Expand Up @@ -1844,6 +1807,10 @@ class OpQsHeader(context: Context?) : ModPack(context!!) {
mWifiManager = getSystemService(WifiManager::class.java)
mBluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
}

mInternetEnabled = isWiFiConnected || isMobileDataConnected
mBluetoothEnabled = isBluetoothEnabled
mMediaIsPlaying = isMediaPlaying
}

private val isLandscape: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.drdisagree.iconify.xposed.modules.views
import android.content.Context
import android.content.pm.PackageManager
import android.content.res.ColorStateList
import android.content.res.Configuration
import android.graphics.Color
import android.graphics.Typeface
import android.graphics.drawable.Drawable
Expand Down Expand Up @@ -68,6 +69,7 @@ class QsOpHeaderView(private val mContext: Context) : LinearLayout(mContext) {
private lateinit var opMediaPlayIconDrawable: Drawable
private lateinit var opMediaPauseIconDrawable: Drawable

private var mOnConfigurationChanged: ((Configuration?) -> Unit)? = null
private var mOnAttach: (() -> Unit)? = null
private var mOnDetach: (() -> Unit)? = null

Expand Down Expand Up @@ -714,6 +716,10 @@ class QsOpHeaderView(private val mContext: Context) : LinearLayout(mContext) {
this.mOnDetach = onDetach
}

fun setOnConfigurationChangedListener(onConfigurationChanged: (newConfig: Configuration?) -> Unit) {
this.mOnConfigurationChanged = onConfigurationChanged
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
mOnAttach?.invoke()
Expand All @@ -723,4 +729,9 @@ class QsOpHeaderView(private val mContext: Context) : LinearLayout(mContext) {
super.onDetachedFromWindow()
mOnDetach?.invoke()
}

override fun onConfigurationChanged(newConfig: Configuration?) {
super.onConfigurationChanged(newConfig)
mOnConfigurationChanged?.invoke(newConfig)
}
}

0 comments on commit c28397a

Please sign in to comment.