Search in sources :

Example 6 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by owncloud.

the class GetShareWithUsersAsyncTask method doInBackground.

@Override
protected Pair<RemoteOperation, RemoteOperationResult> doInBackground(Object... params) {
    GetSharesForFileOperation operation = null;
    RemoteOperationResult result = null;
    if (params != null && params.length == 3) {
        OCFile file = (OCFile) params[0];
        Account account = (Account) params[1];
        FileDataStorageManager fileDataStorageManager = (FileDataStorageManager) params[2];
        try {
            // Get shares request
            operation = new GetSharesForFileOperation(file.getRemotePath(), false, false);
            OwnCloudAccount ocAccount = new OwnCloudAccount(account, MainApp.getAppContext());
            OwnCloudClient client = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, MainApp.getAppContext());
            result = operation.execute(client, fileDataStorageManager);
        } catch (Exception e) {
            result = new RemoteOperationResult(e);
            Log_OC.e(TAG, "Exception while getting shares", e);
        }
    } else {
        result = new RemoteOperationResult(RemoteOperationResult.ResultCode.UNKNOWN_ERROR);
    }
    return new Pair(operation, result);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) GetSharesForFileOperation(com.owncloud.android.operations.GetSharesForFileOperation) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) Pair(android.util.Pair)

Example 7 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by owncloud.

the class FileUploader method uploadFile.

/**
     * Core upload method: sends the file(s) to upload
     *
     * @param uploadKey Key to access the upload to perform, contained in mPendingUploads
     */
