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

Commit 81992d6c authored by Jaikumar Ganesh's avatar Jaikumar Ganesh Committed by Android (Google) Code Review
Browse files

Merge "Trim Opp database when service restarts."

parents 8b519d6c e382ffc4
Loading
Loading
Loading
Loading
+41 −14
Original line number Original line Diff line number Diff line
@@ -869,26 +869,53 @@ public class BluetoothOppService extends Service {
    }
    }


    private void trimDatabase() {
    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[] {
        Cursor cursor = getContentResolver().query(BluetoothShare.CONTENT_URI, new String[] {
            BluetoothShare._ID
            BluetoothShare._ID
        }, BluetoothShare.STATUS + " >= '200'", null, BluetoothShare._ID);
        }, WHERE_INBOUND_SUCCESS, null, BluetoothShare._ID); // sort by id

        if (cursor == null) {
        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;
            return;
        }
        }

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