Search in sources :

Example 66 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class OperationsService method newOperation.

/**
 * Creates a new operation, as described by operationIntent.
 * <p>
 * TODO - move to ServiceHandler (probably)
 *
 * @param operationIntent Intent describing a new operation to queue and execute.
 * @return Pair with the new operation object and the information about its target server.
 */
private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
    RemoteOperation operation = null;
    Target target = null;
    try {
        if (!operationIntent.hasExtra(EXTRA_ACCOUNT) && !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
            Log_OC.e(TAG, "Not enough information provided in intent");
        } else {
            Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
            User user = toUser(account);
            String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
            target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl));
            String action = operationIntent.getAction();
            String remotePath;
            String password;
            ShareType shareType;
            String newParentPath;
            long shareId;
            FileDataStorageManager fileDataStorageManager = new FileDataStorageManager(user, getContentResolver());
            switch(action) {
                case ACTION_CREATE_SHARE_VIA_LINK:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                    if (!TextUtils.isEmpty(remotePath)) {
                        operation = new CreateShareViaLinkOperation(remotePath, password, fileDataStorageManager);
                    }
                    break;
                case ACTION_UPDATE_PUBLIC_SHARE:
                    shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                    if (shareId > 0) {
                        UpdateShareViaLinkOperation updateLinkOperation = new UpdateShareViaLinkOperation(shareId, fileDataStorageManager);
                        password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                        updateLinkOperation.setPassword(password);
                        long expirationDate = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0);
                        updateLinkOperation.setExpirationDateInMillis(expirationDate);
                        boolean hideFileDownload = operationIntent.getBooleanExtra(EXTRA_SHARE_HIDE_FILE_DOWNLOAD, false);
                        updateLinkOperation.setHideFileDownload(hideFileDownload);
                        if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_LABEL)) {
                            updateLinkOperation.setLabel(operationIntent.getStringExtra(EXTRA_SHARE_PUBLIC_LABEL));
                        }
                        operation = updateLinkOperation;
                    }
                    break;
                case ACTION_UPDATE_USER_SHARE:
                    shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                    if (shareId > 0) {
                        UpdateSharePermissionsOperation updateShare = new UpdateSharePermissionsOperation(shareId, fileDataStorageManager);
                        int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
                        updateShare.setPermissions(permissions);
                        long expirationDateInMillis = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0L);
                        updateShare.setExpirationDateInMillis(expirationDateInMillis);
                        password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                        updateShare.setPassword(password);
                        operation = updateShare;
                    }
                    break;
                case ACTION_UPDATE_SHARE_NOTE:
                    shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                    String note = operationIntent.getStringExtra(EXTRA_SHARE_NOTE);
                    if (shareId > 0) {
                        operation = new UpdateNoteForShareOperation(shareId, note, fileDataStorageManager);
                    }
                    break;
                case ACTION_CREATE_SHARE_WITH_SHAREE:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    String shareeName = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
                    shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
                    int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
                    String noteMessage = operationIntent.getStringExtra(EXTRA_SHARE_NOTE);
                    String sharePassword = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                    long expirationDateInMillis = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0L);
                    boolean hideFileDownload = operationIntent.getBooleanExtra(EXTRA_SHARE_HIDE_FILE_DOWNLOAD, false);
                    if (!TextUtils.isEmpty(remotePath)) {
                        CreateShareWithShareeOperation createShareWithShareeOperation = new CreateShareWithShareeOperation(remotePath, shareeName, shareType, permissions, noteMessage, sharePassword, expirationDateInMillis, hideFileDownload, fileDataStorageManager);
                        if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_LABEL)) {
                            createShareWithShareeOperation.setLabel(operationIntent.getStringExtra(EXTRA_SHARE_PUBLIC_LABEL));
                        }
                        operation = createShareWithShareeOperation;
                    }
                    break;
                case ACTION_UPDATE_SHARE_INFO:
                    shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                    if (shareId > 0) {
                        UpdateShareInfoOperation updateShare = new UpdateShareInfoOperation(shareId, fileDataStorageManager);
                        int permissionsToChange = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
                        updateShare.setPermissions(permissionsToChange);
                        long expirationDateInMills = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0L);
                        updateShare.setExpirationDateInMillis(expirationDateInMills);
                        password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                        updateShare.setPassword(password);
                        boolean fileDownloadHide = operationIntent.getBooleanExtra(EXTRA_SHARE_HIDE_FILE_DOWNLOAD, false);
                        updateShare.setHideFileDownload(fileDownloadHide);
                        if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_LABEL)) {
                            updateShare.setLabel(operationIntent.getStringExtra(EXTRA_SHARE_PUBLIC_LABEL));
                        }
                        operation = updateShare;
                    }
                    break;
                case ACTION_UNSHARE:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                    if (shareId > 0) {
                        operation = new UnshareOperation(remotePath, shareId, fileDataStorageManager);
                    }
                    break;
                case ACTION_GET_SERVER_INFO:
                    operation = new GetServerInfoOperation(serverUrl, this);
                    break;
                case ACTION_GET_USER_NAME:
                    operation = new GetUserInfoRemoteOperation();
                    break;
                case ACTION_RENAME:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
                    operation = new RenameFileOperation(remotePath, newName, fileDataStorageManager);
                    break;
                case ACTION_REMOVE:
                    // Remove file or folder
                    OCFile file = operationIntent.getParcelableExtra(EXTRA_FILE);
                    boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
                    boolean inBackground = operationIntent.getBooleanExtra(EXTRA_IN_BACKGROUND, false);
                    operation = new RemoveFileOperation(file, onlyLocalCopy, account, inBackground, getApplicationContext(), fileDataStorageManager);
                    break;
                case ACTION_CREATE_FOLDER:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    operation = new CreateFolderOperation(remotePath, user, getApplicationContext(), fileDataStorageManager);
                    break;
                case ACTION_SYNC_FILE:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
                    operation = new SynchronizeFileOperation(remotePath, user, syncFileContents, getApplicationContext(), fileDataStorageManager);
                    break;
                case ACTION_SYNC_FOLDER:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    operation = new SynchronizeFolderOperation(// TODO remove this dependency from construction time
                    this, remotePath, user, // TODO remove this dependency from construction time
                    System.currentTimeMillis(), fileDataStorageManager);
                    break;
                case ACTION_MOVE_FILE:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                    operation = new MoveFileOperation(remotePath, newParentPath, fileDataStorageManager);
                    break;
                case ACTION_COPY_FILE:
                    remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                    newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                    operation = new CopyFileOperation(remotePath, newParentPath, fileDataStorageManager);
                    break;
                case ACTION_CHECK_CURRENT_CREDENTIALS:
                    operation = new CheckCurrentCredentialsOperation(user, fileDataStorageManager);
                    break;
                case ACTION_RESTORE_VERSION:
                    FileVersion fileVersion = operationIntent.getParcelableExtra(EXTRA_FILE_VERSION);
                    operation = new RestoreFileVersionRemoteOperation(fileVersion.getRemoteId(), fileVersion.getFileName());
                    break;
                default:
                    // do nothing
                    break;
            }
        }
    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        operation = null;
    }
    if (operation != null) {
        return new Pair<>(target, operation);
    } else {
        return null;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UnshareOperation(com.owncloud.android.operations.UnshareOperation) User(com.nextcloud.client.account.User) UpdateNoteForShareOperation(com.owncloud.android.operations.UpdateNoteForShareOperation) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) RenameFileOperation(com.owncloud.android.operations.RenameFileOperation) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation) OCFile(com.owncloud.android.datamodel.OCFile) SynchronizeFolderOperation(com.owncloud.android.operations.SynchronizeFolderOperation) UpdateShareViaLinkOperation(com.owncloud.android.operations.UpdateShareViaLinkOperation) FileVersion(com.owncloud.android.lib.resources.files.model.FileVersion) RestoreFileVersionRemoteOperation(com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation) Pair(android.util.Pair) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) RestoreFileVersionRemoteOperation(com.owncloud.android.lib.resources.files.RestoreFileVersionRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetUserInfoRemoteOperation(com.owncloud.android.lib.resources.users.GetUserInfoRemoteOperation) CreateShareWithShareeOperation(com.owncloud.android.operations.CreateShareWithShareeOperation) CheckCurrentCredentialsOperation(com.owncloud.android.operations.CheckCurrentCredentialsOperation) CreateShareViaLinkOperation(com.owncloud.android.operations.CreateShareViaLinkOperation) CreateFolderOperation(com.owncloud.android.operations.CreateFolderOperation) UpdateShareInfoOperation(com.owncloud.android.operations.UpdateShareInfoOperation) MoveFileOperation(com.owncloud.android.operations.MoveFileOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) GetServerInfoOperation(com.owncloud.android.operations.GetServerInfoOperation) UpdateSharePermissionsOperation(com.owncloud.android.operations.UpdateSharePermissionsOperation) ShareType(com.owncloud.android.lib.resources.shares.ShareType) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation)

