From 2d5e5a8e1afe8947d40b9c6537623c20ea1e81b3 Mon Sep 17 00:00:00 2001 From: Alexey Kulish Date: Sun, 23 Oct 2016 19:55:39 +0300 Subject: [PATCH] New notifications in progress --- .../net/osmand/plus/NavigationService.java | 33 ++- .../net/osmand/plus/NotificationHelper.java | 233 ++++++----------- .../osmand/plus/OsmAndLocationSimulation.java | 2 +- .../net/osmand/plus/OsmandApplication.java | 19 +- .../osmand/plus/activities/MapActivity.java | 7 +- .../plus/activities/MapActivityActions.java | 12 +- .../plus/activities/SavingTrackHelper.java | 16 +- .../monitoring/OsmandMonitoringPlugin.java | 25 +- .../notifications/GpsWakeUpNotification.java | 100 ++++++++ .../plus/notifications/GpxNotification.java | 165 ++++++++++++ .../notifications/NavigationNotification.java | 242 ++++++++++++++++++ .../plus/notifications/OsMoNotification.java | 73 ++++++ .../notifications/OsmandNotification.java | 116 +++++++++ .../osmand/plus/routing/RoutingHelper.java | 37 ++- 14 files changed, 874 insertions(+), 206 deletions(-) create mode 100644 OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java create mode 100644 OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java create mode 100644 OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java create mode 100644 OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java create mode 100644 OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index d32d443038..f13be98057 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -1,9 +1,7 @@ package net.osmand.plus; -import net.osmand.PlatformUtil; -import net.osmand.plus.monitoring.OsmandMonitoringPlugin; -import net.osmand.plus.osmo.OsMoPlugin; import android.app.AlarmManager; +import android.app.Notification; import android.app.PendingIntent; import android.app.Service; import android.content.Context; @@ -22,6 +20,10 @@ import android.support.v4.app.NotificationCompat.Builder; import android.util.Log; import android.widget.Toast; +import net.osmand.PlatformUtil; +import net.osmand.plus.notifications.OsmandNotification; +import net.osmand.plus.osmo.OsMoPlugin; + public class NavigationService extends Service implements LocationListener { public static class NavigationServiceBinder extends Binder { @@ -99,6 +101,8 @@ public class NavigationService extends Service implements LocationListener { if (usedBy == 0) { final Intent serviceIntent = new Intent(ctx, NavigationService.class); ctx.stopService(serviceIntent); + } else { + ((OsmandApplication) getApplication()).getNotificationHelper().refreshNotifications(); } } @@ -147,9 +151,14 @@ public class NavigationService extends Service implements LocationListener { // registering icon at top level // Leave icon visible even for navigation for proper display - Builder ntf = app.getNotificationHelper().buildNotificationInStatusBar(); - if (ntf != null) { - startForeground(NotificationHelper.NOTIFICATION_SERVICE_ID, ntf.build()); + OsmandNotification osmandNotification = app.getNotificationHelper().getTopNotification(); + if (osmandNotification != null) { + Builder notification = osmandNotification.buildNotification(); + if (notification != null) { + Notification n = notification.build(); + osmandNotification.setupNotification(n); + startForeground(osmandNotification.getUniqueId(), n); + } } return START_REDELIVER_INTENT; } @@ -170,7 +179,8 @@ public class NavigationService extends Service implements LocationListener { @Override public void onDestroy() { super.onDestroy(); - ((OsmandApplication) getApplication()).setNavigationService(null); + final OsmandApplication app = (OsmandApplication) getApplication(); + app.setNavigationService(null); usedBy = 0; // remove updates LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); @@ -190,8 +200,13 @@ public class NavigationService extends Service implements LocationListener { AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.cancel(pendingIntent); // remove notification - ((OsmandApplication) getApplication()).getNotificationHelper().removeServiceNotification(); stopForeground(Boolean.TRUE); + app.runInUIThread(new Runnable() { + @Override + public void run() { + app.getNotificationHelper().refreshNotifications(); + } + }, 500); } @Override @@ -242,7 +257,7 @@ public class NavigationService extends Service implements LocationListener { plugin.getTracker().disableTracker(); } } - app.getNotificationHelper().removeServiceNotificationCompletely(); + //app.getNotificationHelper().removeNotifications(); NavigationService.this.stopSelf(); } } diff --git a/OsmAnd/src/net/osmand/plus/NotificationHelper.java b/OsmAnd/src/net/osmand/plus/NotificationHelper.java index b980f8dcf4..a67a7d23c0 100644 --- a/OsmAnd/src/net/osmand/plus/NotificationHelper.java +++ b/OsmAnd/src/net/osmand/plus/NotificationHelper.java @@ -1,180 +1,91 @@ package net.osmand.plus; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.BroadcastReceiver; -import android.content.Context; -import android.content.Intent; -import android.content.IntentFilter; -import android.support.v4.app.NotificationCompat.Builder; -import android.support.v7.app.NotificationCompat; - -import net.osmand.plus.activities.MapActivity; -import net.osmand.plus.monitoring.OsmandMonitoringPlugin; +import net.osmand.plus.notifications.GpsWakeUpNotification; +import net.osmand.plus.notifications.GpxNotification; +import net.osmand.plus.notifications.NavigationNotification; +import net.osmand.plus.notifications.OsMoNotification; +import net.osmand.plus.notifications.OsmandNotification; +import net.osmand.plus.notifications.OsmandNotification.NotificationType; public class NotificationHelper { - public final static int NOTIFICATION_SERVICE_ID = 5; - public final static String OSMAND_STOP_SERVICE_ACTION = "OSMAND_STOP_SERVICE_ACTION"; //$NON-NLS-1$ - public final static String OSMAND_STOP_GPX_SERVICE_ACTION = "OSMAND_STOP_GPX_SERVICE_ACTION"; //$NON-NLS-1$ - public final static String OSMAND_START_GPX_SERVICE_ACTION = "OSMAND_START_GPX_SERVICE_ACTION"; //$NON-NLS-1$ - public final static String OSMAND_SAVE_GPX_SERVICE_ACTION = "OSMAND_SAVE_GPX_SERVICE_ACTION"; //$NON-NLS-1$ - + private OsmandApplication app; - private BroadcastReceiver saveBroadcastReceiver; - private BroadcastReceiver stopBroadcastReceiver; - private BroadcastReceiver startBroadcastReceiver; + + private NavigationNotification navigationNotification; + private GpxNotification gpxNotification; + private OsMoNotification osMoNotification; + private GpsWakeUpNotification gpsWakeUpNotification; public NotificationHelper(OsmandApplication app) { this.app = app; init(); } - + + public GpsWakeUpNotification getGpsWakeUpNotification() { + return gpsWakeUpNotification; + } + private void init() { - saveBroadcastReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); - if (plugin != null) { - plugin.saveCurrentTrack(); - } - } - }; - app.registerReceiver(saveBroadcastReceiver, new IntentFilter(OSMAND_SAVE_GPX_SERVICE_ACTION)); - startBroadcastReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); - if (plugin != null) { - plugin.startGPXMonitoring(null); - } - } - }; - app.registerReceiver(startBroadcastReceiver, new IntentFilter(OSMAND_START_GPX_SERVICE_ACTION)); - stopBroadcastReceiver = new BroadcastReceiver() { - - @Override - public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); - if (plugin != null) { - plugin.stopRecording(); - } - } - }; - app.registerReceiver(stopBroadcastReceiver, new IntentFilter(OSMAND_STOP_GPX_SERVICE_ACTION)); + navigationNotification = new NavigationNotification(app); + gpxNotification = new GpxNotification(app); + osMoNotification = new OsMoNotification(app); + gpsWakeUpNotification = new GpsWakeUpNotification(app); } - public Builder buildNotificationInStatusBar() { - NavigationService service = app.getNavigationService(); - String notificationText = ""; - int icon = R.drawable.bgs_icon; - OsmandMonitoringPlugin monitoringPlugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); - if (service != null) { - int soi = service.getServiceOffInterval(); - notificationText = app.getString(R.string.osmand_running_in_background); - String s = ""; - if ((service.getUsedBy() & NavigationService.USED_BY_NAVIGATION) != 0) { - if (s.length() > 0) { - s += ", "; - } - s += app.getString(R.string.shared_string_navigation).toLowerCase(); - } - if ((service.getUsedBy() & NavigationService.USED_BY_GPX) != 0) { - if (s.length() > 0) { - s += ", "; - } - s += app.getString(R.string.shared_string_trip_recording).toLowerCase() - + ": " + OsmAndFormatter.getFormattedDistance(app.getSavingTrackHelper().getDistance(), app); - } - if ((service.getUsedBy() & NavigationService.USED_BY_LIVE) != 0) { - if (s.length() > 0) { - s += ", "; - } - s += app.getString(R.string.osmo); - } - if(s.length() > 0) { - notificationText += " (" + s + "). "; - } - notificationText += app.getString(R.string.gps_wake_up_timer) + ": "; - if (soi == 0) { - notificationText = notificationText + app.getString(R.string.int_continuosly); - } else if (soi <= 90000) { - notificationText = notificationText + Integer.toString(soi / 1000) + " " + app.getString(R.string.int_seconds); - } else { - notificationText = notificationText + Integer.toString(soi / 1000 / 60) + " " + app.getString(R.string.int_min); - } - //} else if(monitoringPlugin == null) { - // return null; - //} else if(app.getSavingTrackHelper().getDistance() > 0f){ - //This produces system notification if unsaved GPX track exists, displaying recorded distance. But only while OsmAnd is in the foreground and while recording has been stopped (otherwise background notification caries this info anyway) - //Purpose is doubtful, we have widget for that. If we re-instate, we need to implement notification refresh upon track saved, maybe also work on notification wording to clarify meaning. - // notificationText = app.getString(R.string.shared_string_trip_recording); - // float dst = app.getSavingTrackHelper().getDistance(); - // notificationText += ": "+OsmAndFormatter.getFormattedDistance(dst, app); - // icon = R.drawable.ic_action_polygom_dark; + public OsmandNotification getTopNotification() { + if (navigationNotification.isEnabled()) { + return navigationNotification; + } else if (gpxNotification.isEnabled() && gpxNotification.isActive()) { + return gpxNotification; + } else if (gpsWakeUpNotification.isEnabled()) { + return gpsWakeUpNotification; + } else if (osMoNotification.isEnabled()) { + return osMoNotification; + } + return null; + } + + public void showNotifications() { + boolean navNotificationVisible = navigationNotification.showNotification(); + gpxNotification.showNotification(); + osMoNotification.showNotification(); + if (!navNotificationVisible && !gpxNotification.isActive()) { + gpsWakeUpNotification.showNotification(); + } + } + + public void refreshNotification(NotificationType notificationType) { + switch (notificationType) { + case NAVIGATION: + navigationNotification.refreshNotification(); + break; + case GPX: + gpxNotification.refreshNotification(); + break; + case OSMO: + osMoNotification.refreshNotification(); + break; + case GPS: + gpsWakeUpNotification.refreshNotification(); + break; + } + } + + public void refreshNotifications() { + boolean navNotificationVisible = navigationNotification.refreshNotification(); + gpxNotification.refreshNotification(); + osMoNotification.refreshNotification(); + if (!navNotificationVisible && !gpxNotification.isActive()) { + gpsWakeUpNotification.refreshNotification(); } else { - return null; - } - - Intent contentIntent = new Intent(app, MapActivity.class); - PendingIntent contentPendingIntent = PendingIntent.getActivity(app, 0, contentIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - final Builder notificationBuilder = new NotificationCompat.Builder(app) - .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) - .setContentTitle(Version.getAppName(app)).setContentText(notificationText).setSmallIcon(icon) - .setContentIntent(contentPendingIntent).setOngoing(service != null); - if (monitoringPlugin != null) { - Intent saveIntent = new Intent(OSMAND_SAVE_GPX_SERVICE_ACTION); - PendingIntent savePendingIntent = PendingIntent.getBroadcast(app, 0, saveIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - - if(service != null && (service.getUsedBy()&2)!=0) { - //checks if service.getUsedBy() includes NavigationService.USED_BY_GPX - Intent stopIntent = new Intent(OSMAND_STOP_GPX_SERVICE_ACTION); - PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - notificationBuilder.addAction(R.drawable.ic_action_rec_stop, - app.getString(R.string.gpx_monitoring_stop), stopPendingIntent); - notificationBuilder.addAction(R.drawable.ic_action_save, app.getString(R.string.shared_string_save), - savePendingIntent); -// } else if(service == null) { - } else { - Intent startIntent = new Intent(OSMAND_START_GPX_SERVICE_ACTION); - PendingIntent startPendingIntent = PendingIntent.getBroadcast(app, 0, startIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - notificationBuilder.addAction(R.drawable.ic_action_rec_start, - app.getString(R.string.gpx_monitoring_start), startPendingIntent); - notificationBuilder.addAction(R.drawable.ic_action_save, app.getString(R.string.shared_string_save), - savePendingIntent); - } - - - - } - return notificationBuilder; - } - - public void showNotification() { - NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); - Builder newNotification = buildNotificationInStatusBar(); - if(newNotification != null) { - mNotificationManager.notify(NOTIFICATION_SERVICE_ID, newNotification.build()); + gpsWakeUpNotification.removeNotification(); } } - public void removeServiceNotification() { - NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.cancel( NOTIFICATION_SERVICE_ID); - Builder newNotification = buildNotificationInStatusBar(); - if(newNotification != null) { - mNotificationManager.notify(NOTIFICATION_SERVICE_ID, newNotification.build()); - } - } - - public void removeServiceNotificationCompletely() { - NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); - mNotificationManager.cancel( NOTIFICATION_SERVICE_ID); + public void removeNotifications() { + navigationNotification.removeNotification(); + gpxNotification.removeNotification(); + osMoNotification.removeNotification(); + gpsWakeUpNotification.removeNotification(); } } diff --git a/OsmAnd/src/net/osmand/plus/OsmAndLocationSimulation.java b/OsmAnd/src/net/osmand/plus/OsmAndLocationSimulation.java index bc8375d351..5034b7d81c 100644 --- a/OsmAnd/src/net/osmand/plus/OsmAndLocationSimulation.java +++ b/OsmAnd/src/net/osmand/plus/OsmAndLocationSimulation.java @@ -179,7 +179,7 @@ public class OsmAndLocationSimulation { return directions.isEmpty() ? 20.0f : Math.max(20.0f, current.distanceTo(directions.get(0)) / 2 ); } - private void stop() { + public void stop() { routeAnimation = null; } diff --git a/OsmAnd/src/net/osmand/plus/OsmandApplication.java b/OsmAnd/src/net/osmand/plus/OsmandApplication.java index 73e5f909bb..3b6e110838 100644 --- a/OsmAnd/src/net/osmand/plus/OsmandApplication.java +++ b/OsmAnd/src/net/osmand/plus/OsmandApplication.java @@ -25,6 +25,7 @@ import android.widget.Toast; import net.osmand.CallbackWithObject; import net.osmand.PlatformUtil; import net.osmand.access.AccessibilityPlugin; +import net.osmand.data.LatLon; import net.osmand.map.OsmandRegions; import net.osmand.osm.MapPoiTypes; import net.osmand.plus.AppInitializer.AppInitializeListener; @@ -56,6 +57,7 @@ import java.io.File; import java.io.FileWriter; import java.io.PrintStream; import java.lang.Thread.UncaughtExceptionHandler; +import java.util.ArrayList; import java.util.Locale; import btools.routingapp.BRouterServiceConnection; @@ -196,7 +198,7 @@ public class OsmandApplication extends MultiDexApplication { if(RateUsBottomSheetDialog.shouldShow(this)) { osmandSettings.RATE_US_STATE.set(RateUsBottomSheetDialog.RateUsState.IGNORED); } - getNotificationHelper().removeServiceNotification(); + getNotificationHelper().removeNotifications(); } public RendererRegistry getRendererRegistry() { @@ -430,6 +432,19 @@ public class OsmandApplication extends MultiDexApplication { this.navigationService = navigationService; } + public void stopNavigation() { + if (locationProvider.getLocationSimulation().isRouteAnimating()) { + locationProvider.getLocationSimulation().stop(); + } + routingHelper.getVoiceRouter().interruptRouteCommands(); + routingHelper.clearCurrentRoute(null, new ArrayList()); + routingHelper.setRoutePlanningMode(false); + osmandSettings.LAST_ROUTING_APPLICATION_MODE = osmandSettings.APPLICATION_MODE.get(); + osmandSettings.APPLICATION_MODE.set(osmandSettings.DEFAULT_APPLICATION_MODE.get()); + if (osmandSettings.USE_MAP_MARKERS.get()) { + targetPointsHelper.removeAllWayPoints(false, false); + } + } private void fullExit() { // http://stackoverflow.com/questions/2092951/how-to-close-android-application @@ -745,7 +760,7 @@ public class OsmandApplication extends MultiDexApplication { serviceIntent.putExtra(NavigationService.USAGE_INTENT, intent); serviceIntent.putExtra(NavigationService.USAGE_OFF_INTERVAL, interval); startService(serviceIntent); - getNotificationHelper().showNotification(); + //getNotificationHelper().showNotifications(); } diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java index a6dadd8171..f78fa68fac 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivity.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivity.java @@ -881,7 +881,7 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven protected void onStart() { super.onStart(); wakeLockHelper.onStart(this); - getMyApplication().getNotificationHelper().showNotification(); + getMyApplication().getNotificationHelper().showNotifications(); } protected void setProgressDlg(Dialog progressDlg) { @@ -901,15 +901,14 @@ public class MapActivity extends OsmandActionBarActivity implements DownloadEven // } // } wakeLockHelper.onStop(this); - if (getMyApplication().getNavigationService() == null) { - getMyApplication().getNotificationHelper().removeServiceNotificationCompletely(); - } + getMyApplication().getNotificationHelper().refreshNotifications(); super.onStop(); } @Override protected void onDestroy() { super.onDestroy(); + getMyApplication().getNotificationHelper().removeNotifications(); unregisterReceiver(screenOffReceiver); FailSafeFuntions.quitRouteRestoreDialog(); OsmandPlugin.onMapActivityDestroy(this); diff --git a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java index 53697488c6..efaa39a4ab 100644 --- a/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java +++ b/OsmAnd/src/net/osmand/plus/activities/MapActivityActions.java @@ -796,17 +796,7 @@ public class MapActivityActions implements DialogProvider { } public void stopNavigationWithoutConfirm() { - if (getMyApplication().getLocationProvider().getLocationSimulation().isRouteAnimating()) { - getMyApplication().getLocationProvider().getLocationSimulation().startStopRouteAnimation(mapActivity); - } - routingHelper.getVoiceRouter().interruptRouteCommands(); - routingHelper.clearCurrentRoute(null, new ArrayList()); - routingHelper.setRoutePlanningMode(false); - settings.LAST_ROUTING_APPLICATION_MODE = settings.APPLICATION_MODE.get(); - settings.APPLICATION_MODE.set(settings.DEFAULT_APPLICATION_MODE.get()); - if (settings.USE_MAP_MARKERS.get()) { - getMyApplication().getTargetPointsHelper().removeAllWayPoints(false, false); - } + getMyApplication().stopNavigation(); mapActivity.updateApplicationModeSettings(); mapActivity.getDashboard().clearDeletedPoints(); } diff --git a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java index 9b0c65a3e2..9b6987b700 100644 --- a/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java +++ b/OsmAnd/src/net/osmand/plus/activities/SavingTrackHelper.java @@ -18,6 +18,8 @@ import net.osmand.plus.OsmandApplication; import net.osmand.plus.OsmandPlugin; import net.osmand.plus.OsmandSettings; import net.osmand.plus.monitoring.OsmandMonitoringPlugin; +import net.osmand.plus.notifications.OsmandNotification; +import net.osmand.plus.notifications.OsmandNotification.NotificationType; import net.osmand.util.MapUtils; import org.apache.commons.logging.Log; @@ -63,6 +65,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { private LatLon lastPoint; private float distance = 0; + private long duration = 0; private SelectedGpxFile currentTrack; private int points; @@ -227,6 +230,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { } distance = 0; points = 0; + duration = 0; currentTrack.getModifiableGpxFile().points.clear(); currentTrack.getModifiableGpxFile().tracks.clear(); currentTrack.getModifiablePointsToDisplay().clear(); @@ -383,7 +387,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { if (record) { insertData(location.getLatitude(), location.getLongitude(), location.getAltitude(), location.getSpeed(), location.getAccuracy(), locationTime, settings); - ctx.getNotificationHelper().showNotification(); + ctx.getNotificationHelper().refreshNotification(NotificationType.GPX); } } @@ -400,6 +404,9 @@ public class SavingTrackHelper extends SQLiteOpenHelper { float[] lastInterval = new float[1]; net.osmand.Location.distanceBetween(lat, lon, lastPoint.getLatitude(), lastPoint.getLongitude(), lastInterval); + if (lastTimeUpdated > 0 && time > lastTimeUpdated) { + duration += time - lastTimeUpdated; + } distance += lastInterval[0]; lastPoint = new LatLon(lat, lon); } @@ -573,6 +580,7 @@ public class SavingTrackHelper extends SQLiteOpenHelper { GPXTrackAnalysis analysis = currentTrack.getModifiableGpxFile().getAnalysis(System.currentTimeMillis()); distance = analysis.totalDistance; points = analysis.wptPoints; + duration = analysis.timeSpan; } private void prepareCurrentTrackForRecording() { @@ -598,7 +606,11 @@ public class SavingTrackHelper extends SQLiteOpenHelper { public float getDistance() { return distance; } - + + public long getDuration() { + return duration; + } + public int getPoints() { return points; } diff --git a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java index 128440bae3..a4e50b2f91 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java @@ -59,7 +59,13 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { ApplicationMode.regWidgetVisibility("monitoring", am.toArray(new ApplicationMode[am.size()])); settings = app.getSettings(); } - + + @Override + public void disable(OsmandApplication app) { + super.disable(app); + app.getNotificationHelper().refreshNotifications(); + } + @Override public void updateLocation(Location location) { liveMonitoringHelper.updateLocation(location); @@ -334,9 +340,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { @Override protected void onPreExecute() { isSaving = true; - if (monitoringControl != null) { - monitoringControl.updateInfo(null); - } + updateControl(); } @Override @@ -346,7 +350,7 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { helper.saveDataToGpx(app.getAppCustomization().getTracksDir()); helper.close(); } finally { - app.getNotificationHelper().showNotification(); + app.getNotificationHelper().showNotifications(); } return null; } @@ -354,18 +358,21 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { @Override protected void onPostExecute(Void aVoid) { isSaving = false; - if (monitoringControl != null) { - monitoringControl.updateInfo(null); - } + updateControl(); } }, (Void) null); } + public void updateControl() { + if (monitoringControl != null) { + monitoringControl.updateInfo(null); + } + } + public void stopRecording(){ settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false); if (app.getNavigationService() != null) { app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_GPX); - app.getNotificationHelper().showNotification(); } } diff --git a/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java b/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java new file mode 100644 index 0000000000..0d032e844a --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/notifications/GpsWakeUpNotification.java @@ -0,0 +1,100 @@ +package net.osmand.plus.notifications; + +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.support.v4.app.NotificationCompat.BigTextStyle; +import android.support.v4.app.NotificationCompat.Builder; +import android.support.v7.app.NotificationCompat; + +import net.osmand.plus.NavigationService; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.Version; + +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 GpsWakeUpNotification(OsmandApplication app) { + super(app); + } + + @Override + public void init() { + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + Intent serviceIntent = new Intent(app, NavigationService.class); + app.stopService(serviceIntent); + } + }, new IntentFilter(OSMAND_STOP_GPS_WAKE_UP_SERVICE_ACTION)); + } + + @Override + public NotificationType getType() { + return NotificationType.GPS; + } + + @Override + public int getPriority() { + return NotificationCompat.PRIORITY_HIGH; + } + + @Override + public boolean isActive() { + return isEnabled(); + } + + @Override + public boolean isEnabled() { + NavigationService service = app.getNavigationService(); + return service != null && (service.getUsedBy() & USED_BY_WAKE_UP) != 0; + } + + @Override + public Builder buildNotification() { + NavigationService service = app.getNavigationService(); + String notificationTitle; + String notificationText; + color = 0; + icon = R.drawable.bgs_icon; + if (service != null && (service.getUsedBy() & USED_BY_WAKE_UP) != 0) { + int soi = service.getServiceOffInterval(); + color = app.getResources().getColor(R.color.osmand_orange); + notificationTitle = Version.getAppName(app); + notificationText = app.getString(R.string.gps_wake_up_timer) + ": "; + if (soi == 0) { + notificationText = notificationText + app.getString(R.string.int_continuosly); + } else if (soi <= 90000) { + notificationText = notificationText + Integer.toString(soi / 1000) + " " + app.getString(R.string.int_seconds); + } else { + notificationText = notificationText + Integer.toString(soi / 1000 / 60) + " " + app.getString(R.string.int_min); + } + } else { + return null; + } + + final Builder notificationBuilder = createBuilder() + .setContentTitle(notificationTitle) + .setStyle(new BigTextStyle().bigText(notificationText)); + + Intent wakeUpIntent = new Intent(OSMAND_STOP_GPS_WAKE_UP_SERVICE_ACTION); + PendingIntent stopWakeUpPendingIntent = PendingIntent.getBroadcast(app, 0, wakeUpIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notificationBuilder.addAction(R.drawable.ic_action_rec_stop, + app.getString(R.string.stop_navigation_service), stopWakeUpPendingIntent); + + return notificationBuilder; + } + + @Override + public int getUniqueId() { + return 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 new file mode 100644 index 0000000000..bfa08bfbc4 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/notifications/GpxNotification.java @@ -0,0 +1,165 @@ +package net.osmand.plus.notifications; + +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.support.v4.app.NotificationCompat.BigTextStyle; +import android.support.v4.app.NotificationCompat.Builder; +import android.support.v7.app.NotificationCompat; + +import net.osmand.plus.NavigationService; +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 GpxNotification(OsmandApplication app) { + super(app); + } + + @Override + public void init() { + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); + if (plugin != null) { + plugin.saveCurrentTrack(); + } + } + }, new IntentFilter(OSMAND_SAVE_GPX_SERVICE_ACTION)); + + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); + if (plugin != null) { + plugin.startGPXMonitoring(null); + plugin.updateControl(); + } + } + }, new IntentFilter(OSMAND_START_GPX_SERVICE_ACTION)); + + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + final OsmandMonitoringPlugin plugin = OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); + if (plugin != null) { + plugin.stopRecording(); + plugin.updateControl(); + } + } + }, new IntentFilter(OSMAND_STOP_GPX_SERVICE_ACTION)); + } + + @Override + public NotificationType getType() { + return NotificationType.GPX; + } + + @Override + public int getPriority() { + return NotificationCompat.PRIORITY_DEFAULT; + } + + @Override + public boolean isActive() { + NavigationService service = app.getNavigationService(); + return isEnabled() + && service != null + && (service.getUsedBy() & USED_BY_GPX) != 0; + } + + @Override + public boolean isEnabled() { + return OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class) != null; + } + + @Override + public Builder buildNotification() { + if (!isEnabled()) { + return null; + } + String notificationTitle; + String notificationText; + color = 0; + icon = R.drawable.ic_action_polygom_dark; + boolean isGpxRecording = app.getSavingTrackHelper().getIsRecording(); + float recordedDistane = app.getSavingTrackHelper().getDistance(); + ongoing = true; + if (isGpxRecording) { + color = app.getResources().getColor(R.color.osmand_orange); + notificationTitle = app.getString(R.string.shared_string_trip) + " • " + + Algorithms.formatDuration((int) (app.getSavingTrackHelper().getDuration() / 1000), true); + notificationText = app.getString(R.string.shared_string_recorded) + + ": " + OsmAndFormatter.getFormattedDistance(recordedDistane, app); + } else { + if (recordedDistane > 0) { + notificationTitle = app.getString(R.string.shared_string_pause) + " • " + + Algorithms.formatDuration((int) (app.getSavingTrackHelper().getDuration() / 1000), true); + notificationText = app.getString(R.string.shared_string_recorded) + + ": " + OsmAndFormatter.getFormattedDistance(recordedDistane, app); + } else { + ongoing = false; + notificationTitle = app.getString(R.string.shared_string_trip_recording); + notificationText = app.getString(R.string.gpx_logging_no_data); + } + } + + final Builder notificationBuilder = createBuilder() + .setContentTitle(notificationTitle) + .setStyle(new BigTextStyle().bigText(notificationText)); + + Intent saveIntent = new Intent(OSMAND_SAVE_GPX_SERVICE_ACTION); + PendingIntent savePendingIntent = PendingIntent.getBroadcast(app, 0, saveIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + if (isGpxRecording) { + Intent stopIntent = new Intent(OSMAND_STOP_GPX_SERVICE_ACTION); + PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notificationBuilder.addAction(R.drawable.ic_pause, + app.getString(R.string.shared_string_pause), stopPendingIntent); + if (app.getSavingTrackHelper().getDistance() > 0) { + notificationBuilder.addAction(R.drawable.ic_action_save, app.getString(R.string.shared_string_save), + savePendingIntent); + } + } else { + Intent startIntent = new Intent(OSMAND_START_GPX_SERVICE_ACTION); + PendingIntent startPendingIntent = PendingIntent.getBroadcast(app, 0, startIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + if (recordedDistane > 0) { + notificationBuilder.addAction(R.drawable.ic_action_rec_start, + app.getString(R.string.shared_string_continue), startPendingIntent); + notificationBuilder.addAction(R.drawable.ic_action_save, app.getString(R.string.shared_string_save), + savePendingIntent); + } else { + notificationBuilder.addAction(R.drawable.ic_action_rec_start, + app.getString(R.string.shared_string_record), startPendingIntent); + } + } + + return notificationBuilder; + } + + @Override + public int getUniqueId() { + return GPX_NOTIFICATION_SERVICE_ID; + } +} diff --git a/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java new file mode 100644 index 0000000000..afeeef5cab --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/notifications/NavigationNotification.java @@ -0,0 +1,242 @@ +package net.osmand.plus.notifications; + +import android.app.Notification; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.graphics.Bitmap; +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.PorterDuff; +import android.graphics.PorterDuffColorFilter; +import android.graphics.drawable.Drawable; +import android.os.Build; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationCompat.BigTextStyle; +import android.support.v4.app.NotificationCompat.Builder; +import android.view.View; + +import net.osmand.AndroidUtils; +import net.osmand.plus.NavigationService; +import net.osmand.plus.OsmAndFormatter; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.R; +import net.osmand.plus.routing.RouteCalculationResult; +import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo; +import net.osmand.plus.routing.RouteDirectionInfo; +import net.osmand.plus.routing.RoutingHelper; +import net.osmand.plus.views.TurnPathHelper; +import net.osmand.plus.views.mapwidgets.NextTurnInfoWidget.TurnDrawable; +import net.osmand.router.TurnType; +import net.osmand.util.Algorithms; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +import static net.osmand.plus.NavigationService.USED_BY_NAVIGATION; + +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"; + + private Map bitmapCache = new HashMap<>(); + private Bitmap turnBitmap; + private boolean leftSide; + + public NavigationNotification(OsmandApplication app) { + super(app); + } + + @Override + public void init() { + leftSide = app.getSettings().DRIVING_REGION.get().leftHandDriving; + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + RoutingHelper routingHelper = app.getRoutingHelper(); + routingHelper.setRoutePlanningMode(true); + routingHelper.setFollowingMode(false); + routingHelper.setPauseNaviation(true); + } + }, new IntentFilter(OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION)); + + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + RoutingHelper routingHelper = app.getRoutingHelper(); + routingHelper.setRoutePlanningMode(false); + routingHelper.setFollowingMode(true); + } + }, new IntentFilter(OSMAND_RESUME_NAVIGATION_SERVICE_ACTION)); + + app.registerReceiver(new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + app.stopNavigation(); + } + }, new IntentFilter(OSMAND_STOP_NAVIGATION_SERVICE_ACTION)); + } + + @Override + public NotificationType getType() { + return NotificationType.NAVIGATION; + } + + @Override + public int getPriority() { + return NotificationCompat.PRIORITY_HIGH; + } + + @Override + public boolean isActive() { + return isEnabled(); + } + + @Override + public boolean isEnabled() { + NavigationService service = app.getNavigationService(); + return service != null && (service.getUsedBy() & USED_BY_NAVIGATION) != 0; + } + + @Override + public Builder buildNotification() { + NavigationService service = app.getNavigationService(); + String notificationTitle; + StringBuilder notificationText = new StringBuilder(); + color = 0; + icon = R.drawable.ic_action_start_navigation; + turnBitmap = null; + RoutingHelper routingHelper = app.getRoutingHelper(); + boolean followingMode = routingHelper.isFollowingMode() || app.getLocationProvider().getLocationSimulation().isRouteAnimating(); + if (service != null && (service.getUsedBy() & USED_BY_NAVIGATION) != 0) { + color = app.getResources().getColor(R.color.osmand_orange); + + String distanceStr = app.getString(R.string.route_distance) + OsmAndFormatter.getFormattedDistance(app.getRoutingHelper().getLeftDistance(), app); + String durationStr = app.getString(R.string.access_arrival_time) + ": " + SimpleDateFormat.getTimeInstance(DateFormat.SHORT) + .format(new Date(System.currentTimeMillis() + app.getRoutingHelper().getLeftTime() * 1000)); + + TurnType turnType = null; + boolean deviatedFromRoute; + int turnImminent = 0; + int nextTurnDistance = 0; + RouteDirectionInfo ri = null; + if (routingHelper.isRouteCalculated() && followingMode) { + deviatedFromRoute = routingHelper.isDeviatedFromRoute(); + + if (deviatedFromRoute) { + turnImminent = 0; + turnType = TurnType.valueOf(TurnType.OFFR, leftSide); + nextTurnDistance = (int) routingHelper.getRouteDeviation(); + } else { + NextDirectionInfo r = routingHelper.getNextRouteDirectionInfo(new NextDirectionInfo(), true); + if (r != null && r.distanceTo > 0 && r.directionInfo != null) { + ri = r.directionInfo; + turnType = r.directionInfo.getTurnType(); + nextTurnDistance = r.distanceTo; + turnImminent = r.imminent; + } + } + + TurnDrawable drawable = new TurnDrawable(app, false); + int height = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_height); + int width = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_width); + drawable.setBounds(0, 0, width, height); + drawable.setTurnType(turnType); + drawable.setTurnImminent(turnImminent, deviatedFromRoute); + turnBitmap = drawableToBitmap(drawable); + + notificationTitle = OsmAndFormatter.getFormattedDistance(nextTurnDistance, app) + " • " + RouteCalculationResult.toString(turnType, app); + if (ri != null && !Algorithms.isEmpty(ri.getDescriptionRoutePart())) { + notificationText.append(ri.getDescriptionRoutePart()); + notificationText.append("\n"); + } + notificationText.append(distanceStr).append(" • ").append(durationStr); + + } else { + notificationTitle = app.getString(R.string.shared_string_navigation); + String error = routingHelper.getLastRouteCalcErrorShort(); + if (Algorithms.isEmpty(error)) { + notificationText.append("Route calculation..."); + } else { + notificationText.append(error); + } + } + + } else { + return null; + } + + final Builder notificationBuilder = createBuilder() + .setContentTitle(notificationTitle) + .setStyle(new BigTextStyle().bigText(notificationText)) + .setLargeIcon(turnBitmap); + + Intent stopIntent = new Intent(OSMAND_STOP_NAVIGATION_SERVICE_ACTION); + PendingIntent stopPendingIntent = PendingIntent.getBroadcast(app, 0, stopIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notificationBuilder.addAction(R.drawable.ic_action_remove_dark, + app.getString(R.string.shared_string_control_stop), stopPendingIntent); + + if (routingHelper.isRouteCalculated() && followingMode) { + Intent pauseIntent = new Intent(OSMAND_PAUSE_NAVIGATION_SERVICE_ACTION); + PendingIntent pausePendingIntent = PendingIntent.getBroadcast(app, 0, pauseIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notificationBuilder.addAction(R.drawable.ic_pause, + app.getString(R.string.shared_string_pause), pausePendingIntent); + } else { + Intent resumeIntent = new Intent(OSMAND_STOP_NAVIGATION_SERVICE_ACTION); + PendingIntent resumePendingIntent = PendingIntent.getBroadcast(app, 0, resumeIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + notificationBuilder.addAction(R.drawable.ic_play_dark, + app.getString(R.string.shared_string_continue), resumePendingIntent); + } + + return notificationBuilder; + } + + @Override + public void setupNotification(Notification notification) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + int smallIconViewId = app.getResources().getIdentifier("right_icon", "id", android.R.class.getPackage().getName()); + + if (smallIconViewId != 0) { + if (notification.contentIntent != null) + notification.contentView.setViewVisibility(smallIconViewId, View.INVISIBLE); + + if (notification.headsUpContentView != null) + notification.headsUpContentView.setViewVisibility(smallIconViewId, View.INVISIBLE); + + if (notification.bigContentView != null) + notification.bigContentView.setViewVisibility(smallIconViewId, View.INVISIBLE); + } + } + } + + public Bitmap drawableToBitmap(Drawable drawable) { + int height = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_height); + int width = (int) app.getResources().getDimension(android.R.dimen.notification_large_icon_width); + + Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + canvas.drawColor(0, PorterDuff.Mode.CLEAR); + drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + drawable.draw(canvas); + + return bitmap; + } + + @Override + public int getUniqueId() { + return NAVIGATION_NOTIFICATION_SERVICE_ID; + } +} diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java new file mode 100644 index 0000000000..973786d6c0 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/notifications/OsMoNotification.java @@ -0,0 +1,73 @@ +package net.osmand.plus.notifications; + +import android.app.PendingIntent; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; +import android.support.v4.app.NotificationCompat.BigTextStyle; +import android.support.v4.app.NotificationCompat.Builder; + +import net.osmand.plus.NavigationService; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandPlugin; +import net.osmand.plus.R; +import net.osmand.plus.Version; +import net.osmand.plus.activities.MapActivity; +import net.osmand.plus.osmo.OsMoPlugin; + +import static android.support.v7.app.NotificationCompat.*; +import static net.osmand.plus.NavigationService.USED_BY_GPX; +import static net.osmand.plus.NavigationService.USED_BY_LIVE; + +public class OsMoNotification extends OsmandNotification { + + public OsMoNotification(OsmandApplication app) { + super(app); + } + + @Override + public NotificationType getType() { + return NotificationType.OSMO; + } + + @Override + public int getPriority() { + return NotificationCompat.PRIORITY_DEFAULT; + } + + @Override + public boolean isActive() { + NavigationService service = app.getNavigationService(); + return isEnabled() + && service != null + && (service.getUsedBy() & USED_BY_LIVE) != 0; + } + + @Override + public boolean isEnabled() { + return OsmandPlugin.getEnabledPlugin(OsMoPlugin.class) != null; + } + + @Override + public Builder buildNotification() { + String notificationTitle; + String notificationText; + color = 0; + icon = R.drawable.ic_osmo_dark; + color = app.getResources().getColor(R.color.osmand_orange); + notificationTitle = Version.getAppName(app); + notificationText = app.getString(R.string.osmo); + + final Builder notificationBuilder = createBuilder() + .setContentTitle(notificationTitle) + .setStyle(new BigTextStyle().bigText(notificationText)); + + return notificationBuilder; + } + + + + @Override + public int getUniqueId() { + return OSMO_NOTIFICATION_SERVICE_ID; + } +} diff --git a/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java new file mode 100644 index 0000000000..0cd073a658 --- /dev/null +++ b/OsmAnd/src/net/osmand/plus/notifications/OsmandNotification.java @@ -0,0 +1,116 @@ +package net.osmand.plus.notifications; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat.Builder; + +import net.osmand.plus.OsmandApplication; +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 OSMO_NOTIFICATION_SERVICE_ID = 7; + public final static int GPS_WAKE_UP_NOTIFICATION_SERVICE_ID = 8; + + protected OsmandApplication app; + protected boolean ongoing = true; + protected int color; + protected int icon; + + public enum NotificationType { + NAVIGATION, + GPX, + OSMO, + GPS + } + + public OsmandNotification(OsmandApplication app) { + this.app = app; + init(); + } + + public void init() { + } + + public abstract NotificationType getType(); + + protected Builder createBuilder() { + 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) + .setPriority(getPriority()) + .setOngoing(ongoing) + .setContentIntent(contentPendingIntent); + + if (color != 0) { + builder.setColor(color); + } + if (icon != 0) { + builder.setSmallIcon(icon); + } + + return builder; + } + + public abstract Builder buildNotification(); + + public abstract int getUniqueId(); + + public abstract int getPriority(); + + public abstract boolean isActive(); + + public abstract boolean isEnabled(); + + public void setupNotification(Notification notification) { + } + + public boolean showNotification() { + NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); + if (isEnabled()) { + Builder newNotification = buildNotification(); + if (newNotification != null) { + Notification notification = newNotification.build(); + setupNotification(notification); + notificationManager.notify(getUniqueId(), notification); + return true; + } + } + return false; + } + + public boolean refreshNotification() { + NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); + if (isEnabled()) { + Builder newNotification = buildNotification(); + if (newNotification != null) { + Notification notification = newNotification.build(); + setupNotification(notification); + notificationManager.notify(getUniqueId(), notification); + return true; + } + } else { + notificationManager.cancel(getUniqueId()); + } + return false; + } + + public void removeNotification() { + NotificationManager notificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE); + notificationManager.cancel(getUniqueId()); + } + + public void closeSystemDialogs(Context context) { + Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); + context.sendBroadcast(it); + } +} + diff --git a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java index 30eea29b7d..0b1ccf5306 100644 --- a/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java +++ b/OsmAnd/src/net/osmand/plus/routing/RoutingHelper.java @@ -15,6 +15,7 @@ import net.osmand.plus.OsmandSettings; import net.osmand.plus.R; import net.osmand.plus.TargetPointsHelper; import net.osmand.plus.TargetPointsHelper.TargetPoint; +import net.osmand.plus.notifications.OsmandNotification.NotificationType; import net.osmand.plus.routing.RouteCalculationResult.NextDirectionInfo; import net.osmand.plus.routing.RouteProvider.GPXRouteParamsBuilder; import net.osmand.plus.routing.RouteProvider.RouteService; @@ -29,6 +30,8 @@ import java.util.Iterator; import java.util.LinkedList; import java.util.List; +import static net.osmand.plus.notifications.OsmandNotification.NotificationType.NAVIGATION; + public class RoutingHelper { private static final org.apache.commons.logging.Log log = PlatformUtil.getLog(RoutingHelper.class); @@ -66,6 +69,8 @@ public class RoutingHelper { private static final int RECALCULATE_THRESHOLD_CAUSING_FULL_RECALCULATE_INTERVAL = 120000; private Thread currentRunningJob; private long lastTimeEvaluatedRoute = 0; + private String lastRouteCalcError; + private String lastRouteCalcErrorShort; private long recalculateCountInInterval = 0; private int evalWaitInterval = 0; @@ -105,7 +110,15 @@ public class RoutingHelper { public OsmandApplication getApplication() { return app; } - + + public String getLastRouteCalcError() { + return lastRouteCalcError; + } + + public String getLastRouteCalcErrorShort() { + return lastRouteCalcErrorShort; + } + public void setPauseNaviation(boolean b) { this.isPauseNavigation = b; if (b) { @@ -472,6 +485,7 @@ public class RoutingHelper { // that node already passed route.updateCurrentRoute(newCurrentRoute + 1); currentRoute = newCurrentRoute + 1; + app.getNotificationHelper().refreshNotification(NotificationType.NAVIGATION); } else { break; } @@ -830,7 +844,9 @@ public class RoutingHelper { synchronized (RoutingHelper.this) { currentRunningJob = this; } - } + } + lastRouteCalcError = null; + lastRouteCalcErrorShort = null; RouteCalculationResult res = provider.calculateRouteImpl(params); if (params.calculationProgress.isCancelled) { synchronized (RoutingHelper.this) { @@ -856,17 +872,24 @@ public class RoutingHelper { } if(res.isCalculated()){ setNewRoute(prev, res, params.start); - + } else if (onlineSourceWithoutInternet) { - showMessage(app.getString(R.string.error_calculating_route) - + ":\n" + app.getString(R.string.internet_connection_required_for_online_route)); //$NON-NLS-1$ + lastRouteCalcError = app.getString(R.string.error_calculating_route) + + ":\n" + app.getString(R.string.internet_connection_required_for_online_route); + lastRouteCalcErrorShort = app.getString(R.string.error_calculating_route); + showMessage(lastRouteCalcError); //$NON-NLS-1$ } else { if (res.getErrorMessage() != null) { - showMessage(app.getString(R.string.error_calculating_route) + ":\n" + res.getErrorMessage()); //$NON-NLS-1$ + lastRouteCalcError = app.getString(R.string.error_calculating_route) + ":\n" + res.getErrorMessage(); + lastRouteCalcErrorShort = app.getString(R.string.error_calculating_route); + showMessage(lastRouteCalcError); //$NON-NLS-1$ } else { - showMessage(app.getString(R.string.empty_route_calculated)); + lastRouteCalcError = app.getString(R.string.empty_route_calculated); + lastRouteCalcErrorShort = app.getString(R.string.empty_route_calculated); + showMessage(lastRouteCalcError); } } + app.getNotificationHelper().refreshNotification(NAVIGATION); lastTimeEvaluatedRoute = System.currentTimeMillis(); }