From 9e303fe0925a85cf06694412637d3140f6d8a6c4 Mon Sep 17 00:00:00 2001 From: vincent Bourgmayer Date: Thu, 13 Oct 2022 17:22:13 +0200 Subject: [PATCH] Execute RemoteOperation on ThreadHandler instead of main thread: Replace Handler by ThreadHandler in ObserverService & SynchronizationService --- .../e/drive/services/ObserverService.java | 10 +++++++++- .../e/drive/services/SynchronizationService.java | 14 ++++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/services/ObserverService.java b/app/src/main/java/foundation/e/drive/services/ObserverService.java index 99b718a3..7d6a2239 100644 --- a/app/src/main/java/foundation/e/drive/services/ObserverService.java +++ b/app/src/main/java/foundation/e/drive/services/ObserverService.java @@ -20,6 +20,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageInfo; import android.os.Handler; +import android.os.HandlerThread; import android.os.IBinder; import androidx.annotation.Nullable; @@ -72,12 +73,15 @@ public class ObserverService extends Service implements OnRemoteOperationListene private Account mAccount; private HashMap syncRequests; //integer is SyncedFileState id; Parcelable is the operation private SynchronizationServiceConnection synchronizationServiceConnection = new SynchronizationServiceConnection(); + private Handler handler; + private HandlerThread handlerThread; /* Lifecycle Methods */ @Override public void onDestroy(){ Timber.v("onDestroy()"); unbindService(synchronizationServiceConnection); + handlerThread.quitSafely(); super.onDestroy(); this.mSyncedFolders = null; } @@ -110,6 +114,10 @@ public class ObserverService extends Service implements OnRemoteOperationListene this.syncRequests = new HashMap<>(); initialFolderCounter = prefs.getInt(AppConstants.INITIALFOLDERS_NUMBER, 0); + + handlerThread = new HandlerThread("syncService_onResponse"); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); begin(); return START_NOT_STICKY; } @@ -256,7 +264,7 @@ public class ObserverService extends Service implements OnRemoteOperationListene try { final ListFileRemoteOperation loadOperation = new ListFileRemoteOperation(this.mSyncedFolders, this, this.initialFolderCounter); - loadOperation.execute(client, this, new Handler()); + loadOperation.execute(client, this, handler); } catch (IllegalArgumentException exception) { Timber.e(exception); } diff --git a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java index 7b5ceb86..678e462e 100644 --- a/app/src/main/java/foundation/e/drive/services/SynchronizationService.java +++ b/app/src/main/java/foundation/e/drive/services/SynchronizationService.java @@ -17,6 +17,7 @@ import android.content.Intent; import android.content.SharedPreferences; import android.os.Binder; import android.os.Handler; +import android.os.HandlerThread; import android.os.IBinder; import androidx.annotation.Nullable; @@ -60,7 +61,7 @@ public class SynchronizationService extends Service implements OnRemoteOperation private OwnCloudClient ocClient; private NextcloudClient ncClient; private Handler handler; - + private HandlerThread handlerThread; @Override public void onCreate() { super.onCreate(); @@ -87,11 +88,20 @@ public class SynchronizationService extends Service implements OnRemoteOperation threadPool = new Thread[workerAmount]; ocClient = DavClientProvider.getInstance().getClientInstance(account, getApplicationContext()); ncClient = DavClientProvider.getInstance().getNcClientInstance(account, getApplicationContext()); - handler = new Handler(); + + handlerThread = new HandlerThread("syncService_onResponse"); + handlerThread.start(); + handler = new Handler(handlerThread.getLooper()); return START_REDELIVER_INTENT; } + @Override + public void onDestroy() { + handlerThread.quitSafely(); + super.onDestroy(); + } + @Nullable @Override public IBinder onBind(Intent intent) { -- GitLab