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

Commit 097d4de2 authored by Sravan Kumar V's avatar Sravan Kumar V Committed by Gerrit - the friendly Code Review server
Browse files

Bluetooth: Single Contentresolver thread will be started during transfer

Single Contentresolver thread will be started to update UI progress
during transfer. Otherwise, multiple instances will cause fatal
exceptions and lead to application crash.

CRs-Fixed: 734425
Change-Id: I4a25a6949c6db4a16663d69567d5aa4effc5c8b4
parent 09ef1745
Loading
Loading
Loading
Loading
+20 −18
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {

    private final int MIN_FILE_LEN_FOR_TPUT_MEASUREMENT = 500000;

    private ContentResolverUpdateThread uiUpdateThread = null;
    private long position;

    public BluetoothOppObexClientSession(Context context, ObexTransport transport) {
        if (transport == null) {
@@ -125,38 +125,37 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
    }

    private class ContentResolverUpdateThread extends Thread {
        private static final int sSleepTime = 1000;
        private Uri contentUri;
        private Context mContext1;
        private long position;

        public ContentResolverUpdateThread(Context context, Uri cntUri, long pos) {
        public ContentResolverUpdateThread(Context context, Uri cntUri) {
            super("BtOpp ContentResolverUpdateThread");
            mContext1 = context;
            contentUri = cntUri;
            position = pos;
        }


        @Override
        public void run() {

            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

            synchronized (BluetoothOppObexClientSession.this) {
                if (uiUpdateThread != this) {
                    throw new IllegalStateException(
                        "multiple UpdateThreads in BluetoothOppObexClientSession");
                }
            }
            ContentValues updateValues;

                ContentValues updateValues = new ContentValues();
            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            if (V) Log.v(TAG, "Is ContentResolverUpdateThread Interrupted :" + isInterrupted());
            /*  Check if the Operation is interrupted before entering into loop */
            while (!isInterrupted()) {
                updateValues = new ContentValues();
                updateValues.put(BluetoothShare.CURRENT_BYTES, position);
                mContext1.getContentResolver().update(contentUri, updateValues,
                        null, null);
                synchronized (BluetoothOppObexClientSession.this) {
                    uiUpdateThread = null;


                try {
                    Thread.sleep(sSleepTime);
                } catch (InterruptedException e1) {
                    if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted (1), exiting");
                    return;
                }
            }
        }
    }
@@ -379,10 +378,11 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {
        private int sendFile(BluetoothOppSendFileInfo fileInfo) {
            boolean error = false;
            int responseCode = -1;
            long position = 0;
            position = 0;
            int status = BluetoothShare.STATUS_SUCCESS;
            Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + mInfo.mId);
            ContentValues updateValues;
            ContentResolverUpdateThread uiUpdateThread = null;
            HeaderSet reply = new HeaderSet();
            HeaderSet request;
            request = new HeaderSet();
@@ -521,7 +521,8 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {

                                if (uiUpdateThread == null) {
                                    uiUpdateThread = new ContentResolverUpdateThread (mContext1,
                                                                    contentUri, position);
                                                             contentUri);
                                    if (V) Log.v(TAG, "Worker for Updation : Created");
                                    uiUpdateThread.start ( );
                                }
                            }
@@ -530,6 +531,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession {

                    if (uiUpdateThread != null) {
                        try {
                            if (V) Log.v(TAG, "Worker for Updation : Destroying");
                            uiUpdateThread.interrupt ();
                            uiUpdateThread.join ();
                            uiUpdateThread = null;
+30 −23
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen

    boolean mTransferInProgress = false;

    private ContentResolverUpdateThread uiUpdateThread = null;
    private int position;

    public BluetoothOppObexServerSession(Context context, ObexTransport transport) {
        mContext = context;
@@ -171,38 +171,43 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen

    private class ContentResolverUpdateThread extends Thread {

        private static final int sSleepTime = 1000;
        private Uri contentUri;
        private Context mContext1;
        private long position;

        public ContentResolverUpdateThread(Context context, Uri cntUri, long pos) {
        private volatile boolean interrupted = false;
        public ContentResolverUpdateThread(Context context, Uri cntUri) {
            super("BtOpp Server ContentResolverUpdateThread");
            mContext1 = context;
            contentUri = cntUri;
            position = pos;
        }

        @Override
        public void run() {

            Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
            synchronized (BluetoothOppObexServerSession.this) {
                if (uiUpdateThread != this) {
                    throw new IllegalStateException(
                        "multiple UpdateThreads in BluetoothOppObexServerSession");
                }
            }
            ContentValues updateValues;
            if (V) Log.v(TAG, "Is ContentResolverUpdateThread Interrupted :" + isInterrupted());
            /*  Check if the Operation is interrupted before entering into loop */

                ContentValues  updateValues = new ContentValues();
            while ( !isInterrupted() ) {
               updateValues = new ContentValues();
               updateValues.put(BluetoothShare.CURRENT_BYTES, position);
               mContext1.getContentResolver().update(contentUri, updateValues,
                   null, null);
             synchronized (BluetoothOppObexServerSession.this) {
                uiUpdateThread = null;
               /* Check if the Operation is interrupted before entering sleep */
               if (isInterrupted()) {
                   if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted before sleep !,"+
                                     " exiting");
                   return ;
               }

               try {
                   Thread.sleep(sSleepTime);
               } catch (InterruptedException e1) {
                   if (V) Log.v(TAG, "Server ContentResolverUpdateThread was interrupted (1),"+
                                     " exiting");
                   return ;
               }
            }
        }
    }
    /*
@@ -501,6 +506,8 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
        long beginTime = 0;
        int status = -1;
        BufferedOutputStream bos = null;
        ContentResolverUpdateThread uiUpdateThread = null;


        InputStream is = null;
        boolean error = false;
@@ -520,7 +527,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
            mContext.getContentResolver().update(contentUri, updateValues, null, null);
        }

        long position = 0;
        position = 0;
        if (!error) {
            bos = new BufferedOutputStream(fileInfo.mOutputStream, 0x10000);
        }
@@ -552,7 +559,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen
                    }

                    if (uiUpdateThread == null) {
                        uiUpdateThread = new ContentResolverUpdateThread (mContext, contentUri, position);
                        uiUpdateThread = new ContentResolverUpdateThread (mContext, contentUri);
                        if (V) Log.v(TAG, "Worker for Updation : Created");
                        uiUpdateThread.start();
                    }