Skip to content

Commit d8e49b8

Browse files
committedNov 11, 2024
Make sync service as foreground to prevent from killing
1 parent eda5776 commit d8e49b8

File tree

5 files changed

+62
-42
lines changed

5 files changed

+62
-42
lines changed
 

‎app/build.gradle

+3-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ repositories {
5050

5151
dependencies {
5252
implementation fileTree(dir: 'libs', include: ['*.jar'])
53-
implementation 'androidx.appcompat:appcompat:1.6.1'
54-
implementation 'androidx.webkit:webkit:1.10.0'
55-
def lifecycle_version = "2.7.0"
53+
implementation 'androidx.appcompat:appcompat:1.7.0'
54+
implementation 'androidx.webkit:webkit:1.12.1'
55+
def lifecycle_version = "2.8.7"
5656
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
5757
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
5858
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"

‎app/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
1010
<uses-permission android:name="android.permission.RECORD_AUDIO" />
1111
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
12+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
13+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
1214

1315
<queries>
1416
<package android:name="com.nextcloud.client" />
@@ -31,6 +33,7 @@
3133

3234
<service
3335
android:name=".SyncService"
36+
android:foregroundServiceType="dataSync"
3437
android:exported="false" />
3538

3639
<provider
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,32 @@
11
package fr.nuage.souvenirs;
22

3+
import static android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC;
4+
35
import android.app.IntentService;
6+
import android.app.Service;
47
import android.content.Context;
58
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;
615

716
import java.util.UUID;
817

918
import fr.nuage.souvenirs.viewmodel.AlbumListViewModelFactory;
1019
import fr.nuage.souvenirs.viewmodel.AlbumViewModel;
1120
import fr.nuage.souvenirs.viewmodel.SyncToNextcloudAsyncTask;
1221

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 {
2023

2124
// IntentService can perform
2225
private static final String ACTION_SYNC = "fr.nuage.souvenirs.action.SYNC";
2326

2427
private static final String EXTRA_PARAM_ALBUMID = "fr.nuage.souvenirs.extra.PARAM_ALBUMID";
2528

26-
public SyncService() {
27-
super("SyncService");
28-
}
2929

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-
*/
3630
public static void startSync(Context context, AlbumViewModel albumViewModel) {
3731
Intent intent = new Intent(context, SyncService.class);
3832
intent.setAction(ACTION_SYNC);
@@ -41,28 +35,40 @@ public static void startSync(Context context, AlbumViewModel albumViewModel) {
4135
}
4236

4337
@Override
44-
protected void onHandleIntent(Intent intent) {
38+
public int onStartCommand (Intent intent,
39+
int flags,
40+
int startId) {
4541
if (intent != null) {
4642
final String action = intent.getAction();
4743
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+
}
5063
}
5164
}
65+
return START_NOT_STICKY;
5266
}
5367

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-
}
6768

69+
@Nullable
70+
@Override
71+
public IBinder onBind(Intent intent) {
72+
return null;
73+
}
6874
}

‎app/src/main/java/fr/nuage/souvenirs/viewmodel/AlbumListViewModel.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ private void updateAlbumList() {
9999
for (Album a: albums.getAlbumList()) {
100100
//check if exists in list
101101
boolean albumExists = false;
102-
for (AlbumViewModel avm: albumViewModels) {
102+
for(int i = 0; i<albumViewModels.size(); i++) {
103+
AlbumViewModel avm = albumViewModels.get(i);
103104
if (a.getId().equals(avm.getId())) {
104105
albumExists = true;
105106
//check if album is present in VM
@@ -147,7 +148,7 @@ private void updateAlbumList() {
147148
}
148149
if (albumsNC != null) {
149150
if ((avm.getAlbumNC()!=null) && (!albumsNC.isInAlbumList(avm.getAlbumNC().getId()))) {
150-
albumViewModels.get(i).setAlbum(null);
151+
albumViewModels.get(i).setAlbumNC(null);
151152
}
152153
}
153154
if ((avm.getAlbum() == null) && (avm.getAlbumNC() == null)) {

‎app/src/main/java/fr/nuage/souvenirs/viewmodel/SyncToNextcloudAsyncTask.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import android.Manifest;
44
import android.app.Activity;
55
import android.app.Application;
6+
import android.app.Notification;
67
import android.content.Context;
78
import android.content.pm.PackageManager;
89
import android.os.AsyncTask;
@@ -51,6 +52,7 @@ public SyncToNextcloudAsyncTask(Context context, AlbumViewModel album) {
5152
} else {
5253
album.setSyncInProgress(true);
5354
}
55+
nBuilder = createNotification();
5456
}
5557

5658
@Override
@@ -311,15 +313,23 @@ protected void onProgressUpdate(Integer... progress) {
311313
protected void onPreExecute() {
312314
//show progress bar in notification
313315
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
314-
nBuilder = new NotificationCompat.Builder(context, AlbumListActivity.CHANNEL_ID);
315-
nBuilder.setContentTitle(context.getString(R.string.sync_to_nextcloud,albumVM.getName().getValue()))
316+
nBuilder.setProgress(1, 0, true);
317+
notificationManager.notify(notificationId, nBuilder.build());
318+
Log.i(getClass().getName(),context.getString(R.string.sync_to_nextcloud,albumVM.getName().getValue()));
319+
}
320+
321+
private NotificationCompat.Builder createNotification() {
322+
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, AlbumListActivity.CHANNEL_ID);
323+
builder.setContentTitle(context.getString(R.string.sync_to_nextcloud,albumVM.getName().getValue()))
316324
.setPriority(NotificationCompat.PRIORITY_LOW)
317325
.setOngoing(true)
318326
.setOnlyAlertOnce(true)
319327
.setSmallIcon(R.drawable.ic_sync_black_24dp);
320-
nBuilder.setProgress(1, 0, true);
321-
notificationManager.notify(notificationId, nBuilder.build());
322-
Log.i(getClass().getName(),context.getString(R.string.sync_to_nextcloud,albumVM.getName().getValue()));
328+
return builder;
329+
}
330+
331+
public Notification getNotification() {
332+
return nBuilder.build();
323333
}
324334

325335
@Override

0 commit comments

Comments
 (0)
Failed to load comments.