Example 67 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class PreviewImageFragment method onPrepareOptionsMenu.

/**
 * {@inheritDoc}
 */
@Override
public void onPrepareOptionsMenu(@NonNull Menu menu) {
    super.onPrepareOptionsMenu(menu);
    if (containerActivity.getStorageManager() != null && getFile() != null) {
        // Update the file
        setFile(containerActivity.getStorageManager().getFileById(getFile().getFileId()));
        User currentUser = accountManager.getUser();
        FileMenuFilter mf = new FileMenuFilter(getFile(), containerActivity, getActivity(), false, currentUser);
        mf.filter(menu, true);
    }
    // additional restriction for this fragment
    // TODO allow renaming in PreviewImageFragment
    // TODO allow refresh file in PreviewImageFragment
    FileMenuFilter.hideMenuItems(menu.findItem(R.id.action_rename_file), menu.findItem(R.id.action_sync_file), menu.findItem(R.id.action_select_all), menu.findItem(R.id.action_move), menu.findItem(R.id.action_copy), menu.findItem(R.id.action_favorite), menu.findItem(R.id.action_unset_favorite));
    if (getFile().isSharedWithMe() && !getFile().canReshare()) {
        FileMenuFilter.hideMenuItem(menu.findItem(R.id.action_send_share_file));
    }
}
Also used : User(com.nextcloud.client.account.User) FileMenuFilter(com.owncloud.android.files.FileMenuFilter)

