Home | History | Annotate | Download | only in wifi
      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 final class CaptivePortalNetworkCallback extends NetworkCallback {
     26 
     27     private final ConnectedAccessPointPreference mConnectedApPreference;
     28     private final Network mNetwork;
     29 
     30     private boolean mIsCaptivePortal;
     31 
     32     CaptivePortalNetworkCallback(
     33             Network network, ConnectedAccessPointPreference connectedApPreference) {
     34         mNetwork = Preconditions.checkNotNull(network);
     35         mConnectedApPreference = Preconditions.checkNotNull(connectedApPreference);
     36     }
     37 
     38     @Override
     39     public void onLost(Network network) {
     40         if (mNetwork.equals(network)) {
     41             mIsCaptivePortal = false;
     42         }
     43     }
     44 
     45     @Override
     46     public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
     47         if (mNetwork.equals(network)) {
     48             mIsCaptivePortal = WifiUtils.canSignIntoNetwork(networkCapabilities);
     49             mConnectedApPreference.setCaptivePortal(mIsCaptivePortal);
     50         }
     51     }
     52 
     53     /**
     54      * Returns true if the supplied network and preference are not null and are the same as the
     55      * originally supplied values.
     56      */
     57     public boolean isSameNetworkAndPreference(
     58             Network network, ConnectedAccessPointPreference connectedApPreference) {
     59         return mNetwork.equals(network) && mConnectedApPreference == connectedApPreference;
     60     }
     61 
     62     /**
     63      * Returns true if the most recent update to the NetworkCapabilities indicates a captive portal
     64      * network and the Network was not lost in the interim.
     65      */
     66     public boolean isCaptivePortal() {
     67         return mIsCaptivePortal;
     68     }
     69 
     70     /** Returns the currently associated network. */
     71     public Network getNetwork() {
     72         return mNetwork;
     73     }
     74 }
     75