<commit_before>/****************************************************************************
**
** Copyright (c) 2013 - 2019 Jolla Ltd.
** Copyright (c) 2019 Open Mobile Platform LLC.
** Contact: <NAME> <<EMAIL>>
** Contact: <NAME> <<EMAIL>>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import QtQuick 2.0
import Nemo.KeepAlive 1.2
import Sailfish.WebEngine 1.0
import Nemo.DBus 2.0
import MeeGo.Connman 0.2
import org.nemomobile.policy 1.0
import Nemo.Connectivity 1.0

// QtObject cannot have children
Item {
    property QtObject webPage
    property bool videoActive
    property bool audioActive
    readonly property alias displayOff: screenBlanked.blanked
    property bool background

    property string _mediaState: "pause"
    property string _lastStateOwner
    property string _lastMetaOwner
    property bool _isAudioStream
    property bool _isVideoStream

    function calculateStatus() {
        var video = false
        var audio = false

        if (_mediaState === "play" && _lastStateOwner === _lastMetaOwner) {
            if (_isVideoStream) {
                video = true
            }
            if (_isAudioStream) {
                audio = true
            }
        }

        if (videoActive !== video) {
            videoActive = video
        }
        if (audioActive !== audio) {
            audioActive = audio
        }
    }

    function resumeView() {
        if (webPage) {
            webPage.resumeView()
        }
    }

    function suspendView() {
        if (webPage) {
            webPage.suspendView()
        }
    }

    onAudioActiveChanged: {
        if (!audioActive && screenBlanked.blanked) {
            delayedSuspend.suspendIntention = true
        }
    }

    // This is behind 1000ms timer
    onBackgroundChanged: {
        if (!audioActive && background) {
            suspendView()
        } else if (!background) {
            resumeView()
        }
    }

    Connections {
        target: WebEngine

        onInitialized: {
          WebEngine.addObserver("network-enable")
          connectionHelper.notifyOfflineStatus()
        }
        onRecvObserve: {
            if (message === "media-decoder-info") {
                if (data.state === "meta") {
                    _isAudioStream = data.a
                    _isVideoStream = data.v
                    _lastMetaOwner = data.owner
                } else {
                    _mediaState = data.state
                    _lastStateOwner = data.owner
                }
                calculateStatus()
            } else if (message === "network-enable") {
                connectionHelper.attemptToConnectNetwork()
            }
        }
    }

    ConnectionHelper {
        id: connectionHelper

        function notifyOfflineStatus() {
            if (WebEngine.initialized) {
                WebEngine.notifyObservers("embed-network-link-status",
                                          {
                                              "offline": !connectionHelper.online
                                          })
            }
        }

        onOnlineChanged: notifyOfflineStatus()
    }

    DBusInterface {
        id: screenBlanked

        property bool blanked

        function display_status_ind(state) {
            blanked = (state === "off")
        }

        bus: DBus.SystemBus
        service: 'com.nokia.mce'
        path: '/com/nokia/mce/signal'
        iface: 'com.nokia.mce.signal'
        signalsEnabled: true

        onBlankedChanged: {
            if (blanked && !audioActive) {
                delayedSuspend.suspendIntention = true
            } else {
                // Return immediately from suspend.
                resumeView()
                // We want also reset suspendIntention to false.
                delayedSuspend.suspendIntention = false
            }
        }
    }

    DisplayBlanking {
        // This is stopping screen blank timer.<commit_msg>[saifish-browser] Use connectivity to establish online status. Fixes JB#55628 OMP#JOLLA-401<commit_after>/****************************************************************************
**
** Copyright (c) 2013 - 2019 Jolla Ltd.
** Copyright (c) 2019 - 2021 Open Mobile Platform LLC.
** Contact: <NAME> <<EMAIL>>
** Contact: <NAME> <<EMAIL>>
**
****************************************************************************/

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

import QtQuick 2.0
import Nemo.KeepAlive 1.2
import Sailfish.WebEngine 1.0
import Nemo.DBus 2.0
import MeeGo.Connman 0.2
import org.nemomobile.policy 1.0
import Nemo.Connectivity 1.0

// QtObject cannot have children
Item {
    property QtObject webPage
    property bool videoActive
    property bool audioActive
    readonly property alias displayOff: screenBlanked.blanked
    property bool background

    property string _mediaState: "pause"
    property string _lastStateOwner
    property string _lastMetaOwner
    property bool _isAudioStream
    property bool _isVideoStream

    function calculateStatus() {
        var video = false
        var audio = false

        if (_mediaState === "play" && _lastStateOwner === _lastMetaOwner) {
            if (_isVideoStream) {
                video = true
            }
            if (_isAudioStream) {
                audio = true
            }
        }

        if (videoActive !== video) {
            videoActive = video
        }
        if (audioActive !== audio) {
            audioActive = audio
        }
    }

    function resumeView() {
        if (webPage) {
            webPage.resumeView()
        }
    }

    function suspendView() {
        if (webPage) {
            webPage.suspendView()
        }
    }

    onAudioActiveChanged: {
        if (!audioActive && screenBlanked.blanked) {
            delayedSuspend.suspendIntention = true
        }
    }

    // This is behind 1000ms timer
    onBackgroundChanged: {
        if (!audioActive && background) {
            suspendView()
        } else if (!background) {
            resumeView()
        }
    }

    Connections {
        target: WebEngine

        onInitialized: {
          WebEngine.addObserver("network-enable")
          connectionHelper.notifyOfflineStatus()
        }
        onRecvObserve: {
            if (message === "media-decoder-info") {
                if (data.state === "meta") {
                    _isAudioStream = data.a
                    _isVideoStream = data.v
                    _lastMetaOwner = data.owner
                } else {
                    _mediaState = data.state
                    _lastStateOwner = data.owner
                }
                calculateStatus()
            } else if (message === "network-enable") {
                connectionHelper.attemptToConnectNetwork()
            }
        }
    }

    ConnectionHelper {
        id: connectionHelper
        readonly property bool connected: status >= ConnectionHelper.Connected

        function notifyOfflineStatus() {
            if (WebEngine.initialized) {
                WebEngine.notifyObservers("embed-network-link-status",
                                          {
                                              "offline": !connectionHelper.connected
                                          })
            }
        }

        onConnectedChanged: notifyOfflineStatus()
    }

    DBusInterface {
        id: screenBlanked

        property bool blanked

        function display_status_ind(state) {
            blanked = (state === "off")
        }

        bus: DBus.SystemBus
        service: 'com.nokia.mce'
        path: '/com/nokia/mce/signal'
        iface: 'com.nokia.mce.signal'
        signalsEnabled: true

        onBlankedChanged: {
            if (blanked && !audioActive) {
                delayedSuspend.suspendIntention = true
            } else {
                // Return immediately from suspend.
                resumeView()
                // We want also reset suspendIntention to false.
                delayedSuspend.suspendIntention = false
            }
        }
    }

    DisplayBlanking {
        // This is stopping screen blank timer.
{'fraction_non_alphanumeric': 0.11755044580009386, 'fraction_numerical': 0.009385265133740028, 'mean_word_length': 1.6524579962663348, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5266747', 'n_tokens_mistral': 2236, 'n_tokens_neox': 2103, 'n_words': 547}