6ccb6ffb7db7a0cf4d92808211705bbcbd2d3d5f
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / extension / api / device_capabilities / DeviceCapabilities.java
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.
4
5 package org.xwalk.core.internal.extension.api.device_capabilities;
6
7 import android.util.Log;
8
9 import org.xwalk.core.internal.extension.XWalkExtension;
10 import org.xwalk.core.internal.extension.XWalkExtensionContext;
11
12 import org.json.JSONException;
13 import org.json.JSONObject;
14
15 public class DeviceCapabilities extends XWalkExtension {
16     public static final String JS_API_PATH = "jsapi/device_capabilities_api.js";
17
18     private static final String TAG = "DeviceCapabilities";
19     private static final String NAME = "xwalk.experimental.system";
20
21     private DeviceCapabilitiesCPU mCPU;
22     private DeviceCapabilitiesCodecs mCodecs;
23     private DeviceCapabilitiesDisplay mDisplay;
24     private DeviceCapabilitiesMemory mMemory;
25     private DeviceCapabilitiesStorage mStorage;
26
27     public DeviceCapabilities(String jsApiContent, XWalkExtensionContext context) {
28         super(NAME, jsApiContent, context);
29
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);
35     }
36
37     private void handleMessage(int instanceID, String message) {
38         try {
39             JSONObject jsonInput = new JSONObject(message);
40             String cmd = jsonInput.getString("cmd");
41
42             if (cmd.equals("addEventListener")) {
43                 String eventName = jsonInput.getString("eventName");
44                 handleAddEventListener(eventName);
45             } else {
46                 String asyncCallId = jsonInput.getString("asyncCallId");
47                 handleGetDeviceInfo(instanceID, asyncCallId, cmd);
48             }
49         } catch (JSONException e) {
50             printErrorMessage(e);
51         }
52     }
53
54     private void handleGetDeviceInfo(int instanceID, String asyncCallId, String cmd) {
55         try {
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());
67             }
68             jsonOutput.put("asyncCallId", asyncCallId);
69             this.postMessage(instanceID, jsonOutput.toString());
70         } catch (JSONException e) {
71             printErrorMessage(e);
72         }
73     }
74
75     private void handleAddEventListener(String eventName) {
76         if (eventName.equals("storageattach") || eventName.equals("storagedetach")) {
77             mStorage.registerListener();
78         }
79     }
80
81     protected void printErrorMessage(JSONException e) {
82         Log.e(TAG, e.toString());
83     }
84
85     protected JSONObject setErrorMessage(String error) {
86         JSONObject out = new JSONObject();
87         JSONObject errorMessage = new JSONObject();
88         try {
89             errorMessage.put("message", error);
90             out.put("error", errorMessage);
91         } catch (JSONException e) {
92             Log.e(TAG, e.toString());
93         }
94         return out;
95     }
96
97     @Override
98     public void onMessage(int instanceID, String message) {
99         if (!message.isEmpty()) {
100             handleMessage(instanceID, message);
101         }
102     }
103
104     @Override
105     public void onResume() {
106         mDisplay.onResume();
107         mStorage.onResume();
108     }
109
110     @Override
111     public void onPause() {
112         mDisplay.onPause();
113         mStorage.onPause();
114     }
115
116     @Override
117     public void onDestroy() {
118         mDisplay.onDestroy();
119         mStorage.onDestroy();
120     }
121 }