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 com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__ACTION_ENTRY;
20 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__AUTHENTICATION_ENTRY;
21 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__CREDENTIAL_ENTRY;
22 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__REMOTE_ENTRY;
23 import static com.android.internal.util.FrameworkStatsLog.CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__UNKNOWN;
24 import static com.android.server.credentials.ProviderGetSession.ACTION_ENTRY_KEY;
25 import static com.android.server.credentials.ProviderGetSession.AUTHENTICATION_ACTION_ENTRY_KEY;
26 import static com.android.server.credentials.ProviderGetSession.CREDENTIAL_ENTRY_KEY;
27 import static com.android.server.credentials.ProviderGetSession.REMOTE_ENTRY_KEY;
28 
29 import android.util.Slog;
30 
31 import com.android.server.credentials.ProviderCreateSession;
32 
33 import java.util.AbstractMap;
34 import java.util.Map;
35 
36 public enum EntryEnum {
37     UNKNOWN(CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__UNKNOWN),
38     ACTION_ENTRY(CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__ACTION_ENTRY),
39     CREDENTIAL_ENTRY(CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__CREDENTIAL_ENTRY),
40     REMOTE_ENTRY(CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__REMOTE_ENTRY),
41     AUTHENTICATION_ENTRY(
42             CREDENTIAL_MANAGER_FINAL_PHASE_REPORTED__CLICKED_ENTRIES__AUTHENTICATION_ENTRY
43     );
44 
45     private static final String TAG = "EntryEnum";
46 
47     private final int mInnerMetricCode;
48 
49     private static final Map<String, Integer> sKeyToEntryCode = Map.ofEntries(
50             new AbstractMap.SimpleEntry<>(ACTION_ENTRY_KEY,
51                     ACTION_ENTRY.mInnerMetricCode),
52             new AbstractMap.SimpleEntry<>(AUTHENTICATION_ACTION_ENTRY_KEY,
53                     AUTHENTICATION_ENTRY.mInnerMetricCode),
54             new AbstractMap.SimpleEntry<>(REMOTE_ENTRY_KEY,
55                     REMOTE_ENTRY.mInnerMetricCode),
56             new AbstractMap.SimpleEntry<>(CREDENTIAL_ENTRY_KEY,
57                     CREDENTIAL_ENTRY.mInnerMetricCode),
58             new AbstractMap.SimpleEntry<>(ProviderCreateSession.SAVE_ENTRY_KEY,
59                     CREDENTIAL_ENTRY.mInnerMetricCode)
60     );
61 
EntryEnum(int innerMetricCode)62     EntryEnum(int innerMetricCode) {
63         mInnerMetricCode = innerMetricCode;
64     }
65 
66     /**
67      * Gives the West-world version of the metric name.
68      *
69      * @return a code corresponding to the west world metric name
70      */
getMetricCode()71     public int getMetricCode() {
72         return mInnerMetricCode;
73     }
74 
75     /**
76      * Given a string key type known to the framework, this returns the known metric code associated
77      * with that string.
78      *
79      * @param stringKey a string key type for a particular entry
80      * @return the metric code associated with this enum
81      */
getMetricCodeFromString(String stringKey)82     public static int getMetricCodeFromString(String stringKey) {
83         if (!sKeyToEntryCode.containsKey(stringKey)) {
84             Slog.i(TAG, "Attempted to use an unsupported string key entry type");
85             return UNKNOWN.mInnerMetricCode;
86         }
87         return sKeyToEntryCode.get(stringKey);
88     }
89 }
90