Lamw Framework Fundamental

Download as rtf, pdf, or txt
Download as rtf, pdf, or txt
You are on page 1of 23

Lamw: Lazarus Android Module Wizard

Android: Form Designer and Components development


model!
"A wizard to create JNI Android loadable module (.so)
in Lazarus/Free Pascal using
[datamodule like] Form Designer and Components!"

Authors:
José Marques Pessoa
CUA/UFMT
https://github.com/jmpessoa/lazandroidmodulewizard

http://forum.lazarus.freepascal.org/index.php/topic,21919.0.html

Simon,Choi [Original Android Non-Visual Lib]


http://blog.naver.com/simonsayz
I. Android Architecture

1. Applications: These are applications written in Java. [User


Layer]

Some of basic applications includes an calendar, email client, SMS program, maps,
making phone calls, accessing the Web browser, accessing your contacts list and
others.
2. Application Framework [Developers Layer - High]:

The application framework is a set of basic [java] tools/component/APIs with


which a developer can build more complex apps/tools.
.Telephony Manager manages all voice calls.
.Location Manager provide location awareness services.
.Activity Manager manages the life cycle of activities in applications.
.Content Providers manage the sharing of data between applications.
.Resource Manager manages the various types of resources used by applications
[static content: bitmaps, colors, layouts, strings ...]

3. Native Libraries [Developers Layer - Low]:

. Native Android libraries written in C, C++, and used by various system:


.Exposed to Android developers via Android Application framework [item
2].
Ex:
All libraries in this domain can be used by developers via application framework
[item 2]:
Webkit: A fast web-rendering engine used by Safari, Chrome, and other
browsers
SQLite: A full-featured SQL database: database engine used for storage and
sharing of android application data.
OpenSSL: The secure locket layer
libc: Set of C/C++ libraries.
SGL: Render 2D graphic.
SSL: Internet security.
FreeType: Render bitmap and vector fonts.
Open GL|ES: Render 2D or 3D graphic and video.
Media Framework: Media codecs to handle the recording and playback of
various audio, video and picture formats.
etc....

4. Runtime Android: Core java libraries and Dalvik VM

Dalvik Virtual Machine

The Dalvik VM enables every Android application to run in its own process, with
its own instance of the Dalvik virtual machine.
The Dalvik executes files in executable (.Dex) optimized for minimum memory.
The source code (Java) is compiled to a class file (.class) with Java compiler which
is later transformed to Dalvik Executable by DX tools.
Core java libraries
The core java libraries help developers to write applications using standard Java
programming language.

V. Linux Kernel

Memory management programs,


Security settings, power management software and several drivers for
hardware ,
File system access, networking and inter-process-communication.

The kernel also acts as an abstraction layer between hardware and the rest of the
software stack.

II. Elements and Structure of Android Application


Key components:

Activity Component for composing UI.


Service Component that runs on the background. No visual UI.
Broadcast Receiver All registered receivers registered for an announcement will
be notified by the Android runtime when this announcement is broadcasted.
Ex.
An application can broadcast an announcement on the arrival
of some data (e.g. new mail arrival) to other applications.
Receivers who registered for this announcement will be
notified of this announcement and it is up to them to initiate appropriate
actions...
Content Provider Provides standardized interface for sharing data among
applications.

These four components can be closely connected using Intent.

Intent Asynchronous Message [initiated by some application/components]


which sends [requested]
action and data to another application/components.
Intent Filter Defines component by setting intent for receiving.

Others components:

Notification Notifying a certain event to user.


View Every item you see on the screen of an Android device is a View
object
[buttons, text fields, checkboxes, spinners, ListView, GridView ...]
ViewGroup A ViewGroup is a special [invisible] View that can contain other Views
or ViewGroups
Layout A Layout [subclass of ViewGroup] defines how child views inside are
positioned/related visually on the screen
Fragment You can combine multiple fragments in a single activity or reuse a
fragment in multiple activities
Resources Non-code assets as files, bitmaps, layout definitions, user interface
strings ...
Assets Data files to be installed to device along with the application.

AndroidManifest.xml Android Manifest is the interface between an App and


the Android system.
Describes each component of the application and how
they interact and
all the configuration/information/permission about your
app that the Android system must know

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dummyPackage" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="17"/>
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_OWNER_DATA"/>
<uses-permission android:name="android.permission.READ_OWNER_DATA"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_BLUETOOTH_SHARE"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.flash"
android:required="false"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<uses-feature android:name="android.hardware.telephony"
android:required="false"/>
<uses-feature android:name="android.hardware.sensor.stepcounter"/>
<uses-feature android:name="android.hardware.sensor.stepdetector"/>
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="dummyAppName"
android:label="@string/app_name"
android:configChanges= "orientation|keyboardHidden|screenSize|screenLayout|
fontScale">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>

