WifiP2pDeviceList.java revision ea5b16ac5751022de73e8f1225407eb01e7f1824
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.util.Log;
23
24import java.util.ArrayList;
25import java.util.Collection;
26import java.util.Collections;
27
28/**
29 * A class representing a Wi-Fi P2p device list
30 * @hide
31 */
32public class WifiP2pDeviceList implements Parcelable {
33
34    private Collection<WifiP2pDevice> mDevices;
35
36    public WifiP2pDeviceList() {
37        mDevices = new ArrayList<WifiP2pDevice>();
38    }
39
40    //copy constructor
41    public WifiP2pDeviceList(WifiP2pDeviceList source) {
42        if (source != null) {
43            mDevices = source.getDeviceList();
44        }
45    }
46
47    public WifiP2pDeviceList(ArrayList<WifiP2pDevice> devices) {
48        mDevices = new ArrayList<WifiP2pDevice>();
49        for (WifiP2pDevice device : devices) {
50            mDevices.add(device);
51        }
52    }
53
54    public boolean clear() {
55        if (mDevices.isEmpty()) return false;
56        mDevices.clear();
57        return true;
58    }
59
60    public void add(WifiP2pDevice device) {
61        if (device == null) return;
62        if (mDevices.contains(device)) return;
63        mDevices.add(device);
64    }
65
66    public boolean remove(WifiP2pDevice device) {
67        if (device == null) return false;
68        return mDevices.remove(device);
69    }
70
71    public Collection<WifiP2pDevice> getDeviceList() {
72        return Collections.unmodifiableCollection(mDevices);
73    }
74
75    public String toString() {
76        StringBuffer sbuf = new StringBuffer();
77        for (WifiP2pDevice device : mDevices) {
78            sbuf.append("\n").append(device);
79        }
80        return sbuf.toString();
81    }
82
83    /** Implement the Parcelable interface {@hide} */
84    public int describeContents() {
85        return 0;
86    }
87
88    /** Implement the Parcelable interface {@hide} */
89    public void writeToParcel(Parcel dest, int flags) {
90        dest.writeInt(mDevices.size());
91        for(WifiP2pDevice device : mDevices) {
92            dest.writeParcelable(device, flags);
93        }
94    }
95
96    /** Implement the Parcelable interface {@hide} */
97    public static final Creator<WifiP2pDeviceList> CREATOR =
98        new Creator<WifiP2pDeviceList>() {
99            public WifiP2pDeviceList createFromParcel(Parcel in) {
100                WifiP2pDeviceList deviceList = new WifiP2pDeviceList();
101
102                int deviceCount = in.readInt();
103                for (int i = 0; i < deviceCount; i++) {
104                    deviceList.add((WifiP2pDevice)in.readParcelable(null));
105                }
106                return deviceList;
107            }
108
109            public WifiP2pDeviceList[] newArray(int size) {
110                return new WifiP2pDeviceList[size];
111            }
112        };
113}
114