[android] Simplify some code using java.time using the code desugaring

Signed-off-by: Isira Seneviratne <isirasen96@gmail.com>
This commit is contained in:
Isira Seneviratne 2024-03-06 15:09:57 +05:30 committed by GitHub
parent a06d67c309
commit ede7eda0c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 34 additions and 50 deletions

View file

@ -334,6 +334,8 @@ android {
}
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}
@ -352,6 +354,8 @@ android {
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.4'
// Google Play Location Services
//
// Please add symlinks to google/java/app/organicmaps/location for each new gms-enabled flavor below:

View file

@ -1928,9 +1928,9 @@ JNIEXPORT jstring JNICALL
Java_app_organicmaps_Framework_nativeGetKayakHotelLink(JNIEnv * env, jclass, jstring countryIsoCode, jstring uri,
jobject firstDay, jobject lastDay, jboolean isReferral)
{
static jmethodID dateGetTime = jni::GetMethodID(env, firstDay, "getTime", "()J");
jlong firstDaySec = env->CallLongMethod(firstDay, dateGetTime) / 1000L;
jlong lastDaySec = env->CallLongMethod(lastDay, dateGetTime) / 1000L;
static jmethodID instantGetEpochSecond = jni::GetMethodID(env, firstDay, "getEpochSecond", "()J");
jlong firstDaySec = env->CallLongMethod(firstDay, instantGetEpochSecond);
jlong lastDaySec = env->CallLongMethod(lastDay, instantGetEpochSecond);
string const url = osm::GetKayakHotelURLFromURI(jni::ToNativeString(env, countryIsoCode),
jni::ToNativeString(env, uri),

View file

@ -28,6 +28,7 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.Locale;
@ -453,6 +454,6 @@ public class Framework
*/
@Nullable
public static native String nativeGetKayakHotelLink(@NonNull String countryIsoCode, @NonNull String uri,
@NonNull Date firstDay, @NonNull Date lastDay,
@NonNull Instant firstDay, @NonNull Instant lastDay,
boolean isReferral);
}

View file

@ -18,9 +18,10 @@ import app.organicmaps.widget.placepage.PlacePageData;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@ -287,8 +288,8 @@ public class MapObject implements PlacePageData
final String uri = getMetadata(Metadata.MetadataType.FMD_EXTERNAL_URI);
if (TextUtils.isEmpty(uri))
return "";
final Date firstDay = new Date();
final Date lastDay = new Date(firstDay.getTime() + (1000 * 60 * 60 * 24));
final Instant firstDay = Instant.now();
final Instant lastDay = firstDay.plus(1, ChronoUnit.DAYS);
final boolean isReferral = Config.isKayakReferralAllowed();
final String res = Framework.nativeGetKayakHotelLink(Utils.getCountryCode(), uri, firstDay, lastDay, isReferral);
return res == null ? "" : res;

View file

@ -7,7 +7,6 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.car.app.CarContext;
import androidx.car.app.model.CarIcon;
import androidx.car.app.model.DateTimeWithZone;
import androidx.car.app.navigation.model.Destination;
import androidx.car.app.navigation.model.Lane;
import androidx.car.app.navigation.model.Step;
@ -20,7 +19,7 @@ import app.organicmaps.routing.RoutingInfo;
import app.organicmaps.routing.SingleLaneInfo;
import app.organicmaps.util.Graphics;
import java.util.Calendar;
import java.time.ZonedDateTime;
import java.util.Objects;
public final class RoutingUtils
@ -93,20 +92,13 @@ public final class RoutingUtils
return builder.build();
}
@SuppressWarnings("NewApi") // ZonedDateTime is backported for Android versions below 8.0.
@NonNull
private static TravelEstimate createTravelEstimate(@NonNull app.organicmaps.util.Distance distance, int time)
{
final TravelEstimate.Builder builder = new TravelEstimate.Builder(RoutingHelpers.createDistance(distance), createTimeEstimate(time));
builder.setRemainingTimeSeconds(time);
builder.setRemainingDistanceColor(Colors.DISTANCE);
return builder.build();
}
@NonNull
private static DateTimeWithZone createTimeEstimate(int seconds)
{
final Calendar currentTime = Calendar.getInstance();
currentTime.add(Calendar.SECOND, seconds);
return DateTimeWithZone.create(currentTime.getTimeInMillis(), currentTime.getTimeZone());
return new TravelEstimate.Builder(RoutingHelpers.createDistance(distance), ZonedDateTime.now().plusSeconds(time))
.setRemainingTimeSeconds(time)
.setRemainingDistanceColor(Colors.DISTANCE)
.build();
}
}