III. GUI Architecture: MVC:


The Android UI framework is organized around the common Model-
View-Controller pattern.
Android GUI is single-threaded, event-driven and built on nestable components.
Model: What to render -- Android classes that implement the application logic
View: How to render --- Android Views [built-in classes like Button ... ], layouts
and resources
Controller: Events, user input -- Android Activity [and Service,
BrodacastReceiver ...]

The Controller [Controls both model and view]:


Responds to external actions: a keystroke, a screen tap, an incoming call, etc.,
It is implemented as an event queue,
Take care of all logic that needs to be done;
Prepare Model that need to be sent to view layer;
Update view when model changes;
Update model when user manipulate the input;

IV - Application Model: SDK [NDK]


V - Development Kit

.Java JDK
.Android SDK
.Android NDK
.Ant [java make]
Eclipse + ADT [Plugin] or Android Studio

VI - Development: Project Structure

Basic directory structure for developing the Android application.

/assets Data files to be installed to device along with the application.


/bin Compiled application file.
/gen Resource reference files automatically created by the Android build system.
/res Resource files such as character strings and images to be used in the
application.
/src Application source code.
/libs java libs [.jar] and native libs [.so]
AndroidManifest.xml

Android's development tools [Sdk ] combine/compile the resource files [+libs]]


and DEX file [from .class file] to make APK.
*AAPT : Android Asset Packaging Tool

VII. Java Native Interface/JNI


[Specification/Protocol]

A specification/protocol for calling 'native' code from Java and Java code from
native applications ('Invocation API');
An interface that allows Java to interact with code written in another language:
The Java code can invoke native methods;
An interface that allows code written in another language to interact with Java :
The native code can instantiate Java objects and invoke methods;
JNI is about "Programming for the Java Virtual Machine";
JNI is used to interact with "native" code produced by system-level language like
C, C++ and Pascal.

Motivation

When a program cannot be written in Java alone;


To integrate or reuse legacy applications/libraries;
When platform-specific functionality is not accessible using the Java API;
To optimize a (small) time critical part of an application;
Code reusability : Reuse existing/legacy code with Java (mostly C/C++);
JNI can also be used to invoke Java code from within natively-written applications -
such as those written in C/C++;

Development
.Create a Java class with native method(s): --->> public native void
sayHello(String who, int times);
.compile [JDK javac] to get the ".class"
.run the JDK javah to generate C signature [.h]
.Implement the function in native library
.Load the library which implements the method: System.loadLibrary("Hello");
.write Java code that uses the library
.Invoke the native method from Java

Example
---------------------------------------------------------
"org_cuaufmt_jniexamples_Hello.h"
#include <jni.h>
JNIEXPORT void JNICALL Java_org_cuaufmt_jniexamples_Hello_sayHello(JNIEnv *,
jobject, jstring, jint);

--------------------------------------------------------
"Hello.c"
#include <stdio.h>
#include "org_cuaufmt_jniexamples_Hello.h"
JNIEXPORT void JNICALL Java_com_marakana_jniexamples_Hello_sayHi (JNIEnv *env,
jobject obj, jstring who, jint times) {
jint i;
jboolean iscopy;
const char *name;
name = (*env)->GetStringUTFChars(env, who, &iscopy);
for (i = 0; i < times; i++) {
printf("Hello %s\n", name);
}
}

Compile
libHello.so, //linux
Hello.dll, //win
libHello.jnilib //mac

Android and JNI


------------------------------------------------

VIII. Object Pascal [Free/Open ] RAD-Component-


GUI-IDE Language
" Write once ... Compile Anywhere"
Free Pascal [professional] Compiler: 32, 64 and 16 bit ;

Multiple processor architectures:

Intel x86, AMD64/x86-64 [Win/Linux/Android], PowerPC, PowerPC64, SPARC, and


ARM [Linux/Android].;

Multiple Supported operating systems:

Linux, FreeBSD, Haiku, Mac OS X/iOS/Darwin, DOS, Win32, Win64, WinCE, OS/2,
MorphOS,

Nintendo GBA, Nintendo DS, and


Nintendo Wii, PalmOS, Android;

JVM, MIPS[i8086 and Motorola 68k];


FCL: TComponent

Inline Assembler;

program HelloAsm;

var

num, answer : integer;

begin

num := 10;

{$ASMMODE intel}

asm

MOV EAX, num

