Skip to content

Commit 93b2c73

Browse files
committed
Fixed folder deletion
1 parent 0c05c3e commit 93b2c73

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

src/android/app/src/main/java/info/cemu/Cemu/features/DocumentsProvider.java

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,39 +112,62 @@ public String createDocument(String parentDocumentId, String mimeType, String di
112112
@Override
113113
public void deleteDocument(String documentId) throws FileNotFoundException {
114114
var file = getFile(documentId);
115+
if (file.isDirectory()) {
116+
deleteFolder(file);
117+
return;
118+
}
115119
if (!file.delete())
116-
throw new FileNotFoundException("Couldn't delete document with ID '$documentId'");
120+
throw new FileNotFoundException("Couldn't delete document with ID " + documentId);
121+
}
122+
123+
private void deleteFolder(File dirFile) throws FileNotFoundException {
124+
if (!dirFile.isDirectory())
125+
return;
126+
var files = dirFile.listFiles();
127+
if (files == null) return;
128+
for (var file : files) {
129+
if (file.isDirectory()) {
130+
deleteFolder(file);
131+
continue;
132+
}
133+
if (!file.delete())
134+
throw new FileNotFoundException("Couldn't delete file " + file.getPath());
135+
}
136+
if (!dirFile.delete())
137+
throw new FileNotFoundException("Couldn't delete file " + dirFile.getPath());
117138
}
118139

119140
@Override
120141
public void removeDocument(String documentId, String parentDocumentId) throws FileNotFoundException {
121142
var parent = getFile(parentDocumentId);
122143
var file = getFile(documentId);
123144

124-
if (parent.equals(file) || file.getParentFile() == null || file.getParentFile().equals(parent)) {
125-
if (!file.delete())
126-
throw new FileNotFoundException("Couldn't delete document with ID '$documentId'");
127-
} else {
128-
throw new FileNotFoundException("Couldn't delete document with ID '$documentId'");
145+
if (!(parent.equals(file) || file.getParentFile() == null || file.getParentFile().equals(parent)))
146+
throw new FileNotFoundException("Couldn't delete document with ID " + documentId);
147+
if (file.isDirectory()) {
148+
deleteFolder(file);
149+
return;
129150
}
151+
if (!file.delete())
152+
throw new FileNotFoundException("Couldn't delete document with ID " + documentId);
130153
}
131154

132155
@Override
133156
public String renameDocument(String documentId, String displayName) throws FileNotFoundException {
134157
if (displayName == null)
135-
throw new FileNotFoundException("Couldn't rename document '$documentId' as the new name is null");
158+
throw new FileNotFoundException("Couldn't rename document " + documentId + " as the new name is null");
136159

137160
var sourceFile = getFile(documentId);
138161
var sourceParentFile = sourceFile.getParentFile();
139162
if (sourceParentFile == null)
140-
throw new FileNotFoundException("Couldn't rename document '$documentId' as it has no parent");
163+
throw new FileNotFoundException("Couldn't rename document '" + documentId + "' as it has no parent");
141164
var destFile = resolve(sourceParentFile, displayName);
142165

143166
try {
144167
if (!sourceFile.renameTo(destFile))
145-
throw new FileNotFoundException("Couldn't rename document from '${sourceFile.name}' to '${destFile.name}'");
168+
throw new FileNotFoundException("Couldn't rename document from '" + sourceFile.getName() + "' to '" + destFile.getName() + "'");
146169
} catch (Exception exception) {
147-
throw new FileNotFoundException("Couldn't rename document from '${sourceFile.name}' to '${destFile.name}': ${e.message}");
170+
throw new FileNotFoundException("Couldn't rename document from '" + sourceFile.getName() + "' to '" + destFile.getName() + "':" + exception.getMessage());
148171
}
149172

150173
return getDocumentId(destFile);
@@ -167,7 +190,7 @@ public String copyDocument(String sourceDocumentId, String targetParentDocumentI
167190
}
168191
}
169192
} catch (IOException exception) {
170-
throw new FileNotFoundException("Couldn't copy document '$sourceDocumentId': ${e.message}");
193+
throw new FileNotFoundException("Couldn't copy document '" + sourceDocumentId + "': " + exception.getMessage());
171194
}
172195
return getDocumentId(newFile);
173196
}
@@ -179,7 +202,7 @@ public String moveDocument(String sourceDocumentId, String sourceParentDocumentI
179202
removeDocument(sourceDocumentId, sourceParentDocumentId);
180203
return newDocumentId;
181204
} catch (FileNotFoundException e) {
182-
throw new FileNotFoundException("Couldn't move document '$sourceDocumentId'");
205+
throw new FileNotFoundException("Couldn't move document '" + sourceDocumentId + "'");
183206
}
184207
}
185208

@@ -208,7 +231,7 @@ private File resolve(File file, String other) {
208231

209232
private String copyDocument(String sourceDocumentId, String sourceParentDocumentId, String targetParentDocumentId) throws FileNotFoundException {
210233
if (!isChildDocument(sourceParentDocumentId, sourceDocumentId))
211-
throw new FileNotFoundException("Couldn't copy document '$sourceDocumentId' as its parent is not '$sourceParentDocumentId'");
234+
throw new FileNotFoundException("Couldn't copy document '" + sourceDocumentId + "' as its parent is not '" + sourceParentDocumentId + "'");
212235
return copyDocument(sourceDocumentId, targetParentDocumentId);
213236
}
214237

0 commit comments

Comments
 (0)