Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android: Java format and clean #616

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/android/ContentFilesystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.");
}

Expand Down Expand Up @@ -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 {
Expand Down
22 changes: 11 additions & 11 deletions src/android/DirectoryManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* It is used by the FileUtils class.
*/
public class DirectoryManager {
Expand All @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand Down
19 changes: 7 additions & 12 deletions src/android/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -534,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 {
Expand All @@ -543,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);
}
Expand All @@ -553,13 +552,13 @@ 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:
*
* <p>
* Refer to: https://developer.android.com/about/versions/13/behavior-changes-13
*
* @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);
Expand All @@ -570,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);
}
Expand Down Expand Up @@ -659,7 +658,6 @@ public LocalFilesystemURL filesystemURLforLocalPath(String localPath) {
return localURL;
}


/* helper to execute functions async and handle the result codes
*
*/
Expand Down Expand Up @@ -834,7 +832,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.
Expand Down Expand Up @@ -1123,7 +1120,6 @@ public void handleData(InputStream inputStream, String contentType) {
}
}


/**
* Write contents of file.
*
Expand Down Expand Up @@ -1168,7 +1164,6 @@ private long truncateFile(String srcURLstr, long size) throws FileNotFoundExcept
}
}


/*
* Handle the response
*/
Expand Down Expand Up @@ -1300,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());
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/android/Filesystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
Expand All @@ -200,6 +200,7 @@ public long getFreeSpaceInBytes() {
}

public abstract Uri toNativeUri(LocalFilesystemURL inputURL);

public abstract LocalFilesystemURL toLocalUri(Uri inputURL);

public JSONObject getRootEntry() {
Expand All @@ -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();
Expand All @@ -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");
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -320,14 +323,15 @@ public int read() throws IOException {
numBytesToRead--;
return in.read();
}

@Override
public int read(byte[] buffer, int byteOffset, int byteCount) throws IOException {
if (numBytesToRead <= 0) {
return -1;
}
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;
Expand All @@ -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);
}
}
20 changes: 8 additions & 12 deletions src/android/LocalFilesystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand All @@ -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) {
Expand Down
6 changes: 4 additions & 2 deletions src/android/LocalFilesystemURL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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();
Expand Down
Loading
Loading