blob: 4e57efa4d47b4eaee0dd617129f97e0cf40727cf [file] [log] [blame]
Hugo Benichi623ab7d2016-05-30 14:42:29 +09001/*
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
17package android.net.metrics;
18
19import android.net.ConnectivityMetricsEvent;
Hugo Benichi00a42d42016-09-13 15:55:09 +090020import android.net.IIpConnectivityMetrics;
Hugo Benichi623ab7d2016-05-30 14:42:29 +090021import android.os.Parcelable;
22import android.os.RemoteException;
Hugo Benichi00a42d42016-09-13 15:55:09 +090023import android.os.ServiceManager;
Hugo Benichi623ab7d2016-05-30 14:42:29 +090024import android.util.Log;
Hugo Benichi3bba2492016-05-30 14:42:29 +090025import com.android.internal.annotations.VisibleForTesting;
Hugo Benichidf456e12017-03-22 18:29:58 +090026import com.android.internal.util.BitUtils;
Hugo Benichi3bba2492016-05-30 14:42:29 +090027
Hugo Benichi623ab7d2016-05-30 14:42:29 +090028/**
Hugo Benichi00a42d42016-09-13 15:55:09 +090029 * Class for logging IpConnectvity events with IpConnectivityMetrics
Hugo Benichi623ab7d2016-05-30 14:42:29 +090030 * {@hide}
31 */
Hugo Benichi00a42d42016-09-13 15:55:09 +090032public class IpConnectivityLog {
33 private static final String TAG = IpConnectivityLog.class.getSimpleName();
34 private static final boolean DBG = false;
Hugo Benichi623ab7d2016-05-30 14:42:29 +090035
Hugo Benichieab511b2016-09-09 09:23:47 +090036 public static final String SERVICE_NAME = "connmetrics";
37
Hugo Benichi00a42d42016-09-13 15:55:09 +090038 private IIpConnectivityMetrics mService;
39
Hugo Benichi3bba2492016-05-30 14:42:29 +090040 public IpConnectivityLog() {
41 }
42
43 @VisibleForTesting
Hugo Benichi00a42d42016-09-13 15:55:09 +090044 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 Benichi3bba2492016-05-30 14:42:29 +090061 }
62
Hugo Benichi623ab7d2016-05-30 14:42:29 +090063 /**
Hugo Benichi946b7e42017-03-15 16:35:26 +090064 * 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 Benichi623ab7d2016-05-30 14:42:29 +090067 * @return true if the event was successfully logged.
68 */
Hugo Benichi946b7e42017-03-15 16:35:26 +090069 public boolean log(ConnectivityMetricsEvent ev) {
Hugo Benichi90cbc5b2016-07-29 16:29:46 +090070 if (!checkLoggerService()) {
Hugo Benichi623ab7d2016-05-30 14:42:29 +090071 if (DBG) {
Hugo Benichi00a42d42016-09-13 15:55:09 +090072 Log.d(TAG, SERVICE_NAME + " service was not ready");
Hugo Benichi623ab7d2016-05-30 14:42:29 +090073 }
74 return false;
75 }
Hugo Benichi946b7e42017-03-15 16:35:26 +090076 if (ev.timestamp == 0) {
77 ev.timestamp = System.currentTimeMillis();
78 }
Hugo Benichi623ab7d2016-05-30 14:42:29 +090079 try {
Hugo Benichiec27c4d2017-03-15 15:07:42 +090080 int left = mService.logEvent(ev);
Hugo Benichi00a42d42016-09-13 15:55:09 +090081 return left >= 0;
Hugo Benichi623ab7d2016-05-30 14:42:29 +090082 } catch (RemoteException e) {
83 Log.e(TAG, "Error logging event", e);
84 return false;
85 }
86 }
Hugo Benichicfddd682016-05-31 16:28:06 +090087
Hugo Benichi946b7e42017-03-15 16:35:26 +090088 /**
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 Benichi948a8592017-03-16 16:33:47 +0900103 * @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 Benichif927f0c2017-03-17 15:42:40 +0900115 * @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 Benichidf456e12017-03-22 18:29:58 +0900121 public boolean log(int netid, int[] transports, Parcelable data) {
Hugo Benichif927f0c2017-03-17 15:42:40 +0900122 ConnectivityMetricsEvent ev = makeEv(data);
123 ev.netId = netid;
Hugo Benichidf456e12017-03-22 18:29:58 +0900124 ev.transports = BitUtils.packBits(transports);
Hugo Benichif927f0c2017-03-17 15:42:40 +0900125 return log(ev);
126 }
127
128 /**
129 * Log an IpConnectivity event.
Hugo Benichi946b7e42017-03-15 16:35:26 +0900130 * @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 Benichicfddd682016-05-31 16:28:06 +0900141 }
Hugo Benichi623ab7d2016-05-30 14:42:29 +0900142}