Search in sources :

Example 61 with FileDataStorageManager

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

the class SynchronizeFolderOperation method synchronizeData.

/**
 * Synchronizes the data retrieved from the server about the contents of the target folder
 * with the current data in the local database.
 *
 * Grants that mChildren is updated with fresh data after execution.
 *
 * @param folderAndFiles Remote folder and children files in Folder
 */
private void synchronizeData(ArrayList<Object> folderAndFiles) throws OperationCancelledException {
    FileDataStorageManager storageManager = getStorageManager();
    // parse data from remote folder
    OCFile remoteFolder = FileStorageUtils.fillOCFile((RemoteFile) folderAndFiles.get(0));
    remoteFolder.setParentId(mLocalFolder.getParentId());
    remoteFolder.setFileId(mLocalFolder.getFileId());
    Log_OC.d(TAG, "Remote folder " + mLocalFolder.getRemotePath() + " changed - starting update of local data ");
    List<OCFile> updatedFiles = new Vector<>(folderAndFiles.size() - 1);
    mFilesForDirectDownload.clear();
    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, false);
    Map<String, OCFile> localFilesMap = new HashMap<>(localFiles.size());
    for (OCFile file : localFiles) {
        localFilesMap.put(file.getRemotePath(), file);
    }
    // loop to synchronize every child
    OCFile remoteFile = null;
    OCFile localFile = null;
    OCFile updatedFile = null;
    RemoteFile r;
    for (int i = 1; i < folderAndFiles.size(); i++) {
        // / new OCFile instance with the data from the server
        r = (RemoteFile) folderAndFiles.get(i);
        remoteFile = FileStorageUtils.fillOCFile(r);
        // / retrieve local data for the read file
        // localFile = mStorageManager.getFileByPath(remoteFile.getRemotePath());
        localFile = localFilesMap.remove(remoteFile.getRemotePath());
        // / new OCFile instance to merge fresh data from server with local state
        updatedFile = FileStorageUtils.fillOCFile(r);
        updatedFile.setParentId(mLocalFolder.getFileId());
        // / add to updatedFile data about LOCAL STATE (not existing in server)
        updateLocalStateData(remoteFile, localFile, updatedFile);
        // / check and fix, if needed, local storage path
        searchForLocalFileInDefaultPath(updatedFile);
        // / classify file to sync/download contents later
        classifyFileForLaterSyncOrDownload(remoteFile, localFile);
        updatedFiles.add(updatedFile);
    }
    // save updated contents in local database
    storageManager.saveFolder(remoteFolder, 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 62 with FileDataStorageManager

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

the class ActivitiesListActivity method setupContent.

/**
 * sets up the UI elements and loads all activity items.
 */
private void setupContent() {
    emptyContentIcon.setImageResource(R.drawable.ic_activity_light_grey);
    emptyContentProgressBar.getIndeterminateDrawable().setColorFilter(ThemeUtils.primaryAccentColor(), PorterDuff.Mode.SRC_IN);
    setLoadingMessage();
    FileDataStorageManager storageManager = new FileDataStorageManager(getAccount(), getContentResolver());
    adapter = new ActivityListAdapter(this, this, storageManager);
    recyclerView.setAdapter(adapter);
    LinearLayoutManager layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrolled(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) && nextPageUrl != null && !nextPageUrl.isEmpty()) {
                // Almost reached the end, continue to load new activities
                fetchAndSetData(nextPageUrl);
            }
        }
    });
    if (getResources().getBoolean(R.bool.bottom_toolbar_enabled)) {
        bottomNavigationView.setVisibility(View.VISIBLE);
        DisplayUtils.setupBottomBar(bottomNavigationView, getResources(), this, -1);
    }
    fetchAndSetData(null);
}
Also used : FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ActivityListAdapter(com.owncloud.android.ui.adapter.ActivityListAdapter) RecyclerView(android.support.v7.widget.RecyclerView) LinearLayoutManager(android.support.v7.widget.LinearLayoutManager)

Example 63 with FileDataStorageManager

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

the class PreferenceManager method getFolderPreference.

/**
 * Get preference value for a folder.
 * If folder is not set itself, it finds an ancestor that is set.
 *
 * @param context Context object.
 * @param preferenceName Name of the preference to lookup.
 * @param folder Folder.
 * @param defaultValue Fallback value in case no ancestor is set.
 * @return Preference value
 */
public static String getFolderPreference(Context context, String preferenceName, OCFile folder, String defaultValue) {
    Account account = AccountUtils.getCurrentOwnCloudAccount(context);
    if (account == null) {
        return defaultValue;
    }
    ArbitraryDataProvider dataProvider = new ArbitraryDataProvider(context.getContentResolver());
    FileDataStorageManager storageManager = ((ComponentsGetter) context).getStorageManager();
    if (storageManager == null) {
        storageManager = new FileDataStorageManager(account, context.getContentResolver());
    }
    String value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
    while (folder != null && value.isEmpty()) {
        folder = storageManager.getFileById(folder.getParentId());
        value = dataProvider.getValue(account.name, getKeyFromFolder(preferenceName, folder));
    }
    return value.isEmpty() ? defaultValue : value;
}
Also used : ComponentsGetter(com.owncloud.android.ui.activity.ComponentsGetter) Account(android.accounts.Account) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider)

Example 64 with FileDataStorageManager

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

