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