Skip to content

Commit c71db51

Browse files
Smriti Guptaandi34
authored andcommitted
Bluetooth: OPP Update progress in worker thread
OPP throughput was seen very less because of the progress update from BT Read/Write path. Moved it to worker thread. Change-Id: I884356dc8b42a7f7eca1b8581e3118d5ec2ce9e2 (cherry picked from commit a81f3906a955e7188df116b189a83ea2182a1119) (cherry picked from commit a54f9274290a89c832a5aecf891d272c220c483f) (cherry picked from commit c507eaa18533c81788811c4485e3ea8dbe62e7a1)
1 parent 1ae385d commit c71db51

File tree

2 files changed

+161
-7
lines changed

2 files changed

+161
-7
lines changed

src/com/android/bluetooth/opp/BluetoothOppObexClientSession.java

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2013, The Linux Foundation. All rights reserved.
23
* Copyright (c) 2008-2009, Motorola, Inc.
34
*
45
* All rights reserved.
@@ -119,6 +120,45 @@ private static int readFully(InputStream is, byte[] buffer, int size) throws IOE
119120
}
120121
return done;
121122
}
123+
private class ContentResolverUpdateThread extends Thread {
124+
125+
private static final int sSleepTime = 500;
126+
private Uri contentUri;
127+
private Context mContext1;
128+
private int position;
129+
130+
public ContentResolverUpdateThread(Context context, Uri cntUri, int pos) {
131+
super("BtOpp ContentResolverUpdateThread");
132+
mContext1 = context;
133+
contentUri = cntUri;
134+
position = pos;
135+
}
136+
137+
public void updateProgress (int pos) {
138+
position = pos;
139+
}
140+
141+
@Override
142+
public void run() {
143+
ContentValues updateValues;
144+
145+
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
146+
147+
while (true) {
148+
updateValues = new ContentValues();
149+
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
150+
mContext1.getContentResolver().update(contentUri, updateValues,
151+
null, null);
152+
153+
try {
154+
Thread.sleep(sSleepTime);
155+
} catch (InterruptedException e1) {
156+
if (V) Log.v(TAG, "ContentResolverUpdateThread was interrupted (1), exiting");
157+
return;
158+
}
159+
}
160+
}
161+
}
122162

123163
private class ClientThread extends Thread {
124164

@@ -340,6 +380,9 @@ private int sendFile(BluetoothOppSendFileInfo fileInfo) {
340380
int status = BluetoothShare.STATUS_SUCCESS;
341381
Uri contentUri = Uri.parse(BluetoothShare.CONTENT_URI + "/" + mInfo.mId);
342382
ContentValues updateValues;
383+
ContentResolverUpdateThread uiUpdateThread = null;
384+
HeaderSet reply;
385+
reply = new HeaderSet();
343386
HeaderSet request;
344387
request = new HeaderSet();
345388
request.setHeader(HeaderSet.NAME, fileInfo.mFileName);
@@ -465,14 +508,34 @@ private int sendFile(BluetoothOppSendFileInfo fileInfo) {
465508
+ " readLength " + readLength + " bytes took "
466509
+ (System.currentTimeMillis() - timestamp) + " ms");
467510
}
468-
updateValues = new ContentValues();
469-
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
470-
mContext1.getContentResolver().update(contentUri, updateValues,
471-
null, null);
511+
512+
if (uiUpdateThread == null) {
513+
uiUpdateThread = new ContentResolverUpdateThread (mContext1,
514+
contentUri, position);
515+
uiUpdateThread.start ( );
516+
} else {
517+
uiUpdateThread.updateProgress (position);
518+
}
519+
472520
}
473521
}
474522
}
475523

