Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5fbbdd46 authored by mrulhania's avatar mrulhania Committed by Manjeet Rulhania
Browse files

Add an upper limit for appops database size

The upper limit is set to 50MB, and least recent
records will be trimmed to keep database size
below 50MB.

We are deleting at least 1024 records after
considering max batch size of insertion.

Bug: 418075370
Test: presubmit
Test: manual
Flag: android.permission.flags.enable_all_sqlite_appops_accesses
Change-Id: Icf43d2ec69b1e7e8a125569161aad0ed76b68fdc
parent f4243b2d
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -420,6 +420,9 @@ public class AppOpHistoryHelper {
    }

    private class SqliteWriteHandler extends Handler {
        // Max database size 50 MB
        private static final long MAX_DATABASE_SIZE_BYTES = 50 * 1024 * 1024;

        SqliteWriteHandler(Looper looper) {
            super(looper);
        }
@@ -433,6 +436,7 @@ public class AppOpHistoryHelper {
                                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_PERIODIC);
                    } finally {
                        ensurePeriodicJobsAreScheduled();
                        ensureDatabaseSize();
                    }
                }
                case WRITE_DATABASE_CACHE_FULL -> {
@@ -450,6 +454,7 @@ public class AppOpHistoryHelper {
                                SQLITE_APP_OP_EVENT_REPORTED__WRITE_TYPE__WRITE_CACHE_FULL);
                    } finally {
                        ensurePeriodicJobsAreScheduled();
                        ensureDatabaseSize();
                    }
                }
                case DELETE_EXPIRED_ENTRIES_PERIODIC -> {
@@ -465,6 +470,13 @@ public class AppOpHistoryHelper {
            }
        }

        private void ensureDatabaseSize() {
            long databaseSize = mDatabaseFile.length();
            if (databaseSize > MAX_DATABASE_SIZE_BYTES) {
                mDbHelper.execSQL(AppOpHistoryTable.DELETE_TABLE_DATA_LEAST_RECENT_ENTRIES);
            }
        }

        void removeAllPendingMessages() {
            removeMessages(WRITE_DATABASE_PERIODIC);
            removeMessages(DELETE_EXPIRED_ENTRIES_PERIODIC);
+9 −2
Original line number Diff line number Diff line
@@ -74,8 +74,10 @@ final class AppOpHistoryTable {
        static final String CHAIN_ID = "chain_id";
        /** The actual access time of first event in a time window. */
        static final String ACCESS_TIME = "access_time";
        /** The actual duration of first event in a time window. Subsequent events with same
         * discretized duration are summed up in total duration. */
        /**
         * The actual duration of first event in a time window. Subsequent events with same
         * discretized duration are summed up in total duration.
         */
        static final String DURATION = "access_duration";
        /** The sum of actual duration of the app op accesses in a time window. */
        static final String TOTAL_DURATION = "total_duration";
@@ -160,6 +162,11 @@ final class AppOpHistoryTable {
    static final String DELETE_TABLE_DATA_BEFORE_ACCESS_TIME = "DELETE FROM " + TABLE_NAME
            + " WHERE " + Columns.ACCESS_TIME + " < ?";

    // Delete at least 1024 (cache size) records, in other words the batch size for insertion.
    static final String DELETE_TABLE_DATA_LEAST_RECENT_ENTRIES = "DELETE FROM " + TABLE_NAME
            + " WHERE " + Columns.ACCESS_TIME + " <= (SELECT " + Columns.ACCESS_TIME + " FROM "
            + TABLE_NAME + " ORDER BY " + Columns.ACCESS_TIME + " LIMIT 1 OFFSET 1024)";

    static final String DELETE_DATA_FOR_UID_PACKAGE = "DELETE FROM "
            + AppOpHistoryTable.TABLE_NAME
            + " WHERE " + Columns.UID + " = ? AND " + Columns.PACKAGE_NAME + " = ?";