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.companion.utils;
18 
19 import static android.companion.AssociationRequest.DEVICE_PROFILE_APP_STREAMING;
20 import static android.companion.AssociationRequest.DEVICE_PROFILE_AUTOMOTIVE_PROJECTION;
21 import static android.companion.AssociationRequest.DEVICE_PROFILE_COMPUTER;
22 import static android.companion.AssociationRequest.DEVICE_PROFILE_GLASSES;
23 import static android.companion.AssociationRequest.DEVICE_PROFILE_NEARBY_DEVICE_STREAMING;
24 import static android.companion.AssociationRequest.DEVICE_PROFILE_WATCH;
25 
26 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION;
27 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__ACTION__CREATED;
28 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__ACTION__REMOVED;
29 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_APP_STREAMING;
30 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_AUTO_PROJECTION;
31 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_COMPUTER;
32 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_GLASSES;
33 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NEARBY_DEVICE_STREAMING;
34 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NULL;
35 import static com.android.internal.util.FrameworkStatsLog.CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_WATCH;
36 import static com.android.internal.util.FrameworkStatsLog.write;
37 
38 import static java.util.Collections.unmodifiableMap;
39 
40 import android.util.ArrayMap;
41 
42 import java.util.Map;
43 
44 public final class MetricUtils {
45 
46     private static final Map<String, Integer> METRIC_DEVICE_PROFILE;
47     static {
48         final Map<String, Integer> map = new ArrayMap<>();
map.put(null, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NULL)49         map.put(null, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NULL);
map.put( DEVICE_PROFILE_WATCH, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_WATCH )50         map.put(
51                 DEVICE_PROFILE_WATCH,
52                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_WATCH
53         );
map.put( DEVICE_PROFILE_APP_STREAMING, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_APP_STREAMING )54         map.put(
55                 DEVICE_PROFILE_APP_STREAMING,
56                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_APP_STREAMING
57         );
map.put( DEVICE_PROFILE_AUTOMOTIVE_PROJECTION, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_AUTO_PROJECTION )58         map.put(
59                 DEVICE_PROFILE_AUTOMOTIVE_PROJECTION,
60                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_AUTO_PROJECTION
61         );
map.put( DEVICE_PROFILE_COMPUTER, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_COMPUTER )62         map.put(
63                 DEVICE_PROFILE_COMPUTER,
64                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_COMPUTER
65         );
map.put( DEVICE_PROFILE_GLASSES, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_GLASSES )66         map.put(
67                 DEVICE_PROFILE_GLASSES,
68                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_GLASSES
69         );
map.put( DEVICE_PROFILE_NEARBY_DEVICE_STREAMING, CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NEARBY_DEVICE_STREAMING )70         map.put(
71                 DEVICE_PROFILE_NEARBY_DEVICE_STREAMING,
72                 CDM_ASSOCIATION_ACTION__DEVICE_PROFILE__DEVICE_PROFILE_NEARBY_DEVICE_STREAMING
73         );
74 
75         METRIC_DEVICE_PROFILE = unmodifiableMap(map);
76     }
77 
78     /**
79      * Log association creation
80      */
logCreateAssociation(String profile)81     public static void logCreateAssociation(String profile) {
82         write(CDM_ASSOCIATION_ACTION,
83                 CDM_ASSOCIATION_ACTION__ACTION__CREATED,
84                 METRIC_DEVICE_PROFILE.get(profile));
85     }
86 
87     /**
88      * Log association removal
89      */
logRemoveAssociation(String profile)90     public static void logRemoveAssociation(String profile) {
91         write(CDM_ASSOCIATION_ACTION,
92                 CDM_ASSOCIATION_ACTION__ACTION__REMOVED,
93                 METRIC_DEVICE_PROFILE.get(profile));
94     }
95 }
96