Merge "Enhanced support to OPP content share"
diff --git a/src/com/android/bluetooth/opp/BluetoothOppNotification.java b/src/com/android/bluetooth/opp/BluetoothOppNotification.java
index e72ef3e..3a034c8 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppNotification.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppNotification.java
@@ -99,10 +99,6 @@
 
     private int mActiveNotificationId = 0;
 
-    private Notification mOutNoti = null;
-
-    private Notification mInNoti = null;
-
     /**
      * This inner class is used to describe some properties for one transfer.
      */
@@ -348,21 +344,20 @@
         outboundNum = outboundSuccNumber + outboundFailNumber;
         // create the outbound notification
         if (outboundNum > 0) {
-            mOutNoti = new Notification();
-            mOutNoti.icon = android.R.drawable.stat_sys_upload_done;
+            Notification outNoti = new Notification();
+            outNoti.icon = android.R.drawable.stat_sys_upload_done;
             title = mContext.getString(R.string.outbound_noti_title);
             caption = mContext.getString(R.string.noti_caption, outboundSuccNumber,
                     outboundFailNumber);
             intent = new Intent(Constants.ACTION_OPEN_OUTBOUND_TRANSFER);
             intent.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
-            mOutNoti.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(
+            outNoti.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(
                     mContext, 0, intent, 0));
-            mOutNoti.when = timeStamp;
-            mNotificationMgr.notify(NOTIFICATION_ID_OUTBOUND, mOutNoti);
+            outNoti.when = timeStamp;
+            mNotificationMgr.notify(NOTIFICATION_ID_OUTBOUND, outNoti);
         } else {
-            if (mNotificationMgr != null && mOutNoti != null) {
+            if (mNotificationMgr != null) {
                 mNotificationMgr.cancel(NOTIFICATION_ID_OUTBOUND);
-                mOutNoti = null;
                 if (V) Log.v(TAG, "outbound notification was removed.");
             }
         }
@@ -393,21 +388,20 @@
         inboundNum = inboundSuccNumber + inboundFailNumber;
         // create the inbound notification
         if (inboundNum > 0) {
-            mInNoti = new Notification();
-            mInNoti.icon = android.R.drawable.stat_sys_download_done;
+            Notification inNoti = new Notification();
+            inNoti.icon = android.R.drawable.stat_sys_download_done;
             title = mContext.getString(R.string.inbound_noti_title);
             caption = mContext.getString(R.string.noti_caption, inboundSuccNumber,
                     inboundFailNumber);
             intent = new Intent(Constants.ACTION_OPEN_INBOUND_TRANSFER);
             intent.setClassName(Constants.THIS_PACKAGE_NAME, BluetoothOppReceiver.class.getName());
-            mInNoti.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(
+            inNoti.setLatestEventInfo(mContext, title, caption, PendingIntent.getBroadcast(
                     mContext, 0, intent, 0));
-            mInNoti.when = timeStamp;
-            mNotificationMgr.notify(NOTIFICATION_ID_INBOUND, mInNoti);
+            inNoti.when = timeStamp;
+            mNotificationMgr.notify(NOTIFICATION_ID_INBOUND, inNoti);
         } else {
-            if (mNotificationMgr != null && mInNoti != null) {
+            if (mNotificationMgr != null) {
                 mNotificationMgr.cancel(NOTIFICATION_ID_INBOUND);
-                mInNoti = null;
                 if (V) Log.v(TAG, "inbound notification was removed.");
             }
         }
diff --git a/src/com/android/bluetooth/opp/BluetoothOppService.java b/src/com/android/bluetooth/opp/BluetoothOppService.java
index b082266..51b282c 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppService.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppService.java
@@ -904,18 +904,13 @@
         int recordNum = cursor.getCount();
         if (recordNum > Constants.MAX_RECORDS_IN_DATABASE) {
             int numToDelete = recordNum - Constants.MAX_RECORDS_IN_DATABASE;
-            if (cursor.moveToFirst()) {
+
+            if (cursor.moveToPosition(numToDelete)) {
                 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--;
-                }
+                long id = cursor.getLong(columnId);
+                delNum = getContentResolver().delete(BluetoothShare.CONTENT_URI,
+                        BluetoothShare._ID + " < " + id, null);
+                if (V) Log.v(TAG, "Deleted old inbound success share: " + delNum);
             }
         }
         cursor.close();
diff --git a/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java b/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java
index 591d556..1f5d9d9 100644
--- a/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java
+++ b/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java
@@ -36,6 +36,7 @@
 
 import android.app.Activity;
 import android.app.AlertDialog;
+import android.bluetooth.BluetoothAdapter;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.database.Cursor;
@@ -75,6 +76,9 @@
 
     private int mContextMenuPosition;
 
+    /** Class to handle Notification Manager updates */
+    private BluetoothOppNotification mNotifier;
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -117,6 +121,8 @@
             mListView.setOnCreateContextMenuListener(this);
             mListView.setOnItemClickListener(this);
         }
+
+        mNotifier = new BluetoothOppNotification(this);
     }
 
     @Override
@@ -151,12 +157,14 @@
         switch (item.getItemId()) {
             case R.id.transfer_menu_open:
                 openCompleteTransfer();
+                updateNotificationWhenBtDisabled();
                 return true;
 
             case R.id.transfer_menu_clear:
                 int sessionId = mTransferCursor.getInt(mIdColumnId);
                 Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + sessionId);
                 BluetoothOppUtility.updateVisibilityToHidden(this, contentUri);
+                updateNotificationWhenBtDisabled();
                 return true;
         }
         return false;
@@ -224,6 +232,7 @@
 
                 mTransferCursor.moveToNext();
             }
+            updateNotificationWhenBtDisabled();
         }
     }
 
@@ -237,6 +246,7 @@
         // Open the selected item
         mTransferCursor.moveToPosition(position);
         openCompleteTransfer();
+        updateNotificationWhenBtDisabled();
     }
 
     /**
@@ -265,4 +275,17 @@
             this.startActivity(in);
         }
     }
+
+    /**
+     * When Bluetooth is disabled, notification can not be updated by
+     * ContentObserver in OppService, so need update manually.
+     */
+    private void updateNotificationWhenBtDisabled() {
+        BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
+        if (!adapter.isEnabled()) {
+            if (V) Log.v(TAG, "Bluetooth is not enabled, update notification manually.");
+            mNotifier.updateNotification();
+            mNotifier.finishNotification();
+        }
+    }
 }