Skip to content

Commit

Permalink
Add acceptableAssertionUsage and acceptableAssertionApplicationNames …
Browse files Browse the repository at this point in the history
…logic
  • Loading branch information
erikng committed May 24, 2022
1 parent 068f1b3 commit afb9741
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 2 deletions.
1 change: 1 addition & 0 deletions Example Assets/com.github.macadmins.Nudge.json
@@ -1,6 +1,7 @@
{
"optionalFeatures": {
"acceptableApplicationBundleIDs": [],
"acceptableAssertionUsage": false,
"acceptableCameraUsage": false,
"acceptableScreenSharingUsage": false,
"aggressiveUserExperience": true,
Expand Down
4 changes: 4 additions & 0 deletions Example Assets/com.github.macadmins.Nudge.mobileconfig
Expand Up @@ -23,6 +23,10 @@
<dict>
<key>acceptableApplicationBundleIDs</key>
<array/>
<key>acceptableAssertionApplicationNames</key>
<array/>
<key>acceptableAssertionUsage</key>
<false/>
<key>acceptableCameraUsage</key>
<false/>
<key>acceptableScreenSharingUsage</key>
Expand Down
7 changes: 7 additions & 0 deletions Example Assets/com.github.macadmins.Nudge.tester.json
@@ -1,5 +1,12 @@
{
"optionalFeatures": {
"acceptableAssertionApplicationNames": [
"zoom.us",
"Meeting Center",
"Google Chrome",
"Safari"
],
"acceptableAssertionUsage": true,
"acceptableCameraUsage": true,
"acceptableScreenSharingUsage": true,
"attemptToBlockApplicationLaunches": true,
Expand Down
2 changes: 2 additions & 0 deletions Nudge/Preferences/DefaultPreferencesNudge.swift
Expand Up @@ -32,6 +32,8 @@ let actionButtonPath = osVersionRequirementsProfile?.actionButtonPath ?? osVersi
let optionalFeaturesProfile = getOptionalFeaturesProfile()
let optionalFeaturesJSON = getOptionalFeaturesJSON()
let customAcceptableApplicationBundleIDs = optionalFeaturesProfile?["acceptableApplicationBundleIDs"] as? [String] ?? optionalFeaturesJSON?.acceptableApplicationBundleIDs ?? [String]()
let acceptableAssertionApplicationNames = optionalFeaturesProfile?["acceptableAssertionApplicationNames"] as? [String] ?? optionalFeaturesJSON?.acceptableAssertionApplicationNames ?? [String]()
let acceptableAssertionUsage = optionalFeaturesProfile?["acceptableAssertionUsage"] as? Bool ?? optionalFeaturesJSON?.acceptableAssertionUsage ?? false
let acceptableCameraUsage = optionalFeaturesProfile?["acceptableCameraUsage"] as? Bool ?? optionalFeaturesJSON?.acceptableCameraUsage ?? false
let acceptableScreenSharingUsage = optionalFeaturesProfile?["acceptableScreenSharingUsage"] as? Bool ?? optionalFeaturesJSON?.acceptableScreenSharingUsage ?? false
let aggressiveUserExperience = optionalFeaturesProfile?["aggressiveUserExperience"] as? Bool ?? optionalFeaturesJSON?.aggressiveUserExperience ?? true
Expand Down
9 changes: 7 additions & 2 deletions Nudge/Preferences/PreferencesStructure.swift
Expand Up @@ -58,8 +58,9 @@ extension NudgePreferences {

// MARK: - OptionalFeatures
struct OptionalFeatures: Codable {
var acceptableApplicationBundleIDs: [String]?
var acceptableCameraUsage,
var acceptableApplicationBundleIDs, acceptableAssertionApplicationNames: [String]?
var acceptableAssertionUsage,
acceptableCameraUsage,
acceptableScreenSharingUsage,
aggressiveUserExperience,
aggressiveUserFullScreenExperience,
Expand Down Expand Up @@ -91,6 +92,8 @@ extension OptionalFeatures {

func with(
acceptableApplicationBundleIDs: [String]?? = nil,
acceptableAssertionApplicationNames: [String]?? = nil,
acceptableAssertionUsage: Bool?? = nil,
acceptableCameraUsage: Bool?? = nil,
acceptableScreenSharingUsage: Bool?? = nil,
aggressiveUserExperience: Bool?? = nil,
Expand All @@ -104,6 +107,8 @@ extension OptionalFeatures {
) -> OptionalFeatures {
return OptionalFeatures(
acceptableApplicationBundleIDs: acceptableApplicationBundleIDs ?? self.acceptableApplicationBundleIDs,
acceptableAssertionApplicationNames: acceptableAssertionApplicationNames ?? self.acceptableAssertionApplicationNames,
acceptableAssertionUsage: acceptableAssertionUsage ?? self.acceptableAssertionUsage,
acceptableCameraUsage: acceptableCameraUsage ?? self.acceptableCameraUsage,
acceptableScreenSharingUsage: acceptableScreenSharingUsage ?? self.acceptableScreenSharingUsage,
aggressiveUserExperience: aggressiveUserExperience ?? self.aggressiveUserExperience,
Expand Down
1 change: 1 addition & 0 deletions Nudge/UI/Main.swift
Expand Up @@ -7,6 +7,7 @@

import UserNotifications
import SwiftUI

let windowDelegate = AppDelegate.WindowDelegate()
let dnc = DistributedNotificationCenter.default()
let nc = NotificationCenter.default
Expand Down
41 changes: 41 additions & 0 deletions Nudge/Utilities/UILogic.swift
Expand Up @@ -7,6 +7,22 @@

import AppKit
import Foundation
import IOKit.pwr_mgt

//class ADMARDConfig {
// static let rawType = NSClassFromString("ADMARDConfig") as! NSObject.Type
// let rawValue: NSObject
//
// init() {
// self.rawValue = Self.rawType.init() as! NSObject
// }
//
// required init(rawValue: NSObject) {
// guard rawValue.isKind(of: Self.rawType) else { fatalError() }
// self.rawValue = rawValue
// }
//}


// Start doing a basic check
func nudgeStartLogic() {
Expand Down Expand Up @@ -157,6 +173,31 @@ func needToActivateNudge() -> Bool {
uiLog.info("\("Ignoring Nudge activation - Screen sharing is currently active and not pastRequiredInstallationDate", privacy: .public)")
return false
}

// Don't nudge if assertions are set and prior to requiredInstallationDate
if acceptableAssertionUsage && !pastRequiredInstallationDate {
// Idea from https://github.com/saagarjha/vers/blob/d9460f6e14311e0a90c4c171975c93419481586b/vers/Headers.swift
// Bundle(path: "/System/Library/PrivateFrameworks/SystemAdministration.framework")!.load()
// print(ADMARDConfig().rawValue.value(forKey: "isScreenSharingOn"))
// Credit to https://github.com/francescofact/DualDimmer/blob/main/DualDimmer/Worker.swift
var assertions: Unmanaged<CFDictionary>?
if IOPMCopyAssertionsByProcess(&assertions) != kIOReturnSuccess {
uiLog.info("\("Could not assess assertions", privacy: .public)")
return true
}
let retainedAssertions = assertions?.takeRetainedValue()
for assertion in retainedAssertions.unsafelyUnwrapped as NSDictionary{
let assertionValues = (assertion.value as? NSArray).unsafelyUnwrapped
for value in assertionValues as! [NSDictionary]{
let processName = value["Process Name"] as? String ?? ""
let assertionType = value["AssertionTrueType"] as? String ?? ""
if acceptableAssertionApplicationNames.contains(processName) {
uiLog.info("\("Ignoring Nudge activation - Assertion \(assertionType) is set for \(processName)", privacy: .public)")
return false
}
}
}
}

// Don't nudge if refresh timer hasn't passed threshold
if (timerController > Int((Utils().getCurrentDate().timeIntervalSince1970 - nudgePrimaryState.lastRefreshTime.timeIntervalSince1970))) && nudgeLogState.afterFirstLaunch {
Expand Down
36 changes: 36 additions & 0 deletions Schema/jamf/com.github.macadmins.Nudge.json
Expand Up @@ -36,6 +36,42 @@
}
]
},
"acceptableAssertionApplicationNames": {
"description": "The application names using assertions which Nudge allows without taking focus. (You can specify one or more applications. To find the names please run /usr/bin/pmset -g assertions in terminal while the application is open and running)",
"anyOf": [
{
"title": "Not Configured",
"type": "null"
},
{
"title": "Configured",
"type": "array",
"items": {
"options": {
"inputAttributes": {
"placeholder": "zoom.us"
}
},
"type": "string",
"title": "acceptableAssertionApplicationNames"
}
}
]
},
"acceptableAssertionUsage": {
"description": "When enabled, Nudge will not activate or re-activate when assertions are currently set.",
"anyOf": [
{
"title": "Not Configured",
"type": "null"
},
{
"title": "Configured",
"default": false,
"type": "boolean"
}
]
},
"acceptableCameraUsage": {
"description": "When enabled, Nudge will not activate or re-activate when the camera is on.",
"anyOf": [
Expand Down

0 comments on commit afb9741

Please sign in to comment.