@@ -104,10 +104,12 @@ public ContentProviderClient getContentProviderClient() {
104
104
public OCFile getFileByPath (String path ) {
105
105
Cursor c = getFileCursorForValue (ProviderTableMeta .FILE_PATH , path );
106
106
OCFile file = null ;
107
- if (c .moveToFirst ()) {
108
- file = createFileInstance (c );
107
+ if (c != null ) {
108
+ if (c .moveToFirst ()) {
109
+ file = createFileInstance (c );
110
+ }
111
+ c .close ();
109
112
}
110
- c .close ();
111
113
if (file == null && OCFile .ROOT_PATH .equals (path )) {
112
114
return createRootDir (); // root should always exist
113
115
}
@@ -118,20 +120,24 @@ public OCFile getFileByPath(String path) {
118
120
public OCFile getFileById (long id ) {
119
121
Cursor c = getFileCursorForValue (ProviderTableMeta ._ID , String .valueOf (id ));
120
122
OCFile file = null ;
121
- if (c .moveToFirst ()) {
122
- file = createFileInstance (c );
123
+ if (c != null ) {
124
+ if (c .moveToFirst ()) {
125
+ file = createFileInstance (c );
126
+ }
127
+ c .close ();
123
128
}
124
- c .close ();
125
129
return file ;
126
130
}
127
131
128
132
public OCFile getFileByLocalPath (String path ) {
129
133
Cursor c = getFileCursorForValue (ProviderTableMeta .FILE_STORAGE_PATH , path );
130
134
OCFile file = null ;
131
- if (c .moveToFirst ()) {
132
- file = createFileInstance (c );
135
+ if (c != null ) {
136
+ if (c .moveToFirst ()) {
137
+ file = createFileInstance (c );
138
+ }
139
+ c .close ();
133
140
}
134
- c .close ();
135
141
return file ;
136
142
}
137
143
@@ -732,84 +738,87 @@ public void moveLocalFile(OCFile file, String targetPath, String targetParentPat
732
738
);
733
739
}
734
740
735
- /// 2. prepare a batch of update operations to change all the descendants
736
- ArrayList <ContentProviderOperation > operations =
737
- new ArrayList <ContentProviderOperation >(c .getCount ());
741
+ List <String > originalPathsToTriggerMediaScan = new ArrayList <>();
742
+ List <String > newPathsToTriggerMediaScan = new ArrayList <>();
738
743
String defaultSavePath = FileStorageUtils .getSavePath (mAccount .name );
739
- List <String > originalPathsToTriggerMediaScan = new ArrayList <String >();
740
- List <String > newPathsToTriggerMediaScan = new ArrayList <String >();
741
- if (c .moveToFirst ()) {
742
- int lengthOfOldPath = file .getRemotePath ().length ();
743
- int lengthOfOldStoragePath = defaultSavePath .length () + lengthOfOldPath ;
744
- do {
745
- ContentValues cv = new ContentValues (); // keep construction in the loop
746
- OCFile child = createFileInstance (c );
747
- cv .put (
744
+
745
+ /// 2. prepare a batch of update operations to change all the descendants
746
+ if (c != null ) {
747
+ ArrayList <ContentProviderOperation > operations =
748
+ new ArrayList <>(c .getCount ());
749
+ if (c .moveToFirst ()) {
750
+ int lengthOfOldPath = file .getRemotePath ().length ();
751
+ int lengthOfOldStoragePath = defaultSavePath .length () + lengthOfOldPath ;
752
+ do {
753
+ ContentValues cv = new ContentValues (); // keep construction in the loop
754
+ OCFile child = createFileInstance (c );
755
+ cv .put (
748
756
ProviderTableMeta .FILE_PATH ,
749
757
targetPath + child .getRemotePath ().substring (lengthOfOldPath )
750
- );
751
- if (child .getStoragePath () != null &&
758
+ );
759
+ if (child .getStoragePath () != null &&
752
760
child .getStoragePath ().startsWith (defaultSavePath )) {
753
- // update link to downloaded content - but local move is not done here!
754
- String targetLocalPath = defaultSavePath + targetPath +
761
+ // update link to downloaded content - but local move is not done here!
762
+ String targetLocalPath = defaultSavePath + targetPath +
755
763
child .getStoragePath ().substring (lengthOfOldStoragePath );
756
764
757
- cv .put (ProviderTableMeta .FILE_STORAGE_PATH , targetLocalPath );
758
-
759
- originalPathsToTriggerMediaScan .add (child .getStoragePath ());
760
- newPathsToTriggerMediaScan .add (targetLocalPath );
765
+ cv .put (ProviderTableMeta .FILE_STORAGE_PATH , targetLocalPath );
761
766
762
- }
763
- if (targetParent .getAvailableOfflineStatus () !=
764
- OCFile .AvailableOfflineStatus .NOT_AVAILABLE_OFFLINE ) {
765
- // moving to an available offline subfolder
766
- cv .put (
767
- ProviderTableMeta .FILE_KEEP_IN_SYNC ,
768
- OCFile .AvailableOfflineStatus .AVAILABLE_OFFLINE_PARENT .getValue ()
769
- );
767
+ originalPathsToTriggerMediaScan .add (child .getStoragePath ());
768
+ newPathsToTriggerMediaScan .add (targetLocalPath );
770
769
771
- } else {
772
- // moving to a not available offline subfolder - with care
773
- if ( file . getAvailableOfflineStatus () ==
774
- OCFile . AvailableOfflineStatus . AVAILABLE_OFFLINE_PARENT ) {
770
+ }
771
+ if ( targetParent . getAvailableOfflineStatus () !=
772
+ OCFile . AvailableOfflineStatus . NOT_AVAILABLE_OFFLINE ) {
773
+ // moving to an available offline subfolder
775
774
cv .put (
776
775
ProviderTableMeta .FILE_KEEP_IN_SYNC ,
777
- OCFile .AvailableOfflineStatus .NOT_AVAILABLE_OFFLINE .getValue ()
776
+ OCFile .AvailableOfflineStatus .AVAILABLE_OFFLINE_PARENT .getValue ()
778
777
);
778
+
779
+ } else {
780
+ // moving to a not available offline subfolder - with care
781
+ if (file .getAvailableOfflineStatus () ==
782
+ OCFile .AvailableOfflineStatus .AVAILABLE_OFFLINE_PARENT ) {
783
+ cv .put (
784
+ ProviderTableMeta .FILE_KEEP_IN_SYNC ,
785
+ OCFile .AvailableOfflineStatus .NOT_AVAILABLE_OFFLINE .getValue ()
786
+ );
787
+ }
779
788
}
780
- }
781
789
782
- if (child .getRemotePath ().equals (file .getRemotePath ())) {
783
- cv .put (
784
- ProviderTableMeta .FILE_PARENT ,
785
- targetParent .getFileId ()
786
- );
787
- }
788
- operations .add (
790
+ if (child .getRemotePath ().equals (file .getRemotePath ())) {
791
+ cv .put (
792
+ ProviderTableMeta .FILE_PARENT ,
793
+ targetParent .getFileId ()
794
+ );
795
+ }
796
+ operations .add (
789
797
ContentProviderOperation .newUpdate (ProviderTableMeta .CONTENT_URI ).
790
- withValues (cv ).
791
- withSelection (
792
- ProviderTableMeta ._ID + "=?" ,
793
- new String []{String .valueOf (child .getFileId ())}
794
- )
795
- .build ());
798
+ withValues (cv ).
799
+ withSelection (
800
+ ProviderTableMeta ._ID + "=?" ,
801
+ new String []{String .valueOf (child .getFileId ())}
802
+ )
803
+ .build ());
796
804
797
- } while (c .moveToNext ());
798
- }
799
- c .close ();
805
+ } while (c .moveToNext ());
806
+ }
807
+ c .close ();
800
808
801
- /// 3. apply updates in batch
802
- try {
803
- if (getContentResolver () != null ) {
804
- getContentResolver ().applyBatch (MainApp .getAuthority (), operations );
809
+ /// 3. apply updates in batch
810
+ try {
811
+ if (getContentResolver () != null ) {
812
+ getContentResolver ().applyBatch (MainApp .getAuthority (), operations );
805
813
806
- } else {
807
- getContentProviderClient ().applyBatch (operations );
808
- }
814
+ } else {
815
+ getContentProviderClient ().applyBatch (operations );
816
+ }
809
817
810
- } catch (Exception e ) {
811
- Log_OC .e (TAG , "Fail to update " + file .getFileId () + " and descendants in database" ,
818
+ } catch (Exception e ) {
819
+ Log_OC .e (TAG , "Fail to update " + file .getFileId () + " and descendants in database" ,
812
820
e );
821
+ }
813
822
}
814
823
815
824
/// 4. move in local file system
@@ -1000,8 +1009,11 @@ private boolean fileExists(String cmp_key, String value) {
1000
1009
return false ;
1001
1010
}
1002
1011
}
1003
- boolean retval = c .moveToFirst ();
1004
- c .close ();
1012
+ boolean retval = false ;
1013
+ if (c != null ) {
1014
+ retval = c .moveToFirst ();
1015
+ c .close ();
1016
+ }
1005
1017
return retval ;
1006
1018
}
1007
1019
@@ -1200,8 +1212,11 @@ private boolean shareExistsForRemoteId(long remoteId) {
1200
1212
*/
1201
1213
private boolean shareExistsForValue (String key , String value ) {
1202
1214
Cursor c = getShareCursorForValue (key , value );
1203
- boolean retval = c .moveToFirst ();
1204
- c .close ();
1215
+ boolean retval = false ;
1216
+ if (c != null ) {
1217
+ retval = c .moveToFirst ();
1218
+ c .close ();
1219
+ }
1205
1220
return retval ;
1206
1221
}
1207
1222
@@ -1303,10 +1318,12 @@ public OCShare getFirstShareByPathAndType(String path, ShareType type, String sh
1303
1318
}
1304
1319
}
1305
1320
OCShare share = null ;
1306
- if (c .moveToFirst ()) {
1307
- share = createShareInstance (c );
1321
+ if (c != null ) {
1322
+ if (c .moveToFirst ()) {
1323
+ share = createShareInstance (c );
1324
+ }
1325
+ c .close ();
1308
1326
}
1309
- c .close ();
1310
1327
return share ;
1311
1328
}
1312
1329
@@ -1831,14 +1848,16 @@ public ArrayList<OCShare> getSharesWithForAFile(String filePath, String accountN
1831
1848
}
1832
1849
ArrayList <OCShare > shares = new ArrayList <OCShare >();
1833
1850
OCShare share = null ;
1834
- if (c .moveToFirst ()) {
1835
- do {
1836
- share = createShareInstance (c );
1837
- shares .add (share );
1838
- // }
1839
- } while (c .moveToNext ());
1851
+ if (c != null ) {
1852
+ if (c .moveToFirst ()) {
1853
+ do {
1854
+ share = createShareInstance (c );
1855
+ shares .add (share );
1856
+ // }
1857
+ } while (c .moveToNext ());
1858
+ }
1859
+ c .close ();
1840
1860
}
1841
- c .close ();
1842
1861
1843
1862
return shares ;
1844
1863
}
@@ -2161,12 +2180,13 @@ public OCCapability getCapability(String accountName){
2161
2180
OCCapability capability = null ;
2162
2181
Cursor c = getCapabilityCursorForAccount (accountName );
2163
2182
2164
- if (c .moveToFirst ()) {
2165
- capability = createCapabilityInstance (c );
2166
- } else {
2167
- capability = new OCCapability (); // return default with all UNKNOWN
2183
+ capability = new OCCapability (); // default value with all UNKNOWN
2184
+ if (c != null ) {
2185
+ if (c .moveToFirst ()) {
2186
+ capability = createCapabilityInstance (c );
2187
+ }
2188
+ c .close ();
2168
2189
}
2169
- c .close ();
2170
2190
return capability ;
2171
2191
}
2172
2192
0 commit comments