Search in sources :

Example 1 with OCCapability

use of com.owncloud.android.domain.capabilities.model.OCCapability in project android by owncloud.

the class FileUploader method onStartCommand.

/**
 * Entry point to add one or several files to the queue of uploads.
 * <p>
 * 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) {
    Timber.d("Starting command with id %s", startId);
    int createdBy = intent.getIntExtra(KEY_CREATED_BY, UploadFileOperation.CREATED_BY_USER);
    boolean isCameraUploadFile = createdBy == CREATED_AS_CAMERA_UPLOAD_PICTURE || createdBy == CREATED_AS_CAMERA_UPLOAD_VIDEO;
    boolean isAvailableOfflineFile = intent.getBooleanExtra(KEY_IS_AVAILABLE_OFFLINE_FILE, false);
    boolean isRequestedFromWifiBackEvent = intent.getBooleanExtra(KEY_REQUESTED_FROM_WIFI_BACK_EVENT, false);
    if ((isCameraUploadFile || isAvailableOfflineFile || isRequestedFromWifiBackEvent) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        Timber.d("Starting FileUploader service in foreground");
        if (isCameraUploadFile) {
            mNotificationBuilder.setContentTitle(getString(R.string.uploader_upload_camera_upload_files));
        } else if (isAvailableOfflineFile) {
            mNotificationBuilder.setContentTitle(getString(R.string.uploader_upload_available_offline_files));
        } else if (isRequestedFromWifiBackEvent) {
            mNotificationBuilder.setContentTitle(getString(R.string.uploader_upload_requested_from_wifi_files));
        }
        /*
             * After calling startForegroundService method from {@link TransferRequester} for camera uploads or
             * available offline, we have to call this within five seconds after the service is created to avoid
             * an error
             */
        startForeground(141, mNotificationBuilder.build());
    }
    boolean retry = intent.getBooleanExtra(KEY_RETRY, false);
    AbstractList<String> requestedUploads = new Vector<>();
    if (!intent.hasExtra(KEY_ACCOUNT)) {
        Timber.e("Not enough information provided in intent");
        return Service.START_NOT_STICKY;
    }
    Account account = intent.getParcelableExtra(KEY_ACCOUNT);
    Timber.d("Account to upload the file to: %s", account);
    if (account == null || !AccountUtils.exists(account.name, getApplicationContext())) {
        return Service.START_NOT_STICKY;
    }
    if (!retry) {
        if (!(intent.hasExtra(KEY_LOCAL_FILE) || intent.hasExtra(KEY_FILE))) {
            Timber.e("Not enough information provided in intent");
            return Service.START_NOT_STICKY;
        }
        String[] localPaths = null, remotePaths = null, mimeTypes = null;
        OCFile[] files = null;
        if (intent.hasExtra(KEY_FILE)) {
            Parcelable[] files_temp = intent.getParcelableArrayExtra(KEY_FILE);
            files = new OCFile[files_temp.length];
            System.arraycopy(files_temp, 0, files, 0, files_temp.length);
        } else {
            localPaths = intent.getStringArrayExtra(KEY_LOCAL_FILE);
            remotePaths = intent.getStringArrayExtra(KEY_REMOTE_FILE);
            mimeTypes = intent.getStringArrayExtra(KEY_MIME_TYPE);
        }
        boolean forceOverwrite = intent.getBooleanExtra(KEY_FORCE_OVERWRITE, false);
        int localAction = intent.getIntExtra(KEY_LOCAL_BEHAVIOUR, LOCAL_BEHAVIOUR_FORGET);
        boolean isCreateRemoteFolder = intent.getBooleanExtra(KEY_CREATE_REMOTE_FOLDER, false);
        if (intent.hasExtra(KEY_FILE) && files == null) {
            Timber.e("Incorrect array for OCFiles provided in upload intent");
            return Service.START_NOT_STICKY;
        } else if (!intent.hasExtra(KEY_FILE)) {
            if (localPaths == null) {
                Timber.e("Incorrect array for local paths provided in upload intent");
                return Service.START_NOT_STICKY;
            }
            if (remotePaths == null) {
                Timber.e("Incorrect array for remote paths provided in upload intent");
                return Service.START_NOT_STICKY;
            }
            if (localPaths.length != remotePaths.length) {
                Timber.e("Different number of remote paths and local paths!");
                return Service.START_NOT_STICKY;
            }
            files = new OCFile[localPaths.length];
            for (int i = 0; i < localPaths.length; i++) {
                files[i] = UploadFileOperation.obtainNewOCFileToUpload(remotePaths[i], localPaths[i], ((mimeTypes != null) ? mimeTypes[i] : null), getApplicationContext());
                if (files[i] == null) {
                    Timber.e("obtainNewOCFileToUpload() returned null for remotePaths[i]:" + remotePaths[i] + " and localPaths[i]:" + localPaths[i]);
                    return Service.START_NOT_STICKY;
                }
            }
        }
        // at this point variable "OCFile[] files" is loaded correctly.
        String uploadKey;
        UploadFileOperation newUploadFileOperation;
        try {
            FileDataStorageManager storageManager = new FileDataStorageManager(getApplicationContext(), account, getContentResolver());
            OCCapability capabilitiesForAccount = storageManager.getCapability(account.name);
            boolean isChunkingAllowed = capabilitiesForAccount != null && capabilitiesForAccount.isChunkingAllowed();
            Timber.d("Chunking is allowed: %s", isChunkingAllowed);
            for (OCFile ocFile : files) {
                OCUpload ocUpload = new OCUpload(ocFile, account);
                ocUpload.setFileSize(ocFile.getFileLength());
                ocUpload.setForceOverwrite(forceOverwrite);
                ocUpload.setCreateRemoteFolder(isCreateRemoteFolder);
                ocUpload.setCreatedBy(createdBy);
                ocUpload.setLocalAction(localAction);
                /*ocUpload.setUseWifiOnly(isUseWifiOnly);
                    ocUpload.setWhileChargingOnly(isWhileChargingOnly);*/
                ocUpload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
                if (new File(ocFile.getStoragePath()).length() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE && isChunkingAllowed) {
                    ocUpload.setTransferId(SecurityUtils.stringToMD5Hash(ocFile.getRemotePath()) + System.currentTimeMillis());
                    newUploadFileOperation = new ChunkedUploadFileOperation(account, ocFile, ocUpload, forceOverwrite, localAction, this);
                } else {
                    newUploadFileOperation = new UploadFileOperation(account, ocFile, ocUpload, forceOverwrite, localAction, this);
                }
                newUploadFileOperation.setCreatedBy(createdBy);
                if (isCreateRemoteFolder) {
                    newUploadFileOperation.setRemoteFolderToBeCreated();
                }
                newUploadFileOperation.addDatatransferProgressListener(this);
                newUploadFileOperation.addDatatransferProgressListener((FileUploaderBinder) mBinder);
                newUploadFileOperation.addRenameUploadListener(this);
                Pair<String, String> putResult = mPendingUploads.putIfAbsent(account.name, ocFile.getRemotePath(), newUploadFileOperation);
                if (putResult != null) {
                    uploadKey = putResult.first;
                    requestedUploads.add(uploadKey);
                    // Save upload in database
                    long id = mUploadsStorageManager.storeUpload(ocUpload);
                    newUploadFileOperation.setOCUploadId(id);
                }
            }
        } catch (IllegalArgumentException e) {
            Timber.e(e, "Not enough information provided in intent: %s", e.getMessage());
            return START_NOT_STICKY;
        } catch (IllegalStateException e) {
            Timber.e(e, "Bad information provided in intent: %s", e.getMessage());
            return START_NOT_STICKY;
        } catch (Exception e) {
            Timber.e(e, "Unexpected exception while processing upload intent");
            return START_NOT_STICKY;
        }
    // *** TODO REWRITE: block inserted to request A retry; too many code copied, no control exception ***/
    } else {
        if (!intent.hasExtra(KEY_ACCOUNT) || !intent.hasExtra(KEY_RETRY_UPLOAD)) {
            Timber.e("Not enough information provided in intent: no KEY_RETRY_UPLOAD_KEY");
            return START_NOT_STICKY;
        }
        OCUpload upload = intent.getParcelableExtra(KEY_RETRY_UPLOAD);
        UploadFileOperation newUploadFileOperation;
        if (upload.getFileSize() > ChunkedUploadRemoteFileOperation.CHUNK_SIZE) {
            upload.setTransferId(SecurityUtils.stringToMD5Hash(upload.getRemotePath()) + System.currentTimeMillis());
            newUploadFileOperation = new ChunkedUploadFileOperation(account, null, upload, upload.isForceOverwrite(), upload.getLocalAction(), this);
        } else {
            newUploadFileOperation = new UploadFileOperation(account, null, upload, upload.isForceOverwrite(), upload.getLocalAction(), this);
        }
        newUploadFileOperation.addDatatransferProgressListener(this);
        newUploadFileOperation.addDatatransferProgressListener((FileUploaderBinder) mBinder);
        newUploadFileOperation.addRenameUploadListener(this);
        Pair<String, String> putResult = mPendingUploads.putIfAbsent(account.name, upload.getRemotePath(), newUploadFileOperation);
        if (putResult != null) {
            String uploadKey = putResult.first;
            requestedUploads.add(uploadKey);
            // Update upload in database
            upload.setUploadStatus(UploadStatus.UPLOAD_IN_PROGRESS);
            mUploadsStorageManager.updateUpload(upload);
        }
    }
    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) OCCapability(com.owncloud.android.domain.capabilities.model.OCCapability) Message(android.os.Message) Parcelable(android.os.Parcelable) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation) ChunkedUploadFileOperation(com.owncloud.android.operations.ChunkedUploadFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) OCUpload(com.owncloud.android.datamodel.OCUpload) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ChunkedUploadFileOperation(com.owncloud.android.operations.ChunkedUploadFileOperation) Vector(java.util.Vector) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 2 with OCCapability