524+
if (uiUpdateThread != null) {
525+
try {
526+
uiUpdateThread.interrupt ();
527+
uiUpdateThread.join ();
528+
uiUpdateThread = null;
529+
530+
updateValues = new ContentValues();
531+
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
532+
mContext1.getContentResolver().update(contentUri, updateValues,
533+
null, null);
534+
} catch (InterruptedException ie) {
535+
if (V) Log.v(TAG, "Interrupted waiting for uiUpdateThread to join");
536+
}
537+
}
538+
476539
if (responseCode == ResponseCodes.OBEX_HTTP_FORBIDDEN
477540
|| responseCode == ResponseCodes.OBEX_HTTP_NOT_ACCEPTABLE) {
478541
Log.i(TAG, "Remote reject file " + fileInfo.mFileName + " length "
@@ -504,6 +567,13 @@ private int sendFile(BluetoothOppSendFileInfo fileInfo) {
504567
try {
505568
// Close InputStream and remove SendFileInfo from map
506569
BluetoothOppUtility.closeSendFileInfo(mInfo.mUri);
570+
571+
if (uiUpdateThread != null) {
572+
uiUpdateThread.interrupt ();
573+
uiUpdateThread = null;
574+
}
575+
576+
fileInfo.mInputStream.close();
507577
if (!error) {
508578
responseCode = putOperation.getResponseCode();
509579
if (responseCode != -1) {

src/com/android/bluetooth/opp/BluetoothOppObexServerSession.java

Lines changed: 87 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright (c) 2013, The Linux Foundation. All rights reserved.
23
* Copyright (c) 2008-2009, Motorola, Inc.
34
*
45
* All rights reserved.
@@ -46,9 +47,19 @@
4647
import android.os.Message;
4748
import android.os.PowerManager;
4849
import android.os.PowerManager.WakeLock;
50+
import android.os.Process;
4951
import android.util.Log;
5052
import android.webkit.MimeTypeMap;
5153

54+
import android.os.SystemProperties;
55+
import android.database.Cursor;
56+
import android.provider.ContactsContract.Contacts;
57+
import android.provider.ContactsContract.Profile;
58+
import java.io.FileNotFoundException;
59+
import java.io.FileOutputStream;
60+
import java.lang.Thread;
61+
62+
5263
import javax.obex.HeaderSet;
5364
import javax.obex.ObexTransport;
5465
import javax.obex.Operation;
@@ -157,6 +168,46 @@ public void stop() {
157168
mSession = null;
158169
}
159170

171+
private class ContentResolverUpdateThread extends Thread {
172+
173+
private static final int sSleepTime = 500;
174+
private Uri contentUri;
175+
private Context mContext1;
176+
private int position;
177+
178+
public ContentResolverUpdateThread(Context context, Uri cntUri, int pos) {
179+
super("BtOpp Server ContentResolverUpdateThread");
180+
mContext1 = context;
181+
contentUri = cntUri;
182+
position = pos;
183+
}
184+
185+
public void updateProgress (int pos) {
186+
position = pos;
187+
}
188+
189+
@Override
190+
public void run() {
191+
ContentValues updateValues;
192+
193+
Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);
194+
195+
while (true) {
196+
updateValues = new ContentValues();
197+
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
198+
mContext1.getContentResolver().update(contentUri, updateValues,
199+
null, null);
200+
201+
try {
202+
Thread.sleep(sSleepTime);
203+
} catch (InterruptedException e1) {
204+
if (V) Log.v(TAG, "Server ContentResolverUpdateThread was interrupted (1), exiting");
205+
return;
206+
}
207+
}
208+
}
209+
}
210+
160211
public void addShare(BluetoothOppShareInfo info) {
161212
if (D) Log.d(TAG, "addShare for id " + info.mId);
162213
mInfo = info;
@@ -431,6 +482,7 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
431482
*/
432483
int status = -1;
433484
BufferedOutputStream bos = null;
485+
ContentResolverUpdateThread uiUpdateThread = null;
434486

435487
InputStream is = null;
436488
boolean error = false;
@@ -481,9 +533,33 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
481533
+ (System.currentTimeMillis() - timestamp) + " ms");
482534
}
483535

484-
ContentValues updateValues = new ContentValues();
485-
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
486-
mContext.getContentResolver().update(contentUri, updateValues, null, null);
536+
if (uiUpdateThread == null) {
537+
uiUpdateThread = new ContentResolverUpdateThread (mContext, contentUri, position);
538+
if (V) {
539+
Log.v(TAG, "Worker for Updation : Created");
540+
}
541+
uiUpdateThread.start();
542+
} else {
543+
uiUpdateThread.updateProgress (position);
544+
}
545+
}
546+
547+
if (uiUpdateThread != null) {
548+
try {
549+
if (V) {
550+
Log.v(TAG, "Worker for Updation : Destroying");
551+
}
552+
uiUpdateThread.interrupt ();
553+
uiUpdateThread.join ();
554+
uiUpdateThread = null;
555+
556+
ContentValues updateValues = new ContentValues();
557+
updateValues.put(BluetoothShare.CURRENT_BYTES, position);
558+
mContext.getContentResolver().update(contentUri, updateValues,
559+
null, null);
560+
} catch (InterruptedException ie) {
561+
if (V) Log.v(TAG, "Interrupted waiting for uiUpdateThread to join");
562+
}
487563
}
488564
} catch (IOException e1) {
489565
Log.e(TAG, "Error when receiving file");
@@ -494,6 +570,14 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
494570
status = BluetoothShare.STATUS_OBEX_DATA_ERROR;
495571
}
496572
error = true;
573+
} finally {
574+
if (uiUpdateThread != null) {
575+
if (V) {
576+
Log.v(TAG, "Worker for Updation : Finally Destroying");
577+
}
578+
uiUpdateThread.interrupt ();
579+
uiUpdateThread = null;
580+
}
497581
}
498582
}
499583

0 commit comments

Comments
 (0)