IpConnectivityLog.java revision 3bba249c4711b10b2ba5335c7b6653dc570aae64
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 = false;
35
36    public IpConnectivityLog() {
37    }
38
39    @VisibleForTesting
40    public IpConnectivityLog(IConnectivityMetricsLogger service) {
41        super(service);
42    }
43
44    /**
45     * Log an IpConnectivity event. Contrary to logEvent(), this method does not
46     * keep track of skipped events and is thread-safe for callers.
47     *
48     * @param timestamp is the epoch timestamp of the event in ms.
49     * @param data is a Parcelable IpConnectivityEvent instance representing the event.
50     *
51     * @return true if the event was successfully logged.
52     */
53    public <T extends IpConnectivityEvent & Parcelable> boolean log(long timestamp, T data) {
54        if (mService == null) {
55            if (DBG) {
56                Log.d(TAG, CONNECTIVITY_METRICS_LOGGER_SERVICE + " service not ready");
57            }
58            return false;
59        }
60
61        if (System.currentTimeMillis() < mServiceUnblockedTimestampMillis) {
62            if (DBG) {
63                Log.d(TAG, "skipping logging due to throttling for IpConnectivity component");
64            }
65            return false;
66        }
67
68        try {
69            final ConnectivityMetricsEvent event =
70                new ConnectivityMetricsEvent(timestamp, COMPONENT_TAG_CONNECTIVITY, 0, data);
71            final long result = mService.logEvent(event);
72            if (result >= 0) {
73                mServiceUnblockedTimestampMillis = result;
74            }
75            return (result == 0);
76        } catch (RemoteException e) {
77            Log.e(TAG, "Error logging event", e);
78            return false;
79        }
80    }
81}
82