Skip to content

Commit

Permalink
Packaged reflection helpers in stub objects
Browse files Browse the repository at this point in the history
  • Loading branch information
francoiscampbell committed Mar 13, 2016
1 parent 2dd8efe commit 3c9d1f1
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
}
}
})
}
Expand All @@ -58,7 +60,6 @@ class Module : IXposedHookLoadPackage, IXposedHookInitPackageResources {
gravity = Gravity.RIGHT or Gravity.CENTER_VERTICAL
}

//TODO attach dataUsageView to statusbar
systemIcons.addView(dataUsageView, 0)
}
})
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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)
}
}

Original file line number Diff line number Diff line change
@@ -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

}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 3c9d1f1

Please sign in to comment.