WifiP2pInfo.java revision 9cc2718abc0152d79e3e8bf23be94ddd3cc9db87
1/*
2 * Copyright (C) 2011 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.wifi.p2p;
18
19import android.os.Parcelable;
20import android.os.Parcel;
21
22import java.net.InetAddress;
23import java.net.UnknownHostException;
24
25/**
26 * A class representing connection info on Wi-fi P2p
27 * @hide
28 */
29public class WifiP2pInfo implements Parcelable {
30
31    public boolean groupFormed;
32
33    public boolean isGroupOwner;
34
35    public InetAddress groupOwnerAddress;
36
37    /** @hide */
38    WifiP2pInfo() {
39    }
40
41    /** @hide */
42    public String toString() {
43        StringBuffer sbuf = new StringBuffer();
44        sbuf.append("groupFormed: ").append(groupFormed)
45            .append("isGroupOwner: ").append(isGroupOwner)
46            .append("groupOwnerAddress: ").append(groupOwnerAddress);
47        return sbuf.toString();
48    }
49
50    /** Implement the Parcelable interface {@hide} */
51    public int describeContents() {
52        return 0;
53    }
54
55    /** copy constructor {@hide} */
56    public WifiP2pInfo(WifiP2pInfo source) {
57        if (source != null) {
58            groupFormed = source.groupFormed;
59            isGroupOwner = source.isGroupOwner;
60            groupOwnerAddress = source.groupOwnerAddress;
61       }
62    }
63
64    /** Implement the Parcelable interface {@hide} */
65    public void writeToParcel(Parcel dest, int flags) {
66        dest.writeByte(groupFormed ? (byte)1 : (byte)0);
67        dest.writeByte(isGroupOwner ? (byte)1 : (byte)0);
68
69        if (groupOwnerAddress != null) {
70            dest.writeByte((byte)1);
71            dest.writeByteArray(groupOwnerAddress.getAddress());
72        } else {
73            dest.writeByte((byte)0);
74        }
75    }
76
77    /** Implement the Parcelable interface {@hide} */
78    public static final Creator<WifiP2pInfo> CREATOR =
79        new Creator<WifiP2pInfo>() {
80            public WifiP2pInfo createFromParcel(Parcel in) {
81                WifiP2pInfo info = new WifiP2pInfo();
82                info.groupFormed = (in.readByte() == 1);
83                info.isGroupOwner = (in.readByte() == 1);
84                if (in.readByte() == 1) {
85                    try {
86                        info.groupOwnerAddress = InetAddress.getByAddress(in.createByteArray());
87                    } catch (UnknownHostException e) {}
88                }
89                return info;
90            }
91
92            public WifiP2pInfo[] newArray(int size) {
93                return new WifiP2pInfo[size];
94            }
95        };
96}
97