diff --git a/app/src/main/java/foundation/e/drive/services/ResetService.java b/app/src/main/java/foundation/e/drive/services/ResetService.java index 99aad5fe4f0b0618cbbdbb6f73c54616d4945838..395e2acb43dc7291bb47a4136710ffaa325d62f2 100644 --- a/app/src/main/java/foundation/e/drive/services/ResetService.java +++ b/app/src/main/java/foundation/e/drive/services/ResetService.java @@ -39,64 +39,25 @@ public class ResetService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand()"); - if( intent.getExtras() != null ) { - String intent_accountName = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME, ""); - String intent_accountType = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE, ""); + if ( intent.getExtras() != null ) { + final String intent_accountName = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME, ""); + final String intent_accountType = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE, ""); - if(!intent_accountName.isEmpty() && !intent_accountType.isEmpty()){ - SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - - if(intent_accountName.equals(prefs.getString(AccountManager.KEY_ACCOUNT_NAME, "") ) - && intent_accountType.equals(prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, "") ) ){ - - //1. Stop sync services - - Intent stopperIntent = new Intent(getApplicationContext(), ObserverService.class); - boolean result = getApplicationContext().stopService( stopperIntent ); - Log.d(TAG, "stop ObserverService: "+result); - - stopperIntent = new Intent(getApplicationContext(), InitializerService.class); - result = getApplicationContext().stopService( stopperIntent ); - Log.d(TAG, "stop InitializerService: "+result); - - stopperIntent = new Intent(getApplicationContext(), SynchronizationService.class); //@todo try to replace it by stopperIntent.setClassName(getApplicationContext, OperationManagerService.class) - result = getApplicationContext().stopService( stopperIntent ); - - Log.d(TAG, "stop SynchronizationService: "+result); + if (!intent_accountName.isEmpty() && !intent_accountType.isEmpty()) { + final SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + if (intent_accountName.equals(prefs.getString(AccountManager.KEY_ACCOUNT_NAME, "")) + && intent_accountType.equals(prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""))) { WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG); - ( (EdriveApplication) this.getApplication() ).stopRecursiveFileObserver(); - WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG); - - ( (EdriveApplication) this.getApplication() ).stopRecursiveFileObserver(); + stopAllServices(); - //3. delete DB - result = this.deleteDatabase(DbHelper.DATABASE_NAME); + boolean result = deleteDatabase(DbHelper.DATABASE_NAME); Log.d(TAG, "Remove Database: "+result); - //4. preferences cleaning - - //Remove the prefs file. - if(!this.deleteSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME)){ - - //If removal failed, clear all data inside - prefs.edit().remove(AccountManager.KEY_ACCOUNT_NAME) - .remove(AccountManager.KEY_ACCOUNT_TYPE) - .putBoolean(INITIALIZATION_HAS_BEEN_DONE, false) - .remove(INITIALFOLDERS_NUMBER) - .remove(AppConstants.KEY_OMS_IS_WORKING) - .remove(AppConstants.KEY_LAST_SYNC_TIME) - .apply(); - } - - - //5. Remove Cached File - File[] cachedFiles = this.getApplicationContext().getExternalCacheDir().listFiles(); - for(File f : cachedFiles){ - f.delete(); - } + cleanSharedPreferences(prefs); + removeCachedFiles(); } } } @@ -104,9 +65,46 @@ public class ResetService extends Service { return super.onStartCommand(intent, flags, startId); } + + private void stopAllServices() { + Intent stopperIntent = new Intent(getApplicationContext(), ObserverService.class); + boolean result = getApplicationContext().stopService( stopperIntent ); + Log.d(TAG, "stop ObserverService: "+result); + + stopperIntent = new Intent(getApplicationContext(), InitializerService.class); + result = getApplicationContext().stopService( stopperIntent ); + Log.d(TAG, "stop InitializerService: "+result); + + stopperIntent = new Intent(getApplicationContext(), SynchronizationService.class); + result = getApplicationContext().stopService( stopperIntent ); + + Log.d(TAG, "stop SynchronizationService: "+result); + } + + private void cleanSharedPreferences(SharedPreferences prefs) { + if (!this.deleteSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME)) { + + //If removal failed, clear all data inside + prefs.edit().remove(AccountManager.KEY_ACCOUNT_NAME) + .remove(AccountManager.KEY_ACCOUNT_TYPE) + .putBoolean(INITIALIZATION_HAS_BEEN_DONE, false) + .remove(INITIALFOLDERS_NUMBER) + .remove(AppConstants.KEY_OMS_IS_WORKING) + .remove(AppConstants.KEY_LAST_SYNC_TIME) + .apply(); + } + } + + private void removeCachedFiles() { + File[] cachedFiles = this.getApplicationContext().getExternalCacheDir().listFiles(); + for (File f : cachedFiles) { + f.delete(); + } + } + @Nullable @Override public IBinder onBind(Intent intent) { return null; } -} +} \ No newline at end of file