Search in sources :

Example 71 with FileDataStorageManager

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

the class ErrorMessageAdapterIT method getErrorCauseMessageForForbiddenRemoval.

@Test
public void getErrorCauseMessageForForbiddenRemoval() {
    Resources resources = InstrumentationRegistry.getInstrumentation().getTargetContext().getResources();
    User user = new MockUser("name", ACCOUNT_TYPE);
    Context context = MainApp.getAppContext();
    String errorMessage = ErrorMessageAdapter.getErrorCauseMessage(new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN), new RemoveFileOperation(new OCFile(PATH_TO_DELETE), false, user.toPlatformAccount(), false, context, new FileDataStorageManager(user, context.getContentResolver())), resources);
    assertEquals(EXPECTED_ERROR_MESSAGE, errorMessage);
}
Also used : Context(android.content.Context) OCFile(com.owncloud.android.datamodel.OCFile) User(com.nextcloud.client.account.User) MockUser(com.nextcloud.client.account.MockUser) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Resources(android.content.res.Resources) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) MockUser(com.nextcloud.client.account.MockUser) Test(org.junit.Test)

Example 72 with FileDataStorageManager

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

the class FileDetailActivitiesFragment method setupView.

private void setupView() {
    FileDataStorageManager storageManager = new FileDataStorageManager(user, contentResolver);
    operationsHelper = ((ComponentsGetter) requireActivity()).getFileOperationsHelper();
    OCCapability capability = storageManager.getCapability(user.getAccountName());
    restoreFileVersionSupported = capability.getFilesVersioning().isTrue();
    binding.emptyList.emptyListIcon.setImageDrawable(ResourcesCompat.getDrawable(getResources(), R.drawable.ic_activity, null));
    binding.emptyList.emptyListView.setVisibility(View.GONE);
    adapter = new ActivityAndVersionListAdapter(getContext(), accountManager, this, this, clientFactory, themeColorUtils, themeDrawableUtils);
    binding.list.setAdapter(adapter);
    LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
    binding.list.setLayoutManager(layoutManager);
    binding.list.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int visibleItemCount = recyclerView.getChildCount();
            int totalItemCount = layoutManager.getItemCount();
            int firstVisibleItemIndex = layoutManager.findFirstVisibleItemPosition();
            // synchronize loading state when item count changes
            if (!isLoadingActivities && (totalItemCount - visibleItemCount) <= (firstVisibleItemIndex + 5) && lastGiven > 0) {
                // Almost reached the end, continue to load new activities
                fetchAndSetData(lastGiven);
            }
        }
    });
}
Also used : OCCapability(com.owncloud.android.lib.resources.status.OCCapability) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) RecyclerView(androidx.recyclerview.widget.RecyclerView) ActivityAndVersionListAdapter(com.owncloud.android.ui.adapter.ActivityAndVersionListAdapter) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager)

Example 73 with FileDataStorageManager

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

the class OCFileListFragment method onBrowseUp.

/**
 * Call this, when the user presses the up button.
 *
 * Tries to move up the current folder one level. If the parent folder was removed from the
 * database, it continues browsing up until finding an existing folders.
 *
 * return       Count of folder levels browsed up.
 */
