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