NetworkRequest.java revision 3c0bf5e536ea6ee59385065d1d4830d3647cffaf
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
223c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltimport java.util.concurrent.atomic.AtomicInteger;
233c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
243c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt/**
253c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt * @hide
263c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt */
273c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwaltpublic class NetworkRequest implements Parcelable {
283c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public final NetworkCapabilities networkCapabilities;
293c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public final int requestId;
303c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public final boolean legacy;
313c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    private static final AtomicInteger sRequestId = new AtomicInteger();
323c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
333c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public NetworkRequest(NetworkCapabilities nc) {
343c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        this(nc, false, sRequestId.incrementAndGet());
353c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
363c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
373c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public NetworkRequest(NetworkCapabilities nc, boolean legacy) {
383c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        this(nc, legacy, sRequestId.incrementAndGet());
393c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
403c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
413c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    private NetworkRequest(NetworkCapabilities nc, boolean legacy, int rId) {
423c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        requestId = rId;
433c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        networkCapabilities = nc;
443c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        this.legacy = legacy;
453c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
463c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
473c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    // implement the Parcelable interface
483c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public int describeContents() {
493c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        return 0;
503c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
513c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public void writeToParcel(Parcel dest, int flags) {
523c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        dest.writeParcelable(networkCapabilities, flags);
533c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        dest.writeInt(legacy ? 1 : 0);
543c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        dest.writeInt(requestId);
553c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
563c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public static final Creator<NetworkRequest> CREATOR =
573c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        new Creator<NetworkRequest>() {
583c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            public NetworkRequest createFromParcel(Parcel in) {
593c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                NetworkCapabilities nc = (NetworkCapabilities)in.readParcelable(null);
603c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                boolean legacy = (in.readInt() == 1);
613c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                int requestId = in.readInt();
623c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                return new NetworkRequest(nc, legacy, requestId);
633c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            }
643c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            public NetworkRequest[] newArray(int size) {
653c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                return new NetworkRequest[size];
663c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt            }
673c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        };
683c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
693c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public String toString() {
703c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        return "NetworkRequest [ id=" + requestId + ", legacy=" + legacy + ", " +
713c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                networkCapabilities.toString() + " ]";
723c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
733c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
743c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public boolean equals(Object obj) {
753c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        if (obj instanceof NetworkRequest == false) return false;
763c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        NetworkRequest that = (NetworkRequest)obj;
773c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        return (that.legacy == this.legacy &&
783c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                that.requestId == this.requestId &&
793c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                ((that.networkCapabilities == null && this.networkCapabilities == null) ||
803c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                 (that.networkCapabilities != null &&
813c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt                  that.networkCapabilities.equals(this.networkCapabilities))));
823c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
833c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt
843c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    public int hashCode() {
853c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt        return requestId + (legacy ? 1013 : 2026) + (networkCapabilities.hashCode() * 1051);
863c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt    }
873c0bf5e536ea6ee59385065d1d4830d3647cffafRobert Greenwalt}
88