Recent

Author Topic: Android Module Wizard  (Read 704798 times)

rsu333

  • Full Member
  • ***
  • Posts: 110
Re: Android Module Wizard
« Reply #1230 on: June 28, 2021, 03:15:36 pm »
Your work is energetic for us.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1231 on: July 23, 2021, 11:07:13 pm »
Hi, All!

Now LAMW can handle "Firebase Cloud Messaging"  push notification!

NEW! "jsFirebasePushNotificationListener"  component

NEW!  demo:
AppCompatFirebasePushNotificationListenerDemo1

NEW! doc!
"how_to_handling_firebase_push_notification.txt"

Quote
How to: Handling Firebase Push Notification in LAMW  [warning: "beta" version ::  23-July-2021]
   (by jmpessoa)


0) Create your "AppCompat" [[theme]] project
   0.1)Put a "jsFirebasePushNotificationListener" component on form
        0.2)Go to lazarus menu and simply  "Run" --> "Build"

1) Login in your google acount [email or any....]

2) Go to "https://console.firebase.google.com/"

3) Create/Add a new Project   [ex: "MyOrg"]

4) Go to your "MyOrg" project and click "+Add app"

5) Click on Android Icon

6) In (page) "Add Firebase to your Android app":
   6.1) "Register App"
      6.1.1 ""Android package name"
         ex: org.lamw.appcompatfirebasepushnotificationlistenerdemo1

      6.1.2 CLICK the button "Register app"

   6.2) "Download config file"
                 get the "google-services.json" and put it in your LAMW project folder

        Done!

   (don't do anything more, LAMW takes care of it all for you!)

7) Active your Internet phone/device connection

8) Go to Lazarus menu "Run" --> "[LAMW] Build Android Apk and Run"

9) Go to "https://console.firebase.google.com/"

   9.1 Click the "MyOrg" project
   9.2 In the left panel/menu page go to "Engage" --> "Cloud Messaging"
   9.3 Click "New notification" Button
   9.4 Fill the form and click "Next"
   9.5 In the "Target" --> "selectan app"   [select your app package...] and "Next"
        9.6 In the "Scheduling" --> "Now"  and "Next"
        9.7 In the bottom page click the button "Review"
   9.8 "Publish"  !!!!


Congratulations!!!


10) Notes about your/custom Payload expected by LAMW component:
   [https://firebase.google.com/docs/cloud-messaging/concept-options?hl=en]

10.1 Notification Payload


{
"message":{
-    ...
     ...
    "notification":{
      "title" : "my message title",     // ---> "title" expected by LAMW
      "body" : "my message body"        // ---> "body"  expected by LAMW
    }
    ...
    ...
  }
}

Note: Notification messages are delivered to the notification tray when the app is in the background.
For apps in the foreground, messages are handled/delivery by a LAMW callback function for you !!!


10.2 Data Payload

{
  "message":{
    ...
    ...
    "data":{
      "title":"my message title",// ---> "title" expected by LAMW
      "body":"my message body",  // ---> "body" expected by LAMW
      "myKey1":"myValue1"        //free/custom
      "myKey2":"myValue2"        //free/custom
       ......                    //free/custom
    }
    ...
    ...
  }
}

Where myKey1, myKey2, .... are your free/custom key=value pairs ....

warning: from "data" messages only "title" and "body" are handled/delivery by the LAMW callback function for you
         but you can read all "data" when App is "waking up" by clicking in notification....

//by code:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1ActivityCreate(Sender: TObject; intentData: jObject);
  2. var
  3.   data: TDynArrayOfString;
  4.   i, count: integer;
  5. begin
  6.   data:= jIntentManager1.GetBundleContent(intentData, '=');  //key=value
  7.   count:= Length(data);
  8.   for i:= 0 to count-1 do
  9.   begin
  10.      ShowMessage(data[i]);  //key=value
  11.   end;
  12. end;
  13.  

//and/or maybe:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1NewIntent(Sender: TObject; intentData: jObject);
  2. var
  3.   data: TDynArrayOfString;
  4.   i, count: integer;
  5. begin
  6.   data:= jIntentManager1.GetBundleContent(intentData, '=');  //key=value
  7.   count:= Length(data);
  8.   for i:= 0 to count-1 do
  9.   begin
  10.      ShowMessage(data[i]);  //key=value
  11.   end;
  12. end;
  13.  

10.3 Mixing Notification and Data Payload

{
  "message":{
    ...
    "notification":{
      "title" : "my message title",     // ---> "title" expected by LAMW
      "body" : "my message body"        // ---> "body"  expected by LAMW
    }
    "data":{
      "myKey1":"myValue1"        //free/custom
      "myKey2":"myValue2"        //free/custom
       ......
    }
    ...
    ...
  }
}


10.4  WARNING: "beta" version ::  23-July-2021

10.5 References

https://firebase.google.com/docs/cloud-messaging/concept-options?hl=en


Thanks to All!
« Last Edit: August 09, 2021, 12:21:38 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: Android Module Wizard
« Reply #1232 on: August 08, 2021, 10:45:55 am »
Is it posibble in near future LAMW modify firebase db? Just like FB4D  :)
Or just simply modifying by using javascript inside webview?

