Programming with Android System Architecture Luca Bedogni Marco

Programming with Android: System Architecture Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna

Outline Android Architecture: An Overview Android Dalvik Java Virtual Machine Android Components: Activities Android Components: Intents Android Components: Services Android Components: Content Providers Android Application Distribution and Markets Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 2

Android … What? v Android is a Linux-based platform for mobile devices … § Operating System § Applications § Software Development Kit (SDK) v Which kind of mobile devices … (examples) SMARTPHONES TABLETS Luca Bedogni, Marco Di Felice EREADERS - ANDROID TV Programming with Android – System Architecture GOOGLE GLASSES ? 3

Android … What? ANDROID MICROWAVE SMART FRIDGE SMARTPHONES TABLETS Luca Bedogni, Marco Di Felice EREADERS - ANDROID TV Programming with Android – System Architecture GOOGLE GLASSES ? 4

Android … When? 2005 2006 2007 Ø Google buys Android from the Android Inch Ø Open Handset Alliance (OHA) created for open standards for mobile devices. Partners of OHA: Google, Motorola, Samsung, Vodafone, T-Mobile, etc Ø Android 1. 0 Released 2008 2009 Time Ø The first Android smartphone: G 1 HTC-Dream Ø Android 1. 1 Released Ø Android 1. 5 (Cup. Cake) Released Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 5

Android … When? 2008 2009 Ø Android 1. 6 (Donut) Released Ø Android 2. 0 (Eclair) Released Ø Android 2. 2 (Froyo) Released 2010 Ø Android 2. 3 (Gingerbread) Released 2011 Ø Android 3. 0 (Honeycomb) Released (First version for devices with larger screens such as tablets) 2012 Time Ø Android 4. 0 (Ice-Cream Sandwich) Released. (It merges the 3. x tab centric design and the v 2. x phone based design into a single version. ) Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 6

Android … When? 2012 2013 2014 2015 Ø Android 5. 0 (Lollipop) Released Ø UI Restyling (Material Design) Ø Personal unlock mechanisms Ø (Battery) Performance optimization Ø New experimental runtime virtual machine, ART… API Level 21 (Android 5. 0): Ø Material design style Ø Lock screen notification Ø Support for multiple network connections (Scan for network connections with specific capabilities) Time Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 7

Android … Why? 2013 Market Share Luca Bedogni, Marco Di Felice - www. gartner. com Programming with Android – System Architecture 2014 Market Share 8

Sorgente: http: //jeffalangray. com Android … Why? Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 9

http: //thenextweb. com/mobile/2014/08/21/18796 -different-android-devices-according-opensignals-latest-fragmentation-report/ Android … Why? 18, 796 different devices in 2014! Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 10

Android … How? http: //www. droid-life. com/ January 2015 Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 11

Android … How? http: //en. wikipedia. org/wiki/Android_version_history 1. 6 -2. 0 2. 2. x 2. 3. x 4. x Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 12

The Android Architecture } Stack Architecture Open Source Architecture (Apache/MIT License v. 2. 0) Business-friendly License Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 13

The Android Architecture Built on top of Linux kernel (v. 2. 6 -3. 14) Advantages: Ø Portability (i. e. easy to compile on different hardware architectures) Ø Security (e. g. secure multi -process environment) Ø Power Management Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 14

The Android Architecture Native Libraries (C/C++ code) Ø Graphics (Surface Manager) Ø Multimedia (Media Framework) Ø Database DBMS (SQLite) Ø Font Management (Free. Type) Ø Web. Kit Ø C libraries (Bionic) Ø …. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 15

The Android Architecture Application Libraries (Core Components of Android) Ø Activity Manager Ø Packet Manager Ø Telephony Manager Ø Location Manager Ø Contents Provider Ø Notification Manager Ø …. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 16

The Android Architecture Applications (Written in Java code) Ø Android Play Store Ø Entertainment Ø Productivity Ø Personalization Ø Education Ø Geo-communication Ø …. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 17

The Android Architecture Dalvik Virtual Machine (VM) Ø Novel Java Virtual Machine implementation (not using the Oracle JVM) Ø Open License (Oracle JVM is not open!) Ø Optimized for memoryconstrained devices Ø Faster than Oracle JVM Ø …. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 18

