@craft-native/android 0.0.169 → 0.0.171
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -109,7 +109,7 @@ internal class CraftNativeServices(
|
|
|
109
109
|
"Speech" -> speech(method, args, done)
|
|
110
110
|
"Location" -> location(method, args, done)
|
|
111
111
|
"Media" -> when (method) {
|
|
112
|
-
"keep" -> CraftNativeMedia.keep(context, args.optString(0), args.optJSONObject(1)?.optJSONObject("headers") ?: JSONObject(), done)
|
|
112
|
+
"keep" -> CraftNativeMedia.keep(context, args.optString(0), args.optJSONObject(1)?.optJSONObject("headers") ?: JSONObject(), args.optJSONObject(1)?.optBoolean("cellular", true) ?: true, done)
|
|
113
113
|
"path" -> done(success(CraftNativeMedia.local(context, args.optString(0))?.absolutePath ?: JSONObject.NULL))
|
|
114
114
|
"prune" -> {
|
|
115
115
|
val keep = args.optJSONArray(0) ?: JSONArray()
|
|
@@ -808,12 +808,21 @@ internal object CraftNativeMedia {
|
|
|
808
808
|
|
|
809
809
|
private fun describe(address: String, file: File) = JSONObject().put("ok", true).put("url", address).put("path", file.absolutePath).put("size", file.length())
|
|
810
810
|
|
|
811
|
-
|
|
811
|
+
/**
|
|
812
|
+
* `cellular = false` keeps it off a metered connection (mobile data, a
|
|
813
|
+
* hotspot): nothing is fetched and the answer is `{ ok: false, reason:
|
|
814
|
+
* "cellular" }`, so the screen can say it is waiting for Wi-Fi. A copy
|
|
815
|
+
* already kept still answers, whatever the connection.
|
|
816
|
+
*/
|
|
817
|
+
fun keep(context: Context, address: String, headers: JSONObject, cellular: Boolean, done: (CraftNativeCapabilityResult) -> Unit) {
|
|
812
818
|
val uri = runCatching { Uri.parse(address) }.getOrNull()
|
|
813
819
|
if (uri == null || uri.scheme?.lowercase() !in setOf("http", "https") || uri.host.isNullOrBlank()) {
|
|
814
820
|
return done(CraftNativeCapabilityResult(code = "INVALID_ARGUMENT", message = "media.keep needs an absolute http or https URL"))
|
|
815
821
|
}
|
|
816
822
|
local(context, address)?.let { return done(CraftNativeCapabilityResult(data = describe(address, it))) }
|
|
823
|
+
if (!cellular && metered(context)) {
|
|
824
|
+
return done(CraftNativeCapabilityResult(data = JSONObject().put("ok", false).put("url", address).put("reason", "cellular")))
|
|
825
|
+
}
|
|
817
826
|
synchronized(inFlight) {
|
|
818
827
|
inFlight[address]?.let { waiting ->
|
|
819
828
|
waiting.add(done)
|
|
@@ -851,6 +860,12 @@ internal object CraftNativeMedia {
|
|
|
851
860
|
}
|
|
852
861
|
}
|
|
853
862
|
|
|
863
|
+
/** Whether the connection now is one the person pays for by the byte. */
|
|
864
|
+
private fun metered(context: Context): Boolean = runCatching {
|
|
865
|
+
val connectivity = context.applicationContext.getSystemService(Context.CONNECTIVITY_SERVICE) as android.net.ConnectivityManager
|
|
866
|
+
connectivity.isActiveNetworkMetered
|
|
867
|
+
}.getOrDefault(false)
|
|
868
|
+
|
|
854
869
|
/** Removes every copy whose address is not in `keep`; answers how many. */
|
|
855
870
|
fun prune(context: Context, keep: List<String>): Int {
|
|
856
871
|
val wanted = keep.map { file(context, it).name }.toSet()
|