Thank you
« Last Edit: August 08, 2021, 11:11:51 am by Mongkey »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1233 on: August 09, 2021, 12:26:05 am »

Quote
Is it posibble in near future LAMW modify firebase db

Yes, we can try implement firebase "db" ...

But let's clarify:
jsFirebasePushNotificationListener component is about  "Firebase Cloud Messaging"  push notification!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Mongkey

  • Sr. Member
  • ****
  • Posts: 430
Re: Android Module Wizard
« Reply #1234 on: August 09, 2021, 01:48:28 am »
Ok, thank you

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1235 on: January 17, 2022, 07:49:21 pm »
Hi, All!

Android 11 - Api 30  has restricted developers to access devices "publics" folders (ex: "Download, Documents, ..., etc")

Developers no longer have access to a file via its path. Instead, you need to use via “Uri“.

New!   jForm methods:

 //get user permission... and an uri
RequestCreateFile
RequestOpenFile
RequestOpenDirectory
procedure TakePersistableUriPermission

 //handling file by uri....
 function GetFileList                [edited...]
 function GetBitmapFromUri
 function GetTextFromUri
 procedure SaveImageToUri
 procedure SaveTextToUri

New demo:
"AppPublicFoldersAccessDemo1"
« Last Edit: February 01, 2022, 08:38:42 pm by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • Jr. Member
  • **
  • Posts: 62
Re: Android Module Wizard
« Reply #1236 on: March 12, 2022, 02:24:34 pm »
//handling file by uri....
 function GetBitmapFromUri
 function GetTextFromUri
 procedure SaveImageToUri
 procedure SaveTextToUri

New demo:
"AppPublicFoldersAccessDemo1"

I have added function SaveBytesToUri.

The updated code is in attachment, in two files:
1) jForm.java, located at
C:\fpcupdeluxe\ccr\lamw\android_wizard\smartdesigner\java

2) androidwidget.pas, located at
C:\fpcupdeluxe\ccr\lamw\android_bridges


jmpessoa, please search the attached files for phrase “by neuro” and copy/paste new code into LAMW distribution files.

The new code in “androidwidget.pas”:
Code: Pascal  [Select][+][-]
  1. procedure SaveBytesToUri(_bytes: TDynArrayOfJByte; _toTreeUri: jObject); // by neuro
  2. procedure jForm_SaveBytesToUri(env: PJNIEnv; _jform: JObject; _bytes: TDynArrayOfJByte; _toUri: jObject); // by neuro
  3.  
  4. //by neuro
  5. procedure jForm.SaveBytesToUri(_bytes: TDynArrayOfJByte; _toTreeUri: jObject);
  6. begin
  7.   //in designing component state: set value here...
  8.   if FInitialized then
  9.      jForm_SaveBytesToUri(FjEnv, FjObject, _bytes ,_toTreeUri);
  10. end;
  11.  
  12. //by neuro
  13. procedure jForm_SaveBytesToUri(env: PJNIEnv; _jform: JObject; _bytes: TDynArrayOfJByte; _toUri: jObject);
  14. var
  15.   jParams: array[0..1] of jValue;
  16.   jMethod: jMethodID=nil;
  17.   jCls: jClass=nil;
  18.  
  19.   byteArray: jByteArray;
  20.   len: SizeInt;
  21.  
  22. label
  23.   _exceptionOcurred;
  24. begin
  25.  
  26.   jCls:= env^.GetObjectClass(env, _jform);
  27.   if jCls = nil then goto _exceptionOcurred;
  28.   jMethod:= env^.GetMethodID(env, jCls, 'SaveBytesToUri', '([BLandroid/net/Uri;)V');
  29.   if jMethod = nil then goto _exceptionOcurred;
  30.  
  31.   len := Length(_bytes);
  32.   byteArray:= env^.NewByteArray(env, len);
  33.   env^.SetByteArrayRegion(env, byteArray, 0 , len, @_bytes[0]);
  34.  
  35.   jParams[0].l:= byteArray;
  36.   jParams[1].l:= _toUri;
  37.  
  38.   env^.CallVoidMethodA(env, _jform, jMethod, @jParams);
  39.   env^.DeleteLocalRef(env,jParams[0].l);
  40.  
  41.   env^.DeleteLocalRef(env, jCls);
  42.  
  43.   _exceptionOcurred: jni_ExceptionOccurred(env);
  44. end;

