How to communicate with java in Android (working in v11.171-beta).

Previous topic - Next topic

spacefractal

I finally found a workaround from the bug that prevent me to communicate with java directly in this version. Its a bit more advanced, but when its works, then its would been much easier. This method will not break the current apps.

Compiler\platform\android\bin\sdl_main.cpp:
put below lines in the extern "C" area (can been inserted below char g_androidExtStoragePubDir[512] = "~";)
Code (glbasic) Select

char g_androidJAVACALL[512] = "~";
jclass cls;
jmethodID mid;

// * COSTUME JAVACALL *
const char* android_JAVACALL(const char* url)
{

if (cls==NULL)
{ cls = gJNIenv->FindClass("org/libsdl/app/SDLActivity");
mid = gJNIenv->GetStaticMethodID(cls,
   "glb_JAVACALL",
   "(Ljava/lang/String;)Ljava/lang/String;"); // (params;..)return_type
}

   if (mid==NULL)
{ return g_androidJAVACALL;
}

// there could be some exception handling happening here, but there isn't
jstring mystr = gJNIenv->NewStringUTF(url);
jobject retJava = gJNIenv->CallStaticObjectMethod(cls, mid, mystr);
if (retJava==NULL)
{ gJNIenv->DeleteLocalRef(mystr);
gJNIenv->DeleteLocalRef(retJava);
return g_androidJAVACALL;
}
jstring res = (jstring)retJava;
strncpy(g_androidJAVACALL, gJNIenv->GetStringUTFChars(res,  0L), 512);

gJNIenv->DeleteLocalRef(mystr);
gJNIenv->DeleteLocalRef(res);
gJNIenv->DeleteLocalRef(retJava);
res=NULL;
return g_androidJAVACALL;
}


EDIT: Please note, Fivesprite have wrote a more protection one of above code, which can been used, when the above works, its here:
http://www.glbasic.com/forum/index.php?topic=8621.msg77670#msg77670

templateproj\src\com\glbasic\test\SDLActivity.java:
can example inserted before glb_open_url() or around there
Code (glbasic) Select

    // Java functions called from C
public static String glb_JAVACALL(String url)
{ Log.i("glbasic", "calltest");
return "works";
}


test above code in glbasic with:
Code (glbasic) Select

STDOUT "IMPORT"
?IFDEF ANDROID
IMPORT "C" const char* android_JAVACALL(const char* string)
?ENDIF

LOCAL test$=android_JAVACALL("test")
STDOUT "test: "+test$


If the above works corretly, you should get a "works" string in STDOUT, that came from java code call.

With that in mind you can do fun thing with example doing attest loading your self while loading or even better, using Google License Service. When that code have been validated then I post how to use that service.

in java there is a string split command, so you could collect all required calls into that method.

etc String[] tokens = url.split(":");

EDIT:
I got succesful ported Greedy Mouse with this Java communication system over. So its works very well and its dont impact combatible at all. Later in this thread I doing how doing some stuff with that,
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Falstaff

This looks cool, thanks for sharing!

Does this mean you can call native Android SDK stuff? Would it be possible this could be used to create libraries for say, in app-purchase, or advertisements?

mentalthink

Hi Thanks for this awesome Work... I don´t have too much skills about Android, but this can be very interesting for calling internal things in Android... perhaps the trouble can be when they change the Android Version....

Thanks a lot...!!!
:nw: :nw:

spacefractal

Yes, but there is some activity issues left. Etc glbasic close its thread completely when I'm trying create a another activity (example a WebKit), Which look its does on purpuse. Hope we can get those fixed first. When fixed then we can use ads services native or use Dropbox.

