tech talk project work

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

Upload: antonio-cesarano

Post on 07-Apr-2017

143 views

Category:

Software


0 download

TRANSCRIPT

1

Tech Talk

AndroidWrapper for JamaicaVM

Antonio Cesarano,aicas GmbH

9 September 2013

2

Agenda

– Goal

– Issues

– Structure of Android applications

– Incompatibilities with JamaicaVM

– Proposed solution

– AndroidWrapper

3

Goal

Execute the source code of Android applications on the JamaicaVM.

Goal

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

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

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

7

Android applications

Main components– activity_main.xml

– R.java

– MainActivity.java

Structure of Android Application

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

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

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

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

12

Some incompatibilities to solve

Incompatibilities with JamaicaVM

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

14

Incompatibilities - 2

public static void main(String args[])

protected void onCreate(Bundle bundle)

Incompatibilities with JamaicaVM

15

Incompatibilities - 3

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

Incompatibilities with JamaicaVM

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

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

18

main() method non-existent

Wrapper

 onCreate() {  doSomething(); }

Android code

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

Adaptee classes

android.app

android.view

...

Proposed Solution

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

20

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

AndroidWrapper Solution

21

TreeGraphicNode

String androidIDString graphicElement

String marginLeft......

List<TreeGraphicNode>

TreeGraphicNode

Attributes

Children

Parent

AndroidWrapper Solution – XML Parser

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

23

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

24

GraphicDrawer

Two main tasks:1. Create the screen surface

2. Draw graphic components into the screen

AndroidWrapper solution - Graphic manager

25

Create the screen surface

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

MainFrameCommandPanel

ActivityPanel

AndroidWrapper solution - Graphic manager

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

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

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

29

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapperer

Adaptee classes

AndroidWrapper solution

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

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

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

33

Android Wrapper

XML Parser●TreeGraphicNode●GraphicNodeParser

Graphic manager●GraphicDrawer

Wrapper●Activity●AndroidWrapper

Adaptee classes

AndroidWrapper solution

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

35

Adaptee classes

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

Positive aspect Negative aspect

AndroidWrapper solution

36

Positive aspect

Maximum compatibility with JamaicaVM

– Classes written ad-hoc for JamaivcaVM

AndroidWrapper solution

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

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

39

 Mercury

Adaptee classes

1) Write a class from zero

Example: android.widget.ListView

 Venus Earth

 Mars

 ....

JPanel JScrollBar

JLabel

AndroidWrapper solution

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

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

42

Summary: full route

AndroidWrapper solution

43

44

DEMO

45

Questions?

46

Thank you

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

48

Sequence diagram

AndroidWrapper solution - Wrapper