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

Commit c22e91a3 authored by Juffin Alex Varghese's avatar Juffin Alex Varghese
Browse files

Bluetooth-OPP: Monitor display status during transfer

Check the status of the display during ongoing transfer to avoid
creating new update threads for updating the transfer progress if
display is off. Otherwise, power consumption will increase even
though display is off because of starting new thread every one
second.

CRs-Fixed: 745331
Change-Id: I862a163d31cf9a174e907ccb48b92e7cf77b4441
parent 520cbe2a
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.database.sqlite.SQLiteException;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.util.Log;
import android.os.PowerManager;
import android.os.Handler;
import android.os.Message;
import android.os.Process;
@@ -102,6 +103,8 @@ class BluetoothOppNotification {

    private NotificationUpdateThread mUpdateNotificationThread;

    private PowerManager mPowerManager;

    private static final int NOTIFICATION_ID_OUTBOUND = -1000005;

    private static final int NOTIFICATION_ID_INBOUND = -1000006;
@@ -147,6 +150,7 @@ class BluetoothOppNotification {
        mNotificationMgr = (NotificationManager)mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotifications = new HashMap<String, NotificationItem>();
        mPowerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
    }

    /**
@@ -195,14 +199,16 @@ class BluetoothOppNotification {
                updateIncomingFileConfirmNotification();

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

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

            synchronized (BluetoothOppNotification.this) {
                mUpdateNotificationThread = null;
+12 −8
Original line number Diff line number Diff line
@@ -85,6 +85,8 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {

    private Handler mCallback;

    private PowerManager pm;

    private long position;

    public BluetoothOppObexClientSession(Context context, ObexTransport transport) {
@@ -133,7 +135,6 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
    }
    private class ContentResolverUpdateThread extends Thread {

        private static final int sSleepTime = 1000;
        private Uri contentUri;
        private Context mContext1;
        private volatile boolean interrupted = false;
@@ -150,10 +151,12 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
            ContentValues updateValues;

            while (true) {
                if (pm.isScreenOn()) {
                    updateValues = new ContentValues();
                    updateValues.put(BluetoothShare.CURRENT_BYTES, position);
                    mContext1.getContentResolver().update(contentUri, updateValues,
                            null, null);
                }

                /* Check if the Operation is interrupted before entering sleep */
                if (interrupted == true) {
@@ -162,7 +165,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                }

                try {
                    Thread.sleep(sSleepTime);
                    Thread.sleep(BluetoothShare.UI_UPDATE_INTERVAL);
                } catch (InterruptedException e1) {
                    if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted (1), exiting");
                    return;
@@ -208,7 +211,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
            waitingForShare = true;
            mWaitingForRemote = false;
            mNumShares = initialNumShares;
            PowerManager pm = (PowerManager)mContext1.getSystemService(Context.POWER_SERVICE);
            pm = (PowerManager)mContext1.getSystemService(Context.POWER_SERVICE);
            wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        }

@@ -666,6 +669,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
                        Log.i(TAG, "SendFile finished sending file " + fileInfo.mFileName
                                + " length " + fileInfo.mLength
                                + "Bytes in " + (endTime - beginTime) + "ms"  );
                        status = BluetoothShare.STATUS_SUCCESS;
                        outputStream.close();
                    } else {
                        error = true;
+12 −9
Original line number Diff line number Diff line
@@ -110,20 +110,22 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen

    private BluetoothOppReceiveFileInfo mFileInfo;

    private PowerManager pm;

    private WakeLock mWakeLock;

    private WakeLock mPartialWakeLock;

    private long position;

    boolean mTimeoutMsgSent = false;

    boolean mTransferInProgress = false;

    private int position;

    public BluetoothOppObexServerSession(Context context, ObexTransport transport) {
        mContext = context;
        mTransport = transport;
        PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, TAG);
        mPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
@@ -194,7 +196,6 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen

    private class ContentResolverUpdateThread extends Thread {

        private static final int sSleepTime = 1000;
        private Uri contentUri;
        private Context mContext1;
        private volatile boolean interrupted = false;
@@ -210,10 +211,12 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            ContentValues updateValues;
            while (true) {
                if (pm.isScreenOn()) {
                    updateValues = new ContentValues();
                    updateValues.put(BluetoothShare.CURRENT_BYTES, position);
                    mContext1.getContentResolver().update(contentUri, updateValues,
                            null, null);
                }

                /* Check if the Operation is interrupted before entering sleep */
                if (interrupted == true) {
@@ -222,7 +225,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
                }

                try {
                    Thread.sleep(sSleepTime);
                    Thread.sleep(BluetoothShare.UI_UPDATE_INTERVAL);
                } catch (InterruptedException e1) {
                    if (V) Log.v(TAG, "Server ContentResolverUpdateThread was interrupted (1), exiting");
                    return;
+24 −17
Original line number Diff line number Diff line
@@ -96,8 +96,6 @@ public class BluetoothOppService extends Service {
    /** Class to handle Notification Manager updates */
    private BluetoothOppNotification mNotifier;

    private boolean mPendingUpdate;

    private UpdateThread mUpdateThread;

    private ArrayList<BluetoothOppShareInfo> mShares;
@@ -439,10 +437,10 @@ public class BluetoothOppService extends Service {

    private void updateFromProvider() {
        synchronized (BluetoothOppService.this) {
            mPendingUpdate = true;
            if ((mUpdateThread == null) && (mAdapter != null)
                && mAdapter.isEnabled()) {
                if (V) Log.v(TAG, "Starting a new thread");
                mPowerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
                mUpdateThread = new UpdateThread();
                mUpdateThread.start();
            }
@@ -459,25 +457,14 @@ public class BluetoothOppService extends Service {
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

            boolean keepService = false;
            for (;;) {
            do {
                synchronized (BluetoothOppService.this) {
                    if (mUpdateThread != this) {
                        throw new IllegalStateException(
                                "multiple UpdateThreads in BluetoothOppService");
                    }
                    if (V) Log.v(TAG, "pendingUpdate is " + mPendingUpdate + " keepUpdateThread is "
                                + keepService + " sListenStarted is " + mListenStarted);
                    if (!mPendingUpdate) {
                        mUpdateThread = null;
                        if (!keepService && !mListenStarted) {
                            if (V) Log.v(TAG, "Need to stop self");
                            stopSelf();
                            break;
                        }
                        if (V) Log.v(TAG, "***returning from updatethread***");
                        return;
                    }
                    mPendingUpdate = false;
                    if (V) Log.v(TAG, "keepUpdateThread is " + keepService + " sListenStarted is "
                            + mListenStarted);
                }
                Cursor cursor;
                try {
@@ -612,6 +599,26 @@ public class BluetoothOppService extends Service {

                cursor.close();
                cursor = null;

                try {
                    if (mPowerManager.isScreenOn()) {
                        Thread.sleep(BluetoothShare.UI_UPDATE_INTERVAL);
                    }
                } catch (InterruptedException e) {
                    if (V) Log.v(TAG, "OppService UpdateThread was interrupted (1), exiting");
                    return;
                }

                if (V) Log.v(TAG, "Batch size= " + mBatchs.size());
            } while ((mBatchs.size() > 0) && mPowerManager.isScreenOn());

            synchronized (BluetoothOppService.this) {
                mUpdateThread = null;
                if (!keepService && !mListenStarted) {
                    if (V) Log.v(TAG, "Need to stop self");
                    stopSelf();
                }
                if (V) Log.v(TAG, "***returning from updatethread***");
            }
        }

+5 −0
Original line number Diff line number Diff line
@@ -421,4 +421,9 @@ public final class BluetoothShare implements BaseColumns {
     */
    public static final int STATUS_CONNECTION_ERROR = 497;

    /**
     * Ongoing transfer progress update interval to 1 second
     */
    public static final int UI_UPDATE_INTERVAL = 1000;

}