From b0d1edbd67217fa6bdc10f0323b4739ff5beec4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Tue, 10 Sep 2024 12:36:51 +0200 Subject: [PATCH] enhance resiliency of provider json parsing update version to 2.5.6 --- app/build.gradle | 4 +- .../ca/pkay/rcloneexplorer/rclone/Provider.kt | 6 +- .../rcloneexplorer/rclone/ProviderOption.kt | 61 +++++++++++-------- 3 files changed, 40 insertions(+), 31 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 5898ae5c..2361926a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,8 +17,8 @@ android { minSdkVersion 23 compileSdk 34 targetSdkVersion 34 - versionCode 400 // last digit is reserved for ABI, only ever end on 0! - versionName '2.5.5' + versionCode 410 // last digit is reserved for ABI, only ever end on 0! + versionName '2.5.6' testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" buildConfigField "java.lang.String", "CLI", System.getenv('RCX_BUILD_CLI') ? System.getenv('RCX_BUILD_CLI') : "\"c03129b6-b09f-9cb4-8fcd-7f143b8f94ef\"" buildConfigField "java.lang.String", "VCP_AUTHORITY", "\"" + applicationId + ".vcp\""; diff --git a/app/src/main/java/ca/pkay/rcloneexplorer/rclone/Provider.kt b/app/src/main/java/ca/pkay/rcloneexplorer/rclone/Provider.kt index 7e970437..365b3885 100644 --- a/app/src/main/java/ca/pkay/rcloneexplorer/rclone/Provider.kt +++ b/app/src/main/java/ca/pkay/rcloneexplorer/rclone/Provider.kt @@ -17,10 +17,10 @@ class Provider(val name: String) { item.prefix = data.optString("Prefix") item.commandHelp = data.optString("CommandHelp") - val options = data.getJSONArray("Options") + val options = data.optJSONArray("Options") - for (i in 0 until options.length()) { - item.options.add(ProviderOption.newInstance(options.getJSONObject(i))) + for (i in 0 until (options?.length() ?: 0)) { + ProviderOption.newInstance(options.getJSONObject(i))?.let { item.options.add(it) } } return item diff --git a/app/src/main/java/ca/pkay/rcloneexplorer/rclone/ProviderOption.kt b/app/src/main/java/ca/pkay/rcloneexplorer/rclone/ProviderOption.kt index 2e05ba9d..794575f3 100644 --- a/app/src/main/java/ca/pkay/rcloneexplorer/rclone/ProviderOption.kt +++ b/app/src/main/java/ca/pkay/rcloneexplorer/rclone/ProviderOption.kt @@ -1,5 +1,7 @@ package ca.pkay.rcloneexplorer.rclone +import android.util.Log +import de.felixnuesse.extract.extensions.tag import org.json.JSONObject import java.util.Objects @@ -23,37 +25,44 @@ class ProviderOption { var type: String = "" companion object { - fun newInstance(data: JSONObject): ProviderOption { - val item = ProviderOption() + fun newInstance(data: JSONObject): ProviderOption? { - item.name = data.getString("Name") - item.help = data.getString("Help") - item.provider = data.getString("Type") - item.default = data.getString("Default") - //item.value = data.get("Value") - item.shortOpt = data.getString("ShortOpt") - item.hide = data.getInt("Hide") - item.required = data.getBoolean("Required") - item.isPassword = data.getBoolean("IsPassword") - item.noPrefix = data.getBoolean("NoPrefix") - item.advanced = data.getBoolean("Advanced") - item.exclusive = data.getBoolean("Exclusive") - item.defaultStr = data.getString("DefaultStr") - item.valueStr = data.getString("ValueStr") - item.type = data.getString("Type") + try { + val item = ProviderOption() - val examples = data.optJSONArray("Examples") - if (examples != null) { - for (i in 0 until examples.length()) { - item.examples.add(OptionExampleItem( - examples.getJSONObject(i).getString("Value"), - examples.getJSONObject(i).getString("Help"), - examples.getJSONObject(i).getString("Provider") - )) + item.name = data.optString("Name") + item.help = data.optString("Help") + item.provider = data.optString("Type") + item.default = data.optString("Default") + //item.value = data.get("Value") + item.shortOpt = data.optString("ShortOpt") + item.hide = data.optInt("Hide") + item.required = data.optBoolean("Required") + item.isPassword = data.optBoolean("IsPassword") + item.noPrefix = data.optBoolean("NoPrefix") + item.advanced = data.optBoolean("Advanced") + item.exclusive = data.optBoolean("Exclusive") + item.defaultStr = data.optString("DefaultStr") + item.valueStr = data.optString("ValueStr") + item.type = data.optString("Type") + + val examples = data.optJSONArray("Examples") + if (examples != null) { + for (i in 0 until examples.length()) { + item.examples.add(OptionExampleItem( + examples.getJSONObject(i).optString("Value"), + examples.getJSONObject(i).optString("Help"), + examples.getJSONObject(i).optString("Provider") + )) + } } + + return item + } catch (e: Exception) { + Log.e(tag(), data.toString(4)) } - return item + return null } }