From dc6f60e70da3e2ebfd98292a11a13de1ce03b8c9 Mon Sep 17 00:00:00 2001 From: Ammar Githam Date: Mon, 12 Jul 2021 00:08:26 +0900 Subject: [PATCH] Delete prev stackstrace files on version update --- .../awais/instagrabber/utils/FlavorTown.java | 35 ++++++++++--------- .../awais/instagrabber/utils/LocaleUtils.kt | 10 +++--- .../awaisomereport/CrashReporterHelper.kt | 16 +++++++++ 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/app/src/main/java/awais/instagrabber/utils/FlavorTown.java b/app/src/main/java/awais/instagrabber/utils/FlavorTown.java index 676ef8d1..ea0b26e6 100755 --- a/app/src/main/java/awais/instagrabber/utils/FlavorTown.java +++ b/app/src/main/java/awais/instagrabber/utils/FlavorTown.java @@ -14,6 +14,7 @@ import java.util.regex.Pattern; import awais.instagrabber.BuildConfig; import awais.instagrabber.R; +import awaisomereport.CrashReporterHelper; import static awais.instagrabber.utils.Utils.settingsHelper; @@ -70,23 +71,23 @@ public final class FlavorTown { } public static void changelogCheck(@NonNull final Context context) { - if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) < BuildConfig.VERSION_CODE) { - int appUaCode = settingsHelper.getInteger(Constants.APP_UA_CODE); - int browserUaCode = settingsHelper.getInteger(Constants.BROWSER_UA_CODE); - if (browserUaCode == -1 || browserUaCode >= UserAgentUtils.browsers.length) { - browserUaCode = ThreadLocalRandom.current().nextInt(0, UserAgentUtils.browsers.length); - settingsHelper.putInteger(Constants.BROWSER_UA_CODE, browserUaCode); - } - if (appUaCode == -1 || appUaCode >= UserAgentUtils.devices.length) { - appUaCode = ThreadLocalRandom.current().nextInt(0, UserAgentUtils.devices.length); - settingsHelper.putInteger(Constants.APP_UA_CODE, appUaCode); - } - final String appUa = UserAgentUtils.generateAppUA(appUaCode, LocaleUtils.getCurrentLocale().getLanguage()); - settingsHelper.putString(Constants.APP_UA, appUa); - final String browserUa = UserAgentUtils.generateBrowserUA(browserUaCode); - settingsHelper.putString(Constants.BROWSER_UA, browserUa); - Toast.makeText(context, R.string.updated, Toast.LENGTH_SHORT).show(); - settingsHelper.putInteger(Constants.PREV_INSTALL_VERSION, BuildConfig.VERSION_CODE); + if (settingsHelper.getInteger(Constants.PREV_INSTALL_VERSION) >= BuildConfig.VERSION_CODE) return; + int appUaCode = settingsHelper.getInteger(Constants.APP_UA_CODE); + int browserUaCode = settingsHelper.getInteger(Constants.BROWSER_UA_CODE); + if (browserUaCode == -1 || browserUaCode >= UserAgentUtils.browsers.length) { + browserUaCode = ThreadLocalRandom.current().nextInt(0, UserAgentUtils.browsers.length); + settingsHelper.putInteger(Constants.BROWSER_UA_CODE, browserUaCode); } + if (appUaCode == -1 || appUaCode >= UserAgentUtils.devices.length) { + appUaCode = ThreadLocalRandom.current().nextInt(0, UserAgentUtils.devices.length); + settingsHelper.putInteger(Constants.APP_UA_CODE, appUaCode); + } + final String appUa = UserAgentUtils.generateAppUA(appUaCode, LocaleUtils.getCurrentLocale().getLanguage()); + settingsHelper.putString(Constants.APP_UA, appUa); + final String browserUa = UserAgentUtils.generateBrowserUA(browserUaCode); + settingsHelper.putString(Constants.BROWSER_UA, browserUa); + CrashReporterHelper.deleteAllStacktraceFiles(context); + Toast.makeText(context, R.string.updated, Toast.LENGTH_SHORT).show(); + settingsHelper.putInteger(Constants.PREV_INSTALL_VERSION, BuildConfig.VERSION_CODE); } } \ No newline at end of file diff --git a/app/src/main/java/awais/instagrabber/utils/LocaleUtils.kt b/app/src/main/java/awais/instagrabber/utils/LocaleUtils.kt index b95b6481..b6e3df49 100755 --- a/app/src/main/java/awais/instagrabber/utils/LocaleUtils.kt +++ b/app/src/main/java/awais/instagrabber/utils/LocaleUtils.kt @@ -8,16 +8,16 @@ import java.util.* // taken from my app TESV Console Codes object LocaleUtils { - private var defaultLocale: Locale? = null + private lateinit var defaultLocale: Locale @JvmStatic - var currentLocale: Locale? = null + lateinit var currentLocale: Locale private set @JvmStatic fun setLocale(baseContext: Context) { var baseContext1 = baseContext - if (defaultLocale == null) defaultLocale = Locale.getDefault() + defaultLocale = Locale.getDefault() if (baseContext1 is ContextThemeWrapper) baseContext1 = baseContext1.baseContext if (Utils.settingsHelper == null) Utils.settingsHelper = SettingsHelper(baseContext1) val appLanguageSettings = Utils.settingsHelper.getString(PreferenceKeys.APP_LANGUAGE) @@ -30,7 +30,7 @@ object LocaleUtils { } else -> Locale(lang) } - currentLocale?.let { + currentLocale.let { Locale.setDefault(it) val res = baseContext1.resources val config = res.configuration @@ -43,7 +43,7 @@ object LocaleUtils { @JvmStatic fun updateConfig(wrapper: ContextThemeWrapper) { - if (currentLocale == null) return + if (!this::currentLocale.isInitialized) return val configuration = Configuration() // configuration.locale = currentLocale configuration.setLocale(currentLocale) diff --git a/app/src/main/java/awaisomereport/CrashReporterHelper.kt b/app/src/main/java/awaisomereport/CrashReporterHelper.kt index d9af2874..37839498 100644 --- a/app/src/main/java/awaisomereport/CrashReporterHelper.kt +++ b/app/src/main/java/awaisomereport/CrashReporterHelper.kt @@ -126,4 +126,20 @@ object CrashReporterHelper { Log.e(TAG, "", e) } } + + @JvmStatic + fun deleteAllStacktraceFiles(context: Context) { + val filePath = context.filesDir.absolutePath + val errorFileList: Array? = try { + val dir = File(filePath) + if (dir.exists() && !dir.isDirectory) { + dir.delete() + } + dir.mkdirs() + dir.listFiles { _: File?, name: String -> name.endsWith(".stacktrace") } + } catch (e: Exception) { + null + } + errorFileList?.forEach { it.delete() } + } } \ No newline at end of file