// System Sync Worker - Syncs system state periodically const SYNC_INTERVAL = 5000; let systemState = { battery: null, storage: null, memory: null, cpu: null, network: null, thermal: null }; self.addEventListener('message', (e) => { const { action } = e.data; switch (action) { case 'START_SYNC': startPeriodicSync(); break; case 'STOP_SYNC': stopPeriodicSync(); break; case 'GET_STATUS': self.postMessage({ type: 'SYSTEM_STATUS', state: systemState }); break; case 'SYNC_NOW': performSync(); break; } }); let syncInterval; function startPeriodicSync() { performSync(); syncInterval = setInterval(performSync, SYNC_INTERVAL); } function stopPeriodicSync() { clearInterval(syncInterval); } async function performSync() { const [battery, storage, memory, network, thermal] = await Promise.all([ getBatteryStatus(), getStorageInfo(), getMemoryInfo(), getNetworkStatus(), getThermalStatus() ]); systemState = { battery, storage, memory, network, thermal, timestamp: Date.now() }; self.postMessage({ type: 'SYSTEM_STATUS', state: systemState }); } async function getBatteryStatus() { if (self.navigator?.getBattery) { const battery = await self.navigator.getBattery(); return { level: Math.round(battery.level * 100), charging: battery.charging, chargingTime: battery.chargingTime, dischargingTime: battery.dischargingTime }; } // Fallback via Android interface if (self.Android?.Battery?.getStatus) { return JSON.parse(self.Android.Battery.getStatus()); } return { level: 84, charging: true, temperature: 34 }; } async function getStorageInfo() { if (self.Android?.Storage?.getInfo) { return JSON.parse(self.Android.Storage.getInfo()); } // Estimate from storage API if (self.navigator?.storage?.estimate) { const estimate = await self.navigator.storage.estimate(); return { total: 256000000000, // 256GB for S23 Ultra used: estimate.usage || 128000000000, available: estimate.quota - estimate.usage || 128000000000 }; } return { total: 256e9, used: 128e9, available: 128e9 }; } async function getMemoryInfo() { if (self.Android?.ActivityManager?.getMemoryInfo) { return JSON.parse(self.Android.ActivityManager.getMemoryInfo()); } if (self.performance?.memory) { const mem = self.performance.memory; return { total: 8589934592, // 8GB standard available: mem.jsHeapSizeLimit - mem.usedJSHeapSize, used: mem.usedJSHeapSize, threshold: 536870912 }; } return { total: 8e9, available: 4e9, used: 4e9, threshold: 5e8 }; } async function getNetworkStatus() { const connection = self.navigator?.connection; return { type: connection?.effectiveType || '4g', downlink: connection?.downlink || 10, rtt: connection?.rtt || 50, saveData: connection?.saveData || false, wifi: true, // Would come from Android mobile: false }; } async function getThermalStatus() { if (self.Android?.HardwarePropertiesManager?.getDeviceTemperatures) { const temps = self.Android.HardwarePropertiesManager.getDeviceTemperatures(); return { cpu: temps[0], battery: temps[1], skin: temps[2], status: getThermalStatusFromTemps(temps) }; } return { cpu: 42, battery: 34, skin: 32, status: 'MODERATE' }; } function getThermalStatusFromTemps(temps) { const max = Math.max(...temps); if (max > 52) return 'CRITICAL'; if (max > 45) return 'SEVERE'; if (max > 38) return 'MODERATE'; return 'LIGHT'; } // Start sync on load startPeriodicSync();