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

implement mechanism with LocalBroadcastReceiver to get result from folder creation in Initializer

parent 683ea5bd
Pipeline #34174 passed with stage
in 1 minute and 50 seconds
......@@ -11,15 +11,18 @@ package foundation.e.drive.services;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import com.owncloud.android.lib.common.OwnCloudClient;
import com.owncloud.android.lib.common.operations.OnRemoteOperationListener;
......@@ -28,15 +31,18 @@ import com.owncloud.android.lib.common.operations.RemoteOperationResult;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import foundation.e.drive.backgroundServices.CreateInitialRemoteFoldersIntentService;
import foundation.e.drive.models.SyncedFolder;
import foundation.e.drive.operations.CreateInitialFolderRemoteOperation;
import foundation.e.drive.receivers.ScreenOffReceiver;
import foundation.e.drive.utils.AppConstants;
import foundation.e.drive.utils.CommonUtils;
import foundation.e.drive.utils.JobUtils;
import foundation.e.drive.utils.ServiceExceptionHandler;
import static com.owncloud.android.lib.resources.files.FileUtils.PATH_SEPARATOR;
import static foundation.e.drive.backgroundServices.CreateInitialRemoteFoldersIntentService.createFullPathKey;
import static foundation.e.drive.backgroundServices.CreateInitialRemoteFoldersIntentService.syncedFolderKey;
import static foundation.e.drive.utils.AppConstants.INITIALFOLDERS_NUMBER;
import static foundation.e.drive.utils.AppConstants.MEDIA_SYNCABLE_CATEGORIES;
import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES;
......@@ -44,7 +50,7 @@ import static foundation.e.drive.utils.AppConstants.SETTINGS_SYNCABLE_CATEGORIES
/**
* @author Vincent Bourgmayer
*/
public class InitializerService extends Service implements OnRemoteOperationListener {
public class InitializerService extends Service {
final private String TAG = InitializerService.class.getSimpleName();
//Complex properties
private int existingRemoteFolderCounter; //@dev-only; Temporarily used to know if all remotePath exist
......@@ -60,6 +66,9 @@ public class InitializerService extends Service implements OnRemoteOperationList
super.onCreate();
this.existingRemoteFolderCounter = 0;
//JobUtils.scheduleInitializerJob(getApplicationContext());
IntentFilter intentFilter = new IntentFilter(AppConstants.resultIntentActionKey);
LocalBroadcastManager.getInstance(this).registerReceiver(
folderCreationReceiver, intentFilter);
}
@Override
......@@ -110,7 +119,6 @@ public class InitializerService extends Service implements OnRemoteOperationList
/**
* start to do its job
*/
public void start() {
Log.i(TAG, "start");
if (mCloudClient == null){
......@@ -217,54 +225,41 @@ public class InitializerService extends Service implements OnRemoteOperationList
if( this.mHandler == null ) this.mHandler = new Handler();
CreateInitialFolderRemoteOperation createFolderOperation =
new CreateInitialFolderRemoteOperation(
this.mSyncedFolders.get( this.existingRemoteFolderCounter ),
true,
this);
Intent createFolderIntent = new Intent(this, CreateInitialRemoteFoldersIntentService.class);
createFolderOperation.execute(this.mCloudClient, this, this.mHandler);
createFolderIntent.putExtra(syncedFolderKey, mSyncedFolders.get(this.existingRemoteFolderCounter));
createFolderIntent.putExtra(createFullPathKey, true);
createFolderIntent.putExtra(AppConstants.ACCOUNT_KEY_CODE, mAccount);
startService(createFolderIntent);
}else if(this.existingRemoteFolderCounter == this.mSyncedFolders.size() ){
doLastStep();
}else{
Log.e(TAG, "this.existingRemoteFolderCounter : "+this.existingRemoteFolderCounter+" > this.mSyncedFolders.size() : "+this.mSyncedFolders.size() );
this.stopSelf();
}
}
@Override
public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
Log.i(TAG, "onRemoteOperationFinish()");
if(operation instanceof CreateInitialFolderRemoteOperation){
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() );
if( this.restartFolderCreationCounter < 3) {
Log.w( TAG, " restart operation" );
operation.execute( this.mCloudClient, this, this.mHandler );
this.restartFolderCreationCounter+=1;
}else{
Log.e(TAG, "Remote folder's creation failed due to conflict with server");
stopSelf();
}
public void onFolderCreationResult(String resultCode, int httpResultCode){
if(resultCode.equals(RemoteOperationResult.ResultCode.OK) || resultCode.equals(RemoteOperationResult.ResultCode.FOLDER_ALREADY_EXISTS)){
this.existingRemoteFolderCounter+=1;
CreateNextRemoteFolder();
}else if(httpResultCode == 423 || httpResultCode == 409){
if( this.restartFolderCreationCounter < 3) {
Log.w( TAG, " restart operation" );
//operation.execute( this.mCloudClient, this, this.mHandler );
this.restartFolderCreationCounter+=1;
}else{
Log.e(TAG, result.getLogMessage()+" "+result.getHttpCode() );
Log.e(TAG, "Remote folder's creation failed due to conflict with server");
stopSelf();
}
}else{
Log.e(TAG, resultCode+" "+httpResultCode );
stopSelf();
}
}
/**
* Function to check if all remote folder have been created
**/
......@@ -308,4 +303,18 @@ public class InitializerService extends Service implements OnRemoteOperationList
public IBinder onBind(Intent intent) {
return null;
}
private BroadcastReceiver folderCreationReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras;
if(intent == null || (extras = intent.getExtras()) == null) {
return;
}
String resultCode = extras.getString(AppConstants.resultCodeKey);
int httpResultCode = extras.getInt(AppConstants.resultIntentHttpCodeKey);
onFolderCreationResult(resultCode, httpResultCode);
}
};
}
\ No newline at end of file
......@@ -14,17 +14,26 @@ package foundation.e.drive.utils;
//Contains some constant value
public abstract class AppConstants {
public static final String MEDIASYNC_PROVIDER_AUTHORITY ="foundation.e.drive.providers.MediasSyncProvider";
public static final String SETTINGSYNC_PROVIDER_AUTHORITY ="foundation.e.drive.providers.SettingsSyncProvider";
public static final String MEDIASYNC_PROVIDER_AUTHORITY = "foundation.e.drive.providers.MediasSyncProvider";
public static final String SETTINGSYNC_PROVIDER_AUTHORITY = "foundation.e.drive.providers.SettingsSyncProvider";
public static final String INITIALIZATION_HAS_BEEN_DONE ="initService_has_run";
public static final String INITIALFOLDERS_NUMBER="initial_folder_number";
public static final String INITIALIZATION_HAS_BEEN_DONE = "initService_has_run";
public static final String INITIALFOLDERS_NUMBER = "initial_folder_number";
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 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 KEY_OMS_IS_WORKING = "OMS_is_working";
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"};
public static final String[] SETTINGS_SYNCABLE_CATEGORIES = new String[]{"Rom settings"};
//Initializer
public static final String resultCodeKey = "result_code";
public static final String resultIntentActionKey = "CreateInitialFolderResult";
public static final String resultIntentHttpCodeKey = "result_http_code";
public static final String ACCOUNT_KEY_CODE = "account";
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment