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