1/*
2 * Copyright (C) 2008 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.voice;
18
19import com.android.common.speech.LoggingEvents;
20
21import android.content.Context;
22import android.content.Intent;
23
24/**
25 * Provides the logging facility for voice input events. This fires broadcasts back to
26 * the voice search app which then logs on our behalf.
27 *
28 * Note that debug console logging does not occur in this class. If you want to
29 * see console output of these logging events, there is a boolean switch to turn
30 * on on the VoiceSearch side.
31 */
32public class VoiceInputLogger {
33    private static final String TAG = VoiceInputLogger.class.getSimpleName();
34
35    private static VoiceInputLogger sVoiceInputLogger;
36
37    private final Context mContext;
38
39    // The base intent used to form all broadcast intents to the logger
40    // in VoiceSearch.
41    private final Intent mBaseIntent;
42
43    /**
44     * Returns the singleton of the logger.
45     *
46     * @param contextHint a hint context used when creating the logger instance.
47     * Ignored if the singleton instance already exists.
48     */
49    public static synchronized VoiceInputLogger getLogger(Context contextHint) {
50        if (sVoiceInputLogger == null) {
51            sVoiceInputLogger = new VoiceInputLogger(contextHint);
52        }
53        return sVoiceInputLogger;
54    }
55
56    public VoiceInputLogger(Context context) {
57        mContext = context;
58
59        mBaseIntent = new Intent(LoggingEvents.ACTION_LOG_EVENT);
60        mBaseIntent.putExtra(LoggingEvents.EXTRA_APP_NAME, LoggingEvents.VoiceIme.APP_NAME);
61    }
62
63    private Intent newLoggingBroadcast(int event) {
64        Intent i = new Intent(mBaseIntent);
65        i.putExtra(LoggingEvents.EXTRA_EVENT, event);
66        return i;
67    }
68
69    public void flush() {
70        Intent i = new Intent(mBaseIntent);
71        i.putExtra(LoggingEvents.EXTRA_FLUSH, true);
72        mContext.sendBroadcast(i);
73    }
74
75    public void keyboardWarningDialogShown() {
76        mContext.sendBroadcast(newLoggingBroadcast(
77                LoggingEvents.VoiceIme.KEYBOARD_WARNING_DIALOG_SHOWN));
78    }
79
80    public void keyboardWarningDialogDismissed() {
81        mContext.sendBroadcast(newLoggingBroadcast(
82                LoggingEvents.VoiceIme.KEYBOARD_WARNING_DIALOG_DISMISSED));
83    }
84
85    public void keyboardWarningDialogOk() {
86        mContext.sendBroadcast(newLoggingBroadcast(
87                LoggingEvents.VoiceIme.KEYBOARD_WARNING_DIALOG_OK));
88    }
89
90    public void keyboardWarningDialogCancel() {
91        mContext.sendBroadcast(newLoggingBroadcast(
92                LoggingEvents.VoiceIme.KEYBOARD_WARNING_DIALOG_CANCEL));
93    }
94
95    public void settingsWarningDialogShown() {
96        mContext.sendBroadcast(newLoggingBroadcast(
97                LoggingEvents.VoiceIme.SETTINGS_WARNING_DIALOG_SHOWN));
98    }
99
100    public void settingsWarningDialogDismissed() {
101        mContext.sendBroadcast(newLoggingBroadcast(
102                LoggingEvents.VoiceIme.SETTINGS_WARNING_DIALOG_DISMISSED));
103    }
104
105    public void settingsWarningDialogOk() {
106        mContext.sendBroadcast(newLoggingBroadcast(
107                LoggingEvents.VoiceIme.SETTINGS_WARNING_DIALOG_OK));
108    }
109
110    public void settingsWarningDialogCancel() {
111        mContext.sendBroadcast(newLoggingBroadcast(
112                LoggingEvents.VoiceIme.SETTINGS_WARNING_DIALOG_CANCEL));
113    }
114
115    public void swipeHintDisplayed() {
116        mContext.sendBroadcast(newLoggingBroadcast(LoggingEvents.VoiceIme.SWIPE_HINT_DISPLAYED));
117    }
118
119    public void cancelDuringListening() {
120        mContext.sendBroadcast(newLoggingBroadcast(LoggingEvents.VoiceIme.CANCEL_DURING_LISTENING));
121    }
122
123    public void cancelDuringWorking() {
124        mContext.sendBroadcast(newLoggingBroadcast(LoggingEvents.VoiceIme.CANCEL_DURING_WORKING));
125    }
126
127    public void cancelDuringError() {
128        mContext.sendBroadcast(newLoggingBroadcast(LoggingEvents.VoiceIme.CANCEL_DURING_ERROR));
129    }
130
131    public void punctuationHintDisplayed() {
132        mContext.sendBroadcast(newLoggingBroadcast(
133                LoggingEvents.VoiceIme.PUNCTUATION_HINT_DISPLAYED));
134    }
135
136    public void error(int code) {
137        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.ERROR);
138        i.putExtra(LoggingEvents.VoiceIme.EXTRA_ERROR_CODE, code);
139        mContext.sendBroadcast(i);
140    }
141
142    public void start(String locale, boolean swipe) {
143        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.START);
144        i.putExtra(LoggingEvents.VoiceIme.EXTRA_START_LOCALE, locale);
145        i.putExtra(LoggingEvents.VoiceIme.EXTRA_START_SWIPE, swipe);
146        i.putExtra(LoggingEvents.EXTRA_TIMESTAMP, System.currentTimeMillis());
147        mContext.sendBroadcast(i);
148    }
149
150    public void voiceInputDelivered(int length) {
151        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.VOICE_INPUT_DELIVERED);
152        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_LENGTH, length);
153        mContext.sendBroadcast(i);
154    }
155
156    public void textModifiedByTypingInsertion(int length) {
157        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.TEXT_MODIFIED);
158        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_LENGTH, length);
159        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_TYPE,
160                LoggingEvents.VoiceIme.TEXT_MODIFIED_TYPE_TYPING_INSERTION);
161        mContext.sendBroadcast(i);
162    }
163
164    public void textModifiedByTypingInsertionPunctuation(int length) {
165        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.TEXT_MODIFIED);
166        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_LENGTH, length);
167        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_TYPE,
168                LoggingEvents.VoiceIme.TEXT_MODIFIED_TYPE_TYPING_INSERTION_PUNCTUATION);
169        mContext.sendBroadcast(i);
170    }
171
172    public void textModifiedByTypingDeletion(int length) {
173        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.TEXT_MODIFIED);
174        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_LENGTH, length);
175        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_TYPE,
176                LoggingEvents.VoiceIme.TEXT_MODIFIED_TYPE_TYPING_DELETION);
177
178        mContext.sendBroadcast(i);
179    }
180
181    public void textModifiedByChooseSuggestion(int length) {
182        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.TEXT_MODIFIED);
183        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_LENGTH, length);
184        i.putExtra(LoggingEvents.VoiceIme.EXTRA_TEXT_MODIFIED_TYPE,
185                LoggingEvents.VoiceIme.TEXT_MODIFIED_TYPE_CHOOSE_SUGGESTION);
186        mContext.sendBroadcast(i);
187    }
188
189    public void nBestChoose(int index) {
190        Intent i = newLoggingBroadcast(LoggingEvents.VoiceIme.N_BEST_CHOOSE);
191        i.putExtra(LoggingEvents.VoiceIme.EXTRA_N_BEST_CHOOSE_INDEX, index);
192        mContext.sendBroadcast(i);
193    }
194
195    public void inputEnded() {
196        mContext.sendBroadcast(newLoggingBroadcast(LoggingEvents.VoiceIme.INPUT_ENDED));
197    }
198
199    public void voiceInputSettingEnabled() {
200        mContext.sendBroadcast(newLoggingBroadcast(
201                LoggingEvents.VoiceIme.VOICE_INPUT_SETTING_ENABLED));
202    }
203
204    public void voiceInputSettingDisabled() {
205        mContext.sendBroadcast(newLoggingBroadcast(
206                LoggingEvents.VoiceIme.VOICE_INPUT_SETTING_DISABLED));
207    }
208}
209