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;
|
|
|
|
|
2013-02-14 22:03:24 +00:00
|
|
|
import com.android.inputmethod.annotations.UsedForTesting;
|
2012-06-29 14:02:39 +00:00
|
|
|
import com.android.inputmethod.latin.define.ProductionFlag;
|
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
2013-05-09 21:25:28 +00:00
|
|
|
import java.io.FileNotFoundException;
|
2012-06-29 14:02:39 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
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
|
|
|
*
|
2013-03-18 09:21:18 +00:00
|
|
|
* This functionality is off by default. See
|
|
|
|
* {@link ProductionFlag#USES_DEVELOPMENT_ONLY_DIAGNOSTICS}.
|
2012-06-29 14:02:39 +00:00
|
|
|
*/
|
|
|
|
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();
|
2013-03-18 09:21:18 +00:00
|
|
|
private static final boolean DEBUG = false
|
|
|
|
&& ProductionFlag.USES_DEVELOPMENT_ONLY_DIAGNOSTICS_DEBUG;
|
2013-07-05 08:57:01 +00:00
|
|
|
private static final long FLUSH_DELAY_IN_MS = TimeUnit.SECONDS.toMillis(5);
|
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;
|
|
|
|
|
2013-05-09 21:25:28 +00:00
|
|
|
// Earlier implementations used a dummy JsonWriter that just swallowed what it was given, but
|
|
|
|
// this was tricky to do well, because JsonWriter throws an exception if it is passed more than
|
|
|
|
// one top-level object.
|
|
|
|
private JsonWriter mJsonWriter = null;
|
|
|
|
|
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
|
|
|
|
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-05-20 18:15:02 +00:00
|
|
|
/**
|
|
|
|
* Returns true if this is a FeedbackLog.
|
|
|
|
*
|
|
|
|
* FeedbackLogs record only the data associated with a Feedback dialog. Instead of normal
|
|
|
|
* logging, they contain a LogStatement with the complete feedback string and optionally a
|
|
|
|
* recording of the user's supplied demo of the problem.
|
|
|
|
*/
|
|
|
|
public boolean isFeedbackLog() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
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.
|
2013-02-14 22:14:48 +00:00
|
|
|
*
|
|
|
|
* @param onClosed run after the close() operation has completed asynchronously
|
2013-02-15 20:54:06 +00:00
|
|
|
*/
|
2013-02-14 22:03:24 +00:00
|
|
|
private 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 {
|
2013-05-09 21:25:28 +00:00
|
|
|
if (mJsonWriter == null) return null;
|
2013-04-14 19:24:49 +00:00
|
|
|
// TODO: This is necessary to avoid an exception. Better would be to not even
|
|
|
|
// open the JsonWriter if the file is not even opened unless there is valid data
|
|
|
|
// to write.
|
|
|
|
if (!mHasWrittenData) {
|
|
|
|
mJsonWriter.beginArray();
|
2013-02-15 20:56:46 +00:00
|
|
|
}
|
2013-04-14 19:24:49 +00:00
|
|
|
mJsonWriter.endArray();
|
|
|
|
mHasWrittenData = false;
|
2013-02-15 20:56:46 +00:00
|
|
|
mJsonWriter.flush();
|
|
|
|
mJsonWriter.close();
|
|
|
|
if (DEBUG) {
|
2013-05-09 21:25:28 +00:00
|
|
|
Log.d(TAG, "closed " + mFile);
|
2012-08-03 03:22:29 +00:00
|
|
|
}
|
2013-05-09 21:25:28 +00:00
|
|
|
} catch (final 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-27 22:40:54 +00:00
|
|
|
// Marking the file as read-only signals that this log file is ready to be
|
|
|
|
// uploaded.
|
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
|
|
|
}
|
|
|
|
|
2013-02-15 20:54:06 +00:00
|
|
|
/**
|
2013-02-14 22:03:24 +00:00
|
|
|
* Block until the research log has shut down and spooled out all output or {@code timeout}
|
|
|
|
* occurs.
|
2013-02-15 20:54:06 +00:00
|
|
|
*
|
2013-02-14 22:03:24 +00:00
|
|
|
* @param timeout time to wait for close in milliseconds
|
|
|
|
*/
|
|
|
|
public void blockingClose(final long timeout) {
|
|
|
|
close(null);
|
|
|
|
awaitTermination(timeout, TimeUnit.MILLISECONDS);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Waits for publication requests to finish, closes the JsonWriter, but then deletes the backing
|
|
|
|
* output file.
|
2013-02-14 22:14:48 +00:00
|
|
|
*
|
|
|
|
* @param onAbort run after the abort() operation has completed asynchronously
|
2013-02-15 20:54:06 +00:00
|
|
|
*/
|
2013-02-14 22:14:48 +00:00
|
|
|
private synchronized void abort(final Runnable onAbort) {
|
2012-08-03 03:22:29 +00:00
|
|
|
mExecutor.submit(new Callable<Object>() {
|
|
|
|
@Override
|
|
|
|
public Object call() throws Exception {
|
|
|
|
try {
|
2013-05-09 21:25:28 +00:00
|
|
|
if (mJsonWriter == null) return null;
|
2012-08-03 03:22:29 +00:00
|
|
|
if (mHasWrittenData) {
|
2013-04-14 19:24:49 +00:00
|
|
|
// TODO: This is necessary to avoid an exception. Better would be to not
|
|
|
|
// even open the JsonWriter if the file is not even opened unless there is
|
|
|
|
// valid data to write.
|
|
|
|
if (!mHasWrittenData) {
|
|
|
|
mJsonWriter.beginArray();
|
|
|
|
}
|
2012-08-03 03:22:29 +00:00
|
|
|
mJsonWriter.endArray();
|
|
|
|
mJsonWriter.close();
|
|
|
|
mHasWrittenData = false;
|
|
|
|
}
|
|
|
|
} finally {
|
2013-02-05 20:14:03 +00:00
|
|
|
if (mFile != null) {
|
2013-02-14 22:03:24 +00:00
|
|
|
mFile.delete();
|
2013-02-05 20:14:03 +00:00
|
|
|
}
|
2013-02-14 22:14:48 +00:00
|
|
|
if (onAbort != null) {
|
|
|
|
onAbort.run();
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2013-02-14 22:03:24 +00:00
|
|
|
/**
|
|
|
|
* Block until the research log has aborted or {@code timeout} occurs.
|
|
|
|
*
|
|
|
|
* @param timeout time to wait for close in milliseconds
|
|
|
|
*/
|
|
|
|
public void blockingAbort(final long timeout) {
|
2013-02-14 22:14:48 +00:00
|
|
|
abort(null);
|
2013-02-14 22:03:24 +00:00
|
|
|
awaitTermination(timeout, TimeUnit.MILLISECONDS);
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
|
|
|
|
2013-02-14 22:03:24 +00:00
|
|
|
@UsedForTesting
|
|
|
|
public void awaitTermination(final long delay, final TimeUnit timeUnit) {
|
|
|
|
try {
|
|
|
|
if (!mExecutor.awaitTermination(delay, timeUnit)) {
|
|
|
|
Log.e(TAG, "ResearchLog executor timed out while awaiting terminaion");
|
|
|
|
}
|
|
|
|
} catch (final InterruptedException e) {
|
|
|
|
Log.e(TAG, "ResearchLog executor interrupted while awaiting terminaion", e);
|
|
|
|
}
|
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 {
|
2013-05-09 21:25:28 +00:00
|
|
|
if (mJsonWriter != null) 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;
|
|
|
|
}
|
|
|
|
});
|
2013-02-14 22:14:48 +00:00
|
|
|
} catch (final RejectedExecutionException e) {
|
2012-08-03 03:22:29 +00:00
|
|
|
// TODO: Add code to record loss of data, and report.
|
2013-01-08 18:49:42 +00:00
|
|
|
if (DEBUG) {
|
2013-02-14 22:14:48 +00:00
|
|
|
Log.d(TAG, "ResearchLog.publish() rejecting scheduled execution", e);
|
2013-01-08 18:49:42 +00:00
|
|
|
}
|
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-05-09 21:25:28 +00:00
|
|
|
*
|
|
|
|
* @throws IOException if opening the JsonWriter is not possible
|
2012-12-18 21:59:22 +00:00
|
|
|
*/
|
2013-05-09 21:25:28 +00:00
|
|
|
public JsonWriter getInitializedJsonWriterLocked() throws IOException {
|
|
|
|
if (mJsonWriter != null) return mJsonWriter;
|
|
|
|
if (mFile == null) throw new FileNotFoundException();
|
2012-06-29 14:02:39 +00:00
|
|
|
try {
|
2013-02-15 20:47:48 +00:00
|
|
|
final JsonWriter jsonWriter = createJsonWriter(mContext, mFile);
|
2013-05-09 21:25:28 +00:00
|
|
|
if (jsonWriter == null) throw new IOException("Could not create JsonWriter");
|
|
|
|
|
|
|
|
jsonWriter.beginArray();
|
|
|
|
mJsonWriter = jsonWriter;
|
|
|
|
mHasWrittenData = true;
|
|
|
|
return mJsonWriter;
|
2013-02-15 20:47:48 +00:00
|
|
|
} catch (final IOException e) {
|
2013-05-09 21:25:28 +00:00
|
|
|
if (DEBUG) {
|
|
|
|
Log.w(TAG, "Exception when creating JsonWriter", e);
|
|
|
|
Log.w(TAG, "Closing JsonWriter");
|
2012-06-29 14:02:39 +00:00
|
|
|
}
|
2013-05-09 21:25:28 +00:00
|
|
|
if (mJsonWriter != null) mJsonWriter.close();
|
|
|
|
mJsonWriter = null;
|
|
|
|
throw e;
|
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
|
|
|
}
|