introduction to sl4 a

31
. . . . . . Outline . . Introduction . . . . . . . . . . . . . . . . . . . . Usage . . . . . . . Examples INTRODUCTION TO SL4A louieli [email protected] September 4, 2012 louieli [email protected] INTRODUCTION TO SL4A

Upload: louieuser

Post on 01-Dec-2014

1.865 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

INTRODUCTION TO SL4A

louieli [email protected]

September 4, 2012

louieli [email protected]

INTRODUCTION TO SL4A

Page 2: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

1 Outline

2 IntroductionOverview of the SL4A

3 UsageInstallArchitectureusageSharing Scripts

4 Examplesdownloadhttpservertext to speech

louieli [email protected]

INTRODUCTION TO SL4A

Page 3: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Overview of the SL4A

Scripting Layer for Android (SL4A) brings scripting languages toAndroid by allowing you to edit and execute scripts and interactiveinterpreters directly on the Android device. These scripts haveaccess to many of the APIs available to full-fledged Androidapplications, but with a greatly simplified interface that makes iteasy to get things done.

louieli [email protected]

INTRODUCTION TO SL4A

Page 4: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Overview of the SL4A

louieli [email protected]

INTRODUCTION TO SL4A

Page 5: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Install

http://blog.csdn.net/louieuser/article/details/7912661http://blog.csdn.net/louieuser/article/details/7916280

louieli [email protected]

INTRODUCTION TO SL4A

Page 6: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Install

louieli [email protected]

INTRODUCTION TO SL4A

Page 7: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Install

louieli [email protected]

INTRODUCTION TO SL4A

Page 8: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Architecture

louieli [email protected]

INTRODUCTION TO SL4A

Page 9: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Architecture

louieli [email protected]

INTRODUCTION TO SL4A

Page 10: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Architecture

louieli [email protected]

INTRODUCTION TO SL4A

Page 11: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Architecture

All SL4A API calls return an object with three fields:

id: a strictly increasing, numeric id associated with the APIcall.

result: the return value of the API call, or null if there is noreturn value.

error: a description of any error that occurred or null if noerror occurred.

louieli [email protected]

INTRODUCTION TO SL4A

Page 12: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

Python

1 import android2

3 droid = android.Android()4 name = droid.getInput(”Hello!”, ”What is your name?”)5 print name # name is a namedtuple6 droid.makeToast(”Hello, %s” % name.result)

louieli [email protected]

INTRODUCTION TO SL4A

Page 13: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

Lua

1 require ”android”2

3 name = android.getInput(”Hello!”, ”What is your name?”)4 android.printDict(name)5 android.makeToast(”Hello, ” .. name.result)

louieli [email protected]

INTRODUCTION TO SL4A

Page 14: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

Perl

1 use Android;2 my $a = Android−>new();3 $a−>makeToast(”Hello, Android!”);

louieli [email protected]

INTRODUCTION TO SL4A

Page 15: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

JavaScript

1 load(”/sdcard/ase/extras/rhino/android.js”);2 var droid = new Android();3 droid.makeToast(”Hello, Android!”);

louieli [email protected]

INTRODUCTION TO SL4A

Page 16: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

JRuby

1 require ”android”2 droid = Android.new3 droid.makeToast ”Hello, Android!”

louieli [email protected]

INTRODUCTION TO SL4A

Page 17: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

BeanShell

1 source(”/sdcard/ase/extras/bsh/android.bsh”);2 droid = Android();3 droid.call(”makeToast”, ”Hello, Android!”);

louieli [email protected]

INTRODUCTION TO SL4A

Page 18: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

Tcl

1 package require android2 set android [android new]3 set name [$android getInput ”Hello!” ”What is your name?”]4 $android makeToast ”Hello, $name”

louieli [email protected]

INTRODUCTION TO SL4A

Page 19: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

http://code.google.com/p/android-scripting/wiki/ApiReference

louieli [email protected]

INTRODUCTION TO SL4A

Page 20: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

usage

1 #!/usr/bin/env python2 #Launch app.py3 import subprocess4 ADB = r’d:\Android\android−sdk\platform−tools\adb.exe’5 APPLICATION = ’speakit.py’6 TARGET = ’/sdcard/sl4a/scripts/’7

8 def main():9 # Upload the application.

