mirror of
https://github.com/FBukovina/Atoll.git
synced 2026-10-27 15:14:33 +00:00
BugFix: remove the accidental stats in gitignore
This commit is contained in:
parent
742e34fbdc
commit
05ec90e840
13 changed files with 1628 additions and 1 deletions
372
DynamicIsland/managers/CameraMonitor.swift
Normal file
372
DynamicIsland/managers/CameraMonitor.swift
Normal file
|
|
@ -0,0 +1,372 @@
|
|||
//
|
||||
// CameraMonitor.swift
|
||||
// DynamicIsland
|
||||
//
|
||||
// Created for camera privacy indicator feature
|
||||
// Monitors system camera usage with CoreMediaIO private APIs + AVFoundation fallback
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreMediaIO
|
||||
import AVFoundation
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
// MARK: - CMIOObject Property Listener (Event-driven approach)
|
||||
private func cameraPropertyListener(
|
||||
objectID: CMIOObjectID,
|
||||
numberAddresses: UInt32,
|
||||
addresses: UnsafePointer<CMIOObjectPropertyAddress>?,
|
||||
clientData: UnsafeMutableRawPointer?
|
||||
) -> OSStatus {
|
||||
guard let context = clientData else { return OSStatus(kCMIOHardwareNoError) }
|
||||
let monitor = Unmanaged<CameraMonitor>.fromOpaque(context).takeUnretainedValue()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
print("CameraMonitor: 📷 Camera property changed")
|
||||
monitor.checkCameraStatus()
|
||||
}
|
||||
|
||||
return OSStatus(kCMIOHardwareNoError)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
class CameraMonitor: ObservableObject {
|
||||
// MARK: - Published Properties
|
||||
@Published var isCameraActive: Bool = false
|
||||
@Published var activeApp: String? = nil
|
||||
@Published var isMonitoring: Bool = false
|
||||
|
||||
// MARK: - Private Properties
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
private var isListenerRegistered: Bool = false
|
||||
private var cameraDeviceIDs: [CMIOObjectID] = []
|
||||
|
||||
// MARK: - Configuration
|
||||
// Pure event-driven - no polling
|
||||
|
||||
// MARK: - Initialization
|
||||
init() {
|
||||
// No initial setup needed
|
||||
}
|
||||
|
||||
deinit {
|
||||
// Clean up listeners synchronously
|
||||
if isListenerRegistered, !cameraDeviceIDs.isEmpty {
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere),
|
||||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard)
|
||||
)
|
||||
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
for deviceID in cameraDeviceIDs {
|
||||
CMIOObjectRemovePropertyListener(
|
||||
deviceID,
|
||||
&propertyAddress,
|
||||
cameraPropertyListener,
|
||||
context
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Start monitoring camera usage
|
||||
func startMonitoring() {
|
||||
guard !isMonitoring else {
|
||||
print("CameraMonitor: Already monitoring, skipping start")
|
||||
return
|
||||
}
|
||||
|
||||
print("CameraMonitor: 🟢 Starting camera monitoring...")
|
||||
|
||||
isMonitoring = true
|
||||
|
||||
// Enumerate camera devices
|
||||
cameraDeviceIDs = enumerateCameraDevices()
|
||||
|
||||
if cameraDeviceIDs.isEmpty {
|
||||
print("CameraMonitor: ⚠️ No camera devices found")
|
||||
} else {
|
||||
print("CameraMonitor: 📷 Found \(cameraDeviceIDs.count) camera device(s)")
|
||||
}
|
||||
|
||||
// Setup event listener (CoreMediaIO approach)
|
||||
setupPropertyListener()
|
||||
|
||||
// Check initial state
|
||||
checkCameraStatus()
|
||||
|
||||
print("CameraMonitor: ✅ Started monitoring (event-driven CMIO + AVFoundation check)")
|
||||
}
|
||||
|
||||
/// Stop monitoring camera usage
|
||||
func stopMonitoring() {
|
||||
guard isMonitoring else {
|
||||
print("CameraMonitor: Not monitoring, skipping stop")
|
||||
return
|
||||
}
|
||||
|
||||
print("CameraMonitor: 🛑 Stopping monitoring...")
|
||||
|
||||
isMonitoring = false
|
||||
|
||||
// Remove property listener
|
||||
if isListenerRegistered {
|
||||
removePropertyListener()
|
||||
}
|
||||
|
||||
// Reset state
|
||||
if isCameraActive {
|
||||
isCameraActive = false
|
||||
}
|
||||
activeApp = nil
|
||||
|
||||
print("CameraMonitor: ✅ Stopped monitoring")
|
||||
}
|
||||
|
||||
/// Toggle monitoring state
|
||||
func toggleMonitoring() {
|
||||
if isMonitoring {
|
||||
stopMonitoring()
|
||||
} else {
|
||||
startMonitoring()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
/// Enumerate all camera devices using CoreMediaIO
|
||||
private func enumerateCameraDevices() -> [CMIOObjectID] {
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIOHardwarePropertyDevices),
|
||||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeGlobal),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMain)
|
||||
)
|
||||
|
||||
var dataSize: UInt32 = 0
|
||||
var status = CMIOObjectGetPropertyDataSize(
|
||||
CMIOObjectID(kCMIOObjectSystemObject),
|
||||
&propertyAddress,
|
||||
0,
|
||||
nil,
|
||||
&dataSize
|
||||
)
|
||||
|
||||
guard status == OSStatus(kCMIOHardwareNoError), dataSize > 0 else {
|
||||
print("CameraMonitor: ⚠️ Failed to get devices data size (status: \(status))")
|
||||
return []
|
||||
}
|
||||
|
||||
let deviceCount = Int(dataSize) / MemoryLayout<CMIOObjectID>.size
|
||||
var devices = [CMIOObjectID](repeating: 0, count: deviceCount)
|
||||
|
||||
status = CMIOObjectGetPropertyData(
|
||||
CMIOObjectID(kCMIOObjectSystemObject),
|
||||
&propertyAddress,
|
||||
0,
|
||||
nil,
|
||||
dataSize,
|
||||
&dataSize,
|
||||
&devices
|
||||
)
|
||||
|
||||
guard status == OSStatus(kCMIOHardwareNoError) else {
|
||||
print("CameraMonitor: ⚠️ Failed to get devices (status: \(status))")
|
||||
return []
|
||||
}
|
||||
|
||||
// Filter for video input devices
|
||||
let videoDevices = devices.filter { deviceID in
|
||||
return isVideoInputDevice(deviceID)
|
||||
}
|
||||
|
||||
return videoDevices
|
||||
}
|
||||
|
||||
/// Check if device is a video input device
|
||||
private func isVideoInputDevice(_ deviceID: CMIOObjectID) -> Bool {
|
||||
// Check if device has video input streams
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyStreams),
|
||||
mScope: CMIOObjectPropertyScope(kCMIODevicePropertyScopeInput),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementMain)
|
||||
)
|
||||
|
||||
var dataSize: UInt32 = 0
|
||||
let status = CMIOObjectGetPropertyDataSize(
|
||||
deviceID,
|
||||
&propertyAddress,
|
||||
0,
|
||||
nil,
|
||||
&dataSize
|
||||
)
|
||||
|
||||
return status == OSStatus(kCMIOHardwareNoError) && dataSize > 0
|
||||
}
|
||||
|
||||
/// Setup CoreMediaIO property listener
|
||||
private func setupPropertyListener() {
|
||||
guard !cameraDeviceIDs.isEmpty else { return }
|
||||
|
||||
// Listen for device running state changes
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere),
|
||||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard)
|
||||
)
|
||||
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
// Add listener for each camera device
|
||||
for deviceID in cameraDeviceIDs {
|
||||
let status = CMIOObjectAddPropertyListener(
|
||||
deviceID,
|
||||
&propertyAddress,
|
||||
cameraPropertyListener,
|
||||
context
|
||||
)
|
||||
|
||||
if status == OSStatus(kCMIOHardwareNoError) {
|
||||
print("CameraMonitor: ✅ Property listener registered for device \(deviceID)")
|
||||
isListenerRegistered = true
|
||||
} else {
|
||||
print("CameraMonitor: ⚠️ Failed to register listener for device \(deviceID) (status: \(status))")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove CoreMediaIO property listener
|
||||
private func removePropertyListener() {
|
||||
guard !cameraDeviceIDs.isEmpty else { return }
|
||||
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere),
|
||||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard)
|
||||
)
|
||||
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
for deviceID in cameraDeviceIDs {
|
||||
let status = CMIOObjectRemovePropertyListener(
|
||||
deviceID,
|
||||
&propertyAddress,
|
||||
cameraPropertyListener,
|
||||
context
|
||||
)
|
||||
|
||||
if status == OSStatus(kCMIOHardwareNoError) {
|
||||
print("CameraMonitor: ✅ Property listener removed for device \(deviceID)")
|
||||
} else {
|
||||
print("CameraMonitor: ⚠️ Failed to remove listener for device \(deviceID) (status: \(status))")
|
||||
}
|
||||
}
|
||||
|
||||
isListenerRegistered = false
|
||||
}
|
||||
|
||||
/// Check current camera status (CMIO approach)
|
||||
private func checkCameraStatusCMIO() -> Bool {
|
||||
guard !cameraDeviceIDs.isEmpty else { return false }
|
||||
|
||||
var propertyAddress = CMIOObjectPropertyAddress(
|
||||
mSelector: CMIOObjectPropertySelector(kCMIODevicePropertyDeviceIsRunningSomewhere),
|
||||
mScope: CMIOObjectPropertyScope(kCMIOObjectPropertyScopeWildcard),
|
||||
mElement: CMIOObjectPropertyElement(kCMIOObjectPropertyElementWildcard)
|
||||
)
|
||||
|
||||
// Check if any camera device is running
|
||||
for deviceID in cameraDeviceIDs {
|
||||
var isRunning: UInt32 = 0
|
||||
var dataSize = UInt32(MemoryLayout<UInt32>.size)
|
||||
|
||||
let status = CMIOObjectGetPropertyData(
|
||||
deviceID,
|
||||
&propertyAddress,
|
||||
0,
|
||||
nil,
|
||||
dataSize,
|
||||
&dataSize,
|
||||
&isRunning
|
||||
)
|
||||
|
||||
if status == OSStatus(kCMIOHardwareNoError) && isRunning != 0 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/// Check current camera status (AVFoundation fallback)
|
||||
private func checkCameraStatusAVFoundation() -> Bool {
|
||||
// Use AVFoundation to check if any camera is in use
|
||||
let discoverySession = AVCaptureDevice.DiscoverySession(
|
||||
deviceTypes: [.builtInWideAngleCamera, .external],
|
||||
mediaType: .video,
|
||||
position: .unspecified
|
||||
)
|
||||
|
||||
for device in discoverySession.devices {
|
||||
if device.isInUseByAnotherApplication {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
/// Check current camera status (hybrid approach)
|
||||
func checkCameraStatus() {
|
||||
// Try CMIO first, fallback to AVFoundation
|
||||
let isCMIOActive = checkCameraStatusCMIO()
|
||||
let isAVActive = checkCameraStatusAVFoundation()
|
||||
|
||||
// Use OR logic - either method detects usage
|
||||
let isActive = isCMIOActive || isAVActive
|
||||
|
||||
// Debug logging
|
||||
print("CameraMonitor: 🔍 Checking... current=\(isCameraActive), CMIO=\(isCMIOActive), AV=\(isAVActive), final=\(isActive)")
|
||||
|
||||
// Update state if changed
|
||||
if isActive != isCameraActive {
|
||||
print("CameraMonitor: 🔄 State change detected (\(isCameraActive) -> \(isActive))")
|
||||
|
||||
withAnimation(.smooth) {
|
||||
isCameraActive = isActive
|
||||
}
|
||||
|
||||
if isActive {
|
||||
print("CameraMonitor: 📷 Camera ACTIVE")
|
||||
// Could try to identify app here (TODO: investigate)
|
||||
activeApp = "Unknown App"
|
||||
} else {
|
||||
print("CameraMonitor: ⚪ Camera INACTIVE")
|
||||
activeApp = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
extension CameraMonitor {
|
||||
/// Get current camera status without async
|
||||
var currentCameraStatus: Bool {
|
||||
return isCameraActive
|
||||
}
|
||||
|
||||
/// Check if monitoring is available
|
||||
var isMonitoringAvailable: Bool {
|
||||
return !enumerateCameraDevices().isEmpty || !AVCaptureDevice.DiscoverySession(
|
||||
deviceTypes: [.builtInWideAngleCamera, .external],
|
||||
mediaType: .video,
|
||||
position: .unspecified
|
||||
).devices.isEmpty
|
||||
}
|
||||
}
|
||||
315
DynamicIsland/managers/MicrophoneMonitor.swift
Normal file
315
DynamicIsland/managers/MicrophoneMonitor.swift
Normal file
|
|
@ -0,0 +1,315 @@
|
|||
//
|
||||
// MicrophoneMonitor.swift
|
||||
// DynamicIsland
|
||||
//
|
||||
// Created for microphone privacy indicator feature
|
||||
// Monitors system microphone usage with event-driven CoreAudio property listeners
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import CoreAudio
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
// MARK: - CoreAudio Callback Function
|
||||
// C function pointer for CoreAudio property listener
|
||||
private func microphonePropertyListener(
|
||||
inObjectID: AudioObjectID,
|
||||
inNumberAddresses: UInt32,
|
||||
inAddresses: UnsafePointer<AudioObjectPropertyAddress>,
|
||||
inClientData: UnsafeMutableRawPointer?
|
||||
) -> OSStatus {
|
||||
guard let context = inClientData else { return noErr }
|
||||
let monitor = Unmanaged<MicrophoneMonitor>.fromOpaque(context).takeUnretainedValue()
|
||||
|
||||
DispatchQueue.main.async {
|
||||
print("MicrophoneMonitor: 📢 Microphone property changed")
|
||||
monitor.checkMicrophoneStatus()
|
||||
}
|
||||
|
||||
return noErr
|
||||
}
|
||||
|
||||
@MainActor
|
||||
class MicrophoneMonitor: ObservableObject {
|
||||
// MARK: - Published Properties
|
||||
@Published var isMicActive: Bool = false
|
||||
@Published var activeApp: String? = nil
|
||||
@Published var isMonitoring: Bool = false
|
||||
|
||||
// MARK: - Private Properties
|
||||
private var defaultInputDevice: AudioDeviceID = 0
|
||||
private var isListenerRegistered: Bool = false
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
// MARK: - Configuration
|
||||
// Pure event-driven - no polling
|
||||
|
||||
// MARK: - Initialization
|
||||
init() {
|
||||
// No initial setup needed
|
||||
}
|
||||
|
||||
deinit {
|
||||
// Clean up listener synchronously
|
||||
// Remove property listener
|
||||
if isListenerRegistered, defaultInputDevice != 0 {
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioDevicePropertyDeviceIsRunning,
|
||||
mScope: kAudioDevicePropertyScopeInput,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
AudioObjectRemovePropertyListener(
|
||||
defaultInputDevice,
|
||||
&address,
|
||||
microphonePropertyListener,
|
||||
context
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Start monitoring microphone usage
|
||||
func startMonitoring() {
|
||||
guard !isMonitoring else {
|
||||
print("MicrophoneMonitor: Already monitoring, skipping start")
|
||||
return
|
||||
}
|
||||
|
||||
print("MicrophoneMonitor: 🟢 Starting microphone monitoring...")
|
||||
|
||||
isMonitoring = true
|
||||
|
||||
// Get default input device
|
||||
defaultInputDevice = getDefaultInputDevice()
|
||||
guard defaultInputDevice != 0 else {
|
||||
print("MicrophoneMonitor: ⚠️ No input device found")
|
||||
return
|
||||
}
|
||||
|
||||
print("MicrophoneMonitor: 🎤 Found input device ID: \(defaultInputDevice)")
|
||||
|
||||
// Check if property exists
|
||||
let propertyExists = checkPropertyExists()
|
||||
print("MicrophoneMonitor: Property exists: \(propertyExists)")
|
||||
|
||||
// Setup event listener
|
||||
setupPropertyListener()
|
||||
|
||||
// Check initial state
|
||||
checkMicrophoneStatus()
|
||||
|
||||
print("MicrophoneMonitor: ✅ Started monitoring (event-driven only)")
|
||||
}
|
||||
|
||||
/// Stop monitoring microphone usage
|
||||
func stopMonitoring() {
|
||||
guard isMonitoring else {
|
||||
print("MicrophoneMonitor: Not monitoring, skipping stop")
|
||||
return
|
||||
}
|
||||
|
||||
print("MicrophoneMonitor: 🛑 Stopping monitoring...")
|
||||
|
||||
isMonitoring = false
|
||||
|
||||
// Remove property listener
|
||||
if isListenerRegistered {
|
||||
removePropertyListener()
|
||||
}
|
||||
|
||||
// Reset state
|
||||
if isMicActive {
|
||||
isMicActive = false
|
||||
}
|
||||
activeApp = nil
|
||||
|
||||
print("MicrophoneMonitor: ✅ Stopped monitoring")
|
||||
}
|
||||
|
||||
/// Toggle monitoring state
|
||||
func toggleMonitoring() {
|
||||
if isMonitoring {
|
||||
stopMonitoring()
|
||||
} else {
|
||||
startMonitoring()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Private Methods
|
||||
|
||||
/// Get default input device ID
|
||||
private func getDefaultInputDevice() -> AudioDeviceID {
|
||||
var deviceID: AudioDeviceID = 0
|
||||
var size = UInt32(MemoryLayout<AudioDeviceID>.size)
|
||||
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioHardwarePropertyDefaultInputDevice,
|
||||
mScope: kAudioObjectPropertyScopeGlobal,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
let status = AudioObjectGetPropertyData(
|
||||
AudioObjectID(kAudioObjectSystemObject),
|
||||
&address,
|
||||
0,
|
||||
nil,
|
||||
&size,
|
||||
&deviceID
|
||||
)
|
||||
|
||||
if status != noErr {
|
||||
print("MicrophoneMonitor: ⚠️ Failed to get default input device (status: \(status))")
|
||||
return 0
|
||||
}
|
||||
|
||||
return deviceID
|
||||
}
|
||||
|
||||
/// Check if the property exists on the device
|
||||
private func checkPropertyExists() -> Bool {
|
||||
guard defaultInputDevice != 0 else { return false }
|
||||
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioDevicePropertyDeviceIsRunning,
|
||||
mScope: kAudioDevicePropertyScopeInput,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
let hasProperty = AudioObjectHasProperty(defaultInputDevice, &address)
|
||||
print("MicrophoneMonitor: Device \(defaultInputDevice) has property: \(hasProperty)")
|
||||
|
||||
return hasProperty
|
||||
}
|
||||
|
||||
/// Setup CoreAudio property listener
|
||||
private func setupPropertyListener() {
|
||||
guard defaultInputDevice != 0 else { return }
|
||||
|
||||
// Try kAudioDevicePropertyDeviceIsRunning (simpler, more reliable for input devices)
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioDevicePropertyDeviceIsRunning,
|
||||
mScope: kAudioDevicePropertyScopeInput,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
// Pass self as context
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
let status = AudioObjectAddPropertyListener(
|
||||
defaultInputDevice,
|
||||
&address,
|
||||
microphonePropertyListener,
|
||||
context
|
||||
)
|
||||
|
||||
if status == noErr {
|
||||
isListenerRegistered = true
|
||||
print("MicrophoneMonitor: ✅ Property listener registered")
|
||||
} else {
|
||||
print("MicrophoneMonitor: ⚠️ Failed to register property listener (status: \(status))")
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove CoreAudio property listener
|
||||
private func removePropertyListener() {
|
||||
guard defaultInputDevice != 0, isListenerRegistered else { return }
|
||||
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioDevicePropertyDeviceIsRunning,
|
||||
mScope: kAudioDevicePropertyScopeInput,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
let context = Unmanaged.passUnretained(self).toOpaque()
|
||||
|
||||
let status = AudioObjectRemovePropertyListener(
|
||||
defaultInputDevice,
|
||||
&address,
|
||||
microphonePropertyListener,
|
||||
context
|
||||
)
|
||||
|
||||
if status == noErr {
|
||||
isListenerRegistered = false
|
||||
print("MicrophoneMonitor: ✅ Property listener removed")
|
||||
} else {
|
||||
print("MicrophoneMonitor: ⚠️ Failed to remove property listener (status: \(status))")
|
||||
}
|
||||
}
|
||||
|
||||
/// Check current microphone status
|
||||
func checkMicrophoneStatus() {
|
||||
guard defaultInputDevice != 0 else { return }
|
||||
|
||||
let isRunning = isDeviceRunning(defaultInputDevice)
|
||||
|
||||
// Debug logging
|
||||
print("MicrophoneMonitor: 🔍 Checking... current=\(isMicActive), detected=\(isRunning)")
|
||||
|
||||
// Update state if changed
|
||||
if isRunning != isMicActive {
|
||||
print("MicrophoneMonitor: 🔄 State change detected (\(isMicActive) -> \(isRunning))")
|
||||
|
||||
withAnimation(.smooth) {
|
||||
isMicActive = isRunning
|
||||
}
|
||||
|
||||
if isRunning {
|
||||
print("MicrophoneMonitor: 🎤 Microphone ACTIVE")
|
||||
// Could try to identify app here (TODO: investigate)
|
||||
activeApp = "Unknown App"
|
||||
} else {
|
||||
print("MicrophoneMonitor: ⚪ Microphone INACTIVE")
|
||||
activeApp = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if audio device is running
|
||||
private func isDeviceRunning(_ deviceID: AudioDeviceID) -> Bool {
|
||||
var address = AudioObjectPropertyAddress(
|
||||
mSelector: kAudioDevicePropertyDeviceIsRunning,
|
||||
mScope: kAudioDevicePropertyScopeInput,
|
||||
mElement: kAudioObjectPropertyElementMain
|
||||
)
|
||||
|
||||
var isRunning: UInt32 = 0
|
||||
var size = UInt32(MemoryLayout<UInt32>.size)
|
||||
|
||||
let status = AudioObjectGetPropertyData(
|
||||
deviceID,
|
||||
&address,
|
||||
0,
|
||||
nil,
|
||||
&size,
|
||||
&isRunning
|
||||
)
|
||||
|
||||
if status != noErr {
|
||||
print("MicrophoneMonitor: ⚠️ Failed to check device running status (status: \(status))")
|
||||
return false
|
||||
}
|
||||
|
||||
return isRunning != 0
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
extension MicrophoneMonitor {
|
||||
/// Get current microphone status without async
|
||||
var currentMicStatus: Bool {
|
||||
return isMicActive
|
||||
}
|
||||
|
||||
/// Check if monitoring is available
|
||||
var isMonitoringAvailable: Bool {
|
||||
return getDefaultInputDevice() != 0
|
||||
}
|
||||
}
|
||||
252
DynamicIsland/managers/PrivacyIndicatorManager.swift
Normal file
252
DynamicIsland/managers/PrivacyIndicatorManager.swift
Normal file
|
|
@ -0,0 +1,252 @@
|
|||
//
|
||||
// PrivacyIndicatorManager.swift
|
||||
// DynamicIsland
|
||||
//
|
||||
// Created for privacy indicator feature
|
||||
// Coordinates camera, microphone, and screen recording indicators
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftUI
|
||||
import Combine
|
||||
|
||||
// MARK: - Indicator Layout Enum
|
||||
enum IndicatorLayout {
|
||||
case none
|
||||
case cameraOnly
|
||||
case microphoneOnly
|
||||
case cameraAndMicrophone
|
||||
case recordingOnly
|
||||
case recordingWithCamera
|
||||
case recordingWithMicrophone
|
||||
case recordingWithBoth
|
||||
|
||||
// Computed properties for UI positioning
|
||||
var showsRecordingPulsator: Bool {
|
||||
switch self {
|
||||
case .recordingOnly, .recordingWithCamera, .recordingWithMicrophone, .recordingWithBoth:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var showsCameraIndicator: Bool {
|
||||
switch self {
|
||||
case .cameraOnly, .cameraAndMicrophone, .recordingWithCamera, .recordingWithBoth:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
var showsMicrophoneIndicator: Bool {
|
||||
switch self {
|
||||
case .microphoneOnly, .cameraAndMicrophone, .recordingWithMicrophone, .recordingWithBoth:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// Description for debugging
|
||||
var description: String {
|
||||
switch self {
|
||||
case .none: return "None"
|
||||
case .cameraOnly: return "Camera Only"
|
||||
case .microphoneOnly: return "Microphone Only"
|
||||
case .cameraAndMicrophone: return "Camera + Microphone"
|
||||
case .recordingOnly: return "Recording Only"
|
||||
case .recordingWithCamera: return "Recording + Camera"
|
||||
case .recordingWithMicrophone: return "Recording + Microphone"
|
||||
case .recordingWithBoth: return "Recording + Camera + Microphone"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Privacy Indicator Manager
|
||||
@MainActor
|
||||
class PrivacyIndicatorManager: ObservableObject {
|
||||
// MARK: - Singleton
|
||||
static let shared = PrivacyIndicatorManager()
|
||||
|
||||
// MARK: - Published Properties
|
||||
@Published var cameraActive: Bool = false
|
||||
@Published var microphoneActive: Bool = false
|
||||
@Published var screenRecordingActive: Bool = false
|
||||
|
||||
// MARK: - Child Monitors
|
||||
private let cameraMonitor = CameraMonitor()
|
||||
private let microphoneMonitor = MicrophoneMonitor()
|
||||
private var screenRecordingManager: ScreenRecordingManager?
|
||||
|
||||
// MARK: - Cancellables
|
||||
private var cancellables = Set<AnyCancellable>()
|
||||
|
||||
// MARK: - Computed Properties
|
||||
|
||||
/// Current indicator layout based on active states
|
||||
var indicatorLayout: IndicatorLayout {
|
||||
let camera = cameraActive
|
||||
let mic = microphoneActive
|
||||
let recording = screenRecordingActive
|
||||
|
||||
// 8 possible combinations
|
||||
switch (recording, camera, mic) {
|
||||
case (false, false, false):
|
||||
return .none
|
||||
case (false, true, false):
|
||||
return .cameraOnly
|
||||
case (false, false, true):
|
||||
return .microphoneOnly
|
||||
case (false, true, true):
|
||||
return .cameraAndMicrophone
|
||||
case (true, false, false):
|
||||
return .recordingOnly
|
||||
case (true, true, false):
|
||||
return .recordingWithCamera
|
||||
case (true, false, true):
|
||||
return .recordingWithMicrophone
|
||||
case (true, true, true):
|
||||
return .recordingWithBoth
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if any indicator is active
|
||||
var hasAnyIndicator: Bool {
|
||||
return cameraActive || microphoneActive || screenRecordingActive
|
||||
}
|
||||
|
||||
// MARK: - Initialization
|
||||
private init() {
|
||||
print("PrivacyIndicatorManager: 🚀 Initializing...")
|
||||
setupBindings()
|
||||
}
|
||||
|
||||
// MARK: - Setup Methods
|
||||
|
||||
/// Setup bindings to child monitors
|
||||
private func setupBindings() {
|
||||
// Bind camera monitor
|
||||
cameraMonitor.$isCameraActive
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] isActive in
|
||||
guard let self = self else { return }
|
||||
if self.cameraActive != isActive {
|
||||
print("PrivacyIndicatorManager: 📷 Camera state: \(isActive)")
|
||||
self.cameraActive = isActive
|
||||
self.logLayoutChange()
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Bind microphone monitor
|
||||
microphoneMonitor.$isMicActive
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] isActive in
|
||||
guard let self = self else { return }
|
||||
if self.microphoneActive != isActive {
|
||||
print("PrivacyIndicatorManager: 🎤 Microphone state: \(isActive)")
|
||||
self.microphoneActive = isActive
|
||||
self.logLayoutChange()
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
|
||||
// Bind screen recording manager
|
||||
let screenRecManager = ScreenRecordingManager.shared
|
||||
screenRecordingManager = screenRecManager
|
||||
|
||||
screenRecManager.$isRecording
|
||||
.receive(on: DispatchQueue.main)
|
||||
.sink { [weak self] isRecording in
|
||||
guard let self = self else { return }
|
||||
if self.screenRecordingActive != isRecording {
|
||||
print("PrivacyIndicatorManager: 📹 Screen recording state: \(isRecording)")
|
||||
self.screenRecordingActive = isRecording
|
||||
self.logLayoutChange()
|
||||
}
|
||||
}
|
||||
.store(in: &cancellables)
|
||||
}
|
||||
|
||||
/// Log layout changes for debugging
|
||||
private func logLayoutChange() {
|
||||
print("PrivacyIndicatorManager: 🔄 Layout changed to: \(indicatorLayout.description)")
|
||||
print("PrivacyIndicatorManager: 📊 States - Camera: \(cameraActive), Mic: \(microphoneActive), Recording: \(screenRecordingActive)")
|
||||
}
|
||||
|
||||
// MARK: - Public Methods
|
||||
|
||||
/// Start monitoring all privacy indicators
|
||||
func startMonitoring() {
|
||||
print("PrivacyIndicatorManager: 🟢 Starting all monitors...")
|
||||
|
||||
// Start camera monitoring
|
||||
if cameraMonitor.isMonitoringAvailable {
|
||||
cameraMonitor.startMonitoring()
|
||||
} else {
|
||||
print("PrivacyIndicatorManager: ⚠️ Camera monitoring not available")
|
||||
}
|
||||
|
||||
// Start microphone monitoring
|
||||
if microphoneMonitor.isMonitoringAvailable {
|
||||
microphoneMonitor.startMonitoring()
|
||||
} else {
|
||||
print("PrivacyIndicatorManager: ⚠️ Microphone monitoring not available")
|
||||
}
|
||||
|
||||
// Screen recording is already monitored by ScreenRecordingManager
|
||||
print("PrivacyIndicatorManager: ✅ All monitors started")
|
||||
}
|
||||
|
||||
/// Stop monitoring all privacy indicators
|
||||
func stopMonitoring() {
|
||||
print("PrivacyIndicatorManager: 🛑 Stopping all monitors...")
|
||||
|
||||
cameraMonitor.stopMonitoring()
|
||||
microphoneMonitor.stopMonitoring()
|
||||
|
||||
print("PrivacyIndicatorManager: ✅ All monitors stopped")
|
||||
}
|
||||
|
||||
/// Toggle monitoring state
|
||||
func toggleMonitoring() {
|
||||
if cameraMonitor.isMonitoring || microphoneMonitor.isMonitoring {
|
||||
stopMonitoring()
|
||||
} else {
|
||||
startMonitoring()
|
||||
}
|
||||
}
|
||||
|
||||
/// Get detailed status string for debugging
|
||||
func getStatusString() -> String {
|
||||
var status = "Privacy Indicators:\n"
|
||||
status += " Camera: \(cameraActive ? "🟢 Active" : "⚪ Inactive")\n"
|
||||
status += " Microphone: \(microphoneActive ? "🟢 Active" : "⚪ Inactive")\n"
|
||||
status += " Screen Recording: \(screenRecordingActive ? "🟢 Active" : "⚪ Inactive")\n"
|
||||
status += " Layout: \(indicatorLayout.description)"
|
||||
return status
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Extensions
|
||||
|
||||
extension PrivacyIndicatorManager {
|
||||
/// Get camera monitor instance
|
||||
var camera: CameraMonitor {
|
||||
return cameraMonitor
|
||||
}
|
||||
|
||||
/// Get microphone monitor instance
|
||||
var microphone: MicrophoneMonitor {
|
||||
return microphoneMonitor
|
||||
}
|
||||
|
||||
/// Get screen recording manager instance
|
||||
var screenRecording: ScreenRecordingManager? {
|
||||
return screenRecordingManager
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue