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

Commit f6ae1d76 authored by vince-bourgmayer's avatar vince-bourgmayer
Browse files

rewrote onStartCommand of ObserverService

parent 51dcb107
Loading
Loading
Loading
Loading
+60 −27
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ import foundation.e.drive.utils.JobUtils;
 */
public class ObserverService extends Service implements OnRemoteOperationListener {
    private final static String TAG = ObserverService.class.getSimpleName();
    private final static int INTERSYNC_MINIMUM_DELAY = 30000; // min delay between two sync in ms.

    private OwnCloudClient mClient;
    private List<SyncedFolder> mSyncedFolders; //List of synced folder
    private boolean mBoundToOperationManager = false;
@@ -124,40 +126,71 @@ public class ObserverService extends Service implements OnRemoteOperationListene

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i(TAG, "onStartCommand");
        Log.i(TAG, "onStartCommand("+startId+")");

        SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);

        if (prefs.getBoolean(INITIALIZATION_HAS_BEEN_DONE, false)) {

        String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, "");
        String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, "");
            initialFolderCounter = prefs.getInt(AppConstants.INITIALFOLDERS_NUMBER, 0);
        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");
            JobUtils.stopScheduledJob(this, JobUtils.ScannerJobId); //If no account
            return super.onStartCommand(intent, flags, startId);
        }

        //check if user have disable eDrive's sync in account's settings
        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);
        }

        //check if init has been done. I should check if it's really required...
        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);
        }

        //Check this service isn't already working
        if(isWorking){
            Log.w(TAG, "ObserverService is already working");
            return super.onStartCommand(intent,flags,startId);
        }

        //check OperationManagerService isn't working
        //@Todo
        if(false){
            Log.w(TAG, "OperationManagerService is still performing some operation");
            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();

        //if time diff between current sync and last sync is higher or equal to delay minimum between two sync
        if( (currentTime - lastSyncTime ) <  INTERSYNC_MINIMUM_DELAY ){
            Log.w(TAG, "Delay between now and last call is too short");
            return super.onStartCommand( intent, flags, startId );
        }

        //check for the case where intent has been launched by initializerService
        if (!CommonUtils.haveNetworkConnexion(this)) {
            Log.w(TAG, "There is no Internet connexion.");
            return super.onStartCommand( intent, flags, startId );
        }

            if (this.mAccount != null ){
                if ( CommonUtils.isMediaSyncEnabled(this.mAccount) || CommonUtils.isSettingsSyncEnabled(this.mAccount) ) {
        this.mClient = CommonUtils.getOwnCloudClient(this.mAccount, getApplicationContext());
        if (mClient != null) //background task
            begin();
        else {
            Log.e(TAG, "mClient  is null");
                        this.stopSelf();
        }

                }else{
                    this.stopSelf();
                }
            } else {
                JobUtils.stopScheduledJob(this, JobUtils.ScannerJobId); //If no account
                this.stopSelf();
            }
        }else{
            Log.w(TAG, "Server must be initialized before to start");
            Intent initializerIntent = new Intent(this, InitializerService.class);
            startService(initializerIntent);
            this.stopSelf();
        }
        return super.onStartCommand( intent, flags, startId );
    }

+3 −0
Original line number Diff line number Diff line
@@ -22,8 +22,11 @@ public abstract class AppConstants {
    public static final String APPLICATIONS_LIST_FILE_NAME = "packages_list.csv";
    public static final String APPLICATIONS_LIST_FILE_NAME_TMP = "tmp_packages_list.csv";
    public static final String SHARED_PREFERENCE_NAME ="preferences";
    public static final String KEY_LAST_SYNC_TIME = "lastSyncTimestamp";  //key for read/write last sync time (in ms) in prefs.


    public static final String[] MEDIA_SYNCABLE_CATEGORIES = new String[]{"Images", "Movies", "Music", "Ringtones", "Documents", "Podcasts"};
    public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[] {"Rom settings"};


}