WatchUpdaterService: process requests in a separate thread.

Previously, requests were executed on the main thread (base class
is Service, not IntentService), which prompted Android to kill
AAPS multiple times a day due to timeouts (of currently unknown
origins).

(cherry picked from commit 5ae45d5)
This commit is contained in:
Johannes Mockenhaupt 2018-03-01 12:25:20 +01:00
parent 75a6366115
commit 8f813d52fa
No known key found for this signature in database
GPG key ID: 9E1EA6AF7BBBB0D1

View file

@ -6,6 +6,8 @@ import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.util.Log;
@ -82,6 +84,7 @@ public class WatchUpdaterService extends WearableListenerService implements
private static Logger log = LoggerFactory.getLogger(WatchUpdaterService.class);
private Handler handler;
@Override
public void onCreate() {
@ -91,6 +94,11 @@ public class WatchUpdaterService extends WearableListenerService implements
if (wear_integration) {
googleApiConnect();
}
if (handler == null) {
HandlerThread handlerThread = new HandlerThread(this.getClass().getSimpleName() + "Handler");
handlerThread.start();
handler = new Handler(handlerThread.getLooper());
}
}
public void listenForChangeInSettings() {
@ -123,12 +131,10 @@ public class WatchUpdaterService extends WearableListenerService implements
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String action = null;
if (intent != null) {
action = intent.getAction();
}
String action = intent != null ? intent.getAction() : null;
if (wear_integration) {
handler.post(() -> {
if (googleApiClient.isConnected()) {
if (ACTION_RESEND.equals(action)) {
resendData();
@ -151,6 +157,7 @@ public class WatchUpdaterService extends WearableListenerService implements
} else {
googleApiClient.connect();
}
});
}
return START_STICKY;