Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9f5cf9d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Simplify UpstreamNetworkMonitor callback handling"

parents 34ed84d4 452b4b72
Loading
Loading
Loading
Loading
+96 −93
Original line number Original line Diff line number Diff line
@@ -1077,15 +1077,11 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
        }
        }


        public void stop() {
        public void stop() {
            if (mDefaultNetworkCallback != null) {
            releaseCallback(mDefaultNetworkCallback);
                cm().unregisterNetworkCallback(mDefaultNetworkCallback);
            mDefaultNetworkCallback = null;
            mDefaultNetworkCallback = null;
            }


            if (mDunTetheringCallback != null) {
            releaseCallback(mDunTetheringCallback);
                cm().unregisterNetworkCallback(mDunTetheringCallback);
            mDunTetheringCallback = null;
            mDunTetheringCallback = null;
            }


            mNetworkMap.clear();
            mNetworkMap.clear();
        }
        }
@@ -1094,10 +1090,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
            return (network != null) ? mNetworkMap.get(network) : null;
            return (network != null) ? mNetworkMap.get(network) : null;
        }
        }


        public NetworkState processCallback(int arg1, Object obj) {
        private void handleAvailable(Network network) {
            switch (arg1) {
                case EVENT_ON_AVAILABLE: {
                    final Network network = (Network) obj;
            if (VDBG) {
            if (VDBG) {
                Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
                Log.d(TAG, "EVENT_ON_AVAILABLE for " + network);
            }
            }
@@ -1126,56 +1119,58 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
            // the system that requests it) we won't know its
            // the system that requests it) we won't know its
            // LinkProperties or NetworkCapabilities.
            // LinkProperties or NetworkCapabilities.


                    return mNetworkMap.get(network);
            notifyTarget(EVENT_ON_AVAILABLE, network);
        }
        }
                case EVENT_ON_CAPABILITIES: {

                    final NetworkState ns = (NetworkState) obj;
        private void handleNetCap(Network network, NetworkCapabilities newNc) {
                    if (!mNetworkMap.containsKey(ns.network)) {
            if (!mNetworkMap.containsKey(network)) {
                // Ignore updates for networks for which we have not yet
                // Ignore updates for networks for which we have not yet
                // received onAvailable() - which should never happen -
                // received onAvailable() - which should never happen -
                // or for which we have already received onLost().
                // or for which we have already received onLost().
                        return null;
                return;
            }
            }
            if (VDBG) {
            if (VDBG) {
                Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
                Log.d(TAG, String.format("EVENT_ON_CAPABILITIES for %s: %s",
                                ns.network, ns.networkCapabilities));
                        network, newNc));
            }
            }


                    final NetworkState prev = mNetworkMap.get(ns.network);
            final NetworkState prev = mNetworkMap.get(network);
                    mNetworkMap.put(ns.network,
            mNetworkMap.put(network,
                            new NetworkState(null, prev.linkProperties, ns.networkCapabilities,
                    new NetworkState(null, prev.linkProperties, newNc,
                                             ns.network, null, null));
                                     network, null, null));
                    return mNetworkMap.get(ns.network);
            notifyTarget(EVENT_ON_CAPABILITIES, network);
        }
        }
                case EVENT_ON_LINKPROPERTIES: {

                    final NetworkState ns = (NetworkState) obj;
        private void handleLinkProp(Network network, LinkProperties newLp) {
                    if (!mNetworkMap.containsKey(ns.network)) {
            if (!mNetworkMap.containsKey(network)) {
                // Ignore updates for networks for which we have not yet
                // Ignore updates for networks for which we have not yet
                // received onAvailable() - which should never happen -
                // received onAvailable() - which should never happen -
                // or for which we have already received onLost().
                // or for which we have already received onLost().
                        return null;
                return;
            }
            }
            if (VDBG) {
            if (VDBG) {
                Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
                Log.d(TAG, String.format("EVENT_ON_LINKPROPERTIES for %s: %s",
                                ns.network, ns.linkProperties));
                        network, newLp));
            }
            }


                    final NetworkState prev = mNetworkMap.get(ns.network);
            final NetworkState prev = mNetworkMap.get(network);
                    mNetworkMap.put(ns.network,
            mNetworkMap.put(network,
                            new NetworkState(null, ns.linkProperties, prev.networkCapabilities,
                    new NetworkState(null, newLp, prev.networkCapabilities,
                                             ns.network, null, null));
                                     network, null, null));
                    return mNetworkMap.get(ns.network);
            notifyTarget(EVENT_ON_LINKPROPERTIES, network);
        }

        private void handleLost(Network network) {
            if (!mNetworkMap.containsKey(network)) {
                // Ignore updates for networks for which we have not yet
                // received onAvailable() - which should never happen -
                // or for which we have already received onLost().
                return;
            }
            }
                case EVENT_ON_LOST: {
                    final Network network = (Network) obj;
            if (VDBG) {
            if (VDBG) {
                Log.d(TAG, "EVENT_ON_LOST for " + network);
                Log.d(TAG, "EVENT_ON_LOST for " + network);
            }
            }
                    return mNetworkMap.remove(network);
            notifyTarget(EVENT_ON_LOST, mNetworkMap.remove(network));
                }
                default:
                    return null;
            }
        }
        }


        // Fetch (and cache) a ConnectivityManager only if and when we need one.
        // Fetch (and cache) a ConnectivityManager only if and when we need one.
