delete old ResearchLog files

cleans out the local directory of files that may have persisted
accidentally because of system crashes, etc.  default lifetime is
curently 1 day.

Bug: 6188932
Change-Id: I4b51169d6dd0eddb6bf8b661165f88e63782ce7d
main
Kurt Partridge 2012-07-13 15:35:26 -07:00
parent eea34598bf
commit 81dae8d015
1 changed files with 24 additions and 0 deletions

View File

@ -25,6 +25,7 @@ import android.content.SharedPreferences.Editor;
import android.inputmethodservice.InputMethodService; import android.inputmethodservice.InputMethodService;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.Log; import android.util.Log;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.inputmethod.CompletionInfo; import android.view.inputmethod.CompletionInfo;
@ -40,6 +41,7 @@ import com.android.inputmethod.latin.RichInputConnection.Range;
import com.android.inputmethod.latin.define.ProductionFlag; import com.android.inputmethod.latin.define.ProductionFlag;
import java.io.File; import java.io.File;
import java.io.FileFilter;
import java.io.IOException; import java.io.IOException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -93,6 +95,9 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
Character.codePointAt("\uE000", 0); // U+E000 is in the "private-use area" Character.codePointAt("\uE000", 0); // U+E000 is in the "private-use area"
// U+E001 is in the "private-use area" // U+E001 is in the "private-use area"
/* package for test */ static final String WORD_REPLACEMENT_STRING = "\uE001"; /* package for test */ static final String WORD_REPLACEMENT_STRING = "\uE001";
private static final String PREF_LAST_CLEANUP_TIME = "pref_last_cleanup_time";
private static final long DURATION_BETWEEN_DIR_CLEANUP_IN_MS = DateUtils.DAY_IN_MILLIS;
private static final long MAX_LOGFILE_AGE_IN_MS = DateUtils.DAY_IN_MILLIS;
// set when LatinIME should ignore an onUpdateSelection() callback that // set when LatinIME should ignore an onUpdateSelection() callback that
// arises from operations in this class // arises from operations in this class
private static boolean sLatinIMEExpectingUpdateSelection = false; private static boolean sLatinIMEExpectingUpdateSelection = false;
@ -124,10 +129,29 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
mUUIDString = getUUID(prefs); mUUIDString = getUUID(prefs);
sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false); sIsLogging = prefs.getBoolean(PREF_USABILITY_STUDY_MODE, false);
prefs.registerOnSharedPreferenceChangeListener(this); prefs.registerOnSharedPreferenceChangeListener(this);
final long lastCleanupTime = prefs.getLong(PREF_LAST_CLEANUP_TIME, 0L);
final long now = System.currentTimeMillis();
if (lastCleanupTime + DURATION_BETWEEN_DIR_CLEANUP_IN_MS < now) {
final long timeHorizon = now - MAX_LOGFILE_AGE_IN_MS;
cleanupLoggingDir(mFilesDir, timeHorizon);
Editor e = prefs.edit();
e.putLong(PREF_LAST_CLEANUP_TIME, now);
e.apply();
}
} }
mKeyboardSwitcher = keyboardSwitcher; mKeyboardSwitcher = keyboardSwitcher;
} }
private void cleanupLoggingDir(final File dir, final long time) {
for (File file : dir.listFiles()) {
if (file.getName().startsWith(ResearchLogger.FILENAME_PREFIX) &&
file.lastModified() < time) {
file.delete();
}
}
}
private File createLogFile(File filesDir) { private File createLogFile(File filesDir) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
sb.append(FILENAME_PREFIX).append('-'); sb.append(FILENAME_PREFIX).append('-');