40
40
import java .util .concurrent .ThreadFactory ;
41
41
import java .util .concurrent .ThreadPoolExecutor ;
42
42
import java .util .concurrent .TimeUnit ;
43
+ import java .util .concurrent .atomic .AtomicBoolean ;
43
44
import java .util .concurrent .atomic .AtomicInteger ;
44
45
import java .util .function .BiConsumer ;
45
46
@@ -48,8 +49,17 @@ public class TransferService extends EventService {
48
49
49
50
private TransferNotificationDispatcher transferNotificationDispatcher ;
50
51
52
+ private static final ConcurrentHashMap <FeatureDataSource , CompletableFuture <Void >> _activeTasks = new ConcurrentHashMap <>();
53
+ private static final AtomicBoolean _isRunning = new AtomicBoolean (false );
51
54
private final AtomicInteger runningTaskCount = new AtomicInteger (0 );
52
- private final ConcurrentHashMap <FeatureDataSource , CompletableFuture <Void >> _activeTasks = new ConcurrentHashMap <>();
55
+
56
+ public static boolean getServiceRunning () {
57
+ return _isRunning .get ();
58
+ }
59
+
60
+ public static ConcurrentHashMap <FeatureDataSource , CompletableFuture <Void >> getActiveTasks () {
61
+ return _activeTasks ;
62
+ }
53
63
54
64
private ThreadPoolExecutor _executor ;
55
65
// 使用线程池工厂创建可配置的线程池
@@ -61,38 +71,38 @@ public static void restartPhotoBackupService(Context context) {
61
71
Bundle bundle = new Bundle ();
62
72
bundle .putBoolean ("is_force" , true );
63
73
bundle .putBoolean ("is_restart" , true );
64
- startService (context , " RESTART_PHOTO_BACKUP" , bundle );
74
+ startService (context , ServiceActionEnum . RESTART_PHOTO_BACKUP . name () , bundle );
65
75
}
66
76
67
77
public static void stopPhotoBackupService (Context context ) {
68
- startService (context , " STOP_PHOTO_BACKUP" );
78
+ startService (context , ServiceActionEnum . STOP_PHOTO_BACKUP . name () );
69
79
}
70
80
71
- public static void restartFolderBackupService (Context context ) {
81
+ public static void restartFolderBackupService (Context context , boolean isForce ) {
72
82
Bundle bundle = new Bundle ();
73
- bundle .putBoolean ("is_force" , true );
83
+ bundle .putBoolean ("is_force" , isForce );
74
84
bundle .putBoolean ("is_restart" , true );
75
- startService (context , "START_FOLDER_BACKUP" , bundle );
85
+ startService (context , ServiceActionEnum . RESTART_FOLDER_BACKUP . name () , bundle );
76
86
}
77
87
78
88
public static void stopFolderBackupService (Context context ) {
79
- startService (context , " STOP_FOLDER_BACKUP" );
89
+ startService (context , ServiceActionEnum . STOP_FOLDER_BACKUP . name () );
80
90
}
81
91
82
92
public static void startManualUploadService (Context context ) {
83
- startService (context , " START_MANUAL_UPLOAD" );
93
+ startService (context , ServiceActionEnum . START_MANUAL_UPLOAD . name () );
84
94
}
85
95
86
96
public static void startShareToSeafileUploadService (Context context ) {
87
- startService (context , " START_SHARE_TO_SEAFILE_UPLOAD" );
97
+ startService (context , ServiceActionEnum . START_SHARE_TO_SEAFILE_UPLOAD . name () );
88
98
}
89
99
90
100
public static void startDownloadService (Context context ) {
91
- startService (context , " START_FILE_DOWNLOAD" );
101
+ startService (context , ServiceActionEnum . START_FILE_DOWNLOAD . name () );
92
102
}
93
103
94
104
public static void startLocalFileUpdateService (Context context ) {
95
- startService (context , " START_LOCAL_FILE_UPDATE" );
105
+ startService (context , ServiceActionEnum . START_LOCAL_FILE_UPDATE . name () );
96
106
}
97
107
98
108
public static void stopTransfer (Context context , TransferModel model ) {
@@ -170,6 +180,9 @@ public IBinder onBind(Intent intent) {
170
180
171
181
@ Override
172
182
public int onStartCommand (Intent intent , int flags , int startId ) {
183
+
184
+ _isRunning .set (true );
185
+
173
186
if (intent == null ) return START_STICKY ;
174
187
175
188
String action = intent .getAction ();
@@ -189,7 +202,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
189
202
stopPhotoBackup ();
190
203
191
204
break ;
192
- case START_FOLDER_BACKUP :
205
+ case RESTART_FOLDER_BACKUP :
193
206
startFolderBackup (intent );
194
207
195
208
break ;
@@ -232,7 +245,7 @@ private void startPhotoBackup(Intent intent) {
232
245
233
246
if (isRestart ) {
234
247
// if running, then restart, if not run, start it.
235
- CompletableFuture <Void > future = _activeTasks .get (FeatureDataSource .ALBUM_BACKUP );
248
+ CompletableFuture <Void > future = getActiveTasks () .get (FeatureDataSource .ALBUM_BACKUP );
236
249
if (future != null && !future .isDone () && mediaBackupUploader != null ) {
237
250
//cancel
238
251
mediaBackupUploader .stop ();
@@ -259,7 +272,7 @@ private void launchAlbumBackup(Bundle extras) {
259
272
private void stopPhotoBackup () {
260
273
startForegroundNotification (FeatureDataSource .ALBUM_BACKUP );
261
274
262
- CompletableFuture <Void > future = _activeTasks .get (FeatureDataSource .ALBUM_BACKUP );
275
+ CompletableFuture <Void > future = getActiveTasks () .get (FeatureDataSource .ALBUM_BACKUP );
263
276
if (future != null && !future .isDone () && mediaBackupUploader != null ) {
264
277
mediaBackupUploader .stop ();
265
278
future .cancel (true );
@@ -283,7 +296,7 @@ private void startFolderBackup(Intent intent) {
283
296
284
297
if (isRestart ) {
285
298
// if running, then restart, if not run, start it.
286
- CompletableFuture <Void > folderUploadFuture = _activeTasks .get (FeatureDataSource .FOLDER_BACKUP );
299
+ CompletableFuture <Void > folderUploadFuture = getActiveTasks () .get (FeatureDataSource .FOLDER_BACKUP );
287
300
if (folderUploadFuture != null && !folderUploadFuture .isDone () && folderBackupUploader != null ) {
288
301
//cancel
289
302
folderBackupUploader .stop ();
@@ -311,7 +324,7 @@ private void launchFolderBackup(Bundle extras) {
311
324
private void stopFolderBackup () {
312
325
startForegroundNotification (FeatureDataSource .FOLDER_BACKUP );
313
326
314
- CompletableFuture <Void > future = _activeTasks .get (FeatureDataSource .FOLDER_BACKUP );
327
+ CompletableFuture <Void > future = getActiveTasks () .get (FeatureDataSource .FOLDER_BACKUP );
315
328
if (future != null && !future .isDone () && folderBackupUploader != null ) {
316
329
folderBackupUploader .stop ();
317
330
@@ -385,7 +398,7 @@ public void stopById(Bundle extras) {
385
398
private void manageTask (FeatureDataSource source , Bundle extras ) {
386
399
startForegroundNotification (source );
387
400
388
- CompletableFuture <Void > existingTask = _activeTasks .get (source );
401
+ CompletableFuture <Void > existingTask = getActiveTasks () .get (source );
389
402
if (existingTask != null && !existingTask .isDone ()) {
390
403
SafeLogs .d (TAG , "manageTask()" , source .name (), "task is already running" );
391
404
return ;
@@ -434,21 +447,21 @@ public void run() {
434
447
mediaBackupUploader = new MediaBackupUploader (getApplicationContext (), transferNotificationDispatcher );
435
448
SeafException uploadSeafException = mediaBackupUploader .upload ();
436
449
if (uploadSeafException != SeafException .SUCCESS ) {
437
- SafeLogs .d (TAG , "runAlbumBackupTask()" , "scan error: " + uploadSeafException );
450
+ SafeLogs .d (TAG , "runAlbumBackupTask()" , "upload error: " + uploadSeafException );
438
451
} else {
439
- SafeLogs .d (TAG , "runAlbumBackupTask()" , "scan success" );
452
+ SafeLogs .d (TAG , "runAlbumBackupTask()" , "upload success" );
440
453
}
441
454
}
442
455
}, new Runnable () {
443
456
@ Override
444
457
public void run () {
445
458
mediaBackupUploader = null ;
446
459
transferNotificationDispatcher .releaseAcquire (FeatureDataSource .ALBUM_BACKUP );
447
- _activeTasks .remove (FeatureDataSource .ALBUM_BACKUP );
460
+ getActiveTasks () .remove (FeatureDataSource .ALBUM_BACKUP );
448
461
}
449
462
});
450
463
451
- _activeTasks .put (FeatureDataSource .ALBUM_BACKUP , completableFuture );
464
+ getActiveTasks () .put (FeatureDataSource .ALBUM_BACKUP , completableFuture );
452
465
453
466
}
454
467
@@ -490,11 +503,11 @@ public void run() {
490
503
public void run () {
491
504
folderBackupUploader = null ;
492
505
transferNotificationDispatcher .releaseAcquire (FeatureDataSource .FOLDER_BACKUP );
493
- _activeTasks .remove (FeatureDataSource .FOLDER_BACKUP );
506
+ getActiveTasks () .remove (FeatureDataSource .FOLDER_BACKUP );
494
507
}
495
508
});
496
509
497
- _activeTasks .put (FeatureDataSource .FOLDER_BACKUP , completableFuture );
510
+ getActiveTasks () .put (FeatureDataSource .FOLDER_BACKUP , completableFuture );
498
511
}
499
512
500
513
private void runFileUploadTask () {
@@ -516,7 +529,7 @@ public void run() {
516
529
517
530
}
518
531
});
519
- _activeTasks .put (FeatureDataSource .MANUAL_FILE_UPLOAD , completableFuture );
532
+ getActiveTasks () .put (FeatureDataSource .MANUAL_FILE_UPLOAD , completableFuture );
520
533
521
534
}
522
535
@@ -538,10 +551,10 @@ public void run() {
538
551
public void run () {
539
552
downloader = null ;
540
553
transferNotificationDispatcher .releaseAcquire (FeatureDataSource .DOWNLOAD );
541
- _activeTasks .remove (FeatureDataSource .DOWNLOAD );
554
+ getActiveTasks () .remove (FeatureDataSource .DOWNLOAD );
542
555
}
543
556
});
544
- _activeTasks .put (FeatureDataSource .DOWNLOAD , completableFuture );
557
+ getActiveTasks () .put (FeatureDataSource .DOWNLOAD , completableFuture );
545
558
}
546
559
547
560
private void runLocalFileUpdateTask () {
@@ -562,10 +575,10 @@ public void run() {
562
575
public void run () {
563
576
localFileUpdater = null ;
564
577
transferNotificationDispatcher .releaseAcquire (FeatureDataSource .AUTO_UPDATE_LOCAL_FILE );
565
- _activeTasks .remove (FeatureDataSource .AUTO_UPDATE_LOCAL_FILE );
578
+ getActiveTasks () .remove (FeatureDataSource .AUTO_UPDATE_LOCAL_FILE );
566
579
}
567
580
});
568
- _activeTasks .put (FeatureDataSource .AUTO_UPDATE_LOCAL_FILE , completableFuture );
581
+ getActiveTasks () .put (FeatureDataSource .AUTO_UPDATE_LOCAL_FILE , completableFuture );
569
582
}
570
583
571
584
private void runShareToSeafileUploadTask () {
@@ -587,11 +600,11 @@ public void run() {
587
600
public void run () {
588
601
shareToSeafileUploader = null ;
589
602
transferNotificationDispatcher .releaseAcquire (FeatureDataSource .SHARE_FILE_TO_SEAFILE );
590
- _activeTasks .remove (FeatureDataSource .SHARE_FILE_TO_SEAFILE );
603
+ getActiveTasks () .remove (FeatureDataSource .SHARE_FILE_TO_SEAFILE );
591
604
}
592
605
});
593
606
594
- _activeTasks .put (FeatureDataSource .SHARE_FILE_TO_SEAFILE , completableFuture );
607
+ getActiveTasks () .put (FeatureDataSource .SHARE_FILE_TO_SEAFILE , completableFuture );
595
608
}
596
609
597
610
private CompletableFuture <Void > runTask (Runnable runnable , Runnable onComplete ) {
@@ -602,7 +615,8 @@ private CompletableFuture<Void> runTask(Runnable runnable, Runnable onComplete)
602
615
@ Override
603
616
public void accept (Void unused , Throwable throwable ) {
604
617
if (onComplete != null ) onComplete .run ();
605
- if (runningTaskCount .decrementAndGet () == 0 ) {
618
+ int currentCount = runningTaskCount .decrementAndGet ();
619
+ if (currentCount == 0 ) {
606
620
stopThisService ();
607
621
SafeLogs .e (TAG + " - all task complete" );
608
622
}
@@ -621,7 +635,7 @@ private void stopThisService() {
621
635
public void onDestroy () {
622
636
SafeLogs .e (TAG , "onDestroy()" );
623
637
624
- _activeTasks .forEach ((key , value ) -> {
638
+ getActiveTasks () .forEach ((key , value ) -> {
625
639
cancel (value );
626
640
});
627
641
@@ -640,6 +654,8 @@ public void onDestroy() {
640
654
}
641
655
}
642
656
657
+ _isRunning .set (false );
658
+
643
659
super .onDestroy ();
644
660
}
645
661
0 commit comments