Added error notification

This commit is contained in:
crimean 2018-06-11 15:06:38 +03:00
parent 55aec52fc0
commit 3d8a20ca77
7 changed files with 119 additions and 24 deletions

View file

@ -10,6 +10,7 @@
- For wording and consistency, please note http://osmand.net/help-online?id=technical-articles#Creating_a_Consistent_User_Experience
Thx - Hardy
-->
<string name="error_notification_desc">Please send screenshoot of this notification to support@osmand.net</string>
<string name="quick_action_edit_actions">Edit actions</string>
<string name="get_osmand_live">Get OsmAnd Live to unlock all features: Daily map updates with unlimited downloads, all paid and free plugins, Wikipedia, Wikivoyage and much more.</string>
<string name="unirs_render_descr">Modification of the default style to increase contrast of pedestrian and bicycle roads. Uses legacy Mapnik colors.</string>

View file

@ -46,7 +46,7 @@ public class NavigationService extends Service implements LocationListener {
private static WakeLock lockStatic;
private PendingIntent pendingIntent;
protected int usedBy = 0;
private OsmAndLocationProvider locationProvider;
@ -74,7 +74,7 @@ public class NavigationService extends Service implements LocationListener {
public int getServiceOffInterval() {
return serviceOffInterval;
}
public int getUsedBy() {
return usedBy;
}
@ -165,14 +165,11 @@ public class NavigationService extends Service implements LocationListener {
// registering icon at top level
// Leave icon visible even for navigation for proper display
Notification notification = app.getNotificationHelper().buildTopNotification();
if (notification != null) {
startForeground(OsmandNotification.TOP_NOTIFICATION_SERVICE_ID, notification);
app.getNotificationHelper().refreshNotifications();
}
startForeground(OsmandNotification.TOP_NOTIFICATION_SERVICE_ID, notification);
app.getNotificationHelper().refreshNotifications();
return START_REDELIVER_INTENT;
}
@Override
public void onCreate() {

View file

@ -3,15 +3,19 @@ package net.osmand.plus;
import java.util.ArrayList;
import java.util.List;
import net.osmand.plus.notifications.ErrorNotification;
import net.osmand.plus.notifications.GpxNotification;
import net.osmand.plus.notifications.NavigationNotification;
import net.osmand.plus.notifications.OsmandNotification;
import net.osmand.plus.notifications.OsmandNotification.NotificationType;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationManagerCompat;
@ -22,6 +26,7 @@ public class NotificationHelper {
private NavigationNotification navigationNotification;
private GpxNotification gpxNotification;
private ErrorNotification errorNotification;
private List<OsmandNotification> all = new ArrayList<>();
public NotificationHelper(OsmandApplication app) {
@ -32,29 +37,43 @@ public class NotificationHelper {
private void init() {
navigationNotification = new NavigationNotification(app);
gpxNotification = new GpxNotification(app);
errorNotification = new ErrorNotification(app);
all.add(navigationNotification);
all.add(gpxNotification);
}
@NonNull
public Notification buildTopNotification() {
OsmandNotification notification = acquireTopNotification();
if (notification != null) {
removeNotification(notification.getType());
setTopNotification(notification);
Builder notificationBuilder = notification.buildNotification(false);
return notificationBuilder.build();
if (notificationBuilder != null) {
return notificationBuilder.build();
} else {
return buildErrorNotification();
}
} else {
return buildErrorNotification();
}
return null;
}
private Notification buildErrorNotification() {
removeNotification(errorNotification.getType());
setTopNotification(errorNotification);
return errorNotification.buildNotification(false).build();
}
@Nullable
private OsmandNotification acquireTopNotification() {
OsmandNotification notification = null;
if (navigationNotification.isEnabled()) {
notification = navigationNotification;
return navigationNotification;
} else if (gpxNotification.isEnabled() && gpxNotification.isActive()) {
notification = gpxNotification;
return gpxNotification;
} else {
return null;
}
return notification;
}
public void updateTopNotification() {

View file

@ -0,0 +1,78 @@
package net.osmand.plus.notifications;
import android.support.v4.app.NotificationCompat;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
import net.osmand.plus.monitoring.OsmandMonitoringPlugin;
import net.osmand.plus.routing.RoutingHelper;
public class ErrorNotification extends OsmandNotification {
private final static String GROUP_NAME = "ERROR";
public ErrorNotification(OsmandApplication app) {
super(app, GROUP_NAME);
}
@Override
public void init() {
}
@Override
public NotificationType getType() {
return NotificationType.ERROR;
}
@Override
public int getPriority() {
return NotificationCompat.PRIORITY_DEFAULT;
}
@Override
public boolean isActive() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public NotificationCompat.Builder buildNotification(boolean wearable) {
String notificationTitle;
String notificationText;
icon = R.drawable.ic_action_bug_dark;
notificationTitle = app.getString(R.string.shared_string_unexpected_error);
NavigationService service = app.getNavigationService();
RoutingHelper routingHelper = app.getRoutingHelper();
boolean following = routingHelper.isFollowingMode();
boolean planning = routingHelper.isRoutePlanningMode();
boolean pause = routingHelper.isPauseNavigation();
boolean gpxEnabled = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null;
String usedBy = service != null ? "" + service.getUsedBy() : "X";
notificationText = "Info: " + (following ? "1" : "") + (planning ? "2" : "") + (pause ? "3" : "") + (gpxEnabled ? "4" : "") + "-" + usedBy + ". "
+ app.getString(R.string.error_notification_desc);
return createBuilder(wearable)
.setContentTitle(notificationTitle)
.setStyle(new NotificationCompat.BigTextStyle().bigText(notificationText));
}
@Override
public int getOsmandNotificationId() {
return ERROR_NOTIFICATION_SERVICE_ID;
}
@Override
public int getOsmandWearableNotificationId() {
return WEAR_ERROR_NOTIFICATION_SERVICE_ID;
}
}

View file

@ -5,9 +5,9 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmAndFormatter;

View file

@ -10,11 +10,10 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
import android.view.View;
import net.osmand.plus.NavigationService;

View file

@ -2,28 +2,28 @@ package net.osmand.plus.notifications;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.NotificationCompat;
import net.osmand.plus.NotificationHelper;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.activities.MapActivity;
public abstract class OsmandNotification {
public final static int NAVIGATION_NOTIFICATION_SERVICE_ID = 5;
public final static int GPX_NOTIFICATION_SERVICE_ID = 6;
public final static int ERROR_NOTIFICATION_SERVICE_ID = 7;
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_ERROR_NOTIFICATION_SERVICE_ID = 1007;
protected OsmandApplication app;
protected boolean ongoing = true;
@ -36,7 +36,8 @@ public abstract class OsmandNotification {
public enum NotificationType {
NAVIGATION,
GPX,
GPS
GPS,
ERROR,
}
public OsmandNotification(OsmandApplication app, String groupName) {
@ -69,7 +70,7 @@ public abstract class OsmandNotification {
PendingIntent.FLAG_UPDATE_CURRENT);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
app.getNotificationHelper().createNotificationChannel();
}
}
Builder builder = new Builder(app, NotificationHelper.NOTIFICATION_CHANEL_ID)
.setVisibility(android.support.v4.app.NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(top ? NotificationCompat.PRIORITY_HIGH : getPriority())