diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index e57f135e1d21e537b17ac9ad9eb54073bf1e84f4..fc4a4bb87dffd00a7571994faa5dec77c09ac3ca 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -92,61 +92,79 @@ public class ObserverService extends Service implements OnRemoteOperationListene CommonUtils.setServiceUnCaughtExceptionHandler(this); final SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); - String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); + final String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); + final String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); this.mAccount = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this)); - initialFolderCounter = prefs.getInt(AppConstants.INITIALFOLDERS_NUMBER, 0); - // Check if account is invalid - if (this.mAccount == null){ - Log.w(TAG, "No account registered"); + final boolean forcedSync = intent != null && DebugCmdReceiver.ACTION_FORCE_SYNC.equals(intent.getAction()); + + if (!checkStartCondition(prefs, forcedSync)) { return super.onStartCommand(intent, flags, startId); } - //check if user have disable eDrive's sync in account's settings + this.syncRequests = new HashMap<>(); + initialFolderCounter = prefs.getInt(AppConstants.INITIALFOLDERS_NUMBER, 0); + begin(); + return START_NOT_STICKY; + } + + /** + * This method check that all condition are met + * to start ObserverService: + * - a valid account as been registered + * - Synchronization of media and/or settings is enabled + * - Initialization task has been done properly + * - Service isn't already running + * - Check minimum delay since last call if not forced sync + * - Check that network is available depending of metered network allowed or not + * + * It also display log depending of the failure and send intent for initialization if this has + * not been done + * @return false if at least one condition is false + */ + private boolean checkStartCondition(final SharedPreferences prefs, final boolean forcedSync) { + // Check Account not null + if (mAccount == null) { + Log.e(TAG, "No account registered"); + return false; + } + + // Check that Media & Settings sync is enable if (!CommonUtils.isMediaSyncEnabled(mAccount) && !CommonUtils.isSettingsSyncEnabled(mAccount) ){ Log.w(TAG, "eDrive syncing has been disabled in /e/ account's settings"); - return super.onStartCommand(intent, flags, startId); + return false; } - //check if init has been done. I should check if it's really required... + // Check that Initialization has been done if (!prefs.getBoolean(INITIALIZATION_HAS_BEEN_DONE, false)) { Log.w(TAG, "Initialization hasn't been done"); Intent initializerIntent = new Intent(this, InitializerService.class); startService(initializerIntent); - return super.onStartCommand( intent, flags, startId ); + return false; } - //Check this service isn't already working - if (isWorking){ + // Check this service isn't already working + if (isWorking){ //TODO check if really used... Log.w(TAG, "ObserverService is already working"); - return super.onStartCommand(intent,flags,startId); - } - - //Check a minimum delay has been respected between two start. - long lastSyncTime = prefs.getLong(AppConstants.KEY_LAST_SYNC_TIME, 0L); - long currentTime = System.currentTimeMillis(); - boolean forceSync = false; - if (intent != null) { - forceSync = DebugCmdReceiver.ACTION_FORCE_SYNC.equals(intent.getAction()); + return false; } - //if time diff between current sync and last sync is higher or equal to delay minimum between two sync - if (!forceSync && (currentTime - lastSyncTime ) < INTERSYNC_MINIMUM_DELAY ){ + // Check minimum delay since last call & not forced sync + final long lastSyncTime = prefs.getLong(AppConstants.KEY_LAST_SYNC_TIME, 0L); + final long currentTime = System.currentTimeMillis(); + if (!forcedSync && (currentTime - lastSyncTime ) < INTERSYNC_MINIMUM_DELAY ){ Log.w(TAG, "Delay between now and last call is too short"); - return super.onStartCommand( intent, flags, startId ); + return false; } + // Check that network is available depending of metered network allowed or not final boolean meteredNetworkAllowed = CommonUtils.isMeteredNetworkAllowed(mAccount); //check for the case where intent has been launched by initializerService if (!CommonUtils.haveNetworkConnection(this, meteredNetworkAllowed)) { - Log.w(TAG, "There is no Internet connexion."); - return super.onStartCommand( intent, flags, startId ); + Log.w(TAG, "There is no allowed internet connexion."); + return false; } - this.syncRequests = new HashMap<>(); - - begin(); - return START_NOT_STICKY; + return true; } /* Common methods */