Php4android TDC 2011

1,436 views

Published on

Published in: Technology
  • Be the first to comment

Php4android TDC 2011

  1. 1. PHP for Android: PHP scripting on Android
  2. 2. $self ● Kinn Coelho Julião – Gerente de projetos - RPM – Instrutor PHP – 4Linux/DRC – PHP for Android Brasil project founder. – @kinncj
  3. 3. Index ● Android. – Introdução – Arquitetura ● PHP no Android ● Compilação do PHP ● SL4A & PhpForAndroid. ● Exemplo: script “WhereAmI”. – PFAf ● Desenvolvimento e compartilhamento ● Perguntas
  4. 4. Android ● Mobile OS. ● Opensource. ● 200.000 /dia ● -> tablet
  5. 5. Android
  6. 6. Android
  7. 7. PHP on Android ● Android “é” Linux. ● Então, deve funcionar – É claro que sim... ● ARM cross compiling. ● Método 1 (fécil): – Compilação estática gnu libc (> 7MB)
  8. 8. PHP on Android ● Caminho não tão fácil: – Lincado a libc Bionic. ● Bionic: libc do Android – BSD. – sizeof(gnu_libc) * 0.5 – Documentação incompleta. ● PHP no Android. – Php-cli sapi – Agcc – Tweaks devido o bionic. – =~ 2MB – Sockets, json.
  9. 9. The problem ● API php -> OK ● UI? (php-gtk?) ● Chamadas? ● Mensagens? ● Localização? ● Sensores? ● ... ● Mobile web.
  10. 10. The solution ● Google project (Damon Kohler), opensource. ● SL4A (Scripting Layer For Android) ● Android app (APK). ● Camada para linguagens Scripts ● Expõe a API Android ● Realmente ativos, liberam sempre
  11. 11. The solution ● Oficialmente suporta as linguagens: – Python. – Perl. – Ruby. – Lua... ● Para cada interpretador -> APK installer.
  12. 12. The PFA project ● ● @phpforandroid ● SL4A PHP support. ● APK. ● Android PHP binary. ● Framework. ● Free software. Http://www.phpforandroid.net
  13. 13. The PFA project ● http://www.php4android.com.br ● HowTo's ● News ● PT-BR Support
  14. 14. APKs ● APKs: – SL4A – PFA ● SL4A detecta PFA (Intenção de filtro)
  15. 15. PHP for Android ● Installer (that's all folks): – PHP bin. ● php_rX.zip – Scripts ● php_scripts_rX.zip – Misc ● Android.php ● php.ini ● php_extras_rX.zip
  16. 16. SL4A ● Gerenciamento dos Scripts. ● Android Facades. ● Instalação do interpretador.
  17. 17. 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)
  18. 18. The Android class <?php include 'Android.php'; $droid = new Android(); public function __call($name, $args) { return $this->rpc($name, $args); } Json RPC
  19. 19. 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));
  20. 20. The Android class $response = socket_read($this->_socket, 1024, PHP_NORMAL_READ) or die(&quot;Could not read inputn&quot;); $this->_id++; $result = json_decode ($response); return array ('id' => $result-> id , 'result' => $result-> result , 'error' => $result-> error ); }
  21. 21. The Android class <?php require “Android.php”; $droid = new Android(); $droid->viewContacts();
  22. 22. “ WhereAmI” script ● Localização ● Email building (link do maps).
  23. 23. “ WhereAmI” script $droid-> startLocating (); – Received: {&quot;id&quot;:24,&quot;method&quot;:&quot;startLocating&quot;,&quot;params&quot;:[]} – Sent: {&quot;error&quot;:null,&quot;id&quot;:24,&quot;result&quot;:null} $location = $droid-> readLocation (); – Received: {&quot;id&quot;:25,&quot;method&quot;:&quot;readLocation&quot;,&quot;params&quot;:[]} – Sent: {&quot;error&quot;:null,&quot;id&quot;:25,&quot;result&quot;:{&quot;network&quot;: {&quot;provider&quot;:&quot;network&quot;,&quot;time&quot;:1288215482448,&quot;longitud e&quot;:- 46.6167,&quot;latitude&quot;:-23.5333,&quot;speed&quot;:0,&quot;accuracy&quot; :76,&quot;altitude&quot;:0}}}
  24. 24. “ WhereAmI” script $droid-> stopLocating (); – Received: {&quot;id&quot;:26,&quot;method&quot;:&quot;stopLocating&quot;,&quot;params&quot;:[]} – Sent: {&quot;error&quot;:null,&quot;id&quot;:26,&quot;result&quot;:null} $longitude = $location[&quot;result&quot;]->network->longitude; $latitude = $location[&quot;result&quot;]->network->latitude; $mapLink = sprintf('http://maps.google.com/maps? q=%s,%s', $latitude, $longitude);
  25. 25. “ WhereAmI” script $data .= &quot;Google maps : $mapLink&quot;; $droid-> sendEmail ('', 'To aqui Rapeize ', $data ); – Received: {&quot;id&quot;:29,&quot;method&quot;:&quot;sendEmail&quot;,&quot;params&quot;: [&quot;&quot;,&quot;Donde estoy&quot;,&quot;admin_area : Sao Paulo nfeature_name : 3 nsub_admin_area : Sao Paulo npostal_code : 04002033 nlocality : Sao Paulo nthoroughfare : 4Linux ncountry_code : BR ncountry_name : Brazil nnGoogle Maps: http://maps.google.com/maps? q=-23.5333, -46.6167&quot;]} – Sent: {&quot;error&quot;:null,&quot;id&quot;:29,&quot;result&quot;:null}
  26. 26. PFAf ● Maciej Wilgucki @wilgucki ● Framework disponível desde o PFA 0.3. ● Estenda a parte Abstrada, implementa o init ● Organiza seu script com estados ● function FooAction() { .... $this->setNextAction('bar');
  27. 27. PFAf Início Créditos Informação
  28. 28. Development environment ● SL4A... – Emulador, adb. ● [allow_url_fopen, allow_url_include] -> ON – testnow.php – testcache.php ● Remote scripting.
  29. 29. Script sharing ● SL4A scripts folder. ● Barcode (<4296 chars) ● Script on APK: – Template project -> customize. – ANT
  30. 30. Roadmap ● SL4A – API extended. ● GUI... – Bugfixes. ● PFA – Sqlite,... phpize. – supporting SL4A releases.
  31. 31. ????? Perguntas? Logo @Workoholics_

×