Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4eeb90cd authored by Kurt Partridge's avatar Kurt Partridge
Browse files

Clean up initialization ordering

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

Change-Id: I27ad9dfb1bbb2300bd1e61d881a6ea0e116db066
parent 157c0014
Loading
Loading
Loading
Loading
+24 −29
Original line number Diff line number Diff line
@@ -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 {
        mLatinIME = latinIME;
        mFilesDir = latinIME.getFilesDir();
        if (mFilesDir == null || !mFilesDir.exists()) {
                Log.w(TAG, "IME storage directory does not exist.");
            }
            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);
        // 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);

        // 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 (lastCleanupTime + DURATION_BETWEEN_DIR_CLEANUP_IN_MS < now) {
        if (now - lastCleanupTime > DURATION_BETWEEN_DIR_CLEANUP_IN_MS) {
            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();
            mPrefs.edit().putLong(PREF_LAST_CLEANUP_TIME, now).apply();
        }
        }
        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;

        // 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);