Fix lint errors in Android builds (#18502)

This commit is contained in:
刘皓 2025-12-11 22:22:44 -05:00 committed by GitHub
parent 3828186f7c
commit f30b203398
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 53 additions and 61 deletions

View File

@ -30,10 +30,9 @@ import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document;
import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
public final class VfsImplementationSaf
{
@ -41,6 +40,39 @@ public final class VfsImplementationSaf
Document.COLUMN_MIME_TYPE,
};
private static String normalizePath(String path) {
try
{
return new File("/" + path).getCanonicalPath();
}
catch (IOException e)
{
return "/";
}
}
private static String getPathParent(String path) {
try
{
return new File("/" + path).getCanonicalFile().getParent();
}
catch (IOException e)
{
return null;
}
}
private static String getPathFileName(String path) {
try
{
return new File("/" + path).getCanonicalFile().getName();
}
catch (IOException e)
{
return null;
}
}
/**
* Open a Storage Access Framework file, returning its file descriptor if successful or -1 if not.
* The file is not guaranteed to be seeked to any particular position, so it may be a good idea to seek it immediately after opening.
@ -55,21 +87,12 @@ public final class VfsImplementationSaf
{
if (Build.VERSION.SDK_INT < 21)
return -1;
path = normalizePath(path);
boolean createdFile = false;
while (true)
{
final Uri treeUri = Uri.parse(tree);
final Path filePath;
try
{
filePath = Paths.get("/" + path).normalize();
}
catch (InvalidPathException e)
{
return -1;
}
final String filePathString = filePath.toString();
final Uri fileUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, filePathString.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + filePathString);
final Uri fileUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, path.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + path);
final String mode;
if (!write)
mode = "r";
@ -96,14 +119,13 @@ public final class VfsImplementationSaf
if (createdFile || !write || !truncate)
return -1;
createdFile = true;
final Path parentPath = filePath.getParent();
final String parentPath = getPathParent(path);
if (parentPath == null)
return -1;
final String parentPathString = parentPath.toString();
final Uri parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, parentPathString.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + parentPathString);
final Uri parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, parentPath.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + parentPath);
try
{
DocumentsContract.createDocument(content, parentUri, "application/octet-stream", filePath.getFileName().toString());
DocumentsContract.createDocument(content, parentUri, "application/octet-stream", getPathFileName(path));
}
catch (FileNotFoundException | IllegalArgumentException f)
{
@ -124,14 +146,7 @@ public final class VfsImplementationSaf
if (Build.VERSION.SDK_INT < 21)
return false;
final Uri treeUri = Uri.parse(tree);
try
{
path = Paths.get("/" + path).normalize().toString();
}
catch (InvalidPathException e)
{
return false;
}
path = normalizePath(path);
path = path.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + path;
final Uri fileUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, path);
try
@ -172,14 +187,7 @@ public final class VfsImplementationSaf
if (Build.VERSION.SDK_INT < 21)
return;
final Uri treeUri = Uri.parse(tree);
try
{
path = Paths.get("/" + path).normalize().toString();
}
catch (InvalidPathException e)
{
return;
}
path = normalizePath(path);
path = path.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + path;
final Uri fileUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, path);
final Cursor cursor;
@ -244,15 +252,7 @@ public final class VfsImplementationSaf
if (Build.VERSION.SDK_INT < 21)
return -1;
final Uri treeUri = Uri.parse(tree);
final Path directoryPath;
try
{
directoryPath = Paths.get("/" + path).normalize();
}
catch (InvalidPathException e)
{
return -1;
}
path = normalizePath(path);
path = path.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + path;
final Uri directoryUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, path);
Cursor cursor = null;
@ -274,14 +274,13 @@ public final class VfsImplementationSaf
cursor.close();
}
}
final Path parentPath = directoryPath.getParent();
final String parentPath = getPathParent(path);
if (parentPath == null)
return -1;
final String parentPathString = parentPath.toString();
final Uri parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, parentPathString.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + parentPathString);
final Uri parentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri, parentPath.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + parentPath);
try
{
if (DocumentsContract.createDocument(content, parentUri, Document.MIME_TYPE_DIR, directoryPath.getFileName().toString()) == null)
if (DocumentsContract.createDocument(content, parentUri, Document.MIME_TYPE_DIR, getPathFileName(path)) == null)
return -1;
}
catch (FileNotFoundException | IllegalArgumentException e)
@ -314,14 +313,7 @@ public final class VfsImplementationSaf
if (Build.VERSION.SDK_INT < 21)
return;
final Uri treeUri = Uri.parse(tree);
try
{
path = Paths.get("/" + path).normalize().toString();
}
catch (InvalidPathException e)
{
return;
}
path = normalizePath(path);
path = path.length() == 1 ? DocumentsContract.getTreeDocumentId(treeUri) : DocumentsContract.getTreeDocumentId(treeUri) + path;
prefixLength = path.length();
final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(treeUri, path);

View File

@ -118,7 +118,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
getWindow().getAttributes().layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
}
} catch (Exception e) {
Log.w("Key doesn't exist yet.", e.getMessage());
Log.w("RetroActivityFuture", "Key doesn't exist yet: " + e.getMessage());
}
}
}
@ -149,7 +149,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
inputGrabMouse(hasFocus);
}
} catch (Exception e) {
Log.w("[onWindowFocusChanged] exception thrown:", e.getMessage());
Log.w("RetroActivityFuture", "[onWindowFocusChanged] exception thrown: " + e.getMessage());
}
}
@ -185,7 +185,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
} catch (Exception e) {
Log.w("[attemptToggleImmersiveMode] exception thrown:", e.getMessage());
Log.w("RetroActivityFuture", "[attemptToggleImmersiveMode] exception thrown: " + e.getMessage());
}
}
}
@ -200,7 +200,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
mDecorView.releasePointerCapture();
}
} catch (Exception e) {
Log.w("[attemptTogglePointerCapture] exception thrown:", e.getMessage());
Log.w("RetroActivityFuture", "[attemptTogglePointerCapture] exception thrown: " + e.getMessage());
}
}
}
@ -217,7 +217,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
} catch (NoSuchMethodException e) {
// Extensions were not available so do nothing
} catch (Exception e) {
Log.w("[attemptToggleNvidiaCursorVisibility] exception thrown:", e.getMessage());
Log.w("RetroActivityFuture", "[attemptToggleNvidiaCursorVisibility] exception thrown: " + e.getMessage());
}
}
}
@ -235,7 +235,7 @@ public final class RetroActivityFuture extends RetroActivityCamera {
mDecorView.setPointerIcon(null);
}
} catch (Exception e) {
Log.w("[attemptTogglePointerIcon] exception thrown:", e.getMessage());
Log.w("RetroActivityFuture", "[attemptTogglePointerIcon] exception thrown: " + e.getMessage());
}
}
}