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

Commit 520cbe2a authored by Juffin Alex Varghese's avatar Juffin Alex Varghese
Browse files

Bluetooth-OPP: Start Notification when transfer starts

Notification thread will start when there an ongoingtransfer and
terminate when transfer is finished. Otherwise, new thread will be
created at every 1sec.

CRs-Fixed: 745331
Change-Id: I287b1b4e6c92e260cae2a931f2e6d5c0a08823ac
parent 69a644f7
Loading
Loading
Loading
Loading
+28 −50
Original line number Diff line number Diff line
@@ -102,8 +102,6 @@ class BluetoothOppNotification {

    private NotificationUpdateThread mUpdateNotificationThread;

    private int mPendingUpdate = 0;

    private static final int NOTIFICATION_ID_OUTBOUND = -1000005;

    private static final int NOTIFICATION_ID_INBOUND = -1000006;
@@ -113,6 +111,7 @@ class BluetoothOppNotification {

    private int mInboundActiveNotificationId = 0;
    private int mOutboundActiveNotificationId = 0;
    private int mRunning = 0;

    private int mIncomingShownId = 0;

@@ -155,14 +154,10 @@ class BluetoothOppNotification {
     */
    public void updateNotification() {
        synchronized (BluetoothOppNotification.this) {
            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));
            if (mUpdateNotificationThread == null) {
                if (V) Log.v(TAG, "new notify thread!!!");
                mUpdateNotificationThread = new NotificationUpdateThread();
                mUpdateNotificationThread.start();
            }
        }
    }
@@ -171,44 +166,13 @@ class BluetoothOppNotification {
        if (V) Log.v(TAG, "Update Notification while BT is Turning OFF");
        synchronized (BluetoothOppNotification.this) {
            updateActiveNotification();
            mPendingUpdate = 0;
            mInboundUpdateCompleteNotification = true;
            mOutboundUpdateCompleteNotification = true;
            updateCompletedNotification();
            mPendingUpdate = 0;
            cancelIncomingFileConfirmNotification();
        }
    }

    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() {
@@ -218,18 +182,31 @@ class BluetoothOppNotification {
        @Override
        public void run() {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            do {
                synchronized (BluetoothOppNotification.this) {
                    if (mUpdateNotificationThread != this) {
                        throw new IllegalStateException(
                                "multiple UpdateThreads in BluetoothOppNotification");
                    }
                mPendingUpdate = 0;
                }

                updateActiveNotification();
                updateCompletedNotification();
                updateIncomingFileConfirmNotification();

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    if (V) Log.v(TAG, "NotificationThread was interrupted (1), exiting");
                    return;
                }

                if (V) Log.v(TAG, "Running = " + mRunning);
            } while (mRunning > 0);

            synchronized (BluetoothOppNotification.this) {
                mUpdateNotificationThread = null;
                if (V) Log.v(TAG, "NotificationThread is stopped!!!");
            }
        }
    }
@@ -258,6 +235,7 @@ class BluetoothOppNotification {

            cursor = mContext.getContentResolver().query(BluetoothShare.CONTENT_URI, null,
                    WHERE_RUNNING, null, BluetoothShare._ID);
            mRunning  = cursor.getCount();
        } catch (SQLiteException e) {
            cursor = null;
            Log.e(TAG, "SQLite exception: " + e);