• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018 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 com.android.bluetooth.btservice;
17 
18 import com.android.bluetooth.BluetoothMetricsProto.BluetoothLog;
19 import com.android.bluetooth.BluetoothMetricsProto.ProfileConnectionStats;
20 import com.android.bluetooth.BluetoothMetricsProto.ProfileId;
21 
22 import java.util.HashMap;
23 
24 /**
25  * Class with static methods for logging metrics data
26  */
27 public class MetricsLogger {
28     private static final HashMap<ProfileId, Integer> sProfileConnectionCounts = new HashMap<>();
29 
30     /**
31      * Log profile connection event by incrementing an internal counter for that profile.
32      * This log persists over adapter enable/disable and only get cleared when metrics are
33      * dumped or when Bluetooth process is killed.
34      *
35      * @param profileId Bluetooth profile that is connected at this event
36      */
logProfileConnectionEvent(ProfileId profileId)37     public static void logProfileConnectionEvent(ProfileId profileId) {
38         synchronized (sProfileConnectionCounts) {
39             sProfileConnectionCounts.merge(profileId, 1, Integer::sum);
40         }
41     }
42 
43     /**
44      * Dump collected metrics into proto using a builder.
45      * Clean up internal data after the dump.
46      *
47      * @param metricsBuilder proto builder for {@link BluetoothLog}
48      */
dumpProto(BluetoothLog.Builder metricsBuilder)49     public static void dumpProto(BluetoothLog.Builder metricsBuilder) {
50         synchronized (sProfileConnectionCounts) {
51             sProfileConnectionCounts.forEach(
52                     (key, value) -> metricsBuilder.addProfileConnectionStats(
53                             ProfileConnectionStats.newBuilder()
54                                     .setProfileId(key)
55                                     .setNumTimesConnected(value)
56                                     .build()));
57             sProfileConnectionCounts.clear();
58         }
59     }
60 }
61