SL4A - Exploring Android API


Advertisements


SL4A is based on the Facade Design Pattern to communicate with the underlying Android API. This chapter explains the working of Façade Design Pattern and the different SL4A API facades.

What is a Facade Design Pattern?

A facade is an object that provides an interface that simplifies the access to the related interfaces in the application. In other words, a façade insulates the working of the libraries from the client and acts as an entry point to each subsystem level. Thus, the clients communicate only through their facades. The following illustration explains the same.

Facade Design Pattern

SL4A and Android API Facades

SL4A hosts interpreters for each language it supports. The interpreters access the underlying Android APIs through an interface called a façade.

The following table lists the top-level facades and the functionality they provide −

S.No. Android API Facade & Description
1

ActivityResultFacade

It sets how your script will return information as an activity. This method implies that your script will return a result of some kind. The result type can be set to either RESULT_CANCELED(0) or RESULT_OK(-1)

2

AndroidFacade

It provides access to common Android functions like reading input, notifications, launching application components etc.

3

ApplicationManagerFacade

List all available and running packages, launch an activity, create your task manager or terminate a specific set of packages.

4

BatteryManagerFacade

Exposes the battery manager and allows tracking battery status, health, type level, voltage etc.

5

BluetoothFacade

Provides access to Bluetooth capabilities from basic connection features to sending and receiving both ASCII and binary data. This façade requires at least API level 5.

6

CameraFacade

This method provides access to all camera-related functions. Allows taking a picture using the device’s camera and saving it to a specified path. It provides two functions namely cameraCapturePicture and cameraInteractiveCapturePicture. These functions are strictly for using the lens on the rear of the device.

7

CommonIntentsFacade

Provides access to common Android Intents like opening a list of contacts, making a map search, starting an activity by action etc.

8

ContactsFacade

Allows access to contacts such as providing the contact list for picking a contact, querying the contact list by attributes and getting a list of all contacts with their IDs and attributes. Some methods provided by the façade include contactsGet, pickContact, contactsGetById etc.

9

EventFacade

The Android OS maintains an event queue for passing information between applications asynchronously. This façade provides functions to manage the event queue such as clearing the existing events, posting new events, listing, registering and unregistering broadcast signals etc. Examples of methods include eventPost, eventClearBuffer, etc.

10

EyesFreeFacade

Available on devices below API level 4. It allows scripts to speak using text-to-speech technology. This façade is now replaced by TextToSpeechFacade. The only available function is ttsSpeak.

11

LocationFacade

Provides functions that enables tracking the current location either by GPS or by using information about the cell tower you’re currently using. This feature requires an active internet connection to do the actual lookup. The façade provides methods like getLastKnownLocation, startLocating, stopLocating, readLocation etc.

12

MediaPlayerFacade

Allows playing media files, controlling the media player, querying the current status of the media player and getting information about the media files. mediaIsPlaying, mediaPlayInfo and mediaPlayList returns the current state of the media player. A call to the startActivity function launches the media player. Functions like mediaPlay, mediaPlayPause, mediaPlayClose, mediaPlaySeek,mediaPlayStart and mediaPlaySetLooping are used to control the media player.

13

MediaRecorderFacade

This façade provides audio and video recording capability. startInteractiveVideoRecording, recorderStartMicrophone, recorderCaptureVideo functions are used to launch and start audio/video recording respectively. To end a previously started recording call the recorderStop function.

14

PhoneFacade

Makes available basic phone operations like tracking phone state, roaming status, initiating calls, SIM information etc. programmatically. Examples of methods include phoneCallNumber, phoneDialNumber, getCellLocation etc.

15

PreferencesFacade

Allows access to shared preferences like getting the list of existing preferences and reading, modifying and adding new preferences. There are three functions supported by SL4A r4 release: prefGetAll, prefGetValue and prefPutValue.

16

SensorManagerFacade

Allows tracking sensor data such as light,acceleration, magnetic field and orientation. To start/ stop sensing use the startSensing (deprecated and replaced with startSensingThreshold and startSensingTimed by SL4r4) and stopSensing function calls. readSensors, sensorsGetLight, sensorsReadAccelerometer, sensorsReadMagnetometer and sensorsReadOrientation are the functions provided by this façade.

17

SettingsFacade

Provides access to different phone settings like ringer volume, screen brightness, airplane mode, vibration, media volume etc. Functions provided by this façade are checkAirplaneMode, checkRingersilentMode and checkScreenOn(atleast API level 7), getVibrateMode, setScreenTimeout, toggleVibrateMode setRingerVolume etc.

18

SignalStrengthFacade

Allows monitoring phone signal strength. Call the startTrackingSignalStrengths function to start gathering data. Call the readSignalStrengths function to start gathering data. To shut down the process call the stoptrackingSignalStrengths function. It requires at least API level 7.

19

SmsFacade

It has functions for deleting, reading, marking and sending SMS messages. Examples of functions provided by this façade include smsMarkMessageRead, smsDeleteMessage, smsSend etc.

20

SpeechRecognitionFacade

Enables adding speech recognition functionality to the script. It has only one function named recognizeSpeech.

21

TextToSpeechFacade

Provides TTS services for API4 and later. To have a device speak use the ttsSpeak function. To determine if the speak function has completed use the ttsIsSpeaking function.

22

ToneGeneratorFacade

Generates DTMF tones for given digits. To use it you must call the generateDtmfTones function.

23

UiFacade

Provides functions for creating user interface elements like textboxes, checkboxes, datepickers etc. It also allows interactive use of HTML pages.

24

WakeLockFacade

Provides functions to create a wake lock include wakeLockAcquireBright, wakeLockAcquireDim, wakeLockAcquireFull and wakelockAcquirePartial.

25

WebCamFacade

This façade requires at least API level 8. It allows streaming MJPEG streams from the front-facing device camera to the network. To start/stop the webcam use the webcamStart and the webcamStop functions respectively. To adjust the quality of the video while streaming use the webcamAdjustQuality function.

26

WifiFacade

Helps you to control the Wi-Fi radio on your device. It allows scripts to query the status of Wi-Fi connectivity, search for access points, connect to and disconnect WiFi networks and hold a Wi-Fi lock during script execution.

Refer https://code.google.com for a full list of methods provided by these facades. The subsequent chapters illustrate how to create utilities using Android Façade API functions.



Advertisements