From c31bd9eea5ff016f4ec92e1539ae744ced1f7035 Mon Sep 17 00:00:00 2001 From: GaidamakUA Date: Tue, 8 Sep 2015 10:12:14 +0300 Subject: [PATCH] Notification for OsmandMonitoringPlugin --- .../net/osmand/plus/NavigationService.java | 32 ++++--------- .../monitoring/OsmandMonitoringPlugin.java | 46 ++++++++++++++++++- .../SettingsMonitoringActivity.java | 17 +++---- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/NavigationService.java b/OsmAnd/src/net/osmand/plus/NavigationService.java index ad11896d9d..1a2740bd71 100644 --- a/OsmAnd/src/net/osmand/plus/NavigationService.java +++ b/OsmAnd/src/net/osmand/plus/NavigationService.java @@ -38,7 +38,6 @@ public class NavigationService extends Service implements LocationListener { // global id don't conflict with others private 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_SAVE_SERVICE_ACTION = "OSMAND_SAVE_SERVICE_ACTION"; public static int USED_BY_NAVIGATION = 1; public static int USED_BY_GPX = 2; public static int USED_BY_LIVE = 4; @@ -176,26 +175,23 @@ public class NavigationService extends Service implements LocationListener { if (settings.SAVE_GLOBAL_TRACK_TO_GPX.get()) { settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false); } - OsMoPlugin plugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class); - if (plugin != null) { - if (plugin.getTracker().isEnabledTracker()) { - plugin.getTracker().disableTracker(); + OsMoPlugin osmoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class); + if (osmoPlugin != null) { + if (osmoPlugin.getTracker().isEnabledTracker()) { + osmoPlugin.getTracker().disableTracker(); } } + OsmandMonitoringPlugin monitoringPlugin = + OsmandPlugin.getEnabledPlugin(OsmandMonitoringPlugin.class); + if (monitoringPlugin != null) { + monitoringPlugin.stopRecording(); + } NavigationService.this.stopSelf(); } }; registerReceiver(broadcastReceiver, new IntentFilter(OSMAND_STOP_SERVICE_ACTION)); - saveBroadcastReceiver = new BroadcastReceiver() { - @Override - public void onReceive(Context context, Intent intent) { - final OsmandMonitoringPlugin plugin = OsmandPlugin - .getEnabledPlugin(OsmandMonitoringPlugin.class); - plugin.saveCurrentTrack(); - } - }; - registerReceiver(saveBroadcastReceiver, new IntentFilter(OSMAND_SAVE_SERVICE_ACTION)); + //Show currently active wake-up interval int soi = settings.SERVICE_OFF_INTERVAL.get(); @@ -215,14 +211,6 @@ public class NavigationService extends Service implements LocationListener { // notification.flags = Notification.FLAG_NO_CLEAR; // startForeground(NOTIFICATION_SERVICE_ID, notification); - String stop = getResources().getString(R.string.shared_string_control_stop); - Intent stopIntent = new Intent(OSMAND_STOP_SERVICE_ACTION); - PendingIntent stopPendingIntent = PendingIntent.getBroadcast(this, 0, stopIntent, - PendingIntent.FLAG_UPDATE_CURRENT); - String pause = getResources().getString(R.string.shared_string_save); - Intent saveIntent = new Intent(OSMAND_SAVE_SERVICE_ACTION); - PendingIntent savePendingIntent = PendingIntent.getBroadcast(this, 0, saveIntent, - PendingIntent.FLAG_UPDATE_CURRENT); Intent contentIntent = new Intent(this, MapActivity.class); PendingIntent contentPendingIntent = PendingIntent.getActivity(this, 0, contentIntent, PendingIntent.FLAG_UPDATE_CURRENT); diff --git a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java index 635fc44b49..1d0fb01550 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/OsmandMonitoringPlugin.java @@ -3,9 +3,15 @@ package net.osmand.plus.monitoring; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; +import android.content.Intent; +import android.content.IntentFilter; +import android.support.v4.app.NotificationCompat; import android.util.DisplayMetrics; import android.view.View; import android.view.WindowManager; @@ -45,6 +51,8 @@ import gnu.trove.list.array.TIntArrayList; public class OsmandMonitoringPlugin extends OsmandPlugin { private static final String ID = "osmand.monitoring"; + private static final int notificationId = ID.hashCode(); + public final static String OSMAND_SAVE_SERVICE_ACTION = "OSMAND_SAVE_SERVICE_ACTION"; private OsmandSettings settings; private OsmandApplication app; private TextInfoWidget monitoringControl; @@ -329,6 +337,10 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { public void stopRecording(){ settings.SAVE_GLOBAL_TRACK_TO_GPX.set(false); if (app.getNavigationService() != null) { + NotificationManager mNotificationManager = + (NotificationManager) app.getNavigationService() + .getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager.cancel(notificationId); app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_GPX); } } @@ -367,7 +379,39 @@ public class OsmandMonitoringPlugin extends OsmandPlugin { } }); } - + + String stop = map.getResources().getString(R.string.shared_string_control_stop); + Intent stopIntent = new Intent(NavigationService.OSMAND_STOP_SERVICE_ACTION); + PendingIntent stopPendingIntent = PendingIntent.getBroadcast(map, 0, stopIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + String save = map.getResources().getString(R.string.shared_string_save); + Intent saveIntent = new Intent(OSMAND_SAVE_SERVICE_ACTION); + PendingIntent savePendingIntent = PendingIntent.getBroadcast(map, 0, saveIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + BroadcastReceiver saveBroadcastReceiver = new BroadcastReceiver() { + @Override + public void onReceive(Context context, Intent intent) { + final OsmandMonitoringPlugin plugin = OsmandPlugin + .getEnabledPlugin(OsmandMonitoringPlugin.class); + if (plugin != null) { + plugin.saveCurrentTrack(); + } + } + }; + map.registerReceiver(saveBroadcastReceiver, new IntentFilter(OSMAND_SAVE_SERVICE_ACTION)); + + final NotificationCompat.Builder notificationBuilder = + new android.support.v7.app.NotificationCompat.Builder(map) + .setContentTitle(map.getResources().getString(R.string.map_widget_monitoring)) + .setSmallIcon(R.drawable.ic_action_polygom_dark) +// .setLargeIcon(Helpers.getBitmap(R.drawable.mirakel, getBaseContext())) + .setOngoing(true) + .addAction(R.drawable.ic_action_rec_stop, stop, stopPendingIntent) + .addAction(R.drawable.ic_action_save, save, savePendingIntent); + NotificationManager mNotificationManager = + (NotificationManager) map.getSystemService(Context.NOTIFICATION_SERVICE); + mNotificationManager.notify(notificationId, notificationBuilder.build()); } public static void showIntervalChooseDialog(final Context uiCtx, final String patternMsg, diff --git a/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java b/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java index e81fd6651a..ac92f53f1a 100644 --- a/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java +++ b/OsmAnd/src/net/osmand/plus/monitoring/SettingsMonitoringActivity.java @@ -1,14 +1,6 @@ package net.osmand.plus.monitoring; -import android.view.Window; -import net.osmand.plus.NavigationService; -import net.osmand.plus.OsmAndTaskManager.OsmAndTaskRunnable; -import net.osmand.plus.OsmandApplication; -import net.osmand.plus.OsmandSettings; -import net.osmand.plus.R; -import net.osmand.plus.activities.SavingTrackHelper; -import net.osmand.plus.activities.SettingsBaseActivity; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; @@ -21,6 +13,15 @@ import android.preference.Preference.OnPreferenceChangeListener; import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceCategory; import android.preference.PreferenceScreen; +import android.view.Window; + +import net.osmand.plus.NavigationService; +import net.osmand.plus.OsmAndTaskManager.OsmAndTaskRunnable; +import net.osmand.plus.OsmandApplication; +import net.osmand.plus.OsmandSettings; +import net.osmand.plus.R; +import net.osmand.plus.activities.SavingTrackHelper; +import net.osmand.plus.activities.SettingsBaseActivity; public class SettingsMonitoringActivity extends SettingsBaseActivity {