public void uploadFile(String uploadKey) {
    mCurrentUpload = mPendingUploads.get(uploadKey);
    if (mCurrentUpload != null) {
        /// Check account existence
        if (!AccountUtils.exists(mCurrentUpload.getAccount(), this)) {
            Log_OC.w(TAG, "Account " + mCurrentUpload.getAccount().name + " does not exist anymore -> cancelling all its uploads");
            cancelUploadsForAccount(mCurrentUpload.getAccount());
            return;
        }
        /// OK, let's upload
        mUploadsStorageManager.updateDatabaseUploadStart(mCurrentUpload);
        notifyUploadStart(mCurrentUpload);
        sendBroadcastUploadStarted(mCurrentUpload);
        RemoteOperationResult uploadResult = null;
        try {
            /// prepare client object to send the request to the ownCloud server
            if (mCurrentAccount == null || !mCurrentAccount.equals(mCurrentUpload.getAccount())) {
                mCurrentAccount = mCurrentUpload.getAccount();
                mStorageManager = new FileDataStorageManager(mCurrentAccount, getContentResolver());
            }
            // else, reuse storage manager from previous operation
            // always get client from client manager, to get fresh credentials in case of update
            OwnCloudAccount ocAccount = new OwnCloudAccount(mCurrentAccount, this);
            mUploadClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, this);
            /// perform the upload
            uploadResult = mCurrentUpload.execute(mUploadClient, mStorageManager);
        } catch (Exception e) {
            Log_OC.e(TAG, "Error uploading", e);
            uploadResult = new RemoteOperationResult(e);
        } finally {
            Pair<UploadFileOperation, String> removeResult;
            if (mCurrentUpload.wasRenamed()) {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getOldFile().getRemotePath());
            /** TODO: grant that name is also updated for mCurrentUpload.getOCUploadId */
            } else {
                removeResult = mPendingUploads.removePayload(mCurrentAccount.name, mCurrentUpload.getRemotePath());
            }
            mUploadsStorageManager.updateDatabaseUploadResult(uploadResult, mCurrentUpload);
            /// notify result
            notifyUploadResult(mCurrentUpload, uploadResult);
            sendBroadcastUploadFinished(mCurrentUpload, uploadResult, removeResult.second);
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UploadFileOperation(com.owncloud.android.operations.UploadFileOperation)

Example 8 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by owncloud.

the class SynchronizeFolderOperation method mergeRemoteFolder.

/**
     *  Synchronizes the data retrieved from the server about the contents of the target folder
     *  with the current data in the local database.
     *
     *  Grants that mFoldersToVisit is updated with fresh data after execution.
     *
     *  @param folderAndFiles   Remote folder and children files in folder
     */
private void mergeRemoteFolder(ArrayList<Object> folderAndFiles) throws OperationCancelledException {
    Log_OC.d(TAG, "Synchronizing " + mAccount.name + mRemotePath);
    FileDataStorageManager storageManager = getStorageManager();
    // parse data from remote folder
    OCFile updatedFolder = FileStorageUtils.createOCFileFrom((RemoteFile) folderAndFiles.get(0));
    // NOTE: updates ETag with remote value; that's INTENDED
    updatedFolder.copyLocalPropertiesFrom(mLocalFolder);
    Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
    List<OCFile> updatedFiles = new Vector<>(folderAndFiles.size() - 1);
    mFoldersToVisit = new Vector<>(folderAndFiles.size() - 1);
    mFilesToSyncContents.clear();
    if (mCancellationRequested.get()) {
        throw new OperationCancelledException();
    }
    // get current data about local contents of the folder to synchronize
    List<OCFile> localFiles = storageManager.getFolderContent(mLocalFolder);
    Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
    for (OCFile file : localFiles) {
        localFilesMap.put(file.getRemotePath(), file);
    }
    // loop to synchronize every child
    OCFile remoteFile, localFile, updatedLocalFile;
    RemoteFile r;
    int foldersToExpand = 0;
    for (int i = 1; i < folderAndFiles.size(); i++) {
        /// new OCFile instance with the data from the server
        r = (RemoteFile) folderAndFiles.get(i);
        remoteFile = FileStorageUtils.createOCFileFrom(r);
        /// new OCFile instance to merge fresh data from server with local state
        updatedLocalFile = FileStorageUtils.createOCFileFrom(r);
        /// retrieve local data for the read file
        localFile = localFilesMap.remove(remoteFile.getRemotePath());
        /// add to updatedFile data about LOCAL STATE (not existing in server)
        updatedLocalFile.setLastSyncDateForProperties(mCurrentSyncTime);
        if (localFile != null) {
            updatedLocalFile.copyLocalPropertiesFrom(localFile);
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag(localFile.getEtag());
            if (!updatedLocalFile.isFolder() && remoteFile.isImage() && remoteFile.getModificationTimestamp() != localFile.getModificationTimestamp()) {
                updatedLocalFile.setNeedsUpdateThumbnail(true);
            }
        } else {
            updatedLocalFile.setParentId(mLocalFolder.getFileId());
            // remote eTag will not be set unless file CONTENTS are synchronized
            updatedLocalFile.setEtag("");
            // new files need to check av-off status of parent folder!
            if (updatedFolder.isAvailableOffline()) {
                updatedLocalFile.setAvailableOfflineStatus(OCFile.AvailableOfflineStatus.AVAILABLE_OFFLINE_PARENT);
            }
        }
        /// check and fix, if needed, local storage path
        searchForLocalFileInDefaultPath(updatedLocalFile);
        /// prepare content synchronizations
        boolean serverUnchanged = addToSyncContents(updatedLocalFile, remoteFile);
        if (updatedLocalFile.isFolder() && !serverUnchanged) {
            foldersToExpand++;
        }
        updatedFiles.add(updatedLocalFile);
    }
    // save updated contents in local database
    if (foldersToExpand == 0) {
        updatedFolder.setTreeEtag(updatedFolder.getEtag());
    // TODO - propagate up
    }
    storageManager.saveFolder(updatedFolder, updatedFiles, localFilesMap.values());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) HashMap(java.util.HashMap) OperationCancelledException(com.owncloud.android.lib.common.operations.OperationCancelledException) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Vector(java.util.Vector) RemoteFile(com.owncloud.android.lib.resources.files.RemoteFile)

Example 9 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by owncloud.

the class UsersAndGroupsSearchProvider method searchForUsersOrGroups.

private Cursor searchForUsersOrGroups(Uri uri) {
    MatrixCursor response = null;
    String userQuery = uri.getLastPathSegment().toLowerCase();
    /// need to trust on the AccountUtils to get the current account since the query in the client side is not
    /// directly started by our code, but from SearchView implementation
    Account account = AccountUtils.getCurrentOwnCloudAccount(getContext());
    /// request to the OC server about users and groups matching userQuery
    GetRemoteShareesOperation searchRequest = new GetRemoteShareesOperation(userQuery, REQUESTED_PAGE, RESULTS_PER_PAGE);
    RemoteOperationResult result = searchRequest.execute(account, getContext());
    List<JSONObject> names = new ArrayList<JSONObject>();
    if (result.isSuccess()) {
        for (Object o : result.getData()) {
            // Get JSonObjects from response
            names.add((JSONObject) o);
        }
    } else {
        showErrorMessage(result);
    }
    /// convert the responses from the OC server to the expected format
    if (names.size() > 0) {
        response = new MatrixCursor(COLUMNS);
        Iterator<JSONObject> namesIt = names.iterator();
        JSONObject item;
        String displayName = null;
        int icon = 0;
        Uri dataUri = null;
        int count = 0;
        MainApp app = (MainApp) getContext().getApplicationContext();
        Uri userBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_USER_SUFFIX).build();
        Uri groupBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_GROUP_SUFFIX).build();
        Uri remoteBaseUri = new Uri.Builder().scheme(CONTENT).authority(sSuggestAuthority + DATA_REMOTE_SUFFIX).build();
        FileDataStorageManager manager = new FileDataStorageManager(account, getContext().getContentResolver());
        boolean federatedShareAllowed = manager.getCapability(account.name).getFilesSharingFederationOutgoing().isTrue();
        try {
            while (namesIt.hasNext()) {
                item = namesIt.next();
                String userName = item.getString(GetRemoteShareesOperation.PROPERTY_LABEL);
                JSONObject value = item.getJSONObject(GetRemoteShareesOperation.NODE_VALUE);
                int type = value.getInt(GetRemoteShareesOperation.PROPERTY_SHARE_TYPE);
                String shareWith = value.getString(GetRemoteShareesOperation.PROPERTY_SHARE_WITH);
                if (ShareType.GROUP.getValue() == type) {
                    displayName = getContext().getString(R.string.share_group_clarification, userName);
                    icon = R.drawable.ic_group;
                    dataUri = Uri.withAppendedPath(groupBaseUri, shareWith);
                } else if (ShareType.FEDERATED.getValue() == type && federatedShareAllowed) {
                    icon = R.drawable.ic_user;
                    if (userName.equals(shareWith)) {
                        displayName = getContext().getString(R.string.share_remote_clarification, userName);
                    } else {
                        String[] uriSplitted = shareWith.split("@");
                        displayName = getContext().getString(R.string.share_known_remote_clarification, userName, uriSplitted[uriSplitted.length - 1]);
                    }
                    dataUri = Uri.withAppendedPath(remoteBaseUri, shareWith);
                } else if (ShareType.USER.getValue() == type) {
                    displayName = userName;
                    icon = R.drawable.ic_user;
                    dataUri = Uri.withAppendedPath(userBaseUri, shareWith);
                }
                if (displayName != null && dataUri != null) {
                    response.newRow().add(// BaseColumns._ID
                    count++).add(// SearchManager.SUGGEST_COLUMN_TEXT_1
                    displayName).add(// SearchManager.SUGGEST_COLUMN_ICON_1
                    icon).add(dataUri);
                }
            }
        } catch (JSONException e) {
            Log_OC.e(TAG, "Exception while parsing data of users/groups", e);
        }
    }
    return response;
}
Also used : Account(android.accounts.Account) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) GetRemoteShareesOperation(com.owncloud.android.lib.resources.shares.GetRemoteShareesOperation) ArrayList(java.util.ArrayList) MainApp(com.owncloud.android.MainApp) JSONException(org.json.JSONException) Uri(android.net.Uri) MatrixCursor(android.database.MatrixCursor) JSONObject(org.json.JSONObject) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) JSONObject(org.json.JSONObject)

