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.Parcel; 20 import android.os.Parcelable; 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(" groupOwnerIpAddress: ") 49 .append(groupOwnerAddress == null ? "none" : groupOwnerAddress.getHostAddress()); 50 return sbuf.toString(); 51 } 52 53 /** Implement the Parcelable interface */ describeContents()54 public int describeContents() { 55 return 0; 56 } 57 58 /** copy constructor */ WifiP2pInfo(WifiP2pInfo source)59 public WifiP2pInfo(WifiP2pInfo source) { 60 if (source != null) { 61 groupFormed = source.groupFormed; 62 isGroupOwner = source.isGroupOwner; 63 groupOwnerAddress = source.groupOwnerAddress; 64 } 65 } 66 67 /** Implement the Parcelable interface */ writeToParcel(Parcel dest, int flags)68 public void writeToParcel(Parcel dest, int flags) { 69 dest.writeByte(groupFormed ? (byte)1 : (byte)0); 70 dest.writeByte(isGroupOwner ? (byte)1 : (byte)0); 71 72 if (groupOwnerAddress != null) { 73 dest.writeByte((byte)1); 74 dest.writeByteArray(groupOwnerAddress.getAddress()); 75 } else { 76 dest.writeByte((byte)0); 77 } 78 } 79 80 /** Implement the Parcelable interface */ 81 public static final @android.annotation.NonNull Creator<WifiP2pInfo> CREATOR = 82 new Creator<WifiP2pInfo>() { 83 public WifiP2pInfo createFromParcel(Parcel in) { 84 WifiP2pInfo info = new WifiP2pInfo(); 85 info.groupFormed = (in.readByte() == 1); 86 info.isGroupOwner = (in.readByte() == 1); 87 if (in.readByte() == 1) { 88 try { 89 info.groupOwnerAddress = InetAddress.getByAddress(in.createByteArray()); 90 } catch (UnknownHostException e) {} 91 } 92 return info; 93 } 94 95 public WifiP2pInfo[] newArray(int size) { 96 return new WifiP2pInfo[size]; 97 } 98 }; 99 } 100