• No results found

ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi

N/A
N/A
Protected

Academic year: 2021

Share "ANDROID PROGRAMMING - INTRODUCTION. Roberto Beraldi"

Copied!
60
0
0

Loading.... (view fulltext now)

Full text

(1)

ANDROID

PROGRAMMING

-INTRODUCTION

(2)

Web resources (android)

• Code • https://developer.android.com/guide/index.html • http://www.vogella.com/tutorials/android.html • http://www.techotopia.com/index.php/Android_4_App_Develop ment_Essentials • http://www.html.it/guide/guida-android/ • https://github.com/aporter/coursera-android

• Book and articles

• https://androidgroup.googlecode.com/files/Unlocking%20Andro id.pdf • http://anatomyofandroid.com/ • http://css.csail.mit.edu/6.858/2015/readings/android.pdf • Blog of developers • http://stackoverflow.com/

(3)

Smartphone hw architecture

A system-on-chip architecture with three primary

components:

An application processor executing the end-user’s

application software with assistance from a middleware and operating system (i.e., Android, IOs)

A modem or baseband processor with its own operating

system components responding to the baseband radio activities

A number of peripheral devices for interacting with the

(4)

Smartphone hw architecture

(5)

Role of application processor

Application processor executes the user

applications and the related OS services.

• Applications include audio/video codec and players,

games, image processing, speech processing, internet browsing, text editing, etc.

• Application processor takes help from graphics accelerators as and

when needed Most handheld applications are graphics-intensive

• Smartphones come with reasonably large amount of

storage in the form of volatile SDRAM (1-2 GB) as well as non-volatile compact storage (10+ GB)

(6)

Role of modem processor

Reception:

The receiver hardware (part of the modem)

senses incoming signals and generates interrupts for the radio interface logic of the operating system

• The radio interface and the operating system software run on a baseband

or modem processor

• Once the reception begins (after a physical layer

handshake), the incoming audio, video, and other data are processed by the modem processor

• The radio OS components talk to the peripheral device

drivers to present the incoming data to the user through appropriate devices (display, speaker, etc.)

(7)

Role of modem processor

Transmission:

The data to be transmitted are collected

by the radio OS components from memory regions populated by the device drivers.

• For example, audio data captured by the microphone driver or an image or

a video captured by the camera or a position information captured by the GPS device

• A transmission is initiated by the radio interface logic through the

modem transmitter hardware

• The subscriber identification module (SIM) plays an important role in

reception and transmission

(8)

Specific hardware issue

• Overall, mobile device’s hw architecture is similar to

desktop one, with some specific difference. In particular

• System on Chip (SoC)

• All the hw components are integrated (GPU,Wi-fi,etc)

• Multicore with cores that can run at different speed, cores

activated on demand

• Rich set of sensors

• Implemented as IC

• Mainly RISC architecture (ARM is predominant)

(9)

Example of CPU

• Qualcom

• Snapdragon + GPU (Adreno)

• Different families with different cores (2,4)

• Nvidia

• 4i,K1

• Intel

• First 64 bits CPU

• Mediatek

• 8 cores

• Samsung • HiSilicon

(10)

HW: Sensors

• Most devices have built-in sensors that measure motion, orientation, and

various environmental conditions.

• The Android platform supports three broad categories of sensors: • Motion sensors. These sensors measure acceleration forces and

rotational forces along three axes. This category includes

accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.

Environmental sensors. These sensors measure various environmental

parameters, such as ambient air temperature and pressure, illumination, and humidity. This category includes barometers, photometers, and

thermometers.

Position sensors. These sensors measure the physical position of a

(11)

GPS

• Based on triangolaritazion • 24+3 satellites

• Circular orbits on 6 circular planes at about 20 Km from the ground

• The receiver computes the distance from the satellites using synchronized clocks • Computing the distance requires to know the delay (about 0,007 s)

and start time of the received signal

• Satellite clocks are atomic clocks, while gps receiver clocks are not, but their values are adjusted when the intersection of the spheres is not unique

(12)

HW location

• Other location providers are based on cell-ID and wi-fi • Android uses these methods together (GPS, cell tower,

Wi-Fi) to get an idea of where the device is, and make that available to apps via a "Location Services" API.

(13)

Communications

• LTE 4G

• Up to 326,4 Mbit/s dowload • Up to 86,4 Mbit/s upload

• Wi-fi (with an Access Point)

• 802.11a (54 mbps),802.b (11), 802.11g (54),802.11n (300)

• Wi-fi Direct (p2p)

• Same standard as wi-fi

• Bluetooth

• Latest version 4.1(24Mbps)

• Make device discoverable...search for the device...enter passcode • Set-up times about 6 s

• IEEE 802.15.1

(14)

Communication: NFC

