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
19 changes: 19 additions & 0 deletions src/main/java/org/mtransit/android/commons/JSONExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.mtransit.android.commons

import org.json.JSONObject

fun JSONObject.optLong(name: String, fallback: Long? = null) =
takeIf { it.has(name) && !it.isNull(name) }?.optLong(name) ?: fallback

fun JSONObject.optInt(name: String, fallback: Int? = null) =
takeIf { it.has(name) && !it.isNull(name) }?.optInt(name) ?: fallback

fun JSONObject.optDouble(name: String, fallback: Double? = null) =
takeIf { it.has(name) && !it.isNull(name) }?.optDouble(name) ?: fallback

fun JSONObject.optBoolean(name: String, fallback: Boolean? = null) =
takeIf { it.has(name) && !it.isNull(name) }?.optBoolean(name) ?: fallback

fun JSONObject.optString(name: String, fallback: String? = null) =
takeIf { it.has(name) && !it.isNull(name) }?.optString(name) ?: fallback

84 changes: 0 additions & 84 deletions src/main/java/org/mtransit/android/commons/data/AppStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,88 +191,4 @@ public JSONObject getExtrasJSON() {
return null; // no partial result
}
}

public static class AppStatusFilter extends StatusProviderContract.Filter {

private static final String LOG_TAG = AppStatusFilter.class.getSimpleName();

@NonNull
@Override
public String getLogTag() {
return LOG_TAG;
}

@NonNull
private final String pkg;

public AppStatusFilter(@NonNull String targetUUID, @NonNull String pkg) {
super(POI.ITEM_STATUS_TYPE_APP, targetUUID);
this.pkg = pkg;
}

@NonNull
public String getPkg() {
return pkg;
}

@Nullable
@Override
public StatusProviderContract.Filter fromJSONStringStatic(@Nullable String jsonString) {
return fromJSONString(jsonString);
}

@Nullable
public static StatusProviderContract.Filter fromJSONString(@Nullable String jsonString) {
try {
return jsonString == null ? null : fromJSON(new JSONObject(jsonString));
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '%s'", jsonString);
return null;
}
}

private static final String JSON_PKG = "pkg";

@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);
StatusProviderContract.Filter.fromJSON(appStatusFilter, json);
return appStatusFilter;
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '%s'", json);
return null;
}
}

@Nullable
@Override
public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFilter) {
return toJSONString(statusFilter);
}

@Nullable
private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) {
JSONObject json = toJSON(statusFilter);
return json == null ? null : json.toString();
}

@Nullable
private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) {
try {
JSONObject json = new JSONObject();
StatusProviderContract.Filter.toJSON(statusFilter, json);
if (statusFilter instanceof AppStatusFilter) {
AppStatusFilter appStatusFilter = (AppStatusFilter) statusFilter;
json.put(JSON_PKG, appStatusFilter.pkg);
}
return json;
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter);
return null;
}
}
}
}
81 changes: 81 additions & 0 deletions src/main/java/org/mtransit/android/commons/data/AppStatusFilter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.mtransit.android.commons.data

import org.json.JSONException
import org.json.JSONObject
import org.mtransit.android.commons.MTLog
import org.mtransit.android.commons.provider.status.StatusProviderContract
import org.mtransit.commons.model.Secret

data class AppStatusFilter(
override val cacheOnly: Boolean? = null,
override val cacheValidityInMs: Long? = null,
override val inFocus: Boolean? = null,
override val providedEncryptKeysMap: Secret<Map<String, String>>? = null,
override val targetUUID: String,
val pkg: String
) : StatusProviderContract.Filter(POI.ITEM_STATUS_TYPE_APP, targetUUID) {

companion object {
private val LOG_TAG: String = AppStatusFilter::class.java.getSimpleName()

@JvmStatic
fun from(targetUUID: String, pkg: String) = AppStatusFilter(
targetUUID = targetUUID,
pkg = pkg,
)

@JvmStatic
fun fromJSONString(jsonString: String?): StatusProviderContract.Filter? {
try {
return jsonString?.let { fromJSON(JSONObject(it)) }
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '$jsonString'")
return null
}
}

private const val JSON_PKG = "pkg"

fun fromJSON(json: JSONObject): StatusProviderContract.Filter? {
try {
return AppStatusFilter(
cacheOnly = getCacheOnlyFromJSON(json),
cacheValidityInMs = getCacheValidityInMsFromJSON(json),
inFocus = getInFocusFromJSON(json),
providedEncryptKeysMap = getProvidedEncryptKeysMapFromJSON(json),
targetUUID = getTargetUUIDFromJSON(json),
pkg = json.getString(JSON_PKG)
)
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '$json'")
return null
}
}

private fun toJSONString(statusFilter: StatusProviderContract.Filter) = toJSON(statusFilter)?.toString()

private fun toJSON(statusFilter: StatusProviderContract.Filter): JSONObject? {
try {
return JSONObject().apply {
toJSON(statusFilter, this)
if (statusFilter is AppStatusFilter) {
put(JSON_PKG, statusFilter.pkg)
}
}
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter)
return null
}
}
}

override fun getLogTag() = LOG_TAG

override fun copyWith(providedEncryptKeysMap: Secret<Map<String, String>>?) = this.copy(providedEncryptKeysMap = providedEncryptKeysMap)

override fun fromJSONStringStatic(jsonString: String?) = fromJSONString(jsonString)

