phpforandroid en

31
Ivan Mosquera <[email protected]> PHP for Android: PHP scripting on Android

Upload: ivmos

Post on 27-Jun-2015

304.852 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Phpforandroid en

Ivan Mosquera <[email protected]>

PHP for Android: PHP scripting on Android

Page 2: Phpforandroid en

PHP for Android: scripting PHP en Android

$self

● Iván Mosquera– ZCE– PHP, Java, C.– PHP for Android project founder.– @ivmos

● Irontec– Free software company.– Development/Systems/VoIp/Learning.– @irontec

Page 3: Phpforandroid en

PHP for Android: scripting PHP en Android

Slides

● http://www.slideshare.net/ivmos● http://www.slideshare.net/irontec

Page 4: Phpforandroid en

PHP for Android: scripting PHP en Android

Index

● Android.– Intro..– Architecture.

● PHP on Android● PHP compilation.● SL4A & PhpForAndroid.

● Example: script “WhereAmI”.– PFAf

● Development and sharing.● Questions.

Page 5: Phpforandroid en

PHP for Android: scripting PHP en Android

Android

● Mobile OS. ● Opensource.● 200.000 /day.● → tablet

Page 6: Phpforandroid en

PHP for Android: scripting PHP en Android

Android

Page 7: Phpforandroid en

PHP for Android: scripting PHP en Android

Android

Page 8: Phpforandroid en

PHP for Android: scripting PHP en Android

PHP on Android

● Android “is” Linux.● So it should work.

– Of course it does..● ARM cross compiling.● Method 1 (easy):

– Static compilation gnu libc (> 7MB)

Page 9: Phpforandroid en

PHP for Android: scripting PHP en Android

PHP en Android

● Not so easy way:– Linked to Bionic.

● Bionic: libc de Android– BSD.– sizeof(gnu_libc) * 0.5– Missing stuff.

● PHP on Android.– Php-cli sapi– Agcc – Tweaks because of bionic.– =~ 2MB– Sockets, json.

Page 10: Phpforandroid en

PHP for Android: scripting PHP en Android

The problem

● API php → OK● UI? (php-gtk?)● Calls?● Messages?● Location?● Sensors?● ...

● Mobile web.

Page 11: Phpforandroid en

PHP for Android: scripting PHP en Android

The solution

● Google project (Damon Kohler), opensource.● SL4A (Scripting Layer For Android) ● Android app (APK).● Layer for scripting languages.● Exposes Android API.● Really active, often releases.

Page 12: Phpforandroid en

PHP for Android: scripting PHP en Android

The solution

● Officially supported languages:– Python.– Perl.– Ruby.– Lua...

● Foreach interpreter → APK installer.● PHP? → issue #40 → phpforandroid.net

http://code.google.com/p/android-scripting/issues/detail?id=40

Page 13: Phpforandroid en

PHP for Android: scripting PHP en Android

The PFA project

● Http://www.phpforandroid.net● @phpforandroid● SL4A PHP support.● APK.● Android PHP binary.● Framework.● Free software.

Page 14: Phpforandroid en

PHP for Android: scripting PHP en Android

APKs

● APKs:– SL4A– PFA

● SL4A detects PFA(intent filter).

Page 15: Phpforandroid en

PHP for Android: scripting PHP en Android

PHP for Android