• Active device (reader): generate a magnetic field

• The reader emits a small electric current which creates a

magnetic field that in turn bridges the physical space between the devices.

• Passive device: react to the field emitting data

• Like RFID NFC works in the 13.56MHz radiofrequency

spectrum using less than 15mA of power to communicate data over distances that are usually far less than 10 cm.

• Tags typically store between 96 and 512 bytes of data and

transfer data using at speeds of 106Kb/s, 212Kb/s or 424Kb/s

(15)

Communication: NFC

• NFC Card emulation mode:

• enables NFC-enabled devices such as smartphones to act like smart

cards, allowing users to perform transactions such as payment or ticketing.

• NFC Reader/writer mode:

• enables NFC-enabled devices to read information stored on

inexpensive NFC tags embedded in labels or smart posters.

• A tag contains a small amount of non-volatile memory (store text, web address, email, vCard, Trigger, e..g. turn on wifi and lunch this app)

• NFC peer-to-peer mode:

• enables two NFC-enabled devices to communicate with each other to

exchange information in an ad hoc fashion.

• Applications may use higher-layer, e.g., SSL to establish a

secure channel

• For android see:

(16)

Typical usage

• contactless payments,

• marketing and advertising, • security and access control, • product identification,

• location identification,

• mobile phone task launcher apps • ….

(17)

Touch screen technologies

• Resistive touch screen

• Capacitive touch screen ( used in modern

smartphones)

(18)
(19)

Some fact about Android

• Android, Inc. was founded in Palo Alto, California in October

2003

• In July 2005, Google acquired Android Inc.

• At Google, the team led by Andy Rubin developed a mobile

device platform powered by the Linux kernel

• Android is built on top of more than 100 open projects,

including Linux kernel

• To increase security, each application runs with a distinct

system identity (Linux UID and GID)

• Application are isolated from each other

• Use a quite efficient IPC mechanism

• To facilitate resource access from isolated application, android

exploit a permission-based security mechanism

• Each application needs permissions to access system resources • Permissions are granted at application installation time

(20)

Global market

Source: http://www.windows8core.com/windows-phone-losses-03-global-market-share-january-2015-increase-android/

(21)

Android versioning

(22)
(23)

Some android feature

As new features were added (e.g., toolbar, actionbar,

fragments,…) support libraries were developed, so that

such features were also available to older versions

• Android API are very dynamic, so it can happen that some

method or widget are deprecated (always take a look at the official documentation)

• Support of multiple languages

• For example:

• Symbolic name given to a string in the code

(24)
(25)

Android architecture (kernel)

The kernel provides preemptive multitasking ,low level core system services, like

Hardware Abstraction

Hardware Drivers

Security Settings

Support for Shared Libraries Network Stack

(26)

Android architecture (kernel)

• Android-specific components

• Binder – IPC

• Android shared memory

• Power management

• Alarm driver

(27)

Android architecture, interacting with OS

• The ADB tool (see SDK) allows to interact with the linux

OS via a shell

ADB Client ADB Server

Developer option’s debug USB enabled

RSA key sent to the device must be acknowledged USB driver

(28)

Some example (see file systems)

• C:\Users\roberto\android-sdks\platform-tools>adb shell • shell@w55n:/ $ df

• df

• Filesystem Size Used Free Blksize • /dev 458.56M 132.00K 458.43M 4096 • /sys/fs/cgroup 458.56M 12.00K 458.55M 4096 • /mnt/asec 458.56M 0.00K 458.56M 4096 • /mnt/obb 458.56M 0.00K 458.56M 4096 • /system 1.44G 1.34G 101.68M 4096 • /data 1.50G 688.06M 852.85M 4096 • /persist 31.46M 4.02M 27.43M 4096 • /cache 245.95M 4.04M 241.90M 4096 • /persist-lg 7.83M 4.14M 3.70M 4096 • /mpt 31.46M 11.95M 19.51M 4096 • /sns 7.83M 4.02M 3.82M 4096 • /firmware 63.95M 33.22M 30.73M 16384 • /cust 49.18M 5.84M 43.33M 4096 • /mnt/shell/emulated 1.50G 688.06M 852.85M 4096

(29)

Example

• adb pull <f1><f2> • adb push <f2><f1> • adb backup

• adb restore <file>

(30)

Some example (processes)

• shell@w55n:/ $ ps | more

• ps | more

• USER PID PPID VSIZE RSS WCHAN PC NAME

