Search in sources :

Example 1 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 2 with FileDataStorageManager

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

the class EditShareFragment method refreshUiFromDB.

/**
     * Get {@link OCShare} instance from DB and updates the UI.
     *
     * Depends on the parent Activity provides a {@link com.owncloud.android.datamodel.FileDataStorageManager}
     * instance ready to use. If not ready, does nothing.
     *
     * @param editShareView     Root view in the fragment.
     */
private void refreshUiFromDB(View editShareView) {
    FileDataStorageManager storageManager = ((FileActivity) getActivity()).getStorageManager();
    if (storageManager != null) {
        // Get edited share
        mShare = storageManager.getShareById(mShare.getId());
        // Updates UI with new state
        refreshUiFromState(editShareView);
    }
}
Also used : FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) FileActivity(com.owncloud.android.ui.activity.FileActivity)

Example 3 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 4 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 5 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)60 OCFile (com.owncloud.android.datamodel.OCFile)26 Account (android.accounts.Account)18 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)18 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)10 Intent (android.content.Intent)8 File (java.io.File)8 ArrayList (java.util.ArrayList)5 AccountsException (android.accounts.AccountsException)4 Context (android.content.Context)4 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)4 Uri (android.net.Uri)3 Handler (android.os.Handler)3 SynchronizeFileOperation (com.owncloud.android.operations.SynchronizeFileOperation)3 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)3 FileActivity (com.owncloud.android.ui.activity.FileActivity)3 IOException (java.io.IOException)3 Vector (java.util.Vector)3 AccountManager (android.accounts.AccountManager)2 ContentResolver (android.content.ContentResolver)2