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