Browse Source

Use Room/DAO for getFolderContent

Signed-off-by: Dariusz Olszewski <starypatyk@users.noreply.github.com>
Dariusz Olszewski 2 years ago
parent
commit
cdeff1696b

+ 6 - 0
app/src/main/java/com/nextcloud/client/database/DatabaseModule.kt

@@ -25,6 +25,7 @@ package com.nextcloud.client.database
 import android.content.Context
 import com.nextcloud.client.core.Clock
 import com.nextcloud.client.database.dao.ArbitraryDataDao
+import com.nextcloud.client.database.dao.FileDao
 import dagger.Module
 import dagger.Provides
 import javax.inject.Singleton
@@ -42,4 +43,9 @@ class DatabaseModule {
     fun arbitraryDataDao(nextcloudDatabase: NextcloudDatabase): ArbitraryDataDao {
         return nextcloudDatabase.arbitraryDataDao()
     }
+
+    @Provides
+    fun fileDao(nextcloudDatabase: NextcloudDatabase): FileDao {
+        return nextcloudDatabase.fileDao()
+    }
 }

+ 2 - 0
app/src/main/java/com/nextcloud/client/database/NextcloudDatabase.kt

@@ -30,6 +30,7 @@ import androidx.room.RoomDatabase
 import com.nextcloud.client.core.Clock
 import com.nextcloud.client.core.ClockImpl
 import com.nextcloud.client.database.dao.ArbitraryDataDao
+import com.nextcloud.client.database.dao.FileDao
 import com.nextcloud.client.database.entity.ArbitraryDataEntity
 import com.nextcloud.client.database.entity.CapabilityEntity
 import com.nextcloud.client.database.entity.ExternalLinkEntity
