1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.server; 18 19 import android.content.Context; 20 import android.net.TrafficStats; 21 import android.os.Build; 22 import android.util.Log; 23 24 import com.android.modules.utils.build.SdkLevel; 25 import com.android.server.net.NetworkStatsService; 26 27 /** 28 * NetworkStats service initializer for core networking. This is called by system server to create 29 * a new instance of NetworkStatsService. 30 */ 31 public final class NetworkStatsServiceInitializer extends SystemService { 32 private static final String TAG = NetworkStatsServiceInitializer.class.getSimpleName(); 33 private final NetworkStatsService mStatsService; 34 NetworkStatsServiceInitializer(Context context)35 public NetworkStatsServiceInitializer(Context context) { 36 super(context); 37 // Load JNI libraries used by NetworkStatsService and its dependencies 38 System.loadLibrary("service-connectivity"); 39 mStatsService = maybeCreateNetworkStatsService(context); 40 } 41 42 @Override onStart()43 public void onStart() { 44 if (mStatsService != null) { 45 Log.i(TAG, "Registering " + Context.NETWORK_STATS_SERVICE); 46 publishBinderService(Context.NETWORK_STATS_SERVICE, mStatsService, 47 /* allowIsolated= */ false); 48 TrafficStats.init(getContext()); 49 } 50 51 // The following code registers the Perfetto Network Trace Handler on non-user builds. 52 // The enhanced tracing is intended to be used for debugging and diagnosing issues. This 53 // is conditional on the build type rather than `isDebuggable` to match the system_server 54 // selinux rules which only allow the Perfetto connection under the same circumstances. 55 if (SdkLevel.isAtLeastU() && !Build.TYPE.equals("user")) { 56 Log.i(TAG, "Initializing network tracing hooks"); 57 NetworkStatsService.nativeInitNetworkTracing(); 58 } 59 } 60 61 @Override onBootPhase(int phase)62 public void onBootPhase(int phase) { 63 // This has to be run before StatsPullAtomService query usage at 64 // PHASE_THIRD_PARTY_APPS_CAN_START. 65 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY && mStatsService != null) { 66 mStatsService.systemReady(); 67 } 68 } 69 70 /** 71 * Return NetworkStatsService instance, or null if current SDK is lower than T. 72 */ maybeCreateNetworkStatsService(final Context context)73 private NetworkStatsService maybeCreateNetworkStatsService(final Context context) { 74 if (!SdkLevel.isAtLeastT()) return null; 75 76 return NetworkStatsService.create(context); 77 } 78 } 79