Top Banner
1 Tech Talk AndroidWrapper for JamaicaVM Antonio Cesarano, aicas GmbH 9 September 2013
48

Tech Talk Project Work

Apr 07, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tech Talk Project Work

1

Tech Talk

AndroidWrapper for JamaicaVM

Antonio Cesarano,aicas GmbH

9 September 2013

Page 2: Tech Talk Project Work

2

Agenda

– Goal

– Issues

– Structure of Android applications

– Incompatibilities with JamaicaVM

– Proposed solution

– AndroidWrapper

Page 3: Tech Talk Project Work

3

Goal

Execute the source code of Android applications on the JamaicaVM.

Goal

Page 4: Tech Talk Project Work

4

Main question

– Why is not already possible?

Android applications are based on Java but they use own APIs, that are not compilable by JamaicaVM.

javax.swing.JComponent android.view.View

?

Issues

Page 5: Tech Talk Project Work

5

Java code execution

– Windows, Linux and other OS

HARDWARE(Intel, AMD, ARM)

Operating System(Windows, Linux, QNX)

Jamaica Runtime Environment

Java API Class

Jamaica Virtual

Machine

public class Hello{   public static void main(String args[])   {      System.out.println(“Hello World”);   }}

Issues

Page 6: Tech Talk Project Work

6

Java code execution

– Android

HARDWARE(Intel, ARM)

Linux Kernel

Android API Class

Dalvik Virtual

Machine

public class MainActivity extends Activity {      @Override    protected void onCreate(Bundle bundle)     {        super.onCreate(savedInstanceState);          TextView textView = (TextView) findViewById(R.id.textView1);        textView.setText("Hello World");    }}

Android Runtime Environment

Issues

Page 7: Tech Talk Project Work

7

Android applications

Main components– activity_main.xml

– R.java

– MainActivity.java

Structure of Android Application

Page 8: Tech Talk Project Work

8

activity_main.xml

<LinearLayout

 xmlns:tools="http://schemas.android.com/tools"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 tools:context=".MainActivity" 

>

     <TextView

      android:id="@+id/textView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="@string/hello_world" 

      />

</LinearLayout>

Simple example

Structure of Android Application

Page 9: Tech Talk Project Work

9

activity_main.xml

<LinearLayout

 xmlns:tools="http://schemas.android.com/tools"

 android:layout_width="match_parent"

 android:layout_height="match_parent"

 tools:context=".MainActivity" 

>

     <TextView

      android:id="@+id/textView1"

      android:layout_width="wrap_content"

      android:layout_height="wrap_content"

      android:text="@string/hello_world" 

      />

</LinearLayout>

Simple example

Structure of Android Application

Page 10: Tech Talk Project Work

10

R.java

public final class R {

public static final class id {

        public static final int textView1=0x7f080000;

    }

    public static final class layout {

        public static final int activity_main=0x7f030000;

    }

}

The R.java file is auto-generated by the Android Resource Manager and contains references to all resources of the app.

Structure of Android Application

Page 11: Tech Talk Project Work

11

MainActivity.java

public class MainActivity extends Activity 

{  

    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);  

        TextView textView=(TextView)findViewById(R.id.textView1);

        textView.setText("Hello, World!");

    }

}

Hello World example:

Structure of Android Application

Page 12: Tech Talk Project Work

12

Some incompatibilities to solve

Incompatibilities with JamaicaVM

Page 13: Tech Talk Project Work

13

Incompatibilities - 1

javax.swingjava.awt

java.toolsjava.rmi

...

java.io

java.toolsjava.util

java.lang

android.app

android.graphics

android.os

android.content

Android APIJamaica API

Incompatibilities with JamaicaVM

Page 14: Tech Talk Project Work

14

Incompatibilities - 2

public static void main(String args[])

protected void onCreate(Bundle bundle)

Incompatibilities with JamaicaVM

Page 15: Tech Talk Project Work

15

Incompatibilities - 3

Graphics components are not created in the source code but are declared in the activity_main.xml file.

Incompatibilities with JamaicaVM

Page 16: Tech Talk Project Work

16

Proposed Solution

