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