Skip to content

Commit c5bcbdf

Browse files
authored
refactor(android): various cleanup (#607)
* refactor(android): optimize imports (removed unused & sorted) * refactor(android): reindent files and remove trailing spaces * refactor(android): remove duplicate Exception catch * refactor(android): remove unused global permissions variable
1 parent 360a73c commit c5bcbdf

File tree

6 files changed

+524
-583
lines changed

6 files changed

+524
-583
lines changed

src/android/AssetFilesystem.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ Licensed to the Apache Software Foundation (ASF) under one
2424
import org.apache.cordova.CordovaPreferences;
2525
import org.apache.cordova.CordovaResourceApi;
2626
import org.apache.cordova.LOG;
27-
import org.json.JSONArray;
2827
import org.json.JSONException;
2928
import org.json.JSONObject;
3029

@@ -137,7 +136,7 @@ private long getAssetSize(String assetPath) throws FileNotFoundException {
137136
public AssetFilesystem(AssetManager assetManager, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
138137
super(Uri.parse("file:///android_asset/"), "assets", resourceApi, preferences);
139138
this.assetManager = assetManager;
140-
}
139+
}
141140

142141
@Override
143142
public Uri toNativeUri(LocalFilesystemURL inputURL) {
@@ -204,7 +203,7 @@ public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws Fil
204203
entries[i] = localUrlforFullPath(new File(inputURL.path, files[i]).getPath());
205204
}
206205
return entries;
207-
}
206+
}
208207

209208
@Override
210209
public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
@@ -241,25 +240,25 @@ public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
241240
}
242241

243242
@Override
244-
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
243+
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
245244
JSONObject metadata = new JSONObject();
246245
long size = inputURL.isDirectory ? 0 : getAssetSize(inputURL.path);
247246
try {
248-
metadata.put("size", size);
249-
metadata.put("type", inputURL.isDirectory ? "text/directory" : resourceApi.getMimeType(toNativeUri(inputURL)));
250-
metadata.put("name", new File(inputURL.path).getName());
251-
metadata.put("fullPath", inputURL.path);
252-
metadata.put("lastModifiedDate", 0);
247+
metadata.put("size", size);
248+
metadata.put("type", inputURL.isDirectory ? "text/directory" : resourceApi.getMimeType(toNativeUri(inputURL)));
249+
metadata.put("name", new File(inputURL.path).getName());
250+
metadata.put("fullPath", inputURL.path);
251+
metadata.put("lastModifiedDate", 0);
253252
} catch (JSONException e) {
254253
return null;
255254
}
256255
return metadata;
257-
}
256+
}
258257

259-
@Override
260-
public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
261-
return false;
262-
}
258+
@Override
259+
public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
260+
return false;
261+
}
263262

264263
@Override
265264
long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset, boolean isBinary) throws NoModificationAllowedException, IOException {

src/android/ContentFilesystem.java

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public class ContentFilesystem extends Filesystem {
3838

3939
private final Context context;
4040

41-
public ContentFilesystem(Context context, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
42-
super(Uri.parse("content://"), "content", resourceApi, preferences);
41+
public ContentFilesystem(Context context, CordovaResourceApi resourceApi, CordovaPreferences preferences) {
42+
super(Uri.parse("content://"), "content", resourceApi, preferences);
4343
this.context = context;
44-
}
44+
}
4545

4646
@Override
4747
public Uri toNativeUri(LocalFilesystemURL inputURL) {
@@ -84,41 +84,41 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) {
8484
}
8585

8686
@Override
87-
public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
88-
String fileName, JSONObject options, boolean directory) throws IOException, TypeMismatchException, JSONException {
87+
public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL,
88+
String fileName, JSONObject options, boolean directory) throws IOException, TypeMismatchException, JSONException {
8989
throw new UnsupportedOperationException("getFile() not supported for content:. Use resolveLocalFileSystemURL instead.");
90-
}
90+
}
9191

92-
@Override
93-
public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL)
94-
throws NoModificationAllowedException {
92+
@Override
93+
public boolean removeFileAtLocalURL(LocalFilesystemURL inputURL)
94+
throws NoModificationAllowedException {
9595
Uri contentUri = toNativeUri(inputURL);
96-
try {
96+
try {
9797
context.getContentResolver().delete(contentUri, null, null);
98-
} catch (UnsupportedOperationException t) {
99-
// Was seeing this on the File mobile-spec tests on 4.0.3 x86 emulator.
100-
// The ContentResolver applies only when the file was registered in the
101-
// first case, which is generally only the case with images.
98+
} catch (UnsupportedOperationException t) {
99+
// Was seeing this on the File mobile-spec tests on 4.0.3 x86 emulator.
100+
// The ContentResolver applies only when the file was registered in the
101+
// first case, which is generally only the case with images.
102102
NoModificationAllowedException nmae = new NoModificationAllowedException("Deleting not supported for content uri: " + contentUri);
103103
nmae.initCause(t);
104104
throw nmae;
105-
}
105+
}
106106
return true;
107-
}
107+
}
108108

