Skip to content

Commit 06273e6

Browse files
author
Guardiola31337
committed
fix notification and banner etas not in sync
1 parent b65953a commit 06273e6

File tree

2 files changed

+74
-38
lines changed

2 files changed

+74
-38
lines changed

libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/navigation/MapboxNavigationNotification.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ public void onReceive(final Context applicationContext, final Intent intent) {
6464
initialize(applicationContext, mapboxNavigation);
6565
}
6666

67+
// For testing only
68+
MapboxNavigationNotification(Context applicationContext, MapboxNavigation mapboxNavigation,
69+
Notification notification) {
70+
this.applicationContext = applicationContext;
71+
this.notification = notification;
72+
initialize(applicationContext, mapboxNavigation);
73+
}
74+
6775
@Override
6876
public Notification getNotification() {
6977
return notification;
@@ -77,13 +85,24 @@ public int getNotificationId() {
7785
@Override
7886
public void updateNotification(RouteProgress routeProgress) {
7987
updateNotificationViews(routeProgress);
88+
rebuildNotification();
8089
}
8190

8291
@Override
8392
public void onNavigationStopped(Context applicationContext) {
8493
unregisterReceiver(applicationContext);
8594
}
8695

96+
// Package private (no modifier) for testing purposes
97+
String generateArrivalTime(RouteProgress routeProgress, Calendar time) {
98+
MapboxNavigationOptions options = mapboxNavigation.options();
99+
double legDurationRemaining = routeProgress.currentLegProgress().durationRemaining();
100+
int timeFormatType = options.timeFormatType();
101+
String arrivalTime = formatTime(time, legDurationRemaining, timeFormatType, isTwentyFourHourFormat);
102+
String formattedArrivalTime = String.format(etaFormat, arrivalTime);
103+
return formattedArrivalTime;
104+
}
105+
87106
private void initialize(Context applicationContext, MapboxNavigation mapboxNavigation) {
88107
this.mapboxNavigation = mapboxNavigation;
89108
etaFormat = applicationContext.getString(R.string.eta_format);
@@ -96,7 +115,9 @@ private void initialize(Context applicationContext, MapboxNavigation mapboxNavig
96115

97116
registerReceiver(applicationContext);
98117
createNotificationChannel(applicationContext);
99-
notification = buildNotification(applicationContext);
118+
if (notification == null) {
119+
notification = buildNotification(applicationContext);
120+
}
100121
}
101122

102123
private void initializeDistanceFormatter(Context applicationContext, MapboxNavigation mapboxNavigation) {
@@ -172,12 +193,16 @@ private void updateNotificationViews(RouteProgress routeProgress) {
172193
buildRemoteViews();
173194
updateInstructionText(routeProgress.currentLegProgress().currentStep());
174195
updateDistanceText(routeProgress);
175-
updateArrivalTime(routeProgress);
196+
Calendar time = Calendar.getInstance();
197+
String formattedArrivalTime = generateArrivalTime(routeProgress, time);
198+
updateViewsWith(formattedArrivalTime);
176199
LegStep step = routeProgress.currentLegProgress().upComingStep() != null
177200
? routeProgress.currentLegProgress().upComingStep()
178201
: routeProgress.currentLegProgress().currentStep();
179202
updateManeuverImage(step);
203+
}
180204

205+
private void rebuildNotification() {
181206
notification = buildNotification(applicationContext);
182207
notificationManager.notify(NAVIGATION_NOTIFICATION_ID, notification);
183208
}
@@ -222,13 +247,7 @@ private boolean newDistanceText(RouteProgress routeProgress) {
222247
routeProgress.currentLegProgress().currentStepProgress().distanceRemaining()).toString());
223248
}
224249

225-
private void updateArrivalTime(RouteProgress routeProgress) {
226-
MapboxNavigationOptions options = mapboxNavigation.options();
227-
Calendar time = Calendar.getInstance();
228-
double durationRemaining = routeProgress.durationRemaining();
229-
int timeFormatType = options.timeFormatType();
230-
String arrivalTime = formatTime(time, durationRemaining, timeFormatType, isTwentyFourHourFormat);
231-
String formattedArrivalTime = String.format(etaFormat, arrivalTime);
250+
private void updateViewsWith(String formattedArrivalTime) {
232251
collapsedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, formattedArrivalTime);
233252
expandedNotificationRemoteViews.setTextViewText(R.id.notificationArrivalText, formattedArrivalTime);
234253
}
Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.mapbox.services.android.navigation.v5.navigation;
22

3-
import android.app.NotificationManager;
3+
import android.app.Notification;
44
import android.content.Context;
5+
import android.content.pm.PackageManager;
6+
import android.content.res.Configuration;
7+
import android.content.res.Resources;
58

69
import com.google.gson.Gson;
710
import com.google.gson.GsonBuilder;
@@ -11,21 +14,21 @@
1114
import com.mapbox.services.android.navigation.v5.BaseTest;
1215
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress;
1316

14-
import junit.framework.Assert;
15-
1617
import org.junit.Before;
17-
import org.junit.Ignore;
1818
import org.junit.Test;
19-
import org.mockito.Mock;
20-
import org.mockito.Mockito;
2119

22-
public class MapboxNavigationNotificationTest extends BaseTest {
20+
import java.util.Calendar;
21+
import java.util.Date;
22+
import java.util.Locale;
2323

24-
private static final String DIRECTIONS_ROUTE_FIXTURE = "directions_v5_precision_6.json";
24+
import static org.junit.Assert.assertEquals;
25+
import static org.mockito.ArgumentMatchers.anyInt;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
2528

26-
@Mock
27-
NotificationManager notificationManager;
29+
public class MapboxNavigationNotificationTest extends BaseTest {
2830

31+
private static final String DIRECTIONS_ROUTE_FIXTURE = "directions_v5_precision_6.json";
2932
private DirectionsRoute route;
3033

3134
@Before
@@ -37,28 +40,42 @@ public void setUp() throws Exception {
3740
route = response.routes().get(0);
3841
}
3942

40-
@Ignore
4143
@Test
42-
public void sanity() throws Exception {
43-
MapboxNavigationNotification mapboxNavigationNotification = new MapboxNavigationNotification(
44-
Mockito.mock(Context.class), Mockito.mock(MapboxNavigation.class));
45-
Assert.assertNotNull(mapboxNavigationNotification);
46-
}
44+
public void checksArrivalTime() throws Exception {
45+
MapboxNavigation mockedMapboxNavigation = createMapboxNavigation();
46+
Context mockedContext = createContext();
47+
Notification mockedNotification = mock(Notification.class);
48+
MapboxNavigationNotification mapboxNavigationNotification = new MapboxNavigationNotification(mockedContext,
49+
mockedMapboxNavigation, mockedNotification);
50+
RouteProgress routeProgress = buildDefaultTestRouteProgress();
51+
Calendar mockedTime = Calendar.getInstance();
52+
long aprilFifteenThreeFourtyFourFiftyThreePmTwoThousandNineteen = 1555357493308L;
53+
mockedTime.setTime(new Date(aprilFifteenThreeFourtyFourFiftyThreePmTwoThousandNineteen));
4754

48-
@Ignore
49-
@Test
50-
public void updateDefaultNotification_onlyUpdatesNameWhenNew() throws Exception {
51-
RouteProgress routeProgress = RouteProgress.builder()
52-
.directionsRoute(route)
53-
.stepIndex(0)
54-
.legIndex(0)
55-
.build();
55+
String formattedArrivalTime = mapboxNavigationNotification.generateArrivalTime(routeProgress, mockedTime);
5656

57-
MapboxNavigationNotification mapboxNavigationNotification = new MapboxNavigationNotification(
58-
Mockito.mock(Context.class), Mockito.mock(MapboxNavigation.class));
57+
assertEquals("4:46 pm ETA", formattedArrivalTime);
58+
}
59+
60+
private MapboxNavigation createMapboxNavigation() {
61+
MapboxNavigation mockedMapboxNavigation = mock(MapboxNavigation.class);
62+
when(mockedMapboxNavigation.getRoute()).thenReturn(route);
63+
MapboxNavigationOptions mockedMapboxNavigationOptions = mock(MapboxNavigationOptions.class);
64+
when(mockedMapboxNavigation.options()).thenReturn(mockedMapboxNavigationOptions);
65+
when(mockedMapboxNavigationOptions.roundingIncrement()).thenReturn(NavigationConstants.ROUNDING_INCREMENT_FIVE);
66+
return mockedMapboxNavigation;
67+
}
5968

60-
mapboxNavigationNotification.updateNotification(routeProgress);
61-
// notificationManager.getActiveNotifications()[0].getNotification().contentView;
62-
// verify(notificationManager, times(1)).getActiveNotifications()[0];
69+
private Context createContext() {
70+
Context mockedContext = mock(Context.class);
71+
Configuration mockedConfiguration = new Configuration();
72+
mockedConfiguration.locale = new Locale("en");
73+
Resources mockedResources = mock(Resources.class);
74+
when(mockedContext.getResources()).thenReturn(mockedResources);
75+
when(mockedResources.getConfiguration()).thenReturn(mockedConfiguration);
76+
PackageManager mockedPackageManager = mock(PackageManager.class);
77+
when(mockedContext.getPackageManager()).thenReturn(mockedPackageManager);
78+
when(mockedContext.getString(anyInt())).thenReturn("%s ETA");
79+
return mockedContext;
6380
}
6481
}

0 commit comments

Comments
 (0)