update app widget layout

This commit is contained in:
Jimmy Shih
2012-10-02 14:30:53 -07:00
parent 7240d02c06
commit 5db20b48ca
9 changed files with 539 additions and 272 deletions
@@ -33,7 +33,7 @@ import java.util.regex.Pattern;
/**
* Various string manipulation methods.
*
*
* @author Sandor Dornbush
* @author Rodrigo Damazio
*/
@@ -64,7 +64,8 @@ public class StringUtils {
}
/**
* Formats the relative date and time based on user's phone date/time preferences.
* Formats the relative date and time based on user's phone date/time
* preferences.
*
* @param context the context
* @param time the time in milliseconds
@@ -74,15 +75,15 @@ public class StringUtils {
if (now - time > DateUtils.WEEK_IN_MILLIS) {
return formatDateTime(context, time);
} else {
return DateUtils.getRelativeTimeSpanString(time, now, DateUtils.MINUTE_IN_MILLIS,
DateUtils.FORMAT_ABBREV_RELATIVE).toString();
return DateUtils.getRelativeTimeSpanString(
time, now, DateUtils.MINUTE_IN_MILLIS, DateUtils.FORMAT_ABBREV_RELATIVE).toString();
}
}
/**
* Formats the time using the ISO 8601 date time format with fractional
* seconds in UTC time zone.
*
*
* @param time the time in milliseconds
*/
public static String formatDateTimeIso8601(long time) {
@@ -91,7 +92,7 @@ public class StringUtils {
/**
* Formats the elapsed timed in the form "MM:SS" or "H:MM:SS".
*
*
* @param time the time in milliseconds
*/
public static String formatElapsedTime(long time) {
@@ -100,7 +101,7 @@ public class StringUtils {
/**
* Formats the elapsed time in the form "H:MM:SS".
*
*
* @param time the time in milliseconds
*/
public static String formatElapsedTimeWithHour(long time) {
@@ -136,7 +137,45 @@ public class StringUtils {
}
}
}
/**
* Gets the distance in an array of two strings. The first string is the
* distance. The second string is the unit.
*
* @param context the context
* @param distance the distance
* @param metricUnits true to use metric unit
*/
public static String[] getDistanceParts(Context context, double distance, boolean metricUnits) {
String[] result = new String[2];
if (Double.isNaN(distance) || Double.isInfinite(distance)) {
result[0] = context.getString(R.string.value_unknown);
result[1] = context.getString(metricUnits ? R.string.unit_meter : R.string.unit_feet);
return result;
}
int unitId;
if (metricUnits) {
if (distance > 500.0) {
distance *= UnitConversions.M_TO_KM;
unitId = R.string.unit_kilometer;
} else {
unitId = R.string.unit_meter;
}
} else {
if (distance * UnitConversions.M_TO_MI > 0.5) {
distance *= UnitConversions.M_TO_MI;
unitId = R.string.unit_mile;
} else {
distance *= UnitConversions.M_TO_FT;
unitId = R.string.unit_feet;
}
}
result[0] = String.format("%.2f", distance);
result[1] = context.getString(unitId);
return result;
}
/**
* Formats the speed.
*
@@ -171,11 +210,51 @@ public class StringUtils {
}
}
/**
* Gets the speed in an array of two strings. The first string is the
* speed. The second string is the unit.
*
* @param context the context
* @param speed the speed
* @param metricUnits true to use metric unit
* @param reportSpeed true to report speed
*/
public static String[] getSpeedParts(
Context context, double speed, boolean metricUnits, boolean reportSpeed) {
String[] result = new String[2];
int unitId;
if (metricUnits) {
unitId = reportSpeed ? R.string.unit_kilometer_per_hour : R.string.unit_minute_per_kilometer;
} else {
unitId = reportSpeed ? R.string.unit_mile_per_hour : R.string.unit_minute_per_mile;
}
result[1] = context.getString(unitId);
if (Double.isNaN(speed) || Double.isInfinite(speed)) {
result[0] = context.getString(R.string.value_unknown);
return result;
}
speed *= UnitConversions.MS_TO_KMH;
if (metricUnits) {
if (!reportSpeed) {
// convert from hours to minutes
speed = speed == 0 ? 0.0 : 60.0 / speed;
}
} else {
speed *= UnitConversions.KM_TO_MI;
if (!reportSpeed) {
// convert from hours to minutes
speed = speed == 0 ? 0.0 : 60.0 / speed;
}
}
result[0] = String.format("%.2f", speed);
return result;
}
/**
* Formats the given text as a XML CDATA element. This includes adding the
* starting and ending CDATA tags. Please notice that this may result in
* multiple consecutive CDATA tags.
*
*
* @param text the given text
*/
public static String formatCData(String text) {
@@ -185,7 +264,7 @@ public class StringUtils {
/**
* Gets the time, in milliseconds, from an XML date time string as defined at
* http://www.w3.org/TR/xmlschema-2/#dateTime
*
*
* @param xmlDateTime the XML date time string
*/
public static long getTime(String xmlDateTime) {
@@ -193,8 +272,9 @@ public class StringUtils {
ParsePosition position = new ParsePosition(0);
Date date = ISO_8601_BASE.parse(xmlDateTime, position);
if (date == null) {
throw new IllegalArgumentException("Invalid XML dateTime value: " + xmlDateTime
+ " (at position " + position.getErrorIndex() + ")"); }
throw new IllegalArgumentException("Invalid XML dateTime value: " + xmlDateTime
+ " (at position " + position.getErrorIndex() + ")");
}
// Parse the date time extras
Matcher matcher = ISO_8601_EXTRAS.matcher(xmlDateTime.substring(position.getIndex()));
@@ -246,7 +326,7 @@ public class StringUtils {
* Gets the time as an array of three integers. Index 0 contains the number of
* seconds, index 1 contains the number of minutes, and index 2 contains the
* number of hours.
*
*
* @param time the time in milliseconds
* @return an array of 3 elements.
*/
@@ -284,7 +364,6 @@ public class StringUtils {
return Html.fromHtml(context.getString(resId, args));
}
/**
* Gets the frequency display options.
*
@@ -299,8 +378,9 @@ public class StringUtils {
if (value == PreferencesUtils.FREQUENCY_OFF) {
options[i] = context.getString(R.string.value_off);
} else if (value < 0) {
options[i] = context.getString(metricUnits ? R.string.value_integer_kilometer
: R.string.value_integer_mile, Math.abs(value));
options[i] = context.getString(
metricUnits ? R.string.value_integer_kilometer : R.string.value_integer_mile,
Math.abs(value));
} else {
options[i] = context.getString(R.string.value_integer_minute, value);
}
@@ -27,15 +27,16 @@ import com.google.android.apps.mytracks.util.PreferencesUtils;
import com.google.android.apps.mytracks.util.StringUtils;
import com.google.android.maps.mytracks.R;
import android.annotation.TargetApi;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.view.View;
import android.util.SparseBooleanArray;
import android.widget.RemoteViews;
/**
@@ -48,31 +49,57 @@ import android.widget.RemoteViews;
*/
public class TrackWidgetProvider extends AppWidgetProvider {
private static final int LARGE_HEIGHT = 110;
// Array of appwidget id to use large size
private static final SparseBooleanArray useLargeSize = new SparseBooleanArray();
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_ENABLED.equals(action)
|| AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
if (AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
|| context.getString(R.string.track_paused_broadcast_action).equals(action)
|| context.getString(R.string.track_resumed_broadcast_action).equals(action)
|| context.getString(R.string.track_started_broadcast_action).equals(action)
|| context.getString(R.string.track_stopped_broadcast_action).equals(action)
|| context.getString(R.string.track_update_broadcast_action).equals(action)) {
long trackId = intent.getLongExtra(context.getString(R.string.track_id_broadcast_extra), -1L);
update(context, trackId);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, TrackWidgetProvider.class));
for (int appWidgetId : appWidgetIds) {
RemoteViews remoteViews = getRemoteViews(context, trackId, useLargeSize.get(appWidgetId));
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
}
@TargetApi(16)
@Override
public void onAppWidgetOptionsChanged(
Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) {
boolean large = newOptions == null
|| newOptions.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT) >= LARGE_HEIGHT;
useLargeSize.put(appWidgetId, large);
RemoteViews remoteViews = getRemoteViews(context, -1L, large);
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
/**
* Updates the widget.
*
* @param context the context
* @param trackId the track id
* @param large true to use the large layout
*/
private void update(Context context, long trackId) {
private RemoteViews getRemoteViews(Context context, long trackId, boolean large) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
large ? R.layout.track_widget_large : R.layout.track_widget_small);
// Get the preferences
long recordingTrackId = PreferencesUtils.getLong(context, R.string.recording_track_id_key);
boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
boolean recordingTrackPaused = PreferencesUtils.getBoolean(context,
R.string.recording_track_paused_key, PreferencesUtils.RECORDING_TRACK_PAUSED_DEFAULT);
boolean metricUnits = PreferencesUtils.getBoolean(
@@ -83,30 +110,23 @@ public class TrackWidgetProvider extends AppWidgetProvider {
if (trackId == -1L) {
trackId = recordingTrackId;
}
if (trackId == -1L) {
trackId = PreferencesUtils.getLong(context, R.string.selected_track_id_key);
}
Track track = trackId != -1L ? myTracksProviderUtils.getTrack(trackId)
: myTracksProviderUtils.getLastTrack();
TripStatistics tripStatistics = track == null ? null : track.getTripStatistics();
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.track_widget);
boolean isRecording = recordingTrackId != PreferencesUtils.RECORDING_TRACK_ID_DEFAULT;
updateStatisticsContainer(context, remoteViews, track);
updateTotalDistance(context, remoteViews, tripStatistics, metricUnits);
updateTotalTime(remoteViews, tripStatistics, isRecording, recordingTrackPaused);
updateAverageSpeed(context, remoteViews, tripStatistics, metricUnits);
updateMovingTime(context, remoteViews, tripStatistics);
updateTotalDistance(context, remoteViews, tripStatistics, metricUnits);
updateRecordButton(context, remoteViews, isRecording, recordingTrackPaused);
updateRecordStatus(context, remoteViews, isRecording, recordingTrackPaused);
updateStopButton(context, remoteViews, isRecording);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] appWidgetIds = appWidgetManager.getAppWidgetIds(
new ComponentName(context, TrackWidgetProvider.class));
for (int appWidgetId : appWidgetIds) {
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
if (large) {
boolean reportSpeed = PreferencesUtils.getBoolean(
context, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
updateAverageMovingSpeed(context, remoteViews, tripStatistics, metricUnits, reportSpeed);
updateAverageSpeed(context, remoteViews, tripStatistics, metricUnits, reportSpeed);
updateRecordStatus(context, remoteViews, isRecording, recordingTrackPaused);
}
return remoteViews;
}
/**
@@ -130,6 +150,20 @@ public class TrackWidgetProvider extends AppWidgetProvider {
remoteViews.setOnClickPendingIntent(R.id.track_widget_statistics, pendingIntent);
}
/**
* Updates moving time.
*
* @param context the context
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
*/
private void updateMovingTime(
Context context, RemoteViews remoteViews, TripStatistics tripStatistics) {
String movingTimeValue = tripStatistics == null ? context.getString(R.string.value_unknown)
: StringUtils.formatElapsedTime(tripStatistics.getMovingTime());
remoteViews.setTextViewText(R.id.track_widget_moving_time_value, movingTimeValue);
}
/**
* Updates total distance.
*
@@ -140,73 +174,10 @@ public class TrackWidgetProvider extends AppWidgetProvider {
*/
private void updateTotalDistance(Context context, RemoteViews remoteViews,
TripStatistics tripStatistics, boolean metricUnits) {
String totalDistanceValue = tripStatistics == null ? context.getString(R.string.value_unknown)
: StringUtils.formatDistance(context, tripStatistics.getTotalDistance(), metricUnits);
remoteViews.setTextViewText(R.id.track_widget_total_distance_value, totalDistanceValue);
}
/**
* Updates total time.
*
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
* @param isRecording true if recording
* @param recordingTrackPaused true if recording track is paused
*/
private void updateTotalTime(RemoteViews remoteViews, TripStatistics tripStatistics,
boolean isRecording, boolean recordingTrackPaused) {
long totalTime;
if (tripStatistics == null) {
totalTime = 0L;
} else {
totalTime = tripStatistics.getTotalTime();
if (isRecording && !recordingTrackPaused) {
totalTime += System.currentTimeMillis() - tripStatistics.getStopTime();
}
}
remoteViews.setChronometer(R.id.track_widget_total_time_value,
SystemClock.elapsedRealtime() - totalTime, null, isRecording && !recordingTrackPaused);
}
/**
* Updates average speed.
*
* @param context the context
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
* @param metricUnits true to use metric units
*/
private void updateAverageSpeed(Context context, RemoteViews remoteViews,
TripStatistics tripStatistics, boolean metricUnits) {
boolean reportSpeed = PreferencesUtils.getBoolean(
context, R.string.report_speed_key, PreferencesUtils.REPORT_SPEED_DEFAULT);
String averageSpeedLabel = context.getString(
reportSpeed ? R.string.stats_average_speed : R.string.stats_average_pace);
remoteViews.setTextViewText(R.id.track_widget_average_speed_label, averageSpeedLabel);
String averageSpeedValue = tripStatistics == null ? context.getString(R.string.value_unknown)
: StringUtils.formatSpeed(
context, tripStatistics.getAverageSpeed(), metricUnits, reportSpeed);
remoteViews.setTextViewText(R.id.track_widget_average_speed_value, averageSpeedValue);
}
/**
* Updates moving time.
*
* @param context the context
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
*/
private void updateMovingTime(
Context context, RemoteViews remoteViews, TripStatistics tripStatistics) {
boolean showMovingTime = PreferencesUtils.getBoolean(context,
R.string.stats_show_moving_time_key, PreferencesUtils.STATS_SHOW_MOVING_TIME_DEFAULT);
remoteViews.setViewVisibility(
R.id.track_widget_moving_time_container, showMovingTime ? View.VISIBLE : View.GONE);
if (showMovingTime) {
String movingTimeValue = tripStatistics == null ? context.getString(R.string.value_unknown)
: StringUtils.formatElapsedTime(tripStatistics.getMovingTime());
remoteViews.setTextViewText(R.id.track_widget_moving_time_value, movingTimeValue);
}
double distance = tripStatistics == null ? Double.NaN : tripStatistics.getTotalDistance();
String[] distanceParts = StringUtils.getDistanceParts(context, distance, metricUnits);
remoteViews.setTextViewText(R.id.track_widget_total_distance_value, distanceParts[0]);
remoteViews.setTextViewText(R.id.track_widget_total_distance_unit, distanceParts[1]);
}
/**
@@ -235,6 +206,69 @@ public class TrackWidgetProvider extends AppWidgetProvider {
remoteViews.setOnClickPendingIntent(R.id.track_widget_record_button, pendingIntent);
}
/**
* Updates the stop button.
*
* @param context the context
* @param remoteViews the remote views
* @param isRecording true if recording
*/
private void updateStopButton(Context context, RemoteViews remoteViews, boolean isRecording) {
remoteViews.setImageViewResource(
R.id.track_widget_stop_button, isRecording ? R.drawable.btn_stop_1 : R.drawable.btn_stop_0);
remoteViews.setBoolean(R.id.track_widget_stop_button, "setEnabled", isRecording);
if (isRecording) {
Intent intent = new Intent(context, ControlRecordingService.class).setAction(
context.getString(R.string.track_action_end));
PendingIntent pendingIntent = PendingIntent.getService(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.track_widget_stop_button, pendingIntent);
}
}
/**
* Updates average moving speed.
*
* @param context the context
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
* @param metricUnits true to use metric units
* @param reportSpeed true to report speed
*/
private void updateAverageMovingSpeed(Context context, RemoteViews remoteViews,
TripStatistics tripStatistics, boolean metricUnits, boolean reportSpeed) {
String averageMovingSpeedLabel = context.getString(
reportSpeed ? R.string.stats_average_moving_speed : R.string.stats_average_moving_pace);
remoteViews.setTextViewText(
R.id.track_widget_average_moving_speed_label, averageMovingSpeedLabel);
Double speed = tripStatistics == null ? Double.NaN : tripStatistics.getAverageMovingSpeed();
String[] speedParts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
remoteViews.setTextViewText(R.id.track_widget_average_moving_speed_value, speedParts[0]);
remoteViews.setTextViewText(R.id.track_widget_average_moving_speed_unit, speedParts[1]);
}
/**
* Updates average speed.
*
* @param context the context
* @param remoteViews the remote views
* @param tripStatistics the trip statistics
* @param metricUnits true to use metric units
* @param reportSpeed true to report speed
*/
private void updateAverageSpeed(Context context, RemoteViews remoteViews,
TripStatistics tripStatistics, boolean metricUnits, boolean reportSpeed) {
String averageSpeedLabel = context.getString(
reportSpeed ? R.string.stats_average_speed : R.string.stats_average_pace);
remoteViews.setTextViewText(R.id.track_widget_average_speed_label, averageSpeedLabel);
Double speed = tripStatistics == null ? Double.NaN : tripStatistics.getAverageSpeed();
String[] speedParts = StringUtils.getSpeedParts(context, speed, metricUnits, reportSpeed);
remoteViews.setTextViewText(R.id.track_widget_average_speed_value, speedParts[0]);
remoteViews.setTextViewText(R.id.track_widget_average_speed_unit, speedParts[1]);
}
/**
* Updates recording status.
*
@@ -259,24 +293,4 @@ public class TrackWidgetProvider extends AppWidgetProvider {
R.id.track_widget_record_status, context.getResources().getColor(colorId));
remoteViews.setTextViewText(R.id.track_widget_record_status, status);
}
/**
* Updates the stop button.
*
* @param context the context
* @param remoteViews the remote views
* @param isRecording true if recording
*/
private void updateStopButton(Context context, RemoteViews remoteViews, boolean isRecording) {
remoteViews.setImageViewResource(
R.id.track_widget_stop_button, isRecording ? R.drawable.btn_stop_1 : R.drawable.btn_stop_0);
remoteViews.setBoolean(R.id.track_widget_stop_button, "setEnabled", isRecording);
if (isRecording) {
Intent intent = new Intent(context, ControlRecordingService.class).setAction(
context.getString(R.string.track_action_end));
PendingIntent pendingIntent = PendingIntent.getService(
context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.track_widget_stop_button, pendingIntent);
}
}
}