diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java index e869fa40f..975402c80 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTask.java @@ -53,11 +53,6 @@ public class StatusAnnouncerTask implements PeriodicTask { */ private final Context context; - /** - * String utilities. - */ - private final StringUtils stringUtils; - /** * The interface to the text to speech engine. */ @@ -93,14 +88,8 @@ public class StatusAnnouncerTask implements PeriodicTask { } }; - public StatusAnnouncerTask(Context context) { - this(context, new StringUtils(context)); - } - - public StatusAnnouncerTask(Context context, StringUtils stringUtils) { this.context = context; - this.stringUtils = stringUtils; } /** @@ -206,7 +195,7 @@ public class StatusAnnouncerTask implements PeriodicTask { double pace = 3600000.0 / s; Log.w(Constants.TAG, "Converted speed: " + s + " to pace: " + pace); - speed = stringUtils.formatTimeLong((long) pace); + speed = getAnnounceTime((long) pace); } } @@ -216,7 +205,7 @@ public class StatusAnnouncerTask implements PeriodicTask { context.getString(metricUnits ? R.string.voice_kilometers : R.string.voice_miles), - stringUtils.formatTimeLong(stats.getMovingTime()), + getAnnounceTime(stats.getMovingTime()), speed, context.getString(speedLabel)); } @@ -333,4 +322,37 @@ public class StatusAnnouncerTask implements PeriodicTask { public static int getVolumeStream() { return TextToSpeech.Engine.DEFAULT_STREAM; } + + /** + * Gets a string to announce the time. + * + * @param time the time + */ + private String getAnnounceTime(long time) { + int[] parts = StringUtils.getTimeParts(time); + String secLabel = + context.getString(parts[0] == 1 ? R.string.voice_second : R.string.voice_seconds); + String minLabel = + context.getString(parts[1] == 1 ? R.string.voice_minute : R.string.voice_minutes); + String hourLabel = + context.getString(parts[2] == 1 ? R.string.voice_hour : R.string.voice_hours); + + StringBuilder sb = new StringBuilder(); + if (parts[2] != 0) { + sb.append(parts[2]); + sb.append(" "); + sb.append(hourLabel); + sb.append(" "); + sb.append(parts[1]); + sb.append(minLabel); + } else { + sb.append(parts[1]); + sb.append(" "); + sb.append(minLabel); + sb.append(" "); + sb.append(parts[0]); + sb.append(secLabel); + } + return sb.toString(); + } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/util/StringUtils.java b/MyTracks/src/com/google/android/apps/mytracks/util/StringUtils.java index cb55b0289..e28445469 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/util/StringUtils.java +++ b/MyTracks/src/com/google/android/apps/mytracks/util/StringUtils.java @@ -221,39 +221,6 @@ public class StringUtils implements DescriptionGenerator { this.context = context; } - /** - * Formats a time for voice announcement. - * - * @param time the time - */ - public String formatTimeLong(long time) { - int[] parts = getTimeParts(time); - String secLabel = - context.getString(parts[0] == 1 ? R.string.voice_second : R.string.voice_seconds); - String minLabel = - context.getString(parts[1] == 1 ? R.string.voice_minute : R.string.voice_minutes); - String hourLabel = - context.getString(parts[2] == 1 ? R.string.voice_hour : R.string.voice_hours); - - StringBuilder sb = new StringBuilder(); - if (parts[2] != 0) { - sb.append(parts[2]); - sb.append(" "); - sb.append(hourLabel); - sb.append(" "); - sb.append(parts[1]); - sb.append(minLabel); - } else { - sb.append(parts[1]); - sb.append(" "); - sb.append(minLabel); - sb.append(" "); - sb.append(parts[0]); - sb.append(secLabel); - } - return sb.toString(); - } - /** * Generates a description for a track (with information about the * statistics). diff --git a/MyTracksTest/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTaskTest.java b/MyTracksTest/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTaskTest.java index 5d0bc0c87..4f7c2e600 100644 --- a/MyTracksTest/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTaskTest.java +++ b/MyTracksTest/src/com/google/android/apps/mytracks/services/tasks/StatusAnnouncerTaskTest.java @@ -20,7 +20,6 @@ import static com.google.android.testing.mocking.AndroidMock.eq; import static com.google.android.testing.mocking.AndroidMock.expect; import static com.google.android.testing.mocking.AndroidMock.same; -import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask; import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.testing.mocking.AndroidMock; @@ -55,7 +54,6 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { private Locale oldDefaultLocale; private StatusAnnouncerTask task; - private StringUtils stringUtils; private StatusAnnouncerTask mockTask; private Capture initListenerCapture; private Capture phoneListenerCapture; @@ -263,7 +261,6 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { oldDefaultLocale = Locale.getDefault(); Locale.setDefault(DEFAULT_LOCALE); - stringUtils = AndroidMock.createMock(StringUtils.class, getContext()); // Eww, the effort required just to mock TextToSpeech is insane final AtomicBoolean listenerCalled = new AtomicBoolean(); @@ -294,9 +291,8 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { phoneListenerCapture = new Capture(); // Create a partial forwarding mock - mockTask = AndroidMock.createMock(StatusAnnouncerTask.class, - getContext(), stringUtils); - task = new StatusAnnouncerTask(getContext(), stringUtils) { + mockTask = AndroidMock.createMock(StatusAnnouncerTask.class, getContext()); + task = new StatusAnnouncerTask(getContext()) { @Override protected TextToSpeech newTextToSpeech(Context ctx, OnInitListener onInitListener) { @@ -333,11 +329,11 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { expect(tts.setSpeechRate(StatusAnnouncerTask.TTS_SPEECH_RATE)) .andReturn(TextToSpeech.SUCCESS); - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); ttsInitListener.onInit(TextToSpeech.SUCCESS); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testStart_languageNotSupported() { @@ -352,11 +348,11 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { expect(tts.setSpeechRate(StatusAnnouncerTask.TTS_SPEECH_RATE)) .andReturn(TextToSpeech.SUCCESS); - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); ttsInitListener.onInit(TextToSpeech.SUCCESS); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testStart_notReady() { @@ -364,11 +360,11 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { OnInitListener ttsInitListener = initListenerCapture.getValue(); assertNotNull(ttsInitListener); - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); ttsInitListener.onInit(TextToSpeech.ERROR); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testShutdown() { @@ -382,9 +378,9 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { mockTask.listenToPhoneState( same(phoneListener), eq(PhoneStateListener.LISTEN_NONE)); tts.shutdown(); - AndroidMock.replay(mockTask, tts, stringUtils); + AndroidMock.replay(mockTask, tts); task.shutdown(); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun() throws Exception { @@ -404,9 +400,9 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { .andReturn(TextToSpeech.SUCCESS); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); task.runWithStatistics(stats); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_notReady() throws Exception { @@ -414,9 +410,9 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { startTask(TextToSpeech.ERROR); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_duringCall() throws Exception { @@ -425,11 +421,11 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { expect(tts.isSpeaking()).andStubReturn(false); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_OFFHOOK, null); task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_ringWhileSpeaking() throws Exception { @@ -438,7 +434,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { expect(tts.isSpeaking()).andStubReturn(true); expect(tts.stop()).andReturn(TextToSpeech.SUCCESS); - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); // Update the state to ringing - this should stop the current announcement. PhoneStateListener phoneListener = phoneListenerCapture.getValue(); @@ -447,7 +443,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { // Run the announcement - this should do nothing. task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_whileRinging() throws Exception { @@ -456,20 +452,20 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { expect(tts.isSpeaking()).andStubReturn(false); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); PhoneStateListener phoneListener = phoneListenerCapture.getValue(); phoneListener.onCallStateChanged(TelephonyManager.CALL_STATE_RINGING, null); task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_noService() throws Exception { startTask(TextToSpeech.SUCCESS); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); task.run(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } public void testRun_noStats() throws Exception { @@ -478,18 +474,18 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase { startTask(TextToSpeech.SUCCESS); // Run the announcement - AndroidMock.replay(tts, stringUtils); + AndroidMock.replay(tts); task.runWithStatistics(null); - AndroidMock.verify(mockTask, tts, stringUtils); + AndroidMock.verify(mockTask, tts); } private void startTask(int state) { - AndroidMock.resetToNice(tts, stringUtils); - AndroidMock.replay(tts, stringUtils); + AndroidMock.resetToNice(tts); + AndroidMock.replay(tts); doStart(); OnInitListener ttsInitListener = initListenerCapture.getValue(); ttsInitListener.onInit(state); - AndroidMock.resetToDefault(tts, stringUtils); + AndroidMock.resetToDefault(tts); } private void doStart() {