Java Infinite LiveCode

Moderators: LCMark, LCfraser

Java Infinite LiveCode

Postby livecodeali » Wed Mar 29, 2017 3:43 pm

Paul McClernan has had trouble getting the examples in my blog (https://livecode.com/livecode-9-0-dp-6-infinite-livecode-preview/#comment-36573) to work, so here is a thread for ironing out any issues.

Firstly, here is a single library that should add the GetBatteryPercentage function to the message path, so it is usable on Android:

Code: Select all
library com.livecode.library.androidbattery

use com.livecode.foreign
use com.livecode.java

metadata title is "Android Battery Library"
metadata author is "LiveCode"
metadata version is "1.0.0"

foreign handler ClassForName(in pClass as JString) returns JObject binds to "java:java.lang.Class>forName(Ljava/lang/String;)Ljava/lang/Class;!static"
foreign handler GetCurrentApplicationMethod(in pActivityThread as JObject, in pMethod as JString, in pParams as optional JObject) returns JObject binds to "java:java.lang.Class>getMethod(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"
foreign handler MethodInvoke(in pMethod as JObject, in pInstance as optional JObject, in pParams as optional JObject) returns JObject binds to "java:java.lang.reflect.Method>invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"

foreign handler GetIntentFilter(in pAction as JString) returns JObject binds to "java:android.content.IntentFilter>new(Ljava/lang/String;)"
foreign handler ApplicationRegisterReceiver(in pContext as JObject, in pObj as optional JObject, in pFilter as JObject) returns JObject binds to "java:android.content.Context>registerReceiver(Landroid/content/BroadcastReceiver;Landroid/content/IntentFilter;)Landroid/content/Intent;"

foreign handler GetIntentValueInt(in pIntent as JObject, in pWhich as JString, in pDefault as CInt) returns CInt binds to "java:android.content.Intent>getIntExtra(Ljava/lang/String;I)I"

public handler GetBatteryPercentage() returns Number
    unsafe
        variable tClass as JObject
        put ClassForName(StringToJString("android.app.ActivityThread")) into tClass

        variable tMethod as JObject
        put GetCurrentApplicationMethod(tClass, StringToJString("currentApplication"), nothing) into tMethod
               
         variable tContext as JObject
         put MethodInvoke(tMethod, nothing, nothing) into tContext
                 
         variable tFilter as JObject
         put GetIntentFilter(StringToJString("android.intent.action.BATTERY_CHANGED")) into tFilter

         variable tIntent as JObject
         put ApplicationRegisterReceiver(tContext, nothing, tFilter) into tIntent
                 
        variable tLevel as Integer
        put GetIntentValueInt(tIntent, StringToJString("level"), -1) into tLevel

        variable tScale as Integer
        put GetIntentValueInt(tIntent, StringToJString("scale"), -1) into tScale

        return tLevel / tScale * 100   
    end unsafe
end handler

end library
livecodeali
Livecode Staff Member
Livecode Staff Member
 
Posts: 117
Joined: Thu Apr 18, 2013 2:48 pm

Re: Java Infinite LiveCode

Postby livecodeali » Wed Mar 29, 2017 3:49 pm

Regarding the error using the Java BigInteger class, the only reason I can think of why it wouldn't work would be that the value of JAVA_HOME was not set correctly (note the blurb about launching LC from the command line to make it work at the moment).

We have scheduled an item to be done asap which is to make the errors from LCB more helpful in the IDE, but in the meantime, try putting it in the script of a button:

Code: Select all
on mouseUp
   try
      AddBigIntegers("12345","12345")
   catch tError
      put tError
   end try
end mouseUp


this will hopefully make the error clearer
livecodeali
Livecode Staff Member
Livecode Staff Member
 
Posts: 117
Joined: Thu Apr 18, 2013 2:48 pm

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Wed Mar 29, 2017 4:55 pm

I didn't realize that setting $JAVA_HOME is not persistent across restarts.

So I just remembered the example you gave in the comments wouldn't compile until I changed
Code: Select all
put BigInteger_add(tLeftBigInt, tRightBigInt) into tSumBigInt

to
Code: Select all
put _JNI_BigInteger_add(tLeftBigInt, tRightBigInt) into tSumBigInt

Now I'm getting this as a result of button with the try / catch :
885,3,1
896,3,1
573,3,1,AddBigIntegers

I really need to install an editor with line numbers (new laptop).
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Wed Mar 29, 2017 6:18 pm

As for the Android code, I removed my previous build and I pasted your code into a new lcb file, recompiled the lib extension, rebuilt my apk, and reinstalled.

GetBatteryPercentage() still returns nothing on my phone.
My test app has a button and a field:

Code: Select all
on mouseUp
  get GetBatteryPercentage()
  put it into tBatPercent
  put tBatPercent into field 1
  Answer "Percentage: " & tBatPercent
end mouseUp
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Wed Mar 29, 2017 6:45 pm

Did the whole thing over on my work computer and changed the test app code to this:
Code: Select all
on mouseUp
   try
      get GetBatteryPercentage()
   catch tError
      put tError into fld "Output"
   end try
   put GetBatteryPercentage() after fld "Output"
   answer "hello" & GetBatteryPercentage()
end mouseUp

Pushing the button on my phone the field Output field contains two lines:
Code: Select all
219,3,11,GetBatteryPercentage
223,3,11
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Wed Mar 29, 2017 7:38 pm

Just to be sure it's not some unique problem with my setup, I made new basic app with one (custom LCB made) widget and a hello world button which built and worked fine on my phone.
I was testing on Mac OS X 10.10.5 (laptop) Mac OS X 10.9.5 (workstation)
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm

Re: Java Infinite LiveCode

Postby livecodeali » Fri Mar 31, 2017 7:59 am

Thanks for testing - I'll try and work out what's going on! They both work for me here, although I don't have a device at hand to test right now, just simulator.
livecodeali
Livecode Staff Member
Livecode Staff Member
 
Posts: 117
Joined: Thu Apr 18, 2013 2:48 pm

Re: Java Infinite LiveCode

Postby livecodeali » Fri Mar 31, 2017 8:07 am

OK,
Code: Select all
219,3,11,GetBatteryPercentage
223,3,11


means the function was not found, i.e. the library is not loaded in the standalone. You could confirm this by checking the loadedExtensions in the standalone. I suspect there is just a problem with including the module. Note it will not be included automatically, the library needs to be installed and manually selected in the inclusions pane.
livecodeali
Livecode Staff Member
Livecode Staff Member
 
Posts: 117
Joined: Thu Apr 18, 2013 2:48 pm

Re: Java Infinite LiveCode

Postby livecodeali » Fri Mar 31, 2017 8:14 am

It seems that

Code: Select all
573,3,1,AddBigIntegers


also happens when the library is not loaded. The other numbers are slightly bizarre, in particular error 896 doesn't exist...

(ps you can make sense of the error code using
Code: Select all
put line 573 of the scriptexecutionerrors

)
livecodeali
Livecode Staff Member
Livecode Staff Member
 
Posts: 117
Joined: Thu Apr 18, 2013 2:48 pm

Re: Java Infinite LiveCode

Postby LCMark » Fri Mar 31, 2017 8:18 am

Hmmmm the following error:
Now I'm getting this as a result of button with the try / catch :
885,3,1
896,3,1
573,3,1,AddBigIntegers


Is TypeError (one of the arguments is of the wrong type) and TooFewArguments it appears - which is a bit odd as I'm sure it should either be one or the other based on the current implementation.

The error:
573,3,1,AddBigIntegers

Means that the handler isn't found.
LCMark
Livecode Staff Member
Livecode Staff Member
 
Posts: 933
Joined: Thu Apr 11, 2013 11:27 am

Re: Java Infinite LiveCode

Postby LCMark » Fri Mar 31, 2017 8:33 am

So - looking into this - the error is TooFewArguments (the TypeError is an error in the engine code which is throwing the error when it shouldn't).

The problem with the Big integer example above is that it calling AddBigINtegers like a command, which means ("12345", "12345") is treated as a single argument:

Code: Select all
on mouseUp
  try
    get AddBigIntegers("12345", "12345") -- function form
    AddBigIntegers "12345", "12345" -- command form
  catch tError
    ...
  end try
end mouseUp
LCMark
Livecode Staff Member
Livecode Staff Member
 
Posts: 933
Joined: Thu Apr 11, 2013 11:27 am

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Thu Apr 20, 2017 1:55 pm

Forgot to follow up on this...
So the Java BigInt code works on Mac, but only as long as I remember to launch LC IDE from the terminal AND set the variable $JAVA_HOME to the output of shell("/usr/libexec/java_home"). These two things must be done EVERY TIME you want to use the Java library, if you don't do both before calling the function it won't work! This is kind of a PITA so I might make an AppleScript or something to automate it.
Last edited by PaulDaMacMan on Thu Apr 20, 2017 3:13 pm, edited 2 times in total.
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm

Re: Java Infinite LiveCode

Postby PaulDaMacMan » Thu Apr 20, 2017 3:08 pm

Also for anyone else having problems, after changing the standalone General tab's inclusion setting (radio button) to "Select inclusions..." and then manually selecting these FFI libraries in the inclusions tab, both the battery level and java bigint functions worked as expected in my Android standalone (on my phone running Android 7.0).
PaulDaMacMan
 
Posts: 120
Joined: Wed Apr 24, 2013 4:53 pm


Return to LiveCode Builder

Who is online

Users browsing this forum: No registered users and 2 guests