Merge "Avoid disk writes on UI thread."

main
Ken Wakasa 2010-11-18 12:46:58 -08:00 committed by Android (Google) Code Review
commit 95a5daa3e3
1 changed files with 61 additions and 41 deletions

View File

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