Skip to content

Commit

Permalink
Merge pull request #51 from RuralHunter/master
Browse files Browse the repository at this point in the history
Fix MaintainUserStatusWorker leak
  • Loading branch information
zfdang authored Sep 12, 2023
2 parents 064dffa + a9c8292 commit 38c6913
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
7 changes: 5 additions & 2 deletions app/src/main/java/com/zfdang/zsmth_android/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;
Expand Down Expand Up @@ -177,18 +178,20 @@ public void onBackStackChanged() {
}
});

WorkManager.getInstance(getApplicationContext()).cancelAllWorkByTag(MaintainUserStatusWorker.class.getName());
// setup receiver to receive user status update from periodical background service
setupUserStatusReceiver();

// schedule the periodical background service
Data.Builder inputData = new Data.Builder();
inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true);
WorkRequest userStatusWorkRequest =
OneTimeWorkRequest userStatusWorkRequest =
new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class)
.setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES)
.setInputData(inputData.build())
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest);
WorkManager.getInstance(getApplicationContext())
.enqueueUniqueWork(MaintainUserStatusWorker.WORKER_ID, ExistingWorkPolicy.KEEP,userStatusWorkRequest);

// run the background service now
updateUserStatusNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public class SMTHHelper {
// singleton
private static SMTHHelper instance = null;

public static SMTHHelper getInstance() {
public static synchronized SMTHHelper getInstance() {
if (instance == null) {
instance = new SMTHHelper();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import androidx.annotation.NonNull;
import androidx.work.Data;
import androidx.work.ExistingWorkPolicy;
import androidx.work.OneTimeWorkRequest;
import androidx.work.WorkManager;
import androidx.work.WorkRequest;
Expand All @@ -28,13 +29,16 @@
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.observers.DisposableObserver;
import io.reactivex.schedulers.Schedulers;

public class MaintainUserStatusWorker extends Worker {

public static final String REPEAT = "REPEAT";
private static final String TAG = "MUSWorker";

public static final String WORKER_ID=MaintainUserStatusWorker.class.getName();

public MaintainUserStatusWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}
Expand Down Expand Up @@ -181,11 +185,7 @@ public UserStatus apply(UserStatus userStatus) throws Exception {
}
return userStatus;
}
}).observeOn(Schedulers.io()).subscribeOn(Schedulers.io()).subscribe(new Observer<UserStatus>() {
@Override
public void onSubscribe(Disposable d) {
}

}).subscribe(new DisposableObserver<UserStatus>() {
@Override
public void onNext(UserStatus userStatus) {
//Log.d(TAG, "4.0 onNext: " + userStatus.toString());
Expand Down Expand Up @@ -253,11 +253,11 @@ public String getNotificationMessage(UserStatus userStatus) {
private void enqueueNextWorker() {
Data.Builder inputData = new Data.Builder();
inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true);
WorkRequest userStatusWorkRequest =
OneTimeWorkRequest userStatusWorkRequest =
new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class)
.setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES)
.setInputData(inputData.build())
.build();
WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest);
WorkManager.getInstance(getApplicationContext()).enqueueUniqueWork(WORKER_ID, ExistingWorkPolicy.KEEP,userStatusWorkRequest);
}
}

0 comments on commit 38c6913

Please sign in to comment.