mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-12-25 18:15:49 +01:00
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.
This commit is contained in:
parent
9decb7788b
commit
18157daf46
@ -251,6 +251,7 @@
|
||||
<action android:name="android.service.notification.NotificationListenerService" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
<service android:name=".service.NotificationCollectorMonitorService" />
|
||||
<service android:name=".service.DeviceCommunicationService" />
|
||||
|
||||
<receiver
|
||||
|
@ -53,6 +53,7 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
||||
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
|
||||
import nodomain.freeyourgadget.gadgetbridge.model.DeviceService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.service.NotificationCollectorMonitorService;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.FileUtils;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||
import nodomain.freeyourgadget.gadgetbridge.util.GBPrefs;
|
||||
@ -146,6 +147,8 @@ public class GBApplication extends Application {
|
||||
|
||||
if (isRunningMarshmallowOrLater()) {
|
||||
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
||||
//the following will ensure the notification manager is kept alive
|
||||
startService(new Intent(this, NotificationCollectorMonitorService.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,79 @@
|
||||
package nodomain.freeyourgadget.gadgetbridge.service;
|
||||
|
||||
import android.app.ActivityManager;
|
||||
import android.app.Service;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.IBinder;
|
||||
import android.os.Process;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import nodomain.freeyourgadget.gadgetbridge.externalevents.NotificationListener;
|
||||
|
||||
/**
|
||||
* Original source by xinghui - see https://gist.github.com/xinghui/b2ddd8cffe55c4b62f5d8846d5545bf9
|
||||
* NB: no license specified in the source code as of 2017-04-19
|
||||
*/
|
||||
public class NotificationCollectorMonitorService extends Service {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(NotificationCollectorMonitorService.class);
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
ensureCollectorRunning();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
private void ensureCollectorRunning() {
|
||||
ComponentName collectorComponent = new ComponentName(this, NotificationListener.class);
|
||||
LOG.info("ensureCollectorRunning collectorComponent: " + collectorComponent);
|
||||
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
|
||||
boolean collectorRunning = false;
|
||||
List<ActivityManager.RunningServiceInfo> 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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user