Home | History | Annotate | Download | only in metrics
      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 
     17 package android.net.metrics;
     18 
     19 import android.net.ConnectivityMetricsEvent;
     20 import android.net.IIpConnectivityMetrics;
     21 import android.os.Parcelable;
     22 import android.os.RemoteException;
     23 import android.os.ServiceManager;
     24 import android.util.Log;
     25 import com.android.internal.annotations.VisibleForTesting;
     26 import com.android.internal.util.BitUtils;
     27 
     28 /**
     29  * Class for logging IpConnectvity events with IpConnectivityMetrics
     30  * {@hide}
     31  */
     32 public class IpConnectivityLog {
     33     private static final String TAG = IpConnectivityLog.class.getSimpleName();
     34     private static final boolean DBG = false;
     35 
     36     public static final String SERVICE_NAME = "connmetrics";
     37 
     38     private IIpConnectivityMetrics mService;
     39 
     40     public IpConnectivityLog() {
     41     }
     42 
     43     @VisibleForTesting
     44     public IpConnectivityLog(IIpConnectivityMetrics service) {
     45         mService = service;
     46     }
     47 
     48     private boolean checkLoggerService() {
     49         if (mService != null) {
     50             return true;
     51         }
     52         final IIpConnectivityMetrics service =
     53                 IIpConnectivityMetrics.Stub.asInterface(ServiceManager.getService(SERVICE_NAME));
     54         if (service == null) {
     55             return false;
     56         }
     57         // Two threads racing here will write the same pointer because getService
     58         // is idempotent once MetricsLoggerService is initialized.
     59         mService = service;
     60         return true;
     61     }
     62 
     63     /**
     64      * Log a ConnectivityMetricsEvent.
     65      * @param ev the event to log. If the event timestamp is 0,
     66      * the timestamp is set to the current time in milliseconds.
     67      * @return true if the event was successfully logged.
     68      */
     69     public boolean log(ConnectivityMetricsEvent ev) {
     70         if (!checkLoggerService()) {
     71             if (DBG) {
     72                 Log.d(TAG, SERVICE_NAME + " service was not ready");
     73             }
     74             return false;
     75         }
     76         if (ev.timestamp == 0) {
     77             ev.timestamp = System.currentTimeMillis();
     78         }
     79         try {
     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     /**
     89      * Log an IpConnectivity event.
     90      * @param timestamp is the epoch timestamp of the event in ms.
     91      * If the timestamp is 0, the timestamp is set to the current time in milliseconds.
     92      * @param data is a Parcelable instance representing the event.
     93      * @return true if the event was successfully logged.
     94      */
     95     public boolean log(long timestamp, Parcelable data) {
     96         ConnectivityMetricsEvent ev = makeEv(data);
     97         ev.timestamp = timestamp;
     98         return log(ev);
     99     }
    100 
    101     /**
    102      * Log an IpConnectivity event.
    103      * @param ifname the network interface associated with the event.
    104      * @param data is a Parcelable instance representing the event.
    105      * @return true if the event was successfully logged.
    106      */
    107     public boolean log(String ifname, Parcelable data) {
    108         ConnectivityMetricsEvent ev = makeEv(data);
    109         ev.ifname = ifname;
    110         return log(ev);
    111     }
    112 
    113     /**
    114      * Log an IpConnectivity event.
    115      * @param netid the id of the network associated with the event.
    116      * @param transports the current transports of the network associated with the event, as defined
    117      * in NetworkCapabilities.
    118      * @param data is a Parcelable instance representing the event.
    119      * @return true if the event was successfully logged.
    120      */
    121     public boolean log(int netid, int[] transports, Parcelable data) {
    122         ConnectivityMetricsEvent ev = makeEv(data);
    123         ev.netId = netid;
    124         ev.transports = BitUtils.packBits(transports);
    125         return log(ev);
    126     }
    127 
    128     /**
    129      * Log an IpConnectivity event.
    130      * @param data is a Parcelable instance representing the event.
    131      * @return true if the event was successfully logged.
    132      */
    133     public boolean log(Parcelable data) {
    134         return log(makeEv(data));
    135     }
    136 
    137     private static ConnectivityMetricsEvent makeEv(Parcelable data) {
    138         ConnectivityMetricsEvent ev = new ConnectivityMetricsEvent();
    139         ev.data = data;
    140         return ev;
    141     }
    142 }
    143