diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 77b68adf4a..ab34d9dfe1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -52,7 +52,8 @@ android:name="com.google.android.gms.car.application" android:resource="@xml/automotive_app_desc" /> - + @@ -60,7 +61,8 @@ - + @@ -115,11 +117,11 @@ - + - + + value="${EXT_DIR:-/sdcard}/AAPS/logs/${PACKAGE_NAME}" /> ${EXT_FILES_DIR}/AndroidAPS.log diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt index 36df705147..eea9b5d3c2 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt @@ -63,7 +63,9 @@ class MaintenanceFragment : DaggerFragment() { binding.logSend.setOnClickListener { maintenancePlugin.sendLogs() } binding.logDelete.setOnClickListener { uel.log(Action.DELETE_LOGS, Sources.Maintenance) - maintenancePlugin.deleteLogs() + Thread { + maintenancePlugin.deleteLogs(5) + }.start() } binding.navResetdb.setOnClickListener { activity?.let { activity -> diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenancePlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenancePlugin.kt index 571cd19656..cf5c9a6621 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenancePlugin.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenancePlugin.kt @@ -35,6 +35,7 @@ class MaintenancePlugin @Inject constructor( aapsLogger: AAPSLogger, private val buildHelper: BuildHelper, private val config: Config, + private val fileListProvider: PrefFileListProvider, private val loggerUtils: LoggerUtils ) : PluginBase(PluginDescription() .mainType(PluginType.GENERAL) @@ -53,7 +54,7 @@ class MaintenancePlugin @Inject constructor( val recipient = sp.getString(R.string.key_maintenance_logs_email, "logs@androidaps.org") val amount = sp.getInt(R.string.key_maintenance_logs_amount, 2) val logs = getLogFiles(amount) - val zipDir = context.getExternalFilesDir("exports") + val zipDir = fileListProvider.ensureExportDirExists() val zipFile = File(zipDir, constructName()) aapsLogger.debug("zipFile: ${zipFile.absolutePath}") val zip = zipLogs(zipFile, logs) @@ -65,14 +66,14 @@ class MaintenancePlugin @Inject constructor( //todo replace this with a call on startup of the application, specifically to remove // unnecessary garbage from the log exports - fun deleteLogs() { + fun deleteLogs(keep: Int) { val logDir = File(loggerUtils.logDirectory) val files = logDir.listFiles { _: File?, name: String -> (name.startsWith("AndroidAPS") && name.endsWith(".zip")) } Arrays.sort(files) { f1: File, f2: File -> f1.name.compareTo(f2.name) } var delFiles = listOf(*files) - val amount = sp.getInt(R.string.key_logshipper_amount, 5) + val amount = sp.getInt(R.string.key_logshipper_amount, keep) val keepIndex = amount - 1 if (keepIndex < delFiles.size) { delFiles = delFiles.subList(keepIndex, delFiles.size) @@ -80,7 +81,7 @@ class MaintenancePlugin @Inject constructor( file.delete() } } - val exportDir = File(loggerUtils.logDirectory, "exports") + val exportDir = fileListProvider.ensureExportDirExists() if (exportDir.exists()) { val expFiles = exportDir.listFiles() for (file in expFiles) { diff --git a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt index 48f073e804..1d475d162d 100644 --- a/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt +++ b/app/src/main/java/info/nightscout/androidaps/receivers/KeepAliveReceiver.kt @@ -28,6 +28,7 @@ import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.plugins.aps.loop.LoopPlugin import info.nightscout.androidaps.plugins.bus.RxBusWrapper import info.nightscout.androidaps.plugins.configBuilder.RunningConfiguration +import info.nightscout.androidaps.plugins.general.maintenance.MaintenancePlugin import info.nightscout.androidaps.queue.commands.Command import info.nightscout.androidaps.utils.DateUtil import info.nightscout.androidaps.utils.FabricPrivacy @@ -72,6 +73,7 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { @Inject lateinit var rxBus: RxBusWrapper @Inject lateinit var commandQueue: CommandQueueProvider @Inject lateinit var fabricPrivacy: FabricPrivacy + @Inject lateinit var maintenancePlugin: MaintenancePlugin init { (context.applicationContext as HasAndroidInjector).androidInjector().inject(this) @@ -93,6 +95,8 @@ class KeepAliveReceiver : DaggerBroadcastReceiver() { localAlertUtils.checkStaleBGAlert() checkPump() checkAPS() + maintenancePlugin.deleteLogs(30) + return Result.success() } diff --git a/app/src/main/java/info/nightscout/androidaps/utils/buildHelper/BuildHelper.kt b/app/src/main/java/info/nightscout/androidaps/utils/buildHelper/BuildHelper.kt index 0cb6b5c772..04dbd0d789 100644 --- a/app/src/main/java/info/nightscout/androidaps/utils/buildHelper/BuildHelper.kt +++ b/app/src/main/java/info/nightscout/androidaps/utils/buildHelper/BuildHelper.kt @@ -2,20 +2,22 @@ package info.nightscout.androidaps.utils.buildHelper import info.nightscout.androidaps.BuildConfig import info.nightscout.androidaps.interfaces.Config -import info.nightscout.androidaps.plugins.general.maintenance.LoggerUtils +import info.nightscout.androidaps.plugins.general.maintenance.PrefFileListProvider import java.io.File import javax.inject.Inject import javax.inject.Singleton @Singleton -class BuildHelper @Inject constructor(private val config: Config, loggerUtils: LoggerUtils) { +class BuildHelper @Inject constructor( + private val config: Config, + fileListProvider: PrefFileListProvider +) { private var devBranch = false private var engineeringMode = false init { - val extFilesDir = loggerUtils.logDirectory - val engineeringModeSemaphore = File(extFilesDir, "engineering__mode") + val engineeringModeSemaphore = File(fileListProvider.ensureExtraDirExists(), "engineering__mode") engineeringMode = engineeringModeSemaphore.exists() && engineeringModeSemaphore.isFile devBranch = BuildConfig.VERSION.contains("-") || BuildConfig.VERSION.matches(Regex(".*[a-zA-Z]+.*")) diff --git a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt index 86208dce3e..a89dc204e1 100644 --- a/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt +++ b/core/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/PrefFileListProvider.kt @@ -32,6 +32,7 @@ class PrefFileListProvider @Inject constructor( private val path = File(Environment.getExternalStorageDirectory().toString()) private val aapsPath = File(path, "AAPS" + File.separator + "preferences") private val exportsPath = File(path, "AAPS" + File.separator + "exports") + private val extraPath = File(path, "AAPS" + File.separator + "extra") private const val IMPORT_AGE_NOT_YET_OLD_DAYS = 60 } @@ -90,13 +91,21 @@ class PrefFileListProvider @Inject constructor( return File(path, resourceHelper.gs(R.string.app_name) + "Preferences") } - fun ensureExportDirExists() { + fun ensureExportDirExists(): File { if (!aapsPath.exists()) { aapsPath.mkdirs() } if (!exportsPath.exists()) { exportsPath.mkdirs() } + return exportsPath + } + + fun ensureExtraDirExists(): File { + if (!extraPath.exists()) { + extraPath.mkdirs() + } + return extraPath } fun newExportFile(): File {