Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/res/values/pref_keys.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="pref_countdown_time">pref_countdown_time</string>
<string name="pref_unit">pref_unit</string>
<string name="pref_speedunit">pref_speedunit</string>
<string name="pref_speedunit_opposite_sports">pref_speedunit_opposite_sports</string>
<string name="pref_startgps">pref_startgps</string>
<string name="pref_pollInterval">pref_pollInterval</string>
<string name="pref_pollDistance">pref_pollDistance</string>
Expand Down
2 changes: 2 additions & 0 deletions app/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@
<string name="_sign_plus" translatable="false">+</string>
<string name="_sign_minus" translatable="false">-</string>
<string name="path_simplification_default_tolerance" translatable="false">3</string>
<string name="speed_unit_opposite_activities_title">Activities using opposite speed unit</string>
<string name="speed_unit_opposite_activities_summary_empty">No activity overrides. All activities use the global speed unit.</string>
</resources>
6 changes: 6 additions & 0 deletions app/res/xml/settings_units.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,10 @@
android:title="@string/Speed_unit_preference"
app:iconSpaceReserved="false" />

<Preference
android:key="@string/pref_speedunit_opposite_sports"
android:summary="@string/speed_unit_opposite_activities_summary_empty"
android:title="@string/speed_unit_opposite_activities_title"
app:iconSpaceReserved="false" />

</PreferenceScreen>
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ public void workoutEvent(WorkoutInfo workoutInfo, int type) {
LiveService.PARAM_IN_PACE,
formatter.formatVelocityByPreferredUnit(
Formatter.Format.TXT_SHORT,
elapsedTimeMillis == 0 ? 0 : elapsedDistanceMeter * 1000.0 / elapsedTimeMillis))
elapsedTimeMillis == 0 ? 0 : elapsedDistanceMeter * 1000.0 / elapsedTimeMillis,
workoutInfo.getSport()))
.putExtra(LiveService.PARAM_IN_USERNAME, username)
.putExtra(LiveService.PARAM_IN_PASSWORD, password)
.putExtra(LiveService.PARAM_IN_SERVERADRESS, postUrl);
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/org/runnerup/notification/OngoingState.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Notification createNotification() {
Formatter.Format.TXT_LONG, Math.round(workoutInfo.getTime(Scope.ACTIVITY)));
String pace =
formatter.formatVelocityByPreferredUnit(
Formatter.Format.TXT_SHORT, workoutInfo.getSpeed(Scope.ACTIVITY));
Formatter.Format.TXT_SHORT, workoutInfo.getSpeed(Scope.ACTIVITY), workoutInfo.getSport());

