Merging Matthew's fix for issue 137.

This commit is contained in:
Rodrigo Damazio
2010-11-18 16:41:21 -08:00
13 changed files with 176 additions and 169 deletions
@@ -31,10 +31,10 @@ import com.google.android.apps.mytracks.io.AuthManagerFactory;
import com.google.android.apps.mytracks.io.GpxImporter;
import com.google.android.apps.mytracks.io.SendToDocs;
import com.google.android.apps.mytracks.io.SendToMyMaps;
import com.google.android.apps.mytracks.io.SendToMyMaps.OnSendCompletedListener;
import com.google.android.apps.mytracks.io.TempFileCleaner;
import com.google.android.apps.mytracks.io.TrackWriter;
import com.google.android.apps.mytracks.io.TrackWriterFactory;
import com.google.android.apps.mytracks.io.SendToMyMaps.OnSendCompletedListener;
import com.google.android.apps.mytracks.io.TrackWriterFactory.TrackFileFormat;
import com.google.android.apps.mytracks.services.ITrackRecordingService;
import com.google.android.apps.mytracks.services.StatusAnnouncerFactory;
@@ -173,8 +173,8 @@ public class MyTracks extends TabActivity implements OnTouchListener,
ITrackRecordingService trackRecordingService =
ITrackRecordingService.Stub.asInterface(service);
try {
// TODO: Send a start service intent and broadcast service started message
// to avoid the hack below and a race condition.
// TODO: Send a start service intent and broadcast service started
// message to avoid the hack below and a race condition.
if (startNewTrackRequested) {
startNewTrackRequested = false;
startRecordingNewTrack(trackRecordingService);
@@ -237,9 +237,8 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
private boolean isRecordingBasedOnSharedPreferences() {
// TrackRecordingServices guarantees that recordingTrackId is set to
// TrackRecordingService guarantees that recordingTrackId is set to
// -1 if the track has been stopped.
// TODO: Refresh recordingTrackId.
return recordingTrackId >= 0;
}
@@ -256,6 +255,7 @@ public class MyTracks extends TabActivity implements OnTouchListener,
providerUtils = MyTracksProviderUtils.Factory.get(this);
menuManager = new MenuManager(this);
sharedPreferences = getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
dialogManager = new DialogManager(this);
// The volume we want to control is the Text-To-Speech volume
int volumeStream =
@@ -359,7 +359,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
// to the user.
Log.d(MyTracksConstants.TAG, "MyTracks.onResume");
tryBindTrackRecordingService();
dialogManager = new DialogManager(this);
super.onResume();
}
@@ -693,58 +692,18 @@ public class MyTracks extends TabActivity implements OnTouchListener,
}
@Override
public void onSharedPreferenceChanged(
SharedPreferences sharedPreferences, String key) {
// The service itself cannot listen to changes (not supported by Android for
// services that run in a separate process). So we'll notify it manually:
if (key != null && trackRecordingService != null) {
try {
trackRecordingService.sharedPreferenceChanged(key);
} catch (RemoteException e) {
Log.w(MyTracksConstants.TAG,
"MyTracks: Cannot notify track recording service of changes "
+ "to shared preferences: ", e);
}
}
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key != null && key.equals(getString(R.string.selected_track_key))) {
selectedTrackId =
sharedPreferences.getLong(getString(R.string.selected_track_key), -1);
selectedTrackId = sharedPreferences.getLong(
getString(R.string.selected_track_key), -1);
}
if (key != null && key.equals(getString(R.string.recording_track_key))) {
recordingTrackId = sharedPreferences.getLong(
getString(R.string.recording_track_key), -1);
}
}
/**
* Simulates the recording of a random location.
* This is for debugging and testing only. Useful if there is no GPS signal
* available.
*/
// public void recordRandomLocation() {
// if (trackRecordingService != null) {
// Location loc = new Location("gps");
// double latitude = 37.5 + random.nextDouble() / 1000;
// double longitude = -120.0 + random.nextDouble() / 1000;
// loc.setLatitude(latitude);
// loc.setLongitude(longitude);
// loc.setAltitude(random.nextDouble() * 100);
// loc.setTime(System.currentTimeMillis());
// loc.setSpeed(random.nextFloat());
// MyTracksMap map =
// (MyTracksMap) getLocalActivityManager().getActivity("tab1");
// if (map != null) {
// map.onLocationChanged(loc);
// }
// StatsActivity stats =
// (StatsActivity) getLocalActivityManager().getActivity("tab2");
// if (stats != null) {
// stats.onLocationChanged(loc);
// }
// try {
// trackRecordingService.recordLocation(loc);
// } catch (RemoteException e) {
// Log.e(MyTracksConstants.TAG, "MyTracks", e);
// }
// }
// }
/**
* Resets status information for sending to MyMaps/Docs.
*/
@@ -1053,8 +1012,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
ITrackRecordingService trackRecordingService) {
try {
recordingTrackId = trackRecordingService.startNewTrack();
// TODO: This is a hack to propagate recordingTrackId in multiprocess env.
setRecordingTrackId(recordingTrackId);
// Select the recording track.
setSelectedTrackId(recordingTrackId);
Toast.makeText(this, getString(R.string.status_now_recording),
@@ -1096,8 +1053,6 @@ public class MyTracks extends TabActivity implements OnTouchListener,
Intent intent = new Intent(MyTracks.this, MyTracksDetails.class);
intent.putExtra("trackid", recordingTrackId);
intent.putExtra("hasCancelButton", false);
// TODO: This is a hack to propagate recordingTrackId in multiprocess env.
setRecordingTrackId(recordingTrackId = -1);
startActivity(intent);
}
tryUnbindTrackRecordingService();
@@ -1148,29 +1103,16 @@ public class MyTracks extends TabActivity implements OnTouchListener,
* @param trackId the id of the track
*/
public void setSelectedTrackId(final long trackId) {
runOnUiThread(new Runnable() {
public void run() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(getString(R.string.selected_track_key), trackId);
editor.commit();
}
});
sharedPreferences
.edit()
.putLong(getString(R.string.selected_track_key), trackId)
.commit();
}
long getSelectedTrackId() {
return selectedTrackId;
}
private void setRecordingTrackId(final long trackId) {
runOnUiThread(new Runnable() {
public void run() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(getString(R.string.recording_track_key), trackId);
editor.commit();
}
});
}
/**
* Binds to track recording service if it is running.
*/
@@ -1335,4 +1277,9 @@ public class MyTracks extends TabActivity implements OnTouchListener,
static void clearInstance() {
instance = null;
}
// @VisibleForTesting
ITrackRecordingService getTrackRecordingService() {
return trackRecordingService;
}
}
@@ -26,10 +26,10 @@ import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
/**
* An activity that let's the user see and edit the settings.
@@ -158,7 +158,7 @@ public class MyTracksSettings extends PreferenceActivity {
getPreferenceManager().getSharedPreferences()
.unregisterOnSharedPreferenceChangeListener(backupListener);
super.onPause();
super.onDestroy();
}
/**
@@ -20,7 +20,6 @@ import com.google.android.maps.mytracks.R;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.format.Time;
import java.text.SimpleDateFormat;
import java.util.Date;
@@ -84,13 +84,4 @@ interface ITrackRecordingService {
* Deletes all the stored tracks.
*/
void deleteAllTracks();
/**
* Notifies the service that its preferences may have been changed.
* This is necessary because the service running on a separate process cannot
* listen to the changes itself.
*
* @param key the preference key which may have changed
*/
void sharedPreferenceChanged(in String key);
}
@@ -20,7 +20,7 @@ import com.google.android.apps.mytracks.MyTracksSettings;
import com.google.android.maps.mytracks.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.util.Log;
/**
@@ -28,8 +28,9 @@ import android.util.Log;
*
* @author Sandor Dornbush
*/
public class PreferenceManager {
public class PreferenceManager implements OnSharedPreferenceChangeListener {
private TrackRecordingService service;
private SharedPreferences sharedPreferences;
private final String announcementFrequencyKey;
private final String autoResumeTrackCurrentRetryKey;
private final String autoResumeTrackTimeoutKey;
@@ -42,17 +43,16 @@ public class PreferenceManager {
private final String signalSamplingFrequencyKey;
private final String splitFrequencyKey;
private final SharedPreferences sharedPreferences;
public PreferenceManager(TrackRecordingService service) {
this.service = service;
this.sharedPreferences =
service.getSharedPreferences(MyTracksSettings.SETTINGS_NAME, 0);
this.sharedPreferences = service.getSharedPreferences(
MyTracksSettings.SETTINGS_NAME, 0);
if (sharedPreferences == null) {
Log.w(MyTracksConstants.TAG,
"TrackRecordingService: Couldn't get shared preferences.");
throw new IllegalStateException("Couldn't get shared preferences");
}
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
announcementFrequencyKey =
service.getString(R.string.announcement_frequency_key);
@@ -76,6 +76,9 @@ public class PreferenceManager {
service.getString(R.string.signal_sampling_frequency_key);
splitFrequencyKey =
service.getString(R.string.split_frequency_key);
// Refresh all properties.
onSharedPreferenceChanged(sharedPreferences, null);
}
/**
@@ -84,7 +87,9 @@ public class PreferenceManager {
*
* @param key the key that changed (may be null to update all preferences)
*/
public void onSharedPreferenceChanged(String key) {
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
if (key == null || key.equals(minRecordingDistanceKey)) {
service.setMinRecordingDistance(
sharedPreferences.getInt(
@@ -163,14 +168,16 @@ public class PreferenceManager {
}
public void setAutoResumeTrackCurrentRetry(int retryAttempts) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(autoResumeTrackCurrentRetryKey, retryAttempts);
editor.commit();
sharedPreferences
.edit()
.putInt(autoResumeTrackCurrentRetryKey, retryAttempts)
.commit();
}
public void setRecordingTrack(long id) {
Editor editor = sharedPreferences.edit();
editor.putLong(recordingTrackKey, id);
editor.commit();
sharedPreferences
.edit()
.putLong(recordingTrackKey, id)
.commit();
}
}
@@ -125,6 +125,8 @@ public class SplitManager {
* @param splitFrequency The frequency in time or distance
*/
public void setSplitFrequency(int splitFrequency) {
Log.d(MyTracksConstants.TAG,
"setSplitFrequency: splitFrequency = " + splitFrequency);
this.splitFrequency = splitFrequency;
// TODO: Decouple service from this class once and forever.
@@ -34,7 +34,7 @@ import android.util.Log;
import java.util.Locale;
/**
* This class will periodically announce the user's trip statitics.
* This class will periodically announce the user's trip statistics.
*
* @author Sandor Dornbush
*/
@@ -61,7 +61,7 @@ public class StatusAnnouncerTask implements PeriodicTask {
private TextToSpeech tts;
/**
* The response recieved from the TTS engine ater initialization.
* The response received from the TTS engine after initialization.
*/
private boolean ready = false;
@@ -261,7 +261,9 @@ public class StatusAnnouncerTask implements PeriodicTask {
protected void listenToPhoneState(PhoneStateListener listener, int events) {
TelephonyManager telephony =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, events);
if (telephony != null) {
telephony.listen(listener, events);
}
}
/**
@@ -15,24 +15,23 @@
*/
package com.google.android.apps.mytracks.services;
import android.util.Log;
import com.google.android.apps.mytracks.MyTracksConstants;
import android.util.Log;
/**
* This class will manage a period task executer.
* This class manages a period task executer.
*
* @author Sandor Dornbush
*/
public class TaskExecuterManager {
int frequency;
PeriodicTask task;
PeriodicTaskExecuter executer;
private int frequency;
private final PeriodicTask task;
private PeriodicTaskExecuter executer;
public TaskExecuterManager(int frequency,
PeriodicTask task,
TrackRecordingService service) {
public TaskExecuterManager(int frequency, PeriodicTask task,
TrackRecordingService service) {
this.task = task;
setFrequency(frequency, service);
}
@@ -69,7 +68,7 @@ public class TaskExecuterManager {
}
/**
* Restore the task at the current frequency.
* Restores the task at the current frequency.
*/
public void restore() {
if (frequency > 0) {
@@ -77,4 +76,13 @@ public class TaskExecuterManager {
executer.scheduleTask(frequency * 60000);
}
}
/**
* Shuts down this executer.
*/
public void shutdown() {
if (executer != null) {
executer.shutdown();
}
}
}
@@ -103,7 +103,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* recorded points (as compared to each location fix). It's used to overlay
* waypoints precisely in the elevation profile chart.
*/
private double length = 0;
private double length;
/**
* Status announcer executer.
@@ -118,7 +118,7 @@ public class TrackRecordingService extends Service implements LocationListener {
* The interval in milliseconds that we have requested to be notified of gps
* readings.
*/
private long currentRecordingInterval = 0;
private long currentRecordingInterval;
/**
* The policy used to decide how often we should request gps updates.
@@ -166,17 +166,17 @@ public class TrackRecordingService extends Service implements LocationListener {
/**
* Is the service currently recording a track?
*/
private boolean isRecording = false;
private boolean isRecording;
/**
* Last good location the service has received from the location listener
*/
private Location lastLocation = null;
private Location lastLocation;
/**
* Last valid location (i.e. not a marker) that was recorded.
*/
private Location lastValidLocation = null;
private Location lastValidLocation;
/**
* The frequency of status announcements.
@@ -605,28 +605,6 @@ public class TrackRecordingService extends Service implements LocationListener {
// Do nothing
}
/*
* SharedPreferencesChangeListener interface implementation. Note that
* services don't currently receive this event (Android platform limitation).
* This should be called from an activity whenever settings change.
*/
/**
* Notifies that preferences have changed.
* Call this with key == null to update all preferences in one call.
*
* @param key the key that changed (may be null to update all preferences)
*/
public void onSharedPreferenceChanged(String key) {
Log.d(MyTracksConstants.TAG,
"TrackRecordingService.onSharedPreferenceChanged");
prefManager.onSharedPreferenceChanged(key);
if (isRecording) {
registerLocationListener();
}
}
/*
* Application lifetime events: ============================
*/
@@ -647,7 +625,6 @@ public class TrackRecordingService extends Service implements LocationListener {
new TaskExecuterManager(-1, strengthTaskFactory.create(this), this);
prefManager = new PreferenceManager(this);
prefManager.onSharedPreferenceChanged(null);
registerLocationListener();
acquireWakeLock();
/**
@@ -682,15 +659,23 @@ public class TrackRecordingService extends Service implements LocationListener {
Log.d(MyTracksConstants.TAG, "TrackRecordingService.setUpAnnouncer: "
+ announcementExecuter);
if (announcementFrequency != -1 && recordingTrackId != -1) {
if (announcementExecuter == null) {
StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
PeriodicTask announcer = statusAnnouncerFactory.create(this);
if (announcer == null) return;
announcementExecuter = new PeriodicTaskExecuter(announcer, this);
}
announcementExecuter.scheduleTask(announcementFrequency * 60000);
handler.post(new Runnable() {
@Override
public void run() {
if (announcementExecuter == null) {
StatusAnnouncerFactory statusAnnouncerFactory =
new StatusAnnouncerFactory(ApiFeatures.getInstance());
PeriodicTask announcer = statusAnnouncerFactory.create(
TrackRecordingService.this);
if (announcer == null) {
return;
}
announcementExecuter = new PeriodicTaskExecuter(announcer,
TrackRecordingService.this);
}
announcementExecuter.scheduleTask(announcementFrequency * 60000);
}
});
}
}
@@ -718,6 +703,7 @@ public class TrackRecordingService extends Service implements LocationListener {
showNotification();
unregisterLocationListener();
shutdownAnnouncer();
signalManager.shutdown();
splitManager.shutdown();
super.onDestroy();
}
@@ -949,8 +935,8 @@ public class TrackRecordingService extends Service implements LocationListener {
throw new IllegalStateException("No recording track in progress!");
}
isRecording = false;
shutdownAnnouncer();
isRecording = false;
Track recordingTrack = providerUtils.getTrack(recordingTrackId);
if (recordingTrack != null) {
TripStatistics stats = recordingTrack.getStatistics();
@@ -985,13 +971,6 @@ public class TrackRecordingService extends Service implements LocationListener {
public void recordLocation(Location loc) {
onLocationChanged(loc);
}
@Override
public void sharedPreferenceChanged(String key) {
Log.d(MyTracksConstants.TAG,
"TrackRecordingService.sharedPreferenceChanged: " + key);
onSharedPreferenceChanged(key);
}
};
public long startNewTrack() {