1 /* 2 * Copyright (C) 2020 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.internal.telephony.metrics; 18 19 import android.net.NetworkCapabilities; 20 import android.net.NetworkRequest; 21 import android.telephony.SubscriptionManager; 22 import android.telephony.TelephonyManager; 23 24 import com.android.internal.telephony.Phone; 25 import com.android.internal.telephony.PhoneFactory; 26 import com.android.internal.telephony.nano.PersistAtomsProto.NetworkRequests; 27 28 29 /** Metrics for the network requests. */ 30 public class NetworkRequestsStats { NetworkRequestsStats()31 private NetworkRequestsStats() { } 32 33 /** Generate metrics when network request occurs. */ addNetworkRequest(NetworkRequest networkRequest, int subId)34 public static void addNetworkRequest(NetworkRequest networkRequest, int subId) { 35 if (!networkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_ENTERPRISE)) { 36 // Currently only handle enterprise 37 return; 38 } 39 NetworkRequests networkRequests = new NetworkRequests(); 40 networkRequests.carrierId = getCarrierId(subId); 41 networkRequests.enterpriseRequestCount = 1; 42 43 PersistAtomsStorage storage = PhoneFactory.getMetricsCollector().getAtomsStorage(); 44 storage.addNetworkRequests(networkRequests); 45 } 46 47 /** Generate metrics when network release occurs. */ addNetworkRelease(NetworkRequest networkRequest, int subId)48 public static void addNetworkRelease(NetworkRequest networkRequest, int subId) { 49 if (!networkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_ENTERPRISE)) { 50 // Currently only handle enterprise 51 return; 52 } 53 NetworkRequests networkRequests = new NetworkRequests(); 54 networkRequests.carrierId = getCarrierId(subId); 55 networkRequests.enterpriseReleaseCount = 1; 56 57 PersistAtomsStorage storage = PhoneFactory.getMetricsCollector().getAtomsStorage(); 58 storage.addNetworkRequests(networkRequests); 59 } 60 61 /** Returns the carrier ID of the given subscription id. */ getCarrierId(int subId)62 private static int getCarrierId(int subId) { 63 int phoneId = SubscriptionManager.getPhoneId(subId); 64 Phone phone = PhoneFactory.getPhone(phoneId); 65 return phone != null ? phone.getCarrierId() : TelephonyManager.UNKNOWN_CARRIER_ID; 66 } 67 } 68