diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 60305a4..1363694 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -120,6 +120,9 @@ + + + { + Log.d(TAG, "App package replaced, clearing old logs and showing donate link") + repository.setShowDonateLink(true) + // Clear all old logs on app update + LogUtils.clearAllLogs() + } + Intent.ACTION_BOOT_COMPLETED -> { + try { + startAppropriateServices(context, repository) + } catch (e: Exception) { + Log.e(TAG, "Error starting services on boot", e) + } + } + else -> { + Log.w(TAG, "Invalid intent action: ${intent.action}") + } } } diff --git a/app/src/main/java/dev/pranav/applock/core/utils/LogUtils.kt b/app/src/main/java/dev/pranav/applock/core/utils/LogUtils.kt index 8592dd2..7ad5535 100644 --- a/app/src/main/java/dev/pranav/applock/core/utils/LogUtils.kt +++ b/app/src/main/java/dev/pranav/applock/core/utils/LogUtils.kt @@ -8,6 +8,7 @@ import android.util.Log import androidx.core.content.FileProvider import java.io.File import java.time.Instant +import java.time.temporal.ChronoUnit @SuppressLint("StaticFieldLeak") object LogUtils { @@ -78,4 +79,72 @@ object LogUtils { return null } } + + /** + * Clear all security and audit logs. + * Called when the app is updated. + */ + fun clearAllLogs() { + try { + val securityLogFile = File(context.filesDir, SECURITY_LOGS) + if (securityLogFile.exists()) { + securityLogFile.delete() + Log.d(TAG, "Cleared security logs") + } + + val appLogFile = File(context.cacheDir, FILE_NAME) + if (appLogFile.exists()) { + appLogFile.delete() + Log.d(TAG, "Cleared app logs") + } + } catch (e: Exception) { + Log.e(TAG, "Error clearing logs", e) + } + } + + /** + * Purge log entries older than 3 days from the audit log file. + * This prevents logs from growing indefinitely. + */ + fun purgeOldLogs() { + try { + val securityLogFile = File(context.filesDir, SECURITY_LOGS) + if (!securityLogFile.exists()) { + return + } + + val threeDaysAgo = Instant.now().minus(3, ChronoUnit.DAYS) + val lines = securityLogFile.readLines() + val recentLines = mutableListOf() + + for (line in lines) { + try { + // Extract timestamp from log line format: "[ISO-8601 timestamp] D [TAG]: [message]" + val timestampStr = line.substringBefore(" ") + val timestamp = Instant.parse(timestampStr) + + if (timestamp.isAfter(threeDaysAgo)) { + recentLines.add(line) + } + } catch (e: Exception) { + // If we can't parse the timestamp, keep the line to avoid data loss + recentLines.add(line) + } + } + + // Rewrite the file with only recent logs + if (recentLines.size < lines.size) { + if (recentLines.isEmpty()) { + // Delete the file if no recent logs remain + securityLogFile.delete() + Log.d(TAG, "Deleted log file - all entries were older than 3 days") + } else { + securityLogFile.writeText(recentLines.joinToString("\n") + "\n") + Log.d(TAG, "Purged ${lines.size - recentLines.size} old log entries") + } + } + } catch (e: Exception) { + Log.e(TAG, "Error purging old logs", e) + } + } }