Search in sources :

Example 1 with Path

use of org.lukhnos.nnio.file.Path in project android by nextcloud.

the class FilesSyncHelper method insertAllDBEntriesForSyncedFolder.

public static void insertAllDBEntriesForSyncedFolder(SyncedFolder syncedFolder) {
    final Context context = MainApp.getAppContext();
    final ContentResolver contentResolver = context.getContentResolver();
    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(contentResolver);
    Long currentTime = System.currentTimeMillis();
    double currentTimeInSeconds = currentTime / 1000.0;
    String currentTimeString = Long.toString((long) currentTimeInSeconds);
    String syncedFolderInitiatedKey = SYNCEDFOLDERINITIATED + syncedFolder.getId();
    boolean dryRun = TextUtils.isEmpty(arbitraryDataProvider.getValue(GLOBAL, syncedFolderInitiatedKey));
    if (MediaFolderType.IMAGE == syncedFolder.getType()) {
        if (dryRun) {
            arbitraryDataProvider.storeOrUpdateKeyValue(GLOBAL, syncedFolderInitiatedKey, currentTimeString);
        } else {
            FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, syncedFolder);
            FilesSyncHelper.insertContentIntoDB(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, syncedFolder);
        }
    } else if (MediaFolderType.VIDEO == syncedFolder.getType()) {
        if (dryRun) {
            arbitraryDataProvider.storeOrUpdateKeyValue(GLOBAL, syncedFolderInitiatedKey, currentTimeString);
        } else {
            FilesSyncHelper.insertContentIntoDB(android.provider.MediaStore.Video.Media.INTERNAL_CONTENT_URI, syncedFolder);
            FilesSyncHelper.insertContentIntoDB(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, syncedFolder);
        }
    } else {
        try {
            if (dryRun) {
                arbitraryDataProvider.storeOrUpdateKeyValue(GLOBAL, syncedFolderInitiatedKey, currentTimeString);
            } else {
                FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
                Path path = Paths.get(syncedFolder.getLocalPath());
                String dateInitiated = arbitraryDataProvider.getValue(GLOBAL, syncedFolderInitiatedKey);
                Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

                    @Override
                    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
                        File file = path.toFile();
                        if (attrs.lastModifiedTime().toMillis() >= Long.parseLong(dateInitiated) * 1000) {
                            filesystemDataProvider.storeOrUpdateFileValue(path.toAbsolutePath().toString(), attrs.lastModifiedTime().toMillis(), file.isDirectory(), syncedFolder);
                        }
                        return FileVisitResult.CONTINUE;
                    }

                    @Override
                    public FileVisitResult visitFileFailed(Path file, IOException exc) {
                        return FileVisitResult.CONTINUE;
                    }
                });
            }
        } catch (IOException e) {
            Log.e(TAG, "Something went wrong while indexing files for auto upload " + e.getLocalizedMessage());
        }
    }
}
Also used : Context(android.content.Context) Path(org.lukhnos.nnio.file.Path) FilesystemDataProvider(com.owncloud.android.datamodel.FilesystemDataProvider) SimpleFileVisitor(org.lukhnos.nnio.file.SimpleFileVisitor) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) IOException(java.io.IOException) ContentResolver(android.content.ContentResolver) File(java.io.File) BasicFileAttributes(org.lukhnos.nnio.file.attribute.BasicFileAttributes)

Aggregations

ContentResolver (android.content.ContentResolver)1 Context (android.content.Context)1 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)1 FilesystemDataProvider (com.owncloud.android.datamodel.FilesystemDataProvider)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (org.lukhnos.nnio.file.Path)1 SimpleFileVisitor (org.lukhnos.nnio.file.SimpleFileVisitor)1 BasicFileAttributes (org.lukhnos.nnio.file.attribute.BasicFileAttributes)1