● Installer (that's all):– PHP bin.

● php_rX.zip

– Scripts● php_scripts_rX.zip

– Misc● Android.php● php.ini● php_extras_rX.zip

Page 16: Phpforandroid en

PHP for Android: scripting PHP en Android

SL4A

● Script management.● Android Facades.● Interpreter install.

Page 17: Phpforandroid en

PHP for Android: scripting PHP en Android

PHP for Android & SL4A

● 0.1 (ASE r26 unofficial, php 5.3.3rc2)● 0.2 (SL4A_r0, php 5.3.3rc3)● 0.3 (SL4A_r2+, php 5.3.3)

Page 18: Phpforandroid en

PHP for Android: scripting PHP en Android

The Android class

<?phpinclude 'Android.php';$droid = new Android();

public function __call($name, $args) { return $this->rpc($name, $args);}

Json RPC

Page 19: Phpforandroid en

PHP for Android: scripting PHP en Android

The Android class

public function rpc($method, $args) { $data = array( 'id'=>$this->_id, 'method'=>$method, 'params'=>$args ); $request = json_encode($data) . “\n”; $sent = socket_write($this->_socket, $request, strlen($request));

Page 20: Phpforandroid en

PHP for Android: scripting PHP en Android

The Android class

$response = socket_read($this->_socket, 1024, PHP_NORMAL_READ) or die("Could not read input\n"); $this->_id++; $result = json_decode($response);

return array ('id' => $result->id, 'result' => $result->result, 'error' => $result->error ); }

Page 21: Phpforandroid en

PHP for Android: scripting PHP en Android

The Android class

<?phprequire “Android.php”;$droid = new Android();$droid->viewContacts();

Page 22: Phpforandroid en

PHP for Android: scripting PHP en Android

“WhereAmI” script

● Location.● Email building (google maps link).

Page 23: Phpforandroid en

PHP for Android: scripting PHP en Android

“WhereAmI” script

$droid->startLocating();– Received: {"id":24,"method":"startLocating","params":[]}– Sent: {"error":null,"id":24,"result":null}

$location = $droid->readLocation();– Received: {"id":25,"method":"readLocation","params":[]}– Sent: {"error":null,"id":25,"result":{"network":

{"provider":"network","time":1288215482448,"longitude":-3.0339431,"latitude":43.3319985,"speed":0,"accuracy":76,"altitude":0}}}

Page 24: Phpforandroid en

PHP for Android: scripting PHP en Android

“WhereAmI” script

$droid->stopLocating();– Received: {"id":26,"method":"stopLocating","params":[]}– Sent: {"error":null,"id":26,"result":null}

$longitude = $location["result"]->network->longitude;$latitude = $location["result"]->network->latitude;$mapLink = sprintf('http://maps.google.com/maps?q=%s,%s', $latitude, $longitude);

Page 25: Phpforandroid en

PHP for Android: scripting PHP en Android

“WhereAmI” script

$data .= "Google maps : $mapLink";$droid->sendEmail('', 'Donde estoy', $data );

– Received: {"id":29,"method":"sendEmail","params":["","Donde estoy","admin_area : Pa\u00eds Vasco \nfeature_name : 3 \nsub_admin_area : Vizcaya \npostal_code : 48980 \nlocality : Santurtzi \nthoroughfare : Calle del Puerto Pesquero \ncountry_code : ES \ncountry_name : Espa\u00f1a \n\nGoogle Maps: http:\/\/maps.google.com\/maps?q=13.3319985, 6.0339431"]}

– Sent: {"error":null,"id":29,"result":null}

Page 26: Phpforandroid en

PHP for Android: scripting PHP en Android

PFAf

● Maciej Wilgucki @wilgucki● Framework avaiblable since PFA 0.3.● Extend ScriptAbstract, implement init.● Organize your script with states.● function FooAction() {

....$this->setNextAction('bar');

Page 27: Phpforandroid en

PHP for Android: scripting PHP en Android

PFAf

start

locate

credicts

info

Page 28: Phpforandroid en

PHP for Android: scripting PHP en Android

Development environment

● SL4A...– Emulator, adb.

● [allow_url_fopen, allow_url_include] → ON– testnow.php– testcache.php

● Remote scripting.

Page 29: Phpforandroid en

PHP for Android: scripting PHP en Android

Script sharing

● SL4A scripts folder.● Barcode (<4296 chars)● Script on APK:

– Template project → customize.– ANT

Page 30: Phpforandroid en

PHP for Android: scripting PHP en Android

Roadmap

● SL4A– API extended.

● GUI...

– Bugfixes.● PFA

– Sqlite,... phpize.– supporting SL4A releases.

Page 31: Phpforandroid en

PHP for Android: scripting PHP en Android

?????

Questions

Logo @Workoholics_