美文网首页
Choreographer字段:mCallbackQueues

Choreographer字段:mCallbackQueues

作者: 緦菍亭芷 | 来源:发表于2022-02-24 17:42 被阅读0次
   private var callback0: Any? = null
    private var callback1: Any? = null
    private var callback2: Any? = null
    private var callback3: Any? = null
    private var callback4: Any? = null
    private var method: Method? = null
    private fun hook() {
        var mChoreographer = Choreographer.getInstance()
        var mCallbackQueuesField = mChoreographer.javaClass.getDeclaredField("mCallbackQueues")
        mCallbackQueuesField.isAccessible = true
        var mCallbackQueues = mCallbackQueuesField.get(mChoreographer) as? Array<*>
        if (mCallbackQueues != null && mCallbackQueues.size > 0) {
            LogUtils.i("zengbobo MainActivity hook size = ${mCallbackQueues.size}")
            for (i in mCallbackQueues.indices) {
                var callback: Any? = mCallbackQueues?.get(i) ?: continue
                if (method == null) {
                    method = callback?.javaClass?.getDeclaredMethod(
                        "addCallbackLocked",
                        Long::class.java, Any::class.java, Any::class.java
                    )
                    method?.isAccessible = true
                }
                if (i == 0) {
                    callback0 = callback
                    method?.invoke(callback, 0, runnable1, null)
                } else if (i == 1) {
                    callback1 = callback
                    method?.invoke(callback, 0, runnable2, null)
                } else if (i == 2) {
                    callback2 = callback
                    method?.invoke(callback, 0, runnable3, null)
                } else if (i == 3) {
                    callback3 = callback
                    method?.invoke(callback, 0, runnable4, null)
                } else if (i == 4) {
                    callback4 = callback
                    method?.invoke(callback, 0, runnable5, null)
                }
            }
        }

    }

    private fun hook(i: Int) {
        if (i == 0) {
            method?.invoke(callback0, 0, runnable1, null)
        } else if (i == 1) {
            method?.invoke(callback1, 0, runnable2, null)
        } else if (i == 2) {
            method?.invoke(callback2, 0, runnable3, null)
        } else if (i == 3) {
            method?.invoke(callback3, 0, runnable4, null)
        } else if (i == 4) {
            method?.invoke(callback4, 0, runnable5, null)
        }
    }

    private fun hook1(position: Int) {
        var mChoreographer = Choreographer.getInstance()
        var mCallbackQueuesField = mChoreographer.javaClass.getDeclaredField("mCallbackQueues")
        mCallbackQueuesField.isAccessible = true
        var mCallbackQueues = mCallbackQueuesField.get(mChoreographer) as? Array<*>
        if (mCallbackQueues != null && mCallbackQueues.size > 0) {
            LogUtils.i("zengbobo MainActivity hook position=$position size=${mCallbackQueues.size}")
            for (i in mCallbackQueues.indices) {
                var callback: Any? = mCallbackQueues?.get(i) ?: continue

//                var removeCallbacksLocked = callback?.javaClass?.getDeclaredMethod(
//                    "removeCallbacksLocked", Any::class.java, Any::class.java
//                )
//                removeCallbacksLocked?.isAccessible = true
//                if (i == position) {
//                    removeCallbacksLocked?.invoke(callback, runnable1, null)
//                } else if (i == position) {
//                    removeCallbacksLocked?.invoke(callback,  runnable2, null)
//                } else if (i == position) {
//                    removeCallbacksLocked?.invoke(callback,  runnable3, null)
//                } else if (i == position) {
//                    removeCallbacksLocked?.invoke(callback,  runnable4, null)
//                } else if (i == position) {
//                    removeCallbacksLocked?.invoke(callback,  runnable5, null)
//                }

                var method = callback?.javaClass?.getDeclaredMethod(
                    "addCallbackLocked",
                    Long::class.java, Any::class.java, Any::class.java
                )
                method?.isAccessible = true
                if (i == position && position == 0) {
                    method?.invoke(callback, 0, runnable1, null)
                } else if (i == position && position == 1) {
                    method?.invoke(callback, 0, runnable2, null)
                } else if (i == position && position == 2) {
                    method?.invoke(callback, 0, runnable3, null)
                } else if (i == position && position == 3) {
                    method?.invoke(callback, 0, runnable4, null)
                } else if (i == position && position == 4) {
                    method?.invoke(callback, 0, runnable5, null)
                }
            }
        }
    }

    private var time1 = 0L
    private val runnable1 by lazy {
        Runnable {
            LogUtils.i("zengbobo MainActivity runnable1 run ${System.currentTimeMillis() - time5}")
            var t = System.currentTimeMillis()
            LogUtils.i("zengbobo MainActivity runnable1 start ${System.currentTimeMillis() - time1}")
            hook(0)
            LogUtils.i("zengbobo MainActivity runnable1 end ${System.currentTimeMillis() - time1}")
            time1 = System.currentTimeMillis()
        }
    }

    private var time2 = 0L
    private val runnable2 by lazy {
        Runnable {
            LogUtils.i("zengbobo MainActivity runnable2 run ${System.currentTimeMillis() - time1}")
            var t = System.currentTimeMillis()
            LogUtils.i("zengbobo MainActivity runnable2 start ${System.currentTimeMillis() - time2}")
            hook(1)
            LogUtils.i("zengbobo MainActivity runnable2 end  ${System.currentTimeMillis() - time2}")
            time2 = System.currentTimeMillis()
        }
    }

    private var time3 = 0L
    private val runnable3 by lazy {
        Runnable {
            LogUtils.i("zengbobo MainActivity runnable3 run ${System.currentTimeMillis() - time2}")
            var t = System.currentTimeMillis()
            LogUtils.i("zengbobo MainActivity runnable3 start ${System.currentTimeMillis() - time3}")
            hook(2)
            LogUtils.i("zengbobo MainActivity runnable3 end ${System.currentTimeMillis() - time3}")
            time3 = System.currentTimeMillis()
        }
    }

    private var time4 = 0L
    private val runnable4 by lazy {
        Runnable {
            LogUtils.i("zengbobo MainActivity runnable4 run ${System.currentTimeMillis() - time3}")
            var t = System.currentTimeMillis()
            LogUtils.i("zengbobo MainActivity runnable4 start  ${System.currentTimeMillis() - time4}")
            hook(3)
            LogUtils.i("zengbobo MainActivity runnable4 end ${System.currentTimeMillis() - time4}")
            time4 = System.currentTimeMillis()
        }
    }

    private var time5 = 0L
    private val runnable5 by lazy {
        Runnable {
            LogUtils.i("zengbobo MainActivity runnable5 run ${System.currentTimeMillis() - time4}")
            var t = System.currentTimeMillis()
            LogUtils.i("zengbobo MainActivity runnable5 start ${System.currentTimeMillis() - time5}")
            hook(4)
            LogUtils.i("zengbobo MainActivity runnable5 end ${System.currentTimeMillis() - time5}")
            time5 = System.currentTimeMillis()
        }
    }

相关文章

网友评论

      本文标题:Choreographer字段:mCallbackQueues

      本文链接:https://www.haomeiwen.com/subject/mzzzlrtx.html