13c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt/*
23c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * Copyright (C) 2014 The Android Open Source Project
33c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt *
43c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
53c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * you may not use this file except in compliance with the License.
63c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * You may obtain a copy of the License at
73c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt *
83c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
93c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt *
103c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
113c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
123c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
133c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * See the License for the specific language governing permissions and
143c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * limitations under the License.
153c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt */
163c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
173c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltpackage android.net;
183c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
193c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltimport android.os.Parcel;
203c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltimport android.os.Parcelable;
213c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
221ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colittiimport java.util.Objects;
231ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti
243c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt/**
257569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt * Defines a request for a network, made through {@link NetworkRequest.Builder} and used
267569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt * to request a network via {@link ConnectivityManager#requestNetwork} or listen for changes
276078b50b017fbcf8d6cbf9f83226ed5667d5729eRobert Greenwalt * via {@link ConnectivityManager#registerNetworkCallback}.
283c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt */
293c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltpublic class NetworkRequest implements Parcelable {
307b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    /**
317569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt     * The {@link NetworkCapabilities} that define this request.
327569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt     * @hide
337b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     */
343c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public final NetworkCapabilities networkCapabilities;
357b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt
367b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    /**
377b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * Identifies the request.  NetworkRequests should only be constructed by
387b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * the Framework and given out to applications as tokens to be used to identify
397b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * the request.
4034524f06fb77607884c69f891471725f4ac0889aRobert Greenwalt     * @hide
417b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     */
423c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public final int requestId;
433c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
447b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    /**
4532aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt     * Set for legacy requests and the default.  Set to TYPE_NONE for none.
467b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * Causes CONNECTIVITY_ACTION broadcasts to be sent.
477b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * @hide
487b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     */
4932aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt    public final int legacyType;
507b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt
517b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    /**
523d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * A NetworkRequest as used by the system can be one of the following types:
53b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
54b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *     - LISTEN, for which the framework will issue callbacks about any
55b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       and all networks that match the specified NetworkCapabilities,
56b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
57b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *     - REQUEST, capable of causing a specific network to be created
58b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       first (e.g. a telephony DUN request), the framework will issue
59b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       callbacks about the single, highest scoring current network
60b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       (if any) that matches the specified NetworkCapabilities, or
61b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
62b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *     - TRACK_DEFAULT, a hybrid of the two designed such that the
63b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       framework will issue callbacks for the single, highest scoring
64b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       current network (if any) that matches the capabilities of the
65b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       default Internet request (mDefaultRequest), but which cannot cause
66b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       the framework to either create or retain the existence of any
673d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       specific network. Note that from the point of view of the request
683d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       matching code, TRACK_DEFAULT is identical to REQUEST: its special
693d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       behaviour is not due to different semantics, but to the fact that
703d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       the system will only ever create a TRACK_DEFAULT with capabilities
713d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       that are identical to the default request's capabilities, thus
723d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       causing it to share fate in every way with the default request.
733d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
743d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *     - BACKGROUND_REQUEST, like REQUEST but does not cause any networks
753d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       to retain the NET_CAPABILITY_FOREGROUND capability. A network with
763d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       no foreground requests is in the background. A network that has
773d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       one or more background requests and loses its last foreground
783d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       request to a higher-scoring network will not go into the
793d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       background immediately, but will linger and go into the background
803d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       after the linger timeout.
81b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
82b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *     - The value NONE is used only by applications. When an application
83b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       creates a NetworkRequest, it does not have a type; the type is set
84b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       by the system depending on the method used to file the request
85b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       (requestNetwork, registerNetworkCallback, etc.).
86b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
877b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     * @hide
887b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt     */
89b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    public static enum Type {
90b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        NONE,
91b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        LISTEN,
92b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        TRACK_DEFAULT,
933d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti        REQUEST,
943d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti        BACKGROUND_REQUEST,
95b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    };
96b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti
97b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    /**
98b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * The type of the request. This is only used by the system and is always NONE elsewhere.
99b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
100b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * @hide
101b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     */
102b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    public final Type type;
103b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti
104b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    /**
105b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * @hide
106b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     */
107b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    public NetworkRequest(NetworkCapabilities nc, int legacyType, int rId, Type type) {
108f5b74f981fb4e67f77a78b7038785fb3871acaa6Robert Greenwalt        if (nc == null) {
109f5b74f981fb4e67f77a78b7038785fb3871acaa6Robert Greenwalt            throw new NullPointerException();
110f5b74f981fb4e67f77a78b7038785fb3871acaa6Robert Greenwalt        }
1113c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        requestId = rId;
1123c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        networkCapabilities = nc;
11332aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt        this.legacyType = legacyType;
114b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        this.type = type;
1157b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    }
1167b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt
11734524f06fb77607884c69f891471725f4ac0889aRobert Greenwalt    /**
11834524f06fb77607884c69f891471725f4ac0889aRobert Greenwalt     * @hide
11934524f06fb77607884c69f891471725f4ac0889aRobert Greenwalt     */
1207b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt    public NetworkRequest(NetworkRequest that) {
1217b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt        networkCapabilities = new NetworkCapabilities(that.networkCapabilities);
1227b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt        requestId = that.requestId;
12332aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt        this.legacyType = that.legacyType;
124b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        this.type = that.type;
1253c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
1263c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
1277569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt    /**
1287569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt     * Builder used to create {@link NetworkRequest} objects.  Specify the Network features
1297569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt     * needed in terms of {@link NetworkCapabilities} features
1307569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt     */
1317569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt    public static class Builder {
1327569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        private final NetworkCapabilities mNetworkCapabilities = new NetworkCapabilities();
1337569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
1347569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
1357569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Default constructor for Builder.
1367569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
1377569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder() {}
1387569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
1397569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
1407569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Build {@link NetworkRequest} give the current set of capabilities.
1417569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
1427569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public NetworkRequest build() {
143487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            // Make a copy of mNetworkCapabilities so we don't inadvertently remove NOT_RESTRICTED
144487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            // when later an unrestricted capability could be added to mNetworkCapabilities, in
145487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            // which case NOT_RESTRICTED should be returned to mNetworkCapabilities, which
146487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            // maybeMarkCapabilitiesRestricted() doesn't add back.
147487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            final NetworkCapabilities nc = new NetworkCapabilities(mNetworkCapabilities);
148487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            nc.maybeMarkCapabilitiesRestricted();
149487ffe7d3d84bf65212158f7098e8a84b5b55e09Paul Jensen            return new NetworkRequest(nc, ConnectivityManager.TYPE_NONE,
150b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti                    ConnectivityManager.REQUEST_ID_UNSET, Type.NONE);
1517569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
1527569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
1537569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
1547569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Add the given capability requirement to this builder.  These represent
1557569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * the requested network's required capabilities.  Note that when searching
1567569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * for a network to satisfy a request, all capabilities requested must be
1573d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti         * satisfied.  See {@link NetworkCapabilities} for {@code NET_CAPABILITY_*}
1587569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * definitions.
1597569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         *
1607569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to add.
1617569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @return The builder to facilitate chaining
1627569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         *         {@code builder.addCapability(...).addCapability();}.
1637569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
1647569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder addCapability(int capability) {
1657569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.addCapability(capability);
1667569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
1677569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
1687569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
1697569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
1707569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Removes (if found) the given capability from this builder instance.
1717569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         *
1727569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @param capability The {@code NetworkCapabilities.NET_CAPABILITY_*} to remove.
1737569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @return The builder to facilitate chaining.
1747569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
1757569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder removeCapability(int capability) {
1767569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.removeCapability(capability);
1777569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
1787569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
1797569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
1807569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
18184b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         * Completely clears all the {@code NetworkCapabilities} from this builder instance,
18284b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         * removing even the capabilities that are set by default when the object is constructed.
18384b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         *
18484b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         * @return The builder to facilitate chaining.
18584b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         * @hide
18684b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti         */
18784b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti        public Builder clearCapabilities() {
18884b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti            mNetworkCapabilities.clearAll();
18984b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti            return this;
19084b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti        }
19184b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti
19284b83c5a95c9822683eec5a348893c1afba17444Lorenzo Colitti        /**
1937569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Adds the given transport requirement to this builder.  These represent
1947569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * the set of allowed transports for the request.  Only networks using one
1957569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * of these transports will satisfy the request.  If no particular transports
1967569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * are required, none should be specified here.  See {@link NetworkCapabilities}
1977569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * for {@code TRANSPORT_*} definitions.
1987569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         *
1997569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to add.
2007569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @return The builder to facilitate chaining.
2017569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
2027569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder addTransportType(int transportType) {
2037569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.addTransportType(transportType);
2047569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
2057569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
2067569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
2077569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
2087569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * Removes (if found) the given transport from this builder instance.
2097569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         *
2107569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @param transportType The {@code NetworkCapabilities.TRANSPORT_*} to remove.
2117569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @return The builder to facilitate chaining.
2127569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
2137569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder removeTransportType(int transportType) {
2147569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.removeTransportType(transportType);
2157569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
2167569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
2177569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
2187569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
2197569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @hide
2207569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
2217569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder setLinkUpstreamBandwidthKbps(int upKbps) {
2227569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.setLinkUpstreamBandwidthKbps(upKbps);
2237569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
2247569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
2257569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        /**
2267569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         * @hide
2277569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt         */
2287569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        public Builder setLinkDownstreamBandwidthKbps(int downKbps) {
2297569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            mNetworkCapabilities.setLinkDownstreamBandwidthKbps(downKbps);
2307569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt            return this;
2317569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt        }
2325f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt
2335f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt        /**
2345f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * Sets the optional bearer specific network specifier.
2355f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * This has no meaning if a single transport is also not specified, so calling
2365f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * this without a single transport set will generate an exception, as will
2375f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * subsequently adding or removing transports after this is set.
2385f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * </p>
2395f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * The interpretation of this {@code String} is bearer specific and bearers that use
2405f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * it should document their particulars.  For example, Bluetooth may use some sort of
2415f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * device id while WiFi could used ssid and/or bssid.  Cellular may use carrier spn.
2425f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         *
2435f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         * @param networkSpecifier An {@code String} of opaque format used to specify the bearer
2445f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         *                         specific network specifier where the bearer has a choice of
2455f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         *                         networks.
2465f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt         */
2475f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt        public Builder setNetworkSpecifier(String networkSpecifier) {
248ddb9ef0357e3ee609e188fb9e1e461f95aa601faEtan Cohen            if (NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER.equals(networkSpecifier)) {
249ddb9ef0357e3ee609e188fb9e1e461f95aa601faEtan Cohen                throw new IllegalArgumentException("Invalid network specifier - must not be '"
250ddb9ef0357e3ee609e188fb9e1e461f95aa601faEtan Cohen                        + NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER + "'");
251ddb9ef0357e3ee609e188fb9e1e461f95aa601faEtan Cohen            }
2525f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt            mNetworkCapabilities.setNetworkSpecifier(networkSpecifier);
2535f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt            return this;
2545f90bccd96397f23b988c5e668b13f5344fd104bRobert Greenwalt        }
255c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti
256c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti        /**
257c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * Sets the signal strength. This is a signed integer, with higher values indicating a
258c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * stronger signal. The exact units are bearer-dependent. For example, Wi-Fi uses the same
259c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * RSSI units reported by WifiManager.
260c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * <p>
261c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * Note that when used to register a network callback, this specifies the minimum acceptable
262c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * signal strength. When received as the state of an existing network it specifies the
263c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * current value. A value of {@code SIGNAL_STRENGTH_UNSPECIFIED} means no value when
264c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * received and has no effect when requesting a callback.
265c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         *
266c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * @param signalStrength the bearer-specific signal strength.
267c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         * @hide
268c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti         */
269c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti        public Builder setSignalStrength(int signalStrength) {
270c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti            mNetworkCapabilities.setSignalStrength(signalStrength);
271c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti            return this;
272c3f21f3b8b01508f5dc01b2e5fa4c3d652bb67bbLorenzo Colitti        }
2737569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt    }
2747569f1811f6061b2a6605c69bca1bb7af72ae1a0Robert Greenwalt
2753c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    // implement the Parcelable interface
2763c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public int describeContents() {
2773c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        return 0;
2783c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
2793c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public void writeToParcel(Parcel dest, int flags) {
2803c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        dest.writeParcelable(networkCapabilities, flags);
28132aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt        dest.writeInt(legacyType);
2823c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        dest.writeInt(requestId);
2831ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti        dest.writeString(type.name());
2843c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
2853c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public static final Creator<NetworkRequest> CREATOR =
2863c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        new Creator<NetworkRequest>() {
2873c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            public NetworkRequest createFromParcel(Parcel in) {
2883c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                NetworkCapabilities nc = (NetworkCapabilities)in.readParcelable(null);
28932aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt                int legacyType = in.readInt();
2903c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                int requestId = in.readInt();
2911ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti                Type type = Type.valueOf(in.readString());  // IllegalArgumentException if invalid.
2921ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti                NetworkRequest result = new NetworkRequest(nc, legacyType, requestId, type);
2937b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt                return result;
2943c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            }
2953c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            public NetworkRequest[] newArray(int size) {
2963c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                return new NetworkRequest[size];
2973c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            }
2983c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        };
2993c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
300b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    /**
3013d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * Returns true iff. this NetworkRequest is of type LISTEN.
302f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti     *
303f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti     * @hide
304f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti     */
305f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti    public boolean isListen() {
306f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti        return type == Type.LISTEN;
307f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti    }
308f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti
309f4a45f4c330fb4035ceb4878e18a2f7c0c772ab2Lorenzo Colitti    /**
310b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * Returns true iff. the contained NetworkRequest is one that:
311b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
312b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *     - should be associated with at most one satisfying network
313b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *       at a time;
314b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
3153d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *     - should cause a network to be kept up, but not necessarily in
3163d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       the foreground, if it is the best network which can satisfy the
3173d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       NetworkRequest.
318b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
319b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * For full detail of how isRequest() is used for pairing Networks with
320b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * NetworkRequests read rematchNetworkAndRequests().
321b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     *
322b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     * @hide
323b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti     */
324b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    public boolean isRequest() {
3253d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti        return isForegroundRequest() || isBackgroundRequest();
3263d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    }
3273d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti
3283d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    /**
3293d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * Returns true iff. the contained NetworkRequest is one that:
3303d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
3313d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *     - should be associated with at most one satisfying network
3323d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       at a time;
3333d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
3343d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *     - should cause a network to be kept up and in the foreground if
3353d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *       it is the best network which can satisfy the NetworkRequest.
3363d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
3373d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * For full detail of how isRequest() is used for pairing Networks with
3383d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * NetworkRequests read rematchNetworkAndRequests().
3393d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
3403d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * @hide
3413d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     */
3423d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    public boolean isForegroundRequest() {
343b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        return type == Type.TRACK_DEFAULT || type == Type.REQUEST;
344b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti    }
345b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti
3463d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    /**
3473d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * Returns true iff. this NetworkRequest is of type BACKGROUND_REQUEST.
3483d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     *
3493d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     * @hide
3503d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti     */
3513d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    public boolean isBackgroundRequest() {
3523d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti        return type == Type.BACKGROUND_REQUEST;
3533d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti    }
3543d4a10617c6d967aa0f07e129264e07320e99073Lorenzo Colitti
3553c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public String toString() {
356b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti        return "NetworkRequest [ " + type + " id=" + requestId +
357b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti                (legacyType != ConnectivityManager.TYPE_NONE ? ", legacyType=" + legacyType : "") +
3587b81602f3c18df8a4ca0342c514af8f7e394c0d7Robert Greenwalt                ", " + networkCapabilities.toString() + " ]";
3593c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
3603c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
3613c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public boolean equals(Object obj) {
3623c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        if (obj instanceof NetworkRequest == false) return false;
3633c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        NetworkRequest that = (NetworkRequest)obj;
36432aa65a11207e1fb7d346a68fdf3299f87a20db4Robert Greenwalt        return (that.legacyType == this.legacyType &&
3653c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                that.requestId == this.requestId &&
366b35d40d4e2205475a4a3111bb5a9fbd7bc6b0ca3Lorenzo Colitti                that.type == this.type &&
3671ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti                Objects.equals(that.networkCapabilities, this.networkCapabilities));
3683c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
3693c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
3703c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public int hashCode() {
3711ec11eb5bcd8dc2310ffc89b338cc01fad27b3ecLorenzo Colitti        return Objects.hash(requestId, legacyType, networkCapabilities, type);
3723c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
3733c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt}
374