IpConnectivityLog.java revision eab511b582cc00364dee7835534bb511719f9231
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
17package android.net.metrics;
18
19import android.net.ConnectivityMetricsEvent;
20import android.net.ConnectivityMetricsLogger;
21import android.net.IConnectivityMetricsLogger;
22import android.os.Parcelable;
23import android.os.RemoteException;
24import android.util.Log;
25
26import com.android.internal.annotations.VisibleForTesting;
27
28/**
29 * Specialization of the ConnectivityMetricsLogger class for recording IP connectivity events.
30 * {@hide}
31 */
32public class IpConnectivityLog extends ConnectivityMetricsLogger {
33    private static String TAG = "IpConnectivityMetricsLogger";
34    private static final boolean DBG = true;
35
36    public static final String SERVICE_NAME = "connmetrics";
37
38    public IpConnectivityLog() {
39        // mService initialized in super constructor.
40    }
41
42    @VisibleForTesting
43    public IpConnectivityLog(IConnectivityMetricsLogger service) {
44        super(service);
45    }
46
47    /**
48     * Log an IpConnectivity event. Contrary to logEvent(), this method does not
49     * keep track of skipped events and is thread-safe for callers.
50     *
51     * @param timestamp is the epoch timestamp of the event in ms.
52     * @param data is a Parcelable instance representing the event.
53     *
54     * @return true if the event was successfully logged.
55     */
56    public boolean log(long timestamp, Parcelable data) {
57        if (!checkLoggerService()) {
58            if (DBG) {
59                Log.d(TAG, CONNECTIVITY_METRICS_LOGGER_SERVICE + " service was not ready");
60            }
61            return false;
62        }
63
64        if (System.currentTimeMillis() < mServiceUnblockedTimestampMillis) {
65            if (DBG) {
66                Log.d(TAG, "skipping logging due to throttling for IpConnectivity component");
67            }
68            return false;
69        }
70
71        try {
72            final ConnectivityMetricsEvent event =
73                new ConnectivityMetricsEvent(timestamp, COMPONENT_TAG_CONNECTIVITY, 0, data);
74            final long result = mService.logEvent(event);
75            if (result >= 0) {
76                mServiceUnblockedTimestampMillis = result;
77            }
78            return (result == 0);
79        } catch (RemoteException e) {
80            Log.e(TAG, "Error logging event", e);
81            return false;
82        }
83    }
84
85    public void log(Parcelable event) {
86        log(System.currentTimeMillis(), event);
87    }
88}
89