diff options
author | Juffin Alex Varghese <jalex@codeaurora.org> | 2014-10-06 16:04:07 +0530 |
---|---|---|
committer | Linux Build Service Account <lnxbuild@localhost> | 2014-11-04 08:26:18 -0700 |
commit | bc4f106e91a66864c56d2333f2e1f13ab5fed50e (patch) | |
tree | 9c4bf3193eb7aa82af73c2448fb1b1d7f19c57cf /src/com/android/bluetooth/opp | |
parent | 78d8abf7b5a6c885f671d277b1420c4157431c8c (diff) | |
download | android_packages_apps_Bluetooth-bc4f106e91a66864c56d2333f2e1f13ab5fed50e.tar.gz android_packages_apps_Bluetooth-bc4f106e91a66864c56d2333f2e1f13ab5fed50e.tar.bz2 android_packages_apps_Bluetooth-bc4f106e91a66864c56d2333f2e1f13ab5fed50e.zip |
Bluetooth: OPP: 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
Diffstat (limited to 'src/com/android/bluetooth/opp')
-rw-r--r-- | src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java | 55 | ||||
-rw-r--r-- | src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java | 56 |
2 files changed, 69 insertions, 42 deletions
diff --git a/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java b/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java index afd202858..6dcf5ef9a 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java +++ b/src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java @@ -85,7 +85,7 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession { private Handler mCallback; - private ContentResolverUpdateThread uiUpdateThread = null; + private long position; public BluetoothOppObexClientSession(Context context, ObexTransport transport) { if (transport == null) { @@ -133,33 +133,47 @@ 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; + private volatile boolean interrupted = false; - 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; + + while (true) { + 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) { + if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted before sleep !, exiting"); + return; + } + + try { + Thread.sleep(sSleepTime); + } catch (InterruptedException e1) { + if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted (1), exiting"); + return; } } - ContentValues updateValues = new ContentValues(); - updateValues.put(BluetoothShare.CURRENT_BYTES, position); - mContext1.getContentResolver().update(contentUri, updateValues, - null, null); - synchronized (BluetoothOppObexClientSession.this) { - uiUpdateThread = null; - } + } + + @Override + public void interrupt() { + interrupted = true; + super.interrupt(); } } @@ -397,8 +411,9 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession { int status = BluetoothShare.STATUS_SUCCESS; Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + mInfo.mId); ContentValues updateValues; + ContentResolverUpdateThread uiUpdateThread = null; HeaderSet reply; - long position = 0; + position = 0; reply = new HeaderSet(); HeaderSet request; request = new HeaderSet(); @@ -612,9 +627,10 @@ public class BluetoothOppObexClientSession implements BluetoothOppObexSession { } if (uiUpdateThread == null) { - uiUpdateThread = new ContentResolverUpdateThread (mContext1, - contentUri, position); - uiUpdateThread.start ( ); + uiUpdateThread = new ContentResolverUpdateThread(mContext1, + contentUri); + if (V) Log.v(TAG, "Worker for Updation : Created"); + uiUpdateThread.start(); } } @@ -623,6 +639,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; diff --git a/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java b/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java index 978b83127..d9777862d 100644 --- a/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java +++ b/src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java @@ -118,7 +118,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; @@ -194,34 +194,47 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen private class ContentResolverUpdateThread extends Thread { + private static final int sSleepTime = 1000; private Uri contentUri; private Context mContext1; - private int position; + private volatile boolean interrupted = false; - public ContentResolverUpdateThread(Context context, Uri cntUri, int pos) { + 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; + while (true) { + 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) { + 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; } - } - ContentValues updateValues = new ContentValues(); - updateValues.put(BluetoothShare.CURRENT_BYTES, position); - mContext1.getContentResolver().update(contentUri, updateValues, - null, null); - synchronized (BluetoothOppObexServerSession.this) { - uiUpdateThread = null; } } + + @Override + public void interrupt() { + interrupted = true; + super.interrupt(); + } } /* @@ -540,6 +553,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen long beginTime = 0; int status = -1; BufferedOutputStream bos = null; + ContentResolverUpdateThread uiUpdateThread = null; InputStream is = null; boolean error = false; @@ -559,7 +573,7 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen mContext.getContentResolver().update(contentUri, updateValues, null, null); } - int position = 0; + position = 0; if (!error) { bos = new BufferedOutputStream(fileInfo.mOutputStream, 0x10000); } @@ -592,19 +606,15 @@ public class BluetoothOppObexServerSession extends ServerRequestHandler implemen } if (uiUpdateThread == null) { - uiUpdateThread = new ContentResolverUpdateThread (mContext, contentUri, position); - if (V) { - Log.v(TAG, "Worker for Updation : Created"); - } + uiUpdateThread = new ContentResolverUpdateThread (mContext, contentUri); + if (V) Log.v(TAG, "Worker for Updation : Created"); uiUpdateThread.start(); } } if (uiUpdateThread != null) { try { - if (V) { - Log.v(TAG, "Worker for Updation : Destroying"); - } + if (V) Log.v(TAG, "Worker for Updation : Destroying"); uiUpdateThread.interrupt (); uiUpdateThread.join (); uiUpdateThread = null; |