IpConnectivityLog.java revision ec27c4d9f33615be1f94d6bb5c5fd1358580ac05
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 an IpConnectivity event.
64     * @param timestamp is the epoch timestamp of the event in ms.
65     * @param data is a Parcelable instance representing the event.
66     * @return true if the event was successfully logged.
67     */
68    public boolean log(long timestamp, Parcelable data) {
69        if (!checkLoggerService()) {
70            if (DBG) {
71                Log.d(TAG, SERVICE_NAME + " service was not ready");
72            }
73            return false;
74        }
75
76        try {
77            ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
78            ev.timestamp = timestamp;
79            ev.data = data;
80            int left = mService.logEvent(ev);
81            return left >= 0;
82        } catch (RemoteException e) {
83            Log.e(TAG, "Error logging event", e);
84            return false;
85        }
86    }
87
88    public void log(Parcelable event) {
89        log(System.currentTimeMillis(), event);
90    }
91}
92