The new code in “jForm.java”:
Code: Java  [Select][+][-]
  1.     //Android 11: //by neuro
  2.     public void SaveBytesToUri(byte[] _bytes, Uri _toTreeUri) {
  3.         OutputStream out=null;
  4.         try {
  5.             out = controls.activity.getContentResolver().openOutputStream(_toTreeUri, "w");
  6.  
  7.             out.write(_bytes);
  8.             out.flush();
  9.         } catch (FileNotFoundException e) {
  10.             e.printStackTrace();
  11.         } catch (IOException e) {
  12.             e.printStackTrace();
  13.         }
  14.     }  
  15.  
   


Usage example:
Code: Pascal  [Select][+][-]
  1. var
  2.   array_of_bytes: array of byte;
  3.   array_of_jbytes: TDynArrayOfJByte;
  4.   n, size_of_array: Integer;
  5.  
  6. begin  
  7.  
  8. size_of_array := 256;
  9. setLength(array_of_bytes, size_of_array);  
  10. setLength(array_of_jbytes, size_of_array);
  11.  
  12. for n := 0 to size_of_array-1 do
  13. begin
  14. array_of_bytes[n] := Byte(128); // here set the values of byte array
  15. array_of_jbytes[n] := JByte(array_of_bytes[n]);
  16. end;
  17.  
  18. Self.SaveBytesToUri(array_of_jbytes, treeUri);
  19.  
  20.  
  21. end;


We also need function
GetBytesFromUri
which is not yet available.

In sample project AppPublicFoldersAccessDemo1
two buttons work (“Request Create File” and “Request Open Directory”),
however third button “Request Open File” does not work – nothing happens when this button is pressed.
I guess this button should invoke system file picker (equivalent of open file dialog), however it does not work.
I am running AppPublicFoldersAccessDemo1 on Android 8.1 (device: Samsung Galaxy Tab A 10.1).
I have fixed this bug in the post below.
« Last Edit: March 12, 2022, 06:28:52 pm by neuro »

neuro

  • Jr. Member
  • **
  • Posts: 62
Re: Android Module Wizard
« Reply #1237 on: March 12, 2022, 06:18:17 pm »
I have fixed the bug in sample project AppPublicFoldersAccessDemo1.
The problem was that button “Request Open File” does not work.
The fixed code should be replaced in file “androidwidget.pas”, located at
C:\fpcupdeluxe\ccr\lamw\android_bridges

