From 062457f1aad411b0d3bc3beb7e10ebcf92e78273 Mon Sep 17 00:00:00 2001 From: Dennis Guse Date: Mon, 6 Nov 2023 07:33:20 +0100 Subject: [PATCH] Cleanup: use Java 17's instanceof. Fixes #1757. --- .../java/de/dennisguse/opentracks/TrackListActivity.java | 4 ++-- .../opentracks/settings/ResetDialogPreference.java | 4 ++-- .../opentracks/settings/SensorsSettingsFragment.java | 4 ++-- .../opentracks/ui/markers/MarkerDetailActivity.java | 8 ++++---- .../java/de/dennisguse/opentracks/ui/util/ViewUtils.java | 8 ++++---- .../java/de/dennisguse/opentracks/util/StringUtils.java | 4 ++-- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main/java/de/dennisguse/opentracks/TrackListActivity.java b/src/main/java/de/dennisguse/opentracks/TrackListActivity.java index 33da17e80a..ad8e78b27e 100644 --- a/src/main/java/de/dennisguse/opentracks/TrackListActivity.java +++ b/src/main/java/de/dennisguse/opentracks/TrackListActivity.java @@ -379,8 +379,8 @@ private void updateGpsMenuItem(boolean isGpsStarted, boolean isRecording) { startGpsMenuItem.setVisibility(!isRecording ? View.VISIBLE : View.INVISIBLE); if (!isRecording) { startGpsMenuItem.setIcon(AppCompatResources.getDrawable(this, isGpsStarted ? gpsStatusValue.icon : R.drawable.ic_gps_off_24dp)); - if (startGpsMenuItem.getIcon() instanceof AnimatedVectorDrawable) { - ((AnimatedVectorDrawable) startGpsMenuItem.getIcon()).start(); + if (startGpsMenuItem.getIcon() instanceof AnimatedVectorDrawable animatedVectorDrawable) { + animatedVectorDrawable.start(); } } } diff --git a/src/main/java/de/dennisguse/opentracks/settings/ResetDialogPreference.java b/src/main/java/de/dennisguse/opentracks/settings/ResetDialogPreference.java index 8512946f98..011fd6664f 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/ResetDialogPreference.java +++ b/src/main/java/de/dennisguse/opentracks/settings/ResetDialogPreference.java @@ -49,8 +49,8 @@ public void onDialogClosed(boolean positiveResult) { Toast.makeText(activity, R.string.settings_layout_reset_done, Toast.LENGTH_SHORT).show(); } - if (activity instanceof ResetCallback) { - ((ResetCallback) activity).onReset(); + if (activity instanceof ResetCallback resetCallback) { + resetCallback.onReset(); } } } diff --git a/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java b/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java index 3f39f63774..22629c6e22 100644 --- a/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java +++ b/src/main/java/de/dennisguse/opentracks/settings/SensorsSettingsFragment.java @@ -52,9 +52,9 @@ public void onDisplayPreferenceDialog(Preference preference) { private void setWheelCircumferenceInputFilter() { EditTextPreference wheelPreference = findPreference(getString(R.string.settings_sensor_bluetooth_cycling_speed_wheel_circumference_key)); wheelPreference.setOnPreferenceChangeListener((preference, newValue) -> { - if (newValue instanceof String) { + if (newValue instanceof String newValueString) { try { - int newValueInt = Integer.parseInt((String) newValue); + int newValueInt = Integer.parseInt(newValueString); return newValueInt >= 100 && newValueInt < 4000; } catch (NumberFormatException e) { Log.w(TAG, "Entered string is no number."); diff --git a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerDetailActivity.java b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerDetailActivity.java index 431eadf277..1962541192 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerDetailActivity.java +++ b/src/main/java/de/dennisguse/opentracks/ui/markers/MarkerDetailActivity.java @@ -61,12 +61,12 @@ protected void onCreate(Bundle bundle) { Object bundleMarkerId = getIntent().getExtras().get(EXTRA_MARKER_ID); Marker.Id markerId = null; - if (bundleMarkerId instanceof Marker.Id) { - markerId = (Marker.Id) bundleMarkerId; + if (bundleMarkerId instanceof Marker.Id cast) { + markerId = cast; } - if (bundleMarkerId instanceof Long) { + if (bundleMarkerId instanceof Long cast) { //Incoming Intent via Dashboard API. - markerId = new Marker.Id((Long) bundleMarkerId); + markerId = new Marker.Id(cast); } if (markerId == null) { Log.d(TAG, "invalid marker id"); diff --git a/src/main/java/de/dennisguse/opentracks/ui/util/ViewUtils.java b/src/main/java/de/dennisguse/opentracks/ui/util/ViewUtils.java index 70580716f9..dc947cbc9c 100644 --- a/src/main/java/de/dennisguse/opentracks/ui/util/ViewUtils.java +++ b/src/main/java/de/dennisguse/opentracks/ui/util/ViewUtils.java @@ -20,10 +20,10 @@ public static void makeClickableLinks(ViewGroup view) { for (int i = 0; i < view.getChildCount(); i++) { final View child = view.getChildAt(i); - if (child instanceof ViewGroup) { - makeClickableLinks((ViewGroup) child); - } else if (child instanceof TextView) { - ((TextView) child).setMovementMethod(LinkMovementMethod.getInstance()); + if (child instanceof ViewGroup childVg) { + makeClickableLinks(childVg); + } else if (child instanceof TextView childTv) { + childTv.setMovementMethod(LinkMovementMethod.getInstance()); } } } diff --git a/src/main/java/de/dennisguse/opentracks/util/StringUtils.java b/src/main/java/de/dennisguse/opentracks/util/StringUtils.java index e9d9744067..294650ecc1 100644 --- a/src/main/java/de/dennisguse/opentracks/util/StringUtils.java +++ b/src/main/java/de/dennisguse/opentracks/util/StringUtils.java @@ -240,9 +240,9 @@ public static String formatCData(String text) { public static OffsetDateTime parseTime(String xmlDateTime) { try { TemporalAccessor t = DateTimeFormatter.ISO_DATE_TIME.parseBest(xmlDateTime, ZonedDateTime::from, LocalDateTime::from); - if (t instanceof LocalDateTime) { + if (t instanceof LocalDateTime localDateTime) { Log.w(TAG, "Date does not contain timezone information: using UTC."); - t = ((LocalDateTime) t).atZone(ZoneOffset.UTC); + t = localDateTime.atZone(ZoneOffset.UTC); } return OffsetDateTime.from(t); } catch (Exception e) {