From 357ffca8caf3317e18548e492ae654ec8cc98a52 Mon Sep 17 00:00:00 2001 From: Jonathan Klee Date: Wed, 29 Dec 2021 09:27:04 +0100 Subject: [PATCH] Introduce EdriveApplication class EdriveApplication is the class instantiated before any other class in the projet. The application is set as persitent, meaning if it gets killed, it will be respawned by the system. The application will also start automatically at boot so BootCompleteReceiver becomes useless. --- README.md | 3 + app/src/main/AndroidManifest.xml | 10 +-- .../foundation/e/drive/EdriveApplication.java | 67 +++++++++++++++++++ .../drive/receivers/BootCompleteReceiver.java | 61 ----------------- 4 files changed, 72 insertions(+), 69 deletions(-) create mode 100644 app/src/main/java/foundation/e/drive/EdriveApplication.java delete mode 100644 app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java diff --git a/README.md b/README.md index ca74e2ae..1bb0f953 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,9 @@ - Vincent (vincent.bourgmayer@e.email) - Frank +### Install + +Since the application is persistent, you won't be able to reinstall the app with a regular `adb install -r` command. You will have to `pm uninstall foundation.e.drive` before reinstalling the application. ### Notes: To disable All the synchronisation, go into your account and disable BOTH: "application settings" AND "Photos and videos" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 792349d7..1d75965c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,8 @@ http://www.gnu.org/licenses/gpl.html - - - - - - diff --git a/app/src/main/java/foundation/e/drive/EdriveApplication.java b/app/src/main/java/foundation/e/drive/EdriveApplication.java new file mode 100644 index 00000000..993cfe06 --- /dev/null +++ b/app/src/main/java/foundation/e/drive/EdriveApplication.java @@ -0,0 +1,67 @@ +/* + * Copyright © ECORP SAS 2022. + * 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 + * http://www.gnu.org/licenses/gpl.html + */ + +package foundation.e.drive; + +import android.accounts.Account; +import android.accounts.AccountManager; +import android.app.Application; +import android.content.Context; +import android.content.SharedPreferences; +import android.util.Log; + +import foundation.e.drive.utils.AppConstants; +import foundation.e.drive.utils.CommonUtils; +import foundation.e.drive.utils.JobUtils; + +/** + * Class representing the eDrive application. + * It is instantiated before any other class. + */ +public class EdriveApplication extends Application { + + private static final String TAG = "EdriveApplication"; + + @Override + public void onCreate() { + super.onCreate(); + + Log.i(TAG, "Starting"); + + SharedPreferences prefs = getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) { + prefs.edit().putBoolean(AppConstants.KEY_OMS_IS_WORKING, false).commit(); + scheduleScannerJob(); + } else { + Account mAccount = CommonUtils.getAccount(getString(R.string.eelo_account_type), AccountManager.get(this)); + if (mAccount != null) { + String accountName = mAccount.name; + String accountType = mAccount.type; + + prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) + .putBoolean(AppConstants.KEY_OMS_IS_WORKING, false) + .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) + .apply(); + + scheduleScannerJob(); + } + } + } + + private void scheduleScannerJob() { + if (!JobUtils.isScannerJobRegistered(this)) { + JobUtils.scheduleScannerJob(this); + } + } + + @Override + public void onLowMemory() { + super.onLowMemory(); + Log.w(TAG, "System is low on memory. Application might get killed by the system."); + } +} diff --git a/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java b/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java deleted file mode 100644 index a996fe1c..00000000 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java +++ /dev/null @@ -1,61 +0,0 @@ -package foundation.e.drive.receivers; - -import android.accounts.Account; -import android.accounts.AccountManager; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.util.Log; - -import foundation.e.drive.R; -import foundation.e.drive.services.ObserverService; -import foundation.e.drive.utils.AppConstants; -import foundation.e.drive.utils.CommonUtils; -import foundation.e.drive.utils.JobUtils; - - -public class BootCompleteReceiver extends BroadcastReceiver { - private final static String TAG = BootCompleteReceiver.class.getSimpleName(); - - @Override - public void onReceive(Context context, Intent intent) { - Log.i(TAG, "onReceive"); - String intentAction = intent.getAction(); - - if (intentAction == null) { - Log.e(TAG, "intent Action is null"); - } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - SharedPreferences prefs = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) { - //If user account is registered - prefs.edit().putBoolean(AppConstants.KEY_OMS_IS_WORKING, false).commit(); - - if (!JobUtils.isScannerJobRegistered(context)) { - //scanner job isn't registered then register it - JobUtils.scheduleScannerJob(context); - } - - } else { - - Account mAccount = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), AccountManager.get(context)); - - if (mAccount != null) { - String accountName = mAccount.name; - String accountType = mAccount.type; - - //If data come from intent, store them into pref because there aren't stored - prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) - .putBoolean(AppConstants.KEY_OMS_IS_WORKING, false) - .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) - .apply(); - - if (!JobUtils.isScannerJobRegistered(context)) { - //scanner job isn't registered then register it - JobUtils.scheduleScannerJob(context); - } - } - } - } - } -} -- GitLab