1/*
2 * Copyright (C) 2009 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
17package com.android.quicksearchbox;
18
19import android.content.Context;
20import android.util.EventLog;
21
22import java.util.Collection;
23import java.util.List;
24import java.util.Random;
25
26/**
27 * Logs events to {@link EventLog}.
28 */
29public class EventLogLogger implements Logger {
30
31    private static final char LIST_SEPARATOR = '|';
32
33    private final Context mContext;
34
35    private final Config mConfig;
36
37    private final String mPackageName;
38
39    private final Random mRandom;
40
41    public EventLogLogger(Context context, Config config) {
42        mContext = context;
43        mConfig = config;
44        mPackageName = mContext.getPackageName();
45        mRandom = new Random();
46    }
47
48    protected Context getContext() {
49        return mContext;
50    }
51
52    protected int getVersionCode() {
53        return QsbApplication.get(getContext()).getVersionCode();
54    }
55
56    protected Config getConfig() {
57        return mConfig;
58    }
59
60    public void logStart(int latency, String intentSource, Corpus corpus,
61            List<Corpus> orderedCorpora) {
62        // TODO: Add more info to startMethod
63        String startMethod = intentSource;
64        String currentCorpus = getCorpusLogName(corpus);
65        String enabledCorpora = getCorpusLogNames(orderedCorpora);
66        EventLogTags.writeQsbStart(mPackageName, getVersionCode(), startMethod,
67                latency, currentCorpus, enabledCorpora);
68    }
69
70    public void logSuggestionClick(int position, SuggestionCursor suggestionCursor,
71            Collection<Corpus> queriedCorpora, int clickType) {
72        String suggestions = getSuggestions(suggestionCursor);
73        String corpora = getCorpusLogNames(queriedCorpora);
74        int numChars = suggestionCursor.getUserQuery().length();
75        EventLogTags.writeQsbClick(position, suggestions, corpora, numChars,
76                clickType);
77    }
78
79    public void logSearch(Corpus corpus, int startMethod, int numChars) {
80        String corpusName = getCorpusLogName(corpus);
81        EventLogTags.writeQsbSearch(corpusName, startMethod, numChars);
82    }
83
84    public void logVoiceSearch(Corpus corpus) {
85        String corpusName = getCorpusLogName(corpus);
86        EventLogTags.writeQsbVoiceSearch(corpusName);
87    }
88
89    public void logExit(SuggestionCursor suggestionCursor, int numChars) {
90        String suggestions = getSuggestions(suggestionCursor);
91        EventLogTags.writeQsbExit(suggestions, numChars);
92    }
93
94    public void logLatency(CorpusResult result) {
95        if (!shouldLogLatency()) return;
96        String corpusName = getCorpusLogName(result.getCorpus());
97        int latency = result.getLatency();
98        int numChars = result.getUserQuery().length();
99        EventLogTags.writeQsbLatency(corpusName, latency, numChars);
100    }
101
102    private boolean shouldLogLatency() {
103        int freq = mConfig.getLatencyLogFrequency();
104        return freq > mRandom.nextInt(1000);
105    }
106
107    private String getCorpusLogName(Corpus corpus) {
108        if (corpus == null) return null;
109        return corpus.getName();
110    }
111
112    private String getSuggestions(SuggestionCursor cursor) {
113        StringBuilder sb = new StringBuilder();
114        final int count = cursor == null ? 0 : cursor.getCount();
115        for (int i = 0; i < count; i++) {
116            if (i > 0) sb.append(LIST_SEPARATOR);
117            cursor.moveTo(i);
118            String source = cursor.getSuggestionSource().getName();
119            String type = cursor.getSuggestionLogType();
120            if (type == null) type = "";
121            String shortcut = cursor.isSuggestionShortcut() ? "shortcut" : "";
122            sb.append(source).append(':').append(type).append(':').append(shortcut);
123        }
124        return sb.toString();
125    }
126
127    private String getCorpusLogNames(Collection<Corpus> corpora) {
128        StringBuilder sb = new StringBuilder();
129        for (Corpus corpus : corpora) {
130            if (sb.length() > 0) sb.append(LIST_SEPARATOR);
131            sb.append(getCorpusLogName(corpus));
132        }
133        return sb.toString();
134    }
135
136}
137