2012-06-29 14:02:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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
|
2012-06-29 14:02:39 +00:00
|
|
|
*
|
2013-01-21 12:52:57 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-06-29 14:02:39 +00:00
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
2013-01-21 12:52:57 +00:00
|
|
|
* 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.
|
2012-06-29 14:02:39 +00:00
|
|
|
*/
|
|
|
|
|
2012-07-20 18:02:39 +00:00
|
|
|
package com.android.inputmethod.research;
|
2012-06-29 14:02:39 +00:00
|
|
|
|
2013-01-11 18:35:51 +00:00
|
|
|
import android.content.Context;
|
2012-06-29 14:02:39 +00:00
|
|
|
import android.util.JsonWriter;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import com.android.inputmethod.latin.define.ProductionFlag;
|
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
2013-01-11 18:35:51 +00:00
|
|
|
import java.io.FileOutputStream;
|
2012-06-29 14:02:39 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
import java.util.concurrent.Executors;
|
2012-08-03 03:22:29 +00:00
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
2012-06-29 14:02:39 +00:00
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.ScheduledFuture;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs the use of the LatinIME keyboard.
|
|
|
|
*
|
2013-02-15 20:54:06 +00:00
|
|
|
* This class logs operations on the IME keyboard, including what the user has typed. Data is
|
|
|
|
* written to a {@link JsonWriter}, which will write to a local file.
|
|
|
|
*
|
|
|
|
* The JsonWriter is created on-demand by calling {@link #getInitializedJsonWriterLocked}.
|
|
|
|
*
|
|
|
|
* This class uses an executor to perform file-writing operations on a separate thread. It also
|
|
|
|
* tries to avoid creating unnecessary files if there is nothing to write. It also handles
|
|
|
|
* flushing, making sure it happens, but not too frequently.
|
2012-06-29 14:02:39 +00:00
|
|
|
*
|
|
|
|
* This functionality is off by default. See {@link ProductionFlag#IS_EXPERIMENTAL}.
|
|
|
|
*/
|
|
|
|
public class ResearchLog {
|
2013-02-15 20:54:06 +00:00
|
|
|
// TODO: Automatically initialize the JsonWriter rather than requiring the caller to manage it.
|
2012-06-29 14:02:39 +00:00
|
|
|
private static final String TAG = ResearchLog.class.getSimpleName();
|
2012-12-18 02:19:58 +00:00
|
|
|
private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
|
2012-08-03 03:22:29 +00:00
|
|
|
private static final long FLUSH_DELAY_IN_MS = 1000 * 5;
|
|
|
|
private static final int ABORT_TIMEOUT_IN_MS = 1000 * 4;
|
2012-06-29 14:02:39 +00:00
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
/* package */ final ScheduledExecutorService mExecutor;
|
2012-06-29 14:02:39 +00:00
|
|
|
/* package */ final File mFile;
|
2013-01-11 18:35:51 +00:00
|
|
|
private final Context mContext;
|
|
|
|
|
2012-07-18 20:52:41 +00:00
|
|
|
private JsonWriter mJsonWriter = NULL_JSON_WRITER;
|
2012-08-03 03:22:29 +00:00
|
|
|
// true if at least one byte of data has been written out to the log file. This must be
|
|
|
|
// remembered because JsonWriter requires that calls matching calls to beginObject and
|
|
|
|
// endObject, as well as beginArray and endArray, and the file is opened lazily, only when
|
|
|
|
// it is certain that data will be written. Alternatively, the matching call exceptions
|
|
|
|
// could be caught, but this might suppress other errors.
|
|
|
|
private boolean mHasWrittenData = false;
|
2012-06-29 14:02:39 +00:00
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
private static final JsonWriter NULL_JSON_WRITER = new JsonWriter(
|
|
|
|
new OutputStreamWriter(new NullOutputStream()));
|
2012-06-29 14:02:39 +00:00
|
|
|
private static class NullOutputStream extends OutputStream {
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public void write(byte[] buffer, int offset, int count) {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
|
|
|
/** {@inheritDoc} */
|
|
|
|
@Override
|
|
|
|
public void write(byte[] buffer) {
|
|
|
|
// nop
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void write(int oneByte) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-05 20:14:03 +00:00
|
|
|
public ResearchLog(final File outputFile, final Context context) {
|
2012-08-03 03:22:29 +00:00
|
|
|
mExecutor = Executors.newSingleThreadScheduledExecutor();
|
2012-06-29 14:02:39 +00:00
|
|
|
mFile = outputFile;
|
2013-01-11 18:35:51 +00:00
|
|
|
mContext = context;
|
2012-08-20 17:17:40 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 20:54:06 +00:00
|
|
|
/**
|
|
|
|
* Waits for any publication requests to finish and closes the {@link JsonWriter} used for
|
|
|
|
* output.
|
|
|
|
*
|
|
|
|
* See class comment for details about {@code JsonWriter} construction.
|
|
|
|
*/
|
2012-08-08 16:38:24 +00:00
|
|
|
public synchronized void close(final Runnable onClosed) {
|
2012-08-03 03:22:29 +00:00
|
|
|
mExecutor.submit(new Callable<Object>() {
|
|
|
|
@Override
|
|
|
|
public Object call() throws Exception {
|
|
|
|
try {
|
|
|
|
if (mHasWrittenData) {
|
|
|
|
mJsonWriter.endArray();
|
|
|
|
mJsonWriter.flush();
|
|
|
|
mJsonWriter.close();
|
2012-08-09 03:13:41 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "wrote log to " + mFile);
|
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
mHasWrittenData = false;
|
2012-08-09 03:13:41 +00:00
|
|
|
} else {
|
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "close() called, but no data, not outputting");
|
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
} catch (Exception e) {
|
2013-02-15 20:54:06 +00:00
|
|
|
Log.d(TAG, "error when closing ResearchLog:", e);
|
2012-08-03 03:22:29 +00:00
|
|
|
} finally {
|
2013-02-05 20:14:03 +00:00
|
|
|
if (mFile != null && mFile.exists()) {
|
2012-08-03 03:22:29 +00:00
|
|
|
mFile.setWritable(false, false);
|
|
|
|
}
|
2012-08-08 16:38:24 +00:00
|
|
|
if (onClosed != null) {
|
|
|
|
onClosed.run();
|
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
removeAnyScheduledFlush();
|
|
|
|
mExecutor.shutdown();
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
private boolean mIsAbortSuccessful;
|
2012-06-29 14:02:39 +00:00
|
|
|
|
2013-02-15 20:54:06 +00:00
|
|
|
/**
|
|
|
|
* Waits for publication requests to finish, closes the {@link JsonWriter}, but then deletes the
|
|
|
|
* backing file used for output.
|
|
|
|
*
|
|
|
|
* See class comment for details about {@code JsonWriter} construction.
|
|
|
|
*/
|
2012-08-03 03:22:29 +00:00
|
|
|
public synchronized void abort() {
|
|
|
|
mExecutor.submit(new Callable<Object>() {
|
|
|
|
@Override
|
|
|
|
public Object call() throws Exception {
|
|
|
|
try {
|
|
|
|
if (mHasWrittenData) {
|
|
|
|
mJsonWriter.endArray();
|
|
|
|
mJsonWriter.close();
|
|
|
|
mHasWrittenData = false;
|
|
|
|
}
|
|
|
|
} finally {
|
2013-02-05 20:14:03 +00:00
|
|
|
if (mFile != null) {
|
|
|
|
mIsAbortSuccessful = mFile.delete();
|
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
2012-07-18 20:52:41 +00:00
|
|
|
removeAnyScheduledFlush();
|
|
|
|
mExecutor.shutdown();
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
public boolean blockingAbort() throws InterruptedException {
|
|
|
|
abort();
|
|
|
|
mExecutor.awaitTermination(ABORT_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS);
|
|
|
|
return mIsAbortSuccessful;
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
public void awaitTermination(int delay, TimeUnit timeUnit) throws InterruptedException {
|
|
|
|
mExecutor.awaitTermination(delay, timeUnit);
|
2012-07-18 20:52:41 +00:00
|
|
|
}
|
|
|
|
|
2012-06-29 14:02:39 +00:00
|
|
|
/* package */ synchronized void flush() {
|
2012-08-03 03:22:29 +00:00
|
|
|
removeAnyScheduledFlush();
|
|
|
|
mExecutor.submit(mFlushCallable);
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2012-08-03 03:22:29 +00:00
|
|
|
private final Callable<Object> mFlushCallable = new Callable<Object>() {
|
2012-06-29 14:02:39 +00:00
|
|
|
@Override
|
|
|
|
public Object call() throws Exception {
|
2012-08-03 03:22:29 +00:00
|
|
|
mJsonWriter.flush();
|
2012-06-29 14:02:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private ScheduledFuture<Object> mFlushFuture;
|
|
|
|
|
|
|
|
private void removeAnyScheduledFlush() {
|
|
|
|
if (mFlushFuture != null) {
|
|
|
|
mFlushFuture.cancel(false);
|
|
|
|
mFlushFuture = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void scheduleFlush() {
|
|
|
|
removeAnyScheduledFlush();
|
|
|
|
mFlushFuture = mExecutor.schedule(mFlushCallable, FLUSH_DELAY_IN_MS, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:54:06 +00:00
|
|
|
/**
|
|
|
|
* Queues up {@code logUnit} to be published in the background.
|
|
|
|
*
|
|
|
|
* @param logUnit the {@link LogUnit} to be published
|
|
|
|
* @param canIncludePrivateData whether private data in the LogUnit should be included
|
|
|
|
*/
|
2013-01-12 00:49:54 +00:00
|
|
|
public synchronized void publish(final LogUnit logUnit, final boolean canIncludePrivateData) {
|
2012-08-03 03:22:29 +00:00
|
|
|
try {
|
|
|
|
mExecutor.submit(new Callable<Object>() {
|
|
|
|
@Override
|
|
|
|
public Object call() throws Exception {
|
2013-01-12 00:49:54 +00:00
|
|
|
logUnit.publishTo(ResearchLog.this, canIncludePrivateData);
|
2012-08-03 03:22:29 +00:00
|
|
|
scheduleFlush();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (RejectedExecutionException e) {
|
|
|
|
// TODO: Add code to record loss of data, and report.
|
2013-01-08 18:49:42 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.d(TAG, "ResearchLog.publish() rejecting scheduled execution");
|
|
|
|
}
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 21:59:22 +00:00
|
|
|
/**
|
|
|
|
* Return a JsonWriter for this ResearchLog. It is initialized the first time this method is
|
|
|
|
* called. The cached value is returned in future calls.
|
|
|
|
*/
|
2013-02-15 20:16:42 +00:00
|
|
|
public JsonWriter getInitializedJsonWriterLocked() {
|
2013-02-15 20:47:48 +00:00
|
|
|
if (mJsonWriter != NULL_JSON_WRITER || mFile == null) return mJsonWriter;
|
2012-06-29 14:02:39 +00:00
|
|
|
try {
|
2013-02-15 20:47:48 +00:00
|
|
|
final JsonWriter jsonWriter = createJsonWriter(mContext, mFile);
|
|
|
|
if (jsonWriter != null) {
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
mJsonWriter = jsonWriter;
|
|
|
|
mHasWrittenData = true;
|
2012-07-18 20:52:41 +00:00
|
|
|
}
|
2013-02-15 20:47:48 +00:00
|
|
|
} catch (final IOException e) {
|
2013-02-15 20:43:51 +00:00
|
|
|
Log.w(TAG, "Error in JsonWriter; disabling logging", e);
|
2012-06-29 14:02:39 +00:00
|
|
|
try {
|
|
|
|
mJsonWriter.close();
|
2013-02-15 20:47:48 +00:00
|
|
|
} catch (final IllegalStateException e1) {
|
2012-08-03 03:22:29 +00:00
|
|
|
// Assume that this is just the json not being terminated properly.
|
|
|
|
// Ignore
|
2013-02-15 20:47:48 +00:00
|
|
|
} catch (final IOException e1) {
|
2013-02-15 20:43:51 +00:00
|
|
|
Log.w(TAG, "Error in closing JsonWriter; disabling logging", e1);
|
2012-06-29 14:02:39 +00:00
|
|
|
} finally {
|
|
|
|
mJsonWriter = NULL_JSON_WRITER;
|
|
|
|
}
|
|
|
|
}
|
2012-12-18 21:59:22 +00:00
|
|
|
return mJsonWriter;
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
2013-02-15 20:43:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the JsonWriter to write the ResearchLog to.
|
|
|
|
*
|
|
|
|
* This method may be overriden in testing to redirect the output.
|
|
|
|
*/
|
|
|
|
/* package for test */ JsonWriter createJsonWriter(final Context context, final File file)
|
|
|
|
throws IOException {
|
|
|
|
return new JsonWriter(new BufferedWriter(new OutputStreamWriter(
|
|
|
|
context.openFileOutput(file.getName(), Context.MODE_PRIVATE))));
|
|
|
|
}
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|