override fun toJSONString() = toJSONString(this)

override fun toJSONStringStatic(statusFilter: StatusProviderContract.Filter) = toJSONString(statusFilter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -608,72 +608,5 @@ public String toString() {
", statusMsgId=" + statusMsgId +
'}';
}

public static class AvailabilityPercentStatusFilter extends StatusProviderContract.Filter {

private static final String LOG_TAG = AvailabilityPercentStatusFilter.class.getSimpleName();

@NonNull
@Override
public String getLogTag() {
return LOG_TAG;
}

public AvailabilityPercentStatusFilter(@NonNull String targetUUID) {
super(POI.ITEM_STATUS_TYPE_AVAILABILITY_PERCENT, targetUUID);
}

@Nullable
@Override
public StatusProviderContract.Filter fromJSONStringStatic(@Nullable String jsonString) {
return fromJSONString(jsonString);
}

@Nullable
public static StatusProviderContract.Filter fromJSONString(@Nullable String jsonString) {
try {
return jsonString == null ? null : fromJSON(new JSONObject(jsonString));
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '%s'", jsonString);
return null;
}
}

@Nullable
public static StatusProviderContract.Filter fromJSON(@NonNull JSONObject json) {
try {
String targetUUID = StatusProviderContract.Filter.getTargetUUIDFromJSON(json);
AvailabilityPercentStatusFilter availabilityPercentStatusFilter = new AvailabilityPercentStatusFilter(targetUUID);
StatusProviderContract.Filter.fromJSON(availabilityPercentStatusFilter, json);
return availabilityPercentStatusFilter;
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '%s'", json);
return null;
}
}

@Nullable
@Override
public String toJSONStringStatic(@NonNull StatusProviderContract.Filter statusFilter) {
return toJSONString(statusFilter);
}

@Nullable
private static String toJSONString(@NonNull StatusProviderContract.Filter statusFilter) {
JSONObject json = toJSON(statusFilter);
return json == null ? null : json.toString();
}

@Nullable
private static JSONObject toJSON(@NonNull StatusProviderContract.Filter statusFilter) {
try {
JSONObject json = new JSONObject();
StatusProviderContract.Filter.toJSON(statusFilter, json);
return json;
} catch (JSONException jsone) {
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '%s'", statusFilter);
return null;
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.mtransit.android.commons.data

import org.json.JSONException
import org.json.JSONObject
import org.mtransit.android.commons.MTLog
import org.mtransit.android.commons.provider.status.StatusProviderContract
import org.mtransit.commons.model.Secret

data class AvailabilityPercentStatusFilter(
override val cacheOnly: Boolean? = null,
override val cacheValidityInMs: Long? = null,
override val inFocus: Boolean? = null,
override val providedEncryptKeysMap: Secret<Map<String, String>>? = null,
override val targetUUID: String,
) : StatusProviderContract.Filter(POI.ITEM_STATUS_TYPE_AVAILABILITY_PERCENT, targetUUID) {

companion object {
private val LOG_TAG: String = AvailabilityPercentStatusFilter::class.java.getSimpleName()

@JvmStatic
fun from(poi: POI) = from(poi.uuid)

@JvmStatic
fun from(targetUUID: String) = AvailabilityPercentStatusFilter(
targetUUID = targetUUID,
)

@JvmStatic
fun fromJSONString(jsonString: String?): StatusProviderContract.Filter? {
try {
return jsonString?.let { fromJSON(JSONObject(it)) }
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON string '$jsonString'")
return null
}
}

fun fromJSON(json: JSONObject): StatusProviderContract.Filter? {
try {
return AvailabilityPercentStatusFilter(
cacheOnly = getCacheOnlyFromJSON(json),
cacheValidityInMs = getCacheValidityInMsFromJSON(json),
inFocus = getInFocusFromJSON(json),
providedEncryptKeysMap = getProvidedEncryptKeysMapFromJSON(json),
targetUUID = getTargetUUIDFromJSON(json),
)
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while parsing JSON object '$json'")
return null
}
}

private fun toJSONString(statusFilter: StatusProviderContract.Filter) = toJSON(statusFilter)?.toString()

private fun toJSON(statusFilter: StatusProviderContract.Filter): JSONObject? {
try {
return JSONObject().apply {
toJSON(statusFilter, this)
}
} catch (jsone: JSONException) {
MTLog.w(LOG_TAG, jsone, "Error while making JSON object '$statusFilter'")
return null
}
}
}

override fun getLogTag() = LOG_TAG

override fun copyWith(providedEncryptKeysMap: Secret<Map<String, String>>?) = this.copy(providedEncryptKeysMap = providedEncryptKeysMap)

override fun fromJSONStringStatic(jsonString: String?) = fromJSONString(jsonString)

override fun toJSONString() = toJSONString(this)

override fun toJSONStringStatic(statusFilter: StatusProviderContract.Filter) = toJSONString(statusFilter)
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static POIStatus fromCursor(@NonNull Cursor cursor) {

@NonNull
public Cursor toCursor() {
MatrixCursor cursor = new MatrixCursor(StatusProviderContract.PROJECTION_STATUS);
final MatrixCursor cursor = new MatrixCursor(StatusProviderContract.getPROJECTION_STATUS());
cursor.addRow(new Object[]{
this.id,
this.type,
Expand Down
Loading