WifiP2pInfo.java revision ea5b16ac5751022de73e8f1225407eb01e7f1824
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    public WifiP2pInfo() {
38    }
39
40    public String toString() {
41        StringBuffer sbuf = new StringBuffer();
42        sbuf.append("groupFormed: ").append(groupFormed)
43            .append("isGroupOwner: ").append(isGroupOwner)
44            .append("groupOwnerAddress: ").append(groupOwnerAddress);
45        return sbuf.toString();
46    }
47
48    /** Implement the Parcelable interface {@hide} */
49    public int describeContents() {
50        return 0;
51    }
52
53    /** copy constructor {@hide} */
54    public WifiP2pInfo(WifiP2pInfo source) {
55        if (source != null) {
56            groupFormed = source.groupFormed;
57            isGroupOwner = source.isGroupOwner;
58            groupOwnerAddress = source.groupOwnerAddress;
59       }
60    }
61
62    /** Implement the Parcelable interface {@hide} */
63    public void writeToParcel(Parcel dest, int flags) {
64        dest.writeByte(groupFormed ? (byte)1 : (byte)0);
65        dest.writeByte(isGroupOwner ? (byte)1 : (byte)0);
66
67        if (groupOwnerAddress != null) {
68            dest.writeByte((byte)1);
69            dest.writeByteArray(groupOwnerAddress.getAddress());
70        } else {
71            dest.writeByte((byte)0);
72        }
73    }
74
75    /** Implement the Parcelable interface {@hide} */
76    public static final Creator<WifiP2pInfo> CREATOR =
77        new Creator<WifiP2pInfo>() {
78            public WifiP2pInfo createFromParcel(Parcel in) {
79                WifiP2pInfo info = new WifiP2pInfo();
80                info.groupFormed = (in.readByte() == 1);
81                info.isGroupOwner = (in.readByte() == 1);
82                if (in.readByte() == 1) {
83                    try {
84                        info.groupOwnerAddress = InetAddress.getByAddress(in.createByteArray());
85                    } catch (UnknownHostException e) {}
86                }
87                return info;
88            }
89
90            public WifiP2pInfo[] newArray(int size) {
91                return new WifiP2pInfo[size];
92            }
93        };
94}
95