Logger improvements

- Made each method static
- Added a setter of contest
- Added a function to entry counts periodically

Change-Id: I644dfa83c4fcb1933dc267fc4cfad05c9bb7adc7
This commit is contained in:
satok 2010-04-28 12:34:14 +09:00
parent 3592749a4c
commit 8677ac3586
2 changed files with 86 additions and 43 deletions

View file

@ -308,6 +308,7 @@ public class LatinIME extends InputMethodService
}); });
} }
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
LatinImeLogger.init(this);
} }
private void initSuggest(String locale) { private void initSuggest(String locale) {
@ -353,6 +354,7 @@ public class LatinIME extends InputMethodService
if (VOICE_INSTALLED) { if (VOICE_INSTALLED) {
mVoiceInput.destroy(); mVoiceInput.destroy();
} }
LatinImeLogger.commit();
super.onDestroy(); super.onDestroy();
} }

View file

@ -17,36 +17,44 @@
package com.android.inputmethod.latin; package com.android.inputmethod.latin;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences;
import android.os.DropBoxManager; import android.os.DropBoxManager;
import android.preference.PreferenceManager;
import android.text.format.DateUtils; import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import java.util.ArrayList; import java.util.ArrayList;
public class LatinImeLogger { public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChangeListener {
private static final String TAG = "LatinIMELogs"; private static final String TAG = "LatinIMELogs";
private static final boolean DBG = false; private static final boolean DBG = false;
// DEFAULT_LOG_ENABLED should be false when released to public.
private static final boolean DEFAULT_LOG_ENABLED = true;
// Volatile is needed for multi-cpu platform. private static final long MINIMUMSENDINTERVAL = 5 * DateUtils.MINUTE_IN_MILLIS; // 5 min
private static volatile LatinImeLogger sLatinImeLogger; private static final long MINIMUMCOUNTINTERVAL = 20 * DateUtils.SECOND_IN_MILLIS; // 20 sec
private static final long MINIMUMINTERVAL = 20 * DateUtils.SECOND_IN_MILLIS; // 20 sec
private static final char SEPARATER = ';'; private static final char SEPARATER = ';';
private static final int ID_CLICKSUGGESTION = 0; private static final int ID_CLICKSUGGESTION = 0;
private static final int ID_AUTOSUGGESTION = 1; private static final int ID_AUTOSUGGESTION = 1;
private static final int ID_AUTOSUGGESTIONCANCELED = 2; private static final int ID_AUTOSUGGESTIONCANCELED = 2;
private static final int ID_INPUT = 3; private static final int ID_INPUT = 3;
private static final int ID_DELETE = 4; private static final int ID_DELETE = 4;
private static final String PREF_ENABLE_LOG = "enable_log";
private ArrayList<LogEntry> mLogBuffer; private static LatinImeLogger sLatinImeLogger = new LatinImeLogger();
private final Context mContext; public static boolean sLogEnabled = true;
private final DropBoxManager mDropBox;
private ArrayList<LogEntry> mLogBuffer = null;
private Context mContext = null;
private DropBoxManager mDropBox = null;
private long mLastTimeActive; private long mLastTimeActive;
private long mLastTimeSend; private long mLastTimeSend;
private long mLastTimeCountEntry;
private int mDeleteCount; private int mDeleteCount;
private int mInputCount; private int mInputCount;
private static class LogEntry { private static class LogEntry {
public final int mTag; public final int mTag;
public final long mTime; public final long mTime;
@ -58,28 +66,17 @@ public class LatinImeLogger {
} }
} }
/** private void initInternal(Context context) {
* Returns the singleton of the logger.
* @param context
*/
public static LatinImeLogger getLogger(Context context) {
if (sLatinImeLogger == null) {
synchronized (LatinImeLogger.class) {
if (sLatinImeLogger == null) {
sLatinImeLogger =new LatinImeLogger(context);
}
}
}
return sLatinImeLogger;
}
private LatinImeLogger(Context context) {
mContext = context; mContext = context;
mDropBox = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE); mDropBox = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
mLastTimeSend = System.currentTimeMillis(); mLastTimeSend = System.currentTimeMillis();
mLastTimeActive = mLastTimeSend; mLastTimeActive = mLastTimeSend;
mLastTimeCountEntry = mLastTimeSend;
mDeleteCount = 0; mDeleteCount = 0;
mInputCount = 0; mInputCount = 0;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
sLogEnabled = prefs.getBoolean(PREF_ENABLE_LOG, DEFAULT_LOG_ENABLED);
prefs.registerOnSharedPreferenceChangeListener(this);
} }
/** /**
@ -94,7 +91,7 @@ public class LatinImeLogger {
/** /**
* Check if the input string is safe as an entry or not. * Check if the input string is safe as an entry or not.
*/ */
private boolean checkStringDataSafe(String s) { private static boolean checkStringDataSafe(String s) {
for (int i = 0; i < s.length(); ++i) { for (int i = 0; i < s.length(); ++i) {
if (!Character.isDigit(s.charAt(i))) { if (!Character.isDigit(s.charAt(i))) {
return true; return true;
@ -103,6 +100,13 @@ public class LatinImeLogger {
return false; return false;
} }
private void addCountEntry(long time) {
mLogBuffer.add(new LogEntry (time, ID_DELETE, String.valueOf(mDeleteCount)));
mLogBuffer.add(new LogEntry (time, ID_INPUT, String.valueOf(mInputCount)));
mDeleteCount = 0;
mInputCount = 0;
}
/** /**
* Add an entry * Add an entry
* @param tag * @param tag
@ -111,9 +115,17 @@ public class LatinImeLogger {
private void addData(int tag, Object data) { private void addData(int tag, Object data) {
switch (tag) { switch (tag) {
case ID_DELETE: case ID_DELETE:
if (mLastTimeActive - mLastTimeCountEntry > MINIMUMCOUNTINTERVAL
|| (mDeleteCount == 0 && mInputCount == 0)) {
addCountEntry(mLastTimeActive);
}
mDeleteCount += (Integer)data; mDeleteCount += (Integer)data;
break; break;
case ID_INPUT: case ID_INPUT:
if (mLastTimeActive - mLastTimeCountEntry > MINIMUMCOUNTINTERVAL
|| (mDeleteCount == 0 && mInputCount == 0)) {
addCountEntry(mLastTimeActive);
}
mInputCount += (Integer)data; mInputCount += (Integer)data;
break; break;
default: default:
@ -148,17 +160,15 @@ public class LatinImeLogger {
} }
private String createStringFromEntries(ArrayList<LogEntry> logs) { private String createStringFromEntries(ArrayList<LogEntry> logs) {
addCountEntry(System.currentTimeMillis());
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
String nowString = String.valueOf(System.currentTimeMillis());
appendLogEntry(sb, nowString, String.valueOf(ID_DELETE), String.valueOf(mDeleteCount));
appendLogEntry(sb, nowString, String.valueOf(ID_INPUT), String.valueOf(mInputCount));
for (LogEntry log: logs) { for (LogEntry log: logs) {
appendLogEntry(sb, String.valueOf(log.mTime), String.valueOf(log.mTag), log.mData); appendLogEntry(sb, String.valueOf(log.mTime), String.valueOf(log.mTag), log.mData);
} }
return sb.toString(); return sb.toString();
} }
private void commit() { private void commitInternal() {
mDropBox.addText(TAG, createStringFromEntries(mLogBuffer)); mDropBox.addText(TAG, createStringFromEntries(mLogBuffer));
reset(); reset();
mLastTimeSend = System.currentTimeMillis(); mLastTimeSend = System.currentTimeMillis();
@ -166,37 +176,68 @@ public class LatinImeLogger {
private void sendLogToDropBox(int tag, Object s) { private void sendLogToDropBox(int tag, Object s) {
long now = System.currentTimeMillis(); long now = System.currentTimeMillis();
if (now - mLastTimeActive > MINIMUMINTERVAL) { if (now - mLastTimeActive > MINIMUMSENDINTERVAL) {
// Send a log before adding an log entry if the last data is too old. // Send a log before adding an log entry if the last data is too old.
commit(); commitInternal();
addData(tag, s); addData(tag, s);
} else if (now - mLastTimeSend > MINIMUMINTERVAL) { } else if (now - mLastTimeSend > MINIMUMSENDINTERVAL) {
// Send a log after adding an log entry. // Send a log after adding an log entry.
addData(tag, s); addData(tag, s);
commit(); commitInternal();
} else { } else {
addData(tag, s); addData(tag, s);
} }
mLastTimeActive = now; mLastTimeActive = now;
} }
public void logOnClickSuggestion(String s) { public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
sendLogToDropBox(ID_CLICKSUGGESTION, s); if (PREF_ENABLE_LOG.equals(key)) {
if (sharedPreferences.getBoolean(key, DEFAULT_LOG_ENABLED)) {
sLogEnabled = (mContext != null);
} else {
sLogEnabled = false;
}
}
} }
public void logOnAutoSuggestion(String s) { public static void init(Context context) {
sendLogToDropBox(ID_AUTOSUGGESTION, s); sLatinImeLogger.initInternal(context);
} }
public void logOnAutoSuggestionCanceled(String s) { public static void commit() {
sendLogToDropBox(ID_AUTOSUGGESTIONCANCELED, s); if (sLogEnabled) {
sLatinImeLogger.commitInternal();
}
} }
public void logOnDelete(int length) { public static void logOnClickSuggestion(String s) {
sendLogToDropBox(ID_DELETE, length); if (sLogEnabled) {
sLatinImeLogger.sendLogToDropBox(ID_CLICKSUGGESTION, s);
}
} }
public void logOnInputChar(int length) { public static void logOnAutoSuggestion(String s) {
sendLogToDropBox(ID_INPUT, length); if (sLogEnabled) {
sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTION, s);
}
} }
public static void logOnAutoSuggestionCanceled(String s) {
if (sLogEnabled) {
sLatinImeLogger.sendLogToDropBox(ID_AUTOSUGGESTIONCANCELED, s);
}
}
public static void logOnDelete(int length) {
if (sLogEnabled) {
sLatinImeLogger.sendLogToDropBox(ID_DELETE, length);
}
}
public static void logOnInputChar(int length) {
if (sLogEnabled) {
sLatinImeLogger.sendLogToDropBox(ID_INPUT, length);
}
}
} }