2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 import android.annotation.SystemApi;
19 import android.os.Bundle;
20 import android.os.Parcelable;
21 import android.os.RemoteException;
22 import android.os.ServiceManager;
23 import android.util.Log;
27 public class ConnectivityMetricsLogger {
28 private static String TAG = "ConnectivityMetricsLogger";
29 private static final boolean DBG = true;
31 public static final String CONNECTIVITY_METRICS_LOGGER_SERVICE = "connectivity_metrics_logger";
34 public static final int COMPONENT_TAG_CONNECTIVITY = 0;
35 public static final int COMPONENT_TAG_BLUETOOTH = 1;
36 public static final int COMPONENT_TAG_WIFI = 2;
37 public static final int COMPONENT_TAG_TELECOM = 3;
38 public static final int COMPONENT_TAG_TELEPHONY = 4;
40 public static final int NUMBER_OF_COMPONENTS = 5;
43 public static final int TAG_SKIPPED_EVENTS = -1;
45 public static final String DATA_KEY_EVENTS_COUNT = "count";
47 private IConnectivityMetricsLogger mService;
49 private long mServiceUnblockedTimestampMillis = 0;
50 private int mNumSkippedEvents = 0;
52 public ConnectivityMetricsLogger() {
53 mService = IConnectivityMetricsLogger.Stub.asInterface(ServiceManager.getService(
54 CONNECTIVITY_METRICS_LOGGER_SERVICE));
57 public void logEvent(long timestamp, int componentTag, int eventTag, Parcelable data) {
58 if (mService == null) {
60 Log.d(TAG, "logEvent(" + componentTag + "," + eventTag + ") Service not ready");
65 if (mServiceUnblockedTimestampMillis > 0) {
66 if (System.currentTimeMillis() < mServiceUnblockedTimestampMillis) {
67 // Service is throttling events.
68 // Don't send new events because they will be dropped.
74 ConnectivityMetricsEvent skippedEventsEvent = null;
75 if (mNumSkippedEvents > 0) {
76 // Log number of skipped events
77 Bundle b = new Bundle();
78 b.putInt(DATA_KEY_EVENTS_COUNT, mNumSkippedEvents);
79 skippedEventsEvent = new ConnectivityMetricsEvent(mServiceUnblockedTimestampMillis,
80 componentTag, TAG_SKIPPED_EVENTS, b);
82 mServiceUnblockedTimestampMillis = 0;
85 ConnectivityMetricsEvent event = new ConnectivityMetricsEvent(timestamp, componentTag,
90 if (skippedEventsEvent == null) {
91 result = mService.logEvent(event);
93 result = mService.logEvents(new ConnectivityMetricsEvent[]
94 {skippedEventsEvent, event});
98 mNumSkippedEvents = 0;
101 if (result > 0) { // events are throttled
102 mServiceUnblockedTimestampMillis = result;
105 } catch (RemoteException e) {
106 Log.e(TAG, "Error logging event " + e.getMessage());