Skip to content
Merged
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
18 changes: 7 additions & 11 deletions src/main/java/org/mtransit/android/commons/KeyboardUtils.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.mtransit.android.commons

import android.app.Activity
import android.content.Context
import android.view.View
import android.view.inputmethod.InputMethodManager
import androidx.core.content.getSystemService

@Suppress("unused")
class KeyboardUtils : MTLog.Loggable {
Expand All @@ -15,20 +15,16 @@ class KeyboardUtils : MTLog.Loggable {

@JvmStatic
fun showKeyboard(activity: Activity?, view: View?) {
if (activity == null || view == null) {
return
}
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
view ?: return
val imm = activity?.getSystemService<InputMethodManager>() ?: return
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}

@JvmStatic
fun hideKeyboard(activity: Activity?, view: View?) {
if (activity == null || view == null) {
return
}
val imm = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.hideSoftInputFromWindow(view.windowToken, 0)
view ?: return
val imm = activity?.getSystemService<InputMethodManager>() ?: return
imm.hideSoftInputFromWindow(view.windowToken, 0)
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/org/mtransit/android/commons/MTLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static String formatDuration(@Nullable Long durationInMs) {

@NonNull
public static String formatDuration(long durationInMs) {
return "[" + durationInMs + " - " + TimeUtils.formatSimpleDuration(durationInMs) + "]";
return "[" + durationInMs + "|" + TimeUtils.formatSimpleDuration(durationInMs) + "]";
}

@Nullable
Expand All @@ -56,7 +56,7 @@ public static String formatDateTime(@Nullable Long timeInMs) {

@NonNull
public static String formatDateTime(long timeInMs) {
return "[" + timeInMs + " - " + TimeUtils.formatShortDateTime(new Date(timeInMs)) + "]";
return "[" + timeInMs + "|" + TimeUtils.formatShortDateTime(new Date(timeInMs)) + "]";
}

@Nullable
Expand All @@ -66,7 +66,7 @@ public static String formatDateTimeN(@Nullable Date date) {

@NonNull
public static String formatDateTime(@NonNull Date date) {
return "[" + date.getTime() + " - " + TimeUtils.formatShortDateTime(date) + "]";
return "[" + date.getTime() + "|" + TimeUtils.formatShortDateTime(date) + "]";
}

// VERBOSE
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/mtransit/android/commons/TimeUtilsK.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ object TimeUtilsK {
fun currentInstant() = TimeUtils.currentTimeMillis().millisToInstant()
}

fun Duration.formatSimpleDuration() = this.inWholeMilliseconds.let { TimeUtilsK.formatSimpleDuration(it) }

fun Long.millisToInstant() = Instant.fromEpochMilliseconds(this)

fun Long.secsToInstant() = Instant.fromEpochSeconds(this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public AppStatus(@NonNull POIStatus status, boolean appInstalled, boolean appEna
status.getId(),
status.getTargetUUID(),
status.getLastUpdateInMs(),
status.getMaxValidityInMs(),
status.getValidityInMs(),
status.getReadFromSourceAtInMs(),
appInstalled,
appEnabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public AvailabilityPercent(@NonNull POIStatus status) {
status.getId(),
status.getTargetUUID(),
status.getLastUpdateInMs(),
status.getMaxValidityInMs(),
status.getValidityInMs(),
status.getReadFromSourceAtInMs(),
status.getSourceLabel(),
status.isNoData()
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/org/mtransit/android/commons/data/POIStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected static int getDefaultStatusTextColor(@NonNull Context context) {
@ItemStatusType
private final int type;
private long lastUpdateInMs;
private long maxValidityInMs;
private long validityInMs;
private long readFromSourceAtInMs;
@Nullable
private String sourceLabel;
Expand All @@ -58,7 +58,7 @@ public POIStatus(
@NonNull String targetUUID,
@ItemStatusType int type,
long lastUpdateInMs,
long maxValidityInMs,
long validityInMs,
long readFromSourceAtInMs,
@Nullable String sourceLabel,
boolean noData
Expand All @@ -67,7 +67,7 @@ public POIStatus(
this.targetUUID = targetUUID;
this.type = type;
this.lastUpdateInMs = lastUpdateInMs;
this.maxValidityInMs = maxValidityInMs;
this.validityInMs = validityInMs;
this.readFromSourceAtInMs = readFromSourceAtInMs;
this.sourceLabel = sourceLabel;
this.noData = noData;
Expand All @@ -80,9 +80,9 @@ public String toString() {
"id=" + id +
", targetUUID='" + targetUUID + '\'' +
", type=" + type +
", lastUpdateInMs=" + lastUpdateInMs +
", maxValidityInMs=" + maxValidityInMs +
", readFromSourceAtInMs=" + readFromSourceAtInMs +
", lastUpdate=" + MTLog.formatDateTime(lastUpdateInMs) +
", validity=" + MTLog.formatDuration(validityInMs) +
", readFromSourceAt=" + MTLog.formatDateTime(readFromSourceAtInMs) +
", sourceLabel='" + sourceLabel + '\'' +
", noData=" + noData +
'}';
Expand All @@ -98,9 +98,9 @@ public static POIStatus fromCursor(@NonNull Cursor cursor) {
String targetUUID = cursor.getString(cursor.getColumnIndexOrThrow(StatusProviderContract.Columns.T_STATUS_K_TARGET_UUID));
int type = cursor.getInt(cursor.getColumnIndexOrThrow(StatusProviderContract.Columns.T_STATUS_K_TYPE));
long lastUpdateInMs = cursor.getLong(cursor.getColumnIndexOrThrow(StatusProviderContract.Columns.T_STATUS_K_LAST_UPDATE));
long maxValidityInMs = cursor.getLong(cursor.getColumnIndexOrThrow(StatusProviderContract.Columns.T_STATUS_K_MAX_VALIDITY_IN_MS));
long validityInMs = cursor.getLong(cursor.getColumnIndexOrThrow(StatusProviderContract.Columns.T_STATUS_K_VALIDITY));
long readFromSourceAtInMs; // optional
int readFromSourceAtColumnIndex = cursor.getColumnIndex(StatusProviderContract.Columns.T_STATUS_K_READ_FROM_SOURCE_AT_IN_MS);
int readFromSourceAtColumnIndex = cursor.getColumnIndex(StatusProviderContract.Columns.T_STATUS_K_READ_FROM_SOURCE_AT);
if (readFromSourceAtColumnIndex < 0) {
readFromSourceAtInMs = -1L;
} else {
Expand All @@ -118,7 +118,7 @@ public static POIStatus fromCursor(@NonNull Cursor cursor) {
} catch (Exception e) {
MTLog.w(LOG_TAG, e, "Error while retrieving extras information from cursor.");
}
return new POIStatus(id, targetUUID, type, lastUpdateInMs, maxValidityInMs, readFromSourceAtInMs, sourceLabel, noData);
return new POIStatus(id, targetUUID, type, lastUpdateInMs, validityInMs, readFromSourceAtInMs, sourceLabel, noData);
}

@NonNull
Expand All @@ -129,7 +129,7 @@ public Cursor toCursor() {
this.type,
this.targetUUID,
this.lastUpdateInMs,
this.maxValidityInMs,
this.validityInMs,
this.readFromSourceAtInMs,
getExtrasJSONString()
});
Expand Down Expand Up @@ -162,14 +162,14 @@ public ContentValues toContentValues() {
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_TYPE, this.type);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_TARGET_UUID, this.targetUUID);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_LAST_UPDATE, this.lastUpdateInMs);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_MAX_VALIDITY_IN_MS, this.maxValidityInMs);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_READ_FROM_SOURCE_AT_IN_MS, this.readFromSourceAtInMs);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_VALIDITY, this.validityInMs);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_READ_FROM_SOURCE_AT, this.readFromSourceAtInMs);
contentValues.put(StatusProviderContract.Columns.T_STATUS_K_EXTRAS, getExtrasJSONString());
return contentValues;
}

public boolean isUseful() {
return this.lastUpdateInMs + this.maxValidityInMs >= TimeUtils.currentTimeMillis();
return this.lastUpdateInMs + this.validityInMs >= TimeUtils.currentTimeMillis();
}

public boolean isNoData() {
Expand Down Expand Up @@ -233,12 +233,12 @@ public void setLastUpdateInMs(long lastUpdateInMs) {
this.lastUpdateInMs = lastUpdateInMs;
}

public long getMaxValidityInMs() {
return maxValidityInMs;
public long getValidityInMs() {
return validityInMs;
}

public void setMaxValidityInMs(long maxValidityInMs) {
this.maxValidityInMs = maxValidityInMs;
public void setValidityInMs(long validityInMs) {
this.validityInMs = validityInMs;
}

public long getReadFromSourceAtInMs() {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/mtransit/android/commons/data/Schedule.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String getLogTag() {
return LOG_TAG;
}

static final TimestampComparator TIMESTAMPS_COMPARATOR = new TimestampComparator();
public static final TimestampComparator TIMESTAMPS_COMPARATOR = new TimestampComparator();

private static final FrequencyComparator FREQUENCIES_COMPARATOR = new FrequencyComparator();

Expand All @@ -59,7 +59,7 @@ public Schedule(@NonNull POIStatus status, long providerPrecisionInMs, boolean n
status.getId(),
status.getTargetUUID(),
status.getLastUpdateInMs(),
status.getMaxValidityInMs(),
status.getValidityInMs(),
status.getReadFromSourceAtInMs(),
providerPrecisionInMs,
noPickup,
Expand Down Expand Up @@ -285,7 +285,7 @@ private void resetUsefulUntilInMs() {
this.usefulUntilInMs = this.timestamps.get(timestampsCount - 1).getDepartureT() + getUIProviderPrecisionInMs();
}

private long getUsefulUntilInMs() {
public long getUsefulUntilInMs() {
if (this.usefulUntilInMs < 0L) {
resetUsefulUntilInMs();
}
Expand All @@ -298,7 +298,7 @@ public boolean isUseful() {
&& getUsefulUntilInMs() > TimeUtils.currentTimeToTheMinuteMillis();
}

private static class TimestampComparator implements Comparator<Timestamp> {
public static class TimestampComparator implements Comparator<Timestamp> {
@Override
public int compare(Timestamp lhs, Timestamp rhs) {
return Long.compare(
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/org/mtransit/android/commons/data/ScheduleExt.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.mtransit.android.commons.data

import org.mtransit.android.commons.Constants
import org.mtransit.android.commons.MTLog.formatDateTime
import org.mtransit.android.commons.MTLog
import org.mtransit.android.commons.floorBy
import org.mtransit.android.commons.millisToInstant
import org.mtransit.android.commons.roundToNearest
Expand All @@ -15,7 +15,7 @@ fun Schedule.toNoData() = Schedule(
id,
targetUUID,
lastUpdateInMs,
maxValidityInMs,
validityInMs,
readFromSourceAtInMs,
providerPrecisionInMs,
isNoPickup,
Expand Down Expand Up @@ -94,7 +94,13 @@ var Schedule.Timestamp.originalArrivalDelay: Duration

val Schedule.Timestamp.originalArrival get() = arrival - originalArrivalDelay

private fun computeInstant(initialInstant: Instant, delay: Duration, precision: Duration, canRoundToNearest: Boolean = false, canRoundUp: Boolean = false): Instant {
private fun computeInstant(
initialInstant: Instant,
delay: Duration,
precision: Duration,
canRoundToNearest: Boolean = false,
canRoundUp: Boolean = false
): Instant {
val newInstant = initialInstant + delay
val roundedNewInstant = if (canRoundToNearest && delay.absoluteValue > precision.div(2)) {
newInstant.roundToNearest(precision)
Expand Down Expand Up @@ -126,15 +132,15 @@ val Schedule.hasRealTime get() = this.timestamps.any { it.isRealTime }
fun Schedule.Timestamp.toStringShort() = buildString {
append("T{")
arrivalTIfDifferent?.let {
append("a=").append(if (Constants.DEBUG) formatDateTime(arrivalT) else arrivalT)
append("a=").append(if (Constants.DEBUG) MTLog.formatDateTime(arrivalT) else arrivalT)
if (originalArrivalDelayMs != 0L) {
append("[+/-:").append(originalArrivalDelayMs).append("]")
append("[+/-:").append(if (Constants.DEBUG) MTLog.formatDuration(originalArrivalDelayMs) else originalArrivalDelayMs).append("]")
}
append(",")
}
append("d=").append(if (Constants.DEBUG) formatDateTime(departureT) else departureT)
append("d=").append(if (Constants.DEBUG) MTLog.formatDateTime(departureT) else departureT)
if (originalDepartureDelayMs != 0L) {
append("[+/-:").append(originalDepartureDelayMs).append("]")
append("[+/-:").append(if (Constants.DEBUG) MTLog.formatDuration(originalDepartureDelayMs) else originalDepartureDelayMs).append("]")
}
if (isRealTime) {
append("[RT]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static Cursor queryS(@NonNull ScheduleTimestampsProviderContract provider
private static Cursor getScheduleTimestamps(@NonNull ScheduleTimestampsProviderContract provider, @Nullable String selection) {
final ScheduleTimestampsProviderContract.Filter scheduleTimestampsFilter = ScheduleTimestampsProviderContract.Filter.fromJSONString(selection);
if (scheduleTimestampsFilter == null) {
MTLog.w(LOG_TAG, "Error while parsing schedule timestamps filter '%s'!", selection);
MTLog.w(provider, "getScheduleTimestamps() > Error while parsing schedule timestamps filter '%s'!", selection);
return getScheduleTimestampCursor(null);
}
final ScheduleTimestamps scheduleTimestamps = provider.getScheduleTimestamps(scheduleTimestampsFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public static String getTypeS(@NonNull ServiceUpdateProviderContract provider, @
private static Cursor getServiceUpdates(ServiceUpdateProviderContract provider, String selection) {
final ServiceUpdateProviderContract.Filter serviceUpdateFilter = ServiceUpdateProviderContract.Filter.fromJSONString(selection);
if (serviceUpdateFilter == null) {
MTLog.w(LOG_TAG, "Error while parsing service update filter! (%s)", selection);
MTLog.w(provider, "getServiceUpdates() > Error while parsing service update filter! (%s)", selection);
return getServiceUpdateCursor(null);
}
final long nowInMs = TimeUtils.currentTimeMillis();
Expand Down Expand Up @@ -116,7 +116,7 @@ private static Cursor getServiceUpdates(ServiceUpdateProviderContract provider,
}
if (serviceUpdateFilter.isCacheOnlyOrDefault()) {
if (CollectionUtils.getSize(cachedServiceUpdates) == 0) {
MTLog.w(LOG_TAG, "getServiceUpdates() > No useful cache found!");
MTLog.w(provider, "getServiceUpdates() > No useful cache found!");
}
return getServiceUpdateCursor(cachedServiceUpdates);
}
Expand Down Expand Up @@ -144,7 +144,7 @@ private static Cursor getServiceUpdates(ServiceUpdateProviderContract provider,
}
}
if (CollectionUtils.getSize(cachedServiceUpdates) == 0) {
MTLog.w(LOG_TAG, "getServiceUpdates() > no cache & no data from provider for %s!", serviceUpdateFilter.getTargetUUID());
MTLog.d(provider, "getServiceUpdates() > no cache & no data from provider for %s.", serviceUpdateFilter.getTargetUUID());
}
return getServiceUpdateCursor(cachedServiceUpdates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,13 @@ object GTFSRealTimeTripUpdatesProvider : MTLog.Loggable {

val TRIP_UPDATE_MAX_VALIDITY_IN_MS = 1.hours.inWholeMilliseconds

val TRIP_UPDATE_VALIDITY_IN_MS = 10.minutes.inWholeMilliseconds
val TRIP_UPDATE_VALIDITY_IN_FOCUS_IN_MS = 10.seconds.inWholeMilliseconds
val TRIP_UPDATE_VALIDITY_IN_MS = 1.minutes.inWholeMilliseconds
val TRIP_UPDATE_VALIDITY_IN_FOCUS_IN_MS = 30.seconds.inWholeMilliseconds

@Suppress("unused")
val TRIP_UPDATE_MIN_DURATION_BETWEEN_REFRESH_IN_MS = 3.minutes.inWholeMilliseconds
val TRIP_UPDATE_MIN_DURATION_BETWEEN_REFRESH_IN_MS = 1.minutes.inWholeMilliseconds

@Suppress("unused")
val TRIP_UPDATE_MIN_DURATION_BETWEEN_REFRESH_IN_FOCUS_IN_MS = 1.minutes.inWholeMilliseconds
val TRIP_UPDATE_MIN_DURATION_BETWEEN_REFRESH_IN_FOCUS_IN_MS = 10.seconds.inWholeMilliseconds

@Suppress("unused")
@JvmStatic
fun GTFSRealTimeProvider.getMinDurationBetweenRefreshInMs(inFocus: Boolean) =
if (inFocus) TRIP_UPDATE_MIN_DURATION_BETWEEN_REFRESH_IN_FOCUS_IN_MS.adaptForCachedAPI(this.context)
Expand All @@ -75,7 +72,7 @@ object GTFSRealTimeTripUpdatesProvider : MTLog.Loggable {

private fun Long.adaptForCachedAPI(context: Context?) =
if (context?.let { GTFSRealTimeProvider.getAGENCY_TRIP_UPDATES_URL_CACHED(it) }?.isNotBlank() == true) {
this * 2L // fewer calls to Cached API $$
this.coerceAtLeast(1.minutes.inWholeMilliseconds) // fewer calls to Cached API $$
} else this

@JvmStatic
Expand All @@ -94,13 +91,13 @@ object GTFSRealTimeTripUpdatesProvider : MTLog.Loggable {
?: makeCachedStatusFromAgencyDataLock(filter, tripIds)
}

private val tripUpdateLock = Any()
private val tripUpdateLock = mutableMapOf<String, Any>()

private fun GTFSRealTimeProvider.makeCachedStatusFromAgencyDataLock(
filter: Schedule.ScheduleStatusFilter,
tripIds: List<String>
): POIStatus? {
synchronized(tripUpdateLock) {
synchronized(tripUpdateLock.getOrPut(filter.routeDirectionStop.routeDirectionUUID) { Any() }) {
return getCachedStatusS(filter.targetUUID, tripIds) // try another time
?: makeCachedStatusFromAgencyData(filter, tripIds)
}
Expand Down Expand Up @@ -198,7 +195,7 @@ object GTFSRealTimeTripUpdatesProvider : MTLog.Loggable {
schedule.lastUpdateInMs = readFromSourceMs
schedule.readFromSourceAtInMs = readFromSourceMs
schedule.providerPrecisionInMs = PROVIDER_PRECISION_IN_MS
schedule.maxValidityInMs = maxValidityInMs
schedule.validityInMs = TRIP_UPDATE_VALIDITY_IN_MS
cacheStatus(schedule)
}
return getCachedStatusS(filter.targetUUID, tripIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ internal fun processRDTripUpdate(
return
}
if (gTripUpdate.optDelay == null && gTripUpdate.stopTimeUpdateCount == 0) {
MTLog.d(LOG_TAG, "processRDTripUpdate() > SKIP (useless trip update: ${gTripUpdate.toStringExt()})")
MTLog.d(LOG_TAG, "processRDTripUpdate($tripId) > SKIP (useless trip update: ${gTripUpdate.toStringExt()})")
return // nothing to do
}
var stuIdx = 0
Expand Down
Loading