use of com.owncloud.android.domain.capabilities.model.OCCapability in project android by owncloud.

the class AccountUtils method getServerVersion.

/**
 * Access the version of the OC server corresponding to an account SAVED IN THE ACCOUNTMANAGER
 *
 * @param   account     ownCloud account
 * @return Version of the OC server corresponding to account, according to the data saved
 *                      in the system AccountManager
 */
@Nullable
public static OwnCloudVersion getServerVersion(Account account) {
    OwnCloudVersion serverVersion = null;
    if (account != null) {
        // capabilities are now the preferred source for version info
        FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(MainApp.Companion.getAppContext(), account, MainApp.Companion.getAppContext().getContentResolver());
        OCCapability capability = fileDataStorageManager.getCapability(account.name);
        if (capability != null) {
            serverVersion = new OwnCloudVersion(capability.getVersionString());
        } else {
            // legacy: AccountManager as source of version info
            AccountManager accountMgr = AccountManager.get(MainApp.Companion.getAppContext());
            String serverVersionStr = accountMgr.getUserData(account, Constants.KEY_OC_VERSION);
            if (serverVersionStr != null) {
                serverVersion = new OwnCloudVersion(serverVersionStr);
            }
        }
    }
    return serverVersion;
}
Also used : OCCapability(com.owncloud.android.domain.capabilities.model.OCCapability) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) AccountManager(android.accounts.AccountManager) OwnCloudVersion(com.owncloud.android.lib.resources.status.OwnCloudVersion) Nullable(androidx.annotation.Nullable)