• root 1 0 820 680 ffffffff 00000000 S /init • root 2 0 0 0 ffffffff 00000000 S kthreadd • root 3 2 0 0 ffffffff 00000000 S ksoftirqd/0 • root 6 2 0 0 ffffffff 00000000 D kworker/u:0 • root 7 2 0 0 ffffffff 00000000 D kworker/u:0H • root 8 2 0 0 ffffffff 00000000 S migration/0 • root 13 2 0 0 ffffffff 00000000 S khelper • root 14 2 0 0 ffffffff 00000000 S netns • root 18 2 0 0 ffffffff 00000000 S modem_notifier • root 19 2 0 0 ffffffff 00000000 S smd_channel_clo • root 20 2 0 0 ffffffff 00000000 S smsm_cb_wq • root 21 2 0 0 ffffffff 00000000 S kworker/u:1 • root 22 2 0 0 ffffffff 00000000 S rpm-smd • root 23 2 0 0 ffffffff 00000000 S kworker/u:1H • root 24 2 0 0 ffffffff 00000000 S mpm • root 25 2 0 0 ffffffff 00000000 S irq/47-cpr • root 55 2 0 0 ffffffff 00000000 S sync_supers • root 56 2 0 0 ffffffff 00000000 S bdi-default • root 57 2 0 0 ffffffff 00000000 S kblockd • root 58 2 0 0 ffffffff 00000000 S system • root 61 2 0 0 ffffffff 00000000 S irq/282-msm_iom • root 62 2 0 0 ffffffff 00000000 S irq/282-msm_iom

(31)

Quick guide to adb

(32)

Native SW libraries (C/C++)

Surface Manager: Rendering of Views 2D graphics Open GL ES 2D and 3D graphics For Embedded systems

Media Framework:

Manage different codec, e.g. mp3,H.264,MPEG4,etc.

Rendering of Font types

In process DB Web engine

(Bionic)C standard library

(33)

Java Network Interface (JNI)

• Native libraries can be called from java code • Java libraries wraps native code

• Developers can write native code (NDK)

• Critical portion of the application (assembly) • Legacy software

(34)
(35)

Android architecture: Dalvik VM

• Designed for: • Slow CPU

• Little RAM

• 64Mb total, ~10Mb available at runtime

• No swap space

• Limited battery life

• Use registers not stack (like the JVM does)

• Act as a sandbox: each application runs inside a DVM • Just In Time compilation

• Translate bytecode to native code just before its execution

• .dex format has footprint 50% smaller

• Replaced in Android 5.0 with Android Run Time (ART)

(36)

Core libraries

• Core Java classes

• android.*

• java.*, javax.* • junit.*

• org.apache.*, org.json.*, org.xml.*

• Doesn’t include all standard Java SDK classes

(37)

Core libraries (some)

android.app – Provides access to the application model used by of all Android applications.

android.content – Contains classes for accessing and publishing data on the device (content sharing,

Intent) Access to the package manager and resource manager.

android.database – Used to access data published by content providers and includes SQLite database

management classes.

android.graphics – A low-level 2D graphics drawing API including colors, points, filters, rectangles and

canvases.

android.hardware – Presents an API providing access to hardware such as the accelerometer and light

sensor.

android.opengl – A Java interface to the OpenGL ES 3D graphics rendering API.

android.os –Provides basic operating system services, message passing, and inter-process

communication on the device.

android.media – Provides classes to enable playback of audio and video.

android.net – A set of APIs providing access to the network stack. Includes android.net.wifi, which

provides access to the device’s wireless stack.

android.provider – A set of convenience classes that provide access to standard Android content provider

databases such as those maintained by the calendar and contact applications.

android.text – Used to render and manipulate text on a device display.

android.util – A set of utility classes for performing tasks such as string and number conversion, XML

handling and date and time manipulation.

android.view – The fundamental building blocks of application user interfaces.

android.widget - A rich collection of pre-built user interface components such as buttons, labels, list views,

layout managers, radio buttons etc.

android.webkit – A set of classes intended to allow web-browsing capabilities to be built into applications.

(38)

Android architecture: application

framework

Application framework: Set of services in the form of managers.

(39)

Android frameworks (not complete list)

Activity Manager – Controls all aspects of the application lifecycle and activity stack.

Content Providers – Allows applications to publish and share data with other applications.

Resource Manager – Provides access to non-code embedded resources such as strings,

color settings and user interface layouts.

Notifications Manager – Allows applications to display alerts and notifications to the user.

View System – An extensible set of views used to create application user interfaces.

Package Manager – The system by which applications are able to find out information about

other applications currently installed on the device.

Telephony Manager – Provides information to the application about the telephony services

available on the device such as status and subscriber information.

Location Manager – Provides access to the location services allowing an application to

(40)

Android architecture: app layer

(41)
(42)
(43)

Processes

• All of the default system functions of Android are provided

(44)
(45)

System Services

The System Services is a set of about 60-80 services

that provide the user applications with the information and capabilities necessary to work.

• All these services relay on the Binder framework to

communicate and collaborate with each other.

