From 11ec9c45ce68755f6d2ca1c73d3732f4df8c55c3 Mon Sep 17 00:00:00 2001 From: Looki2000 Date: Mon, 27 Feb 2023 14:54:45 +0100 Subject: [PATCH] fixed instant crash on new android versions --- .../net/minetest/minetest/GameActivity.java | 3 +- .../net/minetest/minetest/MainActivity.java | 10 ++++-- .../net/minetest/minetest/UnzipService.java | 23 +++++++----- .../java/net/minetest/minetest/Utils.java | 35 +++++++++++-------- 4 files changed, 45 insertions(+), 26 deletions(-) diff --git a/android/app/src/main/java/net/minetest/minetest/GameActivity.java b/android/app/src/main/java/net/minetest/minetest/GameActivity.java index f5e9fd6..2aa7812 100644 --- a/android/app/src/main/java/net/minetest/minetest/GameActivity.java +++ b/android/app/src/main/java/net/minetest/minetest/GameActivity.java @@ -45,6 +45,7 @@ import java.util.Objects; // Native code finds these methods by name (see porting_android.cpp). // This annotation prevents the minifier/Proguard from mangling them. @Keep +@SuppressWarnings("unused") public class GameActivity extends NativeActivity { static { System.loadLibrary("c++_shared"); @@ -204,4 +205,4 @@ public class GameActivity extends NativeActivity { Intent shareIntent = Intent.createChooser(intent, null); startActivity(shareIntent); } -} +} \ No newline at end of file diff --git a/android/app/src/main/java/net/minetest/minetest/MainActivity.java b/android/app/src/main/java/net/minetest/minetest/MainActivity.java index b6567b4..0bca2fe 100644 --- a/android/app/src/main/java/net/minetest/minetest/MainActivity.java +++ b/android/app/src/main/java/net/minetest/minetest/MainActivity.java @@ -127,8 +127,12 @@ public class MainActivity extends AppCompatActivity { } @Override - public void onRequestPermissionsResult(int requestCode, - @NonNull String[] permissions, @NonNull int[] grantResults) { + public void onRequestPermissionsResult( + int requestCode, + @NonNull String[] permissions, + @NonNull int[] grantResults + ) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == PERMISSIONS) { for (int grantResult : grantResults) { if (grantResult != PackageManager.PERMISSION_GRANTED) { @@ -182,4 +186,4 @@ public class MainActivity extends AppCompatActivity { super.onDestroy(); unregisterReceiver(myReceiver); } -} +} \ No newline at end of file diff --git a/android/app/src/main/java/net/minetest/minetest/UnzipService.java b/android/app/src/main/java/net/minetest/minetest/UnzipService.java index 2199c28..b75bc0c 100644 --- a/android/app/src/main/java/net/minetest/minetest/UnzipService.java +++ b/android/app/src/main/java/net/minetest/minetest/UnzipService.java @@ -29,10 +29,10 @@ import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Environment; +import android.util.Log; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.RequiresApi; import androidx.annotation.StringRes; import java.io.File; @@ -77,9 +77,6 @@ public class UnzipService extends IntentService { try { setIsRunning(true); File userDataDirectory = Utils.getUserDataDirectory(this); - if (userDataDirectory == null) { - throw new IOException("Unable to find user data directory"); - } try (InputStream in = this.getAssets().open(zipFile.getName())) { try (OutputStream out = new FileOutputStream(zipFile)) { @@ -98,7 +95,9 @@ public class UnzipService extends IntentService { failureMessage = e.getLocalizedMessage(); } finally { setIsRunning(false); - zipFile.delete(); + if (!zipFile.delete()) { + Log.w("UnzipService", "Minetest installation ZIP cannot be deleted"); + } } } @@ -131,8 +130,12 @@ public class UnzipService extends IntentService { Intent notificationIntent = new Intent(this, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); + int pendingIntentFlag = 0; + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + pendingIntentFlag = PendingIntent.FLAG_MUTABLE; + } PendingIntent intent = PendingIntent.getActivity(this, 0, - notificationIntent, 0); + notificationIntent, pendingIntentFlag); builder.setContentTitle(getString(R.string.notification_title)) .setSmallIcon(R.mipmap.ic_launcher) @@ -223,7 +226,9 @@ public class UnzipService extends IntentService { return; publishProgress(notificationBuilder, R.string.migrating, 0); - newLocation.mkdir(); + if (!newLocation.mkdir()) { + Log.e("UnzipService", "New installation folder cannot be made"); + } String[] dirs = new String[] { "worlds", "games", "mods", "textures", "client" }; for (int i = 0; i < dirs.length; i++) { @@ -241,7 +246,9 @@ public class UnzipService extends IntentService { } } - recursivelyDeleteDirectory(oldLocation); + if (!recursivelyDeleteDirectory(oldLocation)) { + Log.w("UnzipService", "Old installation files cannot be deleted successfully"); + } } private void publishProgress(@Nullable Notification.Builder notificationBuilder, @StringRes int message, int progress) { diff --git a/android/app/src/main/java/net/minetest/minetest/Utils.java b/android/app/src/main/java/net/minetest/minetest/Utils.java index b2553c8..a80c40a 100644 --- a/android/app/src/main/java/net/minetest/minetest/Utils.java +++ b/android/app/src/main/java/net/minetest/minetest/Utils.java @@ -1,39 +1,46 @@ package net.minetest.minetest; import android.content.Context; +import android.util.Log; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; import java.io.File; +import java.util.Objects; public class Utils { - public static @NonNull File createDirs(File root, String dir) { + @NonNull + public static File createDirs(@NonNull File root, @NonNull String dir) { File f = new File(root, dir); if (!f.isDirectory()) - f.mkdirs(); + if (!f.mkdirs()) + Log.e("Utils", "Directory " + dir + " cannot be created"); return f; } - public static @Nullable File getUserDataDirectory(Context context) { - File extDir = context.getExternalFilesDir(null); - if (extDir == null) { - return null; - } - + @NonNull + public static File getUserDataDirectory(@NonNull Context context) { + File extDir = Objects.requireNonNull( + context.getExternalFilesDir(null), + "Cannot get external file directory" + ); return createDirs(extDir, "Minetest"); } - public static @Nullable File getCacheDirectory(Context context) { - return context.getCacheDir(); + @NonNull + public static File getCacheDirectory(@NonNull Context context) { + return Objects.requireNonNull( + context.getCacheDir(), + "Cannot get cache directory" + ); } - public static boolean isInstallValid(Context context) { + public static boolean isInstallValid(@NonNull Context context) { File userDataDirectory = getUserDataDirectory(context); - return userDataDirectory != null && userDataDirectory.isDirectory() && + return userDataDirectory.isDirectory() && new File(userDataDirectory, "games").isDirectory() && new File(userDataDirectory, "builtin").isDirectory() && new File(userDataDirectory, "client").isDirectory() && new File(userDataDirectory, "textures").isDirectory(); } -} +} \ No newline at end of file