1 /*
2  * Copyright (C) 2021 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.wifi;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.content.Context;
22 import android.net.KeepalivePacketData;
23 import android.net.LinkProperties;
24 import android.net.NetworkAgent;
25 import android.net.NetworkAgentConfig;
26 import android.net.NetworkCapabilities;
27 import android.net.NetworkProvider;
28 import android.net.Uri;
29 import android.os.Looper;
30 
31 import java.time.Duration;
32 
33 /** Created to facilitate mocking during unit testing. */
34 public class WifiNetworkAgent extends NetworkAgent {
35 
36     private static final String TAG = "WifiNetworkAgent";
37 
38     public interface Callback {
onNetworkUnwanted()39         void onNetworkUnwanted();
onValidationStatus(int status, @Nullable Uri redirectUri)40         void onValidationStatus(int status, @Nullable Uri redirectUri);
onSaveAcceptUnvalidated(boolean accept)41         void onSaveAcceptUnvalidated(boolean accept);
onStartSocketKeepalive(int slot, @NonNull Duration interval, @NonNull KeepalivePacketData packet)42         void onStartSocketKeepalive(int slot, @NonNull Duration interval,
43                 @NonNull KeepalivePacketData packet);
onStopSocketKeepalive(int slot)44         void onStopSocketKeepalive(int slot);
onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet)45         void onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet);
onRemoveKeepalivePacketFilter(int slot)46         void onRemoveKeepalivePacketFilter(int slot);
onSignalStrengthThresholdsUpdated(@onNull int[] thresholds)47         void onSignalStrengthThresholdsUpdated(@NonNull int[] thresholds);
onAutomaticReconnectDisabled()48         void onAutomaticReconnectDisabled();
onDscpPolicyStatusUpdated(int policyId, int status)49         void onDscpPolicyStatusUpdated(int policyId, int status);
50     }
51 
52     private final Callback mCallback;
53 
54     /** Cache the latest NetworkCapabilities update */
55     @NonNull private NetworkCapabilities mCurrentNetworkCapabilities;
56 
57     /**
58      * Create a new network agent.
59      *
60      * @param context  a {@link Context} to get system services from.
61      * @param looper   the {@link Looper} on which to invoke the callbacks.
62      * @param nc       the initial {@link NetworkCapabilities} of this network. Update with
63      *                 sendNetworkCapabilities.
64      * @param lp       the initial {@link LinkProperties} of this network. Update with
65      *                 sendLinkProperties.
66      * @param score    the initial score of this network. Update with sendNetworkScore.
67      * @param config   an immutable {@link NetworkAgentConfig} for this agent.
68      * @param provider the {@link NetworkProvider} managing this agent.
69      * @param wifiNetworkAgentCallback implementation
70      */
WifiNetworkAgent( @onNull Context context, @NonNull Looper looper, @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, @NonNull NetworkAgentConfig config, @Nullable NetworkProvider provider, @NonNull Callback wifiNetworkAgentCallback)71     public WifiNetworkAgent(
72             @NonNull Context context,
73             @NonNull Looper looper,
74             @NonNull NetworkCapabilities nc,
75             @NonNull LinkProperties lp,
76             @NonNull NetworkAgentConfig config,
77             @Nullable NetworkProvider provider,
78             @NonNull Callback wifiNetworkAgentCallback) {
79         super(context, looper, TAG, nc, lp, ConnectedScore.WIFI_INITIAL_SCORE, config, provider);
80         mCurrentNetworkCapabilities = nc;
81         mCallback = wifiNetworkAgentCallback;
82         register();
83     }
84 
85     @Override
onNetworkUnwanted()86     public void onNetworkUnwanted() {
87         mCallback.onNetworkUnwanted();
88     }
89 
90     @Override
onValidationStatus(int status, @Nullable Uri redirectUri)91     public void onValidationStatus(int status, @Nullable Uri redirectUri) {
92         mCallback.onValidationStatus(status, redirectUri);
93     }
94 
95     @Override
onSaveAcceptUnvalidated(boolean accept)96     public void onSaveAcceptUnvalidated(boolean accept) {
97         mCallback.onSaveAcceptUnvalidated(accept);
98     }
99 
100     @Override
onStartSocketKeepalive(int slot, @NonNull Duration interval, @NonNull KeepalivePacketData packet)101     public void onStartSocketKeepalive(int slot, @NonNull Duration interval,
102             @NonNull KeepalivePacketData packet) {
103         mCallback.onStartSocketKeepalive(slot, interval, packet);
104     }
105 
106     @Override
onStopSocketKeepalive(int slot)107     public void onStopSocketKeepalive(int slot) {
108         mCallback.onStopSocketKeepalive(slot);
109     }
110 
111     @Override
onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet)112     public void onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet) {
113         mCallback.onAddKeepalivePacketFilter(slot, packet);
114     }
115 
116     @Override
onRemoveKeepalivePacketFilter(int slot)117     public void onRemoveKeepalivePacketFilter(int slot) {
118         mCallback.onRemoveKeepalivePacketFilter(slot);
119     }
120 
121     @Override
onSignalStrengthThresholdsUpdated(@onNull int[] thresholds)122     public void onSignalStrengthThresholdsUpdated(@NonNull int[] thresholds) {
123         mCallback.onSignalStrengthThresholdsUpdated(thresholds);
124     }
125 
126     @Override
onAutomaticReconnectDisabled()127     public void onAutomaticReconnectDisabled() {
128         mCallback.onAutomaticReconnectDisabled();
129     }
130 
131     @Override
onDscpPolicyStatusUpdated(int policyId, int status)132     public void onDscpPolicyStatusUpdated(int policyId, int status) {
133         mCallback.onDscpPolicyStatusUpdated(policyId, status);
134     }
135 
136     @NonNull
getCallback()137     public Callback getCallback() {
138         return mCallback;
139     }
140 
141     /** sendNetworkCapabilities is final in NetworkAgent, so can't override that method directly */
sendNetworkCapabilitiesAndCache(@onNull NetworkCapabilities nc)142     public void sendNetworkCapabilitiesAndCache(@NonNull NetworkCapabilities nc) {
143         mCurrentNetworkCapabilities = nc;
144         super.sendNetworkCapabilities(nc);
145     }
146 
147     @NonNull
getCurrentNetworkCapabilities()148     public NetworkCapabilities getCurrentNetworkCapabilities() {
149         return mCurrentNetworkCapabilities;
150     }
151 }
152