From 1e7e86021348c4a1b272755414d515c6050ae41d Mon Sep 17 00:00:00 2001 From: DevarshVasani Date: Mon, 4 Nov 2024 23:43:27 +0530 Subject: [PATCH 1/3] Added the strict mode added strict mode, which will be enabled only for debug builds. Signed-off-by: DevarshVasani --- .../java/app/organicmaps/MwmApplication.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/android/app/src/main/java/app/organicmaps/MwmApplication.java b/android/app/src/main/java/app/organicmaps/MwmApplication.java index 4951e5936a..55eec023ae 100644 --- a/android/app/src/main/java/app/organicmaps/MwmApplication.java +++ b/android/app/src/main/java/app/organicmaps/MwmApplication.java @@ -6,9 +6,11 @@ import android.app.Activity; import android.app.Application; import android.content.Context; import android.content.SharedPreferences; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Message; +import android.os.StrictMode; import androidx.annotation.Keep; import androidx.annotation.NonNull; @@ -142,6 +144,14 @@ public class MwmApplication extends Application implements Application.ActivityL public void onCreate() { super.onCreate(); + + //this will enable the strict mode only when the application is in debug mode + if (BuildConfig.DEBUG) + { + enableStrictMode(); + } + + Logger.i(TAG, "Initializing application"); LogsManager.INSTANCE.initFileLogging(this); @@ -217,6 +227,43 @@ public class MwmApplication extends Application implements Application.ActivityL Logger.i(TAG, "Platform initialized"); } + private void enableStrictMode() + { + StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() + .detectDiskReads() + .detectDiskWrites() + .detectNetwork() // or .detectAll() for all detectable problems + .penaltyLog(); + + threadPolicyBuilder.detectCustomSlowCalls(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + { + threadPolicyBuilder.detectResourceMismatches(); + } + + StrictMode.setThreadPolicy(threadPolicyBuilder.build()); + + StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder() + .detectLeakedSqlLiteObjects() + .detectLeakedClosableObjects() + .penaltyLog(); + + vmPolicyBuilder.detectFileUriExposure(); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) + { + vmPolicyBuilder.detectCleartextNetwork(); + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) + { + vmPolicyBuilder.detectUnsafeIntentLaunch(); + } + + StrictMode.setVmPolicy(vmPolicyBuilder.build()); + } + private void createPlatformDirectories(@NonNull String writablePath, @NonNull String privatePath, @NonNull String tempPath) throws IOException -- 2.45.3 From dcb486c6617e714750f39a6be22f9102bc6de865 Mon Sep 17 00:00:00 2001 From: DevarshVasani Date: Tue, 5 Nov 2024 22:50:02 +0530 Subject: [PATCH 2/3] Update MwmApplication.java Signed-off-by: DevarshVasani --- .../app/src/main/java/app/organicmaps/MwmApplication.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/android/app/src/main/java/app/organicmaps/MwmApplication.java b/android/app/src/main/java/app/organicmaps/MwmApplication.java index 55eec023ae..8ba4b1b927 100644 --- a/android/app/src/main/java/app/organicmaps/MwmApplication.java +++ b/android/app/src/main/java/app/organicmaps/MwmApplication.java @@ -232,14 +232,15 @@ public class MwmApplication extends Application implements Application.ActivityL StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectDiskReads() .detectDiskWrites() - .detectNetwork() // or .detectAll() for all detectable problems + .detectNetwork() + .detectCustomSlowCalls() .penaltyLog(); - threadPolicyBuilder.detectCustomSlowCalls(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { threadPolicyBuilder.detectResourceMismatches(); + threadPolicyBuilder.detectExplicitGc(); } StrictMode.setThreadPolicy(threadPolicyBuilder.build()); @@ -247,9 +248,10 @@ public class MwmApplication extends Application implements Application.ActivityL StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder() .detectLeakedSqlLiteObjects() .detectLeakedClosableObjects() + .detectFileUriExposure() .penaltyLog(); - vmPolicyBuilder.detectFileUriExposure(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { -- 2.45.3 From 78285dc5edda059029bbaeba87401d65cc969cd5 Mon Sep 17 00:00:00 2001 From: DevarshVasani Date: Tue, 5 Nov 2024 23:21:27 +0530 Subject: [PATCH 3/3] add debug check into static and follow coding convention Signed-off-by: DevarshVasani --- .../java/app/organicmaps/MwmApplication.java | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/android/app/src/main/java/app/organicmaps/MwmApplication.java b/android/app/src/main/java/app/organicmaps/MwmApplication.java index 8ba4b1b927..6c9451bcde 100644 --- a/android/app/src/main/java/app/organicmaps/MwmApplication.java +++ b/android/app/src/main/java/app/organicmaps/MwmApplication.java @@ -145,13 +145,6 @@ public class MwmApplication extends Application implements Application.ActivityL { super.onCreate(); - //this will enable the strict mode only when the application is in debug mode - if (BuildConfig.DEBUG) - { - enableStrictMode(); - } - - Logger.i(TAG, "Initializing application"); LogsManager.INSTANCE.initFileLogging(this); @@ -227,7 +220,7 @@ public class MwmApplication extends Application implements Application.ActivityL Logger.i(TAG, "Platform initialized"); } - private void enableStrictMode() + private static void enableStrictMode() { StrictMode.ThreadPolicy.Builder threadPolicyBuilder = new StrictMode.ThreadPolicy.Builder() .detectDiskReads() @@ -238,10 +231,8 @@ public class MwmApplication extends Application implements Application.ActivityL if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - { threadPolicyBuilder.detectResourceMismatches(); - threadPolicyBuilder.detectExplicitGc(); - } + StrictMode.setThreadPolicy(threadPolicyBuilder.build()); @@ -254,14 +245,12 @@ public class MwmApplication extends Application implements Application.ActivityL if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) - { vmPolicyBuilder.detectCleartextNetwork(); - } + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) - { vmPolicyBuilder.detectUnsafeIntentLaunch(); - } + StrictMode.setVmPolicy(vmPolicyBuilder.build()); } @@ -322,6 +311,10 @@ public class MwmApplication extends Application implements Application.ActivityL static { System.loadLibrary("organicmaps"); + + // This will enable the strict mode only when there is debug build. + if (BuildConfig.DEBUG) + enableStrictMode(); } // Called from JNI. -- 2.45.3