2023-06-14 00:26:43 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import deviceInfo from '/imports/utils/deviceInfo';
|
|
|
|
|
2024-03-07 01:28:18 +08:00
|
|
|
const WAKELOCK_ENABLED = window.meetingClientSettings.public.app.wakeLock.enabled;
|
2023-06-14 00:26:43 +08:00
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
const WAKELOCK_ERRORS = {
|
|
|
|
NOT_SUPPORTED: {
|
|
|
|
locale: 'wakeLockNotSupported',
|
|
|
|
error: 'wake_lock_not_supported',
|
|
|
|
},
|
|
|
|
REQUEST_FAILED: {
|
|
|
|
locale: 'wakeLockAcquireFailed',
|
|
|
|
error: 'wake_lock_request_error',
|
2024-04-30 02:16:51 +08:00
|
|
|
},
|
|
|
|
};
|
2023-08-12 03:57:11 +08:00
|
|
|
|
2023-06-14 00:26:43 +08:00
|
|
|
class WakeLock {
|
|
|
|
constructor() {
|
|
|
|
this.sentinel = null;
|
|
|
|
this.apiSupport = 'wakeLock' in navigator;
|
|
|
|
}
|
|
|
|
|
|
|
|
static isEnabled() {
|
|
|
|
return WAKELOCK_ENABLED;
|
|
|
|
}
|
|
|
|
|
|
|
|
isSupported() {
|
|
|
|
const { isMobile } = deviceInfo;
|
|
|
|
return WakeLock.isEnabled() && this.apiSupport && isMobile;
|
|
|
|
}
|
|
|
|
|
2023-08-12 03:57:11 +08:00
|
|
|
isMobile() {
|
|
|
|
const { isMobile } = deviceInfo;
|
|
|
|
return isMobile;
|
|
|
|
}
|
|
|
|
|
2023-06-14 00:26:43 +08:00
|
|
|
isActive() {
|
|
|
|
return this.sentinel !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleVisibilityChanged() {
|
|
|
|
if (document.visibilityState === 'visible') {
|
|
|
|
this.request();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleRelease() {
|
|
|
|
document.removeEventListener('visibilitychange', this.handleVisibilityChanged);
|
|
|
|
this.sentinel = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async request() {
|
|
|
|
if (!this.isSupported()) {
|
|
|
|
logger.warn({
|
2024-04-30 02:16:51 +08:00
|
|
|
logCode: WAKELOCK_ERRORS.NOT_SUPPORTED.error,
|
2023-06-14 00:26:43 +08:00
|
|
|
}, 'Wake lock API not supported');
|
2023-08-12 03:57:11 +08:00
|
|
|
return {
|
|
|
|
...WAKELOCK_ERRORS.NOT_SUPPORTED,
|
|
|
|
msg: 'Wake lock API not supported',
|
|
|
|
};
|
2023-06-14 00:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.sentinel = await navigator.wakeLock.request('screen');
|
|
|
|
this.sentinel.addEventListener('release', this.handleRelease);
|
|
|
|
document.addEventListener('visibilitychange', this.handleVisibilityChanged.bind(this));
|
|
|
|
document.addEventListener('fullscreenchange', this.handleVisibilityChanged.bind(this));
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn({
|
2024-04-30 02:16:51 +08:00
|
|
|
logCode: WAKELOCK_ERRORS.REQUEST_FAILED.error,
|
2023-06-14 00:26:43 +08:00
|
|
|
extraInfo: {
|
|
|
|
errorName: err.name,
|
|
|
|
errorMessage: err.message,
|
|
|
|
},
|
|
|
|
}, 'Error requesting wake lock.');
|
2023-08-12 03:57:11 +08:00
|
|
|
return {
|
|
|
|
...WAKELOCK_ERRORS.REQUEST_FAILED,
|
|
|
|
msg: `${err.name} - ${err.message}`,
|
|
|
|
};
|
2023-06-14 00:26:43 +08:00
|
|
|
}
|
2023-08-12 03:57:11 +08:00
|
|
|
return {
|
|
|
|
error: false,
|
|
|
|
};
|
2023-06-14 00:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
release() {
|
|
|
|
if (this.isActive()) this.sentinel.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const wakeLock = new WakeLock();
|
|
|
|
|
|
|
|
export default {
|
2023-06-15 00:04:55 +08:00
|
|
|
isEnabled: () => wakeLock.isEnabled(),
|
2023-06-14 00:26:43 +08:00
|
|
|
isSupported: () => wakeLock.isSupported(),
|
2023-08-12 03:57:11 +08:00
|
|
|
isMobile: () => wakeLock.isMobile(),
|
2023-06-14 00:26:43 +08:00
|
|
|
isActive: () => wakeLock.isActive(),
|
|
|
|
request: () => wakeLock.request(),
|
|
|
|
release: () => wakeLock.release(),
|
|
|
|
};
|