Clean up initialization ordering

This change is based on an earlier one that got stuck in Gerrit: Iab77504b

Change-Id: I27ad9dfb1bbb2300bd1e61d881a6ea0e116db066
main
Kurt Partridge 2013-02-26 18:31:09 -08:00
parent 157c00145b
commit 4eeb90cd72
1 changed files with 23 additions and 28 deletions

View File

@ -236,35 +236,34 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
public void init(final LatinIME latinIME, final KeyboardSwitcher keyboardSwitcher,
final Suggest suggest) {
assert latinIME != null;
if (latinIME == null) {
Log.w(TAG, "IMS is null; logging is off");
} else {
mFilesDir = latinIME.getFilesDir();
if (mFilesDir == null || !mFilesDir.exists()) {
Log.w(TAG, "IME storage directory does not exist.");
}
mLatinIME = latinIME;
mFilesDir = latinIME.getFilesDir();
if (mFilesDir == null || !mFilesDir.exists()) {
Log.w(TAG, "IME storage directory does not exist. Cannot start logging.");
return;
}
mSuggest = suggest;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
if (prefs != null) {
sIsLogging = ResearchSettings.readResearchLoggerEnabledFlag(prefs);
prefs.registerOnSharedPreferenceChangeListener(this);
mPrefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
mPrefs.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();
}
}
// Initialize fields from preferences
sIsLogging = ResearchSettings.readResearchLoggerEnabledFlag(mPrefs);
// Initialize fields from resources
final Resources res = latinIME.getResources();
sAccountType = res.getString(R.string.research_account_type);
sAllowedAccountDomain = res.getString(R.string.research_allowed_account_domain);
mLatinIME = latinIME;
mPrefs = prefs;
// Cleanup logging directory
// TODO: Move this and other file-related components to separate file.
final long lastCleanupTime = mPrefs.getLong(PREF_LAST_CLEANUP_TIME, 0L);
final long now = System.currentTimeMillis();
if (now - lastCleanupTime > DURATION_BETWEEN_DIR_CLEANUP_IN_MS) {
final long timeHorizon = now - MAX_LOGFILE_AGE_IN_MS;
cleanupLoggingDir(mFilesDir, timeHorizon);
mPrefs.edit().putLong(PREF_LAST_CLEANUP_TIME, now).apply();
}
// Initialize external services
mUploadIntent = new Intent(mLatinIME, UploaderService.class);
mUploadNowIntent = new Intent(mLatinIME, UploaderService.class);
mUploadNowIntent.putExtra(UploaderService.EXTRA_UPLOAD_UNCONDITIONALLY, true);
@ -453,10 +452,6 @@ public class ResearchLogger implements SharedPreferences.OnSharedPreferenceChang
// Log.w(TAG, "not in usability mode; not logging");
return;
}
if (mFilesDir == null || !mFilesDir.exists()) {
Log.w(TAG, "IME storage directory does not exist. Cannot start logging.");
return;
}
if (mMainLogBuffer == null) {
mMainResearchLog = new ResearchLog(createLogFile(mFilesDir), mLatinIME);
final int numWordsToIgnore = new Random().nextInt(NUMBER_OF_WORDS_BETWEEN_SAMPLES + 1);