summaryrefslogtreecommitdiff
path: root/core/java/android
diff options
context:
space:
mode:
authorJunyu Lai <junyulai@google.com>2022-01-19 08:33:21 +0000
committerjunyulai <junyulai@google.com>2022-01-22 01:01:49 +0800
commitde9cf3392d7872c2bee69b65a614e77bb166b26e (patch)
treea2f9480fcf000b41aafbec28815e540dd72aeebf /core/java/android
parent139e007dc5ce4e7e82bc7bf4fbdef52f6d6d4d37 (diff)
[MS66] Initialize TrafficStats with context
TrafficStats has static methods created in API level 8 that need access to NetworkStatsManager but doesn't take a context. Previously this was achieved by using ServiceManager, but with TrafficStats moving to the connectivity module, this is no longer possible. Instead, make sure TrafficStats has an appropriate context by the time any client code can call the relevant methods. • In app code, this achieved by passing the application context from ActivityThread#handleBindApplication, before any app code can run. • In the system server, this is achieved by passing the context right after creating service. Test: atest TrafficStatsTest CtsWebkitTestCases Bug: 204830222 Change-Id: I251bb8a4431ad12ff61929879ef1363cf06b9244
Diffstat (limited to 'core/java/android')
-rw-r--r--core/java/android/app/ActivityThread.java8
1 files changed, 8 insertions, 0 deletions
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index 3b0a5f3e9bdd..6d7835f84dc7 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -97,6 +97,7 @@ import android.media.MediaFrameworkPlatformInitializer;
import android.media.MediaServiceManager;
import android.net.ConnectivityManager;
import android.net.Proxy;
+import android.net.TrafficStats;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Binder;
@@ -6663,6 +6664,13 @@ public final class ActivityThread extends ClientTransactionHandler
NetworkSecurityConfigProvider.install(appContext);
Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
+ // For backward compatibility, TrafficStats needs static access to the application context.
+ // But for isolated apps which cannot access network related services, service discovery
+ // is restricted. Hence, calling this would result in NPE.
+ if (!Process.isIsolated()) {
+ TrafficStats.init(appContext);
+ }
+
// Continue loading instrumentation.
if (ii != null) {
initInstrumentation(ii, data, appContext);