• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 package android.net.wifi.p2p;
17 
18 import android.annotation.NonNull;
19 import android.annotation.SystemApi;
20 import android.compat.annotation.UnsupportedAppUsage;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.util.LruCache;
24 
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Map;
29 
30 
31 /**
32  * A class representing a Wi-Fi P2p group list
33  *
34  * {@see WifiP2pManager}
35  * @hide
36  */
37 @SystemApi
38 public final class WifiP2pGroupList implements Parcelable {
39 
40     private static final int CREDENTIAL_MAX_NUM             =   32;
41 
42     @UnsupportedAppUsage
43     private final LruCache<Integer, WifiP2pGroup> mGroups;
44     private final GroupDeleteListener mListener;
45 
46     private boolean isClearCalled = false;
47 
48     /** @hide */
49     public interface GroupDeleteListener {
onDeleteGroup(int netId)50         public void onDeleteGroup(int netId);
51     }
52 
53     /** @hide */
WifiP2pGroupList()54     public WifiP2pGroupList() {
55         this(null, null);
56     }
57 
58     /** @hide */
59     @UnsupportedAppUsage
WifiP2pGroupList(WifiP2pGroupList source, GroupDeleteListener listener)60     public WifiP2pGroupList(WifiP2pGroupList source, GroupDeleteListener listener) {
61         mListener = listener;
62         mGroups = new LruCache<Integer, WifiP2pGroup>(CREDENTIAL_MAX_NUM) {
63             @Override
64             protected void entryRemoved(boolean evicted, Integer netId,
65                     WifiP2pGroup oldValue, WifiP2pGroup newValue) {
66                 if (mListener != null && !isClearCalled) {
67                     mListener.onDeleteGroup(oldValue.getNetworkId());
68                 }
69             }
70         };
71 
72         if (source != null) {
73             for (Map.Entry<Integer, WifiP2pGroup> item : source.mGroups.snapshot().entrySet()) {
74                 mGroups.put(item.getKey(), item.getValue());
75             }
76         }
77     }
78 
79     /**
80      * Get the list of P2P groups.
81      */
82     @NonNull
getGroupList()83     public List<WifiP2pGroup> getGroupList() {
84         return new ArrayList<>(mGroups.snapshot().values());
85     }
86 
87     /**
88      * Add the specified group to this group list.
89      *
90      * @param group
91      * @hide
92      */
add(WifiP2pGroup group)93     public void add(WifiP2pGroup group) {
94         mGroups.put(group.getNetworkId(), group);
95     }
96 
97     /**
98      * Remove the group with the specified network id from this group list.
99      *
100      * @param netId
101      * @hide
102      */
remove(int netId)103     public void remove(int netId) {
104         mGroups.remove(netId);
105     }
106 
107     /**
108      * Remove the group with the specified device address from this group list.
109      *
110      * @param deviceAddress
111      */
remove(String deviceAddress)112     void remove(String deviceAddress) {
113         remove(getNetworkId(deviceAddress));
114     }
115 
116     /**
117      * Clear the group.
118      * @hide
119      */
clear()120     public boolean clear() {
121         if (mGroups.size() == 0) return false;
122         isClearCalled = true;
123         mGroups.evictAll();
124         isClearCalled = false;
125         return true;
126     }
127 
128     /**
129      * Return the network id of the group owner profile with the specified p2p device
130      * address.
131      * If more than one persistent group of the same address is present in the list,
132      * return the first one.
133      *
134      * @param deviceAddress p2p device address.
135      * @return the network id. if not found, return -1.
136      * @hide
137      */
getNetworkId(String deviceAddress)138     public int getNetworkId(String deviceAddress) {
139         if (deviceAddress == null) return -1;
140 
141         final Collection<WifiP2pGroup> groups = mGroups.snapshot().values();
142         for (WifiP2pGroup grp: groups) {
143             if (deviceAddress.equalsIgnoreCase(grp.getOwner().deviceAddress)) {
144                 // update cache ordered.
145                 mGroups.get(grp.getNetworkId());
146                 return grp.getNetworkId();
147             }
148         }
149         return -1;
150     }
151 
152     /**
153      * Return the network id of the group with the specified p2p device address
154      * and the ssid.
155      *
156      * @param deviceAddress p2p device address.
157      * @param ssid ssid.
158      * @return the network id. if not found, return -1.
159      * @hide
160      */
getNetworkId(String deviceAddress, String ssid)161     public int getNetworkId(String deviceAddress, String ssid) {
162         if (deviceAddress == null || ssid == null) {
163             return -1;
164         }
165 
166         final Collection<WifiP2pGroup> groups = mGroups.snapshot().values();
167         for (WifiP2pGroup grp: groups) {
168             if (deviceAddress.equalsIgnoreCase(grp.getOwner().deviceAddress) &&
169                     ssid.equals(grp.getNetworkName())) {
170                 // update cache ordered.
171                 mGroups.get(grp.getNetworkId());
172                 return grp.getNetworkId();
173             }
174         }
175 
176         return -1;
177     }
178 
179     /**
180      * Return the group owner address of the group with the specified network id
181      *
182      * @param netId network id.
183      * @return the address. if not found, return null.
184      * @hide
185      */
getOwnerAddr(int netId)186     public String getOwnerAddr(int netId) {
187         WifiP2pGroup grp = mGroups.get(netId);
188         if (grp != null) {
189             return grp.getOwner().deviceAddress;
190         }
191         return null;
192     }
193 
194     /**
195      * Return true if this group list contains the specified network id.
196      * This function does NOT update LRU information.
197      * It means the internal queue is NOT reordered.
198      *
199      * @param netId network id.
200      * @return true if the specified network id is present in this group list.
201      * @hide
202      */
contains(int netId)203     public boolean contains(int netId) {
204         final Collection<WifiP2pGroup> groups = mGroups.snapshot().values();
205         for (WifiP2pGroup grp: groups) {
206             if (netId == grp.getNetworkId()) {
207                 return true;
208             }
209         }
210         return false;
211     }
212 
213     @Override
toString()214     public String toString() {
215         StringBuffer sbuf = new StringBuffer();
216 
217         final Collection<WifiP2pGroup> groups = mGroups.snapshot().values();
218         for (WifiP2pGroup grp: groups) {
219             sbuf.append(grp).append("\n");
220         }
221         return sbuf.toString();
222     }
223 
224     /** Implement the Parcelable interface */
225     @Override
describeContents()226     public int describeContents() {
227         return 0;
228     }
229 
230     /** Implement the Parcelable interface */
231     @Override
writeToParcel(@onNull Parcel dest, int flags)232     public void writeToParcel(@NonNull Parcel dest, int flags) {
233         final Collection<WifiP2pGroup> groups = mGroups.snapshot().values();
234         dest.writeInt(groups.size());
235         for(WifiP2pGroup group : groups) {
236             dest.writeParcelable(group, flags);
237         }
238     }
239 
240     /** Implement the Parcelable interface */
241     public static final @NonNull Creator<WifiP2pGroupList> CREATOR =
242         new Creator<WifiP2pGroupList>() {
243             public WifiP2pGroupList createFromParcel(Parcel in) {
244                 WifiP2pGroupList grpList = new WifiP2pGroupList();
245 
246                 int deviceCount = in.readInt();
247                 for (int i = 0; i < deviceCount; i++) {
248                     grpList.add((WifiP2pGroup)in.readParcelable(null));
249                 }
250                 return grpList;
251             }
252 
253             public WifiP2pGroupList[] newArray(int size) {
254                 return new WifiP2pGroupList[size];
255             }
256         };
257 }
258