Example 68 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class OCFileListFragment method handleSearchEvent.

private void handleSearchEvent(SearchEvent event) {
    if (SearchRemoteOperation.SearchType.PHOTO_SEARCH == event.getSearchType()) {
        return;
    }
    prepareCurrentSearch(event);
    searchFragment = true;
    setEmptyListLoadingMessage();
    mAdapter.setData(new ArrayList<>(), SearchType.NO_SEARCH, mContainerActivity.getStorageManager(), mFile, true);
    setFabVisible(false);
    Runnable switchViewsRunnable = () -> {
        if (isGridViewPreferred(mFile) && !isGridEnabled()) {
            switchToGridView();
        } else if (!isGridViewPreferred(mFile) && isGridEnabled()) {
            switchToListView();
        }
    };
    new Handler(Looper.getMainLooper()).post(switchViewsRunnable);
    final User currentUser = accountManager.getUser();
    final RemoteOperation remoteOperation;
    if (currentSearchType != SearchType.SHARED_FILTER) {
        boolean searchOnlyFolders = false;
        if (getArguments() != null && getArguments().getBoolean(ARG_SEARCH_ONLY_FOLDER, false)) {
            searchOnlyFolders = true;
        }
        OCCapability ocCapability = mContainerActivity.getStorageManager().getCapability(currentUser.getAccountName());
        remoteOperation = new SearchRemoteOperation(event.getSearchQuery(), event.getSearchType(), searchOnlyFolders, ocCapability);
    } else {
        remoteOperation = new GetSharesRemoteOperation();
    }
    remoteOperationAsyncTask = new AsyncTask<Void, Void, Boolean>() {

        @Override
        protected Boolean doInBackground(Void... voids) {
            setTitle();
            if (getContext() != null && !isCancelled()) {
                RemoteOperationResult remoteOperationResult = remoteOperation.execute(currentUser.toPlatformAccount(), getContext());
                FileDataStorageManager storageManager = null;
                if (mContainerActivity != null && mContainerActivity.getStorageManager() != null) {
                    storageManager = mContainerActivity.getStorageManager();
                }
                if (remoteOperationResult.isSuccess() && remoteOperationResult.getResultData() != null && !isCancelled() && searchFragment) {
                    searchEvent = event;
                    if (remoteOperationResult.getResultData() == null || ((List) remoteOperationResult.getResultData()).isEmpty()) {
                        setEmptyView(event);
                    } else {
                        mAdapter.setData(((RemoteOperationResult<List>) remoteOperationResult).getResultData(), currentSearchType, storageManager, mFile, true);
                    }
                    final ToolbarActivity fileDisplayActivity = (ToolbarActivity) getActivity();
                    if (fileDisplayActivity != null) {
                        fileDisplayActivity.runOnUiThread(() -> {
                            if (fileDisplayActivity != null) {
                                setLoading(false);
                            }
                        });
                    }
                }
                return remoteOperationResult.isSuccess();
            } else {
                return Boolean.FALSE;
            }
        }

        @Override
        protected void onPostExecute(Boolean bool) {
            if (!isCancelled()) {
                mAdapter.notifyDataSetChanged();
            }
        }
    };
    remoteOperationAsyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : ToolbarActivity(com.owncloud.android.ui.activity.ToolbarActivity) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) RichWorkspaceDirectEditingRemoteOperation(com.nextcloud.android.lib.richWorkspace.RichWorkspaceDirectEditingRemoteOperation) GetSharesRemoteOperation(com.owncloud.android.lib.resources.shares.GetSharesRemoteOperation) ToggleFavoriteRemoteOperation(com.owncloud.android.lib.resources.files.ToggleFavoriteRemoteOperation) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) User(com.nextcloud.client.account.User) OCCapability(com.owncloud.android.lib.resources.status.OCCapability) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Handler(android.os.Handler) SearchRemoteOperation(com.owncloud.android.lib.resources.files.SearchRemoteOperation) GetSharesRemoteOperation(com.owncloud.android.lib.resources.shares.GetSharesRemoteOperation) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ArrayList(java.util.ArrayList) List(java.util.List)