Dalvik Java Virtual Machine (JVM) Java Source Code Java Standard Edition Java Source Code Java Compiler Java Byte Code Stack-based byte-code Dex Compiler Dalvik Byte Code Register-based byte-code Dalvik Virtual Machine (VM) Java Virtual Machine (JVM) Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 19

Android Applications ANDROID APP CATEGORIES ANDROID APP PRICE http: //www. appbrain. com/stats/android-market-app-categories Luca Bedogni, Marco Di Felice - http: //www. onlinemarketing-trends. com/2011/07/androidmarketplace-top-5 -statistics. html Programming with Android – System Architecture 20

Android Applications Design APPLICATION DESIGN: Ø GUI Definition Ø Events Management Ø Application Data Management Ø Background Operations Ø User Notifications Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 21

Android Applications: Development http: //developer. android. com/guide/developing/building/index. html#detailed-build ² ADB is a client server program that connects clients on developer machine to devices/emulators to facilitate development. ² An IDE like Android Studio handles the entire development process Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 22

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 23

Android Applications Design Ø Beside using the basic components, an Android Application can rely on system services and external libreries Android System Services ² Wi. Fi Service ² Embedded Sensor Service ² Notification Manager Service ²… Luca Bedogni, Marco Di Felice - Google Play Libraries ² Google Maps API ² Activity Recognition API ² Google Cloud Messaging ²… Programming with Android – System Architecture 24

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 25

Android Components: Activities Ø An Activity corresponds to a single screen of the Application. Android Hello. World Button 1 Hello World! Ø An Application can be composed of multiples screens (Activities). Ø The Home Activity is shown when the user launches an application. Ø Different activities can exhange information one with each other. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 26

Android Components: Activities Ø Each activity is composed by a list of graphics components. Ø Some of these components (also called Views) can interact with the user by handling events (e. g. Buttons). Ø Two ways to build the graphic interface: PROGRAMMATIC APPROACH Example: Button button=new Button (this); Text. View text= new Text. View(); text. set. Text(“Hello world”); Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 27

Android Components: Activities Ø Each activity is composed by a list of graphics components. Ø Some of these components (also called Views) can interact with the user by handling events (e. g. Buttons). Ø Two ways to build the graphic interface: DECLARATIVE APPROACH Example: < Text. View android. text=@string/hello” android: textcolor=@color/blue android: layout_width=“fill_parent” android: layout_height=“wrap_content” /> < Button android. id=“@+id/Button 01” android: textcolor=“@color/blue” android: layout_width=“fill_parent” android: layout_height=“wrap_content” /> Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 28

Android Components: Activities EXAMPLE SCREEN CONFIGURATION DISTRIBUTION Device 1 Device 2 HIGH screen pixel density LOW screen pixel density Java App Code XML Layout File Device 1 Device 2 Luca Bedogni, Marco Di Felice - http: //developer. android. com/about/dashboards/index. html Programming with Android – System Architecture 29

Android Components: Activities EXAMPLE Device 1 Device 2 HIGH screen pixel density LOW screen pixel density Java App Code XML Layout File Device 1 Device 2 Luca Bedogni, Marco Di Felice - - Build the application layout through XML files (like HTML) - Define two different XML layouts for two different devices - At runtime, Android detects the current device configuration and loads the appropriate resources for the application - No need to recompile! - Just add a new XML file if you need to support a new device Programming with Android – System Architecture 30

Android Components: Activities Ø Android applications typically use both the approaches! DECLARATIVE APPROACH Define the Application layouts and resources used by the Application (e. g. labels). XML Code PROGRAMMATIC APPROACH Manages the events, and handles the interaction with the user. Java Code Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 31

