OSDN Git Service

b49cc2bffa16b5d9cdc32b72f7b82aa6fd7fd51c
[android-x86/frameworks-base.git] / core / java / android / net / ConnectivityMetricsLogger.java
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 package android.net;
17
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;
24
25 /** {@hide} */
26 @SystemApi
27 public class ConnectivityMetricsLogger {
28     private static String TAG = "ConnectivityMetricsLogger";
29     private static final boolean DBG = true;
30
31     public static final String CONNECTIVITY_METRICS_LOGGER_SERVICE = "connectivity_metrics_logger";
32
33     // Component Tags
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;
39
40     public static final int NUMBER_OF_COMPONENTS = 5;
41
42     // Event Tag
43     public static final int TAG_SKIPPED_EVENTS = -1;
44
45     public static final String DATA_KEY_EVENTS_COUNT = "count";
46
47     private IConnectivityMetricsLogger mService;
48
49     private long mServiceUnblockedTimestampMillis = 0;
50     private int mNumSkippedEvents = 0;
51
52     public ConnectivityMetricsLogger() {
53         mService = IConnectivityMetricsLogger.Stub.asInterface(ServiceManager.getService(
54                 CONNECTIVITY_METRICS_LOGGER_SERVICE));
55     }
56
57     public void logEvent(long timestamp, int componentTag, int eventTag, Parcelable data) {
58         if (mService == null) {
59             if (DBG) {
60                 Log.d(TAG, "logEvent(" + componentTag + "," + eventTag + ") Service not ready");
61             }
62             return;
63         }
64
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.
69                 mNumSkippedEvents++;
70                 return;
71             }
72         }
73
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);
81
82             mServiceUnblockedTimestampMillis = 0;
83         }
84
85         ConnectivityMetricsEvent event = new ConnectivityMetricsEvent(timestamp, componentTag,
86                 eventTag, data);
87
88         try {
89             long result;
90             if (skippedEventsEvent == null) {
91                 result = mService.logEvent(event);
92             } else {
93                 result = mService.logEvents(new ConnectivityMetricsEvent[]
94                         {skippedEventsEvent, event});
95             }
96
97             if (result == 0) {
98                 mNumSkippedEvents = 0;
99             } else {
100                 mNumSkippedEvents++;
101                 if (result > 0) { // events are throttled
102                     mServiceUnblockedTimestampMillis = result;
103                 }
104             }
105         } catch (RemoteException e) {
106             Log.e(TAG, "Error logging event " + e.getMessage());
107         }
108     }
109 }