Browse Source

Improve WorkManager implementation

Signed-off-by: Mario Danic <mario@lovelyhq.com>
Mario Danic 6 years ago
parent
commit
b55a57ceab

+ 7 - 12
app/src/main/java/com/nextcloud/talk/application/NextcloudTalkApplication.java

@@ -52,16 +52,14 @@ import org.webrtc.voiceengine.WebRtcAudioManager;
 import org.webrtc.voiceengine.WebRtcAudioUtils;
 
 import java.security.GeneralSecurityException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.concurrent.TimeUnit;
 
 import javax.inject.Singleton;
 
+import androidx.work.ExistingPeriodicWorkPolicy;
 import androidx.work.OneTimeWorkRequest;
 import androidx.work.PeriodicWorkRequest;
 import androidx.work.WorkManager;
-import androidx.work.WorkRequest;
 import autodagger.AutoComponent;
 import autodagger.AutoInjector;
 
@@ -143,17 +141,14 @@ public class NextcloudTalkApplication extends MultiDexApplication implements Lif
 
         OneTimeWorkRequest pushRegistrationWork = new OneTimeWorkRequest.Builder(PushRegistrationWorker.class).build();
         OneTimeWorkRequest accountRemovalWork = new OneTimeWorkRequest.Builder(AccountRemovalWorker.class).build();
-        OneTimeWorkRequest capabilitiesUpdateWork = new OneTimeWorkRequest.Builder(CapabilitiesWorker.class).build();
-        PeriodicWorkRequest periodicCapabilitiesWork = new PeriodicWorkRequest.Builder(CapabilitiesWorker.class, 1,
-                        TimeUnit.DAYS).build();
+        PeriodicWorkRequest periodicCapabilitiesUpdateWork = new PeriodicWorkRequest.Builder(CapabilitiesWorker.class,
+                1, TimeUnit.DAYS).build();
 
-        List<WorkRequest> workRequests = new ArrayList<>();
-        workRequests.add(pushRegistrationWork);
-        workRequests.add(accountRemovalWork);
-        workRequests.add(capabilitiesUpdateWork);
-        workRequests.add(periodicCapabilitiesWork);
+        WorkManager.getInstance().enqueue(pushRegistrationWork);
+        WorkManager.getInstance().enqueue(accountRemovalWork);
+        WorkManager.getInstance().enqueueUniquePeriodicWork("DailyCapabilitiesUpdateWork",
+                ExistingPeriodicWorkPolicy.REPLACE, periodicCapabilitiesUpdateWork);
 
-        WorkManager.getInstance().enqueue(workRequests);
     }
 
     @Override

+ 0 - 4
app/src/main/java/com/nextcloud/talk/jobs/AccountRemovalWorker.java

@@ -89,7 +89,6 @@ public class AccountRemovalWorker extends Worker {
 
                     ncApi.unregisterDeviceForNotificationsWithNextcloud(ApiUtils.getCredentials(userEntity.getUsername(),
                             userEntity.getToken()), ApiUtils.getUrlNextcloudPush(userEntity.getBaseUrl()))
-                            .subscribeOn(Schedulers.newThread())
                             .subscribe(new Observer<GenericOverall>() {
                                 @Override
                                 public void onSubscribe(Disposable d) {
@@ -109,7 +108,6 @@ public class AccountRemovalWorker extends Worker {
                                         ncApi.unregisterDeviceForNotificationsWithProxy
                                                 (ApiUtils.getCredentials(userEntity.getUsername(),
                                                         userEntity.getToken()), ApiUtils.getUrlPushProxy(), queryMap)
-                                                .subscribeOn(Schedulers.newThread())
                                                 .subscribe(new Observer<Void>() {
                                                     @Override
                                                     public void onSubscribe(Disposable d) {
@@ -178,7 +176,6 @@ public class AccountRemovalWorker extends Worker {
                             });
                 } else {
                     userUtils.deleteUser(userEntity.getId())
-                            .subscribeOn(Schedulers.newThread())
                             .subscribe(new CompletableObserver() {
                                 @Override
                                 public void onSubscribe(Disposable d) {
@@ -199,7 +196,6 @@ public class AccountRemovalWorker extends Worker {
             } catch (IOException e) {
                 Log.d(TAG, "Something went wrong while removing job at parsing PushConfigurationState");
                 userUtils.deleteUser(userEntity.getId())
-                        .subscribeOn(Schedulers.newThread())
                         .subscribe(new CompletableObserver() {
                             @Override
                             public void onSubscribe(Disposable d) {

+ 0 - 1
app/src/main/java/com/nextcloud/talk/jobs/CapabilitiesWorker.java

@@ -132,7 +132,6 @@ public class CapabilitiesWorker extends Worker {
             ncApi.getCapabilities(ApiUtils.getCredentials(internalUserEntity.getUsername(),
                     internalUserEntity.getToken()), ApiUtils.getUrlForCapabilities(internalUserEntity.getBaseUrl()))
                     .retry(3)
-                    .subscribeOn(Schedulers.newThread())
                     .subscribe(new Observer<CapabilitiesOverall>() {
                         @Override
                         public void onSubscribe(Disposable d) {

+ 0 - 3
app/src/main/java/com/nextcloud/talk/jobs/NotificationWorker.java

@@ -82,7 +82,6 @@ import androidx.work.Worker;
 import autodagger.AutoInjector;
 import io.reactivex.Observer;
 import io.reactivex.disposables.Disposable;
-import io.reactivex.schedulers.Schedulers;
 
 @AutoInjector(NextcloudTalkApplication.class)
 public class NotificationWorker extends Worker {
@@ -107,7 +106,6 @@ public class NotificationWorker extends Worker {
         ncApi.getRoom(ApiUtils.getCredentials(userEntity.getUserId(),
                 userEntity.getToken()), ApiUtils.getRoom(userEntity.getBaseUrl(),
                 intent.getExtras().getString(BundleKeys.KEY_ROOM_TOKEN)))
-                .subscribeOn(Schedulers.newThread())
                 .subscribe(new Observer<RoomOverall>() {
                     @Override
                     public void onSubscribe(Disposable d) {
@@ -147,7 +145,6 @@ public class NotificationWorker extends Worker {
         ncApi.getNotification(ApiUtils.getCredentials(userEntity.getUserId(),
                 userEntity.getToken()), ApiUtils.getUrlForNotificationWithId(userEntity.getBaseUrl(),
                 Long.toString(decryptedPushMessage.getNotificationId())))
-                .subscribeOn(Schedulers.newThread())
                 .subscribe(new Observer<NotificationOverall>() {
                     @Override
                     public void onSubscribe(Disposable d) {