diff --git a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java index d1a4f5fdbd..7aa7757da1 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/ExportImportTest.java @@ -96,7 +96,7 @@ public static void preSetUp() { private final ContentProviderUtils contentProviderUtils = new ContentProviderUtils(context); - private static final ActivityType TRACK_ACTIVITY_TYPE = ActivityType.MOUNTAIN_BIKE; + private static final ActivityType TRACK_ACTIVITY_TYPE = ActivityType.MOUNTAIN_BIKING; private static final String TRACK_ACTIVITY_TYPE_LOCALIZED = "the activity type"; private static final String TRACK_DESCRIPTION = "the description"; diff --git a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/GPXTrackImporterTest.java b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/GPXTrackImporterTest.java index 9ed78f4a03..09a35936ff 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/GPXTrackImporterTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/io/file/importer/GPXTrackImporterTest.java @@ -83,7 +83,7 @@ public void gpx_with_pause_resume() throws IOException { assertEquals("the category", importedTrack.getActivityTypeLocalized()); assertEquals("the description", importedTrack.getDescription()); assertEquals("2021-01-07 22:51", importedTrack.getName()); - assertEquals("UNKNOWN", importedTrack.getActivityType()); + assertEquals(ActivityType.UNKNOWN, importedTrack.getActivityType()); //TODO Check trackstatistics @@ -137,7 +137,7 @@ public void gpx_without_speed() throws IOException { assertEquals("", importedTrack.getActivityTypeLocalized()); assertEquals("", importedTrack.getDescription()); assertEquals("20210907_213924.gpx", importedTrack.getName()); - assertEquals("", importedTrack.getActivityType()); + assertEquals(ActivityType.UNKNOWN, importedTrack.getActivityType()); // 3. trackstatistics TrackStatistics trackStatistics = importedTrack.getTrackStatistics(); diff --git a/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsTest.java b/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsTest.java index 308a141fc1..f6bbdee6c7 100644 --- a/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsTest.java +++ b/src/androidTest/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsTest.java @@ -243,8 +243,8 @@ public void testAggregate_severalTracksWithSeveralActivities() { // Check order { - assertEquals(biking, aggregatedStatistics.getItem(0).getActivityType()); - assertEquals(driving, aggregatedStatistics.getItem(3).getActivityType()); + assertEquals(biking, aggregatedStatistics.getItem(0).getActivityTypeLocalized()); + assertEquals(driving, aggregatedStatistics.getItem(3).getActivityTypeLocalized()); } } } diff --git a/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java b/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java index 32fce8b3a0..1288ac5477 100644 --- a/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java +++ b/src/main/java/de/dennisguse/opentracks/TrackEditActivity.java @@ -97,7 +97,7 @@ protected void onCreate(Bundle bundle) { } setActivityTypeIcon(activityType); - viewBinding.fields.trackEditActivityTypeIcon.setOnClickListener(v -> ChooseActivityTypeDialogFragment.showDialog(getSupportFragmentManager(), viewBinding.fields.trackEditActivityType.getText().toString())); + viewBinding.fields.trackEditActivityTypeIcon.setOnClickListener(v -> ChooseActivityTypeDialogFragment.showDialog(getSupportFragmentManager(), this, viewBinding.fields.trackEditActivityType.getText().toString())); viewBinding.fields.trackEditDescription.setText(track.getDescription()); @@ -146,6 +146,6 @@ private void setActivityTypeIcon(ActivityType activityType) { @Override public void onChooseActivityTypeDone(ActivityType activityType) { setActivityTypeIcon(activityType); - viewBinding.fields.trackEditActivityType.setText(getString(activityType.getFirstLocalizedStringId())); + viewBinding.fields.trackEditActivityType.setText(getString(activityType.getLocalizedStringId())); } } diff --git a/src/main/java/de/dennisguse/opentracks/TrackRecordingActivity.java b/src/main/java/de/dennisguse/opentracks/TrackRecordingActivity.java index 60ab6d182e..d6e6860081 100644 --- a/src/main/java/de/dennisguse/opentracks/TrackRecordingActivity.java +++ b/src/main/java/de/dennisguse/opentracks/TrackRecordingActivity.java @@ -319,7 +319,7 @@ public TrackDataHub getTrackDataHub() { @Override public void onChooseActivityTypeDone(ActivityType activityType) { Track track = contentProviderUtils.getTrack(trackId); - String activityTypeLocalized = getString(activityType.getFirstLocalizedStringId()); + String activityTypeLocalized = getString(activityType.getLocalizedStringId()); TrackUtils.updateTrack(this, track, null, activityTypeLocalized, null, contentProviderUtils); } diff --git a/src/main/java/de/dennisguse/opentracks/TrackStoppedActivity.java b/src/main/java/de/dennisguse/opentracks/TrackStoppedActivity.java index 42956a6bcb..14becbb88a 100644 --- a/src/main/java/de/dennisguse/opentracks/TrackStoppedActivity.java +++ b/src/main/java/de/dennisguse/opentracks/TrackStoppedActivity.java @@ -65,7 +65,7 @@ protected void onCreate(Bundle savedInstanceState) { }); setActivityTypeIcon(track.getActivityType()); - viewBinding.trackEditActivityTypeIcon.setOnClickListener(v -> ChooseActivityTypeDialogFragment.showDialog(getSupportFragmentManager(), viewBinding.trackEditActivityType.getText().toString())); + viewBinding.trackEditActivityTypeIcon.setOnClickListener(v -> ChooseActivityTypeDialogFragment.showDialog(getSupportFragmentManager(), this, viewBinding.trackEditActivityType.getText().toString())); viewBinding.trackEditDescription.setText(track.getDescription()); @@ -74,7 +74,7 @@ protected void onCreate(Bundle savedInstanceState) { { Pair parts = SpeedFormatter.Builder() .setUnit(PreferencesUtils.getUnitSystem()) - .setReportSpeedOrPace(PreferencesUtils.isReportSpeed(track.getActivityTypeLocalized())) + .setReportSpeedOrPace(PreferencesUtils.isReportSpeed(track)) .build(this) .getSpeedParts(track.getTrackStatistics().getAverageMovingSpeed()); viewBinding.speed.setText(parts.first); @@ -132,7 +132,7 @@ private void setActivityTypeIcon(ActivityType activityType) { @Override public void onChooseActivityTypeDone(ActivityType activityType) { setActivityTypeIcon(activityType); - viewBinding.trackEditActivityType.setText(getString(activityType.getFirstLocalizedStringId())); + viewBinding.trackEditActivityType.setText(getString(activityType.getLocalizedStringId())); } private void resumeTrackAndFinish() { diff --git a/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java b/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java index 4fce435b73..bea47c7fe3 100644 --- a/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java +++ b/src/main/java/de/dennisguse/opentracks/chart/ChartFragment.java @@ -65,7 +65,7 @@ public static ChartFragment newInstance(boolean chartByDistance) { // Stats gathered from the received data private final List pendingPoints = new ArrayList<>(); - private String category = ""; + private String activityTypeLocalized = ""; // Modes of operation private boolean chartByDistance; @@ -87,7 +87,7 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin } } if (PreferencesUtils.isKey(R.string.stats_rate_key, key)) { - boolean reportSpeed = PreferencesUtils.isReportSpeed(category); + boolean reportSpeed = PreferencesUtils.isReportSpeed(activityTypeLocalized); if (reportSpeed != viewBinding.chartView.getReportSpeed()) { viewBinding.chartView.setReportSpeed(reportSpeed); viewBinding.chartView.applyReportSpeed(); @@ -160,12 +160,12 @@ public void onDestroyView() { public void onTrackUpdated(Track track) { if (isResumed()) { if (track == null) { - category = ""; + activityTypeLocalized = ""; return; } - category = track.getActivityTypeLocalized(); - boolean reportSpeed = PreferencesUtils.isReportSpeed(category); + activityTypeLocalized = track.getActivityTypeLocalized(); + boolean reportSpeed = PreferencesUtils.isReportSpeed(activityTypeLocalized); if (reportSpeed != viewBinding.chartView.getReportSpeed()) { viewBinding.chartView.setReportSpeed(reportSpeed); viewBinding.chartView.applyReportSpeed(); diff --git a/src/main/java/de/dennisguse/opentracks/data/models/ActivityIcon.java b/src/main/java/de/dennisguse/opentracks/data/models/ActivityIcon.java new file mode 100644 index 0000000000..23b339b5bb --- /dev/null +++ b/src/main/java/de/dennisguse/opentracks/data/models/ActivityIcon.java @@ -0,0 +1,35 @@ +package de.dennisguse.opentracks.data.models; + +import de.dennisguse.opentracks.R; + +public enum ActivityIcon { + AIRPLANE("AIRPLANE", R.drawable.ic_activity_flight_24dp), + BIKE("BIKE", R.drawable.ic_activity_bike_24dp), + MOUNTAIN_BIKE("MOUNTAIN_BIKE", R.drawable.ic_activity_mtb_24dp), + MOTOR_BIKE("MOTOR_BIKE", R.drawable.ic_activity_motorbike_24dp), + KAYAK("KAYAK", R.drawable.ic_activity_kayaking_24dp), + BOAT("BOAT", R.drawable.ic_activity_boat_24dp), + SAILING("SAILING", R.drawable.ic_activity_sailing_24dp), + DRIVE("DRIVE", R.drawable.ic_activity_drive_24dp), + RUN("RUN", R.drawable.ic_activity_run_24dp), + SKI("SKI", R.drawable.ic_activity_skiing_24dp), + SNOW_BOARDING("SNOW_BOARDING", R.drawable.ic_activity_snowboarding_24dp), + UNKNOWN("UNKNOWN", R.drawable.ic_logo_24dp), + WALK("WALK", R.drawable.ic_activity_walk_24dp), + ESCOOTER("ESCOOTER", R.drawable.ic_activity_escooter_24dp), + INLINE_SKATING("INLINES_SKATING", R.drawable.ic_activity_inline_skating_24dp), + SKATE_BOARDING("SKATE_BOARDING", R.drawable.ic_activity_skateboarding_24dp), + CLIMBING("CLIMBING", R.drawable.ic_activity_climbing_24dp), + SWIMMING("SWIMMING", R.drawable.ic_activity_swimming_24dp), + SWIMMING_OPEN("SWIMMING_OPEN", R.drawable.ic_activity_swimming_open_24dp), + WORKOUT("WORKOUT", R.drawable.ic_activity_workout_24dp); + + @Deprecated //TODO should be removed. + final String iconId; + final int iconDrawableId; + + ActivityIcon(String iconId, int iconDrawableId) { + this.iconId = iconId; + this.iconDrawableId = iconDrawableId; + } +} diff --git a/src/main/java/de/dennisguse/opentracks/data/models/ActivityType.java b/src/main/java/de/dennisguse/opentracks/data/models/ActivityType.java index cde74d5572..9e607c5c3c 100644 --- a/src/main/java/de/dennisguse/opentracks/data/models/ActivityType.java +++ b/src/main/java/de/dennisguse/opentracks/data/models/ActivityType.java @@ -5,7 +5,6 @@ import androidx.annotation.NonNull; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -13,77 +12,126 @@ import de.dennisguse.opentracks.R; public enum ActivityType { - AIRPLANE("AIRPLANE", R.drawable.ic_activity_flight_24dp, true, R.string.activity_type_airplane, R.string.activity_type_commercial_airplane, R.string.activity_type_rc_airplane), - BIKE("BIKE", R.drawable.ic_activity_bike_24dp, true, R.string.activity_type_biking, R.string.activity_type_cycling, R.string.activity_type_dirt_bike, R.string.activity_type_road_biking, R.string.activity_type_track_cycling), - MOUNTAIN_BIKE("MOUNTAIN_BIKE", R.drawable.ic_activity_mtb_24dp, true, R.string.activity_type_mountain_biking), - MOTOR_BIKE("MOTOR_BIKE", R.drawable.ic_activity_motorbike_24dp, true, R.string.activity_type_motor_bike), - KAYAK("KAYAK", R.drawable.ic_activity_kayaking_24dp, true, R.string.activity_type_kayaking), - BOAT("BOAT", R.drawable.ic_activity_boat_24dp, true, R.string.activity_type_boat, R.string.activity_type_ferry, R.string.activity_type_motor_boating, R.string.activity_type_rc_boat), - SAILING("SAILING", R.drawable.ic_activity_sailing_24dp, true, R.string.activity_type_sailing), - DRIVE("DRIVE", R.drawable.ic_activity_drive_24dp, true, R.string.activity_type_atv, R.string.activity_type_driving, R.string.activity_type_driving_bus, R.string.activity_type_driving_car), - RUN("RUN", R.drawable.ic_activity_run_24dp, false, R.string.activity_type_running, R.string.activity_type_street_running, R.string.activity_type_track_running, R.string.activity_type_trail_running), - SKI("SKI", R.drawable.ic_activity_skiing_24dp, true, R.string.activity_type_cross_country_skiing, R.string.activity_type_skiing), - SNOW_BOARDING("SNOW_BOARDING", R.drawable.ic_activity_snowboarding_24dp, true, R.string.activity_type_snow_boarding), - UNKNOWN("UNKNOWN", R.drawable.ic_logo_24dp, true, R.string.activity_type_unknown), - WALK("WALK", R.drawable.ic_activity_walk_24dp, false, R.string.activity_type_hiking, R.string.activity_type_off_trail_hiking, R.string.activity_type_speed_walking, R.string.activity_type_trail_hiking, R.string.activity_type_walking), - ESCOOTER("ESCOOTER", R.drawable.ic_activity_escooter_24dp, true, R.string.activity_type_escooter), - INLINE_SKATING("INLINES_SKATING", R.drawable.ic_activity_inline_skating_24dp, true, R.string.activity_type_inline_skating), - SKATE_BOARDING("SKATE_BOARDING", R.drawable.ic_activity_skateboarding_24dp, true, R.string.activity_type_skate_boarding), - CLIMBING("CLIMBING", R.drawable.ic_activity_climbing_24dp, false, R.string.activity_type_climbing), - SWIMMING("SWIMMING", R.drawable.ic_activity_swimming_24dp, false, R.string.activity_type_swimming), - SWIMMING_OPEN("SWIMMING_OPEN", R.drawable.ic_activity_swimming_open_24dp, false, R.string.activity_type_swimming_open), - WORKOUT("WORKOUT", R.drawable.ic_activity_workout_24dp, false, R.string.activity_type_workout); - - final String iconId; - final int iconDrawableId; - final boolean showSpeedPreferred; - final int[] localizedStringIds; - //isSpeed? + AIRPLANE("airplane", R.string.activity_type_airplane, ActivityIcon.AIRPLANE, true), + ATV("ATV", R.string.activity_type_atv, ActivityIcon.DRIVE, true), + BIKING("biking", R.string.activity_type_biking, ActivityIcon.BIKE, true), + BLIMP("blimp", R.string.activity_type_blimp, ActivityIcon.UNKNOWN, true), + BOAT("boat", R.string.activity_type_boat, ActivityIcon.BOAT, true), + CLIMBING("climbing", R.string.activity_type_climbing, ActivityIcon.CLIMBING, false), + COMMERCIAL_AIRPLANE("commercial airplane", R.string.activity_type_commercial_airplane, ActivityIcon.AIRPLANE, true), + CROSS_COUNTRY_SKIING("cross-country skiing", R.string.activity_type_cross_country_skiing, ActivityIcon.SKI, true), + CYCLING("cycling", R.string.activity_type_cycling, ActivityIcon.BIKE, true), + DIRT_BIKE("dirt bike", R.string.activity_type_dirt_bike, ActivityIcon.MOTOR_BIKE, true), + DONKEY_BACK_RIDING("donkey back riding", R.string.activity_type_donkey_back_riding, ActivityIcon.UNKNOWN, true), + DRIVING("driving", R.string.activity_type_driving, ActivityIcon.DRIVE, true), + DRIVING_BUS("driving bus", R.string.activity_type_driving_bus, ActivityIcon.DRIVE, true), + DRIVING_CAR("driving car", R.string.activity_type_driving_car, ActivityIcon.DRIVE, true), + ESCOOTER("escooter", R.string.activity_type_escooter, ActivityIcon.ESCOOTER, true), + FERRY("ferry", R.string.activity_type_ferry, ActivityIcon.BOAT, true), + FRISBEE("frisbee", R.string.activity_type_frisbee, ActivityIcon.UNKNOWN, true), + GLIDING("gliding", R.string.activity_type_gliding, ActivityIcon.UNKNOWN, true), + HANG_GLIDING("hang gliding", R.string.activity_type_hang_gliding, ActivityIcon.UNKNOWN, true), + HELICOPTER("helicopter", R.string.activity_type_helicopter, ActivityIcon.UNKNOWN, true), + HIKING("hiking", R.string.activity_type_hiking, ActivityIcon.WALK, false), + HORSE_BACK_RIDING("horse back riding", R.string.activity_type_horse_back_riding, ActivityIcon.UNKNOWN, true), + HOT_AIR_BALLOON("hot air balloon", R.string.activity_type_hot_air_balloon, ActivityIcon.UNKNOWN, true), + ICE_SAILING("ice sailing", R.string.activity_type_ice_sailing, ActivityIcon.UNKNOWN, true), + INLINE_SKATING("inline skating", R.string.activity_type_inline_skating, ActivityIcon.INLINE_SKATING, true), + KAYAKING("kayaking", R.string.activity_type_kayaking, ActivityIcon.KAYAK, true), + KITE_SURFING("kite surfing", R.string.activity_type_kite_surfing, ActivityIcon.UNKNOWN, true), + LAND_SAILING("land sailing", R.string.activity_type_land_sailing, ActivityIcon.UNKNOWN, true), + MIXED_TYPE("mixed type", R.string.activity_type_mixed_type, ActivityIcon.UNKNOWN, true), + MOTOR_BIKE("motor bike", R.string.activity_type_motor_bike, ActivityIcon.MOTOR_BIKE, true), + MOTOR_BOATING("motor boating", R.string.activity_type_motor_boating, ActivityIcon.BOAT, true), + MOUNTAIN_BIKING("mountain biking", R.string.activity_type_mountain_biking, ActivityIcon.MOUNTAIN_BIKE, true), + OFF_TRAIL_HIKING("off trail hiking", R.string.activity_type_hiking, ActivityIcon.WALK, false), + OTHER("other", R.string.activity_type_other, ActivityIcon.UNKNOWN, true), + PADDLING("paddling", R.string.activity_type_paddling, ActivityIcon.UNKNOWN, true), + PARA_GLIDING("para gliding", R.string.activity_type_para_gliding, ActivityIcon.UNKNOWN, true), + RC_AIRPLANE("RC airplane", R.string.activity_type_rc_airplane, ActivityIcon.AIRPLANE, true), + RC_BOAT("RC boat", R.string.activity_type_rc_boat, ActivityIcon.BOAT, true), + RC_HELICOPTER("RC helicopter", R.string.activity_type_rc_helicopter, ActivityIcon.UNKNOWN, true), + RIDING("riding", R.string.activity_type_horse_back_riding, ActivityIcon.UNKNOWN, true), + ROAD_BIKING("road biking", R.string.activity_type_road_biking, ActivityIcon.BIKE, true), + ROLLER_SKIING("roller skiing", R.string.activity_type_roller_skiing, ActivityIcon.UNKNOWN, true), + ROWING("rowing", R.string.activity_type_rowing, ActivityIcon.UNKNOWN, true), + RUNNING("running", R.string.activity_type_running, ActivityIcon.RUN, false), + SAILING("sailing", R.string.activity_type_sailing, ActivityIcon.SAILING, true), + SEAPLANE("seaplane", R.string.activity_type_seaplane, ActivityIcon.AIRPLANE, true), + SKATE_BOARDING("skate boarding", R.string.activity_type_skate_boarding, ActivityIcon.SKATE_BOARDING, true), + SKATING("skating", R.string.activity_type_skating, ActivityIcon.UNKNOWN, true), + SKIING("skiing", R.string.activity_type_skiing, ActivityIcon.SKI, true), + SKY_JUMPING("sky jumping", R.string.activity_type_sky_jumping, ActivityIcon.UNKNOWN, true), + SLED("sled", R.string.activity_type_sled, ActivityIcon.UNKNOWN, true), + SNOW_BOARDING("snow boarding", R.string.activity_type_snow_boarding, ActivityIcon.SNOW_BOARDING, true), + SNOW_SHOEING("snow shoeing", R.string.activity_type_snow_shoeing, ActivityIcon.UNKNOWN, true), + SPEED_WALKING("speed walking", R.string.activity_type_speed_walking, ActivityIcon.WALK, false), + STREET_RUNNING("street running", R.string.activity_type_street_running, ActivityIcon.RUN, false), + SURFING("surfing", R.string.activity_type_surfing, ActivityIcon.UNKNOWN, true), + TRACK_CYCLING("track cycling", R.string.activity_type_track_cycling, ActivityIcon.BIKE, true), + TRACK_RUNNING("track running", R.string.activity_type_trail_running, ActivityIcon.RUN, false), + TRAIL_HIKING("trail hiking", R.string.activity_type_trail_hiking, ActivityIcon.WALK, false), + TRAIL_RUNNING("trail running", R.string.activity_type_trail_running, ActivityIcon.RUN, false), + TRAIN("train", R.string.activity_type_train, ActivityIcon.UNKNOWN, true), + ULTIMATE_FRISBEE("ultimate frisbee", R.string.activity_type_ultimate_frisbee, ActivityIcon.UNKNOWN, true), + WAKEBOARDING("wakeboarding", R.string.activity_type_wakeboarding, ActivityIcon.UNKNOWN, true), + WALKING("walking", R.string.activity_type_walking, ActivityIcon.WALK, false), + WATER_SKIING("water skiing", R.string.activity_type_water_skiing, ActivityIcon.UNKNOWN, true), + WIND_SURFING("wind surfing", R.string.activity_type_wind_surfing, ActivityIcon.UNKNOWN, true), + SWIMMING("swimming", R.string.activity_type_swimming, ActivityIcon.SWIMMING, false), + SWIMMING_OPEN("swimming in open water", R.string.activity_type_swimming_open, ActivityIcon.SWIMMING_OPEN, false), + WORKOUT("workout", R.string.activity_type_workout, ActivityIcon.WORKOUT, false), + UNKNOWN("unknown", R.string.activity_type_unknown, ActivityIcon.UNKNOWN, true); + + final String id; + + final ActivityIcon icon; + final boolean showSpeedPreferred; + final int localizedStringId; - ActivityType(String iconId, int iconDrawableId, boolean showSpeedPreferred, int... localizedStringIds) { - this.iconId = iconId; - this.iconDrawableId = iconDrawableId; + ActivityType(String id, int localizedStringId, ActivityIcon icon, boolean showSpeedPreferred) { + this.id = id; + this.localizedStringId = localizedStringId; + this.icon = icon; this.showSpeedPreferred = showSpeedPreferred; - this.localizedStringIds = localizedStringIds; } - public String getIconId() { - return iconId; + public String getId() { + return id; } - public int getIconDrawableId() { - return iconDrawableId; + @Deprecated + public String getIconId() { + return icon.iconId; } - public boolean isShowSpeedPreferred() { - return showSpeedPreferred; + public int getIconDrawableId() { + return icon.iconDrawableId; } - public int[] getLocalizedStringIds() { - return localizedStringIds; + public int getLocalizedStringId() { + return localizedStringId; } - public int getFirstLocalizedStringId() { - return localizedStringIds[0]; + public boolean isShowSpeedPreferred() { + return showSpeedPreferred; } public static List getLocalizedStrings(Context context) { - List result = new ArrayList<>(); - for (ActivityType activityType : values()) { - List strings = Arrays.stream(activityType.localizedStringIds) - .mapToObj(context::getString) - .collect(Collectors.toList()); - result.addAll(strings); - } - - return result; + return Arrays.stream(values()) + .map(ActivityType::getLocalizedStringId) + .map(context::getString) + .collect(Collectors.toList()); } @NonNull public static ActivityType findBy(String iconId) { + if (ActivityIcon.UNKNOWN.iconId.equals(iconId)) { + return ActivityType.UNKNOWN; + } return Arrays.stream(ActivityType.values()).filter( - it -> it.getIconId().equals(iconId) + it -> it.icon.iconId.equals(iconId) ).findFirst() .orElse(ActivityType.UNKNOWN); } @@ -94,10 +142,8 @@ public static ActivityType findByLocalizedString(Context context, String localiz public static ActivityType findByLocalizedString(Resources resources, String localizedActivityType) { return Arrays.stream(ActivityType.values()) - .filter( - it -> Arrays.stream(it.getLocalizedStringIds()) - .anyMatch(id -> resources.getString(id).equals(localizedActivityType)) - ).findFirst() + .filter(it -> resources.getString(it.getLocalizedStringId()).equals(localizedActivityType)) + .findFirst() .orElse(ActivityType.UNKNOWN); } } diff --git a/src/main/java/de/dennisguse/opentracks/fragments/ChooseActivityTypeDialogFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/ChooseActivityTypeDialogFragment.java index 6048113b79..1aacf353fa 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/ChooseActivityTypeDialogFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/ChooseActivityTypeDialogFragment.java @@ -13,43 +13,65 @@ import androidx.fragment.app.FragmentManager; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; -import java.util.stream.Collectors; import de.dennisguse.opentracks.R; import de.dennisguse.opentracks.data.models.ActivityType; import de.dennisguse.opentracks.databinding.ChooseActivityTypeBinding; -/** - * A DialogFragment to choose an activity type. - */ public class ChooseActivityTypeDialogFragment extends DialogFragment implements AdapterView.OnItemClickListener { private static final String CHOOSE_ACTIVITY_TYPE_DIALOG_TAG = "chooseActivityType"; - public static void showDialog(FragmentManager fragmentManager, String preselectedCategory) { - new ChooseActivityTypeDialogFragment(preselectedCategory).show(fragmentManager, ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG); + @Deprecated + public static void showDialog(FragmentManager fragmentManager, Context context, String activityTypeLocalized) { + ActivityType activityType = ActivityType.findByLocalizedString(context, activityTypeLocalized); + showDialog(fragmentManager, activityType); } - private static int getPosition(Context context, String category) { - if (category == null) { + public static void showDialog(FragmentManager fragmentManager, ActivityType activityType) { + new ChooseActivityTypeDialogFragment(activityType).show(fragmentManager, ChooseActivityTypeDialogFragment.CHOOSE_ACTIVITY_TYPE_DIALOG_TAG); + } + + private static final List activityTypes = List.of( + ActivityType.UNKNOWN, + ActivityType.AIRPLANE, + ActivityType.BIKING, + ActivityType.MOUNTAIN_BIKING, + ActivityType.MOTOR_BIKE, + ActivityType.KAYAKING, + ActivityType.BOAT, + ActivityType.SAILING, + ActivityType.DRIVING, + ActivityType.RUNNING, + ActivityType.SNOW_BOARDING, + ActivityType.SKIING, + ActivityType.WALKING, + ActivityType.ESCOOTER, + ActivityType.INLINE_SKATING, + ActivityType.SKATE_BOARDING, + ActivityType.CLIMBING, + ActivityType.SWIMMING, + ActivityType.SWIMMING_OPEN, + ActivityType.WORKOUT + ); + + private static int getPosition(Context context, ActivityType activityType) { + if (activityType == null) { return -1; } - String iconValue = ActivityType.findByLocalizedString(context, category) - .getIconId(); - return getAllActivityTypeIds().indexOf(iconValue); + return activityTypes.indexOf(activityType); } private ChooseActivityTypeBinding viewBinding; - private final String preselectedCategory; + private final ActivityType preselectedActivityType; private ChooseActivityTypeCaller chooseActivityTypeCaller; - private ChooseActivityTypeDialogFragment(String preselectedCategory) { - this.preselectedCategory = preselectedCategory; + private ChooseActivityTypeDialogFragment(ActivityType activityType) { + this.preselectedActivityType = activityType; } @NonNull @@ -65,13 +87,12 @@ public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, viewBinding = ChooseActivityTypeBinding.inflate(inflater, container, false); List iconDrawableIds = new ArrayList<>(); - for (String iconValue : getAllActivityTypeIds()) { - iconDrawableIds.add(ActivityType.findBy(iconValue) - .getIconDrawableId()); + for (ActivityType activityType : activityTypes) { + iconDrawableIds.add(activityType.getIconDrawableId()); } final ChooseActivityTypeImageAdapter imageAdapter = new ChooseActivityTypeImageAdapter(iconDrawableIds); - int position = getPosition(getContext(), preselectedCategory); + int position = getPosition(getContext(), preselectedActivityType); if (position != -1) { imageAdapter.setSelected(position); } @@ -98,17 +119,10 @@ public void onDestroyView() { @Override public void onItemClick(AdapterView adapterView, View view, int position, long id) { - chooseActivityTypeCaller.onChooseActivityTypeDone(ActivityType.findBy(getAllActivityTypeIds().get(position))); + chooseActivityTypeCaller.onChooseActivityTypeDone(activityTypes.get(position)); dismiss(); } - // TODO Return List - public static List getAllActivityTypeIds() { - return Arrays.stream(ActivityType.values()) - .map(ActivityType::getIconId) - .collect(Collectors.toList()); - } - /** * Interface for chooseActivityTypeCaller of this dialog fragment. */ diff --git a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java index f6d1c14f86..98029379fe 100644 --- a/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java +++ b/src/main/java/de/dennisguse/opentracks/fragments/StatisticsRecordedFragment.java @@ -90,7 +90,7 @@ public static StatisticsRecordedFragment newInstance(Track.Id trackId) { if (PreferencesUtils.isKey(R.string.stats_rate_key, key) && track != null) { updateUInecessary = true; - preferenceReportSpeed = PreferencesUtils.isReportSpeed(track.getActivityTypeLocalized()); + preferenceReportSpeed = PreferencesUtils.isReportSpeed(track); } if (key != null && updateUInecessary && isResumed()) { diff --git a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java index 550d65fac4..d660c7c216 100644 --- a/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java +++ b/src/main/java/de/dennisguse/opentracks/services/TrackRecordingManager.java @@ -72,9 +72,9 @@ Track.Id startNewTrack(TrackPointCreator trackPointCreator) { onNewTrackPoint(segmentStartTrackPoint); - String category = PreferencesUtils.getDefaultActivityTypeLocalized(); - track.setActivityTypeLocalized(category); - track.setActivityType(ActivityType.findByLocalizedString(context, category)); + String activityTypeLocalized = PreferencesUtils.getDefaultActivityTypeLocalized(); + track.setActivityTypeLocalized(activityTypeLocalized); + track.setActivityType(ActivityType.findByLocalizedString(context, activityTypeLocalized)); track.setTrackStatistics(trackStatisticsUpdater.getTrackStatistics()); //TODO Pass TrackPoint track.setName(TrackNameUtils.getTrackName(context, trackId, track.getStartTime())); diff --git a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java index 3bd3c1a959..bc8594d617 100644 --- a/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java +++ b/src/main/java/de/dennisguse/opentracks/services/announcement/VoiceAnnouncement.java @@ -182,7 +182,7 @@ public void announce(@NonNull Track track) { sensorStatistics = contentProviderUtils.getSensorStats(track.getId()); } - Spannable announcement = VoiceAnnouncementUtils.getAnnouncement(context, track.getTrackStatistics(), PreferencesUtils.getUnitSystem(), PreferencesUtils.isReportSpeed(track.getActivityTypeLocalized()), lastInterval, sensorStatistics); + Spannable announcement = VoiceAnnouncementUtils.getAnnouncement(context, track.getTrackStatistics(), PreferencesUtils.getUnitSystem(), PreferencesUtils.isReportSpeed(track), lastInterval, sensorStatistics); if (announcement.length() > 0) { // We don't care about the utterance id. It is supplied here to force onUtteranceCompleted to be called. diff --git a/src/main/java/de/dennisguse/opentracks/settings/ActivityTypePreference.java b/src/main/java/de/dennisguse/opentracks/settings/ActivityTypePreference.java index b80fca9187..088874e925 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/ActivityTypePreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/ActivityTypePreference.java @@ -77,8 +77,8 @@ protected void onBindDialogView(View view) { final Context context = getActivity(); textView = view.findViewById(R.id.activity_type_preference_text_view); - String category = PreferencesUtils.getDefaultActivityTypeLocalized(); - textView.setText(category); + String activityTypeLocalized = PreferencesUtils.getDefaultActivityTypeLocalized(); + textView.setText(activityTypeLocalized); ArrayAdapter adapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, ActivityType.getLocalizedStrings(context)); textView.setAdapter(adapter); textView.setOnItemClickListener((parent, v, position, id) -> { @@ -97,12 +97,12 @@ protected void onBindDialogView(View view) { iconView = view.findViewById(R.id.activity_type_preference_spinner); iconView.setOnClickListener((it) -> showIconSelectDialog()); - updateIcon(ActivityType.findByLocalizedString(context, category)); + updateIcon(ActivityType.findByLocalizedString(context, activityTypeLocalized)); } private void showIconSelectDialog() { String category = PreferencesUtils.getDefaultActivityTypeLocalized(); - ChooseActivityTypeDialogFragment.showDialog(getActivity().getSupportFragmentManager(), category); + ChooseActivityTypeDialogFragment.showDialog(getActivity().getSupportFragmentManager(), getContext(), category); } @Override @@ -118,7 +118,7 @@ public void onDialogClosed(boolean positiveResult) { public void updateUI(ActivityType activityType) { updateIcon(activityType); - textView.setText(getActivity().getString(activityType.getFirstLocalizedStringId())); + textView.setText(getActivity().getString(activityType.getLocalizedStringId())); textView.clearFocus(); } diff --git a/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java b/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java index 4ab1146c1a..be7cea61e6 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java +++ b/src/main/java/de/dennisguse/opentracks/settings/PreferencesUtils.java @@ -46,6 +46,7 @@ import de.dennisguse.opentracks.data.models.HeartRate; import de.dennisguse.opentracks.data.models.HeartRateZones; import de.dennisguse.opentracks.data.models.Speed; +import de.dennisguse.opentracks.data.models.Track; import de.dennisguse.opentracks.io.file.TrackFileFormat; import de.dennisguse.opentracks.io.file.TrackFilenameGenerator; import de.dennisguse.opentracks.ui.customRecordingLayout.CsvLayoutUtils; @@ -233,17 +234,21 @@ public static void applyDefaultUnit() { } } - public static boolean isReportSpeed(String category) { + public static boolean isReportSpeed(String activityTypeLocalized) { final String STATS_RATE_DEFAULT = resources.getString(R.string.stats_rate_default); String currentStatsRate = getString(R.string.stats_rate_key, STATS_RATE_DEFAULT); if (currentStatsRate.equals(getString(R.string.stats_rate_speed_or_pace_default, STATS_RATE_DEFAULT))) { - return ActivityType.findByLocalizedString(resources, category) + return ActivityType.findByLocalizedString(resources, activityTypeLocalized) .isShowSpeedPreferred(); } return currentStatsRate.equals(resources.getString(R.string.stats_rate_speed)); } + public static boolean isReportSpeed(Track track) { + return isReportSpeed(track.getActivityTypeLocalized()); + } + private static String getBluetoothSensorAddressNone() { return resources.getString(R.string.sensor_type_value_none); } diff --git a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatistics.java b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatistics.java index 2f7b553ce0..8de17659d9 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatistics.java +++ b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatistics.java @@ -25,7 +25,7 @@ public AggregatedStatistics(@NonNull List tracks) { dataList.addAll(dataMap.values()); dataList.sort((o1, o2) -> { if (o1.getCountTracks() == o2.getCountTracks()) { - return o1.getActivityType().compareTo(o2.getActivityType()); + return o1.getActivityTypeLocalized().compareTo(o2.getActivityTypeLocalized()); } return (o1.getCountTracks() < o2.getCountTracks() ? 1 : -1); }); @@ -33,11 +33,11 @@ public AggregatedStatistics(@NonNull List tracks) { @VisibleForTesting public void aggregate(@NonNull Track track) { - String activityType = track.getActivityTypeLocalized(); - if (dataMap.containsKey(activityType)) { - dataMap.get(activityType).add(track.getTrackStatistics()); + String activityTypeLocalized = track.getActivityTypeLocalized(); + if (dataMap.containsKey(activityTypeLocalized)) { + dataMap.get(activityTypeLocalized).add(track.getTrackStatistics()); } else { - dataMap.put(activityType, new AggregatedStatistic(activityType, track.getTrackStatistics())); + dataMap.put(activityTypeLocalized, new AggregatedStatistic(activityTypeLocalized, track.getTrackStatistics())); } } @@ -54,17 +54,17 @@ public AggregatedStatistic getItem(int position) { } public static class AggregatedStatistic { - private final String activityType; + private final String activityTypeLocalized; private final TrackStatistics trackStatistics; private int countTracks = 1; - public AggregatedStatistic(String activityType, TrackStatistics trackStatistics) { - this.activityType = activityType; + public AggregatedStatistic(String activityTypeLocalized, TrackStatistics trackStatistics) { + this.activityTypeLocalized = activityTypeLocalized; this.trackStatistics = trackStatistics; } - public String getActivityType() { - return activityType; + public String getActivityTypeLocalized() { + return activityTypeLocalized; } public TrackStatistics getTrackStatistics() { diff --git a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java index 4b74798eec..63ee7b3eb9 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java +++ b/src/main/java/de/dennisguse/opentracks/ui/aggregatedStatistics/AggregatedStatisticsAdapter.java @@ -64,7 +64,7 @@ public View getView(int position, View convertView, ViewGroup parent) { } Resources resources = context.getResources(); - String localizedActivityType = aggregatedStatistic.getActivityType(); + String localizedActivityType = aggregatedStatistic.getActivityTypeLocalized(); if (ActivityType.findByLocalizedString(resources, localizedActivityType) .isShowSpeedPreferred()) { viewHolder.setSpeed(aggregatedStatistic); @@ -84,7 +84,7 @@ public AggregatedStatistics swapData(AggregatedStatistics aggregatedStatistics) public List getCategories() { List categories = new ArrayList<>(); for (int i = 0; i < aggregatedStatistics.getCount(); i++) { - categories.add(aggregatedStatistics.getItem(i).getActivityType()); + categories.add(aggregatedStatistics.getItem(i).getActivityTypeLocalized()); } return categories; } @@ -163,7 +163,7 @@ public void setPace(AggregatedStatistics.AggregatedStatistic aggregatedStatistic //TODO Check preference handling. private void setCommonValues(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { - String activityType = aggregatedStatistic.getActivityType(); + String activityType = aggregatedStatistic.getActivityTypeLocalized(); reportSpeed = PreferencesUtils.isReportSpeed(activityType); unitSystem = PreferencesUtils.getUnitSystem(); @@ -182,7 +182,7 @@ private void setCommonValues(AggregatedStatistics.AggregatedStatistic aggregated } private int getIcon(AggregatedStatistics.AggregatedStatistic aggregatedStatistic) { - String localizedActivityType = aggregatedStatistic.getActivityType(); + String localizedActivityType = aggregatedStatistic.getActivityTypeLocalized(); String iconValue = ActivityType.findByLocalizedString(context, localizedActivityType) .getIconId(); return ActivityType.findBy(iconValue) diff --git a/src/main/java/de/dennisguse/opentracks/ui/intervals/IntervalsFragment.java b/src/main/java/de/dennisguse/opentracks/ui/intervals/IntervalsFragment.java index 40d1029988..96f6aea583 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/intervals/IntervalsFragment.java +++ b/src/main/java/de/dennisguse/opentracks/ui/intervals/IntervalsFragment.java @@ -151,7 +151,7 @@ public void onResume() { ContentProviderUtils contentProviderUtils = new ContentProviderUtils(getContext()); Track track = contentProviderUtils.getTrack(trackId); if (track != null) { - isReportSpeed = PreferencesUtils.isReportSpeed(track.getActivityTypeLocalized()); + isReportSpeed = PreferencesUtils.isReportSpeed(track); } viewModel = new ViewModelProvider(getActivity()).get(IntervalStatisticsModel.class);