1 /*
2  * Copyright (C) 2017 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.settings.intelligence.instrumentation;
18 
19 import android.content.Context;
20 import android.text.TextUtils;
21 
22 import com.android.settings.intelligence.nano.SettingsIntelligenceLogProto
23         .SettingsIntelligenceEvent;
24 import com.android.settings.intelligence.search.SearchResult;
25 
26 import java.util.ArrayList;
27 import java.util.List;
28 
29 public class MetricsFeatureProvider {
30 
31     protected Context mContext;
32     protected List<EventLogger> mLoggers;
33 
MetricsFeatureProvider(Context context)34     public MetricsFeatureProvider(Context context) {
35         mContext = context;
36         mLoggers = new ArrayList<>();
37         mLoggers.add(new LocalEventLogger());
38     }
39 
logGetSuggestion(List<String> ids, long latency)40     public void logGetSuggestion(List<String> ids, long latency) {
41         final SettingsIntelligenceEvent event = new SettingsIntelligenceEvent();
42         event.eventType = SettingsIntelligenceEvent.GET_SUGGESTION;
43         event.latencyMillis = latency;
44         if (ids != null) {
45             event.suggestionIds = ids.toArray(new String[0]);
46         }
47         logEvent(event);
48     }
49 
logDismissSuggestion(String id, long latency)50     public void logDismissSuggestion(String id, long latency) {
51         final SettingsIntelligenceEvent event = new SettingsIntelligenceEvent();
52         event.eventType = SettingsIntelligenceEvent.DISMISS_SUGGESTION;
53         event.latencyMillis = latency;
54         if (!TextUtils.isEmpty(id)) {
55             event.suggestionIds = new String[]{id};
56         }
57         logEvent(event);
58     }
59 
logLaunchSuggestion(String id, long latency)60     public void logLaunchSuggestion(String id, long latency) {
61         final SettingsIntelligenceEvent event = new SettingsIntelligenceEvent();
62         event.eventType = SettingsIntelligenceEvent.LAUNCH_SUGGESTION;
63         event.latencyMillis = latency;
64         if (!TextUtils.isEmpty(id)) {
65             event.suggestionIds = new String[]{id};
66         }
67         logEvent(event);
68     }
69 
logSearchResultClick(SearchResult result, String query, int type, int count, int rank)70     public void logSearchResultClick(SearchResult result, String query, int type, int count,
71             int rank) {
72         final SettingsIntelligenceEvent event = new SettingsIntelligenceEvent();
73         event.eventType = type;
74         event.searchResultMetadata = new SettingsIntelligenceEvent.SearchResultMetadata();
75         event.searchResultMetadata.resultCount = count;
76         event.searchResultMetadata.searchResultRank = rank;
77         event.searchResultMetadata.searchResultKey = result.dataKey != null ? result.dataKey : "";
78         event.searchResultMetadata.searchQueryLength = query != null ? query.length() : 0;
79         logEvent(event);
80     }
81 
logEvent(int eventType)82     public void logEvent(int eventType) {
83         logEvent(eventType, 0 /* latency */);
84     }
85 
logEvent(int eventType, long latency)86     public void logEvent(int eventType, long latency) {
87         final SettingsIntelligenceEvent event = new SettingsIntelligenceEvent();
88         event.eventType = eventType;
89         event.latencyMillis = latency;
90         logEvent(event);
91     }
92 
logEvent(SettingsIntelligenceEvent event)93     private void logEvent(SettingsIntelligenceEvent event) {
94         for (EventLogger logger : mLoggers) {
95             logger.log(event);
96         }
97     }
98 }
99