1 /*
2  * Copyright (C) 2024 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.connectivity;
18 
19 import static com.android.server.ConnectivityStatsLog.NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_RECEIVED;
20 import static com.android.server.ConnectivityStatsLog.NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_REMOVED;
21 import static com.android.server.ConnectivityStatsLog.NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_UNKNOWN;
22 
23 import android.net.NetworkCapabilities;
24 import android.net.NetworkRequest;
25 import android.os.SystemClock;
26 
27 import com.android.net.module.util.BitUtils;
28 
29 
30 class NetworkRequestStateInfo {
31     private final NetworkRequest mNetworkRequest;
32     private final long mNetworkRequestReceivedTime;
33 
34     private enum NetworkRequestState {
35         RECEIVED,
36         REMOVED
37     }
38     private NetworkRequestState mNetworkRequestState;
39     private int mNetworkRequestDurationMillis;
40     private final Dependencies mDependencies;
41 
NetworkRequestStateInfo(NetworkRequest networkRequest, Dependencies deps)42     NetworkRequestStateInfo(NetworkRequest networkRequest,
43             Dependencies deps) {
44         mDependencies = deps;
45         mNetworkRequest = networkRequest;
46         mNetworkRequestReceivedTime = mDependencies.getElapsedRealtime();
47         mNetworkRequestDurationMillis = 0;
48         mNetworkRequestState = NetworkRequestState.RECEIVED;
49     }
50 
NetworkRequestStateInfo(NetworkRequestStateInfo anotherNetworkRequestStateInfo)51     NetworkRequestStateInfo(NetworkRequestStateInfo anotherNetworkRequestStateInfo) {
52         mDependencies = anotherNetworkRequestStateInfo.mDependencies;
53         mNetworkRequest = new NetworkRequest(anotherNetworkRequestStateInfo.mNetworkRequest);
54         mNetworkRequestReceivedTime = anotherNetworkRequestStateInfo.mNetworkRequestReceivedTime;
55         mNetworkRequestDurationMillis =
56                 anotherNetworkRequestStateInfo.mNetworkRequestDurationMillis;
57         mNetworkRequestState = anotherNetworkRequestStateInfo.mNetworkRequestState;
58     }
59 
setNetworkRequestRemoved()60     public void setNetworkRequestRemoved() {
61         mNetworkRequestState = NetworkRequestState.REMOVED;
62         mNetworkRequestDurationMillis = (int) (
63                 mDependencies.getElapsedRealtime() - mNetworkRequestReceivedTime);
64     }
65 
getNetworkRequestStateStatsType()66     public int getNetworkRequestStateStatsType() {
67         if (mNetworkRequestState == NetworkRequestState.RECEIVED) {
68             return NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_RECEIVED;
69         } else if (mNetworkRequestState == NetworkRequestState.REMOVED) {
70             return NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_REMOVED;
71         } else {
72             return NETWORK_REQUEST_STATE_CHANGED__STATE__NETWORK_REQUEST_STATE_UNKNOWN;
73         }
74     }
75 
getRequestId()76     public int getRequestId() {
77         return mNetworkRequest.requestId;
78     }
79 
getPackageUid()80     public int getPackageUid() {
81         return mNetworkRequest.networkCapabilities.getRequestorUid();
82     }
83 
getTransportTypes()84     public int getTransportTypes() {
85         return (int) BitUtils.packBits(mNetworkRequest.networkCapabilities.getTransportTypes());
86     }
87 
getNetCapabilityNotMetered()88     public boolean getNetCapabilityNotMetered() {
89         return mNetworkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED);
90     }
91 
getNetCapabilityInternet()92     public boolean getNetCapabilityInternet() {
93         return mNetworkRequest.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
94     }
95 
getNetworkRequestDurationMillis()96     public int getNetworkRequestDurationMillis() {
97         return mNetworkRequestDurationMillis;
98     }
99 
100     /** Dependency class */
101     public static class Dependencies {
102         // Returns a timestamp with the time base of SystemClock.elapsedRealtime to keep durations
103         // relative to start time and avoid timezone change, including time spent in deep sleep.
getElapsedRealtime()104         public long getElapsedRealtime() {
105             return SystemClock.elapsedRealtime();
106         }
107     }
108 }
109