Search in sources :

Example 31 with User

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);
    }
}
Also used : User(com.nextcloud.client.account.User) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ContentResolver(android.content.ContentResolver)

Example 32 with User

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;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) User(com.nextcloud.client.account.User) FileNotFoundException(java.io.FileNotFoundException)

Example 33 with User

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();
}
Also used : Context(android.content.Context) User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RefreshFolderOperation(com.owncloud.android.operations.RefreshFolderOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) FileNotFoundException(java.io.FileNotFoundException) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation)

Example 34 with User

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;
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) User(com.nextcloud.client.account.User) Message(android.os.Message) ArrayList(java.util.ArrayList)

Example 35 with User

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

Aggregations

User (com.nextcloud.client.account.User)84 OCFile (com.owncloud.android.datamodel.OCFile)21 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)19 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)19 Intent (android.content.Intent)14 Account (android.accounts.Account)12 ArrayList (java.util.ArrayList)12 Context (android.content.Context)9 Fragment (androidx.fragment.app.Fragment)9 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)9 File (java.io.File)9 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)7 Uri (android.net.Uri)6 Bundle (android.os.Bundle)6 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)6 GalleryFragment (com.owncloud.android.ui.fragment.GalleryFragment)6 PreviewTextFileFragment (com.owncloud.android.ui.preview.PreviewTextFileFragment)6 PreviewTextFragment (com.owncloud.android.ui.preview.PreviewTextFragment)6 PreviewTextStringFragment (com.owncloud.android.ui.preview.PreviewTextStringFragment)6 View (android.view.View)5