mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix(android): coerce string width/height in ImageAssetOptions (#10862)
This commit is contained in:
committed by
GitHub
parent
d856826b7a
commit
9db6369aed
@ -170,6 +170,36 @@ public class Utils {
|
||||
boolean autoScaleFactor;
|
||||
}
|
||||
|
||||
private static int parsePositiveInt(JSONObject object, String key) {
|
||||
if (object == null || key == null) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
if (!object.has(key) || object.isNull(key)) {
|
||||
return 0;
|
||||
}
|
||||
Object value = object.get(key);
|
||||
if (value instanceof Number) {
|
||||
int parsed = (int) Math.floor(((Number) value).doubleValue());
|
||||
return parsed > 0 ? parsed : 0;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
String s = ((String) value).trim();
|
||||
if (s.length() == 0) {
|
||||
return 0;
|
||||
}
|
||||
try {
|
||||
int parsed = Integer.parseInt(s);
|
||||
return parsed > 0 ? parsed : 0;
|
||||
} catch (NumberFormatException ignored) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
} catch (JSONException ignored) {
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static final Executor executors = Executors.newCachedThreadPool();
|
||||
|
||||
|
||||
@ -297,8 +327,9 @@ public class Utils {
|
||||
|
||||
try {
|
||||
JSONObject object = new JSONObject(options);
|
||||
opts.width = object.optInt("width", 0);
|
||||
opts.height = object.optInt("height", 0);
|
||||
// Coerce numeric strings or numbers; fallback to 0 for invalid values
|
||||
opts.width = parsePositiveInt(object, "width");
|
||||
opts.height = parsePositiveInt(object, "height");
|
||||
opts.keepAspectRatio = object.optBoolean("keepAspectRatio", true);
|
||||
opts.autoScaleFactor = object.optBoolean("autoScaleFactor", true);
|
||||
} catch (JSONException ignored) {
|
||||
|
||||
Reference in New Issue
Block a user