From eb77c4b1d9aa71d7dd8dd934e9d1971583f9c0e2 Mon Sep 17 00:00:00 2001 From: Liejun Tao Date: Mon, 2 Aug 2010 17:37:23 -0500 Subject: [PATCH] Optimize Opp Notification Current Opp notification is running too frequently. When file transfer speed is fast, it could update notification 7-8 times in 1 second. Use a timer to limit the notification to once per second. Change-Id: I162033d9e12148260515c8ed318928e4de9a4392 --- .../bluetooth/opp/BluetoothOppNotification.java | 79 ++++++++++++++-------- .../android/bluetooth/opp/BluetoothOppService.java | 4 -- .../bluetooth/opp/BluetoothOppTransferHistory.java | 1 - 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/src/com/android/bluetooth/opp/BluetoothOppNotification.java b/src/com/android/bluetooth/opp/BluetoothOppNotification.java index 310a289a..0600cb5c 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppNotification.java +++ b/src/com/android/bluetooth/opp/BluetoothOppNotification.java @@ -43,6 +43,8 @@ import android.database.Cursor; import android.net.Uri; import android.util.Log; import android.widget.RemoteViews; +import android.os.Handler; +import android.os.Message; import android.os.Process; import java.util.HashMap; @@ -87,9 +89,7 @@ class BluetoothOppNotification { private NotificationUpdateThread mUpdateNotificationThread; - private boolean mPendingUpdate = false; - - private boolean mFinised = false; + private int mPendingUpdate = 0; private static final int NOTIFICATION_ID_OUTBOUND = -1000005; @@ -127,26 +127,52 @@ class BluetoothOppNotification { mNotifications = new HashMap(); } - public void finishNotification() { - synchronized (BluetoothOppNotification.this) { - mFinised = true; - } - } - /** * Update the notification ui. */ public void updateNotification() { synchronized (BluetoothOppNotification.this) { - mPendingUpdate = true; - if (mUpdateNotificationThread == null) { - mUpdateNotificationThread = new NotificationUpdateThread(); - mUpdateNotificationThread.start(); - mFinised = false; + mPendingUpdate++; + if (mPendingUpdate > 1) { + if (V) Log.v(TAG, "update too frequent, put in queue"); + return; + } + if (!mHandler.hasMessages(NOTIFY)) { + if (V) Log.v(TAG, "send message"); + mHandler.sendMessage(mHandler.obtainMessage(NOTIFY)); } } } + private static final int NOTIFY = 0; + // Use 1 second timer to limit notification frequency. + // 1. On the first notification, create the update thread. + // Buffer other updates. + // 2. Update thread will clear mPendingUpdate. + // 3. Handler sends a delayed message to self + // 4. Handler checks if there are any more updates after 1 second. + // 5. If there is an update, update it else stop. + private Handler mHandler = new Handler() { + public void handleMessage(Message msg) { + switch (msg.what) { + case NOTIFY: + synchronized (BluetoothOppNotification.this) { + if (mPendingUpdate > 0 && mUpdateNotificationThread == null) { + if (V) Log.v(TAG, "new notify threadi!"); + mUpdateNotificationThread = new NotificationUpdateThread(); + mUpdateNotificationThread.start(); + if (V) Log.v(TAG, "send delay message"); + mHandler.sendMessageDelayed(mHandler.obtainMessage(NOTIFY), 1000); + } else if (mPendingUpdate > 0) { + if (V) Log.v(TAG, "previous thread is not finished yet"); + mHandler.sendMessageDelayed(mHandler.obtainMessage(NOTIFY), 1000); + } + break; + } + } + } + }; + private class NotificationUpdateThread extends Thread { public NotificationUpdateThread() { @@ -156,21 +182,18 @@ class BluetoothOppNotification { @Override public void run() { Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); - for (;;) { - synchronized (BluetoothOppNotification.this) { - if (mUpdateNotificationThread != this) { - throw new IllegalStateException( - "multiple UpdateThreads in BluetoothOppNotification"); - } - if (!mPendingUpdate && mFinised) { - mUpdateNotificationThread = null; - return; - } - mPendingUpdate = false; + synchronized (BluetoothOppNotification.this) { + if (mUpdateNotificationThread != this) { + throw new IllegalStateException( + "multiple UpdateThreads in BluetoothOppNotification"); } - updateActiveNotification(); - updateCompletedNotification(); - updateIncomingFileConfirmNotification(); + mPendingUpdate = 0; + } + updateActiveNotification(); + updateCompletedNotification(); + updateIncomingFileConfirmNotification(); + synchronized (BluetoothOppNotification.this) { + mUpdateNotificationThread = null; } } } diff --git a/src/com/android/bluetooth/opp/BluetoothOppService.java b/src/com/android/bluetooth/opp/BluetoothOppService.java index 51b282c9..35a9dfb1 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppService.java +++ b/src/com/android/bluetooth/opp/BluetoothOppService.java @@ -158,7 +158,6 @@ public class BluetoothOppService extends Service { mNotifier = new BluetoothOppNotification(this); mNotifier.mNotificationMgr.cancelAll(); mNotifier.updateNotification(); - mNotifier.finishNotification(); trimDatabase(); @@ -316,7 +315,6 @@ public class BluetoothOppService extends Service { public void onDestroy() { if (V) Log.v(TAG, "Service onDestroy"); super.onDestroy(); - mNotifier.finishNotification(); getContentResolver().unregisterContentObserver(mObserver); unregisterReceiver(mBluetoothReceiver); mSocketListener.stop(); @@ -391,8 +389,6 @@ public class BluetoothOppService extends Service { stopSelf(); break; } - mNotifier.updateNotification(); - mNotifier.finishNotification(); return; } mPendingUpdate = false; diff --git a/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java b/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java index 1f5d9d98..accdbc04 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java +++ b/src/com/android/bluetooth/opp/BluetoothOppTransferHistory.java @@ -285,7 +285,6 @@ public class BluetoothOppTransferHistory extends Activity implements if (!adapter.isEnabled()) { if (V) Log.v(TAG, "Bluetooth is not enabled, update notification manually."); mNotifier.updateNotification(); - mNotifier.finishNotification(); } } } -- 2.11.0