Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.metrics; |
| 18 | |
| 19 | import android.net.ConnectivityMetricsEvent; |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 20 | import android.net.IIpConnectivityMetrics; |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 21 | import android.os.Parcelable; |
| 22 | import android.os.RemoteException; |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 23 | import android.os.ServiceManager; |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 24 | import android.util.Log; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
Hugo Benichi | df456e1 | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 26 | import com.android.internal.util.BitUtils; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 27 | |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 28 | /** |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 29 | * Class for logging IpConnectvity events with IpConnectivityMetrics |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 30 | * {@hide} |
| 31 | */ |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 32 | public class IpConnectivityLog { |
| 33 | private static final String TAG = IpConnectivityLog.class.getSimpleName(); |
| 34 | private static final boolean DBG = false; |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 35 | |
Hugo Benichi | eab511b | 2016-09-09 09:23:47 +0900 | [diff] [blame] | 36 | public static final String SERVICE_NAME = "connmetrics"; |
| 37 | |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 38 | private IIpConnectivityMetrics mService; |
| 39 | |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 40 | public IpConnectivityLog() { |
| 41 | } |
| 42 | |
| 43 | @VisibleForTesting |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 44 | public IpConnectivityLog(IIpConnectivityMetrics service) { |
| 45 | mService = service; |
| 46 | } |
| 47 | |
| 48 | private boolean checkLoggerService() { |
| 49 | if (mService != null) { |
| 50 | return true; |
| 51 | } |
| 52 | final IIpConnectivityMetrics service = |
| 53 | IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME)); |
| 54 | if (service == null) { |
| 55 | return false; |
| 56 | } |
| 57 | // Two threads racing here will write the same pointer because getService |
| 58 | // is idempotent once MetricsLoggerService is initialized. |
| 59 | mService = service; |
| 60 | return true; |
Hugo Benichi | 3bba249 | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 61 | } |
| 62 | |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 63 | /** |
Hugo Benichi | 946b7e4 | 2017-03-15 16:35:26 +0900 | [diff] [blame] | 64 | * Log a ConnectivityMetricsEvent. |
| 65 | * @param ev the event to log. If the event timestamp is 0, |
| 66 | * the timestamp is set to the current time in milliseconds. |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 67 | * @return true if the event was successfully logged. |
| 68 | */ |
Hugo Benichi | 946b7e4 | 2017-03-15 16:35:26 +0900 | [diff] [blame] | 69 | public boolean log(ConnectivityMetricsEvent ev) { |
Hugo Benichi | 90cbc5b | 2016-07-29 16:29:46 +0900 | [diff] [blame] | 70 | if (!checkLoggerService()) { |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 71 | if (DBG) { |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 72 | Log.d(TAG, SERVICE_NAME + " service was not ready"); |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 73 | } |
| 74 | return false; |
| 75 | } |
Hugo Benichi | 946b7e4 | 2017-03-15 16:35:26 +0900 | [diff] [blame] | 76 | if (ev.timestamp == 0) { |
| 77 | ev.timestamp = System.currentTimeMillis(); |
| 78 | } |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 79 | try { |
Hugo Benichi | ec27c4d | 2017-03-15 15:07:42 +0900 | [diff] [blame] | 80 | int left = mService.logEvent(ev); |
Hugo Benichi | 00a42d4 | 2016-09-13 15:55:09 +0900 | [diff] [blame] | 81 | return left >= 0; |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 82 | } catch (RemoteException e) { |
| 83 | Log.e(TAG, "Error logging event", e); |
| 84 | return false; |
| 85 | } |
| 86 | } |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 87 | |
Hugo Benichi | 946b7e4 | 2017-03-15 16:35:26 +0900 | [diff] [blame] | 88 | /** |
| 89 | * Log an IpConnectivity event. |
| 90 | * @param timestamp is the epoch timestamp of the event in ms. |
| 91 | * If the timestamp is 0, the timestamp is set to the current time in milliseconds. |
| 92 | * @param data is a Parcelable instance representing the event. |
| 93 | * @return true if the event was successfully logged. |
| 94 | */ |
| 95 | public boolean log(long timestamp, Parcelable data) { |
| 96 | ConnectivityMetricsEvent ev = makeEv(data); |
| 97 | ev.timestamp = timestamp; |
| 98 | return log(ev); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Log an IpConnectivity event. |
Hugo Benichi | 948a859 | 2017-03-16 16:33:47 +0900 | [diff] [blame] | 103 | * @param ifname the network interface associated with the event. |
| 104 | * @param data is a Parcelable instance representing the event. |
| 105 | * @return true if the event was successfully logged. |
| 106 | */ |
| 107 | public boolean log(String ifname, Parcelable data) { |
| 108 | ConnectivityMetricsEvent ev = makeEv(data); |
| 109 | ev.ifname = ifname; |
| 110 | return log(ev); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Log an IpConnectivity event. |
Hugo Benichi | f927f0c | 2017-03-17 15:42:40 +0900 | [diff] [blame] | 115 | * @param netid the id of the network associated with the event. |
| 116 | * @param transports the current transports of the network associated with the event, as defined |
| 117 | * in NetworkCapabilities. |
| 118 | * @param data is a Parcelable instance representing the event. |
| 119 | * @return true if the event was successfully logged. |
| 120 | */ |
Hugo Benichi | df456e1 | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 121 | public boolean log(int netid, int[] transports, Parcelable data) { |
Hugo Benichi | f927f0c | 2017-03-17 15:42:40 +0900 | [diff] [blame] | 122 | ConnectivityMetricsEvent ev = makeEv(data); |
| 123 | ev.netId = netid; |
Hugo Benichi | df456e1 | 2017-03-22 18:29:58 +0900 | [diff] [blame] | 124 | ev.transports = BitUtils.packBits(transports); |
Hugo Benichi | f927f0c | 2017-03-17 15:42:40 +0900 | [diff] [blame] | 125 | return log(ev); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Log an IpConnectivity event. |
Hugo Benichi | 946b7e4 | 2017-03-15 16:35:26 +0900 | [diff] [blame] | 130 | * @param data is a Parcelable instance representing the event. |
| 131 | * @return true if the event was successfully logged. |
| 132 | */ |
| 133 | public boolean log(Parcelable data) { |
| 134 | return log(makeEv(data)); |
| 135 | } |
| 136 | |
| 137 | private static ConnectivityMetricsEvent makeEv(Parcelable data) { |
| 138 | ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent(); |
| 139 | ev.data = data; |
| 140 | return ev; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 141 | } |
Hugo Benichi | 623ab7d | 2016-05-30 14:42:29 +0900 | [diff] [blame] | 142 | } |