From 3c9d1f1fa8e82ef1b37dadd9b4b3925bf2fb112b Mon Sep 17 00:00:00 2001 From: Francois Campbell Date: Sun, 13 Mar 2016 04:06:29 -0400 Subject: [PATCH] Packaged reflection helpers in stub objects --- .../xposeddatausage/Module.kt | 5 +- .../android/net/NetworkTemplate.kt | 7 -- .../presenter/DataUsagePresenterImpl.kt | 73 +++---------------- .../stubs/android/net/INetworkStatsService.kt | 22 ++++++ .../stubs/android/net/NetworkPolicy.kt | 10 +++ .../stubs/android/net/NetworkPolicyManager.kt | 50 +++++++++++++ .../stubs/android/net/NetworkTemplate.kt | 12 +++ 7 files changed, 106 insertions(+), 73 deletions(-) delete mode 100644 app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/android/net/NetworkTemplate.kt create mode 100644 app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/INetworkStatsService.kt create mode 100644 app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicy.kt create mode 100644 app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicyManager.kt create mode 100644 app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkTemplate.kt diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/Module.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/Module.kt index 2ea8eae..7260fe8 100644 --- a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/Module.kt +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/Module.kt @@ -34,7 +34,9 @@ class Module : IXposedHookLoadPackage, IXposedHookInitPackageResources { XposedHelpers.findAndHookMethod("$PACKAGE_SYSTEM_UI.statusbar.policy.Clock", lpparam.classLoader, "updateClock", object : XC_MethodHook() { override fun afterHookedMethod(param: MethodHookParam?) { dataUsageView?.update() - XposedBridge.log("Updating data usage counter to: ${dataUsageView?.text}") + if (BuildConfig.DEBUG) { + XposedBridge.log("Updating data usage counter to: ${dataUsageView?.text}") + } } }) } @@ -58,7 +60,6 @@ class Module : IXposedHookLoadPackage, IXposedHookInitPackageResources { gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL } - //TODO attach dataUsageView to statusbar systemIcons.addView(dataUsageView, 0) } }) diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/android/net/NetworkTemplate.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/android/net/NetworkTemplate.kt deleted file mode 100644 index c5f7dac..0000000 --- a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/android/net/NetworkTemplate.kt +++ /dev/null @@ -1,7 +0,0 @@ -package io.github.francoiscampbell.xposeddatausage.android.net - -/** - * Created by francois on 16-03-13. - */ -interface NetworkTemplate { -} \ No newline at end of file diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/presenter/DataUsagePresenterImpl.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/presenter/DataUsagePresenterImpl.kt index 2c4e7ab..59366b3 100644 --- a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/presenter/DataUsagePresenterImpl.kt +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/presenter/DataUsagePresenterImpl.kt @@ -1,83 +1,28 @@ package io.github.francoiscampbell.xposeddatausage.presenter import android.content.Context -import android.net.TrafficStats import android.telephony.TelephonyManager -import de.robv.android.xposed.XposedHelpers +import io.github.francoiscampbell.xposeddatausage.stubs.android.net.INetworkStatsService +import io.github.francoiscampbell.xposeddatausage.stubs.android.net.NetworkPolicyManager +import io.github.francoiscampbell.xposeddatausage.stubs.android.net.NetworkTemplate /** * Created by francois on 16-03-12. */ class DataUsagePresenterImpl(private val context: Context) : DataUsagePresenter { - companion object { - private val classNetworkTemplate = XposedHelpers.findClass("android.net.NetworkTemplate", null) - private val classNetworkPolicyManager = XposedHelpers.findClass("android.net.NetworkPolicyManager", null) - private val classNetworkPolicy = XposedHelpers.findClass("android.net.NetworkPolicy", null) - - private val networkStatsService = XposedHelpers.callStaticMethod(TrafficStats::class.java, "getStatsService") //actually INetworkStatsService - } - - private val networkPolicyManager = XposedHelpers.callStaticMethod(classNetworkPolicyManager, "from", context) //actually NetworkPolicyManager private val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager override fun getCurrentCycleBytes(): Number { val subscriberId = telephonyManager.subscriberId ?: return -1 - XposedHelpers.callMethod(networkStatsService, "forceUpdate") + INetworkStatsService.forceUpdate() - val template = getNetworkTemplateMobile(subscriberId) ?: return -1 - val policy = getPolicyForTemplate(template) ?: return -1 - - val lastCycleBoundary = getLastCycleBoundary(policy) - val nextCycleBoundary = getNextCycleBoundary(policy) - - return XposedHelpers.callMethod( - networkStatsService, - "getNetworkTotalBytes", - arrayOf(classNetworkTemplate, Long::class.java, Long::class.java), - template, - lastCycleBoundary, - nextCycleBoundary) as Number - } + val template = NetworkTemplate.buildTemplateMobileAll(subscriberId) ?: return -1 + val policy = NetworkPolicyManager.getPolicyForTemplate(template, context) ?: return -1 - fun getNetworkTemplateMobile(subscriberId: String): Any? { - return XposedHelpers.callStaticMethod(classNetworkTemplate, "buildTemplateMobileAll", subscriberId) - } - - // Returns the NetworkPolicy that contains the specified NetworkTemplate - fun getPolicyForTemplate(networkTemplate: Any): Any? { - for (networkPolicy in XposedHelpers.callMethod(networkPolicyManager, "getNetworkPolicies") as Array<*>) { - val networkTemplateForPolicy = XposedHelpers.getObjectField(networkPolicy, "template") - if (networkTemplateForPolicy == networkTemplate) { - return networkPolicy - } - } - return null - } - - fun getLastCycleBoundary(networkPolicyMobile: Any): Number { - val lastCycleBoundary = XposedHelpers.callStaticMethod( - classNetworkPolicyManager, - "computeLastCycleBoundary", - arrayOf(Long::class.java, classNetworkPolicy), - System.currentTimeMillis(), - networkPolicyMobile) - if (lastCycleBoundary == null || lastCycleBoundary !is Number) { - return 0 - } - return lastCycleBoundary - } + val lastCycleBoundary = NetworkPolicyManager.getLastCycleBoundary(policy) + val nextCycleBoundary = NetworkPolicyManager.getNextCycleBoundary(policy) - fun getNextCycleBoundary(networkPolicyMobile: Any): Number { - val nextCycleBoundary = XposedHelpers.callStaticMethod( - classNetworkPolicyManager, - "computeNextCycleBoundary", - arrayOf(Long::class.java, classNetworkPolicy), - System.currentTimeMillis(), - networkPolicyMobile) - if (nextCycleBoundary == null || nextCycleBoundary !is Number) { - return 0 - } - return nextCycleBoundary + return INetworkStatsService.getNetworkTotalBytes(template, lastCycleBoundary, nextCycleBoundary) } } diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/INetworkStatsService.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/INetworkStatsService.kt new file mode 100644 index 0000000..4fdc282 --- /dev/null +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/INetworkStatsService.kt @@ -0,0 +1,22 @@ +package io.github.francoiscampbell.xposeddatausage.stubs.android.net + +import android.net.TrafficStats +import de.robv.android.xposed.XposedHelpers + +/** + * Created by francois on 16-03-13. + */ +object INetworkStatsService { + val stubObject = XposedHelpers.callStaticMethod(TrafficStats::class.java, "getStatsService") + + fun forceUpdate() = XposedHelpers.callMethod(stubObject, "forceUpdate") + + fun getNetworkTotalBytes(template: Any, lastCycleBoundary: Number, nextCycleBoundary: Number): Number = XposedHelpers.callMethod( + INetworkStatsService.stubObject, + "getNetworkTotalBytes", + arrayOf(NetworkTemplate.stubClass, Long::class.java, Long::class.java), + template, + lastCycleBoundary, + nextCycleBoundary) as Number + +} \ No newline at end of file diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicy.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicy.kt new file mode 100644 index 0000000..67fce6a --- /dev/null +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicy.kt @@ -0,0 +1,10 @@ +package io.github.francoiscampbell.xposeddatausage.stubs.android.net + +import de.robv.android.xposed.XposedHelpers + +/** + * Created by francois on 16-03-13. + */ +object NetworkPolicy { + val stubClass = XposedHelpers.findClass("android.net.NetworkPolicy", null) +} \ No newline at end of file diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicyManager.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicyManager.kt new file mode 100644 index 0000000..b04eb40 --- /dev/null +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicyManager.kt @@ -0,0 +1,50 @@ +package io.github.francoiscampbell.xposeddatausage.stubs.android.net + +import android.content.Context +import de.robv.android.xposed.XposedHelpers + +/** + * Created by francois on 16-03-13. + */ +object NetworkPolicyManager { + val stubClass = XposedHelpers.findClass("android.net.NetworkPolicyManager", null) + + fun getPolicyForTemplate(networkTemplate: Any, context: Context): Any? { + for (networkPolicy in XposedHelpers.callMethod(from(context), "getNetworkPolicies") as Array<*>) { + val networkTemplateForPolicy = XposedHelpers.getObjectField(networkPolicy, "template") + if (networkTemplateForPolicy == networkTemplate) { + return networkPolicy + } + } + return null + + } + + fun from(context: Context) = XposedHelpers.callStaticMethod(stubClass, "from", context) + + fun getLastCycleBoundary(networkPolicyMobile: Any): Number { + val lastCycleBoundary = XposedHelpers.callStaticMethod( + NetworkPolicyManager.stubClass, + "computeLastCycleBoundary", + arrayOf(Long::class.java, NetworkPolicy.stubClass), + System.currentTimeMillis(), + networkPolicyMobile) + if (lastCycleBoundary == null || lastCycleBoundary !is Number) { + return 0 + } + return lastCycleBoundary + } + + fun getNextCycleBoundary(networkPolicyMobile: Any): Number { + val nextCycleBoundary = XposedHelpers.callStaticMethod( + NetworkPolicyManager.stubClass, + "computeNextCycleBoundary", + arrayOf(Long::class.java, NetworkPolicy.stubClass), + System.currentTimeMillis(), + networkPolicyMobile) + if (nextCycleBoundary == null || nextCycleBoundary !is Number) { + return 0 + } + return nextCycleBoundary + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkTemplate.kt b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkTemplate.kt new file mode 100644 index 0000000..33c1e29 --- /dev/null +++ b/app/src/main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkTemplate.kt @@ -0,0 +1,12 @@ +package io.github.francoiscampbell.xposeddatausage.stubs.android.net + +import de.robv.android.xposed.XposedHelpers + +/** + * Created by francois on 16-03-13. + */ +object NetworkTemplate { + val stubClass = XposedHelpers.findClass("android.net.NetworkTemplate", null) + + fun buildTemplateMobileAll(subscriberId: String) = XposedHelpers.callStaticMethod(stubClass, "buildTemplateMobileAll", subscriberId) +} \ No newline at end of file