1 /* 2 * Copyright (C) 2022 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 package com.android.quicksearchbox 17 18 import android.content.Context 19 import android.util.EventLog 20 import java.util.Random 21 import kotlin.text.StringBuilder 22 23 /** Logs events to [EventLog]. */ 24 class EventLogLogger(context: Context?, config: Config) : Logger { 25 private val mContext: Context? 26 protected val config: Config 27 private val mPackageName: String 28 private val mRandom: Random 29 protected val context: Context? 30 get() = mContext 31 protected val versionCode: Long 32 get() = QsbApplication.get(context).versionCode 33 34 @Override logStartnull35 override fun logStart(onCreateLatency: Int, latency: Int, intentSource: String?) { 36 // TODO: Add more info to startMethod 37 EventLogTags.writeQsbStart( 38 mPackageName, 39 versionCode.toInt(), 40 intentSource, 41 latency, 42 null, 43 null, 44 onCreateLatency 45 ) 46 } 47 48 @Override logSuggestionClicknull49 override fun logSuggestionClick( 50 suggestionId: Long, 51 suggestionCursor: SuggestionCursor?, 52 clickType: Int 53 ) { 54 val suggestions = getSuggestions(suggestionCursor) 55 val numChars: Int = suggestionCursor!!.userQuery!!.length 56 EventLogTags.writeQsbClick(suggestionId, suggestions, null, numChars, clickType) 57 } 58 59 @Override logSearchnull60 override fun logSearch(startMethod: Int, numChars: Int) { 61 EventLogTags.writeQsbSearch(null, startMethod, numChars) 62 } 63 64 @Override logVoiceSearchnull65 override fun logVoiceSearch() { 66 EventLogTags.writeQsbVoiceSearch(null) 67 } 68 69 @Override logExitnull70 override fun logExit(suggestionCursor: SuggestionCursor?, numChars: Int) { 71 val suggestions = getSuggestions(suggestionCursor) 72 EventLogTags.writeQsbExit(suggestions, numChars) 73 } 74 logLatencynull75 @Override override fun logLatency(result: SourceResult?) {} 76 getSuggestionsnull77 private fun getSuggestions(cursor: SuggestionCursor?): String { 78 val sb = StringBuilder() 79 val count = cursor?.count ?: 0 80 for (i in 0 until count) { 81 if (i > 0) sb.append(LIST_SEPARATOR) 82 cursor!!.moveTo(i) 83 val source: String? = cursor.suggestionSource?.name 84 var type: String? = cursor.suggestionLogType 85 if (type == null) type = "" 86 val shortcut = if (cursor.isSuggestionShortcut) "shortcut" else "" 87 sb.append(source).append(":").append(type).append(":").append(shortcut) 88 } 89 return sb.toString() 90 } 91 92 companion object { 93 private const val LIST_SEPARATOR = '|' 94 } 95 96 init { 97 mContext = context 98 this.config = config 99 mPackageName = mContext!!.getPackageName() 100 mRandom = Random() 101 } 102 } 103