mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
Update stuff
This commit is contained in:
parent
1d6946134c
commit
9214c61544
18 changed files with 985 additions and 118 deletions
|
|
@ -23,6 +23,20 @@ struct ExtensionLockScreenWidgetPayload: Identifiable, Hashable, Codable {
|
|||
var priority: AtollLiveActivityPriority { descriptor.priority }
|
||||
}
|
||||
|
||||
struct ExtensionNotchExperiencePayload: Identifiable, Hashable, Codable {
|
||||
let bundleIdentifier: String
|
||||
let descriptor: AtollNotchExperienceDescriptor
|
||||
let receivedAt: Date
|
||||
|
||||
var id: String { descriptor.id }
|
||||
|
||||
var priority: AtollLiveActivityPriority { descriptor.priority }
|
||||
|
||||
var hasTabConfiguration: Bool { descriptor.tab != nil }
|
||||
|
||||
var hasMinimalisticConfiguration: Bool { descriptor.minimalistic != nil }
|
||||
}
|
||||
|
||||
enum ExtensionValidationError: LocalizedError, Equatable {
|
||||
case featureDisabled
|
||||
case unauthorized
|
||||
|
|
|
|||
|
|
@ -22,13 +22,16 @@ final class ExtensionAuthorizationManager: ObservableObject {
|
|||
var isExtensionsFeatureEnabled: Bool { Defaults[.enableThirdPartyExtensions] }
|
||||
var areLiveActivitiesEnabled: Bool { Defaults[.enableExtensionLiveActivities] }
|
||||
var areLockScreenWidgetsEnabled: Bool { Defaults[.enableExtensionLockScreenWidgets] }
|
||||
var areNotchExperiencesEnabled: Bool { Defaults[.enableExtensionNotchExperiences] }
|
||||
|
||||
func updateFeatureToggles(extensionsEnabled: Bool? = nil,
|
||||
liveActivitiesEnabled: Bool? = nil,
|
||||
lockScreenWidgetsEnabled: Bool? = nil) {
|
||||
lockScreenWidgetsEnabled: Bool? = nil,
|
||||
notchExperiencesEnabled: Bool? = nil) {
|
||||
if let extensionsEnabled { Defaults[.enableThirdPartyExtensions] = extensionsEnabled }
|
||||
if let liveActivitiesEnabled { Defaults[.enableExtensionLiveActivities] = liveActivitiesEnabled }
|
||||
if let lockScreenWidgetsEnabled { Defaults[.enableExtensionLockScreenWidgets] = lockScreenWidgetsEnabled }
|
||||
if let notchExperiencesEnabled { Defaults[.enableExtensionNotchExperiences] = notchExperiencesEnabled }
|
||||
objectWillChange.send()
|
||||
}
|
||||
|
||||
|
|
@ -88,9 +91,12 @@ final class ExtensionAuthorizationManager: ObservableObject {
|
|||
record.activityTimestamps.append(now)
|
||||
case .lockScreenWidgets:
|
||||
record.widgetTimestamps.append(now)
|
||||
case .notchExperiences:
|
||||
record.notchExperienceTimestamps.append(now)
|
||||
}
|
||||
record.activityTimestamps = flushOldTimestamps(record.activityTimestamps)
|
||||
record.widgetTimestamps = flushOldTimestamps(record.widgetTimestamps)
|
||||
record.notchExperienceTimestamps = flushOldTimestamps(record.notchExperienceTimestamps)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -126,6 +132,12 @@ final class ExtensionAuthorizationManager: ObservableObject {
|
|||
return true
|
||||
}
|
||||
|
||||
func canProcessNotchExperienceRequest(from bundleIdentifier: String) -> Bool {
|
||||
guard preflight(bundleIdentifier: bundleIdentifier, scope: .notchExperiences) else { return false }
|
||||
guard areNotchExperiencesEnabled else { return false }
|
||||
return true
|
||||
}
|
||||
|
||||
func recordDeniedRequest(bundleIdentifier: String, reason: String) {
|
||||
updateEntry(bundleIdentifier: bundleIdentifier) { entry in
|
||||
entry.lastDeniedReason = reason
|
||||
|
|
|
|||
|
|
@ -30,4 +30,15 @@ struct ExtensionDescriptorValidator {
|
|||
throw ExtensionValidationError.invalidDescriptor("Widget exceeds size limits")
|
||||
}
|
||||
}
|
||||
|
||||
static func validate(_ descriptor: AtollNotchExperienceDescriptor) throws {
|
||||
guard descriptor.isValid else {
|
||||
throw ExtensionValidationError.invalidDescriptor("Missing mandatory fields")
|
||||
}
|
||||
if let durationHint = descriptor.durationHint {
|
||||
guard durationHint > 0, durationHint <= 21_600 else {
|
||||
throw ExtensionValidationError.invalidDescriptor("Duration hint must be between 0 and 6 hours")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,10 @@ final class ExtensionEventBridge {
|
|||
broadcast(payloads, channel: .lockScreenWidgets)
|
||||
}
|
||||
|
||||
func broadcastNotchExperienceSnapshot(_ payloads: [ExtensionNotchExperiencePayload]) {
|
||||
broadcast(payloads, channel: .notchExperiences)
|
||||
}
|
||||
|
||||
func loadPersistedLiveActivities() -> [ExtensionLiveActivityPayload] {
|
||||
loadSnapshot(channel: .liveActivities)
|
||||
}
|
||||
|
|
@ -31,6 +35,10 @@ final class ExtensionEventBridge {
|
|||
loadSnapshot(channel: .lockScreenWidgets)
|
||||
}
|
||||
|
||||
func loadPersistedNotchExperiences() -> [ExtensionNotchExperiencePayload] {
|
||||
loadSnapshot(channel: .notchExperiences)
|
||||
}
|
||||
|
||||
func observeLiveActivitySnapshots(_ handler: @escaping ([ExtensionLiveActivityPayload], Int32) -> Void) -> NSObjectProtocol {
|
||||
observe(channel: .liveActivities, handler: handler)
|
||||
}
|
||||
|
|
@ -39,6 +47,10 @@ final class ExtensionEventBridge {
|
|||
observe(channel: .lockScreenWidgets, handler: handler)
|
||||
}
|
||||
|
||||
func observeNotchExperienceSnapshots(_ handler: @escaping ([ExtensionNotchExperiencePayload], Int32) -> Void) -> NSObjectProtocol {
|
||||
observe(channel: .notchExperiences, handler: handler)
|
||||
}
|
||||
|
||||
func removeObserver(_ observer: NSObjectProtocol) {
|
||||
notificationCenter.removeObserver(observer)
|
||||
}
|
||||
|
|
@ -92,6 +104,8 @@ final class ExtensionEventBridge {
|
|||
return directory.appendingPathComponent("live_activities.json", isDirectory: false)
|
||||
case .lockScreenWidgets:
|
||||
return directory.appendingPathComponent("lock_screen_widgets.json", isDirectory: false)
|
||||
case .notchExperiences:
|
||||
return directory.appendingPathComponent("notch_experiences.json", isDirectory: false)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,6 +134,7 @@ final class ExtensionEventBridge {
|
|||
private enum Channel: CustomStringConvertible {
|
||||
case liveActivities
|
||||
case lockScreenWidgets
|
||||
case notchExperiences
|
||||
|
||||
var notificationName: Notification.Name {
|
||||
switch self {
|
||||
|
|
@ -127,6 +142,8 @@ final class ExtensionEventBridge {
|
|||
return Notification.Name("com.ebullioscopic.Atoll.extensions.liveActivitySnapshot")
|
||||
case .lockScreenWidgets:
|
||||
return Notification.Name("com.ebullioscopic.Atoll.extensions.lockScreenWidgetSnapshot")
|
||||
case .notchExperiences:
|
||||
return Notification.Name("com.ebullioscopic.Atoll.extensions.notchExperienceSnapshot")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -136,6 +153,8 @@ final class ExtensionEventBridge {
|
|||
return "live-activities"
|
||||
case .lockScreenWidgets:
|
||||
return "lock-screen-widgets"
|
||||
case .notchExperiences:
|
||||
return "notch-experiences"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,215 @@
|
|||
import Foundation
|
||||
import Defaults
|
||||
import AtollExtensionKit
|
||||
import SwiftUI
|
||||
|
||||
@MainActor
|
||||
final class ExtensionNotchExperienceManager: ObservableObject {
|
||||
static let shared = ExtensionNotchExperienceManager()
|
||||
|
||||
@Published private(set) var activeExperiences: [ExtensionNotchExperiencePayload] = []
|
||||
|
||||
private let authorizationManager = ExtensionAuthorizationManager.shared
|
||||
private let maxCapacityKey = Defaults.Keys.extensionNotchExperienceCapacity
|
||||
private let eventBridge = ExtensionEventBridge.shared
|
||||
private var observerToken: NSObjectProtocol?
|
||||
private var suppressBroadcast = false
|
||||
private let currentProcessID = ProcessInfo.processInfo.processIdentifier
|
||||
|
||||
private init() {
|
||||
activeExperiences = eventBridge.loadPersistedNotchExperiences()
|
||||
sortExperiences()
|
||||
observerToken = eventBridge.observeNotchExperienceSnapshots { [weak self] payloads, sourcePID in
|
||||
self?.applySnapshot(payloads, sourcePID: sourcePID)
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
if let observerToken {
|
||||
eventBridge.removeObserver(observerToken)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Presentation Lifecycle
|
||||
|
||||
func present(descriptor: AtollNotchExperienceDescriptor, bundleIdentifier: String) throws {
|
||||
guard authorizationManager.canProcessNotchExperienceRequest(from: bundleIdentifier) else {
|
||||
logDiagnostics("Rejected notch experience \(descriptor.id) from \(bundleIdentifier): scope disabled or bundle unauthorized")
|
||||
throw ExtensionValidationError.unauthorized
|
||||
}
|
||||
guard descriptor.bundleIdentifier == bundleIdentifier else {
|
||||
logDiagnostics("Rejected notch experience \(descriptor.id) from \(bundleIdentifier): bundle mismatch (descriptor: \(descriptor.bundleIdentifier))")
|
||||
throw ExtensionValidationError.invalidDescriptor("Bundle identifier mismatch")
|
||||
}
|
||||
|
||||
try ExtensionDescriptorValidator.validate(descriptor)
|
||||
try ensureWebContentSupport(for: descriptor)
|
||||
|
||||
let isUpdate: Bool
|
||||
if let index = activeExperiences.firstIndex(where: { $0.descriptor.id == descriptor.id && $0.bundleIdentifier == bundleIdentifier }) {
|
||||
let payload = ExtensionNotchExperiencePayload(
|
||||
bundleIdentifier: bundleIdentifier,
|
||||
descriptor: descriptor,
|
||||
receivedAt: activeExperiences[index].receivedAt
|
||||
)
|
||||
activeExperiences[index] = payload
|
||||
sortExperiences()
|
||||
isUpdate = true
|
||||
Logger.log("Replaced notch experience \(descriptor.id) for \(bundleIdentifier)", category: .extensions)
|
||||
} else {
|
||||
guard activeExperiences.count < Defaults[maxCapacityKey] else {
|
||||
logDiagnostics("Rejected notch experience \(descriptor.id) from \(bundleIdentifier): capacity limit \(Defaults[maxCapacityKey]) reached")
|
||||
throw ExtensionValidationError.exceedsCapacity
|
||||
}
|
||||
|
||||
let payload = ExtensionNotchExperiencePayload(
|
||||
bundleIdentifier: bundleIdentifier,
|
||||
descriptor: descriptor,
|
||||
receivedAt: .now
|
||||
)
|
||||
activeExperiences.append(payload)
|
||||
sortExperiences()
|
||||
isUpdate = false
|
||||
logDiagnostics("Queued notch experience \(descriptor.id) for \(bundleIdentifier); total experiences: \(activeExperiences.count)")
|
||||
}
|
||||
|
||||
authorizationManager.recordActivity(for: bundleIdentifier, scope: .notchExperiences)
|
||||
broadcastSnapshot()
|
||||
|
||||
if let tabConfig = descriptor.tab, Defaults[.enableExtensionNotchTabs] {
|
||||
Logger.log("Notch experience tab ready (title: \(tabConfig.title))", category: .extensions)
|
||||
}
|
||||
|
||||
if descriptor.minimalistic != nil && Defaults[.enableExtensionNotchMinimalisticOverrides] {
|
||||
Logger.log("Notch experience minimalistic override available", category: .extensions)
|
||||
}
|
||||
|
||||
if !isUpdate {
|
||||
logDiagnostics("Stored notch experience \(descriptor.id) for \(bundleIdentifier); priority \(descriptor.priority.rawValue)")
|
||||
}
|
||||
}
|
||||
|
||||
func update(descriptor: AtollNotchExperienceDescriptor, bundleIdentifier: String) throws {
|
||||
try ExtensionDescriptorValidator.validate(descriptor)
|
||||
guard descriptor.bundleIdentifier == bundleIdentifier else {
|
||||
logDiagnostics("Rejected notch experience update \(descriptor.id) from \(bundleIdentifier): bundle mismatch (descriptor: \(descriptor.bundleIdentifier))")
|
||||
throw ExtensionValidationError.invalidDescriptor("Bundle identifier mismatch")
|
||||
}
|
||||
guard let index = activeExperiences.firstIndex(where: { $0.descriptor.id == descriptor.id && $0.bundleIdentifier == bundleIdentifier }) else {
|
||||
throw ExtensionValidationError.invalidDescriptor("Missing existing notch experience")
|
||||
}
|
||||
try ensureWebContentSupport(for: descriptor)
|
||||
let payload = ExtensionNotchExperiencePayload(
|
||||
bundleIdentifier: bundleIdentifier,
|
||||
descriptor: descriptor,
|
||||
receivedAt: activeExperiences[index].receivedAt
|
||||
)
|
||||
activeExperiences[index] = payload
|
||||
sortExperiences()
|
||||
authorizationManager.recordActivity(for: bundleIdentifier, scope: .notchExperiences)
|
||||
logDiagnostics("Updated notch experience \(descriptor.id) for \(bundleIdentifier)")
|
||||
broadcastSnapshot()
|
||||
}
|
||||
|
||||
func dismiss(experienceID: String, bundleIdentifier: String) {
|
||||
let previousCount = activeExperiences.count
|
||||
withAnimation(.spring(response: 0.3, dampingFraction: 0.9)) {
|
||||
activeExperiences.removeAll { $0.descriptor.id == experienceID && $0.bundleIdentifier == bundleIdentifier }
|
||||
}
|
||||
guard previousCount != activeExperiences.count else { return }
|
||||
Logger.log("Dismissed notch experience \(experienceID) from \(bundleIdentifier)", category: .extensions)
|
||||
ExtensionXPCServiceHost.shared.notifyNotchExperienceDismiss(bundleIdentifier: bundleIdentifier, experienceID: experienceID)
|
||||
logDiagnostics("Removed notch experience \(experienceID) for \(bundleIdentifier); remaining: \(activeExperiences.count)")
|
||||
broadcastSnapshot()
|
||||
}
|
||||
|
||||
func dismissAll(for bundleIdentifier: String) {
|
||||
let ids = activeExperiences
|
||||
.filter { $0.bundleIdentifier == bundleIdentifier }
|
||||
.map { $0.descriptor.id }
|
||||
withAnimation(.spring(response: 0.3, dampingFraction: 0.9)) {
|
||||
activeExperiences.removeAll { $0.bundleIdentifier == bundleIdentifier }
|
||||
}
|
||||
ids.forEach { ExtensionXPCServiceHost.shared.notifyNotchExperienceDismiss(bundleIdentifier: bundleIdentifier, experienceID: $0) }
|
||||
if !ids.isEmpty {
|
||||
logDiagnostics("Removed all notch experiences for \(bundleIdentifier); ids: \(ids.joined(separator: ", "))")
|
||||
broadcastSnapshot()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Presentation Resolution
|
||||
|
||||
func highestPriorityTabPayload() -> ExtensionNotchExperiencePayload? {
|
||||
guard Defaults[.enableThirdPartyExtensions],
|
||||
Defaults[.enableExtensionNotchExperiences],
|
||||
Defaults[.enableExtensionNotchTabs] else {
|
||||
return nil
|
||||
}
|
||||
return activeExperiences.first(where: { $0.descriptor.tab != nil })
|
||||
}
|
||||
|
||||
func minimalisticReplacementPayload() -> ExtensionNotchExperiencePayload? {
|
||||
guard Defaults[.enableThirdPartyExtensions],
|
||||
Defaults[.enableExtensionNotchExperiences],
|
||||
Defaults[.enableExtensionNotchMinimalisticOverrides] else {
|
||||
return nil
|
||||
}
|
||||
return activeExperiences.first(where: { $0.descriptor.minimalistic != nil })
|
||||
}
|
||||
|
||||
func payload(bundleIdentifier: String, experienceID: String) -> ExtensionNotchExperiencePayload? {
|
||||
activeExperiences.first { $0.bundleIdentifier == bundleIdentifier && $0.descriptor.id == experienceID }
|
||||
}
|
||||
|
||||
func payload(experienceID: String) -> ExtensionNotchExperiencePayload? {
|
||||
activeExperiences.first { $0.descriptor.id == experienceID }
|
||||
}
|
||||
|
||||
// MARK: - Snapshot Sync
|
||||
|
||||
private func sortExperiences() {
|
||||
activeExperiences.sort(by: descriptorComparator)
|
||||
}
|
||||
|
||||
private func descriptorComparator(lhs: ExtensionNotchExperiencePayload, rhs: ExtensionNotchExperiencePayload) -> Bool {
|
||||
if lhs.priority == rhs.priority {
|
||||
return lhs.receivedAt < rhs.receivedAt
|
||||
}
|
||||
return lhs.priority > rhs.priority
|
||||
}
|
||||
|
||||
private func broadcastSnapshot() {
|
||||
guard !suppressBroadcast else { return }
|
||||
eventBridge.broadcastNotchExperienceSnapshot(activeExperiences)
|
||||
logDiagnostics("Broadcasted notch experience snapshot (count: \(activeExperiences.count))")
|
||||
}
|
||||
|
||||
private func applySnapshot(_ payloads: [ExtensionNotchExperiencePayload], sourcePID: Int32) {
|
||||
guard sourcePID != currentProcessID else { return }
|
||||
suppressBroadcast = true
|
||||
activeExperiences = payloads.sorted(by: descriptorComparator)
|
||||
suppressBroadcast = false
|
||||
logDiagnostics("Applied external notch experience snapshot from PID \(sourcePID) (count: \(payloads.count))")
|
||||
}
|
||||
|
||||
private func ensureWebContentSupport(for descriptor: AtollNotchExperienceDescriptor) throws {
|
||||
guard descriptor.hasWebContent else { return }
|
||||
guard Defaults[.enableExtensionNotchInteractiveWebViews] else {
|
||||
logDiagnostics("Rejected notch experience \(descriptor.id) due to web content while interactive web views are disabled")
|
||||
throw ExtensionValidationError.unsupportedContent
|
||||
}
|
||||
}
|
||||
|
||||
private func logDiagnostics(_ message: String) {
|
||||
guard Defaults[.extensionDiagnosticsLoggingEnabled] else { return }
|
||||
Logger.log(message, category: .extensions)
|
||||
}
|
||||
}
|
||||
|
||||
private extension AtollNotchExperienceDescriptor {
|
||||
var hasWebContent: Bool {
|
||||
if tab?.webContent != nil { return true }
|
||||
if minimalistic?.webContent != nil { return true }
|
||||
return false
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue