From 18157daf468a7203e88b86da068abc7a9bc8c86e Mon Sep 17 00:00:00 2001 From: Daniele Gobbetti Date: Wed, 19 Apr 2017 13:23:13 +0200 Subject: [PATCH] Ensure that the Notification listener service gets restarted if crashed. This change adds an additional service that checks the status of the NotificationListenerService, and restarts it if it's stale/crashed. Crashes happen mostly during development, but were reported also by users. --- app/src/main/AndroidManifest.xml | 1 + .../gadgetbridge/GBApplication.java | 3 + .../NotificationCollectorMonitorService.java | 79 +++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/NotificationCollectorMonitorService.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b15323fc..140de6d7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -251,6 +251,7 @@ + runningServices = manager.getRunningServices(Integer.MAX_VALUE); + if (runningServices == null) { + LOG.info("ensureCollectorRunning() runningServices is NULL"); + return; + } + for (ActivityManager.RunningServiceInfo service : runningServices) { + if (service.service.equals(collectorComponent)) { + LOG.warn("ensureCollectorRunning service - pid: " + service.pid + ", currentPID: " + Process.myPid() + ", clientPackage: " + service.clientPackage + ", clientCount: " + service.clientCount + + ", clientLabel: " + ((service.clientLabel == 0) ? "0" : "(" + getResources().getString(service.clientLabel) + ")")); + if (service.pid == Process.myPid() /*&& service.clientCount > 0 && !TextUtils.isEmpty(service.clientPackage)*/) { + collectorRunning = true; + } + } + } + if (collectorRunning) { + LOG.debug("ensureCollectorRunning: collector is running"); + return; + } + LOG.debug("ensureCollectorRunning: collector not running, reviving..."); + toggleNotificationListenerService(); + } + + private void toggleNotificationListenerService() { + LOG.debug("toggleNotificationListenerService() called"); + ComponentName thisComponent = new ComponentName(this, NotificationListener.class); + PackageManager pm = getPackageManager(); + pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); + pm.setComponentEnabledSetting(thisComponent, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); + + } + + @Override + public IBinder onBind(Intent intent) { + return null; + } +} \ No newline at end of file