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.net.module.util.DeviceConfigUtils; 26 import com.android.server.net.NetworkStatsService; 27 28 /** 29 * NetworkStats service initializer for core networking. This is called by system server to create 30 * a new instance of NetworkStatsService. 31 */ 32 public final class NetworkStatsServiceInitializer extends SystemService { 33 private static final String TAG = NetworkStatsServiceInitializer.class.getSimpleName(); 34 private static final String ENABLE_NETWORK_TRACING = "enable_network_tracing"; 35 private final boolean mNetworkTracingFlagEnabled; 36 private final NetworkStatsService mStatsService; 37 NetworkStatsServiceInitializer(Context context)38 public NetworkStatsServiceInitializer(Context context) { 39 super(context); 40 // Load JNI libraries used by NetworkStatsService and its dependencies 41 System.loadLibrary("service-connectivity"); 42 mStatsService = maybeCreateNetworkStatsService(context); 43 mNetworkTracingFlagEnabled = DeviceConfigUtils.isTetheringFeatureEnabled( 44 context, ENABLE_NETWORK_TRACING); 45 } 46 47 @Override onStart()48 public void onStart() { 49 if (mStatsService != null) { 50 Log.i(TAG, "Registering " + Context.NETWORK_STATS_SERVICE); 51 publishBinderService(Context.NETWORK_STATS_SERVICE, mStatsService, 52 /* allowIsolated= */ false); 53 TrafficStats.init(getContext()); 54 } 55 56 // The following code registers the Perfetto Network Trace Handler. The enhanced tracing 57 // is intended to be used for debugging and diagnosing issues. This is enabled by default 58 // on userdebug/eng builds and flag protected in user builds. 59 if (SdkLevel.isAtLeastU() && (mNetworkTracingFlagEnabled || !Build.TYPE.equals("user"))) { 60 Log.i(TAG, "Initializing network tracing hooks"); 61 NetworkStatsService.nativeInitNetworkTracing(); 62 } 63 } 64 65 @Override onBootPhase(int phase)66 public void onBootPhase(int phase) { 67 // This has to be run before StatsPullAtomService query usage at 68 // PHASE_THIRD_PARTY_APPS_CAN_START. 69 if (phase == SystemService.PHASE_ACTIVITY_MANAGER_READY && mStatsService != null) { 70 mStatsService.systemReady(); 71 } 72 } 73 74 /** 75 * Return NetworkStatsService instance, or null if current SDK is lower than T. 76 */ maybeCreateNetworkStatsService(final Context context)77 private NetworkStatsService maybeCreateNetworkStatsService(final Context context) { 78 if (!SdkLevel.isAtLeastT()) return null; 79 80 return NetworkStatsService.create(context); 81 } 82 } 83