From: Lixin Yue Date: Wed, 3 Mar 2010 09:54:33 +0000 (+0800) Subject: Trim Opp database when service restarts. X-Git-Tag: android-7.1.2_r17~1426^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=389f6dd45f7a45b12af847a510086125777e1198;p=android-x86%2Fpackages-apps-Bluetooth.git 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 --- diff --git a/src/com/android/bluetooth/opp/BluetoothOppService.java b/src/com/android/bluetooth/opp/BluetoothOppService.java index 49226fa1..b0822660 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppService.java +++ b/src/com/android/bluetooth/opp/BluetoothOppService.java @@ -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; } - if (cursor.moveToFirst()) { - int numDelete = cursor.getCount() - Constants.MAX_RECORDS_IN_DATABASE; - int columnId = cursor.getColumnIndexOrThrow(BluetoothShare._ID); - while (numDelete > 0) { - getContentResolver().delete( - ContentUris.withAppendedId(BluetoothShare.CONTENT_URI, cursor - .getLong(columnId)), null, null); - if (!cursor.moveToNext()) { - break; + + int recordNum = cursor.getCount(); + if (recordNum > Constants.MAX_RECORDS_IN_DATABASE) { + int numToDelete = recordNum - Constants.MAX_RECORDS_IN_DATABASE; + if (cursor.moveToFirst()) { + int columnId = cursor.getColumnIndexOrThrow(BluetoothShare._ID); + 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; + } + numToDelete--; } - numDelete--; } } cursor.close(); diff --git a/src/com/android/bluetooth/opp/Constants.java b/src/com/android/bluetooth/opp/Constants.java index fe5d8791..a07eef21 100644 --- a/src/com/android/bluetooth/opp/Constants.java +++ b/src/com/android/bluetooth/opp/Constants.java @@ -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;