the class AccountRemovalJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    Context context = MainApp.getAppContext();
    PersistableBundleCompat bundle = params.getExtras();
    Account account = AccountUtils.getOwnCloudAccountByName(context, bundle.getString(ACCOUNT, ""));
    AccountManager am = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
    if (account != null && am != null) {
        // disable contact backup job
        ContactsPreferenceActivity.cancelContactBackupJobForAccount(context, account);
        if (am != null) {
            am.removeAccount(account, this, null);
        }
        FileDataStorageManager storageManager = new FileDataStorageManager(account, context.getContentResolver());
        File tempDir = new File(FileStorageUtils.getTemporalPath(account.name));
        File saveDir = new File(FileStorageUtils.getSavePath(account.name));
        FileStorageUtils.deleteRecursively(tempDir, storageManager);
        FileStorageUtils.deleteRecursively(saveDir, storageManager);
        // delete all database entries
        storageManager.deleteAllFiles();
        // remove pending account removal
        ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
        arbitraryDataProvider.deleteKeyForAccount(account.name, PENDING_FOR_REMOVAL);
        // remove synced folders set for account
        SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(context.getContentResolver());
        List<SyncedFolder> syncedFolders = syncedFolderProvider.getSyncedFolders();
        List<Long> syncedFolderIds = new ArrayList<>();
        for (SyncedFolder syncedFolder : syncedFolders) {
            if (syncedFolder.getAccount().equals(account.name)) {
                arbitraryDataProvider.deleteKeyForAccount(FilesSyncHelper.GLOBAL, FilesSyncHelper.SYNCEDFOLDERINITIATED + syncedFolder.getId());
                syncedFolderIds.add(syncedFolder.getId());
            }
        }
        syncedFolderProvider.deleteSyncFoldersForAccount(account);
        UploadsStorageManager uploadsStorageManager = new UploadsStorageManager(context.getContentResolver(), context);
        uploadsStorageManager.removeAccountUploads(account);
        FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(context.getContentResolver());
        for (long syncedFolderId : syncedFolderIds) {
            filesystemDataProvider.deleteAllEntriesForSyncedFolder(Long.toString(syncedFolderId));
        }
        // delete stored E2E keys
        arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PRIVATE_KEY);
        arbitraryDataProvider.deleteKeyForAccount(account.name, EncryptionUtils.PUBLIC_KEY);
        return Result.SUCCESS;
    } else {
        return Result.FAILURE;
    }
}
Also used : Context(android.content.Context) Account(android.accounts.Account) FilesystemDataProvider(com.owncloud.android.datamodel.FilesystemDataProvider) PersistableBundleCompat(com.evernote.android.job.util.support.PersistableBundleCompat) ArrayList(java.util.ArrayList) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) SyncedFolderProvider(com.owncloud.android.datamodel.SyncedFolderProvider) SyncedFolder(com.owncloud.android.datamodel.SyncedFolder) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) UploadsStorageManager(com.owncloud.android.datamodel.UploadsStorageManager) AccountManager(android.accounts.AccountManager) File(java.io.File) NonNull(android.support.annotation.NonNull)

Example 65 with FileDataStorageManager

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

the class OfflineSyncJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(@NonNull Params params) {
    final Context context = MainApp.getAppContext();
    PowerManager.WakeLock wakeLock = null;
    if (!PowerUtils.isPowerSaveMode(context) && Device.getNetworkType(context).equals(JobRequest.NetworkType.UNMETERED) && !ConnectivityUtils.isInternetWalled(context)) {
        Set<Job> jobs = JobManager.instance().getAllJobsForTag(TAG);
        for (Job job : jobs) {
            if (!job.isFinished() && !job.equals(this)) {
                return Result.SUCCESS;
            }
        }
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
            wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
            wakeLock.acquire();
        }
        Cursor cursorOnKeptInSync = context.getContentResolver().query(ProviderMeta.ProviderTableMeta.CONTENT_URI, null, ProviderMeta.ProviderTableMeta.FILE_KEEP_IN_SYNC + " = ?", new String[] { String.valueOf(1) }, null);
        if (cursorOnKeptInSync != null) {
            if (cursorOnKeptInSync.moveToFirst()) {
                String localPath = "";
                String accountName = "";
                Account account = null;
                do {
                    localPath = cursorOnKeptInSync.getString(cursorOnKeptInSync.getColumnIndex(ProviderMeta.ProviderTableMeta.FILE_STORAGE_PATH));
                    accountName = cursorOnKeptInSync.getString(cursorOnKeptInSync.getColumnIndex(ProviderMeta.ProviderTableMeta.FILE_ACCOUNT_OWNER));
                    account = new Account(accountName, MainApp.getAccountType());
                    if (!AccountUtils.exists(account, context) || localPath == null || localPath.length() <= 0) {
                        continue;
                    }
                    offlineFileList.add(new OfflineFile(localPath, account));
                } while (cursorOnKeptInSync.moveToNext());
            }
            cursorOnKeptInSync.close();
        }
        FileDataStorageManager storageManager;
        for (OfflineFile offlineFile : offlineFileList) {
            storageManager = new FileDataStorageManager(offlineFile.getAccount(), context.getContentResolver());
            OCFile file = storageManager.getFileByLocalPath(offlineFile.getLocalPath());
            SynchronizeFileOperation sfo = new SynchronizeFileOperation(file, null, offlineFile.getAccount(), true, context);
            RemoteOperationResult result = sfo.execute(storageManager, context);
            if (result.getCode() == RemoteOperationResult.ResultCode.SYNC_CONFLICT) {
                Intent i = new Intent(context, ConflictsResolveActivity.class);
                i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
                i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
                i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, offlineFile.getAccount());
                context.startActivity(i);
            }
        }
        if (wakeLock != null) {
            wakeLock.release();
        }
    }
    return Result.SUCCESS;
}
Also used : Context(android.content.Context) Account(android.accounts.Account) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) Intent(android.content.Intent) Cursor(android.database.Cursor) PowerManager(android.os.PowerManager) OCFile(com.owncloud.android.datamodel.OCFile) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Job(com.evernote.android.job.Job) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation) NonNull(android.support.annotation.NonNull)

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