NetworkRequest.java revision 3c0bf5e536ea6ee59385065d1d4830d3647cffaf
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.net;
18
19import android.os.Parcel;
20import android.os.Parcelable;
21
22import java.util.concurrent.atomic.AtomicInteger;
23
24/**
25 * @hide
26 */
27public class NetworkRequest implements Parcelable {
28    public final NetworkCapabilities networkCapabilities;
29    public final int requestId;
30    public final boolean legacy;
31    private static final AtomicInteger sRequestId = new AtomicInteger();
32
33    public NetworkRequest(NetworkCapabilities nc) {
34        this(nc, false, sRequestId.incrementAndGet());
35    }
36
37    public NetworkRequest(NetworkCapabilities nc, boolean legacy) {
38        this(nc, legacy, sRequestId.incrementAndGet());
39    }
40
41    private NetworkRequest(NetworkCapabilities nc, boolean legacy, int rId) {
42        requestId = rId;
43        networkCapabilities = nc;
44        this.legacy = legacy;
45    }
46
47    // implement the Parcelable interface
48    public int describeContents() {
49        return 0;
50    }
51    public void writeToParcel(Parcel dest, int flags) {
52        dest.writeParcelable(networkCapabilities, flags);
53        dest.writeInt(legacy ? 1 : 0);
54        dest.writeInt(requestId);
55    }
56    public static final Creator<NetworkRequest> CREATOR =
57        new Creator<NetworkRequest>() {
58            public NetworkRequest createFromParcel(Parcel in) {
59                NetworkCapabilities nc = (NetworkCapabilities)in.readParcelable(null);
60                boolean legacy = (in.readInt() == 1);
61                int requestId = in.readInt();
62                return new NetworkRequest(nc, legacy, requestId);
63            }
64            public NetworkRequest[] newArray(int size) {
65                return new NetworkRequest[size];
66            }
67        };
68
69    public String toString() {
70        return "NetworkRequest [ id=" + requestId + ", legacy=" + legacy + ", " +
71                networkCapabilities.toString() + " ]";
72    }
73
74    public boolean equals(Object obj) {
75        if (obj instanceof NetworkRequest == false) return false;
76        NetworkRequest that = (NetworkRequest)obj;
77        return (that.legacy == this.legacy &&
78                that.requestId == this.requestId &&
79                ((that.networkCapabilities == null && this.networkCapabilities == null) ||
80                 (that.networkCapabilities != null &&
81                  that.networkCapabilities.equals(this.networkCapabilities))));
82    }
83
84    public int hashCode() {
85        return requestId + (legacy ? 1013 : 2026) + (networkCapabilities.hashCode() * 1051);
86    }
87}
88