• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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();
49     }
50 
51     private final Callback mCallback;
52 
53     /** Cache the latest NetworkCapabilities update */
54     @NonNull private NetworkCapabilities mCurrentNetworkCapabilities;
55 
56     /**
57      * Create a new network agent.
58      *
59      * @param context  a {@link Context} to get system services from.
60      * @param looper   the {@link Looper} on which to invoke the callbacks.
61      * @param nc       the initial {@link NetworkCapabilities} of this network. Update with
62      *                 sendNetworkCapabilities.
63      * @param lp       the initial {@link LinkProperties} of this network. Update with
64      *                 sendLinkProperties.
65      * @param score    the initial score of this network. Update with sendNetworkScore.
66      * @param config   an immutable {@link NetworkAgentConfig} for this agent.
67      * @param provider the {@link NetworkProvider} managing this agent.
68      * @param wifiNetworkAgentCallback implementation
69      */
WifiNetworkAgent( @onNull Context context, @NonNull Looper looper, @NonNull NetworkCapabilities nc, @NonNull LinkProperties lp, @NonNull NetworkAgentConfig config, @Nullable NetworkProvider provider, @NonNull Callback wifiNetworkAgentCallback)70     public WifiNetworkAgent(
71             @NonNull Context context,
72             @NonNull Looper looper,
73             @NonNull NetworkCapabilities nc,
74             @NonNull LinkProperties lp,
75             @NonNull NetworkAgentConfig config,
76             @Nullable NetworkProvider provider,
77             @NonNull Callback wifiNetworkAgentCallback) {
78         super(context, looper, TAG, nc, lp, ConnectedScore.WIFI_INITIAL_SCORE, config, provider);
79         mCurrentNetworkCapabilities = nc;
80         mCallback = wifiNetworkAgentCallback;
81         register();
82     }
83 
84     @Override
onNetworkUnwanted()85     public void onNetworkUnwanted() {
86         mCallback.onNetworkUnwanted();
87     }
88 
89     @Override
onValidationStatus(int status, @Nullable Uri redirectUri)90     public void onValidationStatus(int status, @Nullable Uri redirectUri) {
91         mCallback.onValidationStatus(status, redirectUri);
92     }
93 
94     @Override
onSaveAcceptUnvalidated(boolean accept)95     public void onSaveAcceptUnvalidated(boolean accept) {
96         mCallback.onSaveAcceptUnvalidated(accept);
97     }
98 
99     @Override
onStartSocketKeepalive(int slot, @NonNull Duration interval, @NonNull KeepalivePacketData packet)100     public void onStartSocketKeepalive(int slot, @NonNull Duration interval,
101             @NonNull KeepalivePacketData packet) {
102         mCallback.onStartSocketKeepalive(slot, interval, packet);
103     }
104 
105     @Override
onStopSocketKeepalive(int slot)106     public void onStopSocketKeepalive(int slot) {
107         mCallback.onStopSocketKeepalive(slot);
108     }
109 
110     @Override
onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet)111     public void onAddKeepalivePacketFilter(int slot, @NonNull KeepalivePacketData packet) {
112         mCallback.onAddKeepalivePacketFilter(slot, packet);
113     }
114 
115     @Override
onRemoveKeepalivePacketFilter(int slot)116     public void onRemoveKeepalivePacketFilter(int slot) {
117         mCallback.onRemoveKeepalivePacketFilter(slot);
118     }
119 
120     @Override
onSignalStrengthThresholdsUpdated(@onNull int[] thresholds)121     public void onSignalStrengthThresholdsUpdated(@NonNull int[] thresholds) {
122         mCallback.onSignalStrengthThresholdsUpdated(thresholds);
123     }
124 
125     @Override
onAutomaticReconnectDisabled()126     public void onAutomaticReconnectDisabled() {
127         mCallback.onAutomaticReconnectDisabled();
128     }
129 
130     @NonNull
getCallback()131     public Callback getCallback() {
132         return mCallback;
133     }
134 
135     /** sendNetworkCapabilities is final in NetworkAgent, so can't override that method directly */
sendNetworkCapabilitiesAndCache(@onNull NetworkCapabilities nc)136     public void sendNetworkCapabilitiesAndCache(@NonNull NetworkCapabilities nc) {
137         mCurrentNetworkCapabilities = nc;
138         super.sendNetworkCapabilities(nc);
139     }
140 
141     @NonNull
getCurrentNetworkCapabilities()142     public NetworkCapabilities getCurrentNetworkCapabilities() {
143         return mCurrentNetworkCapabilities;
144     }
145 }
146