109-
@Override
110-
public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
111-
throws NoModificationAllowedException {
112-
throw new NoModificationAllowedException("Cannot remove content url");
113-
}
109+
@Override
110+
public boolean recursiveRemoveFileAtLocalURL(LocalFilesystemURL inputURL)
111+
throws NoModificationAllowedException {
112+
throw new NoModificationAllowedException("Cannot remove content url");
113+
}
114114

115115
@Override
116116
public LocalFilesystemURL[] listChildren(LocalFilesystemURL inputURL) throws FileNotFoundException {
117117
throw new UnsupportedOperationException("readEntriesAtLocalURL() not supported for content:. Use resolveLocalFileSystemURL instead.");
118118
}
119119

120-
@Override
121-
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
120+
@Override
121+
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
122122
long size = -1;
123123
long lastModified = 0;
124124
Uri nativeUri = toNativeUri(inputURL);
@@ -143,55 +143,55 @@ public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws
143143
fnfe.initCause(e);
144144
throw fnfe;
145145
} finally {
146-
if (cursor != null)
147-
cursor.close();
146+
if (cursor != null)
147+
cursor.close();
148148
}
149149

150150
JSONObject metadata = new JSONObject();
151151
try {
152-
metadata.put("size", size);
153-
metadata.put("type", mimeType);
154-
metadata.put("name", name);
155-
metadata.put("fullPath", inputURL.path);
156-
metadata.put("lastModifiedDate", lastModified);
152+
metadata.put("size", size);
153+
metadata.put("type", mimeType);
154+
metadata.put("name", name);
155+
metadata.put("fullPath", inputURL.path);
156+
metadata.put("lastModifiedDate", lastModified);
157157
} catch (JSONException e) {
158-
return null;
158+
return null;
159159
}
160160
return metadata;
161-
}
161+
}
162162

163-
@Override
164-
public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
165-
int offset, boolean isBinary) throws NoModificationAllowedException {
163+
@Override
164+
public long writeToFileAtURL(LocalFilesystemURL inputURL, String data,
165+
int offset, boolean isBinary) throws NoModificationAllowedException {
166166
throw new NoModificationAllowedException("Couldn't write to file given its content URI");
167167
}
168-
@Override
169-
public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
170-
throws NoModificationAllowedException {
168+
@Override
169+
public long truncateFileAtURL(LocalFilesystemURL inputURL, long size)
170+
throws NoModificationAllowedException {
171171
throw new NoModificationAllowedException("Couldn't truncate file given its content URI");
172-
}
172+
}
173173

174-
protected Cursor openCursorForURL(Uri nativeUri) {
174+
protected Cursor openCursorForURL(Uri nativeUri) {
175175
ContentResolver contentResolver = context.getContentResolver();
176176
try {
177177
return contentResolver.query(nativeUri, null, null, null, null);
178178
} catch (UnsupportedOperationException e) {
179179
return null;
180180
}
181-
}
181+
}
182182

183-
private Long resourceSizeForCursor(Cursor cursor) {
183+
private Long resourceSizeForCursor(Cursor cursor) {
184184
int columnIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
185185
if (columnIndex != -1) {
186186
String sizeStr = cursor.getString(columnIndex);
187187
if (sizeStr != null) {
188-
return Long.parseLong(sizeStr);
188+
return Long.parseLong(sizeStr);
189189
}
190190
}
191191
return null;
192-
}
193-
194-
protected Long lastModifiedDateForCursor(Cursor cursor) {
192+
}
193+
194+
protected Long lastModifiedDateForCursor(Cursor cursor) {
195195
int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED);
196196
if (columnIndex == -1) {
197197
columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
@@ -203,22 +203,22 @@ protected Long lastModifiedDateForCursor(Cursor cursor) {
203203
}
204204
}
205205
return null;
206-
}
206+
}
207207

208208
@Override
209209
public String filesystemPathForURL(LocalFilesystemURL url) {
210210
File f = resourceApi.mapUriToFile(toNativeUri(url));
211211
return f == null ? null : f.getAbsolutePath();
212212
}
213213

214-
@Override
215-
public LocalFilesystemURL URLforFilesystemPath(String path) {
216-
// Returns null as we don't support reverse mapping back to content:// URLs
217-
return null;
218-
}
214+
@Override
215+
public LocalFilesystemURL URLforFilesystemPath(String path) {
216+
// Returns null as we don't support reverse mapping back to content:// URLs
217+
return null;
218+
}
219219

220-
@Override
221-
public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
222-
return true;
223-
}
220+
@Override
221+
public boolean canRemoveFileAtLocalURL(LocalFilesystemURL inputURL) {
222+
return true;
223+
}
224224
}

0 commit comments

Comments
 (0)