Observe NetworkCallback instead of polling

Switch from polling for the first connected WiFi network to observing
the currently connected WiFi network and associating that with the
preference at the time the preference is created or updated.

Whenever the ConnectedAccessPointPreference is removed or UI is stopped
unregister the callback.

Bug:68031656
Test: make RunSettingsRoboTests
Change-Id: I5d1ed83b6a13e8a83fae04bfdce8d0f13c2ba0ac
This commit is contained in:
Adam Newman
2018-03-19 21:09:52 -07:00
parent 2c1de66c58
commit 5178a9d07a
4 changed files with 149 additions and 61 deletions

View File

@@ -0,0 +1,74 @@
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.Network;
import android.net.NetworkCapabilities;
import com.android.internal.util.Preconditions;
/** Listens for changes to NetworkCapabilities to update the ConnectedAccessPointPreference. */
final class CaptivePortalNetworkCallback extends NetworkCallback {
private final ConnectedAccessPointPreference mConnectedApPreference;
private final Network mNetwork;
private boolean mIsCaptivePortal;
CaptivePortalNetworkCallback(
Network network, ConnectedAccessPointPreference connectedApPreference) {
mNetwork = Preconditions.checkNotNull(network);
mConnectedApPreference = Preconditions.checkNotNull(connectedApPreference);
}
@Override
public void onLost(Network network) {
if (mNetwork.equals(network)) {
mIsCaptivePortal = false;
}
}
@Override
public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) {
if (mNetwork.equals(network)) {
mIsCaptivePortal = WifiUtils.canSignIntoNetwork(networkCapabilities);
mConnectedApPreference.setCaptivePortal(mIsCaptivePortal);
}
}
/**
* Returns true if the supplied network and preference are not null and are the same as the
* originally supplied values.
*/
public boolean isSameNetworkAndPreference(
Network network, ConnectedAccessPointPreference connectedApPreference) {
return mNetwork.equals(network) && mConnectedApPreference == connectedApPreference;
}
/**
* Returns true if the most recent update to the NetworkCapabilities indicates a captive portal
* network and the Network was not lost in the interim.
*/
public boolean isCaptivePortal() {
return mIsCaptivePortal;
}
/** Returns the currently associated network. */
public Network getNetwork() {
return mNetwork;
}
}