Skip to content

Commit 1f6489b

Browse files
authored
ProviderContract.Filter > shared filter params (#164)
1 parent e3f86a1 commit 1f6489b

51 files changed

Lines changed: 1867 additions & 2532 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.mtransit.android.commons
2+
3+
import org.json.JSONObject
4+
5+
fun JSONObject.optLong(name: String, fallback: Long? = null) =
6+
takeIf { it.has(name) && !it.isNull(name) }?.optLong(name) ?: fallback
7+
8+
fun JSONObject.optInt(name: String, fallback: Int? = null) =
9+
takeIf { it.has(name) && !it.isNull(name) }?.optInt(name) ?: fallback
10+
11+
fun JSONObject.optDouble(name: String, fallback: Double? = null) =
12+
takeIf { it.has(name) && !it.isNull(name) }?.optDouble(name) ?: fallback
13+
14+
fun JSONObject.optBoolean(name: String, fallback: Boolean? = null) =
15+
takeIf { it.has(name) && !it.isNull(name) }?.optBoolean(name) ?: fallback
16+
17+
fun JSONObject.optString(name: String, fallback: String? = null) =
18+
takeIf { it.has(name) && !it.isNull(name) }?.optString(name) ?: fallback
19+

src/main/java/org/mtransit/android/commons/data/AppStatus.java

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -191,88 +191,4 @@ public JSONObject getExtrasJSON() {
191191
return null; // no partial result
192192
}
193193
}
194-
195-
public static class AppStatusFilter extends StatusProviderContract.Filter {
196-
197-
private static final String LOG_TAG = AppStatusFilter.class.getSimpleName();
198-
199-
@NonNull
200-
@Override
201-
public String getLogTag() {
202-
return LOG_TAG;
203-
}
204-
205-
@NonNull
206-
private final String pkg;
207-
208-
public AppStatusFilter(@NonNull String targetUUID, @NonNull String pkg) {
209-
super(POI.ITEM_STATUS_TYPE_APP, targetUUID);
210-
this.pkg = pkg;
211-
}
212-
213-
@NonNull
214-
public String getPkg() {
215-
return pkg;
216-
}
217-
218-
@Nullable
219-
@Override
220-
public StatusProviderContract.Filter fromJSONStringStatic(@Nullable String jsonString) {
221-
return fromJSONString(jsonString);
222-
}
223-
224-
@Nullable
225-
public static StatusProviderContract.Filter fromJSONString(@Nullable String jsonString) {
226-
try {
227-
return jsonString == null ? null : fromJSON(new JSONObject(jsonString));
228-
} catch (JSONException jsone) {
229-
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '%s'", jsonString);
230-
return null;
231-
}
232-
}
233-
234-
private static final String JSON_PKG = "pkg";
235-
236-
@Nullable
237-
public static StatusProviderContract.Filter fromJSON(@NonNull JSONObject json) {
238-
try {
239-
String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json);
240-
String pkg = json.getString(JSON_PKG);
241-
AppStatusFilter appStatusFilter = new AppStatusFilter(targetUUID, pkg);
242-
StatusProviderContract.Filter.fromJSON(appStatusFilter, json);
243-
return appStatusFilter;
244-
} catch (JSONException jsone) {
245-
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '%s'", json);
246-
return null;
247-
}
248-
}
249-
250-
@Nullable
251-
@Override
252-
public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFilter) {
253-
return toJSONString(statusFilter);
254-
}
255-
256-
@Nullable
257-
private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) {
258-
JSONObject json = toJSON(statusFilter);
259-
return json == null ? null : json.toString();
260-
}
261-
262-
@Nullable
263-
private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) {
264-
try {
265-
JSONObject json = new JSONObject();
266-
StatusProviderContract.Filter.toJSON(statusFilter, json);
267-
if (statusFilter instanceof AppStatusFilter) {
268-
AppStatusFilter appStatusFilter = (AppStatusFilter) statusFilter;
269-
json.put(JSON_PKG, appStatusFilter.pkg);
270-
}
271-
return json;
272-
} catch (JSONException jsone) {
273-
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter);
274-
return null;
275-
}
276-
}
277-
}
278194
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package org.mtransit.android.commons.data
2+
3+
import org.json.JSONException
4+
import org.json.JSONObject
5+
import org.mtransit.android.commons.MTLog
6+
import org.mtransit.android.commons.provider.status.StatusProviderContract
7+
import org.mtransit.commons.model.Secret
8+
9+
data class AppStatusFilter(
10+
override val cacheOnly: Boolean? = null,
11+
override val cacheValidityInMs: Long? = null,
12+
override val inFocus: Boolean? = null,
13+
override val providedEncryptKeysMap: Secret<Map<String, String>>? = null,
14+
override val targetUUID: String,
15+
val pkg: String
16+
) : StatusProviderContract.Filter(POI.ITEM_STATUS_TYPE_APP, targetUUID) {
17+
18+
companion object {
19+
private val LOG_TAG: String = AppStatusFilter::class.java.getSimpleName()
20+
21+
@JvmStatic
22+
fun from(targetUUID: String, pkg: String) = AppStatusFilter(
23+
targetUUID = targetUUID,
24+
pkg = pkg,
25+
)
26+
27+
@JvmStatic
28+
fun fromJSONString(jsonString: String?): StatusProviderContract.Filter? {
29+
try {
30+
return jsonString?.let { fromJSON(JSONObject(it)) }
31+
} catch (jsone: JSONException) {
32+
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '$jsonString'")
33+
return null
34+
}
35+
}
36+
37+
private const val JSON_PKG = "pkg"
38+
39+
fun fromJSON(json: JSONObject): StatusProviderContract.Filter? {
40+
try {
41+
return AppStatusFilter(
42+
cacheOnly = getCacheOnlyFromJSON(json),
43+
cacheValidityInMs = getCacheValidityInMsFromJSON(json),
44+
inFocus = getInFocusFromJSON(json),
45+
providedEncryptKeysMap = getProvidedEncryptKeysMapFromJSON(json),
46+
targetUUID = getTargetUUIDFromJSON(json),
47+
pkg = json.getString(JSON_PKG)
48+
)
49+
} catch (jsone: JSONException) {
50+
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '$json'")
51+
return null
52+
}
53+
}
54+
55+
private fun toJSONString(statusFilter: StatusProviderContract.Filter) = toJSON(statusFilter)?.toString()
56+
57+
private fun toJSON(statusFilter: StatusProviderContract.Filter): JSONObject? {
58+
try {
59+
return JSONObject().apply {
60+
toJSON(statusFilter, this)
61+
if (statusFilter is AppStatusFilter) {
62+
put(JSON_PKG, statusFilter.pkg)
63+
}
64+
}
65+
} catch (jsone: JSONException) {
66+
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter)
67+
return null
68+
}
69+
}
70+
}
71+
72+
override fun getLogTag() = LOG_TAG
73+
74+
override fun copyWith(providedEncryptKeysMap: Secret<Map<String, String>>?) = this.copy(providedEncryptKeysMap = providedEncryptKeysMap)
75+
76+
override fun fromJSONStringStatic(jsonString: String?) = fromJSONString(jsonString)
77+
78+
override fun toJSONString() = toJSONString(this)
79+
80+
override fun toJSONStringStatic(statusFilter: StatusProviderContract.Filter) = toJSONString(statusFilter)
81+
}

