diff --git a/src/main/java/org/mtransit/android/commons/data/AppStatus.java b/src/main/java/org/mtransit/android/commons/data/AppStatus.java index 23cd748a..70cebde9 100644 --- a/src/main/java/org/mtransit/android/commons/data/AppStatus.java +++ b/src/main/java/org/mtransit/android/commons/data/AppStatus.java @@ -236,9 +236,9 @@ public static StatusProviderContract.Filter fromJSONString(@Nullable String json @Nullable public static StatusProviderContract.Filter fromJSON(@NonNull JSONObject json) { try { - String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json); - String pkg = json.getString(JSON_PKG); - AppStatusFilter appStatusFilter = new AppStatusFilter(targetUUID, pkg); + final String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json); + final String pkg = json.getString(JSON_PKG); + final AppStatusFilter appStatusFilter = new AppStatusFilter(targetUUID, pkg); StatusProviderContract.Filter.fromJSON(appStatusFilter, json); return appStatusFilter; } catch (JSONException jsone) { @@ -255,14 +255,14 @@ public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFi @Nullable private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) { - JSONObject json = toJSON(statusFilter); + final JSONObject json = toJSON(statusFilter); return json == null ? null : json.toString(); } @Nullable private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) { try { - JSONObject json = new JSONObject(); + final JSONObject json = new JSONObject(); StatusProviderContract.Filter.toJSON(statusFilter, json); if (statusFilter instanceof AppStatusFilter) { AppStatusFilter appStatusFilter = (AppStatusFilter) statusFilter; diff --git a/src/main/java/org/mtransit/android/commons/data/AvailabilityPercent.java b/src/main/java/org/mtransit/android/commons/data/AvailabilityPercent.java index 19c8c9d9..2c990e40 100644 --- a/src/main/java/org/mtransit/android/commons/data/AvailabilityPercent.java +++ b/src/main/java/org/mtransit/android/commons/data/AvailabilityPercent.java @@ -642,8 +642,8 @@ public static StatusProviderContract.Filter fromJSONString(@Nullable String json @Nullable public static StatusProviderContract.Filter fromJSON(@NonNull JSONObject json) { try { - String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json); - AvailabilityPercentStatusFilter availabilityPercentStatusFilter = new AvailabilityPercentStatusFilter(targetUUID); + final String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json); + final AvailabilityPercentStatusFilter availabilityPercentStatusFilter = new AvailabilityPercentStatusFilter(targetUUID); StatusProviderContract.Filter.fromJSON(availabilityPercentStatusFilter, json); return availabilityPercentStatusFilter; } catch (JSONException jsone) { @@ -660,14 +660,14 @@ public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFi @Nullable private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) { - JSONObject json = toJSON(statusFilter); + final JSONObject json = toJSON(statusFilter); return json == null ? null : json.toString(); } @Nullable private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) { try { - JSONObject json = new JSONObject(); + final JSONObject json = new JSONObject(); StatusProviderContract.Filter.toJSON(statusFilter, json); return json; } catch (JSONException jsone) { diff --git a/src/main/java/org/mtransit/android/commons/data/ServiceUpdates.kt b/src/main/java/org/mtransit/android/commons/data/ServiceUpdates.kt new file mode 100644 index 00000000..c81e4ad9 --- /dev/null +++ b/src/main/java/org/mtransit/android/commons/data/ServiceUpdates.kt @@ -0,0 +1,5 @@ +package org.mtransit.android.commons.data + +data class ServiceUpdates( + val serviceUpdates: List?, +) diff --git a/src/main/java/org/mtransit/android/commons/provider/agency/AgencyProviderDeployWorker.kt b/src/main/java/org/mtransit/android/commons/provider/agency/AgencyProviderDeployWorker.kt index 80ce8b78..1c99b6fc 100644 --- a/src/main/java/org/mtransit/android/commons/provider/agency/AgencyProviderDeployWorker.kt +++ b/src/main/java/org/mtransit/android/commons/provider/agency/AgencyProviderDeployWorker.kt @@ -41,7 +41,7 @@ class AgencyProviderDeployWorker( override fun getLogTag() = LOG_TAG - override suspend fun doWork() = withContext(Dispatchers.IO) { + override suspend fun doWork(): Result = withContext(Dispatchers.IO) { val agencyProviderMetaData = context.getString(R.string.agency_provider) val agencyProvider = PackageManagerUtils.findContentProvidersWithMetaData(context, context.packageName) @@ -49,7 +49,7 @@ class AgencyProviderDeployWorker( agencyProviderMetaData == provider.metaData?.getString(agencyProviderMetaData) } ?: return@withContext Result.failure(Data.Builder().putString("reason", "no agency provider!").build()) ping(agencyProvider) - Result.success() + return@withContext Result.success() } private fun ping(agencyProvider: ProviderInfo) { diff --git a/src/main/java/org/mtransit/android/commons/provider/bike/BikeStationProvider.java b/src/main/java/org/mtransit/android/commons/provider/bike/BikeStationProvider.java index 97a8f14e..660c2447 100644 --- a/src/main/java/org/mtransit/android/commons/provider/bike/BikeStationProvider.java +++ b/src/main/java/org/mtransit/android/commons/provider/bike/BikeStationProvider.java @@ -256,7 +256,7 @@ public Cursor getSearchSuggest(@Nullable String query) { public Cursor getPOI(@Nullable POIProviderContract.Filter poiFilter) { if (poiFilter != null && poiFilter.getExtraBoolean(POIProviderContract.POI_FILTER_EXTRA_AVOID_LOADING, false)) { if (getLastUpdateInMs() + getPOIMaxValidityInMs() > TimeUtils.currentTimeMillis()) { // not too old to display - Cursor cursor = getPOIFromDB(poiFilter); + final Cursor cursor = getPOIFromDB(poiFilter); if (cursor != null && cursor.getCount() > 0) { return cursor; // returned cached results instead of loading while user is waiting } diff --git a/src/main/java/org/mtransit/android/commons/provider/common/MTSearchRecentSuggestionsProvider.java b/src/main/java/org/mtransit/android/commons/provider/common/MTSearchRecentSuggestionsProvider.java index 249f16e0..74b3bbe6 100644 --- a/src/main/java/org/mtransit/android/commons/provider/common/MTSearchRecentSuggestionsProvider.java +++ b/src/main/java/org/mtransit/android/commons/provider/common/MTSearchRecentSuggestionsProvider.java @@ -28,7 +28,7 @@ protected void setupSuggestions(@NonNull String authority, int mode) { super.setupSuggestions(authority, mode); } - // INHERITED FROM CONTENTPROVIDER + // INHERITED FROM CONTENT PROVIDER @Override public boolean onCreate() { diff --git a/src/main/java/org/mtransit/android/commons/provider/common/ProviderContract.java b/src/main/java/org/mtransit/android/commons/provider/common/ProviderContract.java index fda619e7..7e0e47ea 100644 --- a/src/main/java/org/mtransit/android/commons/provider/common/ProviderContract.java +++ b/src/main/java/org/mtransit/android/commons/provider/common/ProviderContract.java @@ -5,8 +5,11 @@ import android.database.sqlite.SQLiteDatabase; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.annotation.WorkerThread; +import org.json.JSONException; +import org.json.JSONObject; import org.mtransit.android.commons.MTLog; import java.util.concurrent.TimeUnit; @@ -30,4 +33,99 @@ public interface ProviderContract extends MTLog.Loggable { @NonNull Context requireContextCompat(); + + abstract class Filter { + + private static final boolean CACHE_ONLY_DEFAULT = false; + private static final boolean IN_FOCUS_DEFAULT = false; + + @Nullable + private Boolean cacheOnly = null; + @Nullable + private Long cacheValidityInMs = null; + @Nullable + private Boolean inFocus = null; + + public boolean isCacheOnlyOrDefault() { + return this.cacheOnly == null ? CACHE_ONLY_DEFAULT : this.cacheOnly; + } + + @SuppressWarnings("unused") + @Nullable + public Boolean getCacheOnly() { + return this.cacheOnly; + } + + public void setCacheOnly(@Nullable Boolean cacheOnly) { + this.cacheOnly = cacheOnly; + } + + @Nullable + public Long getCacheValidityInMs() { + return this.cacheValidityInMs; + } + + @SuppressWarnings("unused") + public void setCacheValidityInMs(@Nullable Long cacheValidityInMs) { + this.cacheValidityInMs = cacheValidityInMs; + } + + public boolean isInFocusOrDefault() { + return this.inFocus == null ? IN_FOCUS_DEFAULT : this.inFocus; + } + + @SuppressWarnings("unused") + @Nullable + public Boolean getInFocus() { + return this.inFocus; + } + + public void setInFocus(@Nullable Boolean inFocus) { + this.inFocus = inFocus; + } + + private static final String JSON_CACHE_ONLY = "cacheOnly"; + private static final String JSON_CACHE_VALIDITY_IN_MS = "cacheValidityInMs"; + private static final String JSON_IN_FOCUS = "inFocus"; + + @Nullable + @SuppressWarnings("unused") + public static Long getCacheValidityInMsFromJSON(@NonNull JSONObject json) throws JSONException { + return json.has(JSON_CACHE_VALIDITY_IN_MS) ? json.getLong(JSON_CACHE_VALIDITY_IN_MS) : null; + } + + public static void toJSON(@NonNull Filter filter, @NonNull JSONObject json) throws JSONException { + if (filter.cacheOnly != null) { + json.put(JSON_CACHE_ONLY, filter.cacheOnly); + } + if (filter.cacheValidityInMs != null) { + json.put(JSON_CACHE_VALIDITY_IN_MS, filter.cacheValidityInMs); + } + if (filter.inFocus != null) { + json.put(JSON_IN_FOCUS, filter.inFocus); + } + } + + public static void fromJSON(@NonNull Filter filter, @NonNull JSONObject json) throws JSONException { + if (json.has(JSON_CACHE_ONLY)) { + filter.cacheOnly = json.getBoolean(JSON_CACHE_ONLY); + } + if (json.has(JSON_CACHE_VALIDITY_IN_MS)) { + filter.cacheValidityInMs = json.getLong(JSON_CACHE_VALIDITY_IN_MS); + } + if (json.has(JSON_IN_FOCUS)) { + filter.inFocus = json.getBoolean(JSON_IN_FOCUS); + } + } + + @NonNull + protected String toStringParts() { + final StringBuilder sb = new StringBuilder(); + if (this.cacheOnly != null) sb.append("cacheOnly:").append(this.cacheOnly).append(','); + if (this.cacheValidityInMs != null) sb.append("cacheValidityInMs:").append(MTLog.formatDuration(this.cacheValidityInMs)).append(','); + if (this.inFocus != null) sb.append("inFocus:").append(this.inFocus).append(","); + return sb.toString(); + } + } } + diff --git a/src/main/java/org/mtransit/android/commons/provider/gtfs/GTFSRDSProviderExt.kt b/src/main/java/org/mtransit/android/commons/provider/gtfs/GTFSRDSProviderExt.kt index 9655ae26..4f46f343 100644 --- a/src/main/java/org/mtransit/android/commons/provider/gtfs/GTFSRDSProviderExt.kt +++ b/src/main/java/org/mtransit/android/commons/provider/gtfs/GTFSRDSProviderExt.kt @@ -37,7 +37,7 @@ fun Context.getRDS( append(SqlUtils.getWhereEquals(GTFSProviderContract.RouteDirectionStopColumns.T_DIRECTION_K_ID, it)) } } - ) + ).apply { cacheOnly = true } ).toString(), null, SqlUtils.getSortOrderAscending(GTFSProviderContract.RouteDirectionStopColumns.T_DIRECTION_STOPS_K_STOP_SEQUENCE) diff --git a/src/main/java/org/mtransit/android/commons/provider/news/NewsProvider.java b/src/main/java/org/mtransit/android/commons/provider/news/NewsProvider.java index bb2079fb..3e342f9e 100644 --- a/src/main/java/org/mtransit/android/commons/provider/news/NewsProvider.java +++ b/src/main/java/org/mtransit/android/commons/provider/news/NewsProvider.java @@ -189,7 +189,7 @@ private static Cursor getNews(@NonNull NewsProviderContract provider, @Nullable return getNewsCursor(cachedNews); } long cacheValidityInMs = provider.getNewsValidityInMs(newsFilter.isInFocusOrDefault()); - final Long filterCacheValidityInMs = newsFilter.getCacheValidityInMsOrNull(); + final Long filterCacheValidityInMs = newsFilter.getCacheValidityInMs(); if (filterCacheValidityInMs != null && filterCacheValidityInMs > provider.getMinDurationBetweenNewsRefreshInMs(newsFilter.isInFocusOrDefault())) { cacheValidityInMs = filterCacheValidityInMs; } diff --git a/src/main/java/org/mtransit/android/commons/provider/news/NewsProviderContract.java b/src/main/java/org/mtransit/android/commons/provider/news/NewsProviderContract.java index b46c1c66..b28fa9b8 100644 --- a/src/main/java/org/mtransit/android/commons/provider/news/NewsProviderContract.java +++ b/src/main/java/org/mtransit/android/commons/provider/news/NewsProviderContract.java @@ -135,7 +135,7 @@ interface Columns { }; @SuppressWarnings("WeakerAccess") - class Filter implements MTLog.Loggable { + class Filter extends ProviderContract.Filter implements MTLog.Loggable { private static final String LOG_TAG = NewsProviderContract.class.getSimpleName() + ">" + Filter.class.getSimpleName(); @@ -145,21 +145,11 @@ public String getLogTag() { return LOG_TAG; } - private static final boolean CACHE_ONLY_DEFAULT = false; - - private static final boolean IN_FOCUS_DEFAULT = false; - @Nullable private List uuids; @Nullable private List targets; @Nullable - private Boolean cacheOnly = null; - @Nullable - private Long cacheValidityInMs = null; - @Nullable - private Boolean inFocus = null; - @Nullable private Long minCreatedAtInMs = null; @Nullable private Map providedEncryptKeysMap = null; @@ -252,15 +242,13 @@ public Long getMinCreatedAtInMsOrNull() { @NonNull @Override public String toString() { - StringBuilder sb = new StringBuilder(Filter.class.getSimpleName()).append('['); + final StringBuilder sb = new StringBuilder(Filter.class.getSimpleName()).append('['); if (isUUIDFilter(this)) { sb.append("uuids:").append(this.uuids).append(','); } else if (isTargetFilter(this)) { sb.append("targets:").append(this.targets).append(','); } - sb.append("cacheOnly:").append(this.cacheOnly).append(','); - sb.append("inFocus:").append(this.inFocus).append(','); - sb.append("cacheValidityInMs:").append(this.cacheValidityInMs).append(','); + sb.append(super.toStringParts()); sb.append("minCreatedAtInMs:").append(this.minCreatedAtInMs); sb.append(']'); return sb.toString(); @@ -304,54 +292,6 @@ public String getSqlSelection(@NonNull String uuidTableColumn, @NonNull String t return sb.toString(); } - @SuppressWarnings("unused") - @NonNull - public Filter setCacheOnly(@Nullable Boolean cacheOnly) { - this.cacheOnly = cacheOnly; - return this; - } - - public boolean isCacheOnlyOrDefault() { - return this.cacheOnly == null ? CACHE_ONLY_DEFAULT : this.cacheOnly; - } - - @Nullable - public Boolean getCacheOnlyOrNull() { - return this.cacheOnly; - } - - @NonNull - public Filter setInFocus(@Nullable Boolean inFocus) { - this.inFocus = inFocus; - return this; - } - - public boolean isInFocusOrDefault() { - return this.inFocus == null ? IN_FOCUS_DEFAULT : this.inFocus; - } - - @Nullable - public Boolean getInFocusOrNull() { - return this.inFocus; - } - - @Nullable - public Long getCacheValidityInMsOrNull() { - return this.cacheValidityInMs; - } - - @SuppressWarnings("unused") - public boolean hasCacheValidityInMs() { - return this.cacheValidityInMs != null && this.cacheValidityInMs > 0; - } - - @SuppressWarnings("unused") - @NonNull - public Filter setCacheValidityInMs(@Nullable Long cacheValidityInMs) { - this.cacheValidityInMs = cacheValidityInMs; - return this; - } - @NonNull public Filter setProvidedEncryptKeysMap(@Nullable Map providedEncryptKeysMap) { this.providedEncryptKeysMap = providedEncryptKeysMap; @@ -402,16 +342,14 @@ public static Filter fromJSONString(@Nullable String jsonString) { private static final String JSON_UUIDS = "uuids"; private static final String JSON_TARGETS = "targets"; - private static final String JSON_CACHE_ONLY = "cacheOnly"; - private static final String JSON_IN_FOCUS = "inFocus"; - private static final String JSON_CACHE_VALIDITY_IN_MS = "cacheValidityInMs"; private static final String JSON_MIN_CREATED_AT_IN_MS = "minCreatedAtInMs"; private static final String JSON_PROVIDED_ENCRYPT_KEYS_MAP = "providedEncryptKeysMap"; @Nullable public static Filter fromJSON(@NonNull JSONObject json) { try { - Filter newsFilter = new Filter(); + final Filter newsFilter = new Filter(); + ProviderContract.Filter.fromJSON(newsFilter, json); JSONArray jUUIDs = json.optJSONArray(JSON_UUIDS); JSONArray jTargets = json.optJSONArray(JSON_TARGETS); if (jUUIDs != null && jUUIDs.length() > 0) { @@ -427,15 +365,6 @@ public static Filter fromJSON(@NonNull JSONObject json) { } newsFilter.setTargets(targets); } - if (json.has(JSON_CACHE_ONLY)) { - newsFilter.cacheOnly = json.getBoolean(JSON_CACHE_ONLY); - } - if (json.has(JSON_IN_FOCUS)) { - newsFilter.inFocus = json.getBoolean(JSON_IN_FOCUS); - } - if (json.has(JSON_CACHE_VALIDITY_IN_MS)) { - newsFilter.cacheValidityInMs = json.getLong(JSON_CACHE_VALIDITY_IN_MS); - } if (json.has(JSON_MIN_CREATED_AT_IN_MS)) { newsFilter.minCreatedAtInMs = json.getLong(JSON_MIN_CREATED_AT_IN_MS); } @@ -456,26 +385,18 @@ public String toJSONString() { @Nullable public static String toJSONString(@NonNull Filter newsFilter) { - JSONObject json = toJSON(newsFilter); + final JSONObject json = toJSON(newsFilter); return json == null ? null : json.toString(); } @Nullable public static JSONObject toJSON(@NonNull Filter newsFilter) { try { - JSONObject json = new JSONObject(); + final JSONObject json = new JSONObject(); + ProviderContract.Filter.toJSON(newsFilter, json); if (newsFilter.getMinCreatedAtInMsOrNull() != null) { json.put(JSON_MIN_CREATED_AT_IN_MS, newsFilter.getMinCreatedAtInMsOrNull()); } - if (newsFilter.getCacheOnlyOrNull() != null) { - json.put(JSON_CACHE_ONLY, newsFilter.getCacheOnlyOrNull()); - } - if (newsFilter.getInFocusOrNull() != null) { - json.put(JSON_IN_FOCUS, newsFilter.getInFocusOrNull()); - } - if (newsFilter.getCacheValidityInMsOrNull() != null) { - json.put(JSON_CACHE_VALIDITY_IN_MS, newsFilter.getCacheValidityInMsOrNull()); - } if (isUUIDFilter(newsFilter) && newsFilter.uuids != null) { JSONArray jUUIDs = new JSONArray(); for (String uuid : newsFilter.uuids) { diff --git a/src/main/java/org/mtransit/android/commons/provider/poi/POIProviderContract.java b/src/main/java/org/mtransit/android/commons/provider/poi/POIProviderContract.java index 208580f5..1e9da172 100644 --- a/src/main/java/org/mtransit/android/commons/provider/poi/POIProviderContract.java +++ b/src/main/java/org/mtransit/android/commons/provider/poi/POIProviderContract.java @@ -106,7 +106,7 @@ public static String getFkColumnName(@NonNull String key) { } @SuppressWarnings({"WeakerAccess", "unused"}) - class Filter implements MTLog.Loggable { + class Filter extends ProviderContract.Filter implements MTLog.Loggable { private static final String LOG_TAG = POIProviderContract.class.getSimpleName() + ">" + Filter.class.getSimpleName(); @@ -252,7 +252,7 @@ private Filter setArea(double minLat, double maxLat, double minLng, double maxLn @NonNull @Override public String toString() { - StringBuilder sb = new StringBuilder(Filter.class.getSimpleName()).append('['); + final StringBuilder sb = new StringBuilder(Filter.class.getSimpleName()).append('['); if (isAreaFilter(this)) { sb.append("lat:").append(this.lat).append(','); sb.append("lng:").append(this.lng).append(','); @@ -274,6 +274,7 @@ public String toString() { sb.append("sqlSelection:").append(this.sqlSelection).append(','); } sb.append("extras:").append(this.extras).append(','); + sb.append(super.toStringParts()); sb.append(']'); return sb.toString(); } @@ -506,7 +507,8 @@ public static Filter fromJSONString(@Nullable String jsonString) { @SuppressWarnings("ConstantConditions") private static Filter fromJSON(JSONObject json) { try { - Filter poiFilter = new Filter(); + final Filter poiFilter = new Filter(); + ProviderContract.Filter.fromJSON(poiFilter, json); Double lat; Double lng; Double aroundDiff; @@ -606,7 +608,7 @@ private static Filter fromJSON(JSONObject json) { @Nullable public static JSONObject toJSON(@Nullable Filter poiFilter) { try { - JSONObject json = new JSONObject(); + final JSONObject json = new JSONObject(); if (isAreaFilter(poiFilter)) { json.put(JSON_LAT, poiFilter.lat); json.put(JSON_LNG, poiFilter.lng); @@ -647,6 +649,7 @@ public static JSONObject toJSON(@Nullable Filter poiFilter) { } JSONArray jExtras = new JSONArray(); if (poiFilter != null) { + ProviderContract.Filter.toJSON(poiFilter, json); for (int i = 0; i < poiFilter.extras.size(); i++) { JSONObject jExtra = new JSONObject(); jExtra.put(JSON_EXTRAS_KEY, poiFilter.extras.keyAt(i)); diff --git a/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProvider.java b/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProvider.java index 0bd8a311..1583a106 100644 --- a/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProvider.java +++ b/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProvider.java @@ -122,7 +122,7 @@ private static Cursor getServiceUpdates(ServiceUpdateProviderContract provider, return getServiceUpdateCursor(cachedServiceUpdates); } long cacheValidityInMs = provider.getServiceUpdateValidityInMs(serviceUpdateFilter.isInFocusOrDefault()); - Long filterCacheValidityInMs = serviceUpdateFilter.getCacheValidityInMsOrNull(); + Long filterCacheValidityInMs = serviceUpdateFilter.getCacheValidityInMs(); if (filterCacheValidityInMs != null && filterCacheValidityInMs > provider.getMinDurationBetweenServiceUpdateRefreshInMs(serviceUpdateFilter.isInFocusOrDefault())) { cacheValidityInMs = filterCacheValidityInMs; diff --git a/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProviderContract.java b/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProviderContract.java index ce2aff4c..b2fd4e59 100644 --- a/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProviderContract.java +++ b/src/main/java/org/mtransit/android/commons/provider/serviceupdate/ServiceUpdateProviderContract.java @@ -98,7 +98,7 @@ class Columns { } @SuppressWarnings("WeakerAccess") - class Filter implements GTFSRealTimeProviderFilter, MTLog.Loggable { + class Filter extends ProviderContract.Filter implements GTFSRealTimeProviderFilter, MTLog.Loggable { private static final String LOG_TAG = ServiceUpdateProviderContract.class.getSimpleName() + ">" + Filter.class.getSimpleName(); @@ -108,10 +108,6 @@ public String getLogTag() { return LOG_TAG; } - private static final boolean CACHE_ONLY_DEFAULT = false; - - private static final boolean IN_FOCUS_DEFAULT = false; - @Nullable private final POI poi; // RouteDirectionStop or DefaultPOI @Nullable @@ -120,13 +116,6 @@ public String getLogTag() { private final Route route; @Nullable private final RouteDirection routeDirection; - - @Nullable - private Boolean cacheOnly = null; - @Nullable - private Long cacheValidityInMs = null; - @Nullable - private Boolean inFocus = null; @Nullable private Map providedEncryptKeysMap = null; @@ -154,11 +143,8 @@ public Filter(@NonNull String authority, @NonNull RouteDirection routeDirection) @NonNull @Override public String toString() { - final StringBuilder sb = new StringBuilder(); - sb.append(Filter.class.getSimpleName()) - .append("cacheOnly:").append(this.cacheOnly).append(',') - .append("inFocus:").append(this.inFocus).append(',') - .append("cacheValidityInMs:").append(this.cacheValidityInMs).append(','); + final StringBuilder sb = new StringBuilder(Filter.class.getSimpleName()); + sb.append(super.toStringParts()); if (this.poi != null) { sb.append("poi:").append(this.poi).append(','); } @@ -264,48 +250,6 @@ public RouteDirection getRouteDirection() { return routeDirection; } - @SuppressWarnings("unused") - public void setCacheOnly(@Nullable Boolean cacheOnly) { - this.cacheOnly = cacheOnly; - } - - public boolean isCacheOnlyOrDefault() { - return this.cacheOnly == null ? CACHE_ONLY_DEFAULT : this.cacheOnly; - } - - @Nullable - public Boolean getCacheOnlyOrNull() { - return this.cacheOnly; - } - - public void setInFocus(@Nullable Boolean inFocus) { - this.inFocus = inFocus; - } - - public boolean isInFocusOrDefault() { - return this.inFocus == null ? IN_FOCUS_DEFAULT : this.inFocus; - } - - @Nullable - public Boolean getInFocusOrNull() { - return this.inFocus; - } - - @Nullable - public Long getCacheValidityInMsOrNull() { - return this.cacheValidityInMs; - } - - @SuppressWarnings("unused") - public boolean hasCacheValidityInMs() { - return this.cacheValidityInMs != null && this.cacheValidityInMs > 0; - } - - @SuppressWarnings("unused") - public void setCacheValidityInMs(@Nullable Long cacheValidityInMs) { - this.cacheValidityInMs = cacheValidityInMs; - } - @NonNull public Filter setProvidedEncryptKeysMap(@Nullable Map providedEncryptKeysMap) { this.providedEncryptKeysMap = providedEncryptKeysMap; @@ -324,13 +268,9 @@ public Map getProvidedEncryptKeysMap() { @Nullable public String getProvidedEncryptKey(@NonNull String key) { - if (this.providedEncryptKeysMap == null) { - return null; - } + if (this.providedEncryptKeysMap == null) return null; final String value = this.providedEncryptKeysMap.get(key); - if (value == null || value.trim().isEmpty()) { - return null; - } + if (value == null || value.trim().isEmpty()) return null; return value; } @@ -359,9 +299,6 @@ public static Filter fromJSONString(@Nullable String jsonString) { private static final String JSON_ROUTE = "route"; private static final String JSON_ROUTE_DIRECTION = "routeDirection"; private static final String JSON_AUTHORITY = "authority"; - private static final String JSON_CACHE_ONLY = "cacheOnly"; - private static final String JSON_IN_FOCUS = "inFocus"; - private static final String JSON_CACHE_VALIDITY_IN_MS = "cacheValidityInMs"; private static final String JSON_PROVIDED_ENCRYPT_KEYS_MAP = "providedEncryptKeysMap"; @Nullable @@ -383,11 +320,7 @@ public static Filter fromJSON(@NonNull JSONObject json) { } else { return null; // WTF? } - serviceUpdateFilter.cacheOnly = JSONUtils.optBoolean(json, JSON_CACHE_ONLY); - serviceUpdateFilter.inFocus = JSONUtils.optBoolean(json, JSON_IN_FOCUS); - if (json.has(JSON_CACHE_VALIDITY_IN_MS)) { - serviceUpdateFilter.cacheValidityInMs = json.getLong(JSON_CACHE_VALIDITY_IN_MS); - } + ProviderContract.Filter.fromJSON(serviceUpdateFilter, json); if (json.has(JSON_PROVIDED_ENCRYPT_KEYS_MAP)) { serviceUpdateFilter.providedEncryptKeysMap = JSONUtils.toMapOfStrings(json.getJSONObject(JSON_PROVIDED_ENCRYPT_KEYS_MAP)); } @@ -405,14 +338,15 @@ public String toJSONString() { @Nullable public static String toJSONString(@NonNull Filter serviceUpdateFilter) { - JSONObject json = toJSON(serviceUpdateFilter); + final JSONObject json = toJSON(serviceUpdateFilter); return json == null ? null : json.toString(); } @Nullable public static JSONObject toJSON(@NonNull Filter serviceUpdateFilter) { try { - JSONObject json = new JSONObject(); + final JSONObject json = new JSONObject(); + ProviderContract.Filter.toJSON(serviceUpdateFilter, json); if (serviceUpdateFilter.poi != null) { json.put(JSON_POI, serviceUpdateFilter.poi.toJSON()); } @@ -425,15 +359,6 @@ public static JSONObject toJSON(@NonNull Filter serviceUpdateFilter) { if (serviceUpdateFilter.authority != null) { json.put(JSON_AUTHORITY, serviceUpdateFilter.authority); } - if (serviceUpdateFilter.getCacheOnlyOrNull() != null) { - json.put(JSON_CACHE_ONLY, serviceUpdateFilter.getCacheOnlyOrNull()); - } - if (serviceUpdateFilter.getInFocusOrNull() != null) { - json.put(JSON_IN_FOCUS, serviceUpdateFilter.getInFocusOrNull()); - } - if (serviceUpdateFilter.getCacheValidityInMsOrNull() != null) { - json.put(JSON_CACHE_VALIDITY_IN_MS, serviceUpdateFilter.getCacheValidityInMsOrNull()); - } if (serviceUpdateFilter.getProvidedEncryptKeysMap() != null) { json.put(JSON_PROVIDED_ENCRYPT_KEYS_MAP, JSONUtils.toJSONObject(serviceUpdateFilter.getProvidedEncryptKeysMap())); } diff --git a/src/main/java/org/mtransit/android/commons/provider/status/StatusProvider.java b/src/main/java/org/mtransit/android/commons/provider/status/StatusProvider.java index 9eefc5ab..b8a68e5b 100644 --- a/src/main/java/org/mtransit/android/commons/provider/status/StatusProvider.java +++ b/src/main/java/org/mtransit/android/commons/provider/status/StatusProvider.java @@ -116,7 +116,7 @@ private static Cursor getStatus(@NonNull StatusProviderContract provider, @Nulla } // 3 - check if usable cache still valid (or if it could be refreshed) long cacheValidityInMs = provider.getStatusValidityInMs(statusFilter.isInFocusOrDefault()); - Long filterCacheValidityInMs = statusFilter.getCacheValidityInMsOrNull(); + Long filterCacheValidityInMs = statusFilter.getCacheValidityInMs(); if (filterCacheValidityInMs != null && filterCacheValidityInMs > provider.getMinDurationBetweenRefreshInMs(statusFilter.isInFocusOrDefault())) { cacheValidityInMs = filterCacheValidityInMs; } @@ -133,7 +133,7 @@ private static Cursor getStatus(@NonNull StatusProviderContract provider, @Nulla @Nullable private static StatusProviderContract.Filter extractStatusFilter(@Nullable String selection) { - int type = StatusProviderContract.Filter.getTypeFromJSONString(selection); + final int type = StatusProviderContract.Filter.getTypeFromJSONString(selection); StatusProviderContract.Filter statusFilter; switch (type) { case POI.ITEM_STATUS_TYPE_NONE: diff --git a/src/main/java/org/mtransit/android/commons/provider/status/StatusProviderContract.java b/src/main/java/org/mtransit/android/commons/provider/status/StatusProviderContract.java index a4489f55..f81b59d0 100644 --- a/src/main/java/org/mtransit/android/commons/provider/status/StatusProviderContract.java +++ b/src/main/java/org/mtransit/android/commons/provider/status/StatusProviderContract.java @@ -11,6 +11,7 @@ import org.mtransit.android.commons.JSONUtils; import org.mtransit.android.commons.MTLog; import org.mtransit.android.commons.SecureStringUtils; +import org.mtransit.android.commons.data.POI; import org.mtransit.android.commons.data.POIStatus; import org.mtransit.android.commons.provider.common.ProviderContract; @@ -68,7 +69,7 @@ class Columns { } @SuppressWarnings("WeakerAccess") - abstract class Filter implements MTLog.Loggable { + abstract class Filter extends ProviderContract.Filter implements MTLog.Loggable { private static final String LOG_TAG = StatusProviderContract.class.getSimpleName() + ">" + Filter.class.getSimpleName(); @@ -78,23 +79,14 @@ public String getLogTag() { return LOG_TAG; } - private static final boolean CACHE_ONLY_DEFAULT = false; - - private static final boolean IN_FOCUS_DEFAULT = false; - @NonNull private String targetUUID; + @POI.ItemStatusType private int type; @Nullable - private Boolean cacheOnly = null; - @Nullable - private Long cacheValidityInMs = null; - @Nullable - private Boolean inFocus = null; - @Nullable private Map providedEncryptKeysMap = null; - public Filter(int type, @NonNull String targetUUID) { + public Filter(@POI.ItemStatusType int type, @NonNull String targetUUID) { this.type = type; this.targetUUID = targetUUID; } @@ -104,47 +96,11 @@ public String getTargetUUID() { return this.targetUUID; } + @POI.ItemStatusType public int getType() { return this.type; } - public boolean isCacheOnlyOrDefault() { - return this.cacheOnly == null ? CACHE_ONLY_DEFAULT : this.cacheOnly; - } - - @Nullable - public Boolean getCacheOnlyOrNull() { - return this.cacheOnly; - } - - public void setInFocus(@Nullable Boolean inFocus) { - this.inFocus = inFocus; - } - - public boolean isInFocusOrDefault() { - return this.inFocus == null ? IN_FOCUS_DEFAULT : this.inFocus; - } - - @Nullable - public Boolean getInFocusOrNull() { - return this.inFocus; - } - - @Nullable - public Long getCacheValidityInMsOrNull() { - return this.cacheValidityInMs; - } - - @SuppressWarnings("unused") - public boolean hasCacheValidityInMs() { - return cacheValidityInMs != null && cacheValidityInMs > 0; - } - - @SuppressWarnings("unused") - public void setCacheValidityInMs(@Nullable Long cacheValidityInMs) { - this.cacheValidityInMs = cacheValidityInMs; - } - @NonNull public Filter setProvidedEncryptKeysMap(@Nullable Map providedEncryptKeysMap) { this.providedEncryptKeysMap = providedEncryptKeysMap; @@ -162,13 +118,9 @@ public Map getProvidedEncryptKeysMap() { @Nullable public String getProvidedEncryptKey(@NonNull String key) { - if (this.providedEncryptKeysMap == null) { - return null; - } + if (this.providedEncryptKeysMap == null) return null; final String value = this.providedEncryptKeysMap.get(key); - if (value == null || value.trim().isEmpty()) { - return null; - } + if (value == null || value.trim().isEmpty()) return null; return value; } @@ -201,24 +153,10 @@ public static String getTargetUUIDFromJSON(@NonNull JSONObject json) throws JSON return json.getString(JSON_TARGET); } - @Nullable - @SuppressWarnings("unused") - public static Long getCacheValidityInMsFromJSON(@NonNull JSONObject json) throws JSONException { - return json.has(JSON_CACHE_VALIDITY_IN_MS) ? json.getLong(JSON_CACHE_VALIDITY_IN_MS) : null; - } - public static void toJSON(@NonNull Filter statusFilter, @NonNull JSONObject json) throws JSONException { + ProviderContract.Filter.toJSON(statusFilter, json); json.put(JSON_TYPE, statusFilter.getType()); json.put(JSON_TARGET, statusFilter.getTargetUUID()); - if (statusFilter.getCacheOnlyOrNull() != null) { - json.put(JSON_CACHE_ONLY, statusFilter.getCacheOnlyOrNull()); - } - if (statusFilter.getInFocusOrNull() != null) { - json.put(JSON_IN_FOCUS, statusFilter.getInFocusOrNull()); - } - if (statusFilter.getCacheValidityInMsOrNull() != null) { - json.put(JSON_CACHE_VALIDITY_IN_MS, statusFilter.getCacheValidityInMsOrNull()); - } if (statusFilter.getProvidedEncryptKeysMap() != null) { json.put(JSON_PROVIDED_ENCRYPT_KEYS_MAP, JSONUtils.toJSONObject(statusFilter.getProvidedEncryptKeysMap())); } @@ -226,23 +164,12 @@ public static void toJSON(@NonNull Filter statusFilter, @NonNull JSONObject json private static final String JSON_TYPE = "type"; private static final String JSON_TARGET = "target"; - private static final String JSON_CACHE_ONLY = "cacheOnly"; - private static final String JSON_IN_FOCUS = "inFocus"; - private static final String JSON_CACHE_VALIDITY_IN_MS = "cacheValidityInMs"; private static final String JSON_PROVIDED_ENCRYPT_KEYS_MAP = "providedEncryptKeysMap"; public static void fromJSON(@NonNull Filter statusFilter, @NonNull JSONObject json) throws JSONException { + ProviderContract.Filter.fromJSON(statusFilter, json); statusFilter.type = json.getInt(JSON_TYPE); statusFilter.targetUUID = json.getString(JSON_TARGET); - if (json.has(JSON_CACHE_ONLY)) { - statusFilter.cacheOnly = json.getBoolean(JSON_CACHE_ONLY); - } - if (json.has(JSON_IN_FOCUS)) { - statusFilter.inFocus = json.getBoolean(JSON_IN_FOCUS); - } - if (json.has(JSON_CACHE_VALIDITY_IN_MS)) { - statusFilter.cacheValidityInMs = json.getLong(JSON_CACHE_VALIDITY_IN_MS); - } if (json.has(JSON_PROVIDED_ENCRYPT_KEYS_MAP)) { statusFilter.providedEncryptKeysMap = JSONUtils.toMapOfStrings(json.getJSONObject(JSON_PROVIDED_ENCRYPT_KEYS_MAP)); } @@ -262,9 +189,7 @@ public String toString() { return Filter.class.getSimpleName() + "{" + "targetUUID='" + targetUUID + '\'' + ", type=" + type + - ", cacheOnly=" + cacheOnly + - ", cacheValidityInMs=" + cacheValidityInMs + - ", inFocus=" + inFocus + + super.toStringParts() + '}'; } } diff --git a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/GTFSRealTimeVehiclePositionsProvider.kt b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/GTFSRealTimeVehiclePositionsProvider.kt index 97cfe75e..7f7a4563 100644 --- a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/GTFSRealTimeVehiclePositionsProvider.kt +++ b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/GTFSRealTimeVehiclePositionsProvider.kt @@ -144,7 +144,7 @@ object GTFSRealTimeVehiclePositionsProvider : MTLog.Loggable { @JvmStatic fun GTFSRealTimeProvider.getNew(filter: VehicleLocationProviderContract.Filter): List? { - updateAgencyDataIfRequired(filter.inFocusOrDefault) + updateAgencyDataIfRequired(filter.isInFocusOrDefault) return getCached(filter) } diff --git a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/NextBusVehicleLocationsProvider.kt b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/NextBusVehicleLocationsProvider.kt index ce7a3d24..00ff1a01 100644 --- a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/NextBusVehicleLocationsProvider.kt +++ b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/NextBusVehicleLocationsProvider.kt @@ -94,7 +94,7 @@ object NextBusVehicleLocationsProvider { @JvmStatic fun NextBusProvider.getNew(filter: VehicleLocationProviderContract.Filter): List? { - updateAgencyDataIfRequired(filter.inFocusOrDefault) + updateAgencyDataIfRequired(filter.isInFocusOrDefault) return getCached(filter) } diff --git a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProvider.kt b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProvider.kt index 55530b51..62611fa0 100644 --- a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProvider.kt +++ b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProvider.kt @@ -72,13 +72,13 @@ abstract class VehicleLocationProvider : MTContentProvider(), } } } - if (filter.cacheOnlyOrDefault) { + if (filter.isCacheOnlyOrDefault) { if (cachedVehicleLocations.isNullOrEmpty()) { MTLog.w(this, "getVehicleLocations() > No useful cache found!") } return getVehicleLocationCursor(cachedVehicleLocations) } - val cacheValidityInMs = getVehicleLocationValidityInMs(filter.inFocusOrDefault) + val cacheValidityInMs = getVehicleLocationValidityInMs(filter.isInFocusOrDefault) // TODO filter cache validity override like service update? var loadNewVehicleLocations = false if (cachedVehicleLocations.isNullOrEmpty()) { diff --git a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProviderContract.kt b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProviderContract.kt index 28c9e99b..5c0030bb 100644 --- a/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProviderContract.kt +++ b/src/main/java/org/mtransit/android/commons/provider/vehiclelocations/VehicleLocationProviderContract.kt @@ -95,13 +95,7 @@ interface VehicleLocationProviderContract : ProviderContract { override val poi: POI? = null, // RouteDirectionStop or DefaultPOI override val route: Route? = null, override val routeDirection: RouteDirection? = null, - ) : GTFSRealTimeProviderFilter, MTLog.Loggable { - - var inFocus: Boolean? = null - val inFocusOrDefault get() = inFocus ?: false - - var cacheOnly: Boolean? = null - val cacheOnlyOrDefault get() = cacheOnly ?: false + ) : ProviderContract.Filter(), GTFSRealTimeProviderFilter, MTLog.Loggable { var providedEncryptKeysMap: Map? = null private set @@ -138,8 +132,6 @@ interface VehicleLocationProviderContract : ProviderContract { private const val JSON_POI = "poi" private const val JSON_ROUTE = "route" private const val JSON_ROUTE_DIRECTION = "routeDirection" - private const val JSON_CACHE_ONLY = "cacheOnly" - private const val JSON_IN_FOCUS = "inFocus" private const val JSON_PROVIDED_ENCRYPT_KEYS_MAP = "providedEncryptKeysMap" fun fromJSONString(jsonString: String?): Filter? { @@ -163,8 +155,6 @@ interface VehicleLocationProviderContract : ProviderContract { val routeDirection = json.optJSONObject(JSON_ROUTE_DIRECTION)?.let { jRouteDirection -> authority?.let { RouteDirection.fromJSON(jRouteDirection, it) } } - val inFocus = JSONUtils.optBoolean(json, JSON_IN_FOCUS) - val cacheOnly = JSONUtils.optBoolean(json, JSON_CACHE_ONLY) val providedEncryptKeysMap: Map? = json.optJSONObject(JSON_PROVIDED_ENCRYPT_KEYS_MAP)?.let { jProvidedEncryptKeysMap -> JSONUtils.toMapOfStrings(jProvidedEncryptKeysMap) } @@ -172,8 +162,7 @@ interface VehicleLocationProviderContract : ProviderContract { ?: route?.let { Filter(authority = route.authority, route = it) } ?: routeDirection?.let { Filter(authority = routeDirection.authority, routeDirection = it) }) ?.apply { - this.inFocus = inFocus - this.cacheOnly = cacheOnly + fromJSON(this, json) this.providedEncryptKeysMap = providedEncryptKeysMap } } @@ -184,12 +173,11 @@ interface VehicleLocationProviderContract : ProviderContract { fun toJSON(vehicleLocationFilter: Filter): JSONObject? { return try { JSONObject().apply { + toJSON(vehicleLocationFilter, this) put(JSON_AUTHORITY, vehicleLocationFilter.authority) vehicleLocationFilter.poi?.let { put(JSON_POI, it.toJSON()) } vehicleLocationFilter.route?.let { put(JSON_ROUTE, Route.toJSON(it)) } vehicleLocationFilter.routeDirection?.let { put(JSON_ROUTE_DIRECTION, RouteDirection.toJSON(it)) } - vehicleLocationFilter.inFocus?.let { put(JSON_IN_FOCUS, it) } - vehicleLocationFilter.cacheOnly?.let { put(JSON_CACHE_ONLY, it) } vehicleLocationFilter.providedEncryptKeysMap?.let { put(JSON_PROVIDED_ENCRYPT_KEYS_MAP, JSONUtils.toJSONObject(it)) } } } catch (jsone: JSONException) { diff --git a/src/main/java/org/mtransit/android/commons/task/MTCancellableAsyncTask.kt b/src/main/java/org/mtransit/android/commons/task/MTCancellableAsyncTask.kt index 1858e622..4084a0fe 100644 --- a/src/main/java/org/mtransit/android/commons/task/MTCancellableAsyncTask.kt +++ b/src/main/java/org/mtransit/android/commons/task/MTCancellableAsyncTask.kt @@ -11,12 +11,12 @@ abstract class MTCancellableAsyncTask : @WorkerThread override fun doInBackgroundMT(vararg params: Params?): Result? { - if (isCancelled) { - return null - } + if (isCancelledMT) return null return doInBackgroundNotCancelledMT(*params) } + open val isCancelledMT: Boolean get() = isCancelled + @WorkerThread protected abstract fun doInBackgroundNotCancelledMT(vararg params: Params?): Result? @@ -24,9 +24,7 @@ abstract class MTCancellableAsyncTask : @MainThread override fun onProgressUpdate(vararg values: Progress?) { super.onProgressUpdate(*values) - if (isCancelled) { - return - } + if (isCancelledMT) return onProgressUpdateNotCancelledMT(*values) } @@ -42,9 +40,7 @@ abstract class MTCancellableAsyncTask : @MainThread override fun onPostExecute(result: Result?) { super.onPostExecute(result) - if (isCancelled) { - return - } + if (isCancelledMT) return onPostExecuteNotCancelledMT(result) } @@ -55,4 +51,4 @@ abstract class MTCancellableAsyncTask : ) { // not mandatory } -} \ No newline at end of file +}