use of com.nextcloud.client.network.Connectivity in project android by nextcloud.
the class UploadIT method testUploadOnWifiOnlyButNoWifi.
@Test
public void testUploadOnWifiOnlyButNoWifi() {
ConnectivityService connectivityServiceMock = new ConnectivityService() {
@Override
public boolean isInternetWalled() {
return false;
}
@Override
public Connectivity getConnectivity() {
return new Connectivity(true, false, false, true);
}
};
OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt", FOLDER + "noWifi.txt", account.name);
ocUpload.setUseWifiOnly(true);
UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, true, false, getStorageManager());
newUpload.setRemoteFolderToBeCreated();
newUpload.addRenameUploadListener(() -> {
// dummy
});
RemoteOperationResult result = newUpload.execute(client);
assertFalse(result.toString(), result.isSuccess());
assertEquals(RemoteOperationResult.ResultCode.DELAYED_FOR_WIFI, result.getCode());
}
use of com.nextcloud.client.network.Connectivity 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);
}
}
}
use of com.nextcloud.client.network.Connectivity 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;
}
use of com.nextcloud.client.network.Connectivity in project android by nextcloud.
the class UploadIT method testUploadOnWifiOnlyButMeteredWifi.
@Test
public void testUploadOnWifiOnlyButMeteredWifi() {
ConnectivityService connectivityServiceMock = new ConnectivityService() {
@Override
public boolean isInternetWalled() {
return false;
}
@Override
public Connectivity getConnectivity() {
return new Connectivity(true, true, true, true);
}
};
OCUpload ocUpload = new OCUpload(FileStorageUtils.getTemporalPath(account.name) + "/empty.txt", FOLDER + "noWifi.txt", account.name);
ocUpload.setUseWifiOnly(true);
UploadFileOperation newUpload = new UploadFileOperation(uploadsStorageManager, connectivityServiceMock, powerManagementServiceMock, user, null, ocUpload, NameCollisionPolicy.DEFAULT, FileUploader.LOCAL_BEHAVIOUR_COPY, targetContext, true, false, getStorageManager());
newUpload.setRemoteFolderToBeCreated();
newUpload.addRenameUploadListener(() -> {
// dummy
});
RemoteOperationResult result = newUpload.execute(client);
assertFalse(result.toString(), result.isSuccess());
assertEquals(RemoteOperationResult.ResultCode.DELAYED_FOR_WIFI, result.getCode());
}
Aggregations