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