Fix notifications for android wear

This commit is contained in:
Alexey Kulish 2016-11-25 19:19:08 +03:00
parent 76736312db
commit 28fd5b1400
6 changed files with 95 additions and 42 deletions

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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<TurnPathHelper.TurnResource, Bitmap> 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;
}
}

View file

@ -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;
}
}

View file

@ -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) {