Example 3 with OCCapability

use of com.owncloud.android.domain.capabilities.model.OCCapability in project android by owncloud.

the class FileMenuFilter method filter.

/**
 * Performs the real filtering, to be applied in the {@link Menu} by the caller methods.
 * <p>
 * Decides what actions must be shown and hidden.
 *
 * @param toShow List to save the options that must be shown in the menu.
 * @param toHide List to save the options that must be shown in the menu.
 */
private void filter(List<Integer> toShow, List<Integer> toHide, boolean displaySelectAll, boolean displaySelectInverse, boolean onlyAvailableOffline, boolean sharedByLinkFiles) {
    boolean synchronizing = anyFileSynchronizing();
    boolean videoPreviewing = anyFileVideoPreviewing();
    boolean videoStreaming = !anyFileDown() && anyFileVideoPreviewing();
    if (displaySelectAll) {
        toShow.add(R.id.file_action_select_all);
    } else {
        toHide.add(R.id.file_action_select_all);
    }
    if (displaySelectInverse) {
        toShow.add(R.id.action_select_inverse);
    } else {
        toHide.add(R.id.action_select_inverse);
    }
    // DOWNLOAD
    if (mFiles.isEmpty() || containsFolder() || anyFileDown() || synchronizing || videoPreviewing || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_download_file);
    } else {
        toShow.add(R.id.action_download_file);
    }
    // RENAME
    if (!isSingleSelection() || synchronizing || videoPreviewing || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_rename_file);
    } else {
        toShow.add(R.id.action_rename_file);
    }
    // MOVE & COPY
    if (mFiles.isEmpty() || synchronizing || videoPreviewing || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_move);
        toHide.add(R.id.action_copy);
    } else {
        toShow.add(R.id.action_move);
        toShow.add(R.id.action_copy);
    }
    // REMOVE
    if (mFiles.isEmpty() || synchronizing || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_remove_file);
    } else {
        toShow.add(R.id.action_remove_file);
    }
    // OPEN WITH (different to preview!)
    if (!isSingleFile() || !anyFileDown() || synchronizing) {
        toHide.add(R.id.action_open_file_with);
    } else {
        toShow.add(R.id.action_open_file_with);
    }
    // CANCEL SYNCHRONIZATION
    if (mFiles.isEmpty() || !synchronizing || anyFavorite() || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_cancel_sync);
    } else {
        toShow.add(R.id.action_cancel_sync);
    }
    // SYNC CONTENTS (BOTH FILE AND FOLDER)
    if (mFiles.isEmpty() || (!anyFileDown() && !containsFolder()) || synchronizing || onlyAvailableOffline || sharedByLinkFiles) {
        toHide.add(R.id.action_sync_file);
    } else {
        toShow.add(R.id.action_sync_file);
    }
    // SHARE FILE
    boolean shareViaLinkAllowed = (mContext != null && mContext.getResources().getBoolean(R.bool.share_via_link_feature));
    boolean shareWithUsersAllowed = (mContext != null && mContext.getResources().getBoolean(R.bool.share_with_users_feature));
    OCCapability capability = mComponentsGetter.getStorageManager().getCapability(mAccount.name);
    boolean notAllowResharing = anyFileSharedWithMe() && capability != null && capability.getFilesSharingResharing().isFalse();
    if ((!shareViaLinkAllowed && !shareWithUsersAllowed) || !isSingleSelection() || notAllowResharing || onlyAvailableOffline) {
        toHide.add(R.id.action_share_file);
    } else {
        toShow.add(R.id.action_share_file);
    }
    // SEE DETAILS
    if (!isSingleFile()) {
        toHide.add(R.id.action_see_details);
    } else {
        toShow.add(R.id.action_see_details);
    }
    // SEND
    boolean sendAllowed = (mContext != null && mContext.getString(R.string.send_files_to_other_apps).equalsIgnoreCase("on"));
    if (!isSingleFile() || !sendAllowed || synchronizing || videoStreaming || onlyAvailableOffline) {
        toHide.add(R.id.action_send_file);
    } else {
        toShow.add(R.id.action_send_file);
    }
    // SET AS AVAILABLE OFFLINE
    if (synchronizing || !anyUnfavorite() || videoStreaming) {
        toHide.add(R.id.action_set_available_offline);
    } else {
        toShow.add(R.id.action_set_available_offline);
    }
    // UNSET AS AVAILABLE OFFLINE
    if (!anyFavorite() || videoStreaming) {
        toHide.add(R.id.action_unset_available_offline);
    } else {
        toShow.add(R.id.action_unset_available_offline);
    }
}
Also used : OCCapability(com.owncloud.android.domain.capabilities.model.OCCapability)

Aggregations

OCCapability (com.owncloud.android.domain.capabilities.model.OCCapability)3 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)2 Account (android.accounts.Account)1 AccountManager (android.accounts.AccountManager)1 Message (android.os.Message)1 Parcelable (android.os.Parcelable)1 Nullable (androidx.annotation.Nullable)1 OCFile (com.owncloud.android.datamodel.OCFile)1 OCUpload (com.owncloud.android.datamodel.OCUpload)1 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)1 OwnCloudVersion (com.owncloud.android.lib.resources.status.OwnCloudVersion)1 ChunkedUploadFileOperation (com.owncloud.android.operations.ChunkedUploadFileOperation)1 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)1 File (java.io.File)1 Vector (java.util.Vector)1