public int onBrowseUp() {
    OCFile parentDir;
    int moveCount = 0;
    if (mFile != null) {
        FileDataStorageManager storageManager = mContainerActivity.getStorageManager();
        String parentPath = null;
        if (mFile.getParentId() != FileDataStorageManager.ROOT_PARENT_ID) {
            parentPath = new File(mFile.getRemotePath()).getParent();
            parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
            parentDir = storageManager.getFileByPath(parentPath);
            moveCount++;
        } else {
            parentDir = storageManager.getFileByPath(ROOT_PATH);
        }
        while (parentDir == null) {
            parentPath = new File(parentPath).getParent();
            parentPath = parentPath.endsWith(OCFile.PATH_SEPARATOR) ? parentPath : parentPath + OCFile.PATH_SEPARATOR;
            parentDir = storageManager.getFileByPath(parentPath);
            moveCount++;
        }
        // exit is granted because storageManager.getFileByPath("/") never returns null
        mFile = parentDir;
        listDirectory(mFile, MainApp.isOnlyOnDevice(), false);
        onRefresh(false);
        // restore index and top position
        restoreIndexAndTopPosition();
    }
    return moveCount;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OCFile(com.owncloud.android.datamodel.OCFile) File(java.io.File)

Example 74 with FileDataStorageManager

use of com.owncloud.android.datamodel.FileDataStorageManager 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();
                OCFile file = storageManager.getFileByRemoteId(event.remoteId);
                int position = -1;
                if (file != null) {
                    position = mAdapter.getItemPosition(file);
                }
                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 : OCFile(com.owncloud.android.datamodel.OCFile) 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 75 with FileDataStorageManager

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

the class BackupFragment method onDateSet.

@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
    final ContactsPreferenceActivity contactsPreferenceActivity = (ContactsPreferenceActivity) getActivity();
    if (contactsPreferenceActivity == null) {
        Toast.makeText(getContext(), getString(R.string.error_choosing_date), Toast.LENGTH_LONG).show();
        return;
    }
    selectedDate = new Date(year, month, dayOfMonth);
    String contactsBackupFolderString = getResources().getString(R.string.contacts_backup_folder) + OCFile.PATH_SEPARATOR;
    String calendarBackupFolderString = getResources().getString(R.string.calendar_backup_folder) + OCFile.PATH_SEPARATOR;
    FileDataStorageManager storageManager = contactsPreferenceActivity.getStorageManager();
    OCFile contactsBackupFolder = storageManager.getFileByDecryptedRemotePath(contactsBackupFolderString);
    OCFile calendarBackupFolder = storageManager.getFileByDecryptedRemotePath(calendarBackupFolderString);
    List<OCFile> backupFiles = storageManager.getFolderContent(contactsBackupFolder, false);
    backupFiles.addAll(storageManager.getFolderContent(calendarBackupFolder, false));
    // find file with modification with date and time between 00:00 and 23:59
    // if more than one file exists, take oldest
    Calendar date = Calendar.getInstance();
    date.set(year, month, dayOfMonth);
    // start
    date.set(Calendar.HOUR, 0);
    date.set(Calendar.MINUTE, 0);
    date.set(Calendar.SECOND, 1);
    date.set(Calendar.MILLISECOND, 0);
    date.set(Calendar.AM_PM, Calendar.AM);
    long start = date.getTimeInMillis();
    // end
    date.set(Calendar.HOUR, 23);
    date.set(Calendar.MINUTE, 59);
    date.set(Calendar.SECOND, 59);
    long end = date.getTimeInMillis();
    OCFile contactsBackupToRestore = null;
    List<OCFile> calendarBackupsToRestore = new ArrayList<>();
    for (OCFile file : backupFiles) {
        if (start < file.getModificationTimestamp() && end > file.getModificationTimestamp()) {
            // contact
            if (MimeTypeUtil.isVCard(file)) {
                if (contactsBackupToRestore == null) {
                    contactsBackupToRestore = file;
                } else if (contactsBackupToRestore.getModificationTimestamp() < file.getModificationTimestamp()) {
                    contactsBackupToRestore = file;
                }
            }
            // calendars
            if (MimeTypeUtil.isCalendar(file)) {
                calendarBackupsToRestore.add(file);
            }
        }
    }
    List<OCFile> backupToRestore = new ArrayList<>();
    if (contactsBackupToRestore != null) {
        backupToRestore.add(contactsBackupToRestore);
    }
    backupToRestore.addAll(calendarBackupsToRestore);
    if (backupToRestore.isEmpty()) {
        DisplayUtils.showSnackMessage(getView().findViewById(R.id.contacts_linear_layout), R.string.contacts_preferences_no_file_found);
    } else {
        final User user = contactsPreferenceActivity.getUser().orElseThrow(RuntimeException::new);
        OCFile[] files = new OCFile[backupToRestore.size()];
        Fragment contactListFragment = BackupListFragment.newInstance(backupToRestore.toArray(files), user);
        contactsPreferenceActivity.getSupportFragmentManager().beginTransaction().replace(R.id.frame_container, contactListFragment, BackupListFragment.TAG).addToBackStack(ContactsPreferenceActivity.BACKUP_TO_LIST).commit();
    }
}
Also used : ContactsPreferenceActivity(com.owncloud.android.ui.activity.ContactsPreferenceActivity) User(com.nextcloud.client.account.User) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Fragment(androidx.fragment.app.Fragment) FileFragment(com.owncloud.android.ui.fragment.FileFragment) Date(java.util.Date) OCFile(com.owncloud.android.datamodel.OCFile) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager)

Aggregations

FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)91 OCFile (com.owncloud.android.datamodel.OCFile)43 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)21 Account (android.accounts.Account)19 User (com.nextcloud.client.account.User)16 Intent (android.content.Intent)12 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)12 Context (android.content.Context)9 File (java.io.File)9 ArrayList (java.util.ArrayList)9 OCCapability (com.owncloud.android.lib.resources.status.OCCapability)8 Test (org.junit.Test)8 ScreenshotTest (com.owncloud.android.utils.ScreenshotTest)7 OCUpload (com.owncloud.android.db.OCUpload)6 AccountManager (android.accounts.AccountManager)5 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)5 UploadFileOperation (com.owncloud.android.operations.UploadFileOperation)5 DialogFragment (androidx.fragment.app.DialogFragment)4 IOException (java.io.IOException)4 AccountsException (android.accounts.AccountsException)3