Example 69 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class OCFileListFragment method onMessageEvent.

@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessageEvent(EncryptionEvent event) {
    final User user = accountManager.getUser();
    try {
        OwnCloudClient client = clientFactory.create(user);
        RemoteOperationResult remoteOperationResult = new ToggleEncryptionRemoteOperation(event.localId, event.remotePath, event.shouldBeEncrypted).execute(client);
        if (remoteOperationResult.isSuccess()) {
            mAdapter.setEncryptionAttributeForItemID(event.remoteId, event.shouldBeEncrypted);
            // check if keys are stored
            ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(requireContext().getContentResolver());
            String publicKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PUBLIC_KEY);
            String privateKey = arbitraryDataProvider.getValue(user, EncryptionUtils.PRIVATE_KEY);
            if (publicKey.isEmpty() || privateKey.isEmpty()) {
                Log_OC.d(TAG, "no public key for " + user.getAccountName());
                FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
                int position = mAdapter.getItemPosition(storageManager.getFileByRemoteId(event.remoteId));
                SetupEncryptionDialogFragment dialog = SetupEncryptionDialogFragment.newInstance(user, position);
                dialog.setTargetFragment(this, SetupEncryptionDialogFragment.SETUP_ENCRYPTION_REQUEST_CODE);
                dialog.show(getParentFragmentManager(), SetupEncryptionDialogFragment.SETUP_ENCRYPTION_DIALOG_TAG);
            }
        } else if (remoteOperationResult.getHttpCode() == HttpStatus.SC_FORBIDDEN) {
            Snackbar.make(getRecyclerView(), R.string.end_to_end_encryption_folder_not_empty, Snackbar.LENGTH_LONG).show();
        } else {
            Snackbar.make(getRecyclerView(), R.string.common_error_unknown, Snackbar.LENGTH_LONG).show();
        }
    } catch (ClientFactory.CreationException e) {
        Log_OC.e(TAG, "Cannot create client", e);
    }
}
Also used : User(com.nextcloud.client.account.User) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ClientFactory(com.nextcloud.client.network.ClientFactory) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) OwnCloudClient(com.owncloud.android.lib.common.OwnCloudClient) SetupEncryptionDialogFragment(com.owncloud.android.ui.dialog.SetupEncryptionDialogFragment) ToggleEncryptionRemoteOperation(com.owncloud.android.lib.resources.e2ee.ToggleEncryptionRemoteOperation) Subscribe(org.greenrobot.eventbus.Subscribe)

Example 70 with User

use of com.nextcloud.client.account.User in project android by nextcloud.

the class PreviewImageActivity method onStart.

@Override
public void onStart() {
    super.onStart();
    Optional<User> optionalUser = getUser();
    if (optionalUser.isPresent()) {
        OCFile file = getFile();
        // / Validate handled file (first image to preview)
        if (file == null) {
            throw new IllegalStateException("Instanced with a NULL OCFile");
        }
        if (!MimeTypeUtil.isImage(file)) {
            throw new IllegalArgumentException("Non-image file passed as argument");
        }
        // Update file according to DB file, if it is possible
        if (file.getFileId() > FileDataStorageManager.ROOT_PARENT_ID) {
            file = getStorageManager().getFileById(file.getFileId());
        }
        if (file != null) {
            // / Refresh the activity according to the Account and OCFile set
            // reset after getting it fresh from storageManager
            setFile(file);
            getSupportActionBar().setTitle(getFile().getFileName());
            // if (!stateWasRecovered) {
            initViewPager(optionalUser.get());
        // }
        } else {
            // handled file not in the current Account
            finish();
        }
    }
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) User(com.nextcloud.client.account.User)

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