Howover you can call any java libs, example Google Licence Service works very great (much better than internal drm on google play, and you can use own assets copy so it's don't hang in startup and eventuelly crash out. I later put them here.

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

bigsofty

This is one of those "I have no use for this yet", with "yet" being the operative word. I am sure this will be VERY useful to me at a later date, thank you!  :good:
Cheers,

Ian.

"It is practically impossible to teach good programming style to students that have had prior exposure to BASIC.  As potential programmers, they are mentally mutilated beyond hope of regeneration."
(E. W. Dijkstra)

spacefractal

oh dear, spelling in the subject, fixed.

Yes this is not needed early in the development, but its handly when the project is near finished. I did need that due I did not like the default assest installing (which was the first reason to do that).
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

spacefractal

Here is some java calls I use

Code (glbasic) Select

public static String glb_JAVACALL(String url)
{ //Log.i("glbasic", "calltest");

String[] tokens = url.split(":");

// copy the sigle assets
if (tokens[0].equals("copy"))
{ tokens[1]="Media/"+tokens[1];
int ok=mSingleton.copyAsset(tokens[1]);
return "";
}

// RecursiveCopy
if (tokens[0].equals("dircopy"))
{ tokens[1]="Media/"+tokens[1];
mSingleton.recursiveCopy(tokens[1], 0);
}

// get device info
if (tokens[0].equals("getdevice"))
{ String AndroidInfo="OSVersion="+System.getProperty("os.version")+"|";
AndroidInfo=AndroidInfo+"OSDevice="+android.os.Build.DEVICE+"|";
AndroidInfo=AndroidInfo+"OSModel="+android.os.Build.MODEL+"|";
AndroidInfo=AndroidInfo+"OSProduct="+android.os.Build.PRODUCT+"|";
AndroidInfo=AndroidInfo+"OSManufacturer="+android.os.Build.MANUFACTURER+"|";
AndroidInfo=AndroidInfo+"DisplayMetrics="+mSingleton.getResources().getDisplayMetrics().density+"|";
return AndroidInfo;
}

if (tokens[0].equals("ScreenOrientation"))
{ Display display = ((WindowManager) mSingleton.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
screenOrientation = display.getOrientation();
return Integer.toString(screenOrientation);
}

// check file size
if (tokens[0].equals("size"))
{ int size=-1;
tokens[1]="Media/"+tokens[1];
try {
Log.i("glbasic", tokens[1].concat(" is a file, reading..."));
InputStream is = mSingleton.getAssets().open(tokens[1]);
size=-2;
size = is.available();
is.close();
}
catch (IOException e)
{ e.printStackTrace(System.out);
}
return Integer.toString(size);
}

// a working test function.
if (tokens[0].equals("test"))
{ return "This is Working String";
}
return "";
    }

// this check a file and copy a new over on the top, if no file exist or filesize have been changed (this is the asset copy I use)
public int copyAsset(String path)
{ int succes=0;
try {
InputStream in = getAssets().open(path);
FileOutputStream out = new FileOutputStream(getFilesDir().getAbsolutePath().concat(File.separator).concat(path));
int read;
byte[] buffer = new byte[4096];
while ((read = in.read(buffer)) > 0)
{ out.write(buffer, 0, read);
}
out.close();
in.close();
}
catch (IOException e)
{ succes=-1;
e.printStackTrace();
}
return succes;
}


EXAMPLE TO USE:

This fix orientation issue.

Code (glbasic) Select

FUNCTION CallJava$: Args$
LOCAL result$
IF device$<>"a" AND device$<>"ak" THEN RETURN
?IFDEF ANDROID
result$=android_JAVACALL(Args$)
RETURN result$
?ENDIF
ENDFUNCTION

LOCAL orentation=CallJava$("screenOrientation")
DEPRINT("GetOrientation: "+orentation)
SETORIENTATION orentation // that fix orientation issues.
GETSCREENSIZE ScreenWidth, ScreenHeight


A Late edit to reflect change to this post is more clean. Its dosent breaks the follow posts.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

MrTAToad


spacefractal

its did fixed most cases, but I guess you should look on the SETSCREEN bug.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Crivens

Nice. Could this be used to call up the Android keyboard?

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.

Crivens

QuoteNice. Could this be used to call up the Android keyboard?
Do you think this is possible? Would be great to get a native Android keyboard working.

Cheers 
Current fave quote: Cause you like musicians and I like people with boobs.

r0ber7

Quote from: Crivens on 2012-Nov-01
QuoteNice. Could this be used to call up the Android keyboard?
Do you think this is possible? Would be great to get a native Android keyboard working.

Cheers

Ooh ooh!

+1!

There's so much good stuff on these forums. I should make a seperate bookmark file or something.

spacefractal

should been possible, howover activity is a bit worry, but seen targetSDK max to 12 seen stabity is something much more. But have not looked into those things. I have used it to better assest loading, Getting Google License info, fixing orientation issue (XY cant been fixed, but Gernot should have fixed that in next beta) as well getting device info.

Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation - The beagle Jam - Cave Heroes 2023 - https://spacefractal.itch.io/

Kitty Hello

Yo spacefractal. Excellent function. I'll include this dummy in my jave template.

Crivens

Excellent. Would love to see an Android keyboard. Nice work!

Cheers
Current fave quote: Cause you like musicians and I like people with boobs.