src/main/java/org/mtransit/android/commons/data/AvailabilityPercent.java

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -608,72 +608,5 @@ public String toString() {
608608
", statusMsgId=" + statusMsgId +
609609
'}';
610610
}
611-
612-
public static class AvailabilityPercentStatusFilter extends StatusProviderContract.Filter {
613-
614-
private static final String LOG_TAG = AvailabilityPercentStatusFilter.class.getSimpleName();
615-
616-
@NonNull
617-
@Override
618-
public String getLogTag() {
619-
return LOG_TAG;
620-
}
621-
622-
public AvailabilityPercentStatusFilter(@NonNull String targetUUID) {
623-
super(POI.ITEM_STATUS_TYPE_AVAILABILITY_PERCENT, targetUUID);
624-
}
625-
626-
@Nullable
627-
@Override
628-
public StatusProviderContract.Filter fromJSONStringStatic(@Nullable String jsonString) {
629-
return fromJSONString(jsonString);
630-
}
631-
632-
@Nullable
633-
public static StatusProviderContract.Filter fromJSONString(@Nullable String jsonString) {
634-
try {
635-
return jsonString == null ? null : fromJSON(new JSONObject(jsonString));
636-
} catch (JSONException jsone) {
637-
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '%s'", jsonString);
638-
return null;
639-
}
640-
}
641-
642-
@Nullable
643-
public static StatusProviderContract.Filter fromJSON(@NonNull JSONObject json) {
644-
try {
645-
String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json);
646-
AvailabilityPercentStatusFilter availabilityPercentStatusFilter = new AvailabilityPercentStatusFilter(targetUUID);
647-
StatusProviderContract.Filter.fromJSON(availabilityPercentStatusFilter, json);
648-
return availabilityPercentStatusFilter;
649-
} catch (JSONException jsone) {
650-
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '%s'", json);
651-
return null;
652-
}
653-
}
654-
655-
@Nullable
656-
@Override
657-
public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFilter) {
658-
return toJSONString(statusFilter);
659-
}
660-
661-
@Nullable
662-
private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) {
663-
JSONObject json = toJSON(statusFilter);
664-
return json == null ? null : json.toString();
665-
}
666-
667-
@Nullable
668-
private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) {
669-
try {
670-
JSONObject json = new JSONObject();
671-
StatusProviderContract.Filter.toJSON(statusFilter, json);
672-
return json;
673-
} catch (JSONException jsone) {
674-
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter);
675-
return null;
676-
}
677-
}
678-
}
679611
}
612+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package org.mtransit.android.commons.data
2+
3+
import org.json.JSONException
4+
import org.json.JSONObject
5+
import org.mtransit.android.commons.MTLog
6+
import org.mtransit.android.commons.provider.status.StatusProviderContract
7+
import org.mtransit.commons.model.Secret
8+
9+
data class AvailabilityPercentStatusFilter(
10+
override val cacheOnly: Boolean? = null,
11+
override val cacheValidityInMs: Long? = null,
12+
override val inFocus: Boolean? = null,
13+
override val providedEncryptKeysMap: Secret<Map<String, String>>? = null,
14+
override val targetUUID: String,
15+
) : StatusProviderContract.Filter(POI.ITEM_STATUS_TYPE_AVAILABILITY_PERCENT, targetUUID) {
16+
17+
companion object {
18+
private val LOG_TAG: String = AvailabilityPercentStatusFilter::class.java.getSimpleName()
19+
20+
@JvmStatic
21+
fun from(poi: POI) = from(poi.uuid)
22+
23+
@JvmStatic
24+
fun from(targetUUID: String) = AvailabilityPercentStatusFilter(
25+
targetUUID = targetUUID,
26+
)
27+
28+
@JvmStatic
29+
fun fromJSONString(jsonString: String?): StatusProviderContract.Filter? {
30+
try {
31+
return jsonString?.let { fromJSON(JSONObject(it)) }
32+
} catch (jsone: JSONException) {
33+
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '$jsonString'")
34+
return null
35+
}
36+
}
37+
38+
fun fromJSON(json: JSONObject): StatusProviderContract.Filter? {
39+
try {
40+
return AvailabilityPercentStatusFilter(
41+
cacheOnly = getCacheOnlyFromJSON(json),
42+
cacheValidityInMs = getCacheValidityInMsFromJSON(json),
43+
inFocus = getInFocusFromJSON(json),
44+
providedEncryptKeysMap = getProvidedEncryptKeysMapFromJSON(json),
45+
targetUUID = getTargetUUIDFromJSON(json),
46+
)
47+
} catch (jsone: JSONException) {
48+
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '$json'")
49+
return null
50+
}
51+
}
52+
53+
private fun toJSONString(statusFilter: StatusProviderContract.Filter) = toJSON(statusFilter)?.toString()
54+
55+
private fun toJSON(statusFilter: StatusProviderContract.Filter): JSONObject? {
56+
try {
57+
return JSONObject().apply {
58+
toJSON(statusFilter, this)
59+
}
60+
} catch (jsone: JSONException) {
61+
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '$statusFilter'")
62+
return null
63+
}
64+
}
65+
}
66+
67+
override fun getLogTag() = LOG_TAG
68+
69+
override fun copyWith(providedEncryptKeysMap: Secret<Map<String, String>>?) = this.copy(providedEncryptKeysMap = providedEncryptKeysMap)
70+
71+
override fun fromJSONStringStatic(jsonString: String?) = fromJSONString(jsonString)
72+
73+
override fun toJSONString() = toJSONString(this)
74+
75+
override fun toJSONStringStatic(statusFilter: StatusProviderContract.Filter) = toJSONString(statusFilter)
76+
}

src/main/java/org/mtransit/android/commons/data/POIStatus.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static POIStatus fromCursor(@NonNull Cursor cursor) {
123123

124124
@NonNull
125125
public Cursor toCursor() {
126-
MatrixCursor cursor = new MatrixCursor(StatusProviderContract.PROJECTION_STATUS);
126+
final MatrixCursor cursor = new MatrixCursor(StatusProviderContract.getPROJECTION_STATUS());
127127
cursor.addRow(new Object[]{
128128
this.id,
129129
this.type,

0 commit comments

Comments
 (0)