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