From c9831017235378b9cde337f56a32493a9114ca67 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Aug 2023 07:37:18 +0200 Subject: [PATCH 1/2] split EdriveApplication.onCreate() into smaller methods for better readibility --- .../foundation/e/drive/EdriveApplication.java | 69 ++++++++++--------- 1 file changed, 38 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/EdriveApplication.java b/app/src/main/java/foundation/e/drive/EdriveApplication.java index ac1765a2..586b5073 100644 --- a/app/src/main/java/foundation/e/drive/EdriveApplication.java +++ b/app/src/main/java/foundation/e/drive/EdriveApplication.java @@ -1,5 +1,5 @@ /* - * 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"); - - fileEventListener = new FileEventListener(getApplicationContext()); - - final String pathForObserver = Environment.getExternalStorageDirectory().getAbsolutePath(); - mFileObserver = new RecursiveFileObserver(getApplicationContext(), pathForObserver, fileEventListener); - - final SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); CommonUtils.createNotificationChannel(getApplicationContext()); - 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 SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - final String accountName = mAccount.name; - final String accountType = mAccount.type; + if (!isAccountStoredInPreferences(prefs)) { + final Account account = CommonUtils.getAccount(getString(R.string.eelo_account_type), AccountManager.get(this)); + if (account == null) { return; } - prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) - .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) + prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, account.name) + .putString(AccountManager.KEY_ACCOUNT_TYPE, account.type) .apply(); } + + startRecursiveFileObserver(); + FailedSyncPrefsManager.getInstance(getApplicationContext()).clearPreferences(); + + final Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), SynchronizationService.class); + startService(SynchronizationServiceIntent); } /** @@ -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 -- GitLab From 6eb2c243c8d0c9ec8bc48ac77e60d2a8b4ee8587 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 3 Aug 2023 08:14:16 +0200 Subject: [PATCH 2/2] split code of BootCompletedReceiver in smaller method --- .../receivers/BootCompletedReceiver.java | 43 +++++++++++++++---- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java index 0ffe6529..12593bee 100644 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/BootCompletedReceiver.java @@ -1,5 +1,5 @@ /* - * 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 DbHelper dbHelper = new DbHelper(context); - dbHelper.getWritableDatabase().close(); //Force upgrade of db. + final String currentDateProp = CommonUtils.getProp(DATE_SYSTEM_PROPERTY); + + 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); + } } -- GitLab