summaryrefslogtreecommitdiffstats
path: root/wifi/java/android/net/wifi/WifiConnectionStatistics.java
blob: 0046aa5cc6872d2d5330294759985c8893fb92ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.net.wifi;

import android.annotation.SystemApi;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import java.util.HashMap;

/**
 * Wifi Connection Statistics: gather various stats regarding WiFi connections,
 * connection requests, auto-join
 * and WiFi usage.
 * @hide
 */
@SystemApi
public class WifiConnectionStatistics implements Parcelable {
    private static final String TAG = "WifiConnnectionStatistics";

    /**
     *  history of past connection to untrusted SSID
     *  Key = SSID
     *  Value = num connection
     */
    public HashMap<String, WifiNetworkConnectionStatistics> untrustedNetworkHistory;

    // Number of time we polled the chip and were on 5GHz
    public int num5GhzConnected;

    // Number of time we polled the chip and were on 2.4GHz
    public int num24GhzConnected;

    // Number autojoin attempts
    public int numAutoJoinAttempt;

    // Number auto-roam attempts
    public int numAutoRoamAttempt;

    // Number wifimanager join attempts
    public int numWifiManagerJoinAttempt;

    public WifiConnectionStatistics() {
        untrustedNetworkHistory = new HashMap<String, WifiNetworkConnectionStatistics>();
    }

    public void incrementOrAddUntrusted(String SSID, int connection, int usage) {
        WifiNetworkConnectionStatistics stats;
        if (TextUtils.isEmpty(SSID))
            return;
        if (untrustedNetworkHistory.containsKey(SSID)) {
            stats = untrustedNetworkHistory.get(SSID);
            if (stats != null){
                stats.numConnection = connection + stats.numConnection;
                stats.numUsage = usage + stats.numUsage;
            }
        } else {
            stats = new WifiNetworkConnectionStatistics(connection, usage);
        }
        if (stats != null) {
            untrustedNetworkHistory.put(SSID, stats);
        }
    }

    @Override
    public String toString() {
        StringBuilder sbuf = new StringBuilder();
        sbuf.append("Connected on: 2.4Ghz=").append(num24GhzConnected);
        sbuf.append(" 5Ghz=").append(num5GhzConnected).append("\n");
        sbuf.append(" join=").append(numWifiManagerJoinAttempt);
        sbuf.append("\\").append(numAutoJoinAttempt).append("\n");
        sbuf.append(" roam=").append(numAutoRoamAttempt).append("\n");

        for (String Key : untrustedNetworkHistory.keySet()) {
            WifiNetworkConnectionStatistics stats = untrustedNetworkHistory.get(Key);
            if (stats != null) {
                sbuf.append(Key).append(" ").append(stats.toString()).append("\n");
            }
        }
        return sbuf.toString();
    }

    /** copy constructor*/
    public WifiConnectionStatistics(WifiConnectionStatistics source) {
        untrustedNetworkHistory = new HashMap<String, WifiNetworkConnectionStatistics>();
        if (source != null) {
            untrustedNetworkHistory.putAll(source.untrustedNetworkHistory);
        }
    }

    /** Implement the Parcelable interface */
    public int describeContents() {
        return 0;
    }

    /** Implement the Parcelable interface */
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(num24GhzConnected);
        dest.writeInt(num5GhzConnected);
        dest.writeInt(numAutoJoinAttempt);
        dest.writeInt(numAutoRoamAttempt);
        dest.writeInt(numWifiManagerJoinAttempt);

        dest.writeInt(untrustedNetworkHistory.size());
        for (String Key : untrustedNetworkHistory.keySet()) {
            WifiNetworkConnectionStatistics num = untrustedNetworkHistory.get(Key);
            dest.writeString(Key);
            dest.writeInt(num.numConnection);
            dest.writeInt(num.numUsage);

        }
    }

    /** Implement the Parcelable interface */
    public static final Creator<WifiConnectionStatistics> CREATOR =
        new Creator<WifiConnectionStatistics>() {
            public WifiConnectionStatistics createFromParcel(Parcel in) {
                WifiConnectionStatistics stats = new WifiConnectionStatistics();
                stats.num24GhzConnected = in.readInt();
                stats.num5GhzConnected = in.readInt();
                stats.numAutoJoinAttempt = in.readInt();
                stats.numAutoRoamAttempt = in.readInt();
                stats.numWifiManagerJoinAttempt = in.readInt();
                int n = in.readInt();
                while (n-- > 0) {
                    String Key = in.readString();
                    int numConnection = in.readInt();
                    int numUsage = in.readInt();
                    WifiNetworkConnectionStatistics st =
                            new WifiNetworkConnectionStatistics(numConnection, numUsage);
                    stats.untrustedNetworkHistory.put(Key, st);
                }
                return stats;
            }

            public WifiConnectionStatistics[] newArray(int size) {
                return new WifiConnectionStatistics[size];
            }
        };
}