diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index a3dc7f5026..39d29fd688 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -286,7 +286,7 @@ public class NavigationService extends Service implements LocationListener { @Override public void onTaskRemoved(Intent rootIntent) { OsmandApplication app = ((OsmandApplication) getApplication()); - app.getNotificationHelper().removeNotifications(); + app.getNotificationHelper().removeNotifications(false); if (app.getNavigationService() != null && app.getSettings().DISABLE_RECORDING_ONCE_APP_KILLED.get()) { NavigationService.this.stopSelf(); diff --git a/OsmAnd/src/net/osmand/plus/NotificationHelper.java b/OsmAnd/src/net/osmand/plus/NotificationHelper.java index d64fbb0e15..df81b6bce8 100644 --- a/OsmAnd/src/net/osmand/plus/NotificationHelper.java +++ b/OsmAnd/src/net/osmand/plus/NotificationHelper.java @@ -155,9 +155,11 @@ public class NotificationHelper { } } - public void removeNotifications() { + public void removeNotifications(boolean inactiveOnly) { for (OsmandNotification notification : all) { - notification.removeNotification(); + if (!inactiveOnly || !notification.isActive()) { + notification.removeNotification(); + } } } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 05b0478cdb..e08c1b1c5c 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -242,7 +242,7 @@ public class OsmandApplication extends MultiDexApplication { if(RateUsBottomSheetDialog.shouldShow(this)) { osmandSettings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); } - getNotificationHelper().removeNotifications(); + getNotificationHelper().removeNotifications(false); } public RendererRegistry getRendererRegistry() { diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index b0cbeaf65b..6b26f88d0c 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -1315,7 +1315,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven @Override protected void onStop() { - getMyApplication().getNotificationHelper().removeNotifications(); + getMyApplication().getNotificationHelper().removeNotifications(true); if(pendingPause) { onPauseActivity(); } @@ -1404,7 +1404,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven } public void updateApplicationModeSettings() { - changeKeyguardFlags(true); + changeKeyguardFlags(); updateMapSettings(); mapViewTrackingUtilities.updateSettings(); //app.getRoutingHelper().setAppMode(settings.getApplicationMode()); @@ -1902,25 +1902,29 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven return oldPoint.getLayerId().equals(layerId) && oldPoint.getId().equals(point.getId()); } - public void changeKeyguardFlags(final boolean enable) { + public void changeKeyguardFlags() { + changeKeyguardFlags(false, true); + } + + private void changeKeyguardFlags(boolean enable, boolean forceKeepScreenOn) { if (enable) { getWindow().setFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED, WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); setKeepScreenOn(true); } else { getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); - setKeepScreenOn(false); + setKeepScreenOn(forceKeepScreenOn); } } @Override public void lock() { - changeKeyguardFlags(false); + changeKeyguardFlags(false, false); } @Override public void unlock() { - changeKeyguardFlags(true); + changeKeyguardFlags(true, false); } private class ScreenOffReceiver extends BroadcastReceiver { @@ -1986,14 +1990,14 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven @Override public void routeWasCancelled() { - changeKeyguardFlags(true); + changeKeyguardFlags(); } @Override public void routeWasFinished() { if (!mIsDestroyed) { DestinationReachedMenu.show(this); - changeKeyguardFlags(true); + changeKeyguardFlags(); } } diff --git a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java index ef094eb8d3..9fd15ee11e 100644 --- a/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java +++ b/OsmAnd/src/net/osmand/plus/helpers/LockHelper.java @@ -31,7 +31,6 @@ public class LockHelper implements SensorEventListener { private OsmandApplication app; private CommonPreference turnScreenOnTime; private CommonPreference turnScreenOnSensor; - private boolean active; @Nullable private LockUIAdapter lockUIAdapter; @@ -102,11 +101,13 @@ public class LockHelper implements SensorEventListener { private void timedUnlock(final long millis) { uiHandler.removeCallbacks(lockRunnable); - if (!active && wakeLock == null) { + if (wakeLock == null) { uiHandler.post(new Runnable() { @Override public void run() { - unlock(); + if (wakeLock == null) { + unlock(); + } } }); } @@ -159,13 +160,11 @@ public class LockHelper implements SensorEventListener { } public void onStart(@NonNull Activity activity) { - active = true; switchSensorOff(); } public void onStop(@NonNull Activity activity) { lock(); - active = false; if (!activity.isFinishing() && isSensorEnabled()) { switchSensorOn(); } diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java index 96b9b398a4..a38c74c579 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java @@ -26,7 +26,6 @@ public abstract class OsmandNotification { public final static int WEAR_ERROR_NOTIFICATION_SERVICE_ID = 1007; public final static int WEAR_DOWNLOAD_NOTIFICATION_SERVICE_ID = 1008; - protected OsmandApplication app; protected boolean ongoing = true; protected int color; @@ -35,6 +34,8 @@ public abstract class OsmandNotification { private String groupName; + private Notification currentNotification; + public enum NotificationType { NAVIGATION, GPX, @@ -125,7 +126,7 @@ public abstract class OsmandNotification { if (isEnabled()) { Builder notificationBuilder = buildNotification(false); if (notificationBuilder != null) { - Notification notification = notificationBuilder.build(); + Notification notification = getNotification(notificationBuilder); setupNotification(notification); notificationManager.notify(top ? TOP_NOTIFICATION_SERVICE_ID : getOsmandNotificationId(), notification); notifyWearable(notificationManager); @@ -140,10 +141,10 @@ public abstract class OsmandNotification { if (isEnabled()) { Builder notificationBuilder = buildNotification(false); if (notificationBuilder != null) { - Notification notification = notificationBuilder.build(); + Notification notification = getNotification(notificationBuilder); setupNotification(notification); if (top) { - notificationManager.cancel(getOsmandNotificationId()); + //notificationManager.cancel(getOsmandNotificationId()); notificationManager.notify(TOP_NOTIFICATION_SERVICE_ID, notification); } else { notificationManager.notify(getOsmandNotificationId(), notification); @@ -159,7 +160,17 @@ public abstract class OsmandNotification { return false; } + private Notification getNotification(Builder notificationBuilder) { + Notification notification = currentNotification; + if (notification == null) { + notification = notificationBuilder.build(); + currentNotification = notification; + } + return notification; + } + public void removeNotification() { + currentNotification = null; NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app); notificationManager.cancel(getOsmandNotificationId()); notificationManager.cancel(getOsmandWearableNotificationId());