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