EventLogLogger.java revision f252dc7a25ba08b973ecc1cfbbce58eb78d42167
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.content.pm.PackageInfo; 21import android.content.pm.PackageManager; 22import android.text.TextUtils; 23import android.util.EventLog; 24import android.util.Log; 25 26import java.util.ArrayList; 27 28/** 29 * Logs events to {@link EventLog}. 30 */ 31public class EventLogLogger implements Logger { 32 33 private static final boolean DBG = true; 34 private static final String TAG = "QSB.EventLogLogger"; 35 36 private static final char LIST_SEPARATOR = '|'; 37 38 private final Context mContext; 39 40 private final int mVersionCode; 41 42 public EventLogLogger(Context context) { 43 mContext = context; 44 String pkgName = mContext.getPackageName(); 45 try { 46 PackageInfo pkgInfo = mContext.getPackageManager().getPackageInfo(pkgName, 0); 47 mVersionCode = pkgInfo.versionCode; 48 } catch (PackageManager.NameNotFoundException ex) { 49 // The current package should always exist, how else could we 50 // run code from it? 51 throw new RuntimeException(ex); 52 } 53 } 54 55 protected Context getContext() { 56 return mContext; 57 } 58 59 protected int getVersionCode() { 60 return mVersionCode; 61 } 62 63 public void logStart(int latency, String intentSource, Corpus corpus, 64 ArrayList<Corpus> orderedCorpora) { 65 String packageName = mContext.getPackageName(); 66 int version = mVersionCode; 67 // TODO: Add more info to startMethod 68 String startMethod = intentSource; 69 String currentCorpus = getCorpusLogName(corpus); 70 String enabledCorpora = getCorpusLogNames(orderedCorpora); 71 if (DBG) { 72 debug("qsb_start", packageName, version, startMethod, latency, 73 currentCorpus, enabledCorpora); 74 } 75 EventLogTags.writeQsbStart(packageName, version, startMethod, 76 latency, currentCorpus, enabledCorpora); 77 } 78 79 public void logSuggestionClick(int position, 80 SuggestionCursor suggestionCursor, ArrayList<Corpus> queriedCorpora) { 81 String suggestions = getSuggestions(suggestionCursor); 82 String corpora = getCorpusLogNames(queriedCorpora); 83 int numChars = suggestionCursor.getUserQuery().length(); 84 if (DBG) { 85 debug("qsb_click", position, suggestions, corpora, numChars); 86 } 87 EventLogTags.writeQsbClick(position, suggestions, corpora, numChars); 88 } 89 90 public void logSearch(Corpus corpus, int startMethod, int numChars) { 91 String corpusName = getCorpusLogName(corpus); 92 EventLogTags.writeQsbSearch(corpusName, startMethod, numChars); 93 } 94 95 public void logVoiceSearch(Corpus corpus) { 96 String corpusName = getCorpusLogName(corpus); 97 EventLogTags.writeQsbVoiceSearch(corpusName); 98 } 99 100 public void logExit(SuggestionCursor suggestionCursor, int numChars) { 101 String suggestions = getSuggestions(suggestionCursor); 102 EventLogTags.writeQsbExit(suggestions, numChars); 103 } 104 105 public void logWebLatency() { 106 107 } 108 109 private String getCorpusLogName(Corpus corpus) { 110 if (corpus == null) return null; 111 return corpus.getName(); 112 } 113 114 private String getSuggestions(SuggestionCursor cursor) { 115 StringBuilder sb = new StringBuilder(); 116 final int count = cursor.getCount(); 117 for (int i = 0; i < count; i++) { 118 if (i > 0) sb.append(LIST_SEPARATOR); 119 cursor.moveTo(i); 120 String source = cursor.getSuggestionSource().getName(); 121 String type = cursor.getSuggestionLogType(); 122 if (type == null) type = ""; 123 String shortcut = cursor.isSuggestionShortcut() ? "shortcut" : ""; 124 sb.append(source).append(':').append(type).append(':').append(shortcut); 125 } 126 return sb.toString(); 127 } 128 129 private String getCorpusLogNames(ArrayList<Corpus> corpora) { 130 StringBuilder sb = new StringBuilder(); 131 final int count = corpora.size(); 132 for (int i = 0; i < count; i++) { 133 if (i > 0) sb.append(LIST_SEPARATOR); 134 sb.append(getCorpusLogName(corpora.get(i))); 135 } 136 return sb.toString(); 137 } 138 139 private void debug(String tag, Object... args) { 140 Log.d(TAG, tag + "(" + TextUtils.join(",", args) + ")"); 141 } 142} 143