summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Junyu Lai <junyulai@google.com> 2021-12-28 16:18:43 +0000
committer Junyu Lai <junyulai@google.com> 2022-01-17 13:54:06 +0000
commit6f4cedb5fd4f479930a48c6a1c1737a42e409598 (patch)
tree043d5bfd6166abedbbd7449336f999744dc147d8
parenta5729ee2dcb40c8067cb02e986170393ebcc67b6 (diff)
[MS30] Remove ServiceManager#getService dependency
Since TrafficStats is moving into the mainline module, ServiceManager#getService can no longer be accessed. This change use reflection to access getService, since there is no offical way to get the service binder, and TrafficStats is a static utility that doesn't have the context to invoke Context#getSystemService. This change also fixes minor lint errors. Test: atest CtsNetTestCases:TrafficStatsTest Bug: 204830222 Change-Id: I5caec42a71431b39f747fc791b8511d92e5cf7cc
-rw-r--r--packages/ConnectivityT/framework-t/src/android/net/TrafficStats.java23
-rw-r--r--packages/ConnectivityT/framework-t/src/com/android/server/NetworkManagementSocketTagger.java1
2 files changed, 21 insertions, 3 deletions
diff --git a/packages/ConnectivityT/framework-t/src/android/net/TrafficStats.java b/packages/ConnectivityT/framework-t/src/android/net/TrafficStats.java
index d8feb88f0fe4..032bc3f40235 100644
--- a/packages/ConnectivityT/framework-t/src/android/net/TrafficStats.java
+++ b/packages/ConnectivityT/framework-t/src/android/net/TrafficStats.java
@@ -17,6 +17,7 @@
package android.net;
import android.annotation.NonNull;
+import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.SystemApi;
import android.annotation.TestApi;
@@ -27,8 +28,8 @@ import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;
+import android.os.IBinder;
import android.os.RemoteException;
-import android.os.ServiceManager;
import com.android.server.NetworkManagementSocketTagger;
@@ -36,6 +37,8 @@ import dalvik.system.SocketTagger;
import java.io.FileDescriptor;
import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.DatagramSocket;
import java.net.Socket;
import java.net.SocketException;
@@ -53,6 +56,7 @@ import java.net.SocketException;
* use {@link NetworkStatsManager} instead.
*/
public class TrafficStats {
+ private static final String TAG = TrafficStats.class.getSimpleName();
/**
* The return value to indicate that the device does not support the statistic.
*/
@@ -173,12 +177,25 @@ public class TrafficStats {
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
private synchronized static INetworkStatsService getStatsService() {
if (sStatsService == null) {
- sStatsService = INetworkStatsService.Stub.asInterface(
- ServiceManager.getService(Context.NETWORK_STATS_SERVICE));
+ sStatsService = getStatsBinder();
}
return sStatsService;
}
+ @Nullable
+ private static INetworkStatsService getStatsBinder() {
+ try {
+ final Method getServiceMethod = Class.forName("android.os.ServiceManager")
+ .getDeclaredMethod("getService", new Class[]{String.class});
+ final IBinder binder = (IBinder) getServiceMethod.invoke(
+ null, Context.NETWORK_STATS_SERVICE);
+ return INetworkStatsService.Stub.asInterface(binder);
+ } catch (NoSuchMethodException | ClassNotFoundException | IllegalAccessException
+ | InvocationTargetException e) {
+ throw new NullPointerException("Cannot get INetworkStatsService: " + e);
+ }
+ }
+
/**
* Snapshot of {@link NetworkStats} when the currently active profiling
* session started, or {@code null} if no session active.
diff --git a/packages/ConnectivityT/framework-t/src/com/android/server/NetworkManagementSocketTagger.java b/packages/ConnectivityT/framework-t/src/com/android/server/NetworkManagementSocketTagger.java
index e35f6a648b77..1eb52fb44629 100644
--- a/packages/ConnectivityT/framework-t/src/com/android/server/NetworkManagementSocketTagger.java
+++ b/packages/ConnectivityT/framework-t/src/com/android/server/NetworkManagementSocketTagger.java
@@ -26,6 +26,7 @@ import java.net.SocketException;
/**
* Assigns tags to sockets for traffic stats.
+ * @hide
*/
public final class NetworkManagementSocketTagger extends SocketTagger {
private static final String TAG = "NetworkManagementSocketTagger";