Skip to content

Commit

Permalink
Refactor HookEntry class
Browse files Browse the repository at this point in the history
Signed-off-by: DrDisagree <[email protected]>
  • Loading branch information
Mahmud0808 committed Nov 11, 2024
1 parent b03376b commit 1583c6e
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions app/src/main/java/com/drdisagree/iconify/xposed/HookEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.lang.reflect.InvocationTargetException
import java.util.LinkedList
import java.util.Queue
import java.util.concurrent.CompletableFuture

class HookEntry : ServiceConnection {

private var mContext: Context? = null
private lateinit var mContext: Context

init {
instance = this
Expand All @@ -57,15 +58,15 @@ class HookEntry : ServiceConnection {
hookAllMethods(phoneWindowManagerClass, "init", object : XC_MethodHook() {
override fun beforeHookedMethod(param: MethodHookParam) {
try {
if (mContext == null) {
if (!::mContext.isInitialized) {
mContext = param.args[0] as Context

HookRes.modRes = mContext!!.createPackageContext(
HookRes.modRes = mContext.createPackageContext(
BuildConfig.APPLICATION_ID,
Context.CONTEXT_IGNORE_SECURITY
).resources

XPrefs.init(mContext!!)
XPrefs.init(mContext)

CompletableFuture.runAsync { waitForXprefsLoad(loadPackageParam) }
}
Expand All @@ -87,15 +88,15 @@ class HookEntry : ServiceConnection {
object : XC_MethodHook() {
override fun afterHookedMethod(param: MethodHookParam) {
try {
if (mContext == null) {
if (!::mContext.isInitialized) {
mContext = param.args[2] as Context

HookRes.modRes = mContext!!.createPackageContext(
HookRes.modRes = mContext.createPackageContext(
BuildConfig.APPLICATION_ID,
Context.CONTEXT_IGNORE_SECURITY
).resources

XPrefs.init(mContext!!)
XPrefs.init(mContext)

waitForXprefsLoad(loadPackageParam)
}
Expand All @@ -112,11 +113,11 @@ class HookEntry : ServiceConnection {

private fun onXPrefsReady(loadPackageParam: LoadPackageParam) {
if (!isChildProcess && BootLoopProtector.isBootLooped(loadPackageParam.packageName)) {
log("Possible bootloop in ${loadPackageParam.packageName} ; Iconify will not load for now...")
log("Possible crash in ${loadPackageParam.packageName} ; Iconify will not load for now...")
return
}

SystemUtils(mContext!!)
SystemUtils(mContext)

loadModPacks(loadPackageParam)
}
Expand All @@ -136,13 +137,18 @@ class HookEntry : ServiceConnection {

try {
instance.updatePrefs()
} catch (ignored: Throwable) {
} catch (throwable: Throwable) {
log(TAG + "Failed to update prefs in ${mod.name}")
log(TAG + throwable)
}

instance.handleLoadPackage(loadPackageParam)
runningMods.add(instance)
} catch (invocationTargetException: InvocationTargetException) {
log(TAG + "Start Error Dump - Occurred in ${mod.name}")
log(TAG + invocationTargetException.cause)
} catch (throwable: Throwable) {
log("Start Error Dump - Occurred in ${mod.name}")
log(TAG + "Start Error Dump - Occurred in ${mod.name}")
log(TAG + throwable)
}
}
Expand All @@ -165,7 +171,7 @@ class HookEntry : ServiceConnection {

private fun forceConnectRootService() {
CoroutineScope(Dispatchers.Main).launch {
val mUserManager = mContext!!.getSystemService(Context.USER_SERVICE) as UserManager?
val mUserManager = mContext.getSystemService(Context.USER_SERVICE) as UserManager?

withContext(Dispatchers.IO) {
while (mUserManager == null || !mUserManager.isUserUnlocked) {
Expand Down Expand Up @@ -197,7 +203,7 @@ class HookEntry : ServiceConnection {
)
}

mContext!!.bindService(
mContext.bindService(
intent,
instance!!,
Context.BIND_AUTO_CREATE or Context.BIND_ADJUST_WITH_ACTIVITY
Expand Down

0 comments on commit 1583c6e

Please sign in to comment.