Search in sources :

Example 1 with BatteryStatus

use of com.nextcloud.client.device.BatteryStatus in project android by nextcloud.

the class AbstractIT method uploadOCUpload.

public void uploadOCUpload(OCUpload ocUpload) {
    ConnectivityService connectivityServiceMock = new ConnectivityService() {

        @Override
        public boolean isInternetWalled() {
            return false;
        }

        @Override
        public Connectivity getConnectivity() {
            return Connectivity.CONNECTED_WIFI;
        }
    };
    PowerManagementService powerManagementServiceMock = new PowerManagementService() {

        @NonNull
        @Override
        public BatteryStatus getBattery() {
            return new BatteryStatus();
        }

        @Override
        public boolean isPowerSavingEnabled() {
            return false;
        }

        @Override
        public boolean isPowerSavingExclusionAvailable() {
            return false;
        }
    };
    UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
    UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(accountManager, targetContext.getContentResolver());
    UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, false, false, getStorageManager());
    newUpload.addRenameUploadListener(() -> {
    // dummy
    });
    newUpload.setRemoteFolderToBeCreated();
    RemoteOperationResult result = newUpload.execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) ConnectivityService(com.nextcloud.client.network.ConnectivityService) PowerManagementService(com.nextcloud.client.device.PowerManagementService) BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 2 with BatteryStatus

use of com.nextcloud.client.device.BatteryStatus in project android by nextcloud.

the class AbstractOnServerIT method uploadOCUpload.

public void uploadOCUpload(OCUpload ocUpload, int localBehaviour) {
    ConnectivityService connectivityServiceMock = new ConnectivityService() {

        @Override
        public boolean isInternetWalled() {
            return false;
        }

        @Override
        public Connectivity getConnectivity() {
            return Connectivity.CONNECTED_WIFI;
        }
    };
    PowerManagementService powerManagementServiceMock = new PowerManagementService() {

        @NonNull
        @Override
        public BatteryStatus getBattery() {
            return new BatteryStatus();
        }

        @Override
        public boolean isPowerSavingEnabled() {
            return false;
        }

        @Override
        public boolean isPowerSavingExclusionAvailable() {
            return false;
        }
    };
    UserAccountManager accountManager = UserAccountManagerImpl.fromContext(targetContext);
    UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(accountManager, targetContext.getContentResolver());
    UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, localBehaviour, targetContext, false, false, getStorageManager());
    newUpload.addRenameUploadListener(() -> {
    // dummy
    });
    newUpload.setRemoteFolderToBeCreated();
    RemoteOperationResult result = newUpload.execute(client);
    assertTrue(result.getLogMessage(), result.isSuccess());
    OCFile parentFolder = getStorageManager().getFileByEncryptedRemotePath(new File(ocUpload.getRemotePath()).getParent() + "/");
    String uploadedFileName = new File(ocUpload.getRemotePath()).getName();
    OCFile uploadedFile = getStorageManager().getFileByDecryptedRemotePath(parentFolder.getDecryptedRemotePath() + uploadedFileName);
    assertNotNull(uploadedFile.getRemoteId());
    if (localBehaviour == FileUploader.LOCAL_BEHAVIOUR_COPY || localBehaviour == FileUploader.LOCAL_BEHAVIOUR_MOVE) {
        assertTrue(new File(uploadedFile.getStoragePath()).exists());
    }
}
Also used : UserAccountManager(com.nextcloud.client.account.UserAccountManager) OCFile(com.owncloud.android.datamodel.OCFile) ConnectivityService(com.nextcloud.client.network.ConnectivityService) PowerManagementService(com.nextcloud.client.device.PowerManagementService) BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) RemoteFile(com.owncloud.android.lib.resources.files.model.RemoteFile) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 3 with BatteryStatus

use of com.nextcloud.client.device.BatteryStatus in project android by nextcloud.

the class UploadFileOperation method checkConditions.

private RemoteOperationResult checkConditions(File originalFile) {
    RemoteOperationResult remoteOperationResult = null;
    // check that connectivity conditions are met and delays the upload otherwise
    Connectivity connectivity = connectivityService.getConnectivity();
    if (mOnWifiOnly && (!connectivity.isWifi() || connectivity.isMetered())) {
        Log_OC.d(TAG, "Upload delayed until WiFi is available: " + getRemotePath());
        remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_FOR_WIFI);
    }
    // check if charging conditions are met and delays the upload otherwise
    final BatteryStatus battery = powerManagementService.getBattery();
    if (mWhileChargingOnly && !battery.isCharging()) {
        Log_OC.d(TAG, "Upload delayed until the device is charging: " + getRemotePath());
        remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_FOR_CHARGING);
    }
    // check that device is not in power save mode
    if (!mIgnoringPowerSaveMode && powerManagementService.isPowerSavingEnabled()) {
        Log_OC.d(TAG, "Upload delayed because device is in power save mode: " + getRemotePath());
        remoteOperationResult = new RemoteOperationResult(ResultCode.DELAYED_IN_POWER_SAVE_MODE);
    }
    // check if the file continues existing before schedule the operation
    if (!originalFile.exists()) {
        Log_OC.d(TAG, mOriginalStoragePath + " not exists anymore");
        remoteOperationResult = new RemoteOperationResult(ResultCode.LOCAL_FILE_NOT_FOUND);
    }
    // check that internet is not behind walled garden
    if (!connectivityService.getConnectivity().isConnected() || connectivityService.isInternetWalled()) {
        remoteOperationResult = new RemoteOperationResult(ResultCode.NO_NETWORK_CONNECTION);
    }
    return remoteOperationResult;
}
Also used : BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Connectivity(com.nextcloud.client.network.Connectivity)

