Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

SL4A 的實現原理深入解析

關於 SL4A 的簡介,以及在 Android 系統上的安裝與使用,可以參考我的上一篇博文《Android 腳本設計之 SL4A》;本篇則來剖析它內部的實現機制。

深入理解 SL4A

SL4A 的架構實現了本地腳本與原生 Android 程式之間的內部消息通信,因此任何本地腳本語言,只要實作了這套相容的 JSON RPC 通信介面,就能呼叫 SL4A 的 RPC Server 端程式。至於為何選用 JSON,以及這種格式的優點與特徵,這裡就不細述了,大家可以查閱 JSON 官網。

“JavaScript Object Notation (JSON) is a lightweight, text-based,
language-independent data interchange format. It was derived from the
ECMAScript Programming Language Standard. JSON defines a small
set of formatting rules for the portable representation of structured data.
JSON can represent four primitive types (strings, numbers, booleans,
and null) and two structured types (objects and arrays).”

SL4A 總體架構

從上圖可以看出,SL4A 總體由 Client 與 Server 兩部分構成,用以實現通信以及整體架構的獨立性。Client 端負責解析本地腳本,這樣只要本地腳本實作了相容介面,就能方便地擴充腳本語言;而 Server 端則封裝了 Android 原生程式的設計,即便 Android 底層 API 發生變化,Client 端也基本不受影響。Client 把腳本中解析出來的函式呼叫透過 RPC 通信,遠程呼叫 Server 端的代理介面,再由 Server 呼叫原生 Android API(Android Facade 架構)來完成具體功能,呼叫結束後,Server 端再將執行結果回饋給 Client 端。

整個執行過程如下:

 -­‐-­‐ Script Interpreter -­‐-­‐-­‐-­‐ Client/Caller/Consumer Script -­‐-­‐-­‐-­‐-­‐-­‐ "Android" Script Object (locally wraps RPC calls) -­‐ Local Proxy -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ Remote Procedure Calls (RPC) – Exchanges contain a JSON payload -­‐-­‐-­‐-­‐-­‐-­‐ Android API Java Facade -­‐ Remote Proxy -­‐-­‐-­‐-­‐ API Server/Provider -­‐ Android Java application -­‐-­‐ The Android Platform itself

Local Proxy 的實現機制

其實每個本地模組都封裝實作了一個 Android 實體類,然後開放其呼叫介面,內部透過 RPC 與 SL4A Server 端通信。

Python 模組(android.py)之實現:

 

# Copyright (C) 2009 Google Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); you may not # use this file except in compliance with the License. You may obtain a copy of # the License at # # https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the # License for the specific language governing permissions and limitations under # the License. __author__ = 'Damon Kohler <damonkohler@gmail.com>' import collections import json import os import socket import sys PORT = os.environ.get('AP_PORT') HOST = os.environ.get('AP_HOST') HANDSHAKE = os.environ.get('AP_HANDSHAKE') Result = collections.namedtuple('Result', 'id,result,error') class Android(object): def __init__(self, addr=None): if addr is None: addr = HOST, PORT self.conn = socket.create_connection(addr) self.client = self.conn.makefile() self.id = 0 if HANDSHAKE is not None: self._authenticate(HANDSHAKE) def _rpc(self, method, *args): data = {'id': self.id, 'method': method, 'params': args} request = json.dumps(data) self.client.write(request+'\n') self.client.flush() response = self.client.readline() self.id += 1 result = json.loads(response) if result['error'] is not None: print result['error'] # namedtuple doesn't work with unicode keys. return Result(id=result['id'], result=result['result'], error=result['error'], ) def __getattr__(self, name): def rpc_call(*args): return self._rpc(name, *args) return rpc_call
Copyright © Linux教程網 All Rights Reserved