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

Commit e382ffc4 authored by Lixin Yue's avatar Lixin Yue Committed by Jaikumar Ganesh
Browse files

Trim Opp database when service restarts.

When Opp service restart, we will trim the database.
a) No visible case is deleted.
b) Invisible complete outbound (failed and successful) are deleted.
c) Invisible complete failed inbound are deleted.
d) Invisible complete success inbound are deleted if needed to keep records max
to 1000.

Live folder only has received files, so the above makes sense.

Change-Id: If27080721d9abed025162ff805f661fde01564d3
parent b6ee11e6
Loading
Loading
Loading
Loading
+41 −14
Original line number Diff line number Diff line
@@ -869,26 +869,53 @@ public class BluetoothOppService extends Service {
    }

    private void trimDatabase() {
        final String INVISIBLE = BluetoothShare.VISIBILITY + "=" +
                BluetoothShare.VISIBILITY_HIDDEN;

        // remove the invisible/complete/outbound shares
        final String WHERE_INVISIBLE_COMPLETE_OUTBOUND = BluetoothShare.DIRECTION + "="
                + BluetoothShare.DIRECTION_OUTBOUND + " AND " + BluetoothShare.STATUS + ">="
                + BluetoothShare.STATUS_SUCCESS + " AND " + INVISIBLE;
        int delNum = getContentResolver().delete(BluetoothShare.CONTENT_URI,
                WHERE_INVISIBLE_COMPLETE_OUTBOUND, null);
        if (V) Log.v(TAG, "Deleted complete outbound shares, number =  " + delNum);

        // remove the invisible/finished/inbound/failed shares
        final String WHERE_INVISIBLE_COMPLETE_INBOUND_FAILED = BluetoothShare.DIRECTION + "="
                + BluetoothShare.DIRECTION_INBOUND + " AND " + BluetoothShare.STATUS + ">"
                + BluetoothShare.STATUS_SUCCESS + " AND " + INVISIBLE;
        delNum = getContentResolver().delete(BluetoothShare.CONTENT_URI,
                WHERE_INVISIBLE_COMPLETE_INBOUND_FAILED, null);
        if (V) Log.v(TAG, "Deleted complete inbound failed shares, number = " + delNum);

        // Only keep the inbound and successful shares for LiverFolder use
        // Keep the latest 1000 to easy db query
        final String WHERE_INBOUND_SUCCESS = BluetoothShare.DIRECTION + "="
                + BluetoothShare.DIRECTION_INBOUND + " AND " + BluetoothShare.STATUS + "="
                + BluetoothShare.STATUS_SUCCESS + " AND " + INVISIBLE;
        Cursor cursor = getContentResolver().query(BluetoothShare.CONTENT_URI, new String[] {
            BluetoothShare._ID
        }, BluetoothShare.STATUS + " >= '200'", null, BluetoothShare._ID);
        }, WHERE_INBOUND_SUCCESS, null, BluetoothShare._ID); // sort by id

        if (cursor == null) {
            // This isn't good - if we can't do basic queries in our database,
            // nothing's gonna work
            Log.e(TAG, "null cursor in trimDatabase");
            return;
        }

        int recordNum = cursor.getCount();
        if (recordNum > Constants.MAX_RECORDS_IN_DATABASE) {
            int numToDelete = recordNum - Constants.MAX_RECORDS_IN_DATABASE;
            if (cursor.moveToFirst()) {
            int numDelete = cursor.getCount() - Constants.MAX_RECORDS_IN_DATABASE;
                int columnId = cursor.getColumnIndexOrThrow(BluetoothShare._ID);
            while (numDelete > 0) {
                while (numToDelete > 0) {
                    getContentResolver().delete(
                            ContentUris.withAppendedId(BluetoothShare.CONTENT_URI, cursor
                                    .getLong(columnId)), null, null);
                    if (V) Log.v(TAG, "Deleted old inbound success share.");
                    if (!cursor.moveToNext()) {
                        break;
                    }
                numDelete--;
                    numToDelete--;
                }
            }
        }
        cursor.close();
+1 −1
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ public class Constants {
    /** use emulator to debug */
    public static final boolean USE_EMULATOR_DEBUG = false;

    public static final int MAX_RECORDS_IN_DATABASE = 20;
    public static final int MAX_RECORDS_IN_DATABASE = 1000;

    public static final int BATCH_STATUS_PENDING = 0;