1e308b43a34c92eef2a0c242729586410804e50e
[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 /*jslint devel: true*/
18 /*global $, tizen, window*/
19
20 var gInfoTitle = '', gInfo = '', gBatteryListener;
21
22 $(document).delegate('#info', 'pageinit', function () {
23     'use strict';
24     $('#info').bind('pagebeforeshow', function () {
25         $('#info-title').html(gInfoTitle);
26         $('#info-list').html(gInfo).trigger('create').listview('refresh');
27     });
28 });
29
30 function onError(e) {
31     'use strict';
32     alert('Error: ' + e.message);
33 }
34
35 function make2lineListItem(title, value) {
36     'use strict';
37     return '<li class="ui-li-has-multiline ui-li-text-ellipsis">'
38         + title
39         + '<span class="ui-li-text-sub">'
40         + value
41         + '</span></li>';
42 }
43
44 function make1lineListItem(value) {
45     'use strict';
46     return '<li>' + value + '</li>';
47 }
48
49 function makeDividerListItem(value) {
50     'use strict';
51     return '<li data-role="list-divider">' + value + '</li>';
52 }
53
54 function onStorageSuccess(storages) {
55     'use strict';
56     var i;
57     gInfoTitle = 'Storages (' + storages.units.length + ')';
58     gInfo = '';
59     for (i = 0; i < storages.units.length; i = i + 1) {
60         gInfo += makeDividerListItem('Storage ' + (i + 1))
61             + make2lineListItem('Type', storages.units[i].type)
62             + make2lineListItem('Capacity',
63                 Math.floor(storages.units[i].capacity / 1000000) + ' MB')
64             + make2lineListItem('Available capacity',
65                 Math.floor(storages.units[i].availableCapacity / 1000000)
66                 + ' MB')
67             + make2lineListItem('Removable',
68                 (storages.units[i].isRemoveable === true ? 'Yes' : 'No'));
69     }
70
71     $.mobile.changePage('#info');
72 }
73
74 function onBatterySuccess(battery) {
75     'use strict';
76     gInfoTitle = 'Battery';
77     gInfo = make2lineListItem('Level', battery.level)
78         + make2lineListItem('Charging',
79             (battery.isCharging === true ? 'Yes' : 'No'));
80
81     $.mobile.changePage('#info');
82 }
83
84 function onCpuInfoSuccess(cpu) {
85     'use strict';
86     gInfoTitle = 'CPU';
87     gInfo = make2lineListItem('Load', cpu.load);
88
89     $.mobile.changePage('#info');
90 }
91
92 function onDisplaySuccess(display) {
93     'use strict';
94     gInfoTitle = 'Display';
95     gInfo = makeDividerListItem('Resolution')
96         + make2lineListItem('Width', display.resolutionWidth)
97         + make2lineListItem('Height', display.resolutionHeight)
98         + makeDividerListItem('Dots per inch')
99         + make2lineListItem('Horizontal', display.dotsPerInchWidth)
100         + make2lineListItem('Vertical', display.dotsPerInchHeight)
101         + makeDividerListItem('Physical size')
102         + make2lineListItem('Width', display.physicalWidth)
103         + make2lineListItem('Height', display.physicalHeight)
104         + makeDividerListItem('Brightness')
105         + make1lineListItem(display.brightness);
106
107     $.mobile.changePage('#info');
108 }
109
110 function onDeviceSuccess(device) {
111     'use strict';
112     gInfoTitle = 'Device';
113     gInfo = make2lineListItem('IMEI', device.imei)
114         + make2lineListItem('Model', device.model)
115         + make2lineListItem('Version', device.version)
116         + make2lineListItem('Vendor', device.vendor);
117
118     $.mobile.changePage('#info');
119 }
120
121 function onOrientationSuccess(orientation) {
122     'use strict';
123     gInfoTitle = 'Device orientation';
124     gInfo = make2lineListItem('Status', orientation.status);
125
126     $.mobile.changePage('#info');
127 }
128
129 function getSystemProperty(property, onSuccess) {
130     'use strict';
131     try {
132         tizen.systeminfo.getPropertyValue(property, onSuccess, onError);
133     } catch (e) {
134         alert('Exception: ' + e.message);
135     }
136 }
137
138 $(document).delegate('#main', 'pageinit', function () {
139     'use strict';
140     $('#storage-info').bind('vclick', function () {
141         getSystemProperty('STORAGE', onStorageSuccess);
142         return false;
143     });
144     $('#battery-info').bind('vclick', function () {
145         getSystemProperty('BATTERY', onBatterySuccess);
146         return false;
147     });
148     $('#cpu-info').bind('vclick', function () {
149         getSystemProperty('CPU', onCpuInfoSuccess);
150         return false;
151     });
152     $('#display-info').bind('vclick', function () {
153         getSystemProperty('DISPLAY', onDisplaySuccess);
154         return false;
155     });
156
157     $('#orientation-info').bind('vclick', function () {
158         getSystemProperty('DEVICE_ORIENTATION', onOrientationSuccess);
159         return false;
160     });
161
162     $(window).on('tizenhwkey', function (e) {
163         if (e.originalEvent.keyName === 'back') {
164             if ($.mobile.activePage.attr('id') === 'main') {
165                 tizen.application.getCurrentApplication().exit();
166             } else {
167                 history.back();
168             }
169         }
170     });
171 });
172