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();
}
Aggregations