android programming - introductionberaldi/macc_16/slides/02.pdf · smartphone hw architecture • a...
TRANSCRIPT
ANDROID PROGRAMMING -INTRODUCTIONRoberto Beraldi
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/
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
Smartphone hw architecture
source:
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)
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.)
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
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
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
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.
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
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.
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
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
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
Typical usage• contactless payments,• marketing and advertising,• security and access control,• product identification,• location identification,• mobile phone task launcher apps• ….
Touch screen technologies• Resistive touch screen • Capacitive touch screen (� used in modern
smartphones)• Single touch / multi-touch (pointers)
Touch screen: gesture
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)
Global market
Source: http://www.windows8core.com/windows-phone-losses-03-global-market-share-january-2015-increase-android/
Android versioning
See also: https://en.wikipedia.org/wiki/Android_version_history
Android screen sizes
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
Android architecture
Android architecture (kernel)The kernel provides preemptive multitasking ,low level core system services, like
Hardware AbstractionHardware Drivers Security SettingsSupport for Shared LibrariesNetwork Stack
Android architecture (kernel)• Android-specific components
• Binder – IPC• Android shared memory• Power management• Alarm driver• Kernel debugger & Logger
Android architecture, interacting with OS
• The ADB tool (see SDK) allows to interact with the linuxOS via a shell
ADB ClientADB ServerDeveloper option’s debug USB enabledRSA key sent to the device must be acknowledged
USB driver
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
Example• adb pull <f1><f2>• adb push <f2><f1>• adb backup• adb restore <file>
• see adb help for more explanation
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
Quick guide to adb• http://code.tutsplus.com/tutorials/android-adb-quick-guide-
-mobile-12456
Native SW libraries (C/C++)
Surface Manager:Rendering of Views2D graphics
Open GL ES2D and 3D graphicsFor 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
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
JNI
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
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
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
Android architecture: application framework
Application framework:Set of services in the form of managers.
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 receive updates about location changes.
Android architecture: app layer
Application layer
Google play services
Layers….
Processes • All of the default system functions of Android are provided
as the server process type
Binder
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++.
Booting sequence
see: http://en.miui.com/thread-15659-1-1.html
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• jQuery mobile
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
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
Sandbox and permission
Sandbox: Linux UID=1234
Application: UID 1234
DVM
Res
ourc
es
cameraandroid.permission.CAMERA
<uses-permission> ….
http://source.android.com/devices/tech/security/#the-application-sandbox
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.
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.
Sandobox and permission
Sandbox: Linux UID=1234
Application: UID 1234
DVM
Res
ourc
e
camera
Application: UID 1234
DVM
Defining a permission
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, …)
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 threadMain thread � it should respond fast
� responsiveness
Data
• Preference• File• SQLite• Network• Content provider
• Many ways to store data
What an application is composed of?
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.
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
Questions?