File size: 3,726 Bytes
3a08226
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// 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();