1/*
2 * Copyright (C) 2014 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;
18
19import android.annotation.SystemApi;
20
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24
25import java.util.HashMap;
26
27/**
28 * Wifi Connection Statistics: gather various stats regarding WiFi connections,
29 * connection requests, auto-join
30 * and WiFi usage.
31 * @hide
32 * @removed
33 */
34@SystemApi
35public class WifiConnectionStatistics implements Parcelable {
36    private static final String TAG = "WifiConnnectionStatistics";
37
38    /**
39     *  history of past connection to untrusted SSID
40     *  Key = SSID
41     *  Value = num connection
42     */
43    public HashMap<String, WifiNetworkConnectionStatistics> untrustedNetworkHistory;
44
45    // Number of time we polled the chip and were on 5GHz
46    public int num5GhzConnected;
47
48    // Number of time we polled the chip and were on 2.4GHz
49    public int num24GhzConnected;
50
51    // Number autojoin attempts
52    public int numAutoJoinAttempt;
53
54    // Number auto-roam attempts
55    public int numAutoRoamAttempt;
56
57    // Number wifimanager join attempts
58    public int numWifiManagerJoinAttempt;
59
60    public WifiConnectionStatistics() {
61        untrustedNetworkHistory = new HashMap<String, WifiNetworkConnectionStatistics>();
62    }
63
64    public void incrementOrAddUntrusted(String SSID, int connection, int usage) {
65        WifiNetworkConnectionStatistics stats;
66        if (TextUtils.isEmpty(SSID))
67            return;
68        if (untrustedNetworkHistory.containsKey(SSID)) {
69            stats = untrustedNetworkHistory.get(SSID);
70            if (stats != null){
71                stats.numConnection = connection + stats.numConnection;
72                stats.numUsage = usage + stats.numUsage;
73            }
74        } else {
75            stats = new WifiNetworkConnectionStatistics(connection, usage);
76        }
77        if (stats != null) {
78            untrustedNetworkHistory.put(SSID, stats);
79        }
80    }
81
82    @Override
83    public String toString() {
84        StringBuilder sbuf = new StringBuilder();
85        sbuf.append("Connected on: 2.4Ghz=").append(num24GhzConnected);
86        sbuf.append(" 5Ghz=").append(num5GhzConnected).append("\n");
87        sbuf.append(" join=").append(numWifiManagerJoinAttempt);
88        sbuf.append("\\").append(numAutoJoinAttempt).append("\n");
89        sbuf.append(" roam=").append(numAutoRoamAttempt).append("\n");
90
91        for (String Key : untrustedNetworkHistory.keySet()) {
92            WifiNetworkConnectionStatistics stats = untrustedNetworkHistory.get(Key);
93            if (stats != null) {
94                sbuf.append(Key).append(" ").append(stats.toString()).append("\n");
95            }
96        }
97        return sbuf.toString();
98    }
99
100    /** copy constructor*/
101    public WifiConnectionStatistics(WifiConnectionStatistics source) {
102        untrustedNetworkHistory = new HashMap<String, WifiNetworkConnectionStatistics>();
103        if (source != null) {
104            untrustedNetworkHistory.putAll(source.untrustedNetworkHistory);
105        }
106    }
107
108    /** Implement the Parcelable interface */
109    public int describeContents() {
110        return 0;
111    }
112
113    /** Implement the Parcelable interface */
114    @Override
115    public void writeToParcel(Parcel dest, int flags) {
116        dest.writeInt(num24GhzConnected);
117        dest.writeInt(num5GhzConnected);
118        dest.writeInt(numAutoJoinAttempt);
119        dest.writeInt(numAutoRoamAttempt);
120        dest.writeInt(numWifiManagerJoinAttempt);
121
122        dest.writeInt(untrustedNetworkHistory.size());
123        for (String Key : untrustedNetworkHistory.keySet()) {
124            WifiNetworkConnectionStatistics num = untrustedNetworkHistory.get(Key);
125            dest.writeString(Key);
126            dest.writeInt(num.numConnection);
127            dest.writeInt(num.numUsage);
128
129        }
130    }
131
132    /** Implement the Parcelable interface */
133    public static final Creator<WifiConnectionStatistics> CREATOR =
134        new Creator<WifiConnectionStatistics>() {
135            public WifiConnectionStatistics createFromParcel(Parcel in) {
136                WifiConnectionStatistics stats = new WifiConnectionStatistics();
137                stats.num24GhzConnected = in.readInt();
138                stats.num5GhzConnected = in.readInt();
139                stats.numAutoJoinAttempt = in.readInt();
140                stats.numAutoRoamAttempt = in.readInt();
141                stats.numWifiManagerJoinAttempt = in.readInt();
142                int n = in.readInt();
143                while (n-- > 0) {
144                    String Key = in.readString();
145                    int numConnection = in.readInt();
146                    int numUsage = in.readInt();
147                    WifiNetworkConnectionStatistics st =
148                            new WifiNetworkConnectionStatistics(numConnection, numUsage);
149                    stats.untrustedNetworkHistory.put(Key, st);
150                }
151                return stats;
152            }
153
154            public WifiConnectionStatistics[] newArray(int size) {
155                return new WifiConnectionStatistics[size];
156            }
157        };
158}
159