Final widget fixes.

This commit is contained in:
Sandor Dornbush
2011-07-05 22:11:05 -07:00
parent 29df8762e2
commit 5784ab63cc
4 changed files with 77 additions and 48 deletions
@@ -39,6 +39,7 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener {
private final String minRecordingIntervalKey; private final String minRecordingIntervalKey;
private final String minRequiredAccuracyKey; private final String minRequiredAccuracyKey;
private final String recordingTrackKey; private final String recordingTrackKey;
private final String selectedTrackKey;
private final String splitFrequencyKey; private final String splitFrequencyKey;
public PreferenceManager(TrackRecordingService service) { public PreferenceManager(TrackRecordingService service) {
@@ -70,6 +71,8 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener {
service.getString(R.string.min_required_accuracy_key); service.getString(R.string.min_required_accuracy_key);
recordingTrackKey = recordingTrackKey =
service.getString(R.string.recording_track_key); service.getString(R.string.recording_track_key);
selectedTrackKey =
service.getString(R.string.selected_track_key);
splitFrequencyKey = splitFrequencyKey =
service.getString(R.string.split_frequency_key); service.getString(R.string.split_frequency_key);
@@ -179,6 +182,13 @@ public class PreferenceManager implements OnSharedPreferenceChangeListener {
.commit(); .commit();
} }
public void setSelectedTrack(long id) {
sharedPreferences
.edit()
.putLong(selectedTrackKey, id)
.commit();
}
public void shutdown() { public void shutdown() {
sharedPreferences.unregisterOnSharedPreferenceChangeListener(this); sharedPreferences.unregisterOnSharedPreferenceChangeListener(this);
service = null; service = null;
@@ -281,22 +281,34 @@ public class TrackRecordingService extends Service {
"TrackRecordingService.handleStartCommand: " + startId); "TrackRecordingService.handleStartCommand: " + startId);
// Check if called on phone reboot with resume intent. // Check if called on phone reboot with resume intent.
if (intent != null && if (intent != null) {
intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) { if (intent.getBooleanExtra(RESUME_TRACK_EXTRA_NAME, false)) {
Log.d(TAG, "TrackRecordingService: requested resume"); Log.d(TAG, "TrackRecordingService: requested resume");
// Make sure that the current track exists and is fresh enough. // Make sure that the current track exists and is fresh enough.
if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) { if (recordingTrack == null || !shouldResumeTrack(recordingTrack)) {
Log.i(TAG, Log.i(TAG,
"TrackRecordingService: Not resuming, because the previous track (" "TrackRecordingService: Not resuming, because the previous track ("
+ recordingTrack + ") doesn't exist or is too old"); + recordingTrack + ") doesn't exist or is too old");
isRecording = false; isRecording = false;
prefManager.setRecordingTrack(recordingTrackId = -1); prefManager.setRecordingTrack(recordingTrackId = -1);
stopSelfResult(startId); stopSelfResult(startId);
return; 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");
} }
} }
@@ -32,12 +32,12 @@ import android.os.Handler;
import android.util.Log; import android.util.Log;
import android.widget.RemoteViews; 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.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Track; import com.google.android.apps.mytracks.content.Track;
import com.google.android.apps.mytracks.content.TracksColumns; import com.google.android.apps.mytracks.content.TracksColumns;
import com.google.android.apps.mytracks.services.TrackRecordingService; import com.google.android.apps.mytracks.services.TrackRecordingService;
import com.google.android.apps.mytracks.stats.TripStatistics; 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.StringUtils;
import com.google.android.apps.mytracks.util.UnitConversions; import com.google.android.apps.mytracks.util.UnitConversions;
import com.google.android.maps.mytracks.R; import com.google.android.maps.mytracks.R;
@@ -92,6 +92,14 @@ public class TrackWidgetProvider
updateTrack("some action"); 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) { private void initialize(Context context) {
this.context = context; this.context = context;
trackObserver = new TrackObserver(); trackObserver = new TrackObserver();
@@ -123,14 +131,6 @@ public class TrackWidgetProvider
selectedTrackId = intent.getLongExtra( selectedTrackId = intent.getLongExtra(
context.getString(R.string.track_id_broadcast_extra), selectedTrackId); context.getString(R.string.track_id_broadcast_extra), selectedTrackId);
String action = intent.getAction(); 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, Log.d(TAG,
"TrackWidgetProvider.onReceive: trackId=" + selectedTrackId + ", action=" + action); "TrackWidgetProvider.onReceive: trackId=" + selectedTrackId + ", action=" + action);
@@ -157,12 +157,17 @@ public class TrackWidgetProvider
ComponentName widget = new ComponentName(context, TrackWidgetProvider.class); ComponentName widget = new ComponentName(context, TrackWidgetProvider.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget); 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); int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
if (action != null) {
updateViewButton(views, context, action);
}
updateViewTrackStatistics(views, track);
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
} }
} }
@@ -174,30 +179,35 @@ public class TrackWidgetProvider
* @param context The Context of the AppWidget * @param context The Context of the AppWidget
* @param action The action broadcast from the track service * @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 (TRACK_STARTED_ACTION.equals(action)) {
// If a new track is started by this appwidget or elsewhere, // If a new track is started by this appwidget or elsewhere,
// toggle the button to active and have it disable the track if pressed. // toggle the button to active and have it disable the track if pressed.
Intent intent = new Intent(context, TrackRecordingService.class); setButtonIntent(
intent.setAction(context.getString(R.string.end_current_track_action)); views, context, R.string.end_current_track_action, R.drawable.appwidget_button_enabled,
PendingIntent pendingIntent = PendingIntent.getService(context, 0, -1);
intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setImageViewResource(R.id.appwidget_button,
R.drawable.appwidget_button_enabled);
views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent);
} else { } else {
// If a track is stopped by this appwidget or elsewhere, // If a track is stopped by this appwidget or elsewhere,
// toggle the button to inactive and have it start a new track if pressed. // toggle the button to inactive and have it start a new track if pressed.
Intent intent = new Intent(context, TrackRecordingService.class); setButtonIntent(
intent.setAction(context.getString(R.string.start_new_track_action)); views, context, R.string.start_new_track_action, R.drawable.appwidget_button_disabled,
PendingIntent pendingIntent = PendingIntent.getService(context, 0, R.string.select_new_track_extra);
intent, PendingIntent.FLAG_UPDATE_CURRENT);
views.setImageViewResource(R.id.appwidget_button,
R.drawable.appwidget_button_disabled);
views.setOnClickPendingIntent(R.id.appwidget_button, pendingIntent);
} }
} }
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 * Update the specified widget's view with the distance, time, and speed of
* the specified track. * the specified track.
@@ -213,11 +223,6 @@ public class TrackWidgetProvider
return; 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(); TripStatistics stats = track.getStatistics();
// TODO replace this with format strings and miles. // TODO replace this with format strings and miles.
+2
View File
@@ -30,6 +30,8 @@ limitations under the License.
<!-- TrackRecordingService actions --> <!-- TrackRecordingService actions -->
<string name="start_new_track_action">com.google.android.apps.mytracks.START_NEW_TRACK</string> <string name="start_new_track_action">com.google.android.apps.mytracks.START_NEW_TRACK</string>
<!-- Setting this intent makes the new track the selected track. -->
<string name="select_new_track_extra">com.google.android.apps.mytracks.SELECT_NEW_TRACK</string>
<string name="end_current_track_action">com.google.android.apps.mytracks.END_CURRENT_TRACK</string> <string name="end_current_track_action">com.google.android.apps.mytracks.END_CURRENT_TRACK</string>
<!-- My Tracks service constants --> <!-- My Tracks service constants -->