@@ -65,6 +66,7 @@ import com.owncloud.android.db.ProviderMeta
 abstract class NextcloudDatabase : RoomDatabase() {
 
     abstract fun arbitraryDataDao(): ArbitraryDataDao
+    abstract fun fileDao(): FileDao
 
     companion object {
         const val FIRST_ROOM_DB_VERSION = 65

+ 33 - 0
app/src/main/java/com/nextcloud/client/database/dao/FileDao.kt

@@ -0,0 +1,33 @@
+/*
+ * Nextcloud Android client application
+ *
+ *  @author Dariusz Olszewski
+ *  Copyright (C) 2022 Dariusz Olszewski
+ *  Copyright (C) 2022 Nextcloud GmbH
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public
+ * License along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.nextcloud.client.database.dao
+
+import androidx.room.Dao
+import androidx.room.Query
+import com.nextcloud.client.database.entity.FileEntity
+
+@Dao
+interface FileDao {
+    @Query("SELECT * FROM filelist WHERE parent = :parentId")
+    fun getFolderContent(parentId: Long): List<FileEntity>
+}

+ 92 - 35
app/src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -41,6 +41,9 @@ import android.text.TextUtils;
 import com.google.gson.Gson;
 import com.google.gson.JsonSyntaxException;
 import com.nextcloud.client.account.User;
+import com.nextcloud.client.database.NextcloudDatabase;
+import com.nextcloud.client.database.dao.FileDao;
+import com.nextcloud.client.database.entity.FileEntity;
 import com.owncloud.android.MainApp;
 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
 import com.owncloud.android.lib.common.network.WebdavEntry;
@@ -91,6 +94,9 @@ public class FileDataStorageManager {
     private final ContentProviderClient contentProviderClient;
     private final User user;
 
+    private final FileDao fileDao = NextcloudDatabase.getInstance(MainApp.getAppContext()).fileDao();
+    private final Gson gson = new Gson();
+
     public FileDataStorageManager(User user, ContentResolver contentResolver) {
         this.contentProviderClient = null;
         this.contentResolver = contentResolver;
@@ -861,46 +867,18 @@ public class FileDataStorageManager {
     }
 
     private List<OCFile> getFolderContent(long parentId, boolean onlyOnDevice) {
+        Log_OC.d(TAG, "getFolderContent - start");
         List<OCFile> folderContent = new ArrayList<>();
 
-        Uri requestURI = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, String.valueOf(parentId));
-        Cursor cursor;
-
-        if (getContentProviderClient() != null) {
-            try {
-                cursor = getContentProviderClient().query(
-                    requestURI,
-                    null,
-                    ProviderTableMeta.FILE_PARENT + "=?",
-                    new String[]{String.valueOf(parentId)},
-                    null
-                );
-            } catch (RemoteException e) {
-                Log_OC.e(TAG, e.getMessage(), e);
-                return folderContent;
-            }
-        } else {
-            cursor = getContentResolver().query(
-                requestURI,
-                null,
-                ProviderTableMeta.FILE_PARENT + "=?",
-                new String[]{String.valueOf(parentId)},
-                null
-                                               );
-        }
-
-        if (cursor != null) {
-            if (cursor.moveToFirst()) {
-                do {
-                    OCFile child = createFileInstance(cursor);
-                    if (!onlyOnDevice || child.existsOnDevice()) {
-                        folderContent.add(child);
-                    }
-                } while (cursor.moveToNext());
+        List<FileEntity> files = fileDao.getFolderContent(parentId);
+        for (FileEntity fileEntity: files) {
+            OCFile child = createFileInstance(fileEntity);
+            if (!onlyOnDevice || child.existsOnDevice()) {
+                folderContent.add(child);
             }
-            cursor.close();
         }
 
+        Log_OC.d(TAG, "getFolderContent - finished");
         return folderContent;
     }
 
@@ -1046,6 +1024,85 @@ public class FileDataStorageManager {
         return ocFile;
     }
 
+    private OCFile createFileInstance(FileEntity fileEntity) {
+        OCFile ocFile = new OCFile(fileEntity.getPath());
+        ocFile.setDecryptedRemotePath(fileEntity.getPathDecrypted());
+        ocFile.setFileId(fileEntity.getId());
+        ocFile.setParentId(fileEntity.getParent());
+        ocFile.setMimeType(fileEntity.getContentType());
+        ocFile.setStoragePath(fileEntity.getStoragePath());
+        if (ocFile.getStoragePath() == null) {
+            // try to find existing file and bind it with current account;
+            // with the current update of SynchronizeFolderOperation, this won't be
+            // necessary anymore after a full synchronization of the account
+            File file = new File(FileStorageUtils.getDefaultSavePathFor(user.getAccountName(), ocFile));
+            if (file.exists()) {
+                ocFile.setStoragePath(file.getAbsolutePath());
+                ocFile.setLastSyncDateForData(file.lastModified());
+            }
+        }
+        ocFile.setFileLength(fileEntity.getContentLength());
+        ocFile.setCreationTimestamp(fileEntity.getCreation());
+        ocFile.setModificationTimestamp(fileEntity.getModified());
+        ocFile.setModificationTimestampAtLastSyncForData(fileEntity.getModifiedAtLastSyncForData());
+        ocFile.setLastSyncDateForProperties(fileEntity.getLastSyncDate());
+        ocFile.setLastSyncDateForData(fileEntity.getLastSyncDateForData());
+        ocFile.setEtag(fileEntity.getEtag());
+        ocFile.setEtagOnServer(fileEntity.getEtagOnServer());
+        ocFile.setSharedViaLink(fileEntity.getSharedViaLink() == 1);
+        ocFile.setSharedWithSharee(fileEntity.getSharedWithSharee() == 1);
+        ocFile.setPermissions(fileEntity.getPermissions());
+        ocFile.setRemoteId(fileEntity.getRemoteId());
+        ocFile.setUpdateThumbnailNeeded(fileEntity.getUpdateThumbnail() == 1);
+        ocFile.setDownloading(fileEntity.isDownloading() == 1);
+        ocFile.setEtagInConflict(fileEntity.getEtagInConflict());
+        ocFile.setFavorite(fileEntity.getFavorite() == 1);
+        ocFile.setEncrypted(fileEntity.isEncrypted() == 1);
+//        if (ocFile.isEncrypted()) {
+//            ocFile.setFileName(cursor.getString(cursor.getColumnIndexOrThrow(ProviderTableMeta.FILE_NAME)));
+//        }
+        Integer mountType = fileEntity.getMountType();
+        if (mountType != null) {
+            ocFile.setMountType(WebdavEntry.MountType.values()[mountType]);
+        }
+        ocFile.setPreviewAvailable(fileEntity.getHasPreview() == 1);
+        ocFile.setUnreadCommentsCount(fileEntity.getUnreadCommentsCount());
+        ocFile.setOwnerId(fileEntity.getOwnerId());
+        ocFile.setOwnerDisplayName(fileEntity.getOwnerDisplayName());
+        ocFile.setNote(fileEntity.getNote());
+        ocFile.setRichWorkspace(fileEntity.getRichWorkspace());
+        ocFile.setLocked(fileEntity.getLocked() == 1);
+        final int lockTypeInt = fileEntity.getLockType();
+        ocFile.setLockType(lockTypeInt != -1 ? FileLockType.fromValue(lockTypeInt) : null);
+        ocFile.setLockOwnerId(fileEntity.getLockOwner());
+        ocFile.setLockOwnerDisplayName(fileEntity.getLockOwnerDisplayName());
+        ocFile.setLockOwnerEditor(fileEntity.getLockOwnerEditor());
+        ocFile.setLockTimestamp(fileEntity.getLockTimestamp());
+        ocFile.setLockTimeout(fileEntity.getLockTimeout());
+        ocFile.setLockToken(fileEntity.getLockToken());
+
+        String sharees = fileEntity.getSharees();
+        if (sharees == null || NULL_STRING.equals(sharees) || sharees.isEmpty()) {
+            ocFile.setSharees(new ArrayList<>());
+        } else {
+            try {
+                ShareeUser[] shareesArray = gson.fromJson(sharees, ShareeUser[].class);
+                ocFile.setSharees(new ArrayList<>(Arrays.asList(shareesArray)));
+            } catch (JsonSyntaxException e) {
+                // ignore saved value due to api change
+                ocFile.setSharees(new ArrayList<>());
+            }
+        }
+
+        String metadataSize = fileEntity.getMetadataSize();
+        ImageDimension imageDimension = gson.fromJson(metadataSize, ImageDimension.class);
+        if (imageDimension != null) {
+            ocFile.setImageDimension(imageDimension);
+        }
+
+        return ocFile;
+    }
+
     public boolean saveShare(OCShare share) {
         boolean overridden = false;