Code: Pascal  [Select][+][-]
  1. procedure jForm_RequestOpenFile(env: PJNIEnv; _jform: JObject; _uriAsString: string; _fileMimeType: string; _requestCode: integer);
  2. var
  3.   jParams: array[0..3] of jValue; // <<---- bug fixed
  4.   jMethod: jMethodID=nil;
  5.   jCls: jClass=nil;
  6. label
  7.   _exceptionOcurred;
  8. begin
  9.  
  10.   jCls:= env^.GetObjectClass(env, _jform);
  11.   if jCls = nil then goto _exceptionOcurred;
  12.   jMethod:= env^.GetMethodID(env, jCls, 'RequestOpenFile', '(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V'); // <<---- bug fixed
  13.   if jMethod = nil then goto _exceptionOcurred;
  14.  
  15.   jParams[0].l:= env^.NewStringUTF(env, PChar(_uriAsString));
  16.   jParams[1].l:= env^.NewStringUTF(env, PChar(_fileMimeType));
  17.   jParams[2].l:= env^.NewStringUTF(env, PChar(nil)); // <<---- bug fixed
  18.   jParams[3].i:= _requestCode;  // <<---- bug fixed
  19.  
  20.   env^.CallVoidMethodA(env, _jform, jMethod, @jParams);
  21.   env^.DeleteLocalRef(env,jParams[0].l);
  22.   env^.DeleteLocalRef(env,jParams[1].l);
  23.   env^.DeleteLocalRef(env,jParams[2].l); // <<---- bug fixed
  24.  
  25.   env^.DeleteLocalRef(env, jCls);
  26.  
  27.   _exceptionOcurred: jni_ExceptionOccurred(env);
  28. end;

jmpessoa, please update function “jForm_RequestOpenFile” in file “androidwidget.pas” with the fixed code above.


Another problem – in file:
C:\fpcupdeluxe\ccr\lamw\demos\GUI\AppPublicFoldersAccessDemo1\jni\unit1.pas
the code does not work:
Code: Pascal  [Select][+][-]
  1. //or if mimetype is "image/*"
  2. ImageView.SetImage(Self.GetBitmapFromUri(treeUri));

I suspect that the problem is inside file
C:\fpcupdeluxe\ccr\lamw\android_wizard\smartdesigner\java\jForm.java
in program code of function:
Code: Java  [Select][+][-]
  1. //Android 11:
  2. public Bitmap GetBitmapFromUri(Uri _treeUri)

However I still do not know how to fix it.
« Last Edit: March 12, 2022, 08:19:06 pm by neuro »

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1238 on: March 12, 2022, 09:38:46 pm »

hi, neuro!

First: thank you!

But, if possible, can you submit a "pull request" for changes in github?
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • Jr. Member
  • **
  • Posts: 62
Re: Android Module Wizard
« Reply #1239 on: March 12, 2022, 10:11:17 pm »
But, if possible, can you submit a "pull request" for changes in github?

I have never used github before, I need to figure out how to do that, I’ll do that a little later.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1240 on: March 13, 2022, 12:22:15 am »
Quote
I have never used github before, I need to figure out how to do that, I’ll do that a little later.

Ok.  So,  please, send me all modified  ".pas"   and   ".java"  ... I will commit your changes...

(maybe you can "zip" all and put here....)


Thank you!

Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

neuro

  • Jr. Member
  • **
  • Posts: 62
Re: Android Module Wizard
« Reply #1241 on: March 13, 2022, 02:46:11 pm »
can you submit a "pull request" for changes in github?

I have made two "pull requests" for changes in github for two files (jForm.java, androidwidget.pas).

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1242 on: March 13, 2022, 10:07:08 pm »

Quote
I have made two "pull requests" for changes in github for two files (jForm.java, androidwidget.pas).

Done!


Thank you!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

jmpessoa

  • Hero Member
  • *****
  • Posts: 2296
Re: Android Module Wizard
« Reply #1243 on: April 14, 2022, 12:46:48 am »
Hi, All!

LAMW "0.8.6.2" moving to Android API >= 30 !!!

Please, update your android sdk/platforms folder!
How to:
.open a command line terminal and go to android folder "sdk/tools/bin"
.run the command  >>sdkmanager --update
.run the command  >>sdkmanager "build-tools;30.0.2" "platforms;android-30"


Thanks to All!
« Last Edit: April 14, 2022, 04:45:56 am by jmpessoa »
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

dseligo

  • Hero Member
  • *****
  • Posts: 1177
Re: Android Module Wizard
« Reply #1244 on: April 14, 2022, 02:06:22 am »
Hi, All!

LAMW "0.8.6.2" moving to Android API >= 30 !!!

Please, update your android sdk/platforms folder!
How to:
.open a command line terminal and go to android folder "sdk/tools/bin"
.run the command  >>sdkmanager --update'
.run the command  >>sdkmanager "build-tools;30.0.2" "platforms;android-30"


Thanks to All!

Great!

Thank you for your effort.

 

TinyPortal © 2005-2018