Adapter Design Pattern– Adapter pattern (also known as Wrapper) allows classes that normally

could not work together, because of incompatible interfaces, to work together.

Proposed Solution

Page 17: Tech Talk Project Work

17

Different APIs

android.app

android.graphics

android.os

android.content

Android API

...

Adaptee c lasses

android.app

android.graphics

...

Proposed Solution

Android classes for JamaicaRE

Page 18: Tech Talk Project Work

18

main() method non-existent

Wrapper

 onCreate() {  doSomething(); }

Android code

 public static void main()     {        onCreate();     }

Adaptee classes

android.app

android.view

...

Proposed Solution

Page 19: Tech Talk Project Work

19

Get graphic components from XML file

1 - Parse the xml file

2 – Map objects into graphic components

3 – Draw graphic components

1

3

1

2

Proposed Solution

Page 20: Tech Talk Project Work

20

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

AndroidWrapper Solution

Page 21: Tech Talk Project Work

21

TreeGraphicNode

String androidIDString graphicElement

String marginLeft......

List<TreeGraphicNode>

TreeGraphicNode

Attributes

Children

Parent

AndroidWrapper Solution – XML Parser

Page 22: Tech Talk Project Work

22

GraphicNodeParser

LinearLayout

EditText

Button RadioGroup

RadioButton

RadioButton

DOMparent = null;

AndroidID = layout1;GraphicElement = LinearLayout;

Children = 0x7fff9465a02f;

Parent = 0x7fff9575c05f; 

AndroidID = text1;GraphicElement = TextView;

Children = null;

Parent = 0x7fff9575c05f; 

AndroidID = button1;GraphicElement = Button;

Children = null;

Parent = 0x7fff9575c05f; 

AndroidID = group1;GraphicElement = RadioGroup;

Children = 0x7fff4711b09f;

Parent = 0x7fff2311d01f, 

AndroidID = radio1;GraphicElement = RadioButton;

Children = null;

Parent = 0x7fff2311d01f; 

AndroidID = radio2;GraphicElement = RadioButton;

Children = null;

AndroidWrapper Solution – XML Parser

What it does?● Create an instance of TreeGraphicnode from the DOM

Page 23: Tech Talk Project Work

23

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

Page 24: Tech Talk Project Work

24

GraphicDrawer

Two main tasks:1. Create the screen surface

2. Draw graphic components into the screen

AndroidWrapper solution - Graphic manager

Page 25: Tech Talk Project Work

25

Create the screen surface

➢ Create the main frame➢ Create the command panel➢ Create the activity panel

MainFrameCommandPanel

ActivityPanel

AndroidWrapper solution - Graphic manager

Page 26: Tech Talk Project Work

26

Draw graphic components into the screen

Two phases:

➢Draw graphic components written in the xml

➢Add components to the screen

AndroidWrapper solution - Graphic manager

Page 27: Tech Talk Project Work

27

Draw components

parent = null;

AndroidID = layout1;GraphicElement = LinearLayout;

Children = 0x7fff9465a02f;

Parent = 0x7fff9575c05f; 

AndroidID = text1;GraphicElement = TextView;

Children = null;

Parent = 0x7fff9575c05f; 

AndroidID = button1;GraphicElement = Button;

Children = null;

Parent = 0x7fff9575c05f; 

AndroidID = group1;GraphicElement = RadioGroup;

Children = 0x7fff4711b09f;

Parent = 0x7fff2311d01f, 

AndroidID = radio1;GraphicElement = RadioButton;

Children = null;

Parent = 0x7fff2311d01f; 

AndroidID = radio2;GraphicElement = RadioButton;

Children = null;

button1button1

radio1

radio2

group1

text1

layout1drawComponent(node)

Graphic manager – Draw components into the screen

 Key

layout1

text1

button1group1

radio1

radio2

Value

componentsMap

Page 28: Tech Talk Project Work

28

Add components to the screen

parent = null;

AndroidID = layout1;GraphicElement = LinearLayout;

Children = 0x7fff9465a02f;

 Key

layout1

text1

button1

group1

radio1

radio2

Value

componentsMaplayout1

layout1

