Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
ANDROID PROGRAMMING INTRODUCTION Roberto 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/ 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 Some fact about Android • Each application is associated to a Linux user (UID and GUID) and runs inside a dedicated process • Use a quite efficient and secure IPC mechanism (Binder) • To facilitate resource access from isolated applications, android exploits a permission-based security mechanism • Each application needs permissions to access system resources • A permission has a protection level and belongs to a group • NormalDangerousSignedSystemOrSigned • Permissions are granted at application installation time • (at run time 6+) Support libraries • As new features are added (e.g., toolbar, actionbar, fragments,…) support libraries are developed, so that such features are also available to older android versions • v4 support, v7 appcompat,etc.. (see documentation) • 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 for multiple languages • For example: • Symbolic name given to a string in the code • Different values according to the nationality of the smartphone • Support for different versions and screen resolutions Google play services • The Google Play services APK contains the individual Google services and runs as a background service in the Android OS. • The Google Play services APK is delivered through the Google Play Store, so updates to the services are not dependent on carrier or OEM system image updates. Accessing Google Play Services Android architecture Android architecture (kernel) The kernel provides preemptive multitasking ,low level core system services, like Hardware Drivers Security Settings Network Stack Power Management … Android architecture, interacting with OS • The ADB tool (see SDK) allows to interact with the linux OS via a shell USB driver ADB Client 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 Example • adb pull <f1><f2> • adb push <f2><f1> • adb backup • adb restore <file> • see adb help for more explanation Quick guide to adb • http://code.tutsplus.com/tutorials/android-adb-quick-guide- -mobile-12456 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) • It acts 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) • Whole app compiled at installation time 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.* • Some of them are wrappers of ‘native libraries’ 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 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 Android architecture: application framework Application framework: Set of services in the form of managers. Android frameworks (not complete list) • 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. • Activity Manager – Controls all aspects of the application lifecycle and activity stack. • View System – An extensible set of views used to create application user interfaces. • 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. Android architecture: app layer Application layer Security • Security goals • Protect sensible data of users, like contact and e-mail • Protect system resources • Protect an application from other applications • Security mechanisms • Signed Boot Image • 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 Resources <uses-permission> …. android.permission.CAMERA camera http://source.android.com/devices/tech/security/#the-applicationsandbox 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 • Starting from Android 6.0 this is no longer true • Sandboxing prevents one app from being able to read data or modify the code of other apps installed on the system. 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 Resource DVM camera Application: UID 1234 DVM Defining a permission Protection Level of a permission • Normal • The default value. A lower-risk permission that the system automatically grants without asking for the user's explicit approval • Dangerous • A higher-risk permission that would give a requesting application access to private user data. User has to accept the requesting permission (so that the system will grant it) • Signature • A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the 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 Computation Data • Activity • Fragment UI runs in a thread Main thread it should respond fast responsiveness • Service • Broadcast receiver • • • • • Preference File SQLite Network Content provider • Separate thread • Need mechanism to interact with UI • Implements the “business logic” • 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?