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 Original line Diff line number Diff line
@@ -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");
        } else {
        mFilesDir = latinIME.getFilesDir();
        mFilesDir = latinIME.getFilesDir();
        if (mFilesDir == null || !mFilesDir.exists()) {
        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;
        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
        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();
        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;
            final long timeHorizon = now - MAX_LOGFILE_AGE_IN_MS;
            cleanupLoggingDir(mFilesDir, timeHorizon);
            cleanupLoggingDir(mFilesDir, timeHorizon);
                Editor e = prefs.edit();
            mPrefs.edit().putLong(PREF_LAST_CLEANUP_TIME, now).apply();
                e.putLong(PREF_LAST_CLEANUP_TIME, now);
                e.apply();
        }
        }
        }

        final Resources res = latinIME.getResources();
        // Initialize external services
        sAccountType = res.getString(R.string.research_account_type);
        sAllowedAccountDomain = res.getString(R.string.research_allowed_account_domain);
        mLatinIME = latinIME;
        mPrefs = prefs;
        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);