Skip to content

Commit

Permalink
Update MapboxNavigationNotification to consider 24 hour time formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
danesfeder committed Jul 18, 2018
1 parent f52ebe7 commit bebc787
Showing 1 changed file with 19 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.SpannableString;
import android.text.format.DateFormat;
import android.widget.RemoteViews;

import com.mapbox.api.directions.v5.models.LegStep;
Expand All @@ -20,30 +21,30 @@
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
import com.mapbox.services.android.navigation.v5.utils.DistanceUtils;
import com.mapbox.services.android.navigation.v5.utils.ManeuverUtils;
import com.mapbox.services.android.navigation.v5.utils.time.TimeFormatter;

import java.util.Locale;
import java.util.Calendar;

import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_NOTIFICATION_CHANNEL;
import static com.mapbox.services.android.navigation.v5.navigation.NavigationConstants.NAVIGATION_NOTIFICATION_ID;
import static com.mapbox.services.android.navigation.v5.utils.time.TimeFormatter.formatTime;

/**
* This is in charge of creating the persistent navigation session notification and updating it.
*/
class MapboxNavigationNotification implements NavigationNotification {

private static final String END_NAVIGATION_ACTION = "com.mapbox.intent.action.END_NAVIGATION";
private final DistanceUtils distanceUtils;
private final String etaFormat;
private NotificationCompat.Builder notificationBuilder;
private NotificationManager notificationManager;
private Notification notification;
private RemoteViews collapsedNotificationRemoteViews;
private RemoteViews expandedNotificationRemoteViews;
private MapboxNavigation mapboxNavigation;
private SpannableString currentDistanceText;
private String currentArrivalTime;
private DistanceUtils distanceUtils;
private String instructionText;
private int currentManeuverId;
private boolean isTwentyFourHourFormat;

private BroadcastReceiver endNavigationBtnReceiver = new BroadcastReceiver() {
@Override
Expand All @@ -53,12 +54,7 @@ public void onReceive(final Context context, final Intent intent) {
};

MapboxNavigationNotification(Context context, MapboxNavigation mapboxNavigation) {
this.mapboxNavigation = mapboxNavigation;
RouteOptions routeOptions = mapboxNavigation.getRoute().routeOptions();
this.distanceUtils = new DistanceUtils(
context, routeOptions.language(), routeOptions.voiceUnits());
etaFormat = context.getString(R.string.eta_format);
initialize(context);
initialize(context, mapboxNavigation);
}

@Override
Expand All @@ -85,8 +81,12 @@ void unregisterReceiver(Context context) {
}
}

private void initialize(Context context) {
private void initialize(Context context, MapboxNavigation mapboxNavigation) {
this.mapboxNavigation = mapboxNavigation;
RouteOptions routeOptions = mapboxNavigation.getRoute().routeOptions();
distanceUtils = new DistanceUtils(context, routeOptions.language(), routeOptions.voiceUnits());
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
isTwentyFourHourFormat = DateFormat.is24HourFormat(context);
createNotificationChannel(context);
buildNotification(context);
registerReceiver(context);
Expand Down Expand Up @@ -139,17 +139,12 @@ private void registerReceiver(Context context) {
* @param routeProgress the latest RouteProgress object
*/
private void updateNotificationViews(RouteProgress routeProgress) {
// Instruction
updateInstructionText(routeProgress.currentLegProgress().currentStep());
// Distance
updateDistanceText(routeProgress);
// Arrival Time
updateArrivalTime(routeProgress);
// Get upcoming step for maneuver image - current step if null
LegStep step = routeProgress.currentLegProgress().upComingStep() != null
? routeProgress.currentLegProgress().upComingStep()
: routeProgress.currentLegProgress().currentStep();
// Maneuver Image
updateManeuverImage(step);

notificationManager.notify(NAVIGATION_NOTIFICATION_ID, notificationBuilder.build());
Expand Down Expand Up @@ -187,17 +182,13 @@ private boolean newDistanceText(RouteProgress routeProgress) {
}

private void updateArrivalTime(RouteProgress routeProgress) {
if (currentArrivalTime == null || newArrivalTime(routeProgress)) {
currentArrivalTime = TimeFormatter.formatArrivalTime(routeProgress.durationRemaining());
String formattedArrivalText = String.format(Locale.getDefault(), etaFormat, currentArrivalTime);
collapsedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, formattedArrivalText);
expandedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, formattedArrivalText);
}
}

private boolean newArrivalTime(RouteProgress routeProgress) {
return currentArrivalTime != null && !currentArrivalTime.equals(TimeFormatter
.formatArrivalTime(routeProgress.durationRemaining()));
MapboxNavigationOptions options = mapboxNavigation.options();
Calendar time = Calendar.getInstance();
double durationRemaining = routeProgress.durationRemaining();
int timeFormatType = options.timeFormatType();
String arrivalTime = formatTime(time, durationRemaining, timeFormatType, isTwentyFourHourFormat);
collapsedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, arrivalTime);
expandedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, arrivalTime);
}

private void updateManeuverImage(LegStep step) {
Expand Down

0 comments on commit bebc787

Please sign in to comment.