1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.android.inputmethod.latin;
import android.content.SharedPreferences;
import android.inputmethodservice.InputMethodService;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import com.android.inputmethod.keyboard.Keyboard;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Logs the use of the LatinIME keyboard.
*
* This class logs operations on the IME keyboard, including what the user has typed.
* Data is stored locally in a file in app-specific storage.
*
* This functionality is off by default. See {@link ProductionFlag.IS_EXPERIMENTAL}.
*/
public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = ResearchLogger.class.getSimpleName();
private static final String PREF_USABILITY_STUDY_MODE = "usability_study_mode";
private static final ResearchLogger sInstance = new ResearchLogger(new LogFileManager());
public static boolean sIsLogging = false;
/* package */ final Handler mLoggingHandler;
private InputMethodService mIms;
private final Date mDate;
private final SimpleDateFormat mDateFormat;
/**
* Isolates management of files. This variable should never be null, but can be changed
* to support testing.
*/
private LogFileManager mLogFileManager;
/**
* Manages the file(s) that stores the logs.
*
* Handles creation, deletion, and provides Readers, Writers, and InputStreams to access
* the logs.
*/
public static class LogFileManager {
private static final String DEFAULT_FILENAME = "log.txt";
private static final String DEFAULT_LOG_DIRECTORY = "researchLogger";
private static final long LOGFILE_PURGE_INTERVAL = 1000 * 60 * 60 * 24;
private InputMethodService mIms;
private File mFile;
private PrintWriter mPrintWriter;
/* package */ LogFileManager() {
}
public void init(InputMethodService ims) {
mIms = ims;
}
public synchronized void createLogFile() {
try {
createLogFile(DEFAULT_LOG_DIRECTORY, DEFAULT_FILENAME);
} catch (FileNotFoundException e) {
Log.w(TAG, e);
}
}
public synchronized void createLogFile(String dir, String filename)
throws FileNotFoundException {
if (mIms == null) {
Log.w(TAG, "InputMethodService is not configured. Logging is off.");
return;
}
File filesDir = mIms.getFilesDir();
if (filesDir == null || !filesDir.exists()) {
Log.w(TAG, "Storage directory does not exist. Logging is off.");
return;
}
File directory = new File(filesDir, dir);
if (!directory.exists()) {
boolean wasCreated = directory.mkdirs();
if (!wasCreated) {
Log.w(TAG, "Log directory cannot be created. Logging is off.");
return;
}
}
close();
mFile = new File(directory, filename);
boolean append = true;
if (mFile.exists() && mFile.lastModified() + LOGFILE_PURGE_INTERVAL <
System.currentTimeMillis()) {
append = false;
}
mPrintWriter = new PrintWriter(new FileOutputStream(mFile, append), true);
}
public synchronized boolean append(String s) {
if (mPrintWriter == null) {
Log.w(TAG, "PrintWriter is null");
return false;
} else {
mPrintWriter.print(s);
return !mPrintWriter.checkError();
}
}
public synchronized void reset() {
if (mPrintWriter != null) {
mPrintWriter.close();
mPrintWriter = null;
}
if (mFile != null && mFile.exists()) {
mFile.delete();
mFile = null;
}
}
public synchronized void close() {
if (mPrintWriter != null) {
mPrintWriter.close();
mPrintWriter = null;
mFile = null;
}
}
}
private ResearchLogger(LogFileManager logFileManager) {
mDate = new Date();
mDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss.SSSZ");
HandlerThread handlerThread = new HandlerThread("ResearchLogger logging task",
Process.THREAD_PRIORITY_BACKGROUND);
handlerThread.start();
mLoggingHandler = new Handler(handlerThread.getLooper());
mLogFileManager = logFileManager;
}
public static ResearchLogger getInstance() {
return sInstance;
}
public static void init(InputMethodService ims, SharedPreferences prefs) {
sInstance.initInternal(ims, prefs);
}
public void initInternal(InputMethodService ims, SharedPreferences prefs) {
mIms = ims;
if (mLogFileManager != null) {
mLogFileManager.init(ims);
mLogFileManager.createLogFile();
}
if (prefs != null) {
sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false);
}
prefs.registerOnSharedPreferenceChangeListener(this);
}
/**
* Change to a different logFileManager.
*
* @throws IllegalArgumentException if logFileManager is null
*/
void setLogFileManager(LogFileManager manager) {
if (manager == null) {
throw new IllegalArgumentException("warning: trying to set null logFileManager");
} else {
mLogFileManager = manager;
}
}
/**
* Represents a category of logging events that share the same subfield structure.
*/
private static enum LogGroup {
MOTION_EVENT("m"),
KEY("k"),
CORRECTION("c"),
STATE_CHANGE("s");
private final String mLogString;
private LogGroup(String logString) {
mLogString = logString;
}
}
public void logMotionEvent(final int action, final long eventTime, final int id,
final int x, final int y, final float size, final float pressure) {
final String eventTag;
switch (action) {
case MotionEvent.ACTION_CANCEL: eventTag = "[Cancel]"; break;
case MotionEvent.ACTION_UP: eventTag = "[Up]"; break;
case MotionEvent.ACTION_DOWN: eventTag = "[Down]"; break;
case MotionEvent.ACTION_POINTER_UP: eventTag = "[PointerUp]"; break;
case MotionEvent.ACTION_POINTER_DOWN: eventTag = "[PointerDown]"; break;
case MotionEvent.ACTION_MOVE: eventTag = "[Move]"; break;
case MotionEvent.ACTION_OUTSIDE: eventTag = "[Outside]"; break;
default: eventTag = "[Action" + action + "]"; break;
}
if (!TextUtils.isEmpty(eventTag)) {
StringBuilder sb = new StringBuilder();
sb.append(eventTag);
sb.append('\t'); sb.append(eventTime);
sb.append('\t'); sb.append(id);
sb.append('\t'); sb.append(x);
sb.append('\t'); sb.append(y);
sb.append('\t'); sb.append(size);
sb.append('\t'); sb.append(pressure);
write(LogGroup.MOTION_EVENT, sb.toString());
}
}
public void logKeyEvent(int code, int x, int y) {
final StringBuilder sb = new StringBuilder();
sb.append(Keyboard.printableCode(code));
sb.append('\t'); sb.append(x);
sb.append('\t'); sb.append(y);
write(LogGroup.KEY, sb.toString());
}
public void logCorrection(String subgroup, String before, String after, int position) {
final StringBuilder sb = new StringBuilder();
sb.append(subgroup);
sb.append('\t'); sb.append(before);
sb.append('\t'); sb.append(after);
sb.append('\t'); sb.append(position);
write(LogGroup.CORRECTION, sb.toString());
}
public void logStateChange(String subgroup, String details) {
write(LogGroup.STATE_CHANGE, subgroup + "\t" + details);
}
private void write(final LogGroup logGroup, final String log) {
mLoggingHandler.post(new Runnable() {
@Override
public void run() {
final long currentTime = System.currentTimeMillis();
mDate.setTime(currentTime);
final long upTime = SystemClock.uptimeMillis();
final String printString = String.format("%s\t%d\t%s\t%s\n",
mDateFormat.format(mDate), upTime, logGroup.mLogString, log);
if (LatinImeLogger.sDBG) {
Log.d(TAG, "Write: " + '[' + logGroup.mLogString + ']' + log);
}
if (mLogFileManager.append(printString)) {
// success
} else {
if (LatinImeLogger.sDBG) {
Log.w(TAG, "Unable to write to log.");
}
}
}
});
}
public void clearAll() {
mLoggingHandler.post(new Runnable() {
@Override
public void run() {
if (LatinImeLogger.sDBG) {
Log.d(TAG, "Delete log file.");
}
mLogFileManager.reset();
}
});
}
@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
if (key == null || prefs == null) {
return;
}
sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false);
}
}
|