10 subprocess.call([ADB, ’−e’, ’push’, APPLICATION, TARGET + APPLICATION])11 # Launch the application.12 subprocess.call(’”%s” −e shell am start \13 −a com.googlecode.android scripting.action.LAUNCH BACKGROUND SCRIPT \14 −n com.googlecode.android scripting/.activity.ScriptingLayerServiceLauncher \15 −e com.googlecode.android scripting.extra.SCRIPT PATH \16 ”%s%s”’ % (ADB, TARGET, APPLICATION))17

18 if name == ’ main ’:19 main()

louieli [email protected]

INTRODUCTION TO SL4A

Page 21: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Sharing Scripts

QR Code

1 Open http://zxing.appspot.com/generator/

2 Open the Contents drop down and choose Text.

3 On the first line of the Text Content, enter the name of thescript.

4 Below that, paste the script content.

5 Open the Size drop down and choose L.

6 Click Generate.

7 Embed or share the resulting barcode image with your friends.

louieli [email protected]

INTRODUCTION TO SL4A

Page 22: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Sharing Scripts

QR Code

louieli [email protected]

INTRODUCTION TO SL4A

Page 23: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Sharing Scripts

QR Code

To download the script to your phone:

1 Launch SL4A or return the scripts list.

2 Press the Menu button.

3 Tap Add.

4 Tap Scan Barcode.

5 Scan the barcode and SL4A will add the script to your list.

A QR code can encode 4,296 characters of content. So, this isonly effective for small scripts.

louieli [email protected]

INTRODUCTION TO SL4A

Page 24: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

Sharing Scripts

Scripts as APKs

https://code.google.com/p/android-scripting/wiki/SharingScripts#Scripts as APKs

louieli [email protected]

INTRODUCTION TO SL4A

Page 25: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

download

1 def reporthook(numblocks,blocksize,filesize,url=None):2 base = os.path.basename(url)3 try:4 percent = min((numblocks ∗ blocksize ∗ 100) / filesize, 100)5 except:6 percent = 1007 if numblocks != 0:8 droid.dialogSetMaxProgress(filesize)9 droid.dialogSetCurrentProgress(numblocks ∗ blocksize)

louieli [email protected]

INTRODUCTION TO SL4A

Page 26: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

download

1 def main():2 global droid3 droid = android.Android()4 url = ’http://code.jquery.com/jquery−1.8.1.js’5 if url is None: return6 dst = droid.dialogGetInput(’Filename’,’Save file as:’,7 os.path.basename(url)).result8 droid.dialogCreateHorizontalProgress(’Downloading...’,9 ’Saving %s from web.’ % dst)

10 droid.dialogShow()11 urllib.urlretrieve(url, downloads + dst,12 lambda nb, bs, fs, url=url: reporthook(nb,bs,fs,url))13 droid.dialogDismiss()14 droid.dialogCreateAlert(’Operation Finished’,15 ’%s has been saved to %s.’ % (url,downloads + dst))16 droid.dialogShow()

louieli [email protected]

INTRODUCTION TO SL4A

Page 27: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

download

louieli [email protected]

INTRODUCTION TO SL4A

Page 28: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

httpserver

1 import android, socket, SimpleHTTPServer, struct2 from os import chdir3

4 droid = android.Android()5

6 ipdec = droid.wifiGetConnectionInfo().result[’ip address’]7 ipstr = socket.inet ntoa(struct.pack(’L’,ipdec))8

9 chdir(’/sdcard/DCIM/’)10

11 print ”connect to %s” % ipstr12 SimpleHTTPServer.test()

louieli [email protected]

INTRODUCTION TO SL4A

Page 29: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

text to speech

1 <html>2 <head>3 <title>Text to Speech</title>4 <script>5 var droid = new Android();6 var speak = function() {7 droid.eventPost(”say”, document.getElementById(”say”).value);8 }9 </script>

10 </head>11 <body>12 <form onsubmit=”speak(); return false;”>13 <label for=”say”>What would you like to say?</label>14 <input type=”text” id=”say” />15 <input type=”submit” value=”Speak” />16 </form>17 </body>18 </html>

louieli [email protected]

INTRODUCTION TO SL4A

Page 30: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

text to speech

1 import android2 droid = android.Android()3

4 droid.webViewShow(’file:///sdcard/sl4a/scripts/text to speech.html’)5 while True:6 result = droid.eventWaitFor(’say’).result7 droid.ttsSpeak(result[’data’])8 print result9 break

louieli [email protected]

INTRODUCTION TO SL4A

Page 31: Introduction to sl4 a

. . . . . .

Outline. .Introduction

. . .

. . . .

. . . . . . . . .

. . . .

Usage. . ... . .

Examples

text to speech

louieli [email protected]

INTRODUCTION TO SL4A