1
1
/*
2
+ * Copyright (c) 2013, The Linux Foundation. All rights reserved.
2
3
* Copyright (c) 2008-2009, Motorola, Inc.
3
4
*
4
5
* All rights reserved.
46
47
import android .os .Message ;
47
48
import android .os .PowerManager ;
48
49
import android .os .PowerManager .WakeLock ;
50
+ import android .os .Process ;
49
51
import android .util .Log ;
50
52
import android .webkit .MimeTypeMap ;
51
53
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
+
52
63
import javax .obex .HeaderSet ;
53
64
import javax .obex .ObexTransport ;
54
65
import javax .obex .Operation ;
@@ -157,6 +168,46 @@ public void stop() {
157
168
mSession = null ;
158
169
}
159
170
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
+
160
211
public void addShare (BluetoothOppShareInfo info ) {
161
212
if (D ) Log .d (TAG , "addShare for id " + info .mId );
162
213
mInfo = info ;
@@ -431,6 +482,7 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
431
482
*/
432
483
int status = -1 ;
433
484
BufferedOutputStream bos = null ;
485
+ ContentResolverUpdateThread uiUpdateThread = null ;
434
486
435
487
InputStream is = null ;
436
488
boolean error = false ;
@@ -481,9 +533,33 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
481
533
+ (System .currentTimeMillis () - timestamp ) + " ms" );
482
534
}
483
535
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
+ }
487
563
}
488
564
} catch (IOException e1 ) {
489
565
Log .e (TAG , "Error when receiving file" );
@@ -494,6 +570,14 @@ private int receiveFile(BluetoothOppReceiveFileInfo fileInfo, Operation op) {
494
570
status = BluetoothShare .STATUS_OBEX_DATA_ERROR ;
495
571
}
496
572
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
+ }
497
581
}
498
582
}
499
583
0 commit comments