From 839210a4caf026f5d59ff24baf2819762cd24f7f Mon Sep 17 00:00:00 2001 From: Alexis THOMAS Date: Sun, 24 Dec 2023 17:18:17 +0100 Subject: [PATCH 1/3] style(android): java code format by the ide --- src/android/ContentFilesystem.java | 10 ++++++---- src/android/DirectoryManager.java | 22 +++++++++++----------- src/android/FileUtils.java | 6 +----- src/android/Filesystem.java | 24 ++++++++++++++---------- src/android/LocalFilesystem.java | 20 ++++++++------------ src/android/LocalFilesystemURL.java | 6 ++++-- src/android/PendingRequests.java | 19 ++++++++++--------- 7 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/android/ContentFilesystem.java b/src/android/ContentFilesystem.java index b4b0a4ee9..8231581d4 100644 --- a/src/android/ContentFilesystem.java +++ b/src/android/ContentFilesystem.java @@ -25,6 +25,7 @@ Licensed to the Apache Software Foundation (ASF) under one import android.provider.DocumentsContract; import android.provider.MediaStore; import android.provider.OpenableColumns; + import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -78,14 +79,14 @@ public LocalFilesystemURL toLocalUri(Uri inputURL) { b.appendEncodedPath(subPath); } Uri localUri = b.encodedQuery(inputURL.getEncodedQuery()) - .encodedFragment(inputURL.getEncodedFragment()) - .build(); + .encodedFragment(inputURL.getEncodedFragment()) + .build(); return LocalFilesystemURL.parse(localUri); } @Override public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, - String fileName, JSONObject options, boolean directory) throws IOException, TypeMismatchException, JSONException { + String fileName, JSONObject options, boolean directory) throws IOException, TypeMismatchException, JSONException { throw new UnsupportedOperationException("getFile() not supported for content:. Use resolveLocalFileSystemURL instead."); } @@ -162,9 +163,10 @@ public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws @Override public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, - int offset, boolean isBinary) throws NoModificationAllowedException { + int offset, boolean isBinary) throws NoModificationAllowedException { throw new NoModificationAllowedException("Couldn't write to file given its content URI"); } + @Override public long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws NoModificationAllowedException { diff --git a/src/android/DirectoryManager.java b/src/android/DirectoryManager.java index 07af5ea20..9e3ffc8e5 100644 --- a/src/android/DirectoryManager.java +++ b/src/android/DirectoryManager.java @@ -26,7 +26,7 @@ Licensed to the Apache Software Foundation (ASF) under one /** * This class provides file directory utilities. * All file operations are performed on the SD card. - * + *

* It is used by the FileUtils class. */ public class DirectoryManager { @@ -36,8 +36,9 @@ public class DirectoryManager { /** * Determine if a file or directory exists. - * @param name The name of the file to check. - * @return T=exists, F=not found + * + * @param name The name of the file to check. + * @return T=exists, F=not found */ public static boolean testFileExists(String name) { boolean status; @@ -58,7 +59,7 @@ public static boolean testFileExists(String name) { /** * Get the free space in external storage * - * @return Size in KB or -1 if not available + * @return Size in KB or -1 if not available */ public static long getFreeExternalStorageSpace() { String status = Environment.getExternalStorageState(); @@ -96,7 +97,7 @@ public static long getFreeSpaceInBytes(String path) { /** * Determine if SD card exists. * - * @return T=exists, F=not found + * @return T=exists, F=not found */ public static boolean testSaveLocationExists() { String sDCardStatus = Environment.getExternalStorageState(); @@ -117,16 +118,15 @@ public static boolean testSaveLocationExists() { /** * Create a new file object from two file paths. * - * @param file1 Base file path - * @param file2 Remaining file path - * @return File object + * @param file1 Base file path + * @param file2 Remaining file path + * @return File object */ - private static File constructFilePaths (String file1, String file2) { + private static File constructFilePaths(String file1, String file2) { File newPath; if (file2.startsWith(file1)) { newPath = new File(file2); - } - else { + } else { newPath = new File(file1 + "/" + file2); } return newPath; diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 0edf90a0e..92b0402c8 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -553,7 +553,7 @@ private void getWritePermission(String rawArgs, int action, CallbackContext call * If your app targets Android 13 (SDK 33) or higher and needs to access media files that other apps have created, * you must request one or more of the following granular media permissions READ_MEDIA_* * instead of the READ_EXTERNAL_STORAGE permission: - * + *

* Refer to: https://developer.android.com/about/versions/13/behavior-changes-13 * * @return @@ -659,7 +659,6 @@ public LocalFilesystemURL filesystemURLforLocalPath(String localPath) { return localURL; } - /* helper to execute functions async and handle the result codes * */ @@ -834,7 +833,6 @@ private boolean removeRecursively(String baseURLstr) throws FileExistsException, } } - /** * Deletes a file or directory. It is an error to attempt to delete a directory that is not empty. * It is an error to attempt to delete the root directory of a filesystem. @@ -1123,7 +1121,6 @@ public void handleData(InputStream inputStream, String contentType) { } } - /** * Write contents of file. * @@ -1168,7 +1165,6 @@ private long truncateFile(String srcURLstr, long size) throws FileNotFoundExcept } } - /* * Handle the response */ diff --git a/src/android/Filesystem.java b/src/android/Filesystem.java index 115b3bc36..7c36ac157 100644 --- a/src/android/Filesystem.java +++ b/src/android/Filesystem.java @@ -106,7 +106,7 @@ public JSONObject makeEntryForFile(File file) { } abstract JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, String path, - JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException; + JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException; abstract boolean removeFileAtLocalURL(LocalFilesystemURL inputURL) throws InvalidModificationException, NoModificationAllowedException; @@ -174,13 +174,13 @@ protected static String normalizePath(String rawPath) { if (components.get(index).equals("..")) { components.remove(index); if (index > 0) { - components.remove(index-1); + components.remove(index - 1); --index; } } } StringBuilder normalizedPath = new StringBuilder(); - for(String component: components) { + for (String component : components) { normalizedPath.append("/"); normalizedPath.append(component); } @@ -200,6 +200,7 @@ public long getFreeSpaceInBytes() { } public abstract Uri toNativeUri(LocalFilesystemURL inputURL); + public abstract LocalFilesystemURL toLocalUri(Uri inputURL); public JSONObject getRootEntry() { @@ -221,7 +222,7 @@ public JSONObject getParentForLocalURL(LocalFilesystemURL inputURL) throws IOExc protected LocalFilesystemURL makeDestinationURL(String newName, LocalFilesystemURL srcURL, LocalFilesystemURL destURL, boolean isDirectory) { // I know this looks weird but it is to work around a JSON bug. if ("null".equals(newName) || "".equals(newName)) { - newName = srcURL.uri.getLastPathSegment();; + newName = srcURL.uri.getLastPathSegment(); } String newDest = destURL.uri.toString(); @@ -242,7 +243,7 @@ protected LocalFilesystemURL makeDestinationURL(String newName, LocalFilesystemU * or remove the source file when finished. */ public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName, - Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException { + Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException { // First, check to see that we can do it if (move && !srcFs.canRemoveFileAtLocalURL(srcURL)) { throw new NoModificationAllowedException("Cannot move file at source URL"); @@ -294,7 +295,7 @@ public void readFileAtURL(LocalFilesystemURL inputURL, long start, long end, } abstract long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset, - boolean isBinary) throws NoModificationAllowedException, IOException; + boolean isBinary) throws NoModificationAllowedException, IOException; abstract long truncateFileAtURL(LocalFilesystemURL inputURL, long size) throws IOException, NoModificationAllowedException; @@ -308,10 +309,12 @@ abstract long truncateFileAtURL(LocalFilesystemURL inputURL, long size) protected class LimitedInputStream extends FilterInputStream { long numBytesToRead; + public LimitedInputStream(InputStream in, long numBytesToRead) { super(in); this.numBytesToRead = numBytesToRead; } + @Override public int read() throws IOException { if (numBytesToRead <= 0) { @@ -320,6 +323,7 @@ public int read() throws IOException { numBytesToRead--; return in.read(); } + @Override public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException { if (numBytesToRead <= 0) { @@ -327,7 +331,7 @@ public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException } int bytesToRead = byteCount; if (byteCount > numBytesToRead) { - bytesToRead = (int)numBytesToRead; // Cast okay; long is less than int here. + bytesToRead = (int) numBytesToRead; // Cast okay; long is less than int here. } int numBytesRead = in.read(buffer, byteOffset, bytesToRead); numBytesToRead -= numBytesRead; @@ -341,8 +345,8 @@ protected Uri.Builder createLocalUriBuilder() { String path = LocalFilesystemURL.fsNameToCdvKeyword(name); return new Uri.Builder() - .scheme(scheme) - .authority(hostname) - .path(path); + .scheme(scheme) + .authority(hostname) + .path(path); } } diff --git a/src/android/LocalFilesystem.java b/src/android/LocalFilesystem.java index 6b49f9a32..392b1c549 100644 --- a/src/android/LocalFilesystem.java +++ b/src/android/LocalFilesystem.java @@ -106,7 +106,7 @@ public LocalFilesystemURL URLforFilesystemPath(String path) { @Override public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, - String path, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException { + String path, JSONObject options, boolean directory) throws FileExistsException, IOException, TypeMismatchException, EncodingException, JSONException { boolean create = false; boolean exclusive = false; @@ -148,8 +148,7 @@ public JSONObject getFileForLocalURL(LocalFilesystemURL inputURL, if (!fp.exists()) { throw new FileExistsException("create fails"); } - } - else { + } else { if (!fp.exists()) { throw new FileNotFoundException("path does not exist"); } @@ -324,7 +323,7 @@ private void copyDirectory(Filesystem srcFs, LocalFilesystemURL srcURL, File dst @Override public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName, - Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException { + Filesystem srcFs, LocalFilesystemURL srcURL, boolean move) throws IOException, InvalidModificationException, JSONException, NoModificationAllowedException, FileExistsException { // Check to see if the destination directory exists String newParent = this.filesystemPathForURL(destURL); @@ -371,7 +370,7 @@ public JSONObject copyFileToURL(LocalFilesystemURL destURL, String newName, @Override public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, - int offset, boolean isBinary) throws IOException, NoModificationAllowedException { + int offset, boolean isBinary) throws IOException, NoModificationAllowedException { boolean append = false; if (offset > 0) { @@ -386,8 +385,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, rawData = data.getBytes(Charset.defaultCharset()); } ByteArrayInputStream in = new ByteArrayInputStream(rawData); - try - { + try { byte buff[] = new byte[rawData.length]; String absolutePath = filesystemPathForURL(inputURL); FileOutputStream out = new FileOutputStream(absolutePath, append); @@ -402,9 +400,7 @@ public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, if (isPublicDirectory(absolutePath)) { broadcastNewFile(Uri.fromFile(new File(absolutePath))); } - } - catch (NullPointerException e) - { + } catch (NullPointerException e) { // This is a bug in the Android implementation of the Java Stack NoModificationAllowedException realException = new NoModificationAllowedException(inputURL.toString()); realException.initCause(e); @@ -419,7 +415,7 @@ private boolean isPublicDirectory(String absolutePath) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // Lollipop has a bug where SD cards are null. for (File f : context.getExternalMediaDirs()) { - if(f != null && absolutePath.startsWith(f.getAbsolutePath())) { + if (f != null && absolutePath.startsWith(f.getAbsolutePath())) { return true; } } @@ -429,7 +425,7 @@ private boolean isPublicDirectory(String absolutePath) { return absolutePath.startsWith(extPath); } - /** + /** * Send broadcast of new file so files appear over MTP */ private void broadcastNewFile(Uri nativeUri) { diff --git a/src/android/LocalFilesystemURL.java b/src/android/LocalFilesystemURL.java index 0419679c4..5c6bb95ad 100644 --- a/src/android/LocalFilesystemURL.java +++ b/src/android/LocalFilesystemURL.java @@ -38,7 +38,7 @@ private LocalFilesystemURL(Uri uri, String fsName, String fsPath, boolean isDire } public static LocalFilesystemURL parse(Uri uri) { - if(!uri.toString().contains(CDVFILE_KEYWORD)) { + if (!uri.toString().contains(CDVFILE_KEYWORD)) { return null; } @@ -66,7 +66,9 @@ public static LocalFilesystemURL parse(String uri) { return parse(Uri.parse(uri)); } - public static String fsNameToCdvKeyword(String fsName) { return CDVFILE_KEYWORD + fsName + "__"; } + public static String fsNameToCdvKeyword(String fsName) { + return CDVFILE_KEYWORD + fsName + "__"; + } public String toString() { return uri.toString(); diff --git a/src/android/PendingRequests.java b/src/android/PendingRequests.java index 4c75f4231..279aa2dc2 100644 --- a/src/android/PendingRequests.java +++ b/src/android/PendingRequests.java @@ -32,12 +32,13 @@ class PendingRequests { /** * Creates a request and adds it to the array of pending requests. Each created request gets a * unique result code for use with requestPermission() - * @param rawArgs The raw arguments passed to the plugin - * @param action The action this request corresponds to (get file, etc.) - * @param callbackContext The CallbackContext for this plugin call - * @return The request code that can be used to retrieve the Request object + * + * @param rawArgs The raw arguments passed to the plugin + * @param action The action this request corresponds to (get file, etc.) + * @param callbackContext The CallbackContext for this plugin call + * @return The request code that can be used to retrieve the Request object */ - public synchronized int createRequest(String rawArgs, int action, CallbackContext callbackContext) { + public synchronized int createRequest(String rawArgs, int action, CallbackContext callbackContext) { Request req = new Request(rawArgs, action, callbackContext); requests.put(req.requestCode, req); return req.requestCode; @@ -45,9 +46,9 @@ public synchronized int createRequest(String rawArgs, int action, CallbackContex /** * Gets the request corresponding to this request code and removes it from the pending requests - * @param requestCode The request code for the desired request - * @return The request corresponding to the given request code or null if such a - * request is not found + * + * @param requestCode The request code for the desired request + * @return The request corresponding to the given request code or null if such a request is not found */ public synchronized Request getAndRemove(int requestCode) { Request result = requests.get(requestCode); @@ -76,7 +77,7 @@ private Request(String rawArgs, int action, CallbackContext callbackContext) { this.rawArgs = rawArgs; this.action = action; this.callbackContext = callbackContext; - this.requestCode = currentReqId ++; + this.requestCode = currentReqId++; } public int getAction() { From b751457f58b103b180b2f7cff87be70f28010774 Mon Sep 17 00:00:00 2001 From: Alexis THOMAS Date: Sun, 24 Dec 2023 17:19:41 +0100 Subject: [PATCH 2/3] feat(android): only use cordova LOG --- src/android/FileUtils.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index 92b0402c8..f992ab9c4 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -26,7 +26,6 @@ Licensed to the Apache Software Foundation (ASF) under one import android.os.Build; import android.os.Environment; import android.util.Base64; -import android.util.Log; import android.webkit.MimeTypeMap; import android.webkit.WebResourceResponse; @@ -1296,9 +1295,9 @@ public CordovaPluginPathHandler getPathHandler() { return new WebResourceResponse(fileMimeType, null, fileIS); } catch (FileNotFoundException e) { - Log.e(LOG_TAG, e.getMessage()); + LOG.e(LOG_TAG, e.getMessage()); } catch (IOException e) { - Log.e(LOG_TAG, e.getMessage()); + LOG.e(LOG_TAG, e.getMessage()); } } } From 8dcf14db92cb95dda4ea5c19b3c42964683579ff Mon Sep 17 00:00:00 2001 From: Alexis THOMAS Date: Sun, 24 Dec 2023 19:54:51 +0100 Subject: [PATCH 3/3] feat(android): use already imported Build class --- src/android/FileUtils.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/android/FileUtils.java b/src/android/FileUtils.java index f992ab9c4..3e9d905da 100644 --- a/src/android/FileUtils.java +++ b/src/android/FileUtils.java @@ -533,7 +533,7 @@ public void run(JSONArray args) throws FileNotFoundException, JSONException, Mal private void getReadPermission(String rawArgs, int action, CallbackContext callbackContext) { int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { PermissionHelper.requestPermissions(this, requestCode, new String[]{Manifest.permission.READ_MEDIA_IMAGES, Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO}); } else { @@ -542,7 +542,7 @@ private void getReadPermission(String rawArgs, int action, CallbackContext callb } private void getWritePermission(String rawArgs, int action, CallbackContext callbackContext) { - if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { int requestCode = pendingRequests.createRequest(rawArgs, action, callbackContext); PermissionHelper.requestPermission(this, requestCode, Manifest.permission.WRITE_EXTERNAL_STORAGE); } @@ -558,7 +558,7 @@ private void getWritePermission(String rawArgs, int action, CallbackContext call * @return */ private boolean hasReadPermission() { - if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { return PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_IMAGES) && PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_VIDEO) && PermissionHelper.hasPermission(this, Manifest.permission.READ_MEDIA_AUDIO); @@ -569,7 +569,7 @@ private boolean hasReadPermission() { private boolean hasWritePermission() { // Starting with API 33, requesting WRITE_EXTERNAL_STORAGE is an auto permission rejection - return android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU ? true : PermissionHelper.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE); }