Merge "Avoid disk writes on UI thread."
commit
95a5daa3e3
|
@ -21,6 +21,9 @@ import android.view.inputmethod.InputMethodManager;
|
|||
import android.content.Context;
|
||||
import android.inputmethodservice.InputMethodService;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Process;
|
||||
import android.text.format.DateUtils;
|
||||
import android.text.format.Time;
|
||||
import android.util.Log;
|
||||
|
@ -172,7 +175,7 @@ public class LatinIMEUtil {
|
|||
}
|
||||
}
|
||||
public String getLastString() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < mLength; ++i) {
|
||||
char c = mCharBuf[normalize(mEnd - 1 - i)];
|
||||
if (!((LatinIME)mContext).isWordSeparator(c)) {
|
||||
|
@ -247,16 +250,22 @@ public class LatinIMEUtil {
|
|||
private static final String FILENAME = "log.txt";
|
||||
private static final UsabilityStudyLogUtils sInstance =
|
||||
new UsabilityStudyLogUtils();
|
||||
private final Handler mLoggingHandler;
|
||||
private File mFile;
|
||||
private File mDirectory;
|
||||
private InputMethodService mIms;
|
||||
private PrintWriter mWriter;
|
||||
private Date mDate;
|
||||
private SimpleDateFormat mDateFormat;
|
||||
private final Date mDate;
|
||||
private final SimpleDateFormat mDateFormat;
|
||||
|
||||
private UsabilityStudyLogUtils() {
|
||||
mDate = new Date();
|
||||
mDateFormat = new SimpleDateFormat("dd MMM HH:mm:ss.SSS");
|
||||
|
||||
HandlerThread handlerThread = new HandlerThread("UsabilityStudyLogUtils logging task",
|
||||
Process.THREAD_PRIORITY_BACKGROUND);
|
||||
handlerThread.start();
|
||||
mLoggingHandler = new Handler(handlerThread.getLooper());
|
||||
}
|
||||
|
||||
public static UsabilityStudyLogUtils getInstance() {
|
||||
|
@ -266,7 +275,6 @@ public class LatinIMEUtil {
|
|||
public void init(InputMethodService ims) {
|
||||
mIms = ims;
|
||||
mDirectory = ims.getFilesDir();
|
||||
createLogFileIfNotExist();
|
||||
}
|
||||
|
||||
private void createLogFileIfNotExist() {
|
||||
|
@ -301,22 +309,28 @@ public class LatinIMEUtil {
|
|||
LatinImeLogger.onPrintAllUsabilityStudtyLogs();
|
||||
}
|
||||
|
||||
public void write(String log) {
|
||||
public void write(final String log) {
|
||||
mLoggingHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
createLogFileIfNotExist();
|
||||
final long currentTime = System.currentTimeMillis();
|
||||
mDate.setTime(currentTime);
|
||||
|
||||
final String printString = String.format("%s\t%d\t%s\n", mDateFormat.format(mDate),
|
||||
currentTime, log);
|
||||
final String printString = String.format("%s\t%d\t%s\n",
|
||||
mDateFormat.format(mDate), currentTime, log);
|
||||
if (LatinImeLogger.sDBG) {
|
||||
Log.d(TAG, "Write: " + log);
|
||||
}
|
||||
mWriter.print(printString);
|
||||
mWriter.flush();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void printAll() {
|
||||
StringBuffer sb = new StringBuffer();
|
||||
mLoggingHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
mWriter.flush();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader br = getBufferedReader();
|
||||
String line;
|
||||
try {
|
||||
|
@ -337,8 +351,12 @@ public class LatinIMEUtil {
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void clearAll() {
|
||||
mLoggingHandler.post(new Runnable() {
|
||||
public void run() {
|
||||
if (mFile != null && mFile.exists()) {
|
||||
if (LatinImeLogger.sDBG) {
|
||||
Log.d(TAG, "Delete log file.");
|
||||
|
@ -347,6 +365,8 @@ public class LatinIMEUtil {
|
|||
mWriter.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private BufferedReader getBufferedReader() {
|
||||
createLogFileIfNotExist();
|
||||
|
@ -365,7 +385,7 @@ public class LatinIMEUtil {
|
|||
mFile.delete();
|
||||
}
|
||||
}
|
||||
return new PrintWriter(new FileOutputStream(mFile));
|
||||
return new PrintWriter(new FileOutputStream(mFile), true /* autoFlush */);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue