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    /** Returns true if any device the list was removed @hide */
118    public boolean remove(WifiP2pDeviceList list) {
119        boolean ret = false;
120        for (WifiP2pDevice d : list.mDevices.values()) {
121            if (remove(d)) ret = true;
122        }
123        return ret;
124    }
125
126    /** Get the list of devices */
127    public Collection<WifiP2pDevice> getDeviceList() {
128        return Collections.unmodifiableCollection(mDevices.values());
129    }
130
131    /** @hide */
132    public boolean isGroupOwner(String deviceAddress) {
133        if (deviceAddress != null) {
134            WifiP2pDevice device = mDevices.get(deviceAddress);
135            if (device != null) return device.isGroupOwner();
136        }
137        return false;
138    }
139
140    public String toString() {
141        StringBuffer sbuf = new StringBuffer();
142        for (WifiP2pDevice device : mDevices.values()) {
143            sbuf.append("\n").append(device);
144        }
145        return sbuf.toString();
146    }
147
148    /** Implement the Parcelable interface */
149    public int describeContents() {
150        return 0;
151    }
152
153    /** Implement the Parcelable interface */
154    public void writeToParcel(Parcel dest, int flags) {
155        dest.writeInt(mDevices.size());
156        for(WifiP2pDevice device : mDevices.values()) {
157            dest.writeParcelable(device, flags);
158        }
159    }
160
161    /** Implement the Parcelable interface */
162    public static final Creator<WifiP2pDeviceList> CREATOR =
163        new Creator<WifiP2pDeviceList>() {
164            public WifiP2pDeviceList createFromParcel(Parcel in) {
165                WifiP2pDeviceList deviceList = new WifiP2pDeviceList();
166
167                int deviceCount = in.readInt();
168                for (int i = 0; i < deviceCount; i++) {
169                    deviceList.update((WifiP2pDevice)in.readParcelable(null));
170                }
171                return deviceList;
172            }
173
174            public WifiP2pDeviceList[] newArray(int size) {
175                return new WifiP2pDeviceList[size];
176            }
177        };
178}
179