<commit_before>/*
 * Directive to enable the permissions required for the app to function properly.
 */

angular.module('emission.appstatus.permissioncheck',
    [])
.directive('permissioncheck', function() {
    return {
        scope: {
        },
        controller: "PermissionCheckControl",
        templateUrl: "templates/appstatus/permissioncheck.html"
    };
}).
controller("PermissionCheckControl", function($scope, $element, $attrs,
        $ionicPlatform, $ionicPopup, $window, $translate) {
    console.log("PermissionCheckControl initialized");

    $scope.setupLocChecks = function(platform, version) {
        if (platform.toLowerCase() == "android") {
            return $scope.setupAndroidLocChecks(version);
        } else if (platform.toLowerCase() == "ios") {
            return $scope.setupIOSLocChecks(version);
        } else {
            alert("Unknown platform, no tracking");
        }
    }

    $scope.setupFitnessChecks = function(platform, version) {
        if (platform.toLowerCase() == "android") {
            return $scope.setupAndroidFitnessChecks(version);
        } else if (platform.toLowerCase() == "ios") {
            return $scope.setupIOSFitnessChecks(version);
        } else {
            alert("Unknown platform, no tracking");
        }
    }

    $scope.recomputeLocStatus = function() {
        let iconMap = (statusState) => statusState? "✅" : "❌";
        let classMap = (statusState) => statusState? "status-green" : "status-red";
        $scope.locChecks.forEach((lc) => {
            lc.statusIcon = iconMap(lc.statusState);
            lc.statusClass = classMap(lc.statusState)
        });
        $scope.overallLocStatus = $scope.locChecks.map((lc) => lc.statusState).reduce((pv, cv) => pv && cv);
        console.log("overallLocStatus = "+$scope.overallLocStatus+" from ", $scope.locChecks);
        $scope.overallLocStatusIcon = iconMap($scope.overallLocStatus);
        $scope.overallLocStatusClass = classMap($scope.overallLocStatus);
    }

    $scope.setupAndroidLocChecks = function(platform, version) {
        let checkOrFixSettings = function(nativeFn, showError=true) {
            return nativeFn()
                .then((status) => {
                    console.log("availability ", status)
                    $scope.$apply(() => {
                        locSettingsCheck.statusState = true;
                        $scope.recomputeLocStatus();
                    });
                    return status;
                }).catch((error) => {
                    console.log("Error", error)
                    if (showError) {
                        $ionicPopup.alert({
                            title: "Error in location settings",
                            template: "<div class='item-text-wrap'>"+error+"</div>",
                            okText: "Please fix again"
                        });
                    };
                    $scope.$apply(() => {
                        locSettingsCheck.statusState = false;
                        $scope.recomputeLocStatus();
                    });
                    return error;
                });
        }
        let fixSettings = function() {
            console.log("Fix and refresh location settings");
            return checkOrFixSettings($window.cordova.plugins.BEMDataCollection.fixLocationSettings, showError=true);
        };
        let checkSettings = function() {
            console.log("Refresh location settings");
            return checkOrFixSettings($window.cordova.plugins.BEMDataCollection.isValidLocationSettings, showError=false);
        };
        let fixAndRefreshPermissionsPlaceholder = function() {
            console.log("fix or refresh location permissions");
        };
        var androidSettingsDescTag = "intro.appstatus.locsettings.description.android-gte-9";
        if (version < 9) {
            androidSettingsDescTag = "intro.appstatus.locsettings.description.android-lt-9";
        }
        var androidPermDescTag = "intro.appstatus.locperms.description.android-gte-11";
        if($scope.osver < 6) {
            androidPermDescTag = 'intro.appstatus.locperms.description.android-lt-6';
        } else if ($scope.osver < 10) {
            androidPermDescTag = "intro.appstatus.locperms.description.android-6-9";
        } else if ($scope.osver < 11) {
            androidPermDescTag= "intro.appstatus.locperms.description.android-10";
        }
        console.log("description tags are "+androidSettingsDescTag+" "+androidPermDescTag);
        // location settings
        let locSettingsCheck = {
            name: $translate.instant("intro.appstatus.locsettings.name"),
            desc: $translate.instant(androidSettingsDescTag),
            statusState: false,
            fix: fixSettings,
            refresh: checkSettings
        }
        let locPermissionsCheck = {
            name: $translate.instant("intro.appstatus.locperms.name"),
            desc: $translate.instant(androidPermDescTag),
            statusState: false,
            fix: fixAndRefreshPermissionsPlaceholder,
            refresh: fixAndRefreshPermissionsPlaceholder
        }
        $scope.locChecks = [locSettingsCheck, locPermissionsCheck];
        let locCheckPromises = $scope.locChecks.map((lc) => lc.refresh());
        console.log(locCheckPromises);
        Promise.all(locCheckPromises)
            .then((result) => $scope.recomputeLocStatus())
            .catch((error) => $scope.recomputeLocStatus())
    }

    $scope.setupAndroidFitnessChecks = function(platform, version) {
        $scope.fitnessPermNeeded = ($scope.osver >= 10);
  
        let fixAndRefreshPermissionsPlaceholder = function() {
            console.log("Fix and refresh fitnessation permissions");
        };
        let fitnessPermissionsCheck = {
            name: $translate.instant("intro.appstatus.fitnessperms.name"),
            desc: $translate.instant("intro.appstatus.fitnessperms.description.android"),<commit_msg>Add status support for the fitness sensors<commit_after>/*
 * Directive to enable the permissions required for the app to function properly.
 */

angular.module('emission.appstatus.permissioncheck',
    [])
.directive('permissioncheck', function() {
    return {
        scope: {
        },
        controller: "PermissionCheckControl",
        templateUrl: "templates/appstatus/permissioncheck.html"
    };
}).
controller("PermissionCheckControl", function($scope, $element, $attrs,
        $ionicPlatform, $ionicPopup, $window, $translate) {
    console.log("PermissionCheckControl initialized");

    $scope.setupLocChecks = function(platform, version) {
        if (platform.toLowerCase() == "android") {
            return $scope.setupAndroidLocChecks(version);
        } else if (platform.toLowerCase() == "ios") {
            return $scope.setupIOSLocChecks(version);
        } else {
            alert("Unknown platform, no tracking");
        }
    }

    $scope.setupFitnessChecks = function(platform, version) {
        if (platform.toLowerCase() == "android") {
            return $scope.setupAndroidFitnessChecks(version);
        } else if (platform.toLowerCase() == "ios") {
            return $scope.setupIOSFitnessChecks(version);
        } else {
            alert("Unknown platform, no tracking");
        }
    }

    $scope.recomputeLocStatus = function() {
        let iconMap = (statusState) => statusState? "✅" : "❌";
        let classMap = (statusState) => statusState? "status-green" : "status-red";
        $scope.locChecks.forEach((lc) => {
            lc.statusIcon = iconMap(lc.statusState);
            lc.statusClass = classMap(lc.statusState)
        });
        $scope.overallLocStatus = $scope.locChecks.map((lc) => lc.statusState).reduce((pv, cv) => pv && cv);
        console.log("overallLocStatus = "+$scope.overallLocStatus+" from ", $scope.locChecks);
        $scope.overallLocStatusIcon = iconMap($scope.overallLocStatus);
        $scope.overallLocStatusClass = classMap($scope.overallLocStatus);
    }

    $scope.setupAndroidLocChecks = function(platform, version) {
        let checkOrFix = function(checkObj, nativeFn, showError=true) {
            return nativeFn()
                .then((status) => {
                    console.log("availability ", status)
                    $scope.$apply(() => {
                        checkObj.statusState = true;
                        $scope.recomputeLocStatus();
                    });
                    return status;
                }).catch((error) => {
                    console.log("Error", error)
                    if (showError) {
                        $ionicPopup.alert({
                            title: "Error in location settings",
                            template: "<div class='item-text-wrap'>"+error+"</div>",
                            okText: "Please fix again"
                        });
                    };
                    $scope.$apply(() => {
                        checkObj.statusState = false;
                        $scope.recomputeLocStatus();
                    });
                    return error;
                });
        }
        let fixSettings = function() {
            console.log("Fix and refresh location settings");
            return checkOrFix(locSettingsCheck, $window.cordova.plugins.BEMDataCollection.fixLocationSettings, showError=true);
        };
        let checkSettings = function() {
            console.log("Refresh location settings");
            return checkOrFix(locSettingsCheck, $window.cordova.plugins.BEMDataCollection.isValidLocationSettings, showError=false);
        };
        let fixPerms = function() {
            console.log("fix and refresh location permissions");
            return checkOrFix(locPermissionsCheck, $window.cordova.plugins.BEMDataCollection.fixLocationPermissions, showError=true);
        };
        let checkPerms = function() {
            console.log("fix and refresh location permissions");
            return checkOrFix(locPermissionsCheck, $window.cordova.plugins.BEMDataCollection.isValidLocationPermissions, showError=false);
        };
        var androidSettingsDescTag = "intro.appstatus.locsettings.description.android-gte-9";
        if (version < 9) {
            androidSettingsDescTag = "intro.appstatus.locsettings.description.android-lt-9";
        }
        var androidPermDescTag = "intro.appstatus.locperms.description.android-gte-11";
        if($scope.osver < 6) {
            androidPermDescTag = 'intro.appstatus.locperms.description.android-lt-6';
        } else if ($scope.osver < 10) {
            androidPermDescTag = "intro.appstatus.locperms.description.android-6-9";
        } else if ($scope.osver < 11) {
            androidPermDescTag= "intro.appstatus.locperms.description.android-10";
        }
        console.log("description tags are "+androidSettingsDescTag+" "+androidPermDescTag);
        // location settings
        let locSettingsCheck = {
            name: $translate.instant("intro.appstatus.locsettings.name"),
            desc: $translate.instant(androidSettingsDescTag),
            statusState: false,
            fix: fixSettings,
            refresh: checkSettings
        }
        let locPermissionsCheck = {
            name: $translate.instant("intro.appstatus.locperms.name"),
            desc: $translate.instant(androidPermDescTag),
            statusState: false,
            fix: fixPerms,
            refresh: checkPerms
        }
        $scope.locChecks = [locSettingsCheck, locPermissionsCheck];
        let locCheckPromises = $scope.locChecks.map((lc) => lc.refresh());
        console.log(locCheckPromises);
        Promise.all(locCheckPromises)
            .then((result) => $scope.recomputeLocStatus())
            .catch((error) => $scope.recomputeLocStatus())
    }

    $scope.setupAndroidFitnessChecks = function(platform, version) {
        $scope.fitnessPermNeeded = ($scope.osver >= 10);
  
        let fixAndRefreshPermissionsPlaceholder = function() {
            console.log("Fix and refresh fitnessation permissions");
        };
        let fitnessPermissionsCheck = {
            name: $translate.instant("intro.appstatus.fitnessperms.name"),
            desc: $translate.instant("intro.appstatus.fitnessperms.description.android"),
{'fraction_non_alphanumeric': 0.11977557326394536, 'fraction_numerical': 0.0027646771832818343, 'mean_word_length': 2.2062043795620436, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '348390', 'n_tokens_mistral': 3225, 'n_tokens_neox': 3152, 'n_words': 552}