Skip to content
Draft
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
10 changes: 5 additions & 5 deletions src/main/java/org/mtransit/android/commons/data/AppStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.mtransit.android.commons.data

data class ServiceUpdates(
val serviceUpdates: List<ServiceUpdate>?,
)
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ 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)
?.first { provider ->
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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<String> uuids;
@Nullable
private List<String> targets;
@Nullable
private Boolean cacheOnly = null;
@Nullable
private Long cacheValidityInMs = null;
@Nullable
private Boolean inFocus = null;
@Nullable
private Long minCreatedAtInMs = null;
@Nullable
private Map<String, String> providedEncryptKeysMap = null;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String, String> providedEncryptKeysMap) {
this.providedEncryptKeysMap = providedEncryptKeysMap;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand Down
Loading