use of com.nextcloud.client.account.User in project android by nextcloud.
the class DocumentsStorageProvider method initiateStorageMap.
private void initiateStorageMap() {
rootIdToStorageManager.clear();
ContentResolver contentResolver = getContext().getContentResolver();
for (User user : accountManager.getAllUsers()) {
final FileDataStorageManager storageManager = new FileDataStorageManager(user, contentResolver);
rootIdToStorageManager.put(user.hashCode(), storageManager);
}
}
use of com.nextcloud.client.account.User in project android by nextcloud.
the class DocumentsStorageProvider method isChildDocument.
@Override
public boolean isChildDocument(String parentDocumentId, String documentId) {
Log.d(TAG, "isChildDocument(), parent=" + parentDocumentId + ", id=" + documentId);
try {
// get file for parent document
Document parentDocument = toDocument(parentDocumentId);
OCFile parentFile = parentDocument.getFile();
if (parentFile == null) {
throw new FileNotFoundException("No parent file with ID " + parentDocumentId);
}
// get file for child candidate document
Document currentDocument = toDocument(documentId);
OCFile childFile = currentDocument.getFile();
if (childFile == null) {
throw new FileNotFoundException("No child file with ID " + documentId);
}
String parentPath = parentFile.getDecryptedRemotePath();
String childPath = childFile.getDecryptedRemotePath();
// The alternative is to go up the folder hierarchy from currentDocument with getParent()
// until we arrive at parentDocument or the storage root.
// However, especially for long paths this is expensive and can take substantial time.
// The solution below uses paths and is faster by a factor of 2-10 depending on the nesting level of child.
// So far, the same document with its unique ID can never be in two places at once.
// If this assumption ever changes, this code would need to be adapted.
User parentDocumentOwner = parentDocument.getUser();
User currentDocumentOwner = currentDocument.getUser();
return parentDocumentOwner.nameEquals(currentDocumentOwner) && childPath.startsWith(parentPath);
} catch (FileNotFoundException e) {
Log.e(TAG, "failed to check for child document", e);
}
return false;
}
use of com.nextcloud.client.account.User in project android by nextcloud.
the class DocumentsStorageProvider method copyDocument.
@Override
public String copyDocument(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
Log.d(TAG, "copyDocument(), id=" + sourceDocumentId);
Document document = toDocument(sourceDocumentId);
FileDataStorageManager storageManager = document.getStorageManager();
Document targetFolder = toDocument(targetParentDocumentId);
RemoteOperationResult result = new CopyFileOperation(document.getRemotePath(), targetFolder.getRemotePath(), document.getStorageManager()).execute(document.getClient());
if (!result.isSuccess()) {
Log_OC.e(TAG, result.toString());
throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId + " to " + targetParentDocumentId);
}
Context context = getNonNullContext();
User user = document.getUser();
RemoteOperationResult updateParent = new RefreshFolderOperation(targetFolder.getFile(), System.currentTimeMillis(), false, false, true, storageManager, user, context).execute(targetFolder.getClient());
if (!updateParent.isSuccess()) {
Log_OC.e(TAG, updateParent.toString());
throw new FileNotFoundException("Failed to copy document with documentId " + sourceDocumentId + " to " + targetParentDocumentId);
}
String newPath = targetFolder.getRemotePath() + document.getFile().getFileName();
if (document.getFile().isFolder()) {
newPath = newPath + PATH_SEPARATOR;
}
Document newFile = new Document(storageManager, newPath);
context.getContentResolver().notifyChange(toNotifyUri(targetFolder), null, false);
return newFile.getDocumentId();
}
use of com.nextcloud.client.account.User in project android by nextcloud.
the class FileUploader method onStartCommand.
/**
* Entry point to add one or several files to the queue of uploads.
*
* New uploads are added calling to startService(), resulting in a call to this method. This ensures the service
* will keep on working although the caller activity goes away.
*/
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log_OC.d(TAG, "Starting command with id " + startId);
startForeground(FOREGROUND_SERVICE_ID, mNotification);
if (intent == null) {
Log_OC.e(TAG, "Intent is null");
return Service.START_NOT_STICKY;
}
if (!intent.hasExtra(KEY_ACCOUNT)) {
Log_OC.e(TAG, "Not enough information provided in intent");
return Service.START_NOT_STICKY;
}
final Account account = intent.getParcelableExtra(KEY_ACCOUNT);
if (account == null) {
return Service.START_NOT_STICKY;
}
Optional<User> optionalUser = accountManager.getUser(account.name);
if (!optionalUser.isPresent()) {
return Service.START_NOT_STICKY;
}
final User user = optionalUser.get();
boolean retry = intent.getBooleanExtra(KEY_RETRY, false);
List<String> requestedUploads = new ArrayList<>();
boolean onWifiOnly = intent.getBooleanExtra(KEY_WHILE_ON_WIFI_ONLY, false);
boolean whileChargingOnly = intent.getBooleanExtra(KEY_WHILE_CHARGING_ONLY, false);
if (!retry) {
// Start new uploads
if (!(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
Log_OC.e(TAG, "Not enough information provided in intent");
return Service.START_NOT_STICKY;
}
Integer error = gatherAndStartNewUploads(intent, user, requestedUploads, onWifiOnly, whileChargingOnly);
if (error != null) {
return error;
}
} else {
// Retry uploads
if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_RETRY_UPLOAD)) {
Log_OC.e(TAG, "Not enough information provided in intent: no KEY_RETRY_UPLOAD_KEY");
return START_NOT_STICKY;
}
retryUploads(intent, user, requestedUploads);
}
if (requestedUploads.size() > 0) {
Message msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
msg.obj = requestedUploads;
mServiceHandler.sendMessage(msg);
sendBroadcastUploadsAdded();
}
return Service.START_NOT_STICKY;
}
use of com.nextcloud.client.account.User 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);
}
}
}
Aggregations