IpConnectivityLog.java revision 946b7e424e0e4d5bffc65ef405f32b966d192d3c
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.IIpConnectivityMetrics;
21import android.os.Parcelable;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.util.Log;
25import com.android.internal.annotations.VisibleForTesting;
26
27/**
28 * Class for logging IpConnectvity events with IpConnectivityMetrics
29 * {@hide}
30 */
31public class IpConnectivityLog {
32    private static final String TAG = IpConnectivityLog.class.getSimpleName();
33    private static final boolean DBG = false;
34
35    public static final String SERVICE_NAME = "connmetrics";
36
37    private IIpConnectivityMetrics mService;
38
39    public IpConnectivityLog() {
40    }
41
42    @VisibleForTesting
43    public IpConnectivityLog(IIpConnectivityMetrics service) {
44        mService = service;
45    }
46
47    private boolean checkLoggerService() {
48        if (mService != null) {
49            return true;
50        }
51        final IIpConnectivityMetrics service =
52                IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME));
53        if (service == null) {
54            return false;
55        }
56        // Two threads racing here will write the same pointer because getService
57        // is idempotent once MetricsLoggerService is initialized.
58        mService = service;
59        return true;
60    }
61
62    /**
63     * Log a ConnectivityMetricsEvent.
64     * @param ev the event to log. If the event timestamp is 0,
65     * the timestamp is set to the current time in milliseconds.
66     * @return true if the event was successfully logged.
67     */
68    public boolean log(ConnectivityMetricsEvent ev) {
69        if (!checkLoggerService()) {
70            if (DBG) {
71                Log.d(TAG, SERVICE_NAME + " service was not ready");
72            }
73            return false;
74        }
75        if (ev.timestamp == 0) {
76            ev.timestamp = System.currentTimeMillis();
77        }
78        try {
79            int left = mService.logEvent(ev);
80            return left >= 0;
81        } catch (RemoteException e) {
82            Log.e(TAG, "Error logging event", e);
83            return false;
84        }
85    }
86
87    /**
88     * Log an IpConnectivity event.
89     * @param timestamp is the epoch timestamp of the event in ms.
90     * If the timestamp is 0, the timestamp is set to the current time in milliseconds.
91     * @param data is a Parcelable instance representing the event.
92     * @return true if the event was successfully logged.
93     */
94    public boolean log(long timestamp, Parcelable data) {
95        ConnectivityMetricsEvent ev = makeEv(data);
96        ev.timestamp = timestamp;
97        return log(ev);
98    }
99
100    /**
101     * Log an IpConnectivity event.
102     * @param data is a Parcelable instance representing the event.
103     * @return true if the event was successfully logged.
104     */
105    public boolean log(Parcelable data) {
106        return log(makeEv(data));
107    }
108
109    private static ConnectivityMetricsEvent makeEv(Parcelable data) {
110        ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
111        ev.data = data;
112        return ev;
113    }
114}
115