1
1
package fr .nuage .souvenirs ;
2
2
3
+ import static android .content .pm .ServiceInfo .FOREGROUND_SERVICE_TYPE_DATA_SYNC ;
4
+
3
5
import android .app .IntentService ;
6
+ import android .app .Service ;
4
7
import android .content .Context ;
5
8
import android .content .Intent ;
9
+ import android .content .pm .ServiceInfo ;
10
+ import android .os .Build ;
11
+ import android .os .IBinder ;
12
+
13
+ import androidx .annotation .Nullable ;
14
+ import androidx .core .app .ServiceCompat ;
6
15
7
16
import java .util .UUID ;
8
17
9
18
import fr .nuage .souvenirs .viewmodel .AlbumListViewModelFactory ;
10
19
import fr .nuage .souvenirs .viewmodel .AlbumViewModel ;
11
20
import fr .nuage .souvenirs .viewmodel .SyncToNextcloudAsyncTask ;
12
21
13
- /**
14
- * An {@link IntentService} subclass for handling asynchronous task requests in
15
- * a service on a separate handler thread.
16
- * <p>
17
- * helper methods.
18
- */
19
- public class SyncService extends IntentService {
22
+ public class SyncService extends Service {
20
23
21
24
// IntentService can perform
22
25
private static final String ACTION_SYNC = "fr.nuage.souvenirs.action.SYNC" ;
23
26
24
27
private static final String EXTRA_PARAM_ALBUMID = "fr.nuage.souvenirs.extra.PARAM_ALBUMID" ;
25
28
26
- public SyncService () {
27
- super ("SyncService" );
28
- }
29
29
30
- /**
31
- * Starts this service to perform action sync with the given parameters. If
32
- * the service is already performing a task this action will be queued.
33
- *
34
- * @see IntentService
35
- */
36
30
public static void startSync (Context context , AlbumViewModel albumViewModel ) {
37
31
Intent intent = new Intent (context , SyncService .class );
38
32
intent .setAction (ACTION_SYNC );
@@ -41,28 +35,40 @@ public static void startSync(Context context, AlbumViewModel albumViewModel) {
41
35
}
42
36
43
37
@ Override
44
- protected void onHandleIntent (Intent intent ) {
38
+ public int onStartCommand (Intent intent ,
39
+ int flags ,
40
+ int startId ) {
45
41
if (intent != null ) {
46
42
final String action = intent .getAction ();
47
43
if (ACTION_SYNC .equals (action )) {
48
- final String albumId = intent .getStringExtra (EXTRA_PARAM_ALBUMID );
49
- handleActionSync (albumId );
44
+ //check id
45
+ UUID id = UUID .fromString (intent .getStringExtra (EXTRA_PARAM_ALBUMID ));
46
+ if (id != null ) {
47
+ AlbumViewModel albumViewModel = AlbumListViewModelFactory .getAlbumListViewModel ().getAlbum (id );
48
+ SyncToNextcloudAsyncTask task = new SyncToNextcloudAsyncTask (getApplication ().getApplicationContext (),albumViewModel );
49
+ //make forground
50
+ int type = 0 ;
51
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .R ) {
52
+ type = ServiceInfo .FOREGROUND_SERVICE_TYPE_DATA_SYNC ;
53
+ }
54
+ ServiceCompat .startForeground (
55
+ this ,
56
+ 100 ,
57
+ task .getNotification (),
58
+ type
59
+ );
60
+ //start sync to nextcloud task
61
+ task .execute ();
62
+ }
50
63
}
51
64
}
65
+ return START_NOT_STICKY ;
52
66
}
53
67
54
- /**
55
- * Handle action Sync in the provided background thread with the provided
56
- * parameters.
57
- */
58
- private void handleActionSync (String albumId ) {
59
- UUID id = UUID .fromString (albumId );
60
- if (id != null ) {
61
- AlbumViewModel albumViewModel = AlbumListViewModelFactory .getAlbumListViewModel ().getAlbum (id );
62
- //start sync to nextcloud task
63
- SyncToNextcloudAsyncTask task = new SyncToNextcloudAsyncTask (getApplication ().getApplicationContext (),albumViewModel );
64
- task .execute ();
65
- }
66
- }
67
68
69
+ @ Nullable
70
+ @ Override
71
+ public IBinder onBind (Intent intent ) {
72
+ return null ;
73
+ }
68
74
}
0 commit comments