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 import android.net.wifi.p2p.WifiP2pDevice; 22 import android.text.TextUtils; 23 24 import java.util.ArrayList; 25 import java.util.Collection; 26 import java.util.Collections; 27 import java.util.HashMap; 28 29 /** 30 * A class representing a Wi-Fi P2p device list. 31 * 32 * Note that the operations are not thread safe. 33 * {@see WifiP2pManager} 34 */ 35 public class WifiP2pDeviceList implements Parcelable { 36 37 private final HashMap<String, WifiP2pDevice> mDevices = new HashMap<String, WifiP2pDevice>(); 38 WifiP2pDeviceList()39 public WifiP2pDeviceList() { 40 } 41 42 /** copy constructor */ WifiP2pDeviceList(WifiP2pDeviceList source)43 public WifiP2pDeviceList(WifiP2pDeviceList source) { 44 if (source != null) { 45 for (WifiP2pDevice d : source.getDeviceList()) { 46 mDevices.put(d.deviceAddress, new WifiP2pDevice(d)); 47 } 48 } 49 } 50 51 /** @hide */ WifiP2pDeviceList(ArrayList<WifiP2pDevice> devices)52 public WifiP2pDeviceList(ArrayList<WifiP2pDevice> devices) { 53 for (WifiP2pDevice device : devices) { 54 if (device.deviceAddress != null) { 55 mDevices.put(device.deviceAddress, new WifiP2pDevice(device)); 56 } 57 } 58 } 59 validateDevice(WifiP2pDevice device)60 private void validateDevice(WifiP2pDevice device) { 61 if (device == null) throw new IllegalArgumentException("Null device"); 62 if (TextUtils.isEmpty(device.deviceAddress)) { 63 throw new IllegalArgumentException("Empty deviceAddress"); 64 } 65 } 66 validateDeviceAddress(String deviceAddress)67 private void validateDeviceAddress(String deviceAddress) { 68 if (TextUtils.isEmpty(deviceAddress)) { 69 throw new IllegalArgumentException("Empty deviceAddress"); 70 } 71 } 72 73 /** Clear the list @hide */ clear()74 public boolean clear() { 75 if (mDevices.isEmpty()) return false; 76 mDevices.clear(); 77 return true; 78 } 79 80 /** 81 * Add/update a device to the list. If the device is not found, a new device entry 82 * is created. If the device is already found, the device details are updated 83 * @param device to be updated 84 * @hide 85 */ update(WifiP2pDevice device)86 public void update(WifiP2pDevice device) { 87 updateSupplicantDetails(device); 88 mDevices.get(device.deviceAddress).status = device.status; 89 } 90 91 /** Only updates details fetched from the supplicant @hide */ updateSupplicantDetails(WifiP2pDevice device)92 public void updateSupplicantDetails(WifiP2pDevice device) { 93 validateDevice(device); 94 WifiP2pDevice d = mDevices.get(device.deviceAddress); 95 if (d != null) { 96 d.deviceName = device.deviceName; 97 d.primaryDeviceType = device.primaryDeviceType; 98 d.secondaryDeviceType = device.secondaryDeviceType; 99 d.wpsConfigMethodsSupported = device.wpsConfigMethodsSupported; 100 d.deviceCapability = device.deviceCapability; 101 d.groupCapability = device.groupCapability; 102 d.wfdInfo = device.wfdInfo; 103 return; 104 } 105 //Not found, add a new one 106 mDevices.put(device.deviceAddress, device); 107 } 108 109 /** @hide */ updateGroupCapability(String deviceAddress, int groupCapab)110 public void updateGroupCapability(String deviceAddress, int groupCapab) { 111 validateDeviceAddress(deviceAddress); 112 WifiP2pDevice d = mDevices.get(deviceAddress); 113 if (d != null) { 114 d.groupCapability = groupCapab; 115 } 116 } 117 118 /** @hide */ updateStatus(String deviceAddress, int status)119 public void updateStatus(String deviceAddress, int status) { 120 validateDeviceAddress(deviceAddress); 121 WifiP2pDevice d = mDevices.get(deviceAddress); 122 if (d != null) { 123 d.status = status; 124 } 125 } 126 127 /** 128 * Fetch a device from the list 129 * @param deviceAddress is the address of the device 130 * @return WifiP2pDevice device found, or null if none found 131 */ get(String deviceAddress)132 public WifiP2pDevice get(String deviceAddress) { 133 validateDeviceAddress(deviceAddress); 134 return mDevices.get(deviceAddress); 135 } 136 137 /** @hide */ remove(WifiP2pDevice device)138 public boolean remove(WifiP2pDevice device) { 139 validateDevice(device); 140 return mDevices.remove(device.deviceAddress) != null; 141 } 142 143 /** 144 * Remove a device from the list 145 * @param deviceAddress is the address of the device 146 * @return WifiP2pDevice device removed, or null if none removed 147 * @hide 148 */ remove(String deviceAddress)149 public WifiP2pDevice remove(String deviceAddress) { 150 validateDeviceAddress(deviceAddress); 151 return mDevices.remove(deviceAddress); 152 } 153 154 /** Returns true if any device the list was removed @hide */ remove(WifiP2pDeviceList list)155 public boolean remove(WifiP2pDeviceList list) { 156 boolean ret = false; 157 for (WifiP2pDevice d : list.mDevices.values()) { 158 if (remove(d)) ret = true; 159 } 160 return ret; 161 } 162 163 /** Get the list of devices */ getDeviceList()164 public Collection<WifiP2pDevice> getDeviceList() { 165 return Collections.unmodifiableCollection(mDevices.values()); 166 } 167 168 /** @hide */ isGroupOwner(String deviceAddress)169 public boolean isGroupOwner(String deviceAddress) { 170 validateDeviceAddress(deviceAddress); 171 WifiP2pDevice device = mDevices.get(deviceAddress); 172 if (device == null) { 173 throw new IllegalArgumentException("Device not found " + deviceAddress); 174 } 175 return device.isGroupOwner(); 176 } 177 toString()178 public String toString() { 179 StringBuffer sbuf = new StringBuffer(); 180 for (WifiP2pDevice device : mDevices.values()) { 181 sbuf.append("\n").append(device); 182 } 183 return sbuf.toString(); 184 } 185 186 /** Implement the Parcelable interface */ describeContents()187 public int describeContents() { 188 return 0; 189 } 190 191 /** Implement the Parcelable interface */ writeToParcel(Parcel dest, int flags)192 public void writeToParcel(Parcel dest, int flags) { 193 dest.writeInt(mDevices.size()); 194 for(WifiP2pDevice device : mDevices.values()) { 195 dest.writeParcelable(device, flags); 196 } 197 } 198 199 /** Implement the Parcelable interface */ 200 public static final Creator<WifiP2pDeviceList> CREATOR = 201 new Creator<WifiP2pDeviceList>() { 202 public WifiP2pDeviceList createFromParcel(Parcel in) { 203 WifiP2pDeviceList deviceList = new WifiP2pDeviceList(); 204 205 int deviceCount = in.readInt(); 206 for (int i = 0; i < deviceCount; i++) { 207 deviceList.update((WifiP2pDevice)in.readParcelable(null)); 208 } 209 return deviceList; 210 } 211 212 public WifiP2pDeviceList[] newArray(int size) { 213 return new WifiP2pDeviceList[size]; 214 } 215 }; 216 } 217