Search in sources :

Example 6 with BatteryStatus

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

the class FilesSyncHelper method restartJobsIfNeeded.

public static void restartJobsIfNeeded(final UploadsStorageManager uploadsStorageManager, final UserAccountManager accountManager, final ConnectivityService connectivityService, final PowerManagementService powerManagementService) {
    final Context context = MainApp.getAppContext();
    boolean accountExists;
    boolean whileChargingOnly = true;
    boolean useWifiOnly = true;
    OCUpload[] failedUploads = uploadsStorageManager.getFailedUploads();
    for (OCUpload failedUpload : failedUploads) {
        accountExists = false;
        if (!failedUpload.isWhileChargingOnly()) {
            whileChargingOnly = false;
        }
        if (!failedUpload.isUseWifiOnly()) {
            useWifiOnly = false;
        }
        // check if accounts still exists
        for (Account account : accountManager.getAccounts()) {
            if (account.name.equals(failedUpload.getAccountName())) {
                accountExists = true;
                break;
            }
        }
        if (!accountExists) {
            uploadsStorageManager.removeUpload(failedUpload);
        }
    }
    failedUploads = uploadsStorageManager.getFailedUploads();
    if (failedUploads.length == 0) {
        // nothing to do
        return;
    }
    if (whileChargingOnly) {
        final BatteryStatus batteryStatus = powerManagementService.getBattery();
        final boolean charging = batteryStatus.isCharging() || batteryStatus.isFull();
        if (!charging) {
            // all uploads requires charging
            return;
        }
    }
    if (useWifiOnly && !connectivityService.getConnectivity().isWifi()) {
        // all uploads requires wifi
        return;
    }
    new Thread(() -> {
        if (connectivityService.getConnectivity().isConnected() && !connectivityService.isInternetWalled()) {
            FileUploader.retryFailedUploads(context, uploadsStorageManager, connectivityService, accountManager, powerManagementService);
        }
    }).start();
}
Also used : Context(android.content.Context) Account(android.accounts.Account) OCUpload(com.owncloud.android.db.OCUpload) BatteryStatus(com.nextcloud.client.device.BatteryStatus)

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