Example 4 with BatteryStatus

use of com.nextcloud.client.device.BatteryStatus in project android by nextcloud.

the class FileUploader method retryFailedUploads.

/**
 * Retry a subset of all the stored failed uploads.
 */
public static void retryFailedUploads(@NonNull final Context context, @NonNull final UploadsStorageManager uploadsStorageManager, @NonNull final ConnectivityService connectivityService, @NonNull final UserAccountManager accountManager, @NonNull final PowerManagementService powerManagementService) {
    OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
    if (failedUploads == null || failedUploads.length == 0) {
        // nothing to do
        return;
    }
    final Connectivity connectivity = connectivityService.getConnectivity();
    final boolean gotNetwork = connectivity.isConnected() && !connectivityService.isInternetWalled();
    final boolean gotWifi = connectivity.isWifi();
    final BatteryStatus batteryStatus = powerManagementService.getBattery();
    final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
    final boolean isPowerSaving = powerManagementService.isPowerSavingEnabled();
    Optional<User> uploadUser = Optional.empty();
    for (OCUpload failedUpload : failedUploads) {
        // 1. extract failed upload owner account and cache it between loops (expensive query)
        if (!uploadUser.isPresent() || !uploadUser.get().nameEquals(failedUpload.getAccountName())) {
            uploadUser = accountManager.getUser(failedUpload.getAccountName());
        }
        final boolean isDeleted = !new File(failedUpload.getLocalPath()).exists();
        if (isDeleted) {
            // 2A. for deleted files, mark as permanently failed
            if (failedUpload.getLastResult() != UploadResult.FILE_NOT_FOUND) {
                failedUpload.setLastResult(UploadResult.FILE_NOT_FOUND);
                uploadsStorageManager.updateUpload(failedUpload);
            }
        } else if (!isPowerSaving && gotNetwork && canUploadBeRetried(failedUpload, gotWifi, charging)) {
            // 2B. for existing local files, try restarting it if possible
            retryUpload(context, uploadUser.get(), failedUpload);
        }
    }
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) User(com.nextcloud.client.account.User) BatteryStatus(com.nextcloud.client.device.BatteryStatus) Connectivity(com.nextcloud.client.network.Connectivity) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 5 with BatteryStatus

use of com.nextcloud.client.device.BatteryStatus in project android by nextcloud.

the class UploadIT method testUploadOnChargingOnlyAndCharging.

@Test
public void testUploadOnChargingOnlyAndCharging() {
    PowerManagementService powerManagementServiceMock = new PowerManagementService() {

        @Override
        public boolean isPowerSavingEnabled() {
            return false;
        }

        @Override
        public boolean isPowerSavingExclusionAvailable() {
            return false;
        }

        @NonNull
        @Override
        public BatteryStatus getBattery() {
            return new BatteryStatus(true, 100);
        }
    };
    OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt", FOLDER + "charging.txt", account.name);
    ocUpload.setWhileChargingOnly(true);
    UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, false, true, getStorageManager());
    newUpload.setRemoteFolderToBeCreated();
    newUpload.addRenameUploadListener(() -> {
    // dummy
    });
    RemoteOperationResult result = newUpload.execute(client);
    assertTrue(result.toString(), result.isSuccess());
}
Also used : OCUpload(com.owncloud.android.db.OCUpload) PowerManagementService(com.nextcloud.client.device.PowerManagementService) BatteryStatus(com.nextcloud.client.device.BatteryStatus) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) Test(org.junit.Test)

Aggregations

BatteryStatus (com.nextcloud.client.device.BatteryStatus)6 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)4 PowerManagementService (com.nextcloud.client.device.PowerManagementService)3 OCUpload (com.owncloud.android.db.OCUpload)3 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)3 UserAccountManager (com.nextcloud.client.account.UserAccountManager)2 Connectivity (com.nextcloud.client.network.Connectivity)2 ConnectivityService (com.nextcloud.client.network.ConnectivityService)2 OCFile (com.owncloud.android.datamodel.OCFile)2 UploadsStorageManager (com.owncloud.android.datamodel.UploadsStorageManager)2 File (java.io.File)2 Account (android.accounts.Account)1 Context (android.content.Context)1 User (com.nextcloud.client.account.User)1 RemoteFile (com.owncloud.android.lib.resources.files.model.RemoteFile)1 Test (org.junit.Test)1