merge heads

This commit is contained in:
Jimmy Shih
2012-08-09 10:22:52 -07:00
12 changed files with 80 additions and 63 deletions
@@ -32,9 +32,9 @@ import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.content.WaypointCreationRequest.WaypointType; import com.google.android.apps.mytracks.content.WaypointCreationRequest.WaypointType;
import com.google.android.apps.mytracks.services.sensors.SensorManager; import com.google.android.apps.mytracks.services.sensors.SensorManager;
import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory; import com.google.android.apps.mytracks.services.sensors.SensorManagerFactory;
import com.google.android.apps.mytracks.services.tasks.AnnouncementPeriodicTaskFactory;
import com.google.android.apps.mytracks.services.tasks.PeriodicTaskExecutor; import com.google.android.apps.mytracks.services.tasks.PeriodicTaskExecutor;
import com.google.android.apps.mytracks.services.tasks.SplitTask; import com.google.android.apps.mytracks.services.tasks.SplitPeriodicTaskFactory;
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerFactory;
import com.google.android.apps.mytracks.stats.TripStatistics; import com.google.android.apps.mytracks.stats.TripStatistics;
import com.google.android.apps.mytracks.stats.TripStatisticsBuilder; import com.google.android.apps.mytracks.stats.TripStatisticsBuilder;
import com.google.android.apps.mytracks.util.IntentUtils; import com.google.android.apps.mytracks.util.IntentUtils;
@@ -277,8 +277,8 @@ public class TrackRecordingService extends Service {
context = this; context = this;
myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this); myTracksProviderUtils = MyTracksProviderUtils.Factory.get(this);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
announcementExecutor = new PeriodicTaskExecutor(this, new StatusAnnouncerFactory()); announcementExecutor = new PeriodicTaskExecutor(this, new AnnouncementPeriodicTaskFactory());
splitExecutor = new PeriodicTaskExecutor(this, new SplitTask.Factory()); splitExecutor = new PeriodicTaskExecutor(this, new SplitPeriodicTaskFactory());
executorService = Executors.newSingleThreadExecutor(); executorService = Executors.newSingleThreadExecutor();
sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE); sharedPreferences = getSharedPreferences(Constants.SETTINGS_NAME, Context.MODE_PRIVATE);
sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener); sharedPreferences.registerOnSharedPreferenceChangeListener(sharedPreferenceChangeListener);
@@ -38,7 +38,7 @@ import java.util.Locale;
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class StatusAnnouncerTask implements PeriodicTask { public class AnnouncementPeriodicTask implements PeriodicTask {
/** /**
* The rate at which announcements are spoken. * The rate at which announcements are spoken.
@@ -46,7 +46,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
@VisibleForTesting @VisibleForTesting
static final float TTS_SPEECH_RATE = 0.9f; static final float TTS_SPEECH_RATE = 0.9f;
private static final String TAG = StatusAnnouncerTask.class.getSimpleName(); private static final String TAG = AnnouncementPeriodicTask.class.getSimpleName();
private static final long HOUR_TO_MILLISECOND = 60 * 60 * 1000; private static final long HOUR_TO_MILLISECOND = 60 * 60 * 1000;
private final Context context; private final Context context;
@@ -75,7 +75,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
} }
}; };
public StatusAnnouncerTask(Context context) { public AnnouncementPeriodicTask(Context context) {
this.context = context; this.context = context;
} }
@@ -20,15 +20,14 @@ import com.google.android.apps.mytracks.util.ApiAdapterFactory;
import android.content.Context; import android.content.Context;
/** /**
* Factory which wraps construction and setup of text-to-speech announcements in * A {@link PeriodicTaskFactory} for text-to-speech announcement periodic task.
* an API-level-safe way. *
*
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class StatusAnnouncerFactory implements PeriodicTaskFactory { public class AnnouncementPeriodicTaskFactory implements PeriodicTaskFactory {
@Override @Override
public PeriodicTask create(Context context) { public PeriodicTask create(Context context) {
return ApiAdapterFactory.getApiAdapter().getStatusAnnouncerTask(context); return ApiAdapterFactory.getApiAdapter().getAnnouncementPeriodicTask(context);
} }
} }
@@ -32,9 +32,9 @@ import java.util.HashMap;
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
@TargetApi(8) @TargetApi(8)
public class Api8StatusAnnouncerTask extends StatusAnnouncerTask { public class Api8AnnouncementPeriodicTask extends AnnouncementPeriodicTask {
private static final String TAG = Api8StatusAnnouncerTask.class.getSimpleName(); private static final String TAG = Api8AnnouncementPeriodicTask.class.getSimpleName();
private static final HashMap<String, String> SPEECH_PARAMS = new HashMap<String, String>(); private static final HashMap<String, String> SPEECH_PARAMS = new HashMap<String, String>();
static { static {
SPEECH_PARAMS.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "not_used"); SPEECH_PARAMS.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, "not_used");
@@ -53,7 +53,7 @@ public class Api8StatusAnnouncerTask extends StatusAnnouncerTask {
private final AudioManager audioManager; private final AudioManager audioManager;
public Api8StatusAnnouncerTask(Context context) { public Api8AnnouncementPeriodicTask(Context context) {
super(context); super(context);
audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
} }
@@ -19,16 +19,16 @@ package com.google.android.apps.mytracks.services.tasks;
import android.content.Context; import android.content.Context;
/** /**
* An interface for classes that can create periodic tasks. * An interface for classes that can create {@link PeriodicTask}.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public interface PeriodicTaskFactory { public interface PeriodicTaskFactory {
/** /**
* Creates a periodic task. * Creates a {@link PeriodicTask}.
* *
* @return the task, or null if task is not supported * @return the task, or null if the task is not supported
*/ */
public PeriodicTask create(Context context); public PeriodicTask create(Context context);
} }
@@ -19,36 +19,21 @@ package com.google.android.apps.mytracks.services.tasks;
import com.google.android.apps.mytracks.content.WaypointCreationRequest; import com.google.android.apps.mytracks.content.WaypointCreationRequest;
import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService;
import android.content.Context;
/** /**
* A simple task to insert statistics markers periodically. * A simple task to insert statistics markers periodically.
* *
* @author Sandor Dornbush * @author Sandor Dornbush
*/ */
public class SplitTask implements PeriodicTask { public class SplitPeriodicTask implements PeriodicTask {
private SplitTask() {}
@Override @Override
public void start() {} public void start() {}
@Override @Override
public void run(TrackRecordingService service) { public void run(TrackRecordingService trackRecordingService) {
service.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS); trackRecordingService.insertWaypoint(WaypointCreationRequest.DEFAULT_STATISTICS);
} }
@Override @Override
public void shutdown() {} public void shutdown() {}
/**
* Create new {@link SplitTask}.
*/
public static class Factory implements PeriodicTaskFactory {
@Override
public PeriodicTask create(Context context) {
return new SplitTask();
}
}
} }
@@ -0,0 +1,32 @@
/*
* Copyright 2012 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.google.android.apps.mytracks.services.tasks;
import android.content.Context;
/**
* A {@link PeriodicTaskFactory} for {@link SplitPeriodicTask}.
*
* @author Jimmy Shih
*/
public class SplitPeriodicTaskFactory implements PeriodicTaskFactory {
@Override
public PeriodicTask create(Context context) {
return new SplitPeriodicTask();
}
}
@@ -19,8 +19,8 @@ import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.ContextualActionModeCallback; import com.google.android.apps.mytracks.ContextualActionModeCallback;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener; import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener;
import com.google.android.apps.mytracks.services.sensors.BluetoothConnectionManager; import com.google.android.apps.mytracks.services.sensors.BluetoothConnectionManager;
import com.google.android.apps.mytracks.services.tasks.AnnouncementPeriodicTask;
import com.google.android.apps.mytracks.services.tasks.PeriodicTask; import com.google.android.apps.mytracks.services.tasks.PeriodicTask;
import com.google.android.apps.mytracks.services.tasks.StatusAnnouncerTask;
import com.google.api.client.http.HttpTransport; import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.apache.ApacheHttpTransport; import com.google.api.client.http.apache.ApacheHttpTransport;
@@ -50,8 +50,8 @@ import java.util.List;
public class Api7Adapter implements ApiAdapter { public class Api7Adapter implements ApiAdapter {
@Override @Override
public PeriodicTask getStatusAnnouncerTask(Context context) { public PeriodicTask getAnnouncementPeriodicTask(Context context) {
return new StatusAnnouncerTask(context); return new AnnouncementPeriodicTask(context);
} }
@Override @Override
@@ -15,9 +15,9 @@
*/ */
package com.google.android.apps.mytracks.util; package com.google.android.apps.mytracks.util;
import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener;
import com.google.android.apps.mytracks.io.backup.Api8BackupPreferencesListener; import com.google.android.apps.mytracks.io.backup.Api8BackupPreferencesListener;
import com.google.android.apps.mytracks.services.tasks.Api8StatusAnnouncerTask; import com.google.android.apps.mytracks.io.backup.BackupPreferencesListener;
import com.google.android.apps.mytracks.services.tasks.Api8AnnouncementPeriodicTask;
import com.google.android.apps.mytracks.services.tasks.PeriodicTask; import com.google.android.apps.mytracks.services.tasks.PeriodicTask;
import android.content.Context; import android.content.Context;
@@ -30,8 +30,8 @@ import android.content.Context;
public class Api8Adapter extends Api7Adapter { public class Api8Adapter extends Api7Adapter {
@Override @Override
public PeriodicTask getStatusAnnouncerTask(Context context) { public PeriodicTask getAnnouncementPeriodicTask(Context context) {
return new Api8StatusAnnouncerTask(context); return new Api8AnnouncementPeriodicTask(context);
} }
@Override @Override
@@ -42,13 +42,13 @@ import java.util.List;
public interface ApiAdapter { public interface ApiAdapter {
/** /**
* Gets a status announcer task. * Gets an announcement periodic task.
* <p> * <p>
* Due to changes in API level 8. * Due to changes in API level 8.
* *
* @param context the context * @param context the context
*/ */
public PeriodicTask getStatusAnnouncerTask(Context context); public PeriodicTask getAnnouncementPeriodicTask(Context context);
/** /**
* Gets a {@link BackupPreferencesListener}. * Gets a {@link BackupPreferencesListener}.
@@ -13,20 +13,21 @@
* License for the specific language governing permissions and limitations under * License for the specific language governing permissions and limitations under
* the License. * the License.
*/ */
package com.google.android.apps.mytracks.services.tasks; package com.google.android.apps.mytracks.services.tasks;
import android.test.AndroidTestCase; import android.test.AndroidTestCase;
/** /**
* Tests for {@link StatusAnnouncerFactory}. * Tests for {@link AnnouncementPeriodicTaskFactory}.
* These tests require Donut+ to run. * These tests require Donut+ to run.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class StatusAnnouncerFactoryTest extends AndroidTestCase { public class AnnouncementPeriodicTaskFactoryTest extends AndroidTestCase {
public void testCreate() { public void testCreate() {
PeriodicTaskFactory factory = new StatusAnnouncerFactory(); PeriodicTaskFactory factory = new AnnouncementPeriodicTaskFactory();
PeriodicTask task = factory.create(getContext()); PeriodicTask task = factory.create(getContext());
assertTrue(task instanceof StatusAnnouncerTask); assertTrue(task instanceof AnnouncementPeriodicTask);
} }
} }
@@ -40,21 +40,21 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.easymock.Capture; import org.easymock.Capture;
/** /**
* Tests for {@link StatusAnnouncerTask}. * Tests for {@link AnnouncementPeriodicTask}.
* WARNING: I'm not responsible if your eyes start bleeding while reading this * WARNING: I'm not responsible if your eyes start bleeding while reading this
* code. You have been warned. It's still better than no test, though. * code. You have been warned. It's still better than no test, though.
* *
* @author Rodrigo Damazio * @author Rodrigo Damazio
*/ */
public class StatusAnnouncerTaskTest extends AndroidTestCase { public class AnnouncementPeriodicTaskTest extends AndroidTestCase {
// Use something other than our hardcoded value // Use something other than our hardcoded value
private static final Locale DEFAULT_LOCALE = Locale.KOREAN; private static final Locale DEFAULT_LOCALE = Locale.KOREAN;
private static final String ANNOUNCEMENT = "I can haz cheeseburger?"; private static final String ANNOUNCEMENT = "I can haz cheeseburger?";
private Locale oldDefaultLocale; private Locale oldDefaultLocale;
private StatusAnnouncerTask task; private AnnouncementPeriodicTask task;
private StatusAnnouncerTask mockTask; private AnnouncementPeriodicTask mockTask;
private Capture<OnInitListener> initListenerCapture; private Capture<OnInitListener> initListenerCapture;
private Capture<PhoneStateListener> phoneListenerCapture; private Capture<PhoneStateListener> phoneListenerCapture;
private TextToSpeechDelegate ttsDelegate; private TextToSpeechDelegate ttsDelegate;
@@ -252,7 +252,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
@UsesMocks({ @UsesMocks({
StatusAnnouncerTask.class, AnnouncementPeriodicTask.class,
StringUtils.class, StringUtils.class,
}) })
@Override @Override
@@ -291,8 +291,8 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
phoneListenerCapture = new Capture<PhoneStateListener>(); phoneListenerCapture = new Capture<PhoneStateListener>();
// Create a partial forwarding mock // Create a partial forwarding mock
mockTask = AndroidMock.createMock(StatusAnnouncerTask.class, getContext()); mockTask = AndroidMock.createMock(AnnouncementPeriodicTask.class, getContext());
task = new StatusAnnouncerTask(getContext()) { task = new AnnouncementPeriodicTask(getContext()) {
@Override @Override
protected TextToSpeech newTextToSpeech(Context ctx, protected TextToSpeech newTextToSpeech(Context ctx,
OnInitListener onInitListener) { OnInitListener onInitListener) {
@@ -369,7 +369,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
expect(tts.isLanguageAvailable(DEFAULT_LOCALE)).andStubReturn(TextToSpeech.LANG_AVAILABLE); expect(tts.isLanguageAvailable(DEFAULT_LOCALE)).andStubReturn(TextToSpeech.LANG_AVAILABLE);
expect(tts.setLanguage(DEFAULT_LOCALE)).andReturn(TextToSpeech.LANG_AVAILABLE); expect(tts.setLanguage(DEFAULT_LOCALE)).andReturn(TextToSpeech.LANG_AVAILABLE);
expect(tts.setSpeechRate(StatusAnnouncerTask.TTS_SPEECH_RATE)).andReturn(TextToSpeech.SUCCESS); expect(tts.setSpeechRate(AnnouncementPeriodicTask.TTS_SPEECH_RATE)).andReturn(TextToSpeech.SUCCESS);
// Expect actual announcement call // Expect actual announcement call
expect(tts.speak( expect(tts.speak(
@@ -458,7 +458,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with time zero. * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time zero.
*/ */
public void testGetAnnounceTime_time_zero() { public void testGetAnnounceTime_time_zero() {
long time = 0; // 0 seconds long time = 0; // 0 seconds
@@ -466,7 +466,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with time one. * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with time one.
*/ */
public void testGetAnnounceTime_time_one() { public void testGetAnnounceTime_time_one() {
long time = 1 * 1000; // 1 second long time = 1 * 1000; // 1 second
@@ -474,7 +474,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with singular * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular
* numbers with the hour unit. * numbers with the hour unit.
*/ */
public void testGetAnnounceTime_singular_has_hour() { public void testGetAnnounceTime_singular_has_hour() {
@@ -483,7 +483,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with plural numbers * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers
* with the hour unit. * with the hour unit.
*/ */
public void testGetAnnounceTime_plural_has_hour() { public void testGetAnnounceTime_plural_has_hour() {
@@ -492,7 +492,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with singular * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with singular
* numbers without the hour unit. * numbers without the hour unit.
*/ */
public void testGetAnnounceTime_singular_no_hour() { public void testGetAnnounceTime_singular_no_hour() {
@@ -501,7 +501,7 @@ public class StatusAnnouncerTaskTest extends AndroidTestCase {
} }
/** /**
* Tests {@link StatusAnnouncerTask#getAnnounceTime(long)} with plural numbers * Tests {@link AnnouncementPeriodicTask#getAnnounceTime(long)} with plural numbers
* without the hour unit. * without the hour unit.
*/ */
public void testGetAnnounceTime_plural_no_hour() { public void testGetAnnounceTime_plural_no_hour() {