Android Components: Activities Button Text. Edit Ø Views can generate events (caused by human interactions) that must be managed by the Android-developer. ESEMPIO public void on. Click(View arg 0) { if (arg 0 == Button) { // Manage Button events } } Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 32

Android Components: Activities Ø The Activity Manager is responsible for creating, destroying, managing activities. Ø Activities can be on different states: starting, running, stopped, destroyed, paused. Ø Only one activity can be on the running state at a time. Ø Activities are organized on a stack, and have an event-driven life cycle (details later …) Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 33

Android Components: Activities Ø Main difference between Android-programming and Java (Oracle) -programming: ØMobile devices have constrained resource capabilities! Ø Activity lifetime depends on users’ choice (i. e. change of visibility) as well as on system contraints (i. e. memory shortage). Ø Developer must implement lifecycle methods to account for state changes of each Activity … Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 34

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 35

Android Components: Intents Ø Intents: asynchronous messages to activate core Android components (e. g. Activities). Ø Explicit Intent The component (e. g. Activity 1) specifies the destination of the intent (e. g. Activity 2). LOGIN Welcome Marco! Activity 2 Activity 1 marco PASSWORD ***** Login Intent Login Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 36

Android Components: Intents Activity 2 Multiple choices might be available to the user! View Implicit Intent Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture Activity 2 Activity 1 Ø Intents: asynchronous messages to activate core Android components (e. g. Activities). Ø Implicit Intent The component (e. g. Activity 1) specifies the type of the intent (e. g. “View a video”). } Intent. Filters 37

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 38

Android Components: Services Ø Services: like Activities, but run in background and do not provide an user interface. Ø Used for non-interactive tasks (e. g. networking). Ø Service life-time composed of 3 states: Starting Destroyed on. Create() on. Start() on. Destroy() Running (on background) Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 39

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 40

Android Components: Content Providers Ø Each Android application has its own private set of data (managed through files or through SQLite database). Ø Content Providers: Standard interface to access and share data among different applications. insert() APP update() delete() Content Provider query() Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture DB e. g. Photo Gallery 41

Android Applications Design Ø Developing an Android Application means using in a proper way the Android basic components … Activity Fragment Luca Bedogni, Marco Di Felice - Layout Views Intent Content Providers Service Broadcast Receiver Programming with Android – System Architecture 42

Android Components: Broadcast Receivers Ø Publish/Subscribe paradigm Ø Broadcast Receivers: An application can be signaled of external events. Ø Notification types: Call incoming, SMS delivery, Wifi network detected, etc Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 43

Android Components: Broadcast Receivers BROADCAST RECEIVER example class Wifi. Receiver extends Broadcast. Receiver { public void on. Receive(Context c, Intent intent) { String s = new String. Builder(); wifi. List = main. Wifi. get. Scan. Results(); for(int i = 0; i < wifi. List. size(); i++){ s. append(new Integer(i+1). to. String() + ". "); s. append((wifi. List. get(i)). to. String()); s. append("\n"); } main. Text. set. Text(sb); } } Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 44

Android Components: System API Ø Using the components described so far, Android applications can then leverage the system API … SOME EXAMPLEs … Ø Ø Ø Telephony Manager data access (call, SMS, etc) Sensor management (GPS, accelerometer, etc) Network connectivity (Wifi, bluetooth, NFC, etc) Web surfing (HTTP client, Web. View, etc) Storage management (files, SQLite db, etc) …. Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 45

Android Components: Google API Ø … or easily interface with other Google services: Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 46

Android Application Distribution Ø Each Android application is contained on a single APK file. APK FILE XML Files Ø Java Byte-code (compiled for Dalvik JVM) C Luca Bedogni, Marco Di Felice Ø Resources (e. g. images. videos, XML layout files) ØLibraries (optimal native C/C++ code) - Programming with Android – System Architecture 47

Android Application Distribution Ø Each application must be signed through a key before being distributed. Ø Applications can be distributed via Web or via Stores. Ø Android Play Store: application store run by Google … but several other application stores are available (they are just normal applications). Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 48

Android Application Security Ø Android applications run with a distinct system identity (Linux user ID and group ID), in an isolated way. Ø Applications must explicitly share resources and data. They do this by declaring the permissions they need for additional capabilities. Ø Applications statically declare the permissions they require. Ø User must give his/her consensus during the installation. ANDROIDMANIFEST. XML <uses-permission android: name=“android. permission. IACCESS_FINE_LOCATION" /> <uses-permission android: name=“android. permission. INTERNET" /> Luca Bedogni, Marco Di Felice - Programming with Android – System Architecture 49
- Slides: 49