1 // Copyright (c) 2013 Intel Corporation. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.xwalk.runtime.extension.api.device_capabilities;
7 import android.util.Log;
9 import org.xwalk.runtime.extension.XWalkExtension;
10 import org.xwalk.runtime.extension.XWalkExtensionContext;
12 import org.json.JSONException;
13 import org.json.JSONObject;
15 public class DeviceCapabilities extends XWalkExtension {
16 public static final String NAME = "xwalk.experimental.system";
17 public static final String JS_API_PATH = "jsapi/device_capabilities_api.js";
19 private static final String TAG = "DeviceCapabilities";
21 private DeviceCapabilitiesCPU mCPU;
22 private DeviceCapabilitiesCodecs mCodecs;
23 private DeviceCapabilitiesDisplay mDisplay;
24 private DeviceCapabilitiesMemory mMemory;
25 private DeviceCapabilitiesStorage mStorage;
27 public DeviceCapabilities(String jsApiContent, XWalkExtensionContext context) {
28 super(NAME, jsApiContent, context);
30 mCPU = new DeviceCapabilitiesCPU(this, context);
31 mCodecs = new DeviceCapabilitiesCodecs(this, context);
32 mDisplay = new DeviceCapabilitiesDisplay(this, context);
33 mMemory = new DeviceCapabilitiesMemory(this, context);
34 mStorage = new DeviceCapabilitiesStorage(this, context);
37 private void handleMessage(int instanceID, String message) {
39 JSONObject jsonInput = new JSONObject(message);
40 String cmd = jsonInput.getString("cmd");
42 if (cmd.equals("addEventListener")) {
43 String eventName = jsonInput.getString("eventName");
44 handleAddEventListener(eventName);
46 String promiseId = jsonInput.getString("_promise_id");
47 handleGetDeviceInfo(instanceID, promiseId, cmd);
49 } catch (JSONException e) {
54 private void handleGetDeviceInfo(int instanceID, String promiseId, String cmd) {
56 JSONObject jsonOutput = new JSONObject();
57 if (cmd.equals("getCPUInfo")) {
58 jsonOutput.put("data", mCPU.getInfo());
59 } else if (cmd.equals("getCodecsInfo")) {
60 jsonOutput.put("data", mCodecs.getInfo());
61 } else if (cmd.equals("getDisplayInfo")) {
62 jsonOutput.put("data", mDisplay.getInfo());
63 } else if (cmd.equals("getMemoryInfo")) {
64 jsonOutput.put("data", mMemory.getInfo());
65 } else if (cmd.equals("getStorageInfo")) {
66 jsonOutput.put("data", mStorage.getInfo());
68 jsonOutput.put("_promise_id", promiseId);
69 this.postMessage(instanceID, jsonOutput.toString());
70 } catch (JSONException e) {
75 private void handleAddEventListener(String eventName) {
76 if (eventName.equals("storageattach") || eventName.equals("storagedetach")) {
77 mStorage.registerListener();
81 protected void printErrorMessage(JSONException e) {
82 Log.e(TAG, e.toString());
85 protected JSONObject setErrorMessage(String error) {
86 JSONObject out = new JSONObject();
87 JSONObject errorMessage = new JSONObject();
89 errorMessage.put("message", error);
90 out.put("error", errorMessage);
91 } catch (JSONException e) {
92 Log.e(TAG, e.toString());
98 public void onMessage(int instanceID, String message) {
99 if (!message.isEmpty()) {
100 handleMessage(instanceID, message);
105 public void onResume() {
111 public void onPause() {
117 public void onDestroy() {
118 mDisplay.onDestroy();
119 mStorage.onDestroy();