Преглед на файлове

close cursor

Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
tobiasKaminsky преди 7 години
родител
ревизия
3c50d9285d

+ 10 - 8
src/main/java/com/owncloud/android/datamodel/FileDataStorageManager.java

@@ -849,13 +849,16 @@ public class FileDataStorageManager {
             );
         }
 
-        if (c != null && c.moveToFirst()) {
-            do {
-                OCFile child = createFileInstance(c);
-                if (!onlyOnDevice || child.existsOnDevice()) {
-                    ret.add(child);
-                }
-            } while (c.moveToNext());
+        if (c != null) {
+            if (c.moveToFirst()) {
+                do {
+                    OCFile child = createFileInstance(c);
+                    if (!onlyOnDevice || child.existsOnDevice()) {
+                        ret.add(child);
+                    }
+                } while (c.moveToNext());
+            }
+            
             c.close();
         }
 
@@ -2009,7 +2012,6 @@ public class FileDataStorageManager {
         }
 
         return c;
-
     }
 
     public OCCapability getCapability(String accountName) {

+ 15 - 13
src/main/java/com/owncloud/android/datamodel/FilesystemDataProvider.java

@@ -1,4 +1,4 @@
-/**
+/*
  * Nextcloud Android client application
  *
  * Copyright (C) 2017 Mario Danic
@@ -88,17 +88,19 @@ public class FilesystemDataProvider {
                 new String[]{likeParam, syncedFolderId, "0", "0"},
                 null);
 
-        if (cursor != null && cursor.moveToFirst()) {
-            do {
-                String value = cursor.getString(cursor.getColumnIndex(
-                        ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
-                if (value == null) {
-                    Log_OC.e(TAG, "Cannot get local path");
-                } else {
-                    localPathsToUpload.add(value);
-                }
-            } while (cursor.moveToNext());
-
+        if (cursor != null) {
+            if (cursor.moveToFirst()) {
+                do {
+                    String value = cursor.getString(cursor.getColumnIndex(
+                            ProviderMeta.ProviderTableMeta.FILESYSTEM_FILE_LOCAL_PATH));
+                    if (value == null) {
+                        Log_OC.e(TAG, "Cannot get local path");
+                    } else {
+                        localPathsToUpload.add(value);
+                    }
+                } while (cursor.moveToNext());
+            }
+            
             cursor.close();
         }
 
@@ -211,7 +213,7 @@ public class FilesystemDataProvider {
 
     private long getFileChecksum(String filepath) {
 
-        InputStream inputStream = null;
+        InputStream inputStream;
         try {
             inputStream = new BufferedInputStream(new FileInputStream(filepath));
             CRC32 crc = new CRC32();

+ 9 - 3
src/main/java/com/owncloud/android/datamodel/SyncedFolderProvider.java

@@ -1,4 +1,4 @@
-/**
+/*
  * Nextcloud Android client application
  *
  * @author Andy Scherzinger
@@ -143,7 +143,6 @@ public class SyncedFolderProvider extends Observable {
                 // update sync folder object in db
                 result = updateSyncFolder(syncedFolder);
 
-                cursor.close();
             }
         } else {
             if (cursor == null) {
@@ -154,6 +153,10 @@ public class SyncedFolderProvider extends Observable {
             }
         }
 
+        if (cursor != null) {
+            cursor.close();
+        }
+
         return result;
     }
 
@@ -175,7 +178,6 @@ public class SyncedFolderProvider extends Observable {
 
         if (cursor != null && cursor.getCount() == 1) {
             result = createSyncedFolderFromCursor(cursor);
-            cursor.close();
         } else {
             if (cursor == null) {
                 Log_OC.e(TAG, "Sync folder db cursor for local path=" + localPath + " in NULL.");
@@ -185,6 +187,10 @@ public class SyncedFolderProvider extends Observable {
             }
         }
 
+        if (cursor != null) {
+            cursor.close();
+        }
+
         return result;
     }
 

+ 5 - 1
src/main/java/com/owncloud/android/jobs/ContactsBackupJob.java

@@ -70,7 +70,7 @@ public class ContactsBackupJob extends Job {
 
     @NonNull
     @Override
-    protected Result onRunJob(Params params) {
+    protected Result onRunJob(@NonNull Params params) {
         final Context context = MainApp.getAppContext();
         PersistableBundleCompat bundle = params.getExtras();
 
@@ -139,6 +139,10 @@ public class ContactsBackupJob extends Job {
             } catch (IOException e) {
                 Log_OC.d(TAG, "Error ", e);
             } finally {
+                if (cursor != null) {
+                    cursor.close();
+                }
+                
                 if (fw != null) {
                     try {
                         fw.close();

+ 11 - 6
src/main/java/com/owncloud/android/jobs/ContactsImportJob.java

@@ -1,20 +1,20 @@
-/**
+/*
  * Nextcloud Android client application
  *
  * @author Tobias Kaminsky
  * Copyright (C) 2017 Tobias Kaminsky
  * Copyright (C) 2017 Nextcloud GmbH.
- * <p>
+ * 
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * at your option) any later version.
- * <p>
+ * 
  * 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.
- * <p>
+ * 
  * 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/>.
  */
@@ -56,7 +56,7 @@ public class ContactsImportJob extends Job {
 
     @NonNull
     @Override
-    protected Result onRunJob(Params params) {
+    protected Result onRunJob(@NonNull Params params) {
         PersistableBundleCompat bundle = params.getExtras();
 
         String vCardFilePath = bundle.getString(VCARD_FILE_PATH, "");
@@ -67,11 +67,12 @@ public class ContactsImportJob extends Job {
         File file = new File(vCardFilePath);
         ArrayList<VCard> vCards = new ArrayList<>();
 
+        Cursor cursor = null;
         try {
             ContactOperations operations = new ContactOperations(getContext(), accountName, accountType);
             vCards.addAll(Ezvcard.parse(file).all());
             Collections.sort(vCards, new ContactListFragment.VCardComparator());
-            Cursor cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
+            cursor = getContext().getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null,
                     null, null, null);
 
             TreeMap<VCard, Long> ownContactList = new TreeMap<>(new ContactListFragment.VCardComparator());
@@ -101,6 +102,10 @@ public class ContactsImportJob extends Job {
             }
         } catch (Exception e) {
             Log_OC.e(TAG, e.getMessage());
+        } finally {
+            if (cursor != null) {
+                cursor.close();
+            }
         }
 
         return Result.SUCCESS;