|
1 /* |
|
2 * JavaScript file |
|
3 */ |
|
4 |
|
5 function init() |
|
6 { |
|
7 widget.setDisplayPortrait(); |
|
8 watchSensorNotifications(); |
|
9 } |
|
10 |
|
11 // Call this function to add a callback that will be notified of orientation changes |
|
12 function watchSensorNotifications() { |
|
13 var sensors = device.getServiceObject("Service.Sensor", "ISensor"); |
|
14 var SensorParams = { |
|
15 SearchCriterion : "Orientation" // TODO Possible values (one of): |
|
16 // SearchCriterion : "All" |
|
17 // SearchCriterion : "AccelerometerAxis" |
|
18 // SearchCriterion : "AccelerometerDoubleTapping" |
|
19 // SearchCriterion : "Rotation" |
|
20 }; |
|
21 var result = sensors.ISensor.FindSensorChannel(SensorParams); |
|
22 |
|
23 if (result.ErrorCode != 0) { |
|
24 var errorCode = result.ErrorCode; |
|
25 var errorMessage = result.ErrorMessage; |
|
26 // TODO Handle error |
|
27 return null; |
|
28 } |
|
29 // TODO Function named "sensorCallback" will be called when device orientation changes. This function should be created. |
|
30 var result2 = sensors.ISensor.RegisterForNotification( |
|
31 { ChannelInfoMap : result.ReturnValue[0], |
|
32 ListeningType : "ChannelData" }, sensorCallback); |
|
33 if (result.ErrorCode == 0) { |
|
34 var transactionId = result.TransactionID; |
|
35 // TODO Use this transaction ID to cancel notifications when watching orientation is no longer needed |
|
36 return transactionId; |
|
37 } else { |
|
38 var errorCode = result.ErrorCode; |
|
39 var errorMessage = result.ErrorMessage; |
|
40 // TODO Handle error |
|
41 return null; |
|
42 } |
|
43 } |
|
44 |
|
45 // This function can be passed as callback to |
|
46 // sensors.ISensor.RegisterForNotification method |
|
47 function sensorCallback(transactionId, code, result) { |
|
48 if (result.ErrorCode == 0) { |
|
49 // TODO Process notification |
|
50 var dataType = result.ReturnValue.DataType; // One of: "AxisData", "DoubleTappingData", "OrientationData" or "RotationData" |
|
51 var orientation = result.ReturnValue.DeviceOrientation; // Orientation |
|
52 // var xAxis = result.ReturnValue.XAxisData; // Accelerometer |
|
53 // var yAxis = result.ReturnValue.YAxisData; // Accelerometer |
|
54 // var zAxis = result.ReturnValue.ZAxisData; // Accelerometer |
|
55 // var direction = result.ReturnValue.DeviceDirection; // Accelerometer double tapping |
|
56 // var xRotation = result.ReturnValue.XRotation; // Rotation |
|
57 // var yRotation = result.ReturnValue.YRotation; // Rotation |
|
58 // var zRotation = result.ReturnValue.ZRotation; // Rotation |
|
59 setVisibleDiv(orientation); |
|
60 } else { |
|
61 var errorCode = result.ErrorCode; |
|
62 var errorMessage = result.ErrorMessage; |
|
63 // TODO Handle error |
|
64 } |
|
65 } |
|
66 |
|
67 function setVisibleDiv(orientation) { |
|
68 var elements = document.getElementsByTagName("div"); |
|
69 for ( var i = 0; i < elements.length; i++) { |
|
70 var el = elements[i]; |
|
71 el.style.display = el.id == orientation ? "inherit" : "none"; |
|
72 } |
|
73 document.getElementById("orientation").innerHTML = orientation; |
|
74 } |