From 76a163fa8f49ce3eb5716461cdc2235a1089e41f Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Fri, 25 Nov 2016 19:19:08 +0300 Subject: [PATCH] Fix notifications for android wear --- .../net/osmand/plus/NotificationHelper.java | 2 +- .../notifications/GpsWakeUpNotification.java | 14 +++- .../plus/notifications/GpxNotification.java | 16 ++-- .../notifications/NavigationNotification.java | 14 +++- .../plus/notifications/OsMoNotification.java | 14 +++- .../notifications/OsmandNotification.java | 77 +++++++++++++------ 6 files changed, 95 insertions(+), 42 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/NotificationHelper.java b/OsmAnd/src/net/osmand/plus/NotificationHelper.java index 73a1e2a1ab..639d7fe99e 100644 --- a/OsmAnd/src/net/osmand/plus/NotificationHelper.java +++ b/OsmAnd/src/net/osmand/plus/NotificationHelper.java @@ -44,7 +44,7 @@ public class NotificationHelper { if (notification != null) { removeNotification(notification.getType()); setTopNotification(notification); - Builder notificationBuilder = notification.buildNotification(); + Builder notificationBuilder = notification.buildNotification(false); return notificationBuilder.build(); } return null; diff --git a/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java b/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java index 0d032e844a..f79f55c4ae 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java @@ -19,9 +19,10 @@ import static net.osmand.plus.NavigationService.USED_BY_WAKE_UP; public class GpsWakeUpNotification extends OsmandNotification { public final static String OSMAND_STOP_GPS_WAKE_UP_SERVICE_ACTION = "OSMAND_STOP_GPS_WAKE_UP_SERVICE_ACTION"; + public final static String GROUP_NAME = "GPS_WAKE_UP"; public GpsWakeUpNotification(OsmandApplication app) { - super(app); + super(app, GROUP_NAME); } @Override @@ -58,7 +59,7 @@ public class GpsWakeUpNotification extends OsmandNotification { } @Override - public Builder buildNotification() { + public Builder buildNotification(boolean wearable) { NavigationService service = app.getNavigationService(); String notificationTitle; String notificationText; @@ -80,7 +81,7 @@ public class GpsWakeUpNotification extends OsmandNotification { return null; } - final Builder notificationBuilder = createBuilder() + final Builder notificationBuilder = createBuilder(wearable) .setContentTitle(notificationTitle) .setStyle(new BigTextStyle().bigText(notificationText)); @@ -94,7 +95,12 @@ public class GpsWakeUpNotification extends OsmandNotification { } @Override - public int getUniqueId() { + public int getOsmandNotificationId() { return GPS_WAKE_UP_NOTIFICATION_SERVICE_ID; } + + @Override + public int getOsmandWearableNotificationId() { + return WEAR_GPS_WAKE_UP_NOTIFICATION_SERVICE_ID; + } } diff --git a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java index a80881ca16..aa240630f0 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java @@ -14,23 +14,22 @@ import net.osmand.plus.OsmAndFormatter; import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.R; -import net.osmand.plus.activities.MapActivity; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; import net.osmand.util.Algorithms; import static net.osmand.plus.NavigationService.USED_BY_GPX; -import static net.osmand.plus.NavigationService.USED_BY_NAVIGATION; public class GpxNotification extends OsmandNotification { public final static String OSMAND_SAVE_GPX_SERVICE_ACTION = "OSMAND_SAVE_GPX_SERVICE_ACTION"; public final static String OSMAND_START_GPX_SERVICE_ACTION = "OSMAND_START_GPX_SERVICE_ACTION"; public final static String OSMAND_STOP_GPX_SERVICE_ACTION = "OSMAND_STOP_GPX_SERVICE_ACTION"; + public final static String GROUP_NAME = "GPX"; private boolean wasDismissed; public GpxNotification(OsmandApplication app) { - super(app); + super(app, GROUP_NAME); } @Override @@ -100,7 +99,7 @@ public class GpxNotification extends OsmandNotification { } @Override - public Builder buildNotification() { + public Builder buildNotification(boolean wearable) { if (!isEnabled()) { return null; } @@ -134,7 +133,7 @@ public class GpxNotification extends OsmandNotification { return null; } - final Builder notificationBuilder = createBuilder() + final Builder notificationBuilder = createBuilder(wearable) .setContentTitle(notificationTitle) .setStyle(new BigTextStyle().bigText(notificationText)); @@ -173,7 +172,12 @@ public class GpxNotification extends OsmandNotification { } @Override - public int getUniqueId() { + public int getOsmandNotificationId() { return GPX_NOTIFICATION_SERVICE_ID; } + + @Override + public int getOsmandWearableNotificationId() { + return WEAR_GPX_NOTIFICATION_SERVICE_ID; + } } diff --git a/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java index 6156b01537..1c1985f372 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java @@ -44,13 +44,14 @@ public class NavigationNotification extends OsmandNotification { public final static String OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION = "OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION"; public final static String OSMAND_RESUME_NAVIGATION_SERVICE_ACTION = "OSMAND_RESUME_NAVIGATION_SERVICE_ACTION"; public final static String OSMAND_STOP_NAVIGATION_SERVICE_ACTION = "OSMAND_STOP_NAVIGATION_SERVICE_ACTION"; + public final static String GROUP_NAME = "NAVIGATION"; private Map bitmapCache = new HashMap<>(); private Bitmap turnBitmap; private boolean leftSide; public NavigationNotification(OsmandApplication app) { - super(app); + super(app, GROUP_NAME); } @Override @@ -108,7 +109,7 @@ public class NavigationNotification extends OsmandNotification { } @Override - public Builder buildNotification() { + public Builder buildNotification(boolean wearable) { NavigationService service = app.getNavigationService(); String notificationTitle; StringBuilder notificationText = new StringBuilder(); @@ -200,7 +201,7 @@ public class NavigationNotification extends OsmandNotification { return null; } - final Builder notificationBuilder = createBuilder() + final Builder notificationBuilder = createBuilder(wearable) .setContentTitle(notificationTitle) .setStyle(new BigTextStyle().bigText(notificationText)) .setLargeIcon(turnBitmap); @@ -260,7 +261,12 @@ public class NavigationNotification extends OsmandNotification { } @Override - public int getUniqueId() { + public int getOsmandNotificationId() { return NAVIGATION_NOTIFICATION_SERVICE_ID; } + + @Override + public int getOsmandWearableNotificationId() { + return WEAR_NAVIGATION_NOTIFICATION_SERVICE_ID; + } } diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java index affec69371..0b93bda9b1 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java @@ -22,9 +22,10 @@ public class OsMoNotification extends OsmandNotification { public final static String OSMAND_STOP_OSMO_SERVICE_ACTION = "OSMAND_STOP_OSMO_SERVICE_ACTION"; public final static String OSMAND_START_SHARE_LOCATION_ACTION = "OSMAND_START_SHARE_LOCATION_ACTION"; public final static String OSMAND_STOP_SHARE_LOCATION_ACTION = "OSMAND_STOP_SHARE_LOCATION_ACTION"; + public final static String GROUP_NAME = "OSMO"; public OsMoNotification(OsmandApplication app) { - super(app); + super(app, GROUP_NAME); } @Override @@ -115,7 +116,7 @@ public class OsMoNotification extends OsmandNotification { } @Override - public Builder buildNotification() { + public Builder buildNotification(boolean wearable) { OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class); if (osMoPlugin == null) { return null; @@ -130,7 +131,7 @@ public class OsMoNotification extends OsmandNotification { notificationTitle = app.getString(R.string.osmo_service_running); notificationText = app.getString(R.string.osmo_share_my_location) + ": " + (shareLocation ? app.getString(R.string.shared_string_yes) : app.getString(R.string.shared_string_no)).toLowerCase(); - final Builder notificationBuilder = createBuilder() + final Builder notificationBuilder = createBuilder(wearable) .setContentTitle(notificationTitle) .setContentText(notificationText); @@ -169,7 +170,12 @@ public class OsMoNotification extends OsmandNotification { @Override - public int getUniqueId() { + public int getOsmandNotificationId() { return OSMO_NOTIFICATION_SERVICE_ID; } + + @Override + public int getOsmandWearableNotificationId() { + return WEAR_OSMO_NOTIFICATION_SERVICE_ID; + } } diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java index 9cf82f0de7..ccee708d45 100644 --- a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java +++ b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java @@ -1,12 +1,11 @@ package net.osmand.plus.notifications; import android.app.Notification; -import android.app.NotificationManager; import android.app.PendingIntent; -import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.support.v4.app.NotificationCompat.Builder; +import android.support.v4.app.NotificationManagerCompat; import android.support.v7.app.NotificationCompat; import net.osmand.plus.OsmandApplication; @@ -20,12 +19,19 @@ public abstract class OsmandNotification { public final static int GPS_WAKE_UP_NOTIFICATION_SERVICE_ID = 8; public final static int TOP_NOTIFICATION_SERVICE_ID = 100; + public final static int WEAR_NAVIGATION_NOTIFICATION_SERVICE_ID = 1005; + public final static int WEAR_GPX_NOTIFICATION_SERVICE_ID = 1006; + public final static int WEAR_OSMO_NOTIFICATION_SERVICE_ID = 1007; + public final static int WEAR_GPS_WAKE_UP_NOTIFICATION_SERVICE_ID = 1008; + protected OsmandApplication app; protected boolean ongoing = true; protected int color; protected int icon; protected boolean top; + private String groupName; + public enum NotificationType { NAVIGATION, GPX, @@ -33,14 +39,19 @@ public abstract class OsmandNotification { GPS } - public OsmandNotification(OsmandApplication app) { + public OsmandNotification(OsmandApplication app, String groupName) { this.app = app; + this.groupName = groupName; init(); } public void init() { } + public String getGroupName() { + return groupName; + } + public abstract NotificationType getType(); public boolean isTop() { @@ -51,17 +62,20 @@ public abstract class OsmandNotification { this.top = top; } - protected Builder createBuilder() { + protected Builder createBuilder(boolean wearable) { Intent contentIntent = new Intent(app, MapActivity.class); PendingIntent contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); Builder builder = new Builder(app) - .setVisibility(android.support.v7.app.NotificationCompat.VISIBILITY_PUBLIC) + .setVisibility(android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC) .setPriority(top ? NotificationCompat.PRIORITY_HIGH : getPriority()) - .setOngoing(ongoing) + .setOngoing(ongoing && !wearable) .setContentIntent(contentPendingIntent) .setDeleteIntent(NotificationDismissReceiver.createIntent(app, getType())); + if (top) { + builder.setGroup(groupName).setGroupSummary(!wearable); + } if (color != 0) { builder.setColor(color); @@ -73,9 +87,11 @@ public abstract class OsmandNotification { return builder; } - public abstract Builder buildNotification(); + public abstract Builder buildNotification(boolean wearable); - public abstract int getUniqueId(); + public abstract int getOsmandNotificationId(); + + public abstract int getOsmandWearableNotificationId(); public abstract int getPriority(); @@ -90,13 +106,20 @@ public abstract class OsmandNotification { } public boolean showNotification() { - NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app); if (isEnabled()) { - Builder newNotification = buildNotification(); - if (newNotification != null) { - Notification notification = newNotification.build(); + Builder notificationBuilder = buildNotification(false); + if (notificationBuilder != null) { + Notification notification = notificationBuilder.build(); setupNotification(notification); - notificationManager.notify(top ? TOP_NOTIFICATION_SERVICE_ID : getUniqueId(), notification); + notificationManager.notify(top ? TOP_NOTIFICATION_SERVICE_ID : getOsmandNotificationId(), notification); + if (top) { + Builder wearNotificationBuilder = buildNotification(true); + if (wearNotificationBuilder != null) { + Notification wearNotification = wearNotificationBuilder.build(); + notificationManager.notify(getOsmandWearableNotificationId(), wearNotification); + } + } return true; } } @@ -104,31 +127,39 @@ public abstract class OsmandNotification { } public boolean refreshNotification() { - NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app); if (isEnabled()) { - Builder newNotification = buildNotification(); - if (newNotification != null) { - Notification notification = newNotification.build(); + Builder notificationBuilder = buildNotification(false); + if (notificationBuilder != null) { + Notification notification = notificationBuilder.build(); setupNotification(notification); if (top) { - notificationManager.cancel(getUniqueId()); + notificationManager.cancel(getOsmandNotificationId()); notificationManager.notify(TOP_NOTIFICATION_SERVICE_ID, notification); + + Builder wearNotificationBuilder = buildNotification(true); + if (wearNotificationBuilder != null) { + Notification wearNotification = wearNotificationBuilder.build(); + notificationManager.notify(getOsmandWearableNotificationId(), wearNotification); + } + } else { - notificationManager.notify(getUniqueId(), notification); + notificationManager.notify(getOsmandNotificationId(), notification); } return true; } else { - notificationManager.cancel(getUniqueId()); + notificationManager.cancel(getOsmandNotificationId()); } } else { - notificationManager.cancel(getUniqueId()); + notificationManager.cancel(getOsmandNotificationId()); } return false; } public void removeNotification() { - NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); - notificationManager.cancel(getUniqueId()); + NotificationManagerCompat notificationManager = NotificationManagerCompat.from(app); + notificationManager.cancel(getOsmandNotificationId()); + notificationManager.cancel(getOsmandWearableNotificationId()); } public void closeSystemDialogs(Context context) {