From a712164440c2472f1096cc8ffdbcba321f9b96e1 Mon Sep 17 00:00:00 2001 From: Miku AuahDark Date: Fri, 26 Jan 2024 23:18:54 +0800 Subject: [PATCH] Minor reformat and fix. --- .../java/org/love2d/android/GameActivity.java | 48 +++--- .../org/love2d/android/AboutActivity.java | 13 +- .../love2d/android/LoveDocumentsProvider.java | 144 +++++++++--------- .../java/org/love2d/android/MainActivity.java | 28 ++-- 4 files changed, 117 insertions(+), 116 deletions(-) diff --git a/app/src/main/java/org/love2d/android/GameActivity.java b/app/src/main/java/org/love2d/android/GameActivity.java index 36f47bf2..8321a6ea 100644 --- a/app/src/main/java/org/love2d/android/GameActivity.java +++ b/app/src/main/java/org/love2d/android/GameActivity.java @@ -20,11 +20,6 @@ package org.love2d.android; -import androidx.annotation.Keep; -import androidx.core.app.ActivityCompat; - -import org.libsdl.app.SDLActivity; - import android.Manifest; import android.content.Context; import android.content.Intent; @@ -43,6 +38,11 @@ import android.util.Log; import android.view.DisplayCutout; import android.view.WindowManager; +import androidx.annotation.Keep; +import androidx.core.app.ActivityCompat; + +import org.libsdl.app.SDLActivity; + import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -75,7 +75,7 @@ public class GameActivity extends SDLActivity { @Override protected String[] getLibraries() { - return new String[] { + return new String[]{ "c++_shared", "SDL2", "oboe", @@ -193,15 +193,12 @@ public class GameActivity extends SDLActivity { } } - if (inputStream != null) { - try { - inputStream.close(); - } catch (IOException e) { - // Ignored - } + try { + inputStream.close(); + } catch (IOException ignored) { } - return inputStream != null; + return true; } @Keep @@ -235,7 +232,7 @@ public class GameActivity extends SDLActivity { HashMap map = buildFileTree(getAssets(), "", new HashMap<>()); ArrayList result = new ArrayList<>(); - for (Map.Entry data: map.entrySet()) { + for (Map.Entry data : map.entrySet()) { result.add((data.getValue() ? "d" : "f") + data.getKey()); } @@ -418,13 +415,12 @@ public class GameActivity extends SDLActivity { // Mark as file map.put(dir, true); - // This Object comparison is intentional. - if (strippedDir != dir) { + if (!strippedDir.equals(dir)) { map.put(strippedDir, true); } if (list != null) { - for (String path: list) { + for (String path : list) { buildFileTree(assetManager, dir + path + "/", map); } } @@ -440,6 +436,10 @@ public class GameActivity extends SDLActivity { try { ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r"); + if (pfd == null) { + throw new RuntimeException("pfd is null"); + } + fd = pfd.dup().detachFd(); pfd.close(); } catch (Exception e) { @@ -453,12 +453,14 @@ public class GameActivity extends SDLActivity { String scheme = game.getScheme(); String path = game.getPath(); - if (scheme.equals("content")) { - // Convert the URI to file descriptor. - args = new String[] {"/love2d://fd/" + convertToFileDescriptor(game)}; - } else if (scheme.equals("file")) { - // Regular file, pass as-is. - args = new String[] {path}; + if (scheme != null) { + if (scheme.equals("content")) { + // Convert the URI to file descriptor. + args = new String[]{"/love2d://fd/" + convertToFileDescriptor(game)}; + } else if (scheme.equals("file")) { + // Regular file, pass as-is. + args = new String[]{path}; + } } } } diff --git a/app/src/normal/java/org/love2d/android/AboutActivity.java b/app/src/normal/java/org/love2d/android/AboutActivity.java index 6983465d..e726a1ad 100644 --- a/app/src/normal/java/org/love2d/android/AboutActivity.java +++ b/app/src/normal/java/org/love2d/android/AboutActivity.java @@ -20,14 +20,15 @@ package org.love2d.android; -import androidx.appcompat.app.AppCompatActivity; - import android.content.Intent; +import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.widget.Button; import android.widget.TextView; +import androidx.appcompat.app.AppCompatActivity; + public class AboutActivity extends AppCompatActivity { @Override @@ -45,7 +46,11 @@ public class AboutActivity extends AppCompatActivity { }); // Set version - TextView versionText = findViewById(R.id.textView4); - versionText.setText(getString(R.string.version_info, BuildConfig.VERSION_NAME)); + try { + String version = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; + TextView versionText = findViewById(R.id.textView4); + versionText.setText(getString(R.string.version_info, version)); + } catch (PackageManager.NameNotFoundException ignored) { + } } } diff --git a/app/src/normal/java/org/love2d/android/LoveDocumentsProvider.java b/app/src/normal/java/org/love2d/android/LoveDocumentsProvider.java index 2bcc0a4e..fe148c2a 100644 --- a/app/src/normal/java/org/love2d/android/LoveDocumentsProvider.java +++ b/app/src/normal/java/org/love2d/android/LoveDocumentsProvider.java @@ -15,8 +15,8 @@ */ /* -* Modified for use with LOVE by the LOVE Development Team -* Copyright (C) 2023 LOVE Development Team + * Modified for use with LOVE by the LOVE Development Team + * Copyright (C) 2023 LOVE Development Team */ package org.love2d.android; @@ -29,7 +29,6 @@ import android.database.MatrixCursor; import android.graphics.Point; import android.os.Build; import android.os.CancellationSignal; -import android.os.Handler; import android.os.ParcelFileDescriptor; import android.provider.DocumentsContract.Document; import android.provider.DocumentsContract.Root; @@ -42,15 +41,11 @@ import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.Collections; import java.util.Comparator; -import java.util.HashSet; import java.util.LinkedList; import java.util.Objects; import java.util.PriorityQueue; -import java.util.Set; /** * Manages documents and exposes them to the Android system for sharing. @@ -61,25 +56,25 @@ public class LoveDocumentsProvider extends DocumentsProvider { // Use these as the default columns to return information about a root if no specific // columns are requested in a query. private static final String[] DEFAULT_ROOT_PROJECTION = new String[]{ - Root.COLUMN_ROOT_ID, - Root.COLUMN_MIME_TYPES, - Root.COLUMN_FLAGS, - Root.COLUMN_ICON, - Root.COLUMN_TITLE, - Root.COLUMN_SUMMARY, - Root.COLUMN_DOCUMENT_ID, - Root.COLUMN_AVAILABLE_BYTES + Root.COLUMN_ROOT_ID, + Root.COLUMN_MIME_TYPES, + Root.COLUMN_FLAGS, + Root.COLUMN_ICON, + Root.COLUMN_TITLE, + Root.COLUMN_SUMMARY, + Root.COLUMN_DOCUMENT_ID, + Root.COLUMN_AVAILABLE_BYTES }; // Use these as the default columns to return information about a document if no specific // columns are requested in a query. private static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[]{ - Document.COLUMN_DOCUMENT_ID, - Document.COLUMN_MIME_TYPE, - Document.COLUMN_DISPLAY_NAME, - Document.COLUMN_LAST_MODIFIED, - Document.COLUMN_FLAGS, - Document.COLUMN_SIZE + Document.COLUMN_DOCUMENT_ID, + Document.COLUMN_MIME_TYPE, + Document.COLUMN_DISPLAY_NAME, + Document.COLUMN_LAST_MODIFIED, + Document.COLUMN_FLAGS, + Document.COLUMN_SIZE }; // No official policy on how many to return, but make sure you do limit the number of recent @@ -120,19 +115,13 @@ public class LoveDocumentsProvider extends DocumentsProvider { // documents will show up in the "Recents" category. FLAG_SUPPORTS_SEARCH allows users // to search all documents the application shares. FLAG_SUPPORTS_IS_CHILD allows // testing parent child relationships, available after SDK 21 (Lollipop). - if (SDK_INT < Build.VERSION_CODES.LOLLIPOP) { - row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | - Root.FLAG_SUPPORTS_RECENTS | - Root.FLAG_SUPPORTS_SEARCH); - } else { - row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | - Root.FLAG_SUPPORTS_RECENTS | - Root.FLAG_SUPPORTS_SEARCH | - Root.FLAG_SUPPORTS_IS_CHILD); - } + row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | + Root.FLAG_SUPPORTS_RECENTS | + Root.FLAG_SUPPORTS_SEARCH | + Root.FLAG_SUPPORTS_IS_CHILD); // COLUMN_TITLE is the root title (e.g. what will be displayed to identify your provider). - row.add(Root.COLUMN_TITLE, getContext().getApplicationInfo().loadLabel(getContext().getPackageManager()).toString()); + row.add(Root.COLUMN_TITLE, Objects.requireNonNull(getContext()).getApplicationInfo().loadLabel(getContext().getPackageManager()).toString()); // This document id must be unique within this provider and consistent across time. The // system picker UI may save it and refer to it later. @@ -149,7 +138,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { @Override public Cursor queryRecentDocuments(String rootId, String[] projection) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "queryRecentDocuments"); // This implementation walks a local file structure to find the most recently @@ -181,7 +170,11 @@ public class LoveDocumentsProvider extends DocumentsProvider { final File file = pending.removeFirst(); if (file.isDirectory()) { // If it's a directory, add all its children to the unprocessed list - Collections.addAll(pending, file.listFiles()); + File[] files = file.listFiles(); + + if (files != null) { + Collections.addAll(pending, files); + } } else { // If it's a file, add it to the ordered queue. lastModifiedFiles.add(file); @@ -200,7 +193,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { @Override public Cursor querySearchDocuments(String rootId, String query, String[] projection) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "querySearchDocuments"); // Create a cursor with the requested projection, or the default projection. @@ -225,7 +218,11 @@ public class LoveDocumentsProvider extends DocumentsProvider { final File file = pending.removeFirst(); if (file.isDirectory()) { // If it's a directory, add all its children to the unprocessed list - Collections.addAll(pending, file.listFiles()); + File[] files = file.listFiles(); + + if (files != null) { + Collections.addAll(pending, files); + } } else { // If it's a file and it matches, add it to the result cursor. if (file.getName().toLowerCase().contains(query)) { @@ -239,18 +236,18 @@ public class LoveDocumentsProvider extends DocumentsProvider { @Override public AssetFileDescriptor openDocumentThumbnail(String documentId, Point sizeHint, CancellationSignal signal) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "openDocumentThumbnail"); final File file = getFileForDocId(documentId); final ParcelFileDescriptor pfd = - ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); + ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH); } @Override public Cursor queryDocument(String documentId, String[] projection) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "queryDocument"); // Create a cursor with the requested projection, or the default projection. @@ -263,14 +260,18 @@ public class LoveDocumentsProvider extends DocumentsProvider { public Cursor queryChildDocuments(String parentDocumentId, String[] projection, String sortOrder) throws FileNotFoundException { Log.v(TAG, "queryChildDocuments, parentDocumentId: " + - parentDocumentId + - " sortOrder: " + - sortOrder); + parentDocumentId + + " sortOrder: " + + sortOrder); final MatrixCursor result = new MatrixCursor(resolveDocumentProjection(projection)); final File parent = getFileForDocId(parentDocumentId); - for (File file : parent.listFiles()) { - includeFile(result, null, file); + File[] files = parent.listFiles(); + + if (files != null) { + for (File file : files) { + includeFile(result, null, file); + } } return result; } @@ -292,22 +293,18 @@ public class LoveDocumentsProvider extends DocumentsProvider { @Override public boolean isChildDocument(String parentDocumentId, String documentId) { Log.v(TAG, "isChildDocument"); - if(documentId.startsWith(parentDocumentId)) { - return true; - } else { - return false; - } + return documentId.startsWith(parentDocumentId); } @Override public String createDocument(String documentId, String mimeType, String displayName) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "createDocument"); File parent = getFileForDocId(documentId); File file = new File(parent.getPath(), displayName); int conflictId = 1; - while(file.exists()) { + while (file.exists()) { file = new File(parent.getPath(), displayName + "(" + conflictId++ + ")"); } try { @@ -321,18 +318,18 @@ public class LoveDocumentsProvider extends DocumentsProvider { if (!wasNewFileCreated) { throw new FileNotFoundException("Failed to create document with name " + - displayName + " and documentId " + documentId); + displayName + " and documentId " + documentId); } } catch (IOException e) { throw new FileNotFoundException("Failed to create document with name " + - displayName + " and documentId " + documentId); + displayName + " and documentId " + documentId); } return getDocIdForFile(file); } @Override public String renameDocument(String documentId, String displayName) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "renameDocument"); if (displayName == null) { throw new FileNotFoundException("Failed to rename document, new name is null"); @@ -373,7 +370,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { @Override public void removeDocument(String documentId, String parentDocumentId) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "removeDocument"); File parent = getFileForDocId(parentDocumentId); File file = getFileForDocId(documentId); @@ -411,14 +408,14 @@ public class LoveDocumentsProvider extends DocumentsProvider { Log.v(TAG, "copyDocument with document parent"); if (!isChildDocument(sourceParentDocumentId, sourceDocumentId)) { throw new FileNotFoundException("Failed to copy document with id " + - sourceDocumentId + ". Parent is not: " + sourceParentDocumentId); + sourceDocumentId + ". Parent is not: " + sourceParentDocumentId); } return copyDocument(sourceDocumentId, targetParentDocumentId); } @Override public String copyDocument(String sourceDocumentId, String targetParentDocumentId) - throws FileNotFoundException { + throws FileNotFoundException { Log.v(TAG, "copyDocument"); File parent = getFileForDocId(targetParentDocumentId); @@ -426,13 +423,13 @@ public class LoveDocumentsProvider extends DocumentsProvider { File newFile = new File(parent.getPath(), oldFile.getName()); try { int conflictId = 1; - while(newFile.exists()) { + while (newFile.exists()) { newFile = new File(parent.getPath(), oldFile.getName() + "(" + conflictId++ + ")"); } // Create the new File to copy into boolean wasNewFileCreated = false; boolean isDirectory = Document.MIME_TYPE_DIR.equals(getTypeForFile(oldFile)); - if(isDirectory) { + if (isDirectory) { if (newFile.mkdir()) { if (newFile.setWritable(true) && newFile.setReadable(true)) { wasNewFileCreated = true; @@ -448,13 +445,13 @@ public class LoveDocumentsProvider extends DocumentsProvider { if (!wasNewFileCreated) { throw new FileNotFoundException("Failed to copy document " + sourceDocumentId + - ". Could not create new file."); + ". Could not create new file."); } - if(!isDirectory) { + if (!isDirectory) { // Copy the bytes into the new file - try (InputStream inStream = new FileInputStream(oldFile)) { - try (OutputStream outStream = new FileOutputStream(newFile)) { + try (FileInputStream inStream = new FileInputStream(oldFile)) { + try (FileOutputStream outStream = new FileOutputStream(newFile)) { // Transfer bytes from in to out byte[] buf = new byte[16384]; // disk: 8-64k int len; @@ -466,7 +463,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { } } catch (IOException e) { throw new FileNotFoundException("Failed to copy document: " + sourceDocumentId + - ". " + e.getMessage()); + ". " + e.getMessage()); } return getDocIdForFile(newFile); } @@ -478,7 +475,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { try { // Copy document, insisting that the parent is correct String newDocumentId = copyDocument(sourceDocumentId, sourceParentDocumentId, - targetParentDocumentId); + targetParentDocumentId); // Remove old document removeDocument(sourceDocumentId, sourceParentDocumentId); return newDocumentId; @@ -530,7 +527,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { final int lastDot = name.lastIndexOf('.'); if (lastDot >= 0) { final String extension = name.substring(lastDot + 1); - if(extension == "love") { + if (extension.equals("love")) { return "application/x-love-game"; } final String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); @@ -575,10 +572,9 @@ public class LoveDocumentsProvider extends DocumentsProvider { * @param result the cursor to modify * @param docId the document ID representing the desired file (may be null if given file) * @param file the File object representing the desired file (may be null if given docID) - * @throws FileNotFoundException */ private void includeFile(MatrixCursor result, String docId, File file) - throws FileNotFoundException { + throws FileNotFoundException { if (docId == null) { docId = getDocIdForFile(file); } else { @@ -597,11 +593,8 @@ public class LoveDocumentsProvider extends DocumentsProvider { // FLAG_SUPPORTS_DELETE flags |= Document.FLAG_SUPPORTS_WRITE; flags |= Document.FLAG_SUPPORTS_DELETE; + flags |= Document.FLAG_SUPPORTS_RENAME; - // Add SDK specific flags if appropriate - if (SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - flags |= Document.FLAG_SUPPORTS_RENAME; - } if (SDK_INT >= Build.VERSION_CODES.N) { flags |= Document.FLAG_SUPPORTS_REMOVE; flags |= Document.FLAG_SUPPORTS_MOVE; @@ -619,8 +612,8 @@ public class LoveDocumentsProvider extends DocumentsProvider { final MatrixCursor.RowBuilder row = result.newRow(); row.add(Document.COLUMN_DOCUMENT_ID, docId); - if(file.getAbsolutePath() == mBaseDir.getAbsolutePath()) { - row.add(Document.COLUMN_DISPLAY_NAME, getContext().getApplicationInfo().loadLabel(getContext().getPackageManager()).toString()); + if (file.getAbsolutePath().equals(mBaseDir.getAbsolutePath())) { + row.add(Document.COLUMN_DISPLAY_NAME, Objects.requireNonNull(getContext()).getApplicationInfo().loadLabel(getContext().getPackageManager()).toString()); } else { row.add(Document.COLUMN_DISPLAY_NAME, displayName); } @@ -630,7 +623,7 @@ public class LoveDocumentsProvider extends DocumentsProvider { row.add(Document.COLUMN_FLAGS, flags); // Add a custom icon - if(mimeType == "application/x-love-game") { + if (mimeType.equals("application/x-love-game")) { row.add(Document.COLUMN_ICON, R.drawable.love); } else { row.add(Document.COLUMN_ICON, null); @@ -642,7 +635,6 @@ public class LoveDocumentsProvider extends DocumentsProvider { * * @param docId the document ID representing the desired file * @return a File represented by the given document ID - * @throws java.io.FileNotFoundException */ private File getFileForDocId(String docId) throws FileNotFoundException { File target = mBaseDir; diff --git a/app/src/normal/java/org/love2d/android/MainActivity.java b/app/src/normal/java/org/love2d/android/MainActivity.java index 43905b7b..e591bcd9 100644 --- a/app/src/normal/java/org/love2d/android/MainActivity.java +++ b/app/src/normal/java/org/love2d/android/MainActivity.java @@ -20,15 +20,6 @@ package org.love2d.android; -import androidx.activity.result.ActivityResultLauncher; -import androidx.activity.result.contract.ActivityResultContracts; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.app.AppCompatActivity; -import androidx.constraintlayout.widget.ConstraintLayout; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; - import android.annotation.SuppressLint; import android.content.Intent; import android.net.Uri; @@ -40,6 +31,15 @@ import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.app.AppCompatActivity; +import androidx.constraintlayout.widget.ConstraintLayout; +import androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; +import androidx.swiperefreshlayout.widget.SwipeRefreshLayout; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -74,7 +74,7 @@ public class MainActivity extends AppCompatActivity { GameListAdapter adapter = new GameListAdapter(); - // Set refresh listener + // Set refresh listener swipeLayout.setOnRefreshListener(() -> { scanGames(adapter, noGameText, swipeLayout); }); @@ -99,7 +99,7 @@ public class MainActivity extends AppCompatActivity { // Handle item selection if (itemId == R.id.optionItem) { - openFileLauncher.launch(new String[] {"*/*"}); + openFileLauncher.launch(new String[]{"*/*"}); return true; } else if (itemId == R.id.optionItem2) { Intent intent = new Intent(this, GameActivity.class); @@ -120,7 +120,8 @@ public class MainActivity extends AppCompatActivity { private AlertDialog getGameFolderDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this) .setTitle(getString(R.string.game_folder)) - .setPositiveButton(R.string.ok, (dialog1, which) -> { }); + .setPositiveButton(R.string.ok, (dialog1, which) -> { + }); StringBuilder message = new StringBuilder() .append(getString(R.string.game_folder_location, getPackageName())) .append("\n\n"); @@ -211,7 +212,8 @@ public class MainActivity extends AppCompatActivity { ZipFile zip = new ZipFile(file, ZipFile.OPEN_READ); valid = zip.getEntry("main.lua") != null; zip.close(); - } catch (IOException ignored) { } + } catch (IOException ignored) { + } return valid; }