Merge lp:~karni/ubuntuone-android-files/safe-delete-real-deal into lp:ubuntuone-android-files

Proposed by Michał Karnicki
Status: Merged
Merged at revision: 464
Proposed branch: lp:~karni/ubuntuone-android-files/safe-delete-real-deal
Merge into: lp:ubuntuone-android-files
Diff against target: 247 lines (+79/-58)
4 files modified
src/com/ubuntuone/android/files/activity/FilesActivity.java (+1/-1)
src/com/ubuntuone/android/files/provider/MetaUtilities.java (+54/-39)
src/com/ubuntuone/android/files/service/MetaService.java (+6/-6)
src/com/ubuntuone/android/files/util/FileUtilities.java (+18/-12)
To merge this branch: bzr merge lp:~karni/ubuntuone-android-files/safe-delete-real-deal
Reviewer Review Type Date Requested Status
Paul Hummer (community) Approve
Review via email: mp+146983@code.launchpad.net

Commit message

Fixed not deleting local file when performing delete.

Description of the change

Properly delete locally cached files when delete is performed by the user.
Do not delete non empty directories.

To post a comment you must log in.
Revision history for this message
Paul Hummer (rockstar) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/com/ubuntuone/android/files/activity/FilesActivity.java'
2--- src/com/ubuntuone/android/files/activity/FilesActivity.java 2013-02-05 16:02:57 +0000
3+++ src/com/ubuntuone/android/files/activity/FilesActivity.java 2013-02-06 23:11:24 +0000
4@@ -1268,7 +1268,7 @@
5 private void onFileDeleteFromDeviceClicked(final FileViewHolder holder) {
6 String path = FileUtilities.getFilePathFromResourcePath(
7 holder.resourcePath);
8- FileUtilities.removeSilently(path);
9+ FileUtilities.safeDeleteSilently(path);
10 MetaUtilities.setStateAndData(holder.resourcePath, null, null);
11 MetaUtilities.notifyChange(Nodes.CONTENT_URI);
12 }
13
14=== modified file 'src/com/ubuntuone/android/files/provider/MetaUtilities.java'
15--- src/com/ubuntuone/android/files/provider/MetaUtilities.java 2012-09-26 00:22:58 +0000
16+++ src/com/ubuntuone/android/files/provider/MetaUtilities.java 2013-02-06 23:11:24 +0000
17@@ -558,53 +558,68 @@
18 return false;
19 }
20
21- public static void deleteByResourcePath(String resourcePath) {
22- String where = Nodes.NODE_RESOURCE_PATH + "=?";
23- String[] selectionArgs = new String[] { resourcePath };
24- sResolver.delete(Nodes.CONTENT_URI, where, selectionArgs);
25- }
26-
27- public static void cleanupTreeByResourcePath(String resourcePath) {
28- Log.i(TAG, "cleaning up tree of: " + resourcePath);
29+ public static void delete(String resourcePath) {
30+ Log.d(TAG, "Delete " + resourcePath);
31 String kindString = MetaUtilities.getStringField(
32 resourcePath, Nodes.NODE_KIND);
33- if (kindString == null) {
34+ if (kindString == null) { // Consistency check.
35+ Log.e(TAG, "Node has null kind!");
36 return;
37 }
38-
39 U1NodeKind kind = U1NodeKind.valueOf(kindString.toUpperCase(Locale.US));
40+
41 if (kind == U1NodeKind.FILE) {
42- String data = FileUtilities.getFilePathFromResourcePath(resourcePath);
43- FileUtilities.removeSilently(data);
44- MetaUtilities.deleteByResourcePath(resourcePath);
45+ deleteFile(resourcePath);
46 } else {
47- String resourcePathFmt = resourcePath + "/%";
48- String[] projection = new String[] {
49- Nodes.NODE_RESOURCE_PATH, Nodes.NODE_DATA
50- };
51- String selection = Nodes.NODE_RESOURCE_PATH + " LIKE ?";
52- String[] selectionArgs = new String[] { resourcePathFmt };
53-
54- Cursor c = sResolver.query(Nodes.CONTENT_URI, projection,
55+ deleteDirectory(resourcePath);
56+ }
57+ }
58+
59+ public static void deleteFile(String fileResPath) {
60+ deleteFileContent(fileResPath);
61+ deleteNodeMetadata(fileResPath);
62+ }
63+
64+ private static void deleteFileContent(String fileResPath) {
65+ String path = FileUtilities.getFilePathFromResourcePath(fileResPath);
66+ FileUtilities.safeDeleteSilently(path);
67+ }
68+
69+ public static void deleteDirectory(String dirResPath) {
70+ deleteDirectoryContent(dirResPath);
71+ deleteNodeMetadata(dirResPath);
72+ }
73+
74+ private static void deleteDirectoryContent(String dirResPath) {
75+ String resPathFmt = dirResPath + "/%";
76+ String[] projection =
77+ new String[] { Nodes.NODE_RESOURCE_PATH, Nodes.NODE_DATA };
78+ String selection = Nodes.NODE_RESOURCE_PATH + " LIKE ?";
79+ String[] selectionArgs = new String[] { resPathFmt };
80+
81+ Cursor c = null;
82+ try {
83+ c = sResolver.query(Nodes.CONTENT_URI, projection,
84 selection, selectionArgs, null);
85- if (c != null) {
86- try {
87- while (c.moveToNext()) {
88- String data = FileUtilities.getFilePathFromResourcePath(
89- resourcePathFmt);
90- FileUtilities.removeSilently(data);
91- }
92- } finally {
93- c.close();
94- }
95+ if (c != null && c.moveToFirst()) {
96+ do {
97+ String resPath = c.getString(c.getColumnIndex(
98+ Nodes.NODE_RESOURCE_PATH));
99+ delete(resPath);
100+ } while (c.moveToNext());
101 }
102-
103- sResolver.delete(Nodes.CONTENT_URI, selection, selectionArgs);
104-
105- selection = Nodes.NODE_RESOURCE_PATH + "=?";
106- selectionArgs = new String[] { resourcePath };
107- sResolver.delete(Nodes.CONTENT_URI, selection, selectionArgs);
108+ } finally {
109+ if (c != null) c.close();
110 }
111+
112+ String path = FileUtilities.getFilePathFromResourcePath(dirResPath);
113+ FileUtilities.safeDeleteSilently(path);
114+ }
115+
116+ public static void deleteNodeMetadata(String resourcePath) {
117+ String where = Nodes.NODE_RESOURCE_PATH + "=?";
118+ String[] selectionArgs = new String[] { resourcePath };
119+ sResolver.delete(Nodes.CONTENT_URI, where, selectionArgs);
120 }
121
122 public static Uri buildNodeUri(int id) {
123@@ -764,10 +779,10 @@
124 }
125 } else {
126 if (node.getKind() == U1NodeKind.FILE) {
127- FileUtilities.removeSilently(FileUtilities
128+ FileUtilities.safeDeleteSilently(FileUtilities
129 .getFilePathFromResourcePath(resourcePath));
130 }
131- MetaUtilities.deleteByResourcePath(resourcePath);
132+ MetaUtilities.deleteNodeMetadata(resourcePath);
133 }
134 MetaUtilities.notifyChange(Nodes.CONTENT_URI);
135 }
136
137=== modified file 'src/com/ubuntuone/android/files/service/MetaService.java'
138--- src/com/ubuntuone/android/files/service/MetaService.java 2013-02-02 12:48:16 +0000
139+++ src/com/ubuntuone/android/files/service/MetaService.java 2013-02-06 23:11:24 +0000
140@@ -299,7 +299,7 @@
141 if (NetworkUtil.isConnected(MetaService.this)) {
142 // We are connected, thus left cachedNodePaths are invalid.
143 for (String oldNodePath : cachedNodePaths) {
144- MetaUtilities.cleanupTreeByResourcePath(oldNodePath);
145+ MetaUtilities.delete(oldNodePath);
146 }
147 }
148 contentResolver.notifyChange(Nodes.CONTENT_URI, null);
149@@ -445,7 +445,7 @@
150 if (!oldHash.equals(newHash)) {
151 String path = FileUtilities.getFilePathFromResourcePath(
152 node.getResourcePath());
153- FileUtilities.removeSilently(path);
154+ FileUtilities.safeDeleteSilently(path);
155 data = "";
156 }
157 }
158@@ -660,14 +660,15 @@
159 api.deleteNode(resourcePath, new U1NodeListener() {
160 @Override
161 public void onStart() {
162- receiver.send(Status.RUNNING, data);
163+ if (receiver != null)
164+ receiver.send(Status.RUNNING, data);
165 MetaUtilities.setState(resourcePath, ResourceState.STATE_DELETING);
166 MetaUtilities.notifyChange(Nodes.CONTENT_URI);
167 }
168
169 @Override
170 public void onSuccess(U1Node node) {
171- MetaUtilities.deleteByResourcePath(resourcePath);
172+ MetaUtilities.delete(resourcePath);
173 }
174
175 @Override
176@@ -682,7 +683,6 @@
177
178 @Override
179 public void onFinish() {
180- MetaUtilities.deleteByResourcePath(resourcePath);
181 MetaUtilities.notifyChange(Nodes.CONTENT_URI);
182 if (receiver != null)
183 receiver.send(Status.FINISHED, data);
184@@ -760,7 +760,7 @@
185 values.put(Nodes.NODE_DATA, "");
186
187 String data = c.getString(c.getColumnIndex(Nodes.NODE_DATA));
188- FileUtilities.removeSilently(data);
189+ FileUtilities.safeDeleteSilently(data);
190
191 Uri uri = MetaUtilities.buildNodeUri(id);
192 ContentProviderOperation op = ContentProviderOperation
193
194=== modified file 'src/com/ubuntuone/android/files/util/FileUtilities.java'
195--- src/com/ubuntuone/android/files/util/FileUtilities.java 2012-08-06 20:03:41 +0000
196+++ src/com/ubuntuone/android/files/util/FileUtilities.java 2013-02-06 23:11:24 +0000
197@@ -191,7 +191,7 @@
198 return result;
199 }
200
201- public static void removeSilently(String path) {
202+ public static void safeDeleteSilently(String path) {
203 try {
204 if (!TextUtils.isEmpty(path)) {
205 // Legacy check.
206@@ -200,17 +200,12 @@
207 }
208
209 File file = new File(path);
210- if (file.exists()) {
211- if (file.isDirectory()) {
212- File[] children = file.listFiles();
213- if (children != null) {
214- for (File child : children) {
215- removeSilently(child.getAbsolutePath());
216- }
217- }
218- } else {
219- file.delete();
220- }
221+ if (!file.exists()) return;
222+
223+ if (file.isDirectory() && isDirectoryEmpty(file)) {
224+ file.delete();
225+ } else if (file.isFile()) {
226+ file.delete();
227 }
228 }
229 } catch (Exception e) {
230@@ -218,6 +213,17 @@
231 }
232 }
233
234+ private static boolean isDirectoryEmpty(File dir) {
235+ File[] children = dir.listFiles();
236+ if (children == null) {
237+ return true;
238+ }
239+ if (children.length == 0) {
240+ return true;
241+ }
242+ return false;
243+ }
244+
245 public static String sanitize(String filename) {
246 return filename.replace(":", "_");
247 }

Subscribers

People subscribed via source and target branches

to status/vote changes: