-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Packaged reflection helpers in stub objects
- Loading branch information
1 parent
2dd8efe
commit 3c9d1f1
Showing
7 changed files
with
106 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
...src/main/kotlin/io/github/francoiscampbell/xposeddatausage/android/net/NetworkTemplate.kt
This file was deleted.
Oops, something went wrong.
73 changes: 9 additions & 64 deletions
73
...ain/kotlin/io/github/francoiscampbell/xposeddatausage/presenter/DataUsagePresenterImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
|
22 changes: 22 additions & 0 deletions
22
...tlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/INetworkStatsService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
...main/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicy.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
50 changes: 50 additions & 0 deletions
50
...tlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkPolicyManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...in/kotlin/io/github/francoiscampbell/xposeddatausage/stubs/android/net/NetworkTemplate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |