Skip to content

Commit

Permalink
[MYR-8718]: Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Pohrer committed Oct 18, 2023
1 parent 9f6b7d5 commit 03f5dab
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 60 deletions.
20 changes: 10 additions & 10 deletions src/main/java/ag/boersego/bgjs/JNIV8Object.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,19 @@ public boolean hasV8OwnField(@NonNull String name) {
public static double asNumber(Object obj) {
if(obj == null) {
return 0;
} else if(obj instanceof JNIV8Object) {
return ((JNIV8Object) obj).toNumber();
} else if(obj instanceof Number) {
} else if(obj instanceof JNIV8Object object) {
return object.toNumber();
} else if(obj instanceof Number number) {
// handles Byte, Short, Integer, Long, Float, Double
return ((Number)obj).doubleValue();
} else if(obj instanceof String) {
return Double.valueOf((String)obj);
} else if(obj instanceof Boolean) {
return ((Boolean) obj ? 1 : 0);
return number.doubleValue();
} else if(obj instanceof String string) {
return Double.valueOf(string);
} else if(obj instanceof Boolean bool) {
return (bool ? 1 : 0);
} else if(obj instanceof JNIV8Undefined) {
return Double.NaN;
} else if(obj instanceof Character) {
return Character.getNumericValue((Character) obj);
} else if(obj instanceof Character character) {
return Character.getNumericValue(character);
}
// other java-only types are "false"
throw new ClassCastException("Cannot convert to number: " + obj);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ag/boersego/bgjs/V8Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ protected void onReady() {
*/
protected void onSuspend() {
for (final JNIV8Module module : mModules) {
if (module instanceof JNIV8Module.IJNIV8Suspendable) {
((JNIV8Module.IJNIV8Suspendable) module).onSuspend();
if (module instanceof JNIV8Module.IJNIV8Suspendable suspendable) {
suspendable.onSuspend();
}
}
}
Expand All @@ -226,8 +226,8 @@ protected void onSuspend() {
*/
protected void onResume() {
for (final JNIV8Module module : mModules) {
if (module instanceof JNIV8Module.IJNIV8Suspendable) {
((JNIV8Module.IJNIV8Suspendable) module).onResume();
if (module instanceof JNIV8Module.IJNIV8Suspendable suspendable) {
suspendable.onResume();
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/ag/boersego/bgjs/V8TextureView.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ public void setClearColor(final int color) {
mClearAlpha = ((c & 0xff000000L) >> 24) / 256f;
mClearRed = ((c & 0x00ff0000L) >> 16) / 256f;
mClearGreen = ((c & 0x0000ff00L) >> 8) / 256f;
mClearBlue = ((c & 0x000000ffL)) / 256f;
mClearBlue = (c & 0x000000ffL) / 256f;
mClearColorSet = true;
} catch (final Exception e) {
Log.i(TAG, "Cannot set clear color from background color", e);
Expand Down Expand Up @@ -777,6 +777,7 @@ public void run() {
try {
finishGL();
} catch (final Exception ignored) {
//no action intended
}
return;
}
Expand Down Expand Up @@ -896,6 +897,7 @@ public void run() {
try {
Thread.sleep(16 - (renderDone - startRender));
} catch (final InterruptedException ignored) {
//no action intended
}
}
}
Expand Down
8 changes: 0 additions & 8 deletions src/main/java/ag/boersego/bgjs/V8Value.java

This file was deleted.

15 changes: 8 additions & 7 deletions src/main/java/ag/boersego/bgjs/data/AjaxRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public Headers getResponseHeaders() {

/**
* This traffic-counting code is also in Java-WebSocket.
* TODO: Share code.
*/
public static class AjaxTrafficCounter {

Expand All @@ -81,7 +80,8 @@ public static long getOutTraffic() {
}

public static int getInTrafficPerMinute() {
int numFieldsFound = 0, sum = 0;
int numFieldsFound = 0;
int sum = 0;
for (int i = 0; i < 6; i++) {
if (trafficInPerMinute[i] > -1) {
sum += trafficInPerMinute[i];
Expand Down Expand Up @@ -262,6 +262,7 @@ public void run() {
try {
Log.w(TAG, "no referer set " + mCaller.getClass().getCanonicalName());
} catch (final Exception ignored) {
//no action intended
}
}
}
Expand All @@ -280,25 +281,25 @@ public void run() {
if (mFileName != null) {
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("filename", mFileName, RequestBody.create(MediaType.parse(mOutputType), mData))
.addFormDataPart("filename", mFileName, RequestBody.create(mData, MediaType.parse(mOutputType)))
.build();

requestBuilder.post(requestBody);

} else {
requestBuilder.post(RequestBody.create(MediaType.parse(mOutputType), mData));
requestBuilder.post(RequestBody.create(mData, MediaType.parse(mOutputType)));
}

} else {
if (mMethod.equals("POST")) {
requestBuilder.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), mData));
requestBuilder.post(RequestBody.create(mData, MediaType.parse("application/x-www-form-urlencoded")));
}
}
} else if (mMethod != null) {
if (mMethod.equals("DELETE")) {
requestBuilder.delete();
} else if (mMethod.equals("POST")) {
requestBuilder.post(RequestBody.create(null, new byte[0]));
requestBuilder.post(RequestBody.create(new byte[0], null));
}
}
}
Expand Down Expand Up @@ -326,7 +327,6 @@ public void run() {
response = client.newCall(request).execute();

if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
final String responseStr;
int dataSizeGuess = (int) response.body().contentLength();
if (dataSizeGuess > 0) {
AjaxTrafficCounter.addTraffic(0, dataSizeGuess);
Expand All @@ -340,6 +340,7 @@ public void run() {
try {
mErrorData = response.body().string();
} catch (Exception ignored) {
//no action intended
}
mErrorCode = response.code();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ class BGJSModuleAjaxRequest(engine: V8Engine) : JNIV8Object(engine), Runnable {

} else {
var info: String? = null
var failDetails: HttpResponseDetails? = null
val failDetails: HttpResponseDetails?
if (mErrorThrowable != null) {
info = if (mErrorThrowable is SocketTimeoutException || mErrorThrowable is UnknownHostException) "timeout" else "error"
}
Expand All @@ -165,7 +165,6 @@ class BGJSModuleAjaxRequest(engine: V8Engine) : JNIV8Object(engine), Runnable {
if (DEBUG) {
Log.d(TAG, "ajax $method error response $mErrorCode for $url with type $contentType and body $mErrorData")
}
// Log.d(TAG, "Error code $mErrorCode, info $info, body $mErrorData")
val errorObj = JNIV8GenericObject.Create(v8Engine)
if (_responseIsJson && mErrorData != null) {
val parsedResponse: Any?
Expand Down
13 changes: 0 additions & 13 deletions src/main/java/ag/boersego/bgjs/modules/BGJSModuleFetch.kt
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,6 @@ class BGJSModuleFetch(private val okHttpClient: OkHttpClient) : JNIV8Module("fet
redirectRequest.parsedUrl = it
redirectRequest.counter++

// TODO: HTTP-redirect fetch step 9
if (httpResponse.code != 303 && request.body != null) {

}

// HTTP-redirect fetch step 11
if (httpResponse.code == 303 || ((httpResponse.code == 301 || httpResponse.code == 302) && request.method == "POST")) {
redirectRequest.method = "GET"
Expand Down Expand Up @@ -254,17 +249,9 @@ class BGJSModuleFetch(private val okHttpClient: OkHttpClient) : JNIV8Module("fet
}
}


//TODO: deflate
// handle the infamous raw deflate response from old servers
// a hack for old IIS and Apache servers
if (codings == "deflate" || codings == "x-deflate") {

}
resolver.resolve(fetchResponse)
}
})

}

companion object {
Expand Down
34 changes: 19 additions & 15 deletions src/main/java/ag/boersego/bgjs/modules/BGJSModuleLocalStorage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,28 @@ class BGJSModuleLocalStorage private constructor(applicationContext: Context) :
val exports = JNIV8GenericObject.Create(engine)

exports.setV8Accessor("length", JNIV8Function.Create(engine) { _, arguments ->
if (arguments.isNotEmpty()) {
throw IllegalArgumentException("clear needs no arguments")
require(arguments.isEmpty()) {
"length needs no arguments"
}

preferences.all.size
}, null)

exports.setV8Field("clear", JNIV8Function.Create(engine) { _, arguments ->
if (arguments.isNotEmpty()) {
throw IllegalArgumentException("clear needs no arguments")
require(arguments.isEmpty()) {
"clear needs no arguments"
}

preferences.edit().clear().apply()
insertOrderPref.edit().clear().apply()
JNIV8Undefined.GetInstance()
})

exports.setV8Field("getItem", JNIV8Function.Create(engine) { _, arguments ->
if (arguments.isEmpty() || arguments[0] !is String) {
throw IllegalArgumentException("getItem needs one parameter of type String")
require(arguments.isNotEmpty() && arguments[0] is String) {
"getItem needs one parameter of type String"
}

val key = arguments[0] as String

preferences.getString(key, null)
Expand All @@ -55,10 +58,11 @@ class BGJSModuleLocalStorage private constructor(applicationContext: Context) :
// (Thus, adding or removing a key may change the order of the keys, but merely changing the value of an existing key must not.)
// If n is greater than or equal to the number of key/value pairs in the object, then this method must return null.

exports.setV8Field("key", JNIV8Function.Create(engine) { receiver, arguments ->
if (arguments.isEmpty() || arguments[0] !is Double) {
throw IllegalArgumentException("getItem needs one parameter of type Number")
exports.setV8Field("key", JNIV8Function.Create(engine) { _, arguments ->
require(arguments.isNotEmpty() && arguments[0] is Double) {
"getItem needs one parameter of type Number"
}

val index = (arguments[0] as Double).toInt()
val orderedKeys = insertOrderPref.getString(INSERT_ORDER_KEY, null)?.split(",")
if (orderedKeys != null && index < orderedKeys.size) {
Expand All @@ -69,9 +73,10 @@ class BGJSModuleLocalStorage private constructor(applicationContext: Context) :
})

exports.setV8Field("removeItem", JNIV8Function.Create(engine) { _, arguments ->
if (arguments.isEmpty() || arguments[0] !is String) {
throw IllegalArgumentException("removeItem needs one parameter of type String")
require(arguments.isNotEmpty() && arguments[0] is String) {
"removeItem needs one parameter of type String"
}

val key = arguments[0] as String
preferences.edit().remove(key).apply()

Expand All @@ -83,9 +88,10 @@ class BGJSModuleLocalStorage private constructor(applicationContext: Context) :
})

exports.setV8Field("setItem", JNIV8Function.Create(engine) { _, arguments ->
if (arguments.size < 2 || arguments[0] !is String) {
throw IllegalArgumentException("setItem needs two parameters of type String")
require(arguments.size >= 2 && arguments[0] is String) {
"setItem needs two parameters of type String"
}

val key = arguments[0] as String
val value = when (arguments[1]) {
is JNIV8Undefined -> "undefined"
Expand Down Expand Up @@ -117,8 +123,6 @@ class BGJSModuleLocalStorage private constructor(applicationContext: Context) :
private const val INSERT_ORDER_KEY = "insert_order"
@Volatile private var instance: BGJSModuleLocalStorage? = null

const val PREF_KEY_AUTH_STATE = "auth:state"

@JvmStatic
fun getInstance(ctx : Context) : BGJSModuleLocalStorage {
val i = instance
Expand Down

0 comments on commit 03f5dab

Please sign in to comment.