Try to clean up widget.

This commit is contained in:
Sandor Dornbush
2011-06-21 21:17:03 -07:00
parent 63677b1451
commit 17c1c81b4e
4 changed files with 156 additions and 31 deletions
+83
View File
@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
Copyright 2010 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.
-->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/screen"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:enabled="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:apiKey="0TXoRmxTinrZKQ9JSazj-9A34XX6_lXsRvTpXWA"/>
<!-- corresponding certificate md5: 81:DF:A2:63:53:C7:6B:1E:6C:24:62:D4:91:1C:36:3A -->
<LinearLayout android:id="@+id/messagepane"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_bar"
android:layout_weight="0"
android:padding="3dip"
android:visibility="gone">
<TextView android:id="@+id/messagetext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#fff"
android:text="@string/wait_for_fix"
android:layout_weight="0"/>
</LinearLayout>
<LinearLayout android:id="@+id/busypane"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0"
android:padding="3dip"
android:visibility="gone">
<ProgressBar
android:layout_weight="1"
android:layout_height="0dip"
android:layout_width="wrap_content"
android:maxWidth="80dip"
android:maxHeight="80dip"
android:minWidth="80dip"
android:minHeight="80dip"
android:layout_gravity="center"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:paddingTop="10dip">
<LinearLayout
android:layout_width = "wrap_content"
android:layout_height= "wrap_content"
android:layout_weight="1"/>
<ImageButton
android:id="@+id/showOptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:visibility="visible"
android:src="@android:drawable/ic_menu_more"/>
</LinearLayout>
</RelativeLayout>
@@ -20,7 +20,6 @@ import static com.google.android.apps.mytracks.Constants.RESUME_TRACK_EXTRA_NAME
import com.google.android.apps.mytracks.MyTracks;
import com.google.android.apps.mytracks.Constants;
import com.google.android.apps.mytracks.TrackDataHub;
import com.google.android.apps.mytracks.content.MyTracksLocation;
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
import com.google.android.apps.mytracks.content.Sensor;
@@ -278,10 +277,6 @@ public class TrackRecordingService extends Service implements LocationListener {
announcementExecutor.update();
splitExecutor.update();
// Send broadcast that the track has been updated.
sendTrackBroadcast(R.string.track_updated_broadcast_action,
recordingTrack.getId());
return true;
}
@@ -409,13 +404,11 @@ public class TrackRecordingService extends Service implements LocationListener {
public void unregisterLocationListener() {
if (locationManager == null) {
Log.e(TAG,
"TrackRecordingService: Do not have any location manager.");
Log.e(TAG, "TrackRecordingService: Do not have any location manager.");
return;
}
locationManager.removeUpdates(this);
Log.d(TAG,
"Location listener now unregistered w/ TrackRecordingService.");
Log.d(TAG, "Location listener now unregistered w/ TrackRecordingService.");
}
private Track getRecordingTrack() {
@@ -1112,9 +1105,6 @@ public class TrackRecordingService extends Service implements LocationListener {
// Persist the current recording track.
prefManager.setRecordingTrack(recordingTrackId);
TrackDataHub dataHub = MyTracks.getInstance().getDataHub();
dataHub.loadTrack(track.getId());
// Notify the world that we're now recording.
sendTrackBroadcast(
R.string.track_started_broadcast_action, recordingTrackId);
@@ -1160,6 +1150,7 @@ public class TrackRecordingService extends Service implements LocationListener {
}
releaseWakeLock();
unregisterLocationListener();
// Notify the world that we're no longer recording.
sendTrackBroadcast(
@@ -21,12 +21,15 @@ import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.os.Handler;
import android.util.Log;
import android.widget.RemoteViews;
import com.google.android.apps.mytracks.Constants;
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.StringUtils;
@@ -39,9 +42,54 @@ import com.google.android.maps.mytracks.R;
* @author Paul R. Saxman
*/
public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
class TrackObserver extends ContentObserver {
public TrackObserver() {
super(contentHandler);
}
public void onChange(boolean selfChange) {
updateTrack(-1, null);
}
}
private final Handler contentHandler;
private MyTracksProviderUtils providerUtils;
// TODO kill this. I am fairly certain this is a bug.
private Context context;
private String unknown;
private String time;
private String distance;
private String speed;
public AppWidgetProvider() {
super();
contentHandler = new Handler();
}
@Override
public void onEnabled(Context context) {
context.getContentResolver().registerContentObserver(TracksColumns.CONTENT_URI, true, new TrackObserver());
initialize(context);
}
private void initialize(Context context) {
if (this.context != null) {
return;
}
providerUtils = MyTracksProviderUtils.Factory.get(context);
this.context = context;
this.distance = context.getString(R.string.kilometer);
this.speed = context.getString(R.string.kilometer_per_hour);
this.time = context.getString(R.string.min);
this.unknown = context.getString(R.string.unknown);
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
initialize(context);
long trackId = intent.getLongExtra(
context.getString(R.string.track_id_broadcast_extra), -1);
@@ -51,27 +99,32 @@ public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
"MyTracksAppWidgetProvider.onReceive: trackId=" + trackId + ", action="
+ action);
updateTrack(trackId, action);
}
private void updateTrack(long trackId, String action) {
Track track = null;
if (trackId != -1) {
Log.d(Constants.TAG,
"MyTracksAppWidgetProvider.onReceive: Retrieving specified track.");
track = MyTracksProviderUtils.Factory.get(context).getTrack(trackId);
track = providerUtils.getTrack(trackId);
} else {
Log.d(Constants.TAG,
"MyTracksAppWidgetProvider.onReceive: Attempting to retrieve previous track.");
track = MyTracksProviderUtils.Factory.get(context).getLastTrack();
// TODO we should really read the pref.
track = providerUtils.getLastTrack();
}
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
ComponentName widget =
new ComponentName(context, AppWidgetProvider.class);
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.appwidget);
ComponentName widget = new ComponentName(context, AppWidgetProvider.class);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(widget);
for (int appWidgetId : appWidgetIds) {
updateViewButton(views, context, action);
updateViewTrackStatistics(views, track, context);
if (action != null) {
updateViewButton(views, context, action);
}
updateViewTrackStatistics(views, track);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
@@ -112,15 +165,13 @@ public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
* the specified track.
*
* @param views The RemoteViews to update with statistics
* @param context The Context of the AppWidget
* @param track The track to extract statistics from.
*/
protected void updateViewTrackStatistics(RemoteViews views, Track track,
Context context) {
protected void updateViewTrackStatistics(RemoteViews views, Track track) {
if (track == null) {
views.setTextViewText(R.id.appwidget_distance_text, "NA");
views.setTextViewText(R.id.appwidget_time_text, "NA");
views.setTextViewText(R.id.appwidget_speed_text, "NA");
views.setTextViewText(R.id.appwidget_distance_text, unknown);
views.setTextViewText(R.id.appwidget_time_text, unknown);
views.setTextViewText(R.id.appwidget_speed_text, unknown);
return;
}
@@ -131,19 +182,20 @@ public class AppWidgetProvider extends android.appwidget.AppWidgetProvider {
TripStatistics stats = track.getStatistics();
// TODO replace this with format strings and miles.
// convert meters to kilometers
String distance = StringUtils.formatSingleDecimalPlace(stats.getTotalDistance() / 1000)
+ " " + context.getString(R.string.kilometer);
+ " " + this.distance;
// convert ms to minutes
String time = StringUtils.formatSingleDecimalPlace(stats.getMovingTime() / 60000d)
+ " mins";
+ " " + this.time;
String speed = "NA";
String speed = unknown;
if (!Double.isNaN(stats.getAverageMovingSpeed())) {
// convert m/s to km/h
speed = StringUtils.formatSingleDecimalPlace(stats.getAverageMovingSpeed() * 3.6)
+ " " + context.getString(R.string.kilometer_per_hour);
+ " " + this.speed;
}
views.setTextViewText(R.id.appwidget_distance_text, distance);
-1
View File
@@ -26,7 +26,6 @@ limitations under the License.
<string name="track_paused_broadcast_action">com.google.android.apps.mytracks.TRACK_PAUSED</string>
<string name="track_resumed_broadcast_action">com.google.android.apps.mytracks.TRACK_RESUMED</string>
<string name="track_stopped_broadcast_action">com.google.android.apps.mytracks.TRACK_STOPPED</string>
<string name="track_updated_broadcast_action">com.google.android.apps.mytracks.TRACK_UPDATED</string>
<string name="track_id_broadcast_extra">com.google.android.apps.mytracks.TRACK_ID</string>
<!-- TrackRecordingService actions -->