ADD EAX, 110B //add binary 110

SUB EAX, 2 //subtract decimal 2

MOV answer, EAX

end;

writeln('answer =' + answer);

end;

Latest News - February 24th, 2015


Support for the iOS/AArch64 platform has been added to svn trunk.

Incoming News -
[Target to] Java Script [see SmartPascal ...]

Free Pascal and C


Free Pascal compiler supports linking with objects and shared objects compiled in
C

GCC compiler supports linking with objects and shared objects compiled in Free
Pascal

Easy use/bindings of C libraries in Pascal: there is tools to translation C function


header [.h]

X. Lazarus: [Free/Open] Object Pascal RAD IDE


[Components]

Run at Multiple operating systems: Win, Linux, Mac, BSB;


Supported Many operating systems [cross-compile] : All Free Pascal Supported !;
GUI Widget set API : Win32, gtk2+, Carbon/Cocoa, CustomDraw, fpGUI, QT and
Android --- by jmpessoa ;-)) .

XI. Lamw Framework : Lazarus Android Moldule


Wizard
Component Model: [jControl and jVisualControl]
Java Class Wrapper <--- JNI ---> Pascal Class/Component
Java Class <--- JNI --- Pascal Class/Component

Project Wizard

Cross Compile Settings


Code Generator
Form Designer
Components Palette

Tools [helpers]
Settings Paths: [Jdk, Sdk, Ndk, Ant, ...]
Late: Apk Build/Install
New jComponent Creator

Lamw Apk Application Architecture: Centered on Controller


Component
-------------------------------------------------------------------------------------------------------------------------------------------
---------------------------------------------

Java Side: Pascal Side:

Java Activity <<--- JNI --->> Native Pascal


Library/Module

Controls <<--- JNI ---> > Controls


Create Objects <<--- JNI ----- Create
Objects
Create Objects <<--- JNI ----- Create
Objects
System Events
User Events
Dispatch Events ---- JNI --->> Handle Events
-------------------------------------------------------------------------------------------------------------------------------------------
---

Architecture as Code [Simon Brown]

Java::[App.java] JNI
Pascal::[controls.pas]

Activity
CustomApp
jControls jcontls; /*controls.java*/
libcontrols.so
onCreate
jcontls = New jControls(this, new RelativeLayout(this));
setContentView(jcontls.appLayout);
jcontrls.LoadSystemLibrary('libcontrols.so'); ----->>
JNI_OnLoad
jcontls.NativeEventOnCreate(jContls,jContls.Layout); ----->>
NativeEventOnCreate(jContls,jLayout)

CustomApp.Init(jContls,Layout);

jForm.jCreate(Self,Layout); <<-----
jForm.jCreate(Self, CustomApp.jLayout)

jComponent1.jCreate(Self,Layout); <<-----
jComponent1.jCreate(Self, CustomApp.jLayout);
... ...
... ...

jComponentN.jCreate(Self,Layout); <<----
jComponentN.jCreate(Self, CustomApp.jLayout);
... ...
... ...
Events
jComponentX.onClick();
jComponentX.NativeEventOnClick(Self); ---->>
NativeEventOnClick(Self);

XII - Screenshots
XIV - Demo
XV - References
https://github.com/jmpessoa/lazandroidmodulewizard

http://forum.lazarus.freepascal.org/index.php/topic,21919.0.html

http://www.codeproject.com/Articles/798780/Introduction-to-Android

https://www.tbray.org/ongoing/When/201x/2010/11/14/What-Android-Is

http://blog.ameykelkar.com/2014/01/android-architecture-brief-introduction.html

http://expressmagazine.net/development/620/android-architecture

https://thenewcircle.com/static/bookshelf/java_fundamentals_tutorial/_java_native_interface_jni.html

http://homepage.cs.uiowa.edu/~slonnegr/wpj/JNI.pdf

http://www.ibm.com/developerworks/java/tutorials/j-jni/j-jni.html

http://justobjects.org/cowcatcher/browse/advjava/slides/java-jni/core/jni/slide.0.20.html

https://blog.udemy.com/pascal-ide/

http://expressmagazine.net/development/620/android-architecture#sthash.UPW1J03x.dpuf
http://www.cubrid.org/blog/dev-platform/android-at-a-glance/

http://gkonandroid.blogspot.com.br/2013/11/mvc-architecture-in-android.html

ftp://ftp.freepascal.org/pub/fpc/docs-pdf/CinFreePascal.pdf

http://www.math.uni-leipzig.de/pool/tuts/FreePascal/prog/node13.html

XIV. Thank you!

You might also like