Example 10 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager in project android by owncloud.

the class RootCursor method addRoot.

public void addRoot(Account account, Context context) {
    final FileDataStorageManager manager = new FileDataStorageManager(account, context.getContentResolver());
    final OCFile mainDir = manager.getFileByPath("/");
    newRow().add(Root.COLUMN_ROOT_ID, account.name).add(Root.COLUMN_DOCUMENT_ID, mainDir.getFileId()).add(Root.COLUMN_SUMMARY, account.name).add(Root.COLUMN_TITLE, context.getString(R.string.app_name)).add(Root.COLUMN_ICON, R.mipmap.icon).add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_SEARCH);
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager)

Aggregations

FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)23 OCFile (com.owncloud.android.datamodel.OCFile)9 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)6 Account (android.accounts.Account)4 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)4 AccountsException (android.accounts.AccountsException)2 Intent (android.content.Intent)2 File (java.io.File)2 IOException (java.io.IOException)2 ContentResolver (android.content.ContentResolver)1 SharedPreferences (android.content.SharedPreferences)1 MatrixCursor (android.database.MatrixCursor)1 Uri (android.net.Uri)1 Handler (android.os.Handler)1 Pair (android.support.v4.util.Pair)1 ScrollingMovementMethod (android.text.method.ScrollingMovementMethod)1 Pair (android.util.Pair)1 Button (android.widget.Button)1 ListView (android.widget.ListView)1 TextView (android.widget.TextView)1