@@ -1193,26 +1188,36 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
        private class UpstreamNetworkCallback extends NetworkCallback {
        private class UpstreamNetworkCallback extends NetworkCallback {
            @Override
            @Override
            public void onAvailable(Network network) {
            public void onAvailable(Network network) {
                mTarget.sendMessage(mWhat, EVENT_ON_AVAILABLE, 0, network);
                mTarget.getHandler().post(() -> handleAvailable(network));
            }
            }


            @Override
            @Override
            public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) {
            public void onCapabilitiesChanged(Network network, NetworkCapabilities newNc) {
                mTarget.sendMessage(mWhat, EVENT_ON_CAPABILITIES, 0,
                mTarget.getHandler().post(() -> handleNetCap(network, newNc));
                        new NetworkState(null, null, newNc, network, null, null));
            }
            }


            @Override
            @Override
            public void onLinkPropertiesChanged(Network network, LinkProperties newLp) {
            public void onLinkPropertiesChanged(Network network, LinkProperties newLp) {
                mTarget.sendMessage(mWhat, EVENT_ON_LINKPROPERTIES, 0,
                mTarget.getHandler().post(() -> handleLinkProp(network, newLp));
                        new NetworkState(null, newLp, null, network, null, null));
            }
            }


            @Override
            @Override
            public void onLost(Network network) {
            public void onLost(Network network) {
                mTarget.sendMessage(mWhat, EVENT_ON_LOST, 0, network);
                mTarget.getHandler().post(() -> handleLost(network));
            }
            }
        }
        }

        private void releaseCallback(NetworkCallback cb) {
            if (cb != null) cm().unregisterNetworkCallback(cb);
        }

        private void notifyTarget(int which, Network network) {
            notifyTarget(which, mNetworkMap.get(network));
        }

        private void notifyTarget(int which, NetworkState netstate) {
            mTarget.sendMessage(mWhat, which, 0, netstate);
        }
    }
    }


    // Needed because the canonical source of upstream truth is just the
    // Needed because the canonical source of upstream truth is just the
@@ -1729,9 +1734,7 @@ public class Tethering extends BaseNetworkObserver implements IControlsTethering
                        mTryCell = !mTryCell;
                        mTryCell = !mTryCell;
                        break;
                        break;
                    case EVENT_UPSTREAM_CALLBACK: {
                    case EVENT_UPSTREAM_CALLBACK: {
                        // First: always update local state about every network.
                        final NetworkState ns = (NetworkState) message.obj;
                        final NetworkState ns = mUpstreamNetworkMonitor.processCallback(
                                message.arg1, message.obj);


                        if (ns == null || !pertainsToCurrentUpstream(ns)) {
                        if (ns == null || !pertainsToCurrentUpstream(ns)) {
                            // TODO: In future, this is where upstream evaluation and selection
                            // TODO: In future, this is where upstream evaluation and selection