Graphic manager – Draw components into the screen

Page 29: Tech Talk Project Work

29

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapperer

Adaptee classes

AndroidWrapper solution

Page 30: Tech Talk Project Work

30

MainActivity.java

public class MainActivity extends Activity 

{  

    protected void onCreate(Bundle bundle) {

        super.onCreate(bundle);  

        TextView textView=(TextView)findViewById(R.id.textView1);

        textView.setText("Hello World");

    }

}

Hello World example:

AndroidWrapper solution - Wrapper

Page 31: Tech Talk Project Work

31

Activity

The Activity adaptee class is responsible to instantiate a GraphicDrawer and get the graphic from it.

protected void onCreate(Bundle bundle)

{

graphicDrawer = new GraphicDrawer(bundle);

graphicDrawer.generateGraphic();

}

layout1

button1button1

radio1

radio2

text1

group1

AndroidWrapper solution - Wrapper

Page 32: Tech Talk Project Work

32

AndroidWrapper

Two simple tasks:

1. Class R = 

Class mainActivity = 

File xmlFile  =

R.java

activity_main.xml

MainActivity.java

2. public static void main(String arg[])   {     mainActivity.onCreate(new Bundle(R,xmlFile))   }

AndroidWrapper solution - Wrapper

Page 33: Tech Talk Project Work

33

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

AndroidWrapper solution

Page 34: Tech Talk Project Work

34

Adaptee classes

To work correctly, the Adapter must find the JamaicaVM version of the classes requested by the MainActivity.

JamaicaREandroid.app.Activity

android.widget.TextView

...

   MainActivity.java

import android.app.Activity;import android.widget.TextView;

....

public void onCreate(){       ......

.....}.....

AndroidWrapper solution

Page 35: Tech Talk Project Work

35

Adaptee classes

Solution:– Write all Android classes ad-hoc for JamaicaVM

Positive aspect Negative aspect

AndroidWrapper solution

Page 36: Tech Talk Project Work

36

Positive aspect

Maximum compatibility with JamaicaVM

– Classes written ad-hoc for JamaivcaVM

AndroidWrapper solution

Page 37: Tech Talk Project Work

37

Negative aspect

Reproduce the whole Android Runtime Environment:● The number of the classes is very enormous

● The complexity is not always low

AndroidWrapper solution – Adaptee classes

Page 38: Tech Talk Project Work

38

– not always:

Three scenarios:

1) Need to write the class from zero

2) Wrap the class with an equivalent one

3) Use the original class

Adaptee classes - complexity

AndroidWrapper solution

Page 39: Tech Talk Project Work

39

 Mercury

Adaptee classes

1) Write a class from zero

Example: android.widget.ListView

 Venus Earth

 Mars

 ....

JPanel JScrollBar

JLabel

AndroidWrapper solution

Page 40: Tech Talk Project Work

40

Adaptee classes

2) Wrap the class with an equivalent one

Example: android.widget.Button

public Class Button extends JButton{   public Button(String text)   {      super(text);   }   ....}

AndroidWrapper solution

Page 41: Tech Talk Project Work

41

Adaptee classes

3) Use the original class

- When Android classes use only objects of Java

e.g android.util.TextUtils

AndroidWrapper solution

javax.swingjava.awt

java.toolsjava.rmi

...

java.io

java.toolsjava.util

java.lang

android.app

android.graphics

android.os

android.content

Android APIJava API

Page 42: Tech Talk Project Work

42

Summary: full route

AndroidWrapper solution

Page 43: Tech Talk Project Work

43

Page 44: Tech Talk Project Work

44

DEMO

Page 45: Tech Talk Project Work

45

Questions?

Page 46: Tech Talk Project Work

46

Thank you

Page 47: Tech Talk Project Work

47

Adaptee classes

Complexity for reproduce the whole Android Runtime Environment

Complexity

Activity

ClassesTextutils Button TextView

low

medium

high

Parcelable Resources

Use original class

Wrap with equivalent class

Write from zero

average complexity

...

AndroidWrapper solution

Page 48: Tech Talk Project Work

48

Sequence diagram

AndroidWrapper solution - Wrapper