lineage-sdk: NetworkTraffic: Cleanup network callback

* Don't call ConnectionManager.get* methods from within the callback,
  docs say it's racy.

* Remove the NetworkState struct whilst we're here.  It was
  useful for debug during development but is no longer needed.

Change-Id: Ia1abbdfa31b258c38ba809844340d0dc06e5bd07
This commit is contained in:
Sam Mortimer
2020-05-03 11:04:44 -07:00
committed by Bruno Martins
parent 1c3c0432d0
commit 082c3c6b86

View File

@@ -1,5 +1,5 @@
/**
* Copyright (C) 2017-2019 The LineageOS project
* Copyright (C) 2017-2020 The LineageOS project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -99,23 +99,12 @@ public class NetworkTraffic extends TextView {
private Drawable mDrawable;
// Network tracking related variables
final private ConnectivityManager mConnectivityManager;
final private HashMap<Network, NetworkState> mNetworkMap = new HashMap<>();
private final ConnectivityManager mConnectivityManager;
private final HashMap<Network, LinkProperties> mLinkPropertiesMap = new HashMap<>();
// Used to indicate that the set of sources contributing
// to current stats have changed.
private boolean mNetworksChanged = true;
public class NetworkState {
public NetworkCapabilities mNetworkCapabilities;
public LinkProperties mLinkProperties;
public NetworkState(NetworkCapabilities networkCapabilities,
LinkProperties linkProperties) {
mNetworkCapabilities = networkCapabilities;
mLinkProperties = linkProperties;
}
};
private INetworkManagementService mNetworkManagementService;
public NetworkTraffic(Context context) {
@@ -204,8 +193,8 @@ public class NetworkTraffic extends TextView {
long txBytes = 0;
long rxBytes = 0;
// Add interface stats
for (NetworkState state : mNetworkMap.values()) {
final String iface = state.mLinkProperties.getInterfaceName();
for (LinkProperties linkProperties : mLinkPropertiesMap.values()) {
final String iface = linkProperties.getInterfaceName();
if (iface == null) {
continue;
}
@@ -487,37 +476,15 @@ public class NetworkTraffic extends TextView {
private ConnectivityManager.NetworkCallback mNetworkCallback =
new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
mNetworkMap.put(network,
new NetworkState(mConnectivityManager.getNetworkCapabilities(network),
mConnectivityManager.getLinkProperties(network)));
mNetworksChanged = true;
}
@Override
public void onCapabilitiesChanged(Network network,
NetworkCapabilities networkCapabilities) {
if (mNetworkMap.containsKey(network)) {
mNetworkMap.put(network, new NetworkState(networkCapabilities,
mConnectivityManager.getLinkProperties(network)));
mNetworksChanged = true;
}
}
@Override
public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {
if (mNetworkMap.containsKey(network)) {
mNetworkMap.put(network,
new NetworkState(mConnectivityManager.getNetworkCapabilities(network),
linkProperties));
mNetworksChanged = true;
}
mLinkPropertiesMap.put(network, linkProperties);
mNetworksChanged = true;
}
@Override
public void onLost(Network network) {
mNetworkMap.remove(network);
mLinkPropertiesMap.remove(network);
mNetworksChanged = true;
}
};