ANDROID PROGRAMMING - INTRODUCTION Roberto Beraldi Web resources - - PowerPoint PPT Presentation

android programming introduction
SMART_READER_LITE
LIVE PREVIEW

ANDROID PROGRAMMING - INTRODUCTION Roberto Beraldi Web resources - - PowerPoint PPT Presentation

ANDROID PROGRAMMING - INTRODUCTION Roberto Beraldi Web resources (android) Code https://developer.android.com/guide/index.html http://www.vogella.com/tutorials/android.html


slide-1
SLIDE 1

ANDROID PROGRAMMING - INTRODUCTION

Roberto Beraldi

slide-2
SLIDE 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/
slide-3
SLIDE 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

end-user

slide-4
SLIDE 4

Smartphone hw architecture

source:

slide-5
SLIDE 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)

slide-6
SLIDE 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
  • r 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.)

slide-7
SLIDE 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

  • For example it sends the user’s authentication key
slide-8
SLIDE 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)
  • One instruction at each clock cycle
slide-9
SLIDE 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
  • P8
slide-10
SLIDE 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
  • device. This category includes orientation sensors and magnetometers.
slide-11
SLIDE 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

slide-12
SLIDE 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.

slide-13
SLIDE 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
  • NFC
slide-14
SLIDE 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

slide-15
SLIDE 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:
  • http://developer.android.com/guide/topics/connectivity/nfc/nfc.html
slide-16
SLIDE 16

Typical usage

  • contactless payments,
  • marketing and advertising,
  • security and access control,
  • product identification,
  • location identification,
  • mobile phone task launcher apps
  • ….
slide-17
SLIDE 17

Touch screen technologies

  • Resistive touch screen
  • Capacitive touch screen ( used in modern

smartphones)

  • Single touch / multi-touch (pointers)
slide-18
SLIDE 18

Touch screen: gesture

slide-19
SLIDE 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
  • There are 130 resources (android 4.2)
slide-20
SLIDE 20

Global market

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

slide-21
SLIDE 21

Android versioning

See also: https://en.wikipedia.org/wiki/Android_version_history

slide-22
SLIDE 22

Android screen sizes

slide-23
SLIDE 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
  • Different values according to the nationality of the smartphone
slide-24
SLIDE 24

Android architecture

slide-25
SLIDE 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

slide-26
SLIDE 26

Android architecture (kernel)

  • Android-specific components
  • Binder – IPC
  • Android shared memory
  • Power management
  • Alarm driver
  • Kernel debugger & Logger
slide-27
SLIDE 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

slide-28
SLIDE 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
slide-29
SLIDE 29

Example

  • adb pull <f1><f2>
  • adb push <f2><f1>
  • adb backup
  • adb restore <file>
  • see adb help for more explanation
slide-30
SLIDE 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
slide-31
SLIDE 31

Quick guide to adb

  • http://code.tutsplus.com/tutorials/android-adb-quick-guide-
  • mobile-12456
slide-32
SLIDE 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 Wrapped as Java libraries

slide-33
SLIDE 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
slide-34
SLIDE 34

JNI

slide-35
SLIDE 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)

See: https://source.android.com/devices/tech/dalvik/index.html

slide-36
SLIDE 36

Core libraries

  • Core Java classes
  • android.*
  • java.*, javax.*
  • junit.*
  • org.apache.*, org.json.*, org.xml.*
  • Doesn’t include all standard Java SDK classes
  • developer.android.com/reference/packages.html
slide-37
SLIDE 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.
  • See: http://developer.android.com/reference/packages.html
slide-38
SLIDE 38

Android architecture: application framework

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

slide-39
SLIDE 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
  • ther 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

receive updates about location changes.

slide-40
SLIDE 40

Android architecture: app layer

Application layer

slide-41
SLIDE 41

Google play services

slide-42
SLIDE 42

Layers….

slide-43
SLIDE 43

Processes

  • All of the default system functions of Android are provided

as the server process type

slide-44
SLIDE 44

Binder

slide-45
SLIDE 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++.

slide-46
SLIDE 46

Booting sequence

see: http://en.miui.com/thread-15659-1-1.html

slide-47
SLIDE 47

How to program a mobile device

  • Android applications are typically written in Java, using Eclipse
  • r 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
  • jQuery mobile
slide-48
SLIDE 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

  • For example exploit open data
slide-49
SLIDE 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
  • Application-defined and user-granted permissions
slide-50
SLIDE 50

Sandbox and permission

Sandbox: Linux UID=1234 Application: UID 1234 DVM Resources camera android.permission.CAMERA <uses-permission> ….

http://source.android.com/devices/tech/security/#the-application- sandbox

slide-51
SLIDE 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.

slide-52
SLIDE 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.

slide-53
SLIDE 53

Sandobox and permission

Sandbox: Linux UID=1234 Application: UID 1234 DVM Resource camera Application: UID 1234 DVM

slide-54
SLIDE 54

Defining a permission

slide-55
SLIDE 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

are my friends, …)

slide-56
SLIDE 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
  • Many ways to store data
slide-57
SLIDE 57

What an application is composed of?

slide-58
SLIDE 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
  • Accessed via a manager.
slide-59
SLIDE 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

may decide to kill the process

slide-60
SLIDE 60

Questions?