From 5784ab63ccf4208c9ee85bea211095ce5b73b494 Mon Sep 17 00:00:00 2001 From: Sandor Dornbush Date: Tue, 5 Jul 2011 22:11:05 -0700 Subject: [PATCH] Final widget fixes. --- .../mytracks/services/PreferenceManager.java | 10 +++ .../services/TrackRecordingService.java | 42 +++++++---- .../mytracks/widgets/TrackWidgetProvider.java | 71 ++++++++++--------- MyTracksLib/res/values/third_party.xml | 2 + 4 files changed, 77 insertions(+), 48 deletions(-) diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/PreferenceManager.java b/MyTracks/src/com/google/android/apps/mytracks/services/PreferenceManager.java index efe060df5..ca625966c 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/PreferenceManager.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/PreferenceManager.java @@ -39,6 +39,7 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener { private final String minRecordingIntervalKey; private final String minRequiredAccuracyKey; private final String recordingTrackKey; + private final String selectedTrackKey; private final String splitFrequencyKey; public PreferenceManager(TrackRecordingService service) { @@ -70,6 +71,8 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener { service.getString(R.string.min_required_accuracy_key); recordingTrackKey = service.getString(R.string.recording_track_key); + selectedTrackKey = + service.getString(R.string.selected_track_key); splitFrequencyKey = service.getString(R.string.split_frequency_key); @@ -179,6 +182,13 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener { .commit(); } + public void setSelectedTrack(long id) { + sharedPreferences + .edit() + .putLong(selectedTrackKey, id) + .commit(); + } + public void shutdown() { sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); service = null; diff --git a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java index d1e1a5bf7..d12aacad5 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java +++ b/MyTracks/src/com/google/android/apps/mytracks/services/TrackRecordingService.java @@ -281,22 +281,34 @@ public class TrackRecordingService extends Service { "TrackRecordingService.handleStartCommand: " + startId); // Check if called on phone reboot with resume intent. - if (intent != null && - intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) { - Log.d(TAG, "TrackRecordingService: requested resume"); - - // Make sure that the current track exists and is fresh enough. - if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) { - Log.i(TAG, - "TrackRecordingService: Not resuming, because the previous track (" - + recordingTrack + ") doesn't exist or is too old"); - isRecording = false; - prefManager.setRecordingTrack(recordingTrackId = -1); - stopSelfResult(startId); - return; + if (intent != null) { + if (intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) { + Log.d(TAG, "TrackRecordingService: requested resume"); + + // Make sure that the current track exists and is fresh enough. + if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) { + Log.i(TAG, + "TrackRecordingService: Not resuming, because the previous track (" + + recordingTrack + ") doesn't exist or is too old"); + isRecording = false; + prefManager.setRecordingTrack(recordingTrackId = -1); + stopSelfResult(startId); + return; + } + + Log.i(TAG, "TrackRecordingService: resuming"); + } else { + // Process actions for controlling the service. + String action = intent.getAction(); + if (getString(R.string.start_new_track_action).equals(action)) { + startNewTrack(); + if (intent.getBooleanExtra(getString(R.string.select_new_track_extra), false)) { + prefManager.setSelectedTrack(recordingTrackId); + } + } else if (getString(R.string.end_current_track_action).equals(action)) { + endCurrentTrack(); + } } - - Log.i(TAG, "TrackRecordingService: resuming"); } } diff --git a/MyTracks/src/com/google/android/apps/mytracks/widgets/TrackWidgetProvider.java b/MyTracks/src/com/google/android/apps/mytracks/widgets/TrackWidgetProvider.java index 6e0887d3e..5477ee128 100644 --- a/MyTracks/src/com/google/android/apps/mytracks/widgets/TrackWidgetProvider.java +++ b/MyTracks/src/com/google/android/apps/mytracks/widgets/TrackWidgetProvider.java @@ -32,12 +32,12 @@ import android.os.Handler; import android.util.Log; import android.widget.RemoteViews; +import com.google.android.apps.mytracks.MyTracks; import com.google.android.apps.mytracks.content.MyTracksProviderUtils; import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.stats.TripStatistics; -import com.google.android.apps.mytracks.util.ApiFeatures; import com.google.android.apps.mytracks.util.StringUtils; import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.maps.mytracks.R; @@ -92,6 +92,14 @@ public class TrackWidgetProvider updateTrack("some action"); } + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + initialize(context); + + // So long as an action is set the buttons will get set up properly. + updateTrack("some action"); + } + private void initialize(Context context) { this.context = context; trackObserver = new TrackObserver(); @@ -123,14 +131,6 @@ public class TrackWidgetProvider selectedTrackId = intent.getLongExtra( context.getString(R.string.track_id_broadcast_extra), selectedTrackId); String action = intent.getAction(); - // TODO this should only trigger for intents this sent. - if (TRACK_STARTED_ACTION.equals(action)) { - ApiFeatures.getInstance().getApiPlatformAdapter() - .applyPreferenceChanges( - sharedPreferences.edit().putLong( - context.getString(R.string.selected_track_key), selectedTrackId)); - } - Log.d(TAG, "TrackWidgetProvider.onReceive: trackId=" + selectedTrackId + ", action=" + action); @@ -157,12 +157,17 @@ public class TrackWidgetProvider ComponentName widget = new ComponentName(context, TrackWidgetProvider.class); RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget); + // Make all of the stats open the mytracks activity. + Intent i = new Intent(context, MyTracks.class); + PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); + views.setOnClickPendingIntent(R.id.appwidget_track_statistics, pendingIntent); + + if (action != null) { + updateViewButton(views, context, action); + } + updateViewTrackStatistics(views, track); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget); for (int appWidgetId : appWidgetIds) { - if (action != null) { - updateViewButton(views, context, action); - } - updateViewTrackStatistics(views, track); appWidgetManager.updateAppWidget(appWidgetId, views); } } @@ -174,30 +179,35 @@ public class TrackWidgetProvider * @param context The Context of the AppWidget * @param action The action broadcast from the track service */ - protected void updateViewButton(RemoteViews views, Context context, String action) { + private void updateViewButton(RemoteViews views, Context context, String action) { if (TRACK_STARTED_ACTION.equals(action)) { // If a new track is started by this appwidget or elsewhere, // toggle the button to active and have it disable the track if pressed. - Intent intent = new Intent(context, TrackRecordingService.class); - intent.setAction(context.getString(R.string.end_current_track_action)); - PendingIntent pendingIntent = PendingIntent.getService(context, 0, - intent, PendingIntent.FLAG_UPDATE_CURRENT); - views.setImageViewResource(R.id.appwidget_button, - R.drawable.appwidget_button_enabled); - views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent); + setButtonIntent( + views, context, R.string.end_current_track_action, R.drawable.appwidget_button_enabled, + -1); } else { // If a track is stopped by this appwidget or elsewhere, // toggle the button to inactive and have it start a new track if pressed. - Intent intent = new Intent(context, TrackRecordingService.class); - intent.setAction(context.getString(R.string.start_new_track_action)); - PendingIntent pendingIntent = PendingIntent.getService(context, 0, - intent, PendingIntent.FLAG_UPDATE_CURRENT); - views.setImageViewResource(R.id.appwidget_button, - R.drawable.appwidget_button_disabled); - views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent); + setButtonIntent( + views, context, R.string.start_new_track_action, R.drawable.appwidget_button_disabled, + R.string.select_new_track_extra); } } + private void setButtonIntent( + RemoteViews views, Context context, int action, int icon, int extra) { + Intent intent = new Intent(context, TrackRecordingService.class); + intent.setAction(context.getString(action)); + if (extra != -1) { + intent.putExtra(context.getString(extra), true); + } + PendingIntent pendingIntent = PendingIntent.getService(context, 0, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent); + views.setImageViewResource(R.id.appwidget_button, icon); + } + /** * Update the specified widget's view with the distance, time, and speed of * the specified track. @@ -213,11 +223,6 @@ public class TrackWidgetProvider return; } - // TODO(saxman) Display stats details when track stats view is pressed. - // Intent i = new Intent(context, StatsActivity.class); - // PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, i, 0); - // views.setOnClickPendingIntent(R.id.appwidget_track_statistics, pendingIntent); - TripStatistics stats = track.getStatistics(); // TODO replace this with format strings and miles. diff --git a/MyTracksLib/res/values/third_party.xml b/MyTracksLib/res/values/third_party.xml index 52adbb40e..d1b4defa4 100644 --- a/MyTracksLib/res/values/third_party.xml +++ b/MyTracksLib/res/values/third_party.xml @@ -30,6 +30,8 @@ limitations under the License. com.google.android.apps.mytracks.START_NEW_TRACK + + com.google.android.apps.mytracks.SELECT_NEW_TRACK com.google.android.apps.mytracks.END_CURRENT_TRACK