Recent

Author Topic: Managed finally to determine android device orientation via JNI  (Read 5989 times)

stab

  • Full Member
  • ***
  • Posts: 237
Here is my code to determine orientation:

function TfrmJNITestMain.GetRotation : LongInt;
var
  windowManagerClass, displayClass : jclass;
  javaMethodId_WindowService, javaMethodId_getDefaultDisplay, javaMethodId_getRotation : JMethodID;
  windowManagerObject, displayObject: JObject;
  javaString_WINDOW_SERVICE, javaString_getDefaultDisplay : JString;
begin
  try
    // get windowManagerClass
    windowManagerClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/WindowManager');

    // get displayClass
    displayClass := javaEnvRef^^.FindClass(javaEnvRef, 'android/view/Display');

    // get the string Context.WINDOW_SERVICE remember that NewStringUTF does not require ReleaseStringUTFChars
    javaString_WINDOW_SERVICE := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getWindowManager'));

    // get method id for WindowManager object
    javaMethodId_WindowService := javaEnvRef^^.GetMethodID(javaEnvRef, javaAndroidAppActivityClass, 'getWindowManager', '()Landroid/view/WindowManager;');

    // Get the WindowManager object
    // Window w = (Window) getSystemService(Context.WINDOW_SERVICE);
    windowManagerObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, javaActivityObject, javaMethodId_WindowService);

    javaString_getDefaultDisplay := javaEnvRef^^.NewStringUTF(javaEnvRef, pchar('getDefaultDisplay'));

    // Get method id for Display object
    javaMethodId_getDefaultDisplay := javaEnvRef^^.GetMethodID(javaEnvRef, windowManagerClass, 'getDefaultDisplay', '()Landroid/view/Display;');

    // Get the display object
    displayObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, windowManagerObject, javaMethodId_getDefaultDisplay);

    // Get method id for method: getRotation
    javaMethodId_getRotation := javaEnvRef^^.GetMethodID(javaEnvRef, displayClass, 'getRotation', '()I');

    // Now call getRotation method in the display object
    Result := javaEnvRef^^.CallIntMethod(javaEnvRef, displayObject, javaMethodId_getRotation);
    debugln('rotation: ' + IntToStr(Result));
  except
    on E:Exception do
      debugln('Error:'+ E.Message);
  end;
end;


stab :D

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Managed finally to determine android device orientation via JNI
« Reply #1 on: April 12, 2013, 01:59:33 pm »
Ehm yes. Are you going to post a patch to the bugtracker with that code then ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

stab

  • Full Member
  • ***
  • Posts: 237
Re: Managed finally to determine android device orientation via JNI
« Reply #2 on: April 12, 2013, 02:05:35 pm »
Think I'll first wait for a comment from felipemdc.

I guess he'll be back tonight(that is swedish time)

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Managed finally to determine android device orientation via JNI
« Reply #3 on: April 12, 2013, 02:15:23 pm »
Good plan :)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Managed finally to determine android device orientation via JNI
« Reply #4 on: April 12, 2013, 02:18:54 pm »
nice, what was exactly the problem? I tried here but wasn't able to pin point it ... JNI can be really tricky =)

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Managed finally to determine android device orientation via JNI
« Reply #5 on: April 12, 2013, 02:21:12 pm »
The try..except is useless, by the way.

I tryed already to capture JNI exceptions and transform them into Pascal exception but JNI simply does not provide enough exception support APIs for that. If it is possible, I couldn't find an way with the existing JNI routines.

You can even capture an exception, but after that all of JNI, except a routine to dump a standard error information to logcat, stops working, so you cannot get any kind of information about the exception =( You also cannot recover from it ...

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Managed finally to determine android device orientation via JNI
« Reply #6 on: April 12, 2013, 03:01:41 pm »
It is not easy to get right the way to merge this to the LCL, so I did it myself. You can see here how it is done, in case you want to learn: http://svn.freepascal.org/cgi-bin/viewvc.cgi?view=revision&root=lazarus&revision=40797

Now one can read the screen orientation like this:

uses LazDeviceAPIs;

procedure Tform1.btnProgressClick(Sender: TObject);
var
  Rotation: TScreenRotation;
begin
  Rotation := Device.GetScreenRotation(0);

stab

  • Full Member
  • ***
  • Posts: 237
Re: Managed finally to determine android device orientation via JNI
« Reply #7 on: April 12, 2013, 03:23:07 pm »
My call to get WindowManager object was not right, even though the object was not nil.

When I changed to:
    // get method id for WindowManager object
    javaMethodId_WindowService := javaEnvRef^^.GetMethodID(javaEnvRef, javaAndroidAppActivityClass, 'getWindowManager', '()Landroid/view/WindowManager;');

    // Get the WindowManager object
    // Window w = (Window) getSystemService(Context.WINDOW_SERVICE);
    windowManagerObject := javaEnvRef^^.CallObjectMethod(javaEnvRef, javaActivityObject, javaMethodId_WindowService);

I got a valid WindowManager object and than it all worked.

Thanks for your help and link to your merge to LCL :D


 

TinyPortal © 2005-2018