forked from upstream-mirrors/OpenTracks
Add activity icon next to calorie in stats page
This commit is contained in:
@@ -86,6 +86,9 @@ public class CalorieUtils {
|
||||
* @param activityType the activity type
|
||||
*/
|
||||
public static ActivityType getActivityType(Context context, String activityType) {
|
||||
if (activityType == null || activityType.equals("")) {
|
||||
return ActivityType.INVALID;
|
||||
}
|
||||
if (TrackIconUtils.getIconValue(context, activityType).equals(TrackIconUtils.WALK)
|
||||
|| TrackIconUtils.getIconValue(context, activityType).equals(TrackIconUtils.RUN)) {
|
||||
return ActivityType.FOOT;
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package com.google.android.apps.mytracks.util;
|
||||
|
||||
import com.google.android.apps.mytracks.stats.TripStatistics;
|
||||
import com.google.android.apps.mytracks.util.CalorieUtils.ActivityType;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.app.Activity;
|
||||
@@ -26,6 +27,7 @@ import android.text.SpannableStringBuilder;
|
||||
import android.text.Spanned;
|
||||
import android.text.style.SuperscriptSpan;
|
||||
import android.view.View;
|
||||
import android.widget.Spinner;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.Locale;
|
||||
@@ -129,7 +131,7 @@ public class StatsUtils {
|
||||
* @param tripStatistics the trip statistics
|
||||
*/
|
||||
public static void setTripStatisticsValues(
|
||||
Context context, Activity activity, View view, TripStatistics tripStatistics) {
|
||||
Context context, Activity activity, View view, TripStatistics tripStatistics, String category) {
|
||||
boolean metricUnits = PreferencesUtils.isMetricUnits(context);
|
||||
boolean reportSpeed = PreferencesUtils.isReportSpeed(context);
|
||||
|
||||
@@ -139,9 +141,17 @@ public class StatsUtils {
|
||||
context, getView(activity, view, R.id.stats_distance), totalDistance, metricUnits);
|
||||
|
||||
// Set calorie
|
||||
double calorie = tripStatistics == null ? Double.NaN : tripStatistics.getCalorie();
|
||||
ActivityType activityType = CalorieUtils.getActivityType(context, category);
|
||||
double calorie = tripStatistics == null || activityType == ActivityType.INVALID ? Double.NaN
|
||||
: tripStatistics.getCalorie();
|
||||
setCalorie(context, getView(activity, view, R.id.stats_calorie), calorie);
|
||||
|
||||
Spinner spinner = (Spinner) getView(activity, view, R.id.stats_activity_type_icon);
|
||||
spinner.setVisibility(category != null ? View.VISIBLE : View.GONE);
|
||||
if (category != null) {
|
||||
TrackIconUtils.setIconSpinner(spinner, TrackIconUtils.getIconValue(context, category));
|
||||
}
|
||||
|
||||
// Set total time
|
||||
setTimeValue(context, getView(activity, view, R.id.stats_total_time), R.string.stats_total_time,
|
||||
tripStatistics != null ? tripStatistics.getTotalTime() : -1L);
|
||||
@@ -226,7 +236,7 @@ public class StatsUtils {
|
||||
* @param calorie the value of calorie
|
||||
*/
|
||||
private static void setCalorie(Context context, View view, double calorie) {
|
||||
String value = calorie == Double.NaN ? null
|
||||
String value = Double.isNaN(calorie) ? null
|
||||
: String.format(Locale.getDefault(), CALORIES_FORMAT, calorie);
|
||||
setItem(context, view, R.string.stats_calorie, value, context.getString(R.string.unit_calorie));
|
||||
}
|
||||
|
||||
@@ -21,8 +21,13 @@ import com.google.android.maps.mytracks.R;
|
||||
import android.content.Context;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.Bitmap.Config;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.Color;
|
||||
import android.util.Pair;
|
||||
import android.view.View;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.Spinner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -196,6 +201,35 @@ public class TrackIconUtils {
|
||||
return output;
|
||||
}
|
||||
|
||||
public static void setIconSpinner(Spinner spinner, String iconValue) {
|
||||
@SuppressWarnings("unchecked")
|
||||
ArrayAdapter<StringBuilder> adapter = (ArrayAdapter<StringBuilder>) spinner.getAdapter();
|
||||
StringBuilder stringBuilder = adapter.getItem(0);
|
||||
stringBuilder.delete(0, stringBuilder.length());
|
||||
stringBuilder.append(iconValue);
|
||||
adapter.notifyDataSetChanged();
|
||||
}
|
||||
|
||||
public static ArrayAdapter<StringBuilder> getIconSpinnerAdapter(
|
||||
final Context context, String iconValue) {
|
||||
return new ArrayAdapter<StringBuilder>(context, android.R.layout.simple_spinner_item,
|
||||
new StringBuilder[] { new StringBuilder(iconValue) }) {
|
||||
@Override
|
||||
public View getView(int position, View convertView, android.view.ViewGroup parent) {
|
||||
ImageView imageView = convertView != null ? (ImageView) convertView
|
||||
: new ImageView(getContext());
|
||||
Bitmap source = BitmapFactory.decodeResource(
|
||||
context.getResources(), TrackIconUtils.getIconDrawable(getItem(position).toString()));
|
||||
if (ApiAdapterFactory.getApiAdapter().isSpinnerBackgroundLight()) {
|
||||
source = TrackIconUtils.invertBitmap(source);
|
||||
}
|
||||
imageView.setImageBitmap(source);
|
||||
imageView.setPadding(4, 4, -4, -4);
|
||||
return imageView;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the activity type is in the list.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* Copyright 2013 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.
|
||||
*/
|
||||
|
||||
package com.google.android.apps.mytracks.util;
|
||||
|
||||
import com.google.android.apps.mytracks.content.MyTracksProviderUtils;
|
||||
import com.google.android.apps.mytracks.content.Track;
|
||||
import com.google.android.apps.mytracks.services.TrackRecordingServiceConnection;
|
||||
import com.google.android.maps.mytracks.R;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
/**
|
||||
* Utilities for updating track.
|
||||
*
|
||||
* @author Jimmy Shih
|
||||
*/
|
||||
public class TrackUtils {
|
||||
|
||||
private TrackUtils() {}
|
||||
|
||||
public static void updateTrack(Context context, Track track, String name, String category,
|
||||
String description, MyTracksProviderUtils myTracksProviderUtils,
|
||||
TrackRecordingServiceConnection trackRecordingServiceConnection) {
|
||||
if (name != null) {
|
||||
track.setName(name);
|
||||
}
|
||||
boolean newCategory = false;
|
||||
boolean isRecording = false;
|
||||
if (category != null) {
|
||||
newCategory = !category.equals(track.getCategory());
|
||||
track.setCategory(category);
|
||||
track.setIcon(TrackIconUtils.getIconValue(context, category));
|
||||
if (newCategory) {
|
||||
isRecording = track.getId()
|
||||
== PreferencesUtils.getLong(context, R.string.recording_track_id_key);
|
||||
if (!isRecording) {
|
||||
// Update calorie
|
||||
double calorie = CalorieUtils.getTrackCalorie(context, track, -1L);
|
||||
track.getTripStatistics().setCalorie(calorie);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (description != null) {
|
||||
track.setDescription(description);
|
||||
}
|
||||
track.setModifiedTime(System.currentTimeMillis());
|
||||
myTracksProviderUtils.updateTrack(track);
|
||||
|
||||
if (newCategory && isRecording) {
|
||||
// Update calorie through track recording service
|
||||
TrackRecordingServiceConnectionUtils.updateCalorie(trackRecordingServiceConnection);
|
||||
}
|
||||
|
||||
boolean driveSync = PreferencesUtils.getBoolean(
|
||||
context, R.string.drive_sync_key, PreferencesUtils.DRIVE_SYNC_DEFAULT);
|
||||
if (driveSync) {
|
||||
PreferencesUtils.addToList(context, R.string.drive_edited_list_key,
|
||||
PreferencesUtils.DRIVE_EDITED_LIST_DEFAULT, String.valueOf(track.getId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user