• Also, applications can instantiate these services through

Binder. Most are written in Java except for a couple of them, which are written in C/C++.

(46)

Booting sequence

(47)

How to program a mobile device

• Android applications are typically written in Java, using Eclipse

or Android Studio (official IDE)

• Native Language in C/C++

• Libraries for critical hw components • Functions can be called from java

• Native application in C#

• Xamarin (commercial platform)

• Portability among OS

• JavaScript (running inside webkit engine)

• JS functions can call Android methods • PhoneGap, Apache Cordova

• Set of js libraries, improves portability among OS

• Web applications

(48)

Interaction with cloud

• Mobile apps can interact with cloud to store data (photo,

contacts, etc), and synchronize different devices

• Can call web-api to enrich the functionality of the

application

(49)

Security

Security goals

• Protect sensible data of users, like contact and e-mail • Protect system resources

• Protect an application from other applications

Security mechanisms

• Native mechanisms, at kernel level

• Sandboxing, each application runs inside a sandbox

• Limited access to system resources. Resources restricted via permissions

• Secure IPC

• Application signature via a certificate

(50)

Sandbox and permission

Sandbox: Linux UID=1234

Application: UID 1234 DVM R e so u rce s camera android.permission.CAMERA <uses-permission> …. http://source.android.com/devices/tech/security/#the-application-sandbox

(51)

Sandbox and permission

• Sandbox provides access to a limited number of system

resources

• The access to a resosource is restricted using a

permission

• User should declare the use of the permission in the

manifest file and grant (all) permissions at installation time

• Sandboxing prevents one app from being able to read

data or modify the code of other apps installed on the system.

• This feature helps to contain malware and other security threats, so

that even if a vulnerable app is cracked via an exploit (or a

malicious app manages to get itself installed on a device), it can't be used to gain further access to other software or data stored on the device.

(52)

Sandbox and permission

Different applications can run in the same

process.

For this approach, one first must sign those

applications using the same private key and then

must assign to them the same Linux user ID

using the manifest file, by defining the manifest

attribute android:sharedUserId with the same

value/name.

(53)

Sandobox and permission

Sandbox: Linux UID=1234

Application: UID 1234 DVM R e so u rce camera Application: UID 1234 DVM

(54)
(55)

Characteristics of android applications

• User interaction

• touch screen based UI interface

• Variable screen size

• From low, medium, high (smart TV)

• Resource

• usage is an issue

• …but.. • Sensors

• Position, orientation, magnetic field, light sensor, ..

• Portable

• Context-awareness based applications (what’s around me, where

(56)

Bird’s eye view to application architecture

User Interface • Activity • Fragment

Computation

• Service

• Broadcast receiver

• Separate thread

• Need mechanism to interact with UI • Implements the “business logic” UI runs in a thread

Main thread it should respond fast responsiveness Data • Preference • File • SQLite • Network • Content provider

(57)
(58)

What an application is composed of?

• Software components • Activity • Fragment • Service • Broadcast receiver • Content provider • Intent • Resources

• Pictures, video, audio file, etc.

• Accessed via an ID

(59)

Android applications

• Every application runs in its own linux process (receivers

its own User ID)

• A process is created when a component of the application

needs to be run

• An unusual feature of Android is that an application

process’s lifetime is not directly controlled by the application (more on this soon)

• For example, if the application is temporary not visible the system

(60)

References

Related documents

and LCP olecranon plate Good Fair Fair Fair Excellent Excellent Excellent Good Good Poor Good LOM Ulnar nerve Sx Heterotophic ossification LOM, ulnar nerve Sx Ulnar nerve Sx

Na TC da perna esquerda observaram- -se, na face interna, 6 lesões sólidas heterogéneas (a maior na pele e no tecido celular subcutâneo e duas delas em contacto com a aponevrose

On Computer, install drivers for your Android device (e.g. Android Phone) so that the computer detected the Android Device and allows the Android Apps to be installed on the

Volume  StatusBar– NotificationLine Camera Numeric ID:5554 Hang up Home Call Power Search 19 19 Back Menu Tab Launch Pad Android Emulator Keyboard OSfunction

Prevalence of Metamera sillasenorum in the leeches Helob- della triserialis and Helobdella stagnalis after experimental infection using 3 different

When BWYv, Blrv, SbDv, and CpCSv isolates were tested by TBiA using antiserum produced against the Syrian isolate (SC402-08) of CpCSv, no cross-reactions were observed between

The Pathway of Distinction Analysis (PoDA) [8] was applied to the NAFLD genotype data for the following histologic phenotypes: steatohepatitis, NAFLD activity score (NAS) and

Fi- nally, we acknowledge the enduring support for the construction and operation of the LHC and the CMS detector provided by the following funding agencies: the Austrian Federal