String content =
String.format(
Expand All @@ -88,7 +88,7 @@ public Notification createNotification() {
distance,
context.getString(org.runnerup.common.R.string.time),
time,
context.getString(org.runnerup.common.R.string.pace),
formatter.formatVelocityLabel(workoutInfo.getSport()),
pace);
builder.setContentText(content);

Expand Down
111 changes: 95 additions & 16 deletions app/src/main/org/runnerup/util/Formatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.Locale;
import org.runnerup.R;
import org.runnerup.common.util.Constants;
import org.runnerup.common.util.Constants.DB;
import org.runnerup.workout.Dimension;
import org.runnerup.workout.SpeedUnit;

Expand Down Expand Up @@ -217,7 +218,19 @@ private static boolean guessDefaultUnit(Resources res, Editor editor) {
* @return Configured Speed Unit (falls back to pace, if the configured value is invalid)
*/
public static SpeedUnit getPreferredSpeedUnit(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
return getGlobalPreferredSpeedUnit(
context, PreferenceManager.getDefaultSharedPreferences(context));
}

public static SpeedUnit getPreferredSpeedUnit(Context context, int sport) {
SpeedUnit globalSpeedUnit = getPreferredSpeedUnit(context);
if (!usesOppositeSpeedUnit(context, sport)) {
return globalSpeedUnit;
}
return globalSpeedUnit == SpeedUnit.PACE ? SpeedUnit.SPEED : SpeedUnit.PACE;
}

private static SpeedUnit getGlobalPreferredSpeedUnit(Context context, SharedPreferences prefs) {
// use either pace or speed according to the user's preference
String speedUnit =
prefs.getString(
Expand All @@ -232,6 +245,69 @@ public static SpeedUnit getPreferredSpeedUnit(Context context) {
}
}

public static int[] getOppositeSpeedUnitSports(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String storedSports =
prefs.getString(context.getString(R.string.pref_speedunit_opposite_sports), null);
if (storedSports == null || storedSports.isEmpty()) {
return new int[0];
}
return normalizeSportIds(SafeParse.parseIntList(storedSports));
}

public static void setOppositeSpeedUnitSports(Context context, int[] sports) {
int[] normalizedSports = normalizeSportIds(sports);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
Editor editor = prefs.edit();
String key = context.getString(R.string.pref_speedunit_opposite_sports);
if (normalizedSports.length == 0) {
editor.remove(key).apply();
return;
}
editor.putString(key, SafeParse.storeIntList(normalizedSports)).apply();
}

private static boolean usesOppositeSpeedUnit(Context context, int sport) {
if (sport < 0) {
return false;
}
for (int oppositeSport : getOppositeSpeedUnitSports(context)) {
if (oppositeSport == sport) {
return true;
}
}
return false;
}

private static int[] normalizeSportIds(int[] sports) {
if (sports == null || sports.length == 0) {
return new int[0];
}

boolean[] includedSports = new boolean[DB.ACTIVITY.SPORT_MAX + 1];
for (int sport : sports) {
if (0 <= sport && sport <= DB.ACTIVITY.SPORT_MAX) {
includedSports[sport] = true;
}
}

int count = 0;
for (boolean includedSport : includedSports) {
if (includedSport) {
count++;
}
}

int[] normalizedSports = new int[count];
int index = 0;
for (int sport = 0; sport < includedSports.length; sport++) {
if (includedSports[sport]) {
normalizedSports[index++] = sport;
}
}
return normalizedSports;
}

public double getUnitMeters() {
return this.base_meters;
}
Expand Down Expand Up @@ -483,15 +559,14 @@ public String formatPace(Format target, double seconds_per_meter) {
* @return display value
*/
public String formatVelocityByPreferredUnit(Format target, double meters_per_second) {
String paceTextUnit =
this.sharedPreferences.getString(
context.getResources().getString(R.string.pref_speedunit), SpeedUnit.PACE.getValue());
assert paceTextUnit != null;
if (paceTextUnit.contentEquals(SpeedUnit.PACE.getValue())) {
return formatVelocityByPreferredUnit(target, meters_per_second, -1);
}

public String formatVelocityByPreferredUnit(Format target, double meters_per_second, int sport) {
if (getPreferredSpeedUnit(context, sport) == SpeedUnit.PACE) {
return this.formatPaceSpeed(target, meters_per_second);
} else {
return this.formatSpeed(target, meters_per_second);
}
return this.formatSpeed(target, meters_per_second);
}

/**
Expand All @@ -500,15 +575,14 @@ public String formatVelocityByPreferredUnit(Format target, double meters_per_sec
* @return value
*/
public String formatVelocityLabel() {
String paceTextUnit =
this.sharedPreferences.getString(
context.getResources().getString(R.string.pref_speedunit), SpeedUnit.PACE.getValue());
assert paceTextUnit != null;
if (paceTextUnit.contentEquals(SpeedUnit.PACE.getValue())) {
return formatVelocityLabel(-1);
}

public String formatVelocityLabel(int sport) {
if (getPreferredSpeedUnit(context, sport) == SpeedUnit.PACE) {
return this.context.getString(org.runnerup.common.R.string.Pace);
} else {
return this.context.getString(org.runnerup.common.R.string.Speed);
}
return this.context.getString(org.runnerup.common.R.string.Speed);
}

/**
Expand Down Expand Up @@ -538,11 +612,16 @@ public String formatPaceSpeed(Format target, double meters_per_second) {
*/
String getVelocityUnit(
Context context) { // Resources resources, SharedPreferences sharedPreferences) {
return getVelocityUnit(context, -1);
}

String getVelocityUnit(
Context context, int sport) { // Resources resources, SharedPreferences sharedPreferences) {
int du =
metric
? org.runnerup.common.R.string.metrics_distance_km
: org.runnerup.common.R.string.metrics_distance_mi;
switch (getPreferredSpeedUnit(context)) {
switch (getPreferredSpeedUnit(context, sport)) {
case SPEED:
return resources.getString(du)
+ "/"
Expand Down
Loading
Loading