1 /*
2  * Copyright (C) 2022 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 com.android.server.nearby.metrics;
18 
19 import android.nearby.NearbyDeviceParcelable;
20 import android.nearby.ScanRequest;
21 import android.os.WorkSource;
22 
23 import com.android.server.nearby.proto.NearbyStatsLog;
24 
25 /**
26  * A class to collect and report Nearby metrics.
27  */
28 public class NearbyMetrics {
29     /**
30      * Logs a scan started event.
31      */
logScanStarted(int scanSessionId, ScanRequest scanRequest)32     public static void logScanStarted(int scanSessionId, ScanRequest scanRequest) {
33         NearbyStatsLog.write(
34                 NearbyStatsLog.NEARBY_DEVICE_SCAN_STATE_CHANGED,
35                 getUid(scanRequest),
36                 scanSessionId,
37                 NearbyStatsLog
38                         .NEARBY_DEVICE_SCAN_STATE_CHANGED__SCAN_STATE__NEARBY_SCAN_STATE_STARTED,
39                 scanRequest.getScanType(),
40                 0,
41                 0,
42                 "",
43                 "");
44     }
45 
46     /**
47      * Logs a scan stopped event.
48      */
logScanStopped(int scanSessionId, ScanRequest scanRequest)49     public static void logScanStopped(int scanSessionId, ScanRequest scanRequest) {
50         NearbyStatsLog.write(
51                 NearbyStatsLog.NEARBY_DEVICE_SCAN_STATE_CHANGED,
52                 getUid(scanRequest),
53                 scanSessionId,
54                 NearbyStatsLog
55                         .NEARBY_DEVICE_SCAN_STATE_CHANGED__SCAN_STATE__NEARBY_SCAN_STATE_STOPPED,
56                 scanRequest.getScanType(),
57                 0,
58                 0,
59                 "",
60                 "");
61     }
62 
63     /**
64      * Logs a scan device discovered event.
65      */
logScanDeviceDiscovered(int scanSessionId, ScanRequest scanRequest, NearbyDeviceParcelable nearbyDevice)66     public static void logScanDeviceDiscovered(int scanSessionId, ScanRequest scanRequest,
67             NearbyDeviceParcelable nearbyDevice) {
68         NearbyStatsLog.write(
69                 NearbyStatsLog.NEARBY_DEVICE_SCAN_STATE_CHANGED,
70                 getUid(scanRequest),
71                 scanSessionId,
72                 NearbyStatsLog
73                         .NEARBY_DEVICE_SCAN_STATE_CHANGED__SCAN_STATE__NEARBY_SCAN_STATE_DISCOVERED,
74                 scanRequest.getScanType(),
75                 nearbyDevice.getMedium(),
76                 nearbyDevice.getRssi(),
77                 nearbyDevice.getFastPairModelId(),
78                 "");
79     }
80 
getUid(ScanRequest scanRequest)81     private static int getUid(ScanRequest scanRequest) {
82         WorkSource workSource = scanRequest.getWorkSource();
83         return workSource.isEmpty() ? -1 : workSource.getUid(0);
84     }
85 }
86