View file

@ -5,13 +5,12 @@ import android.os.Parcelable;
import androidx.annotation.IntRange;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import app.organicmaps.util.StringUtils;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
// Called from JNI.
@Keep
@ -37,19 +36,15 @@ public class HoursMinutes implements Parcelable
m24HourFormat = in.readByte() != 0;
}
@NonNull
@Override
public String toString()
{
if (m24HourFormat)
return StringUtils.formatUsingUsLocale("%02d:%02d", hours, minutes);
Calendar calendar = new GregorianCalendar();
calendar.set(Calendar.HOUR_OF_DAY, (int)hours);
calendar.set(Calendar.MINUTE, (int)minutes);
SimpleDateFormat fmt12 = new SimpleDateFormat("hh:mm a", Locale.getDefault());
return fmt12.format(calendar.getTime());
final LocalTime localTime = LocalTime.of((int) hours, (int) minutes);
return localTime.format(DateTimeFormatter.ofPattern("hh:mm a"));
}
@Override

View file

@ -22,7 +22,7 @@ import app.organicmaps.util.Utils;
import app.organicmaps.util.concurrency.UiThread;
import app.organicmaps.util.log.Logger;
import java.util.Calendar;
import java.time.LocalTime;
import java.util.concurrent.TimeUnit;
@ -918,9 +918,7 @@ public class RoutingController
static String formatArrivalTime(int seconds)
{
Calendar current = Calendar.getInstance();
current.set(Calendar.SECOND, 0);
current.add(Calendar.SECOND, seconds);
return StringUtils.formatUsingUsLocale("%d:%02d", current.get(Calendar.HOUR_OF_DAY), current.get(Calendar.MINUTE));
final LocalTime time = LocalTime.now().withSecond(seconds);
return StringUtils.formatUsingUsLocale("%d:%02d", time.getHour(), time.getMinute());
}
}

View file

@ -19,10 +19,8 @@ import app.organicmaps.util.StringUtils;
import app.organicmaps.util.UiUtils;
import com.google.android.material.progressindicator.LinearProgressIndicator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
public class NavMenu
@ -199,17 +197,12 @@ public class NavMenu
private void updateTimeEstimate(int seconds)
{
final Calendar currentTime = Calendar.getInstance();
currentTime.add(Calendar.SECOND, seconds);
DateFormat timeFormat;
if (android.text.format.DateFormat.is24HourFormat(mTimeMinuteValue.getContext()))
timeFormat = new SimpleDateFormat("HH:mm", Locale.getDefault());
else
timeFormat = new SimpleDateFormat("h:mm aa", Locale.getDefault());
mTimeEstimate.setText(timeFormat.format(currentTime.getTime()));
final String format = android.text.format.DateFormat.is24HourFormat(mTimeMinuteValue.getContext())
? "HH:mm" : "h:mm aa";
final LocalTime localTime = LocalTime.now().plusSeconds(seconds);
mTimeEstimate.setText(localTime.format(DateTimeFormatter.ofPattern(format)));
}
private void updateSpeedView(@NonNull RoutingInfo info)
{
final Location last = LocationHelper.from(mActivity).getSavedLocation();