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

Commit de9ae859 authored by Vincent Bourgmayer's avatar Vincent Bourgmayer
Browse files

000 split some code for readibility

parent 7d0e5d18
Loading
Loading
Loading
Loading
+38 −31
Original line number Diff line number Diff line
/*
 * Copyright © ECORP SAS 2022.
 * Copyright © ECORP SAS 2022-2023.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
@@ -27,6 +27,8 @@ import foundation.e.lib.telemetry.Telemetry;
import timber.log.Timber;
import static timber.log.Timber.DebugTree;

import androidx.annotation.NonNull;

/**
 * Class representing the eDrive application.
 * It is instantiated before any other class.
@@ -40,42 +42,27 @@ public class EdriveApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        setupLogging();
        instantiateFileEventListener();

        if (BuildConfig.DEBUG) {
            Timber.plant(new DebugTree());
        } else {
            Telemetry.init(BuildConfig.SENTRY_DSN, this, true);
            Timber.plant(new ReleaseTree());
        }
        Timber.tag("EdriveApplication");
        CommonUtils.createNotificationChannel(getApplicationContext());

        fileEventListener = new FileEventListener(getApplicationContext());
        final SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        final String pathForObserver = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileObserver = new RecursiveFileObserver(getApplicationContext(), pathForObserver, fileEventListener);
        if (!isAccountStoredInPreferences(prefs)) {
            final Account account = CommonUtils.getAccount(getString(R.string.eelo_account_type), AccountManager.get(this));
            if (account == null) { return; }

        final SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
        CommonUtils.createNotificationChannel(getApplicationContext());
            prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, account.name)
                    .putString(AccountManager.KEY_ACCOUNT_TYPE, account.type)
                    .apply();
        }

        if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) {
        startRecursiveFileObserver();

        FailedSyncPrefsManager.getInstance(getApplicationContext()).clearPreferences();

        final Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), SynchronizationService.class);
        startService(SynchronizationServiceIntent);

        } else { //todo check utility of below code
            final Account mAccount = CommonUtils.getAccount(getString(R.string.eelo_account_type), AccountManager.get(this));
            if (mAccount == null) { return; }

            final String accountName = mAccount.name;
            final String accountType = mAccount.type;

            prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName)
                    .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType)
                    .apply();
        }
    }

    /**
@@ -100,4 +87,24 @@ public class EdriveApplication extends Application {
        super.onLowMemory();
        Timber.i("System is low on memory. Application might get killed by the system.");
    }

    private void instantiateFileEventListener() {
        fileEventListener = new FileEventListener(getApplicationContext());

        final String pathForObserver = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileObserver = new RecursiveFileObserver(getApplicationContext(), pathForObserver, fileEventListener);
    }

    private void setupLogging() {
        if (BuildConfig.DEBUG) {
            Timber.plant(new DebugTree());
        } else {
            Telemetry.init(BuildConfig.SENTRY_DSN, this, true);
            Timber.plant(new ReleaseTree());
        }
    }

    private boolean isAccountStoredInPreferences(@NonNull SharedPreferences prefs) {
        return prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null;
    }
}
 No newline at end of file
+34 −9
Original line number Diff line number Diff line
/*
 * Copyright © ECORP SAS 2022.
 * Copyright © MURENA SAS 2022-2023.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
@@ -25,6 +25,7 @@ import timber.log.Timber;

/**
 * @author Abhishek Aggarwal
 * @author vincent Bourgmayer
 */
public class BootCompletedReceiver extends BroadcastReceiver {
    private static final String DATE_SYSTEM_PROPERTY = "ro.build.date";
@@ -34,16 +35,14 @@ public class BootCompletedReceiver extends BroadcastReceiver {
        final String action = intent.getAction();
        Timber.tag(BootCompletedReceiver.class.getSimpleName()).v("onReceive(...)");
        final SharedPreferences pref = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
            if (!CommonUtils.getProp(DATE_SYSTEM_PROPERTY).equals(pref.getString(DATE_SYSTEM_PROPERTY, ""))) {
                pref.edit().putString(DATE_SYSTEM_PROPERTY, CommonUtils.getProp(DATE_SYSTEM_PROPERTY)).apply();
                //reinitialized the app each time
                pref.edit().putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false).apply();
                context.startService(new Intent(context, InitializerService.class));
            final String currentDateProp = CommonUtils.getProp(DATE_SYSTEM_PROPERTY);

                final DbHelper dbHelper = new DbHelper(context);
                dbHelper.getWritableDatabase().close(); //Force upgrade of db.
            if (isOsUpdated(pref, currentDateProp)) { // App is persistent and system so do not have classical update
                handleOsUpdate(pref, context, currentDateProp);
            }

            if (pref.getBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false)
                    && BuildConfig.VERSION_CODE > pref.getInt(PREF_VERSION_CODE, 1002000)) {
                pref.edit().putInt(PREF_VERSION_CODE, BuildConfig.VERSION_CODE).apply();
@@ -55,4 +54,30 @@ public class BootCompletedReceiver extends BroadcastReceiver {
            }
        }
    }

    private void forceDBUpdate(@NonNull Context context) {
        final DbHelper dbHelper = new DbHelper(context);
        dbHelper.getWritableDatabase().close(); //Force upgrade of db.
    }

    /**
     * Force reinitialization, upgrade of DB in case of OS update
     *
     * @param prefs SharedPreferences to extract last known build date
     * @param context Context used to start InitializationService
     * @param currentDateProp new build date
     */
    private void handleOsUpdate(@NonNull SharedPreferences prefs, @NonNull Context context, @NonNull String currentDateProp) {
        prefs.edit().putString(DATE_SYSTEM_PROPERTY, currentDateProp)
                .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false)
                .apply();

        context.startService(new Intent(context, InitializerService.class));
        forceDBUpdate(context);
    }

    private boolean isOsUpdated(@NonNull SharedPreferences prefs, @NonNull String currentDateProp) {
        final String lastKnownDateProp = prefs.getString(DATE_SYSTEM_PROPERTY, "");
        return !currentDateProp.equals(lastKnownDateProp);
    }
}