diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java index 6b208015559e88507b27716673d1d9eaae69c1c6..ef08ccb205acb6100b603966e4e59c208a9a5c15 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -25,7 +25,6 @@ import com.owncloud.android.lib.common.operations.OnRemoteOperationListener; import com.owncloud.android.lib.common.operations.RemoteOperation; import com.owncloud.android.lib.common.operations.RemoteOperationResult; - import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -36,7 +35,6 @@ import foundation.e.drive.operations.CreateInitialFolderRemoteOperation; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; - import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR; import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER; import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES; @@ -52,10 +50,10 @@ public class InitializerService extends Service implements OnRemoteOperationList private final String TAG = InitializerService.class.getSimpleName(); private int existingRemoteFolderCounter; //Temporarily used to know if all remotePath exist - private List mSyncedFolders; - private OwnCloudClient mCloudClient; - private Handler mHandler; - private Account mAccount; + private List syncedFolders; + private OwnCloudClient cloudClient; + private Handler handler; + private Account account; private int restartFolderCreationCounter =0; @Override @@ -66,39 +64,37 @@ public class InitializerService extends Service implements OnRemoteOperationList } @Override - public int onStartCommand( Intent intent, int flags, int startId ) { + public int onStartCommand(Intent intent, int flags, int startId) { Log.i(TAG, "onStartCommand(...)"); CommonUtils.setServiceUnCaughtExceptionHandler(this); //Get account - SharedPreferences prefs = this.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE ); - - - if (prefs.getBoolean( AppConstants.INITIALIZATION_HAS_BEEN_DONE, false ) ) { + SharedPreferences prefs = this.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + if (prefs.getBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, false)) { Log.w(TAG, "Initializer has already been run"); } else { - String accountName = prefs.getString( AccountManager.KEY_ACCOUNT_NAME, "" ); - String accountType = prefs.getString( AccountManager.KEY_ACCOUNT_TYPE, "" ); + String accountName = prefs.getString(AccountManager.KEY_ACCOUNT_NAME, ""); + String accountType = prefs.getString(AccountManager.KEY_ACCOUNT_TYPE, ""); - if ( accountName.isEmpty() && accountType.isEmpty() && intent.getExtras() != null ) { + if (accountName.isEmpty() && accountType.isEmpty() && intent.getExtras() != null) { - accountName = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_NAME, "" ); - accountType = intent.getExtras().getString( AccountManager.KEY_ACCOUNT_TYPE, "" ); + accountName = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_NAME, ""); + accountType = intent.getExtras().getString(AccountManager.KEY_ACCOUNT_TYPE, ""); - prefs.edit().putString( AccountManager.KEY_ACCOUNT_NAME, accountName ) - .putString( AccountManager.KEY_ACCOUNT_TYPE, accountType ) + prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) + .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) .apply(); } - if (accountName.isEmpty() ) { + if (accountName.isEmpty()) { Log.w(TAG, "Account's name not found. Neither in shared prefs nor in intent's extras"); stopSelf(); } else { - this.mAccount = CommonUtils.getAccount( accountName, accountType, AccountManager.get(this) ); + this.account = CommonUtils.getAccount(accountName, accountType, AccountManager.get(this)); //Get OwnCloudlient - if (this.mAccount != null) { - this.mCloudClient = CommonUtils.getOwnCloudClient( this.mAccount, getApplicationContext()); + if (this.account != null) { + this.cloudClient = CommonUtils.getOwnCloudClient(this.account, getApplicationContext()); start(); } else { Log.w(TAG, "Got account is invalid."); @@ -109,25 +105,20 @@ public class InitializerService extends Service implements OnRemoteOperationList return super.onStartCommand(intent, flags, startId); } - - /** - * start to do its job - */ - public void start() { Log.i(TAG, "start()"); - if (mCloudClient == null){ + if (cloudClient == null) { stopSelf(); return; } List syncCategories = new ArrayList<>(); - if (CommonUtils.isMediaSyncEnabled(mAccount)) { + if (CommonUtils.isMediaSyncEnabled(account)) { syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); } - if (CommonUtils.isSettingsSyncEnabled(mAccount)) { + if (CommonUtils.isSettingsSyncEnabled(account)) { syncCategories.addAll(Arrays.asList(SETTINGS_SYNCABLE_CATEGORIES)); } @@ -136,101 +127,95 @@ public class InitializerService extends Service implements OnRemoteOperationList this.existingRemoteFolderCounter = 0; CreateNextRemoteFolder(); - } + } /** * Return a list of SyncedFolder * @param categories categories indicating which syncedFolder to create */ - private void getInitialSyncedFolders( List categories){ + private void getInitialSyncedFolders(List categories) { Log.i(TAG, "getInitialSyncedFolders"); - this.mSyncedFolders = new ArrayList<>(); + this.syncedFolders = new ArrayList<>(); - for(int i=-1, size = categories.size(); ++i < size;){ + for(int i=-1, size = categories.size(); ++i < size;) { final String DEVICE_SPECIFIC_PATH = PATH_SEPARATOR+"Devices"+PATH_SEPARATOR+ Build.BRAND+"_"+ Build.MODEL+"_" + Build.SERIAL; - switch ( categories.get(i) ){ + switch (categories.get(i)) { case "Medias": break; case "Images": - mSyncedFolders.add( new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DCIM), - "/Photos/", true) ); - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PICTURES), - "/Pictures/", true ) ); + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DCIM), + "/Photos/", true)); + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PICTURES), + "/Pictures/", true)); break; case "Movies": - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MOVIES), + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MOVIES), "/Movies/", true)); break; case "Music": - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MUSIC), + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_MUSIC), "/Music/", true)); break; case "Ringtones": - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_RINGTONES), + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_RINGTONES), "/Ringtones/", true)); break; case "Documents": - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DOCUMENTS), + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_DOCUMENTS), "/Documents/", true)); break; case "Podcasts": - mSyncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PODCASTS), + syncedFolders.add(new SyncedFolder(categories.get(i), getExternalFolder(Environment.DIRECTORY_PODCASTS), "/Podcasts/", true)); break; case "Rom settings": String remoteFolderPath = DEVICE_SPECIFIC_PATH+"/rom_settings/"; - mSyncedFolders.add( new SyncedFolder(categories.get(i), "/data/system/users/0/", remoteFolderPath, true, false, true, false) ); + syncedFolders.add(new SyncedFolder(categories.get(i), "/data/system/users/0/", remoteFolderPath, true, false, true, false)); try{ - mSyncedFolders.add( new SyncedFolder( + syncedFolders.add(new SyncedFolder( categories.get(i), getFilesDir().getCanonicalPath()+PATH_SEPARATOR, remoteFolderPath+"app_list/", true, false, true, - false) ); - } catch (Exception e){ Log.e(TAG, e.toString()); } - break; + false)); + } catch (Exception e) { Log.e(TAG, e.toString()); } + break; } } } - private String getExternalFolder(String directory){ + private String getExternalFolder(String directory) { return CommonUtils.getLocalPath(Environment.getExternalStoragePublicDirectory(directory))+ PATH_SEPARATOR; } - /** - * Start to createSyncedFolder in the cloud - */ - private void CreateNextRemoteFolder(){ + private void CreateNextRemoteFolder() { Log.i(TAG, "createNextRemoteFolder()"); this.restartFolderCreationCounter = 0; - if (this.mSyncedFolders == null || this.mSyncedFolders.isEmpty() ){ + if (this.syncedFolders == null || this.syncedFolders.isEmpty()) { this.stopSelf(); } //It means that there are still folders to create - if (this.existingRemoteFolderCounter < this.mSyncedFolders.size() ){ - - if (this.mHandler == null ) this.mHandler = new Handler(); + if (this.existingRemoteFolderCounter < this.syncedFolders.size()) { + if (this.handler == null) this.handler = new Handler(); CreateInitialFolderRemoteOperation createFolderOperation = new CreateInitialFolderRemoteOperation( - this.mSyncedFolders.get( this.existingRemoteFolderCounter ), + this.syncedFolders.get(this.existingRemoteFolderCounter), true, this); - createFolderOperation.execute(this.mCloudClient, this, this.mHandler); - - } else if (this.existingRemoteFolderCounter == this.mSyncedFolders.size() ){ + createFolderOperation.execute(this.cloudClient, this, this.handler); + } else if (this.existingRemoteFolderCounter == this.syncedFolders.size()) { doLastStep(); - } else { - Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.mSyncedFolders.size() ); + Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.syncedFolders.size()); this.stopSelf(); } } @@ -238,27 +223,25 @@ public class InitializerService extends Service implements OnRemoteOperationList @Override public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) { Log.i(TAG, "onRemoteOperationFinish()"); - if (operation instanceof CreateInitialFolderRemoteOperation){ + if (operation instanceof CreateInitialFolderRemoteOperation) { - if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS){ + if (result.isSuccess() || result.getCode() == RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS) { this.existingRemoteFolderCounter+=1; CreateNextRemoteFolder(); - } else if (result.getHttpCode() == 423 || result.getHttpCode() == 409){//file locked or conflict in result - Log.e( TAG, result.getLogMessage() ); + } else if (result.getHttpCode() == 423 || result.getHttpCode() == 409) {//file locked or conflict in result + Log.e(TAG, result.getLogMessage()); if (this.restartFolderCreationCounter < 3) { - Log.w( TAG, " restart operation" ); - operation.execute( this.mCloudClient, this, this.mHandler ); + Log.w(TAG, " restart operation"); + operation.execute(this.cloudClient, this, this.handler); this.restartFolderCreationCounter+=1; - } else { Log.e(TAG, "Remote folder's creation failed due to conflict with server"); stopSelf(); } - } else { - Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode() ); + Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode()); stopSelf(); } } @@ -268,20 +251,20 @@ public class InitializerService extends Service implements OnRemoteOperationList /** * Function to check if all remote folder have been created **/ - private void doLastStep(){ + private void doLastStep() { Log.i(TAG, "doLastStep()"); final Context appContext = getApplicationContext(); - appContext.getSharedPreferences( AppConstants.SHARED_PREFERENCE_NAME, - Context.MODE_PRIVATE ) + appContext.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, + Context.MODE_PRIVATE) .edit() .putBoolean(AppConstants.INITIALIZATION_HAS_BEEN_DONE, true) - .putInt( INITIALFOLDERS_NUMBER, mSyncedFolders.size() ) + .putInt(INITIALFOLDERS_NUMBER, syncedFolders.size()) .apply(); CommonUtils.registerPeriodicFullScanWorker(WorkManager.getInstance(appContext)); //all folder have been created - ((EdriveApplication) this.getApplication() ).startRecursiveFileObserver(); + ((EdriveApplication) this.getApplication()).startRecursiveFileObserver(); Intent SynchronizationServiceIntent = new Intent(getApplicationContext(), foundation.e.drive.services.SynchronizationService.class); startService(SynchronizationServiceIntent); @@ -296,11 +279,11 @@ public class InitializerService extends Service implements OnRemoteOperationList @Override public void onDestroy() { super.onDestroy(); - this.mHandler = null; - this.mAccount = null; - this.mCloudClient = null; - if(this.mSyncedFolders != null) this.mSyncedFolders.clear(); - this.mSyncedFolders = null; + this.handler = null; + this.account = null; + this.cloudClient = null; + if (this.syncedFolders != null) this.syncedFolders.clear(); + this.syncedFolders = null; } @Nullable