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; 21import android.net.wifi.p2p.WifiP2pDevice; 22import android.text.TextUtils; 23import android.util.Log; 24 25import java.util.ArrayList; 26import java.util.Collection; 27import java.util.Collections; 28import java.util.HashMap; 29 30/** 31 * A class representing a Wi-Fi P2p device list. 32 * 33 * Note that the operations are not thread safe. 34 * {@see WifiP2pManager} 35 */ 36public class WifiP2pDeviceList implements Parcelable { 37 38 private HashMap<String, WifiP2pDevice> mDevices; 39 40 public WifiP2pDeviceList() { 41 mDevices = new HashMap<String, WifiP2pDevice>(); 42 } 43 44 /** copy constructor */ 45 public WifiP2pDeviceList(WifiP2pDeviceList source) { 46 if (source != null) { 47 for (WifiP2pDevice d : source.getDeviceList()) { 48 mDevices.put(d.deviceAddress, d); 49 } 50 } 51 } 52 53 /** @hide */ 54 public WifiP2pDeviceList(ArrayList<WifiP2pDevice> devices) { 55 mDevices = new HashMap<String, WifiP2pDevice>(); 56 for (WifiP2pDevice device : devices) { 57 if (device.deviceAddress != null) { 58 mDevices.put(device.deviceAddress, device); 59 } 60 } 61 } 62 63 /** @hide */ 64 public boolean clear() { 65 if (mDevices.isEmpty()) return false; 66 mDevices.clear(); 67 return true; 68 } 69 70 /** @hide */ 71 public void update(WifiP2pDevice device) { 72 if (device == null || device.deviceAddress == null) return; 73 WifiP2pDevice d = mDevices.get(device.deviceAddress); 74 if (d != null) { 75 d.deviceName = device.deviceName; 76 d.primaryDeviceType = device.primaryDeviceType; 77 d.secondaryDeviceType = device.secondaryDeviceType; 78 d.wpsConfigMethodsSupported = device.wpsConfigMethodsSupported; 79 d.deviceCapability = device.deviceCapability; 80 d.groupCapability = device.groupCapability; 81 return; 82 } 83 //Not found, add a new one 84 mDevices.put(device.deviceAddress, device); 85 } 86 87 /** @hide */ 88 public void updateGroupCapability(String deviceAddress, int groupCapab) { 89 if (TextUtils.isEmpty(deviceAddress)) return; 90 WifiP2pDevice d = mDevices.get(deviceAddress); 91 if (d != null) { 92 d.groupCapability = groupCapab; 93 } 94 } 95 96 /** @hide */ 97 public void updateStatus(String deviceAddress, int status) { 98 if (TextUtils.isEmpty(deviceAddress)) return; 99 WifiP2pDevice d = mDevices.get(deviceAddress); 100 if (d != null) { 101 d.status = status; 102 } 103 } 104 105 /** @hide */ 106 public WifiP2pDevice get(String deviceAddress) { 107 if (deviceAddress == null) return null; 108 109 return mDevices.get(deviceAddress); 110 } 111 112 /** @hide */ 113 public boolean remove(WifiP2pDevice device) { 114 if (device == null || device.deviceAddress == null) return false; 115 return mDevices.remove(device.deviceAddress) != null; 116 } 117 118 /** Get the list of devices */ 119 public Collection<WifiP2pDevice> getDeviceList() { 120 return Collections.unmodifiableCollection(mDevices.values()); 121 } 122 123 /** @hide */ 124 public boolean isGroupOwner(String deviceAddress) { 125 if (deviceAddress != null) { 126 WifiP2pDevice device = mDevices.get(deviceAddress); 127 if (device != null) return device.isGroupOwner(); 128 } 129 return false; 130 } 131 132 public String toString() { 133 StringBuffer sbuf = new StringBuffer(); 134 for (WifiP2pDevice device : mDevices.values()) { 135 sbuf.append("\n").append(device); 136 } 137 return sbuf.toString(); 138 } 139 140 /** Implement the Parcelable interface */ 141 public int describeContents() { 142 return 0; 143 } 144 145 /** Implement the Parcelable interface */ 146 public void writeToParcel(Parcel dest, int flags) { 147 dest.writeInt(mDevices.size()); 148 for(WifiP2pDevice device : mDevices.values()) { 149 dest.writeParcelable(device, flags); 150 } 151 } 152 153 /** Implement the Parcelable interface */ 154 public static final Creator<WifiP2pDeviceList> CREATOR = 155 new Creator<WifiP2pDeviceList>() { 156 public WifiP2pDeviceList createFromParcel(Parcel in) { 157 WifiP2pDeviceList deviceList = new WifiP2pDeviceList(); 158 159 int deviceCount = in.readInt(); 160 for (int i = 0; i < deviceCount; i++) { 161 deviceList.update((WifiP2pDevice)in.readParcelable(null)); 162 } 163 return deviceList; 164 } 165 166 public WifiP2pDeviceList[] newArray(int size) { 167 return new WifiP2pDeviceList[size]; 168 } 169 }; 170} 171