From 4eeb90cd723db5961bf597ae26f540a9a3e30970 Mon Sep 17 00:00:00 2001
From: Kurt Partridge <kep@google.com>
Date: Tue, 26 Feb 2013 18:31:09 -0800
Subject: [PATCH] Clean up initialization ordering

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

Change-Id: I27ad9dfb1bbb2300bd1e61d881a6ea0e116db066
---
 .../inputmethod/research/ResearchLogger.java  | 53 +++++++++----------
 1 file changed, 24 insertions(+), 29 deletions(-)

diff --git a/java/src/com/android/inputmethod/research/ResearchLogger.java b/java/src/com/android/inputmethod/research/ResearchLogger.java
index 061ae3810a..70e86961e0 100644
--- a/java/src/com/android/inputmethod/research/ResearchLogger.java
+++ b/java/src/com/android/inputmethod/research/ResearchLogger.java
@@ -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.");
-            }
-        }
-        mSuggest = suggest;
-        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
-        if (prefs != null) {
-            sIsLogging = ResearchSettings.readResearchLoggerEnabledFlag(prefs);
-            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();
-            }
+        mLatinIME = latinIME;
+        mFilesDir = latinIME.getFilesDir();
+        if (mFilesDir == null || !mFilesDir.exists()) {
+            Log.w(TAG, "IME storage directory does not exist.  Cannot start logging.");
+            return;
         }
+        mPrefs = PreferenceManager.getDefaultSharedPreferences(latinIME);
+        mPrefs.registerOnSharedPreferenceChangeListener(this);
+
+        // 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);
-- 
GitLab