<!--
........................................................................................................................
Description:
    This rule script defines when to activate and deactivate orientation channel.

Feature:
    autorotateui

Dependencies:
    autorotateui_rotate.rul
........................................................................................................................
-->
<script xmlns="http://www.nokia.com/ns/cep/script/1.0/"
xmlns:cep="http://www.nokia.com/ns/cep/1.0/">

<!--
........................................................................................................................
Description:
    Activate orientation channel from sensor source.
........................................................................................................................
-->
    <if>
        <and>
            <!-- Check that the feature has not yet been enabled -->
            <notEqual>
                <contextRef source='Sensor' type='Event.AutoRotate'/>
                <string>Enabled</string>
            </notEqual>
            <or>
                <!-- Sensor setting has been enabled -->
                <and>
                    <!-- Turning interaction must be currently enabled -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                        <string>On</string>
                    </equals>
                    <!-- Sensor setting context value changes to 'On' -->
                    <contextChanged>
                        <contextRef source='Sensor' type='Setting.Active'/>
                    </contextChanged>
                    <equals>
                        <contextRef source='Sensor' type='Setting.Active'/>
                        <string>On</string>
                    </equals>
                </and>

                <!-- Turning interaction setting has been enabled -->
                <and>
                    <!-- Sensor setting must be currently enabled -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.Active'/>
                        <string>On</string>
                    </equals>
                    <!-- Turning interaction setting context value changes to ON -->
                    <contextChanged>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                    </contextChanged>
                    <equals>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                        <string>On</string>
                    </equals>
                </and>

                <!-- Events -->
                <and>
                    <!-- Sensor setting must be currently enabled -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.Active'/>
                        <string>On</string>
                    </equals>
                    <!-- Turning interaction setting context value changes to ON -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                        <string>On</string>
                    </equals>
                    <!-- Lights went on -->
                    <contextUpdated>
                        <contextRef source='Device' type='Display.Status.Light'/>
                    </contextUpdated>
                    <equals>
                        <contextRef source='Device' type='Display.Status.Light'/>
                        <string>On</string>
                    </equals>
                </and>
            </or>
        </and>
        <actions>
            <!-- Activate orientation channel from sensor source -->
            <publishContext>
                <contextRef source="Sensor" type="Event" value="Orientation.Activate"/>
            </publishContext>

            <!-- Mark feature as 'Enabled' -->
            <publishContext>
                <contextRef source="Sensor" type="Event.AutoRotate" value="Enabled"/>
            </publishContext>
        </actions>
    </if>

<!--
........................................................................................................................
Description:
    Deactivate orientation channel from sensor source.
........................................................................................................................
-->
    <elseIf>
        <and>
            <!-- Check that the feature has not already been disabled -->
            <notEqual>
                <contextRef source='Sensor' type='Event.AutoRotate'/>
                <string>Disabled</string>
            </notEqual>
            <or>
                <!-- Sensor setting has been disabled -->
                <and>
                    <!-- Sensor setting context value changes to 'Off' -->
                    <contextChanged>
                        <contextRef source='Sensor' type='Setting.Active'/>
                    </contextChanged>
                    <equals>
                        <contextRef source='Sensor' type='Setting.Active'/>
                        <string>Off</string>
                    </equals>
                </and>

                <!-- Turning interaction setting has been disabled -->
                <and>
                    <!-- Turning interaction setting context value changes to 'Off' -->
                    <contextChanged>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                    </contextChanged>
                    <equals>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                        <string>Off</string>
                    </equals>
                </and>

                <!-- Events -->
                <and>
                    <!-- Sensor setting must be currently enabled -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.Active'/>
                        <string>On</string>
                    </equals>
                    <!-- Turning interaction setting context value changes to ON -->
                    <equals>
                        <contextRef source='Sensor' type='Setting.TurningInteraction.AutoRotation'/>
                        <string>On</string>
                    </equals>
                    <!-- Lights went off -->
                    <contextUpdated>
                        <contextRef source='Device' type='Display.Status.Light'/>
                    </contextUpdated>
                    <notEqual>
                        <contextRef source='Device' type='Display.Status.Light'/>
                        <string>On</string>
                    </notEqual>
                </and>
            </or>
        </and>
        <actions>
            <!-- Deactivate orientation channel from sensor source -->
            <publishContext>
                <contextRef source="Sensor" type="Event" value="Orientation.Deactivate"/>
            </publishContext>

            <!-- Mark feature as 'Disabled' -->
            <publishContext>
                <contextRef source="Sensor" type="Event.AutoRotate" value="Disabled"/>
            </publishContext>
        </actions>
    </elseIf>

</script>
