SL4A – Python Library

SL4A – Python Library ”; Previous Next Python has a standard library, which includes a wide variety of routines that help you code and reuse these codes easily. A Module is a Python file that has definitions of variables and a set of related routines or functions. Some of the core Modules provided by the Python library are as follows − Built-in Functions and Exceptions − Python imports both these modules when it starts up and makes their content available for all programs. The built-in module defines built-in functions like len, int, range, while the exceptions module defines all built-in exceptions. Operating System Interface Modules − The OS module makes available, the functions that enables performing OS level operations through scripts. Type Support Modules − Type support modules include string module-to implement, commonly used string operations; math module provides mathematical operations etc. Regular Expressions − Regular Expressions are string patterns written in a specific syntax, which can be used to match or extract strings or substrings. The re module provides Regex support for Python. Language Support Modules − The sys module gives you access to various interpreter variables, such as the module search path, and the interpreter version. The operator module provides functional equivalents to many built-in operators. The copy module allows you to copy objects. Finally, the gc module gives you more control over the garbage collector facilities in python 2.0. About JSON The JavaScript Object Notation (JSON) is a data-interchange format. Though many programming languages support JSON, it is especially useful for JavaScript-based apps, including websites and browser extensions. JSON can represent numbers, Booleans, strings, null, arrays (ordered sequences of values), and objects (string-value mappings) made up of these values (or of other arrays and objects). Print Page Previous Next Advertisements ”;

SL4A – Quick Guide

SL4A – Quick Guide ”; Previous Next SL4A – Android Overview Android is a Linux based operating system, designed primarily for portable devices like smartphones and tablet. Android is more than an operating system. It is a software stack that provides many features for users, developers and manufacturers. Android Version Features Android (Alpha) 1.0 First version of Android Android 1.5 (Cupcake) Added auto-rotation option, copy and paste feature added in the web browser Android 1.6 (Donut) Voice search and Search box were added Android 2.0/2.1 (Éclair) Bluetooth 2.1 support Android 2.2/2.2.3(Froyo) Support for Adobe Flash 10.1 Android 2.3/2.3.7 (Gingerbread) Internet calling, one touch word selection and copy/paste Android 3.0/3.2.6 (Honeycomb) This version is available only for tablets.Features include support for multi-core processors, ability to encrypt all user data Android 4.0/4.0.4 (Ice cream sandwich) Virtual button in the UI, ability to shut down apps that are using data in the background Android 4.1/4.3.1 (Jelly Bean) Based on Linux Kernel 3.0.31? Smoother User Interface, Bluetooth data transfer for Android Beam Android 4.4/4.4.4,4.4W/4.4W.2 (KitKat) Integration with cloud storage, Built-In sensors, Lock Art Screen Android 5.0/5.1.1 (Lollipop) Improved RAM and Battery Management, Fixes for sudden App closure, issues with wireless connections, notifications etc. It is open source and business friendly. The user can choose to replace built-in applications with applications of their choice. Many versions of Android have been released since its original release. SL4A – Android Architecture The Android software stack comprises of different layers, each layer manifesting well-defined behavior and providing specific services to the layer above it. The following figure presents a broad architecture of Android with its primary components. Linux Kernel Android is built on the Linux kernel code modified to run on embedded systems as against the traditional Linux system. Hardware drivers for many common devices are built into the kernel, thereby ensuring device portability. It is based on secure user based permission model, which prevents one application from reading another application’s information or from interfering with its execution (e.g. CPU, memory, devices etc.). Group ID based access to networking and Bluetooth features, logger, alarm, power management, low memory killer, Binder IPC mechanisms are a few noted enhancements to the Kernel. Libraries and Runtime This forms the second layer of the architecture. It includes a set of hardware optimized C, C++ libraries, also referred to as the native layer. Examples include Media Libraries, SQLite, SSL, Bionic, WebKit etc. The Android Runtime comprises of two different components − the Dalvik VM and Core Libraries. The Dalvik VM provides an execution environment for the applications on the Android Platform. The set of core libraries provides most of the functionality available in Java APIs. Application Framework This layer makes available the Libraries as services to the applications. Some most important components within this layer include Location Manager, Resource Manager, Activity Manager and Notification Manager. Applications By default, Android comes with a rich set of applications, including the browser, SMS program, calendar, contact manager, maps etc. Google Play provides alternatives to these applications, if the user desires so. Building Blocks An application can interface with the system with four function blocks. They are as follows − Activities − Activities represent a single screen that the user sees or interacts with visually. For example, an email app might have one activity that shows a list of new emails, another activity to compose an email, and another activity for reading emails. Services − A majority of processing is done by these services for your application. It is the logic behind the user interface. For example, a service might play music in the background while the user is in a different app, or it might fetch data over the network without blocking user interaction with an activity. Broadcast Receivers − A component that can be registered to listen for system or application events and issue notifications about the same. An instance for broadcast originating from the system is a ‘low battery notification’ while an application level broadcast could be a ‘download successful’ notification. Content Providers − A content provider manages and helps you share data between multiple applications. For e.g. a content provider may be used to share the contacts data. These components interact with each other through messages called Intents. SL4A – Scripting Layer for Android Android scripting is a handy tool for simple tasks such as automation and prototyping as against using the Java programming language for the same. The Scripting Layer for Android (SL4A) provides a platform for multiple scripting solutions on the android platform. What is SL4A? The Scripting Layer for Android (SL4A) is a library that provides a platform to write simple scripts to automate tasks on an android device using languages other than JAVA. SL4A, originally called Android Scripting Environment (ASE), was first announced on the Google Open Source blog in June 2009. The project saw the light of the day primarily through the efforts of Damon Kohler and it has grown through the contributions of other developers. It supports most popular scripting languages. These include − Beanshell 2.0b4 JRuby 1.4 Lua 5.1.4 Perl 5.10.1 PHP 5.3.3 Python Rhino 1.7R2 Javascript TCL Why SL4A – Scripting Languages Vs JAVA One of the first questions one would probably ask is, “Why use SL4A instead of Java?” Firstly, not everyone is a fan of Java. Scripting languages provide an easy programming environment as compared to Java. Secondly, the language requires the use of an edit/compile/run design loop. This means that you edit, re-compile and then run the application each time you desire some modification. On the contrary, scripts are interpreted and executed on the fly. Moreover, SL4A even makes it possible, in many cases, to reuse code written for a desktop environment. The following figure illustrates scripts written in Python targeting SL4A and in Java respectively. SL4A – Architecture SL4A has three main components. They are as follows − Script Interpreters Android RPC Client Facades In this chapter, we will discuss more about these three components. Script Interpreters

SL4A – Working with Eclipse

SL4A – Working with Eclipse ”; Previous Next This chapter will walk you through the basics of Eclipse IDE and configuring it to develop Python scripts for Android. Basics of Eclipse Eclipse is an application with many capabilities and options. A welcome screen will greet you, the first time you launch the program. Eclipse uses a number of basic concepts and terminology to address different functions and operations of the program. Some of these terminologies are explained here − Workbench − It identifies the overall window of the Eclipse application. It contains multiple child windows, each window containing multiple tabs. Workspace − It refers to the location on your system’s file system where projects created using Eclipse will be saved. Perspective − It is a personal preference for which menus and windows are open at any one time. Eclipse has a number of perspectives configured for typical usage, such as writing code, debugging etc. You can also switch to a different perspective when your activity changes. Perspectives are also customizable. Installing Plugins for Eclipse One needs to configure Eclipse to be able to develop SL4A applications. Once the prerequisites are installed successfully on the host, the following plugins for Eclipse must be added − ADT plugin for Eclipse (available at http://dl-ssl.google.com/android/eclipse/ ) Python Plugin for Eclipse (available at http://pydev.org/updates ) The following steps can be used to install plugins on Eclipse − Step 1 − Click Help. Step 2 − Select Install New Software. Step 3 − Click Add. Step 4 − Specify the name and the URL to access the plugin. Click OK. Step 5 − Select all the tools and click Next. The plugin installation starts Python Interpreter in Eclipse Follow the given steps to configure Python Interpreter in Eclipse. Step 1 − Select Windows → Preferences. Step 2 − Go to PyDev → Interpreter → Python. Step 3 − Click the New button. Step 4 − Specify a name for the interpreter. In the Location field, enter the path to python.exe. For example, C:Python26python.exe Step 5 − Click OK. Print Page Previous Next Advertisements ”;

SL4A – Development Environment

SL4A – Development Environment ”; Previous Next This chapter details the procedure to set up your development environment. It also discusses how to configure your device to use SL4A. Development Environment You must download and install several pre-requisites before you can start developing with SL4A. Java Development Kit (JDK) To begin with, you should have a recent version of JDK (JDK 5 or upwards) installed on your system. To confirm that a compatible version of the JDK installed available to the environment, execute the following on the command line or console terminal, as follows − $javac –version $java –version Note − Set the PATH environment variable to jdk/bin folder Python It is likely that Python is installed on Mac OS X and Linux, by default. Windows OS, by default, does not have Python installed. Python can be downloaded from http://python.org. Once installed, modify your Windows path to add the Python directory. To verify if Python is installed on your system, open the terminal window and enter Python. Android.py file The only additional file that one needs to download is the android.py file. Include a copy of this file under /Lib/site-packages in the Python installation directory. This file includes python modules for Android development. Android SDK A number of tools are provided with the Android SDK. The first step is to download the appropriate install file for your operating system. Installers for Mac OS X, Linux and Windows are available. The SDK installer can be found at https://developer.android.com/sdk. The development environment discussed here is Windows. Google provides a zip file and an executable to install SDK on Windows. Installing Eclipse on a Development Machine Eclipse was developed as an extensible, Integrated Development Environment (IDE) written in Java language. It requires an installation of Java to work. Eclipse comes in many flavors − Classic, Galelio, Luno, Mars, Neon, etc. It is distributed in a single .zip file. All you have to do is unpack the program contents. The extracted folder has an Eclipse.exe file. Double click the file to start the program. Print Page Previous Next Advertisements ”;

SL4A – Home

SL4A Tutorial PDF Version Quick Guide Resources Job Search Discussion The Scripting Layer for Android (SL4A) is a library that helps write scripts targeting the Android Platform. SL4A supports a number of scripting languages like Perl, Python, JRuby, PHP etc. It can also be extended to support new languages. While Java can be used for Android Development, it is a good thing when all you need to do is write a simple test case or turn on or off settings on your device. This tutorial will help you automate your Android device using simple Python scripts. Audience Android is the monopoly of Java enthusiasts. Well, if you are not a Java admirer and wondering, how you can develop Android Apps, here is something for your benefit. This tutorial introduces you to developing Android Applications using Python. Prerequisites Before you start proceeding with this tutorial, we are assuming that you are already aware about the Python. If you are not well aware of these concepts, then we will suggest you to go through our short tutorials on Python. Print Page Previous Next Advertisements ”;

SL4A – Building GUIs

SL4A – Building GUIs ”; Previous Next A Graphical User Interface (GUI) represents a set of graphical components that enables the user to browse, access and interact with the application functionalities. There are two basic approaches to user interaction with SL4A namely − Dialog boxes such as Alerts. Using HTML and JavaScript to build the UI and then Python behind the scenes to handle any additional processing. This chapter explains both the approaches. Python Dialog Box – Based GUIs SL4A includes a UI façade to access the basic UI elements provided by the android API. These functions return a result object as a named tuple when called. Each result is assigned a unique id for tracking purpose. The second element is the result. It represents the user’s input. The tuple also includes the third element error to provide feedback to the caller about any error condition that might have been encountered. If no errors were encountered this element is set to None. The following table lists the commonly used UiFacade functions. S.No. UiFacade Function & Description 1 dialogGetInput Queries the user for a text input. title (String) title of the input box (default = Value) message (String) message to display above the input box (default = Please enter value:) defaultText (String) text to insert into the input box (optional) The result is the user”s input, or None (null) if cancel was hit. 2 dialogShow Show dialog 3 dialogCreateAlert Create alert dialog. title (String)(optional) message (String) (optional) 4 dialogDismiss Dismiss dialog. 5 dialogCreateDatePicker Create date picker dialog. year (Integer) (default = 1970) month (Integer) (default = 1) day (Integer) (default = 1) 6 dialogCreateTimePicker Create time picker dialog. hour (Integer) (default = 0) minute (Integer) (default = 0) is24hour (Boolean) Use 24 hour clock (default = false) 7 dialogGetPassword Queries the user for a password. title (String) title of the password box (default = Password) message (String) message to display above the input box (default = Please enter password:) 8 dialogGetResponse Returns dialog response. 9 dialogSetPositiveButtonText Set alert dialog positive button text. text (String) 10 dialogSetNegativeButtonText Set alert dialog button text. text (String) 11 dialogSetNeutralButtonText Set alert dialog button text. text (String) 12 dialogSetSingleChoiceItems This creates a list of radio buttons. 13 dialogSetMultiChoiceItems This creates a checkbox 14 dialogCreateHorizontalProgress Create a horizontal progress dialog. title (String) (optional) message (String) (optional) maximum progress (Integer) (default = 100) 15 dialogCreateSpinnerProgress Create a spinner progress dialog. title (String) (optional) message (String) (optional) maximum progress (Integer) (default = 100) 16 addContexMenuItem Adds a new item to context menu. label (String) label for this menu item event (String) event that will be generated on menu item click eventData (Object) (optional) 17 addOptionsMenuItem Adds a new item to options menu. label (String) label for this menu item event (String) event that will be generated on menu item click eventData (Object) (optional) iconName (String) 18 webViewShow Display a WebView with the given URL. url (String) wait (Boolean) block until the user exits the WebView (optional) 19 clearContextMenu Removes all items previously added to context menu. 20 clearOptionsMenu Removes all items previously added to options menu 21 makeToast Create a notification Examples A simple Toast Example import android droid = android.Android() uname = droid.getInput(“Enter your name”) print uname droid.makeToast(“Hello %s” %uname.result) A Horizontal Progress Bar import android droid = android.Android() title = “Progress” str = “Loading…” droid.dialogCreateHorizontalProgress(title,str,100) droid.showDialog() for x in range(0,99) time.sleep(0.1) droid.dialogSetCurrentProgress(x) droid.dialogDismiss() Modal Vs Non-Modal Dialog Boxes A modal dialog box or window is a child to another process or window. With a modal dialog box, processing will wait or block until the user interacts with the new dialog box. A typical example of this case is an alert dialog box. The alert will not close until the user performs an action. The following image is an example of a modal dialog box. To sum up, use a modal dialog box when you need input from the user before you continue execution. Python GUIs with HTML SL4A enables building Graphical User Interfaces (GUIs) based on CSS, HTML, JavaScript and Python. The approach uses HTML and JavaScript to build the UI, CSS to enhance the appearance and consistency of HTML elements and fonts and Python to handle any additional processing. The following example illustrates a basic HTML GUI example − 1. Speech.html <html> <head> <title>Text To Speech</title> <script> var droid = new Android(); var speak = function(){ droid.postEvent(“say”,document.getElementById(“say”).value); } </script> </head> <body> <form onsubmit = “speak()”;return false;”> <label for = “say”>What is your message?</label> <input type = “text” id = “say”/> <input type = “submit” value = “Speak”/> </form> </body> </html> 2. txtToSpeech.py import android droid = android.Android() droid.webViewShow(‘file:///sdcard/sl4a/scripts/Speech.html’) while True: result = droid.waitForEvent(‘say’).result droid.ttsSpeak(result[‘data’]) Both the files − Speech.html and txtToSpeech.py, must reside in the /sdcard/sl4a/scripts directory on the device. Run the python script to launch the HTML file. This file is launched by the webViewShow API call. The event is generated when the Speak button is clicked. Print Page Previous Next Advertisements ”;

SL4A – Android Overview

SL4A – Android Overview ”; Previous Next Android is a Linux based operating system, designed primarily for portable devices like smartphones and tablet. Android is more than an operating system. It is a software stack that provides many features for users, developers and manufacturers. Android Version Features Android (Alpha) 1.0 First version of Android Android 1.5 (Cupcake) Added auto-rotation option, copy and paste feature added in the web browser Android 1.6 (Donut) Voice search and Search box were added Android 2.0/2.1 (Éclair) Bluetooth 2.1 support Android 2.2/2.2.3(Froyo) Support for Adobe Flash 10.1 Android 2.3/2.3.7 (Gingerbread) Internet calling, one touch word selection and copy/paste Android 3.0/3.2.6 (Honeycomb) This version is available only for tablets.Features include support for multi-core processors, ability to encrypt all user data Android 4.0/4.0.4 (Ice cream sandwich) Virtual button in the UI, ability to shut down apps that are using data in the background Android 4.1/4.3.1 (Jelly Bean) Based on Linux Kernel 3.0.31? Smoother User Interface, Bluetooth data transfer for Android Beam Android 4.4/4.4.4,4.4W/4.4W.2 (KitKat) Integration with cloud storage, Built-In sensors, Lock Art Screen Android 5.0/5.1.1 (Lollipop) Improved RAM and Battery Management, Fixes for sudden App closure, issues with wireless connections, notifications etc. It is open source and business friendly. The user can choose to replace built-in applications with applications of their choice. Many versions of Android have been released since its original release. Print Page Previous Next Advertisements ”;

SL4A – Scripting Layer for Android

SL4A – Scripting Layer for Android ”; Previous Next Android scripting is a handy tool for simple tasks such as automation and prototyping as against using the Java programming language for the same. The Scripting Layer for Android (SL4A) provides a platform for multiple scripting solutions on the android platform. What is SL4A? The Scripting Layer for Android (SL4A) is a library that provides a platform to write simple scripts to automate tasks on an android device using languages other than JAVA. SL4A, originally called Android Scripting Environment (ASE), was first announced on the Google Open Source blog in June 2009. The project saw the light of the day primarily through the efforts of Damon Kohler and it has grown through the contributions of other developers. It supports most popular scripting languages. These include − Beanshell 2.0b4 JRuby 1.4 Lua 5.1.4 Perl 5.10.1 PHP 5.3.3 Python Rhino 1.7R2 Javascript TCL Why SL4A – Scripting Languages Vs JAVA One of the first questions one would probably ask is, “Why use SL4A instead of Java?” Firstly, not everyone is a fan of Java. Scripting languages provide an easy programming environment as compared to Java. Secondly, the language requires the use of an edit/compile/run design loop. This means that you edit, re-compile and then run the application each time you desire some modification. On the contrary, scripts are interpreted and executed on the fly. Moreover, SL4A even makes it possible, in many cases, to reuse code written for a desktop environment. The following figure illustrates scripts written in Python targeting SL4A and in Java respectively. Print Page Previous Next Advertisements ”;