1 /*
2  * Copyright (C) 2018 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 package com.android.settings.wifi;
17 
18 import android.net.ConnectivityManager.NetworkCallback;
19 import android.net.Network;
20 import android.net.NetworkCapabilities;
21 
22 import com.android.internal.util.Preconditions;
23 
24 /** Listens for changes to NetworkCapabilities to update the ConnectedAccessPointPreference. */
25 class CaptivePortalNetworkCallback extends NetworkCallback {
26 
27     private final ConnectedAccessPointPreference mConnectedApPreference;
28     private final Network mNetwork;
29 
30     private boolean mIsCaptivePortal;
31 
CaptivePortalNetworkCallback( Network network, ConnectedAccessPointPreference connectedApPreference)32     CaptivePortalNetworkCallback(
33             Network network, ConnectedAccessPointPreference connectedApPreference) {
34         mNetwork = Preconditions.checkNotNull(network);
35         mConnectedApPreference = Preconditions.checkNotNull(connectedApPreference);
36     }
37 
38     @Override
onLost(Network network)39     public final void onLost(Network network) {
40         if (mNetwork.equals(network)) {
41             setIsCaptivePortal(false);
42         }
43     }
44 
45     @Override
onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities)46     public final void onCapabilitiesChanged(Network network,
47             NetworkCapabilities networkCapabilities) {
48         if (mNetwork.equals(network)) {
49             boolean isCaptivePortal = WifiUtils.canSignIntoNetwork(networkCapabilities);
50             setIsCaptivePortal(isCaptivePortal);
51             mConnectedApPreference.setCaptivePortal(isCaptivePortal);
52         }
53     }
54 
55     /**
56      * Called when captive portal capability changes for the current network. Default implementation
57      * is a no-op. Use {@link CaptivePortalNetworkCallback#isCaptivePortal()} to read new
58      * capability.
59      */
onCaptivePortalCapabilityChanged()60     public void onCaptivePortalCapabilityChanged() {}
61 
setIsCaptivePortal(boolean isCaptivePortal)62     private void setIsCaptivePortal(boolean isCaptivePortal) {
63         if (isCaptivePortal == mIsCaptivePortal) {
64             return;
65         }
66         mIsCaptivePortal = isCaptivePortal;
67         onCaptivePortalCapabilityChanged();
68     }
69 
70     /**
71      * Returns true if the supplied network and preference are not null and are the same as the
72      * originally supplied values.
73      */
isSameNetworkAndPreference( Network network, ConnectedAccessPointPreference connectedApPreference)74     public final boolean isSameNetworkAndPreference(
75             Network network, ConnectedAccessPointPreference connectedApPreference) {
76         return mNetwork.equals(network) && mConnectedApPreference == connectedApPreference;
77     }
78 
79     /**
80      * Returns true if the most recent update to the NetworkCapabilities indicates a captive portal
81      * network and the Network was not lost in the interim.
82      */
isCaptivePortal()83     public final boolean isCaptivePortal() {
84         return mIsCaptivePortal;
85     }
86 
87     /** Returns the currently associated network. */
getNetwork()88     public final Network getNetwork() {
89         return mNetwork;
90     }
91 }
92