1 /*
2  * Copyright (C) 2023 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.credentials.metrics;
18 
19 import static android.credentials.selection.RequestInfo.TYPE_CREATE;
20 import static android.credentials.selection.RequestInfo.TYPE_GET;
21 import static android.credentials.selection.RequestInfo.TYPE_GET_VIA_REGISTRY;
22 import static android.credentials.selection.RequestInfo.TYPE_UNDEFINED;
23 
24 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_CLEAR_CREDENTIAL;
25 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_CREATE_CREDENTIAL;
26 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL;
27 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL_PROVIDER_SERVICES;
28 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL_VIA_REGISTRY;
29 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_IS_ENABLED_CREDENTIAL_PROVIDER_SERVICE;
30 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_REGISTER_CREDENTIAL_DESCRIPTION;
31 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_SET_ENABLED_PROVIDERS;
32 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_UNKNOWN;
33 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_UNREGISTER_CREDENTIAL_DESCRIPTION;
34 
35 import android.credentials.selection.RequestInfo;
36 import android.util.Slog;
37 
38 import java.util.AbstractMap;
39 import java.util.Map;
40 
41 public enum ApiName {
42     UNKNOWN(CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_UNKNOWN),
43     GET_CREDENTIAL(CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL),
44     GET_CREDENTIAL_VIA_REGISTRY(
45 CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL_VIA_REGISTRY),
46     CREATE_CREDENTIAL(
47             CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_CREATE_CREDENTIAL),
48     CLEAR_CREDENTIAL(
49             CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_CLEAR_CREDENTIAL),
50     IS_ENABLED_CREDENTIAL_PROVIDER_SERVICE(
51 CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_IS_ENABLED_CREDENTIAL_PROVIDER_SERVICE
52     ),
53 
54     SET_ENABLED_PROVIDERS(
55             CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_SET_ENABLED_PROVIDERS),
56 
57     GET_CREDENTIAL_PROVIDER_SERVICES(
58     CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_GET_CREDENTIAL_PROVIDER_SERVICES),
59 
60     REGISTER_CREDENTIAL_DESCRIPTION(
61     CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_REGISTER_CREDENTIAL_DESCRIPTION
62     ),
63 
64     UNREGISTER_CREDENTIAL_DESCRIPTION(
65     CREDENTIAL_MANAGER_INITIAL_PHASE_REPORTED__API_NAME__API_NAME_UNREGISTER_CREDENTIAL_DESCRIPTION
66     );
67 
68     private static final String TAG = "ApiName";
69 
70     private final int mInnerMetricCode;
71 
72     private static final Map<String, Integer> sRequestInfoToMetric = Map.ofEntries(
73             new AbstractMap.SimpleEntry<>(TYPE_CREATE,
74                     CREATE_CREDENTIAL.mInnerMetricCode),
75             new AbstractMap.SimpleEntry<>(TYPE_GET,
76                     GET_CREDENTIAL.mInnerMetricCode),
77             new AbstractMap.SimpleEntry<>(TYPE_GET_VIA_REGISTRY,
78                     GET_CREDENTIAL_VIA_REGISTRY.mInnerMetricCode),
79             new AbstractMap.SimpleEntry<>(TYPE_UNDEFINED,
80                     CLEAR_CREDENTIAL.mInnerMetricCode)
81     );
82 
ApiName(int innerMetricCode)83     ApiName(int innerMetricCode) {
84         mInnerMetricCode = innerMetricCode;
85     }
86 
87     /**
88      * Gives the West-world version of the metric name.
89      *
90      * @return a code corresponding to the west world metric name
91      */
getMetricCode()92     public int getMetricCode() {
93         return mInnerMetricCode;
94     }
95 
96     /**
97      * Given a string key type known to the framework, this returns the known metric code associated
98      * with that string. This is mainly used by {@link RequestSessionMetric} collection contexts.
99      * This relies on {@link RequestInfo} string keys.
100      *
101      * @param stringKey a string key type for a particular request info
102      * @return the metric code associated with this request info's api name counterpart
103      */
getMetricCodeFromRequestInfo(String stringKey)104     public static int getMetricCodeFromRequestInfo(String stringKey) {
105         if (!sRequestInfoToMetric.containsKey(stringKey)) {
106             Slog.i(TAG, "Attempted to use an unsupported string key request info");
107             return UNKNOWN.mInnerMetricCode;
108         }
109         return sRequestInfoToMetric.get(stringKey);
110     }
111 }
112