OSDN Git Service

Trim the Bluetooth database off the main thread.
authorBrad Fitzpatrick <bradfitz@android.com>
Tue, 28 Sep 2010 15:53:41 +0000 (08:53 -0700)
committerBrad Fitzpatrick <bradfitz@android.com>
Tue, 28 Sep 2010 15:53:41 +0000 (08:53 -0700)
This looks to be most if not all of the remaining StrictMode
violations in this process.  Not a huge deal, but cleaning up noise
will make bigger problems stand out easier in the future.

Change-Id: I262194c12114114fbe059d73475afb1bbb5654ab

src/com/android/bluetooth/opp/BluetoothOppService.java

index 35a9dfb..d5edc89 100644 (file)
@@ -40,6 +40,7 @@ import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothDevice;
 import android.content.BroadcastReceiver;
 import android.content.ContentUris;
+import android.content.ContentResolver;
 import android.content.ContentValues;
 import android.content.Context;
 import android.content.Intent;
@@ -159,7 +160,12 @@ public class BluetoothOppService extends Service {
         mNotifier.mNotificationMgr.cancelAll();
         mNotifier.updateNotification();
 
-        trimDatabase();
+        final ContentResolver contentResolver = getContentResolver();
+        new Thread("trimDatabase") {
+            public void run() {
+                trimDatabase(contentResolver);
+            }
+        }.start();
 
         IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
         registerReceiver(mBluetoothReceiver, filter);
@@ -864,7 +870,8 @@ public class BluetoothOppService extends Service {
                 && info.mDirection == BluetoothShare.DIRECTION_INBOUND && !info.mMediaScanned;
     }
 
-    private void trimDatabase() {
+    // Run in a background thread at boot.
+    private static void trimDatabase(ContentResolver contentResolver) {
         final String INVISIBLE = BluetoothShare.VISIBILITY + "=" +
                 BluetoothShare.VISIBILITY_HIDDEN;
 
@@ -872,7 +879,7 @@ public class BluetoothOppService extends Service {
         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,
+        int delNum = contentResolver.delete(BluetoothShare.CONTENT_URI,
                 WHERE_INVISIBLE_COMPLETE_OUTBOUND, null);
         if (V) Log.v(TAG, "Deleted complete outbound shares, number =  " + delNum);
 
@@ -880,7 +887,7 @@ public class BluetoothOppService extends Service {
         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,
+        delNum = contentResolver.delete(BluetoothShare.CONTENT_URI,
                 WHERE_INVISIBLE_COMPLETE_INBOUND_FAILED, null);
         if (V) Log.v(TAG, "Deleted complete inbound failed shares, number = " + delNum);
 
@@ -889,7 +896,7 @@ public class BluetoothOppService extends Service {
         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 = contentResolver.query(BluetoothShare.CONTENT_URI, new String[] {
             BluetoothShare._ID
         }, WHERE_INBOUND_SUCCESS, null, BluetoothShare._ID); // sort by id
 
@@ -904,7 +911,7 @@ public class BluetoothOppService extends Service {
             if (cursor.moveToPosition(numToDelete)) {
                 int columnId = cursor.getColumnIndexOrThrow(BluetoothShare._ID);
                 long id = cursor.getLong(columnId);
-                delNum = getContentResolver().delete(BluetoothShare.CONTENT_URI,
+                delNum = contentResolver.delete(BluetoothShare.CONTENT_URI,
                         BluetoothShare._ID + " < " + id, null);
                 if (V) Log.v(TAG, "Deleted old inbound success share: " + delNum);
             }