OsMo notifiation done

This commit is contained in:
Alexey Kulish 2016-10-25 22:46:36 +03:00
parent 888f7dd818
commit 421cac7b8c
5 changed files with 125 additions and 8 deletions

View file

@ -9,6 +9,9 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
-->
<string name="osmo_share_location">Share location</string>
<string name="osmo_pause_location">Pause location</string>
<string name="osmo_service_running">OsMo service is running</string>
<string name="trip_rec_notification_settings">Trip recording (no data)</string>
<string name="trip_rec_notification_settings_desc">Show notification which allows to start trip recording by pressing Record button</string>
<string name="shared_string_notifications">Notifications</string>

View file

@ -1,7 +1,10 @@
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;
import android.support.v4.app.NotificationCompat.BigTextStyle;
import android.support.v4.app.NotificationCompat.Builder;
@ -10,20 +13,84 @@ 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 final static String OSMAND_START_OSMO_SERVICE_ACTION = "OSMAND_START_OSMO_SERVICE_ACTION";
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 OsMoNotification(OsmandApplication app) {
super(app);
}
@Override
public void init() {
app.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
if (osMoPlugin != null) {
osMoPlugin.getService().connect(true);
osMoPlugin.refreshMap();
}
}
}, new IntentFilter(OSMAND_START_OSMO_SERVICE_ACTION));
app.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
if (osMoPlugin != null) {
osMoPlugin.getTracker().disableTracker();
osMoPlugin.getService().disconnect();
if (app.getNavigationService() != null) {
app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_LIVE);
}
osMoPlugin.refreshMap();
}
}
}, new IntentFilter(OSMAND_STOP_OSMO_SERVICE_ACTION));
app.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
if (osMoPlugin != null) {
osMoPlugin.getService().connect(true);
if (osMoPlugin.getTracker() != null) {
osMoPlugin.getTracker().enableTracker();
}
app.startNavigationService(NavigationService.USED_BY_LIVE, 0);
}
}
}, new IntentFilter(OSMAND_START_SHARE_LOCATION_ACTION));
app.registerReceiver(new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
if (osMoPlugin != null) {
if (osMoPlugin.getTracker() != null) {
osMoPlugin.getTracker().disableTracker();
}
if (app.getNavigationService() != null) {
app.getNavigationService()
.stopIfNeeded(app, NavigationService.USED_BY_LIVE);
}
}
}
}, new IntentFilter(OSMAND_STOP_SHARE_LOCATION_ACTION));
}
@Override
public NotificationType getType() {
return NotificationType.OSMO;
@ -44,28 +111,63 @@ public class OsMoNotification extends OsmandNotification {
@Override
public boolean isEnabled() {
return OsmandPlugin.getEnabledPlugin(OsMoPlugin.class) != null;
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
return osMoPlugin != null && osMoPlugin.getService().isEnabled();
}
@Override
public Builder buildNotification() {
OsMoPlugin osMoPlugin = OsmandPlugin.getEnabledPlugin(OsMoPlugin.class);
if (osMoPlugin == null) {
return null;
}
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);
notificationTitle = app.getString(R.string.osmo_plugin_name);
notificationText = app.getString(R.string.osmo_service_running);
final Builder notificationBuilder = createBuilder()
.setContentTitle(notificationTitle)
.setStyle(new BigTextStyle().bigText(notificationText));
if (osMoPlugin.getService().isEnabled()) {
Intent stopServiceIntent = new Intent(OSMAND_STOP_OSMO_SERVICE_ACTION);
PendingIntent stopServicePendingIntent = PendingIntent.getBroadcast(app, 0, stopServiceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_action_rec_stop,
app.getString(R.string.shared_string_control_stop), stopServicePendingIntent);
if (osMoPlugin.getTracker() != null) {
if (osMoPlugin.getTracker().isEnabledTracker()) {
Intent stopShareLocatiponIntent = new Intent(OSMAND_STOP_SHARE_LOCATION_ACTION);
PendingIntent stopShareLocatiponIntentPendingIntent = PendingIntent.getBroadcast(app, 0, stopShareLocatiponIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_action_remove_dark,
app.getString(R.string.osmo_pause_location), stopShareLocatiponIntentPendingIntent);
} else {
Intent startShareLocationIntent = new Intent(OSMAND_START_SHARE_LOCATION_ACTION);
PendingIntent startShareLocationPendingIntent = PendingIntent.getBroadcast(app, 0, startShareLocationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_action_gshare_dark,
app.getString(R.string.osmo_share_location), startShareLocationPendingIntent);
}
}
} else {
Intent startServiceIntent = new Intent(OSMAND_START_OSMO_SERVICE_ACTION);
PendingIntent startServicePendingIntent = PendingIntent.getBroadcast(app, 0, startServiceIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_play_dark,
app.getString(R.string.shared_string_control_start), startServicePendingIntent);
}
return notificationBuilder;
}
@Override
public int getUniqueId() {
return OSMO_NOTIFICATION_SERVICE_ID;

View file

@ -17,6 +17,7 @@ import net.osmand.plus.GPXUtilities;
import net.osmand.plus.GPXUtilities.GPXFile;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.GpxSelectionHelper.SelectedGpxFile;
import net.osmand.plus.NavigationService;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.OsmandPlugin;
import net.osmand.plus.R;
@ -94,6 +95,9 @@ public class OsMoPlugin extends OsmandPlugin implements OsMoReactor {
@Override
public void disable(OsmandApplication app) {
super.disable(app);
if (app.getNavigationService() != null) {
app.getNavigationService().stopIfNeeded(app, NavigationService.USED_BY_LIVE);
}
tracker.disableTracker();
service.disconnect();
}

View file

@ -21,6 +21,8 @@ import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandApplication;
import net.osmand.plus.R;
import net.osmand.plus.Version;
import net.osmand.plus.notifications.OsmandNotification;
import net.osmand.plus.notifications.OsmandNotification.NotificationType;
import org.apache.commons.logging.Log;
import org.json.JSONArray;
@ -174,6 +176,7 @@ public class OsMoService implements OsMoReactor {
}
thread = new OsMoThread(this);
enabled = true;
app.getNotificationHelper().refreshNotification(NotificationType.OSMO);
return true;
}
@ -186,6 +189,7 @@ public class OsMoService implements OsMoReactor {
enabled = false;
thread.stopConnection();
app.getSettings().OSMO_LAST_PING.set(0l);
app.getNotificationHelper().refreshNotification(NotificationType.OSMO);
}
}

View file

@ -3,6 +3,8 @@ package net.osmand.plus.osmo;
import net.osmand.Location;
import net.osmand.PlatformUtil;
import net.osmand.plus.OsmandSettings.OsmandPreference;
import net.osmand.plus.notifications.OsmandNotification;
import net.osmand.plus.notifications.OsmandNotification.NotificationType;
import net.osmand.plus.osmo.OsMoGroupsStorage.OsMoDevice;
import org.apache.commons.logging.Log;
@ -67,6 +69,7 @@ public class OsMoTracker implements OsMoReactor {
public void enableTracker() {
if (!isEnabledTracker()) {
enableTrackerCmd();
service.getMyApplication().getNotificationHelper().refreshNotification(NotificationType.OSMO);
}
}
@ -80,6 +83,7 @@ public class OsMoTracker implements OsMoReactor {
stateSendLocation.set(false);
service.pushCommand("TRACKER_SESSION_CLOSE");
}
service.getMyApplication().getNotificationHelper().refreshNotification(NotificationType.OSMO);
}
public void startTrackingId(OsMoDevice d) {