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    @Override
61    public void logStart(int onCreateLatency, int latency, String intentSource) {
62        // TODO: Add more info to startMethod
63        String startMethod = intentSource;
64        EventLogTags.writeQsbStart(mPackageName, getVersionCode(), startMethod,
65                latency, null, null, onCreateLatency);
66    }
67
68    @Override
69    public void logSuggestionClick(long id, SuggestionCursor suggestionCursor, int clickType) {
70        String suggestions = getSuggestions(suggestionCursor);
71        int numChars = suggestionCursor.getUserQuery().length();
72        EventLogTags.writeQsbClick(id, suggestions, null, numChars,
73                clickType);
74    }
75
76    @Override
77    public void logSearch(int startMethod, int numChars) {
78        EventLogTags.writeQsbSearch(null, startMethod, numChars);
79    }
80
81    @Override
82    public void logVoiceSearch() {
83        EventLogTags.writeQsbVoiceSearch(null);
84    }
85
86    @Override
87    public void logExit(SuggestionCursor suggestionCursor, int numChars) {
88        String suggestions = getSuggestions(suggestionCursor);
89        EventLogTags.writeQsbExit(suggestions, numChars);
90    }
91
92    @Override
93    public void logLatency(SourceResult result) {
94    }
95
96    private String getSuggestions(SuggestionCursor cursor) {
97        StringBuilder sb = new StringBuilder();
98        final int count = cursor == null ? 0 : cursor.getCount();
99        for (int i = 0; i < count; i++) {
100            if (i > 0) sb.append(LIST_SEPARATOR);
101            cursor.moveTo(i);
102            String source = cursor.getSuggestionSource().getName();
103            String type = cursor.getSuggestionLogType();
104            if (type == null) type = "";
105            String shortcut = cursor.isSuggestionShortcut() ? "shortcut" : "";
106            sb.append(source).append(':').append(type).append(':').append(shortcut);
107        }
108        return sb.toString();
109    }
110
111}
112