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

Commit 058f77ba authored by Aayush Gupta's avatar Aayush Gupta
Browse files

Merge branch 'e1-resetService' into '1-epic-refactoring-p1'

Refactor ResetService: split "onCreate()" content into private method

See merge request !88
parents 9982181d da7182ca
Loading
Loading
Loading
Loading
Loading
+49 −51
Original line number Diff line number Diff line
@@ -40,17 +40,33 @@ public class ResetService extends Service {
    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, "");
            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);
                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();

                    stopAllServices();

                    boolean result = deleteDatabase(DbHelper.DATABASE_NAME);
                    Log.d(TAG, "Remove Database: "+result);

                    cleanSharedPreferences(prefs);
                    removeCachedFiles();
                }
            }
        }
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

                    //1. Stop sync services

    private void stopAllServices() {
        Intent stopperIntent = new Intent(getApplicationContext(), ObserverService.class);
        boolean result = getApplicationContext().stopService( stopperIntent );
        Log.d(TAG, "stop ObserverService: "+result);
@@ -59,26 +75,13 @@ public class ResetService extends Service {
        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)
        stopperIntent = new Intent(getApplicationContext(), SynchronizationService.class);
        result = getApplicationContext().stopService( stopperIntent );

        Log.d(TAG, "stop SynchronizationService: "+result);
    }

                    WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG);

                    ( (EdriveApplication) this.getApplication() ).stopRecursiveFileObserver();

                    WorkManager.getInstance(this).cancelAllWorkByTag(AppConstants.WORK_GENERIC_TAG);

                    ( (EdriveApplication) this.getApplication() ).stopRecursiveFileObserver();

                    //3. delete DB
                    result = this.deleteDatabase(DbHelper.DATABASE_NAME);
                    Log.d(TAG, "Remove Database: "+result);

                    //4. preferences cleaning

                    //Remove the prefs file.
    private void cleanSharedPreferences(SharedPreferences prefs) {
        if (!this.deleteSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME)) {

            //If removal failed, clear all data inside
@@ -90,19 +93,14 @@ public class ResetService extends Service {
                    .remove(AppConstants.KEY_LAST_SYNC_TIME)
                    .apply();
        }
    }


                    //5. Remove Cached File
    private void removeCachedFiles() {
        File[] cachedFiles =  this.getApplicationContext().getExternalCacheDir().listFiles();
        for (File f : cachedFiles) {
            f.delete();
        }
    }
            }
        }
        stopSelf();
        return super.onStartCommand(intent, flags, startId);
    }

    @Nullable
    @Override