f9de5958aa5694e2884468f9ca310df93a1a3727
[apps/web/sample/SystemInfo.git] / project / js / main.js
1 /*
2  *      Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  *      Licensed under the Flora License, Version 1.1 (the "License");
5  *      you may not use this file except in compliance with the License.
6  *      You may obtain a copy of the License at
7  *
8  *              http://floralicense.org/license
9  *
10  *      Unless required by applicable law or agreed to in writing, software
11  *      distributed under the License is distributed on an "AS IS" BASIS,
12  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *      See the License for the specific language governing permissions and
14  *      limitations under the License.
15  */
16
17 var gInfoTitle = "", gInfo = "", gBatteryListener;
18
19 $(document).delegate("#main", "pageinit", function() {
20         $("#storage-info").bind("vclick", function() {
21                 getSystemProperty("STORAGE", onStorageSuccess);
22                 return false;
23         });
24         $("#battery-info").bind("vclick", function() {
25                 getSystemProperty("BATTERY", onBatterySuccess);
26                 return false;
27         });
28         $("#cpu-info").bind("vclick", function() {
29                 getSystemProperty("CPU", onCpuInfoSuccess);
30                 return false;
31         });
32         $("#display-info").bind("vclick", function() {
33                 getSystemProperty("DISPLAY", onDisplaySuccess);
34                 return false;
35         });
36
37         $("#orientation-info").bind("vclick", function() {
38                 getSystemProperty("DEVICE_ORIENTATION", onOrientationSuccess);
39                 return false;
40         });
41
42         $(window).on('tizenhwkey', function (e) {
43                 if (e.originalEvent.keyName === "back") {
44                         if ($.mobile.activePage.attr('id') === 'main') {
45                                 tizen.application.getCurrentApplication().exit();
46                         } else {
47                                 history.back();
48                         }
49                 }
50         });
51 });
52
53 $(document).delegate("#info", "pageinit", function() {
54         $("#info").bind("pagebeforeshow", function() {
55                 $("#info-title").html(gInfoTitle);
56                 $("#info-list").html(gInfo).trigger("create").listview("refresh");
57         });
58 });
59
60 function onError(e) {
61         alert("Error: " + e.message);
62 }
63
64 function make2lineListItem(title, value) {
65         return '<li class="ui-li-has-multiline ui-li-text-ellipsis">'
66                         + title
67                         + '<span class="ui-li-text-sub">'
68                         + value
69                         + '</span></li>';
70 }
71
72 function make1lineListItem(value) {
73         return '<li>' + value + '</li>';
74 }
75
76 function makeDividerListItem(value) {
77         return '<li data-role="list-divider">' + value + '</li>';
78 }
79
80 function onStorageSuccess(storages) {
81         gInfoTitle = "Storages (" + storages.units.length + ")";
82         gInfo = "";
83         for (var i = 0; i < storages.units.length; i++) {
84                 gInfo += makeDividerListItem("Storage " + (i + 1))
85                                 + make2lineListItem("Type", storages.units[i].type)
86                                 + make2lineListItem("Capacity", Math.floor(storages.units[i].capacity / 1000000) + " MB")
87                                 + make2lineListItem("Available capacity", Math.floor(storages.units[i].availableCapacity / 1000000) + " MB")
88                                 + make2lineListItem("Removable", (storages.units[i].isRemoveable == true ? "Yes" : "No"));
89         }
90
91         $.mobile.changePage("#info");
92 }
93
94 function onBatterySuccess(battery) {
95         gInfoTitle = "Battery";
96         gInfo = make2lineListItem("Level", battery.level)
97                         + make2lineListItem("Charging", (battery.isCharging == true ? "Yes" : "No"));
98
99         $.mobile.changePage("#info");
100 }
101
102 function onCpuInfoSuccess(cpu) {
103         gInfoTitle = "CPU";
104         gInfo = make2lineListItem("Load", cpu.load);
105
106         $.mobile.changePage("#info");
107 }
108
109 function onDisplaySuccess(display) {
110         gInfoTitle = "Display";
111         gInfo = makeDividerListItem("Resolution")
112                         + make2lineListItem("Width", display.resolutionWidth)
113                         + make2lineListItem("Height", display.resolutionHeight)
114                         + makeDividerListItem("Dots per inch")
115                         + make2lineListItem("Horizontal", display.dotsPerInchWidth)
116                         + make2lineListItem("Vertical", display.dotsPerInchHeight)
117                         + makeDividerListItem("Physical size")
118                         + make2lineListItem("Width", display.physicalWidth)
119                         + make2lineListItem("Height", display.physicalHeight)
120                         + makeDividerListItem("Brightness")
121                         + make1lineListItem(display.brightness);
122
123         $.mobile.changePage("#info");
124 }
125
126 function onDeviceSuccess(device) {
127         gInfoTitle = "Device";
128         gInfo = make2lineListItem("IMEI", device.imei)
129                         + make2lineListItem("Model", device.model)
130                         + make2lineListItem("Version", device.version)
131                         + make2lineListItem("Vendor", device.vendor);
132
133         $.mobile.changePage("#info");
134 }
135
136 function onOrientationSuccess(orientation) {
137         gInfoTitle = "Device orientation";
138         gInfo = make2lineListItem("Status", orientation.status);
139
140         $.mobile.changePage("#info");
141 }
142
143 function getSystemProperty(property, onSuccess) {
144         try {
145                 tizen.systeminfo.getPropertyValue(property, onSuccess, onError);
146         } catch (e) {
147                 alert("Exception: " + e.message);
148         }
149 }