Add low battery level warning

This commit is contained in:
Ilya Laktyushin 2020-08-08 18:54:26 +03:00
parent 8b1794141d
commit 2bbd833577
4 changed files with 54 additions and 6 deletions

View File

@ -953,7 +953,7 @@ final class CallControllerNode: ViewControllerTracingNode, CallControllerNodePro
}
}
} else {
self.displayToastsAfterTimestamp = CACurrentMediaTime() + 2.0
self.displayToastsAfterTimestamp = CACurrentMediaTime() + 1.5
}
}
if self.isMuted, let (availableOutputs, _) = self.audioOutputState, availableOutputs.count > 2 {
@ -1457,6 +1457,8 @@ final class CallControllerNode: ViewControllerTracingNode, CallControllerNodePro
@objc func tapGesture(_ recognizer: UITapGestureRecognizer) {
if case .ended = recognizer.state {
if !self.pictureInPictureTransitionFraction.isZero {
self.view.window?.endEditing(true)
if let (layout, navigationHeight) = self.validLayout {
self.pictureInPictureTransitionFraction = 0.0

View File

@ -188,6 +188,8 @@ public final class PresentationCallImpl: PresentationCall {
private var receptionDisposable: Disposable?
private var reportedIncomingCall = false
private var batteryLevelDisposable: Disposable?
private var callWasActive = false
private var shouldPresentCallRating = false
@ -415,6 +417,7 @@ public final class PresentationCallImpl: PresentationCall {
self.sessionStateDisposable?.dispose()
self.ongoingContextStateDisposable?.dispose()
self.receptionDisposable?.dispose()
self.batteryLevelDisposable?.dispose()
self.audioSessionDisposable?.dispose()
if let dropCallKitCallTimer = self.dropCallKitCallTimer {
@ -631,6 +634,35 @@ public final class PresentationCallImpl: PresentationCall {
}
})
func batteryLevelIsLowSignal() -> Signal<Bool, NoError> {
return Signal { subscriber in
let device = UIDevice.current
device.isBatteryMonitoringEnabled = true
var previousBatteryLevelIsLow = false
let timer = SwiftSignalKit.Timer(timeout: 30.0, repeat: true, completion: {
let batteryLevelIsLow = device.batteryLevel < 0.1 && device.batteryState != .charging
if batteryLevelIsLow != previousBatteryLevelIsLow {
previousBatteryLevelIsLow = batteryLevelIsLow
subscriber.putNext(batteryLevelIsLow)
}
}, queue: Queue.mainQueue())
timer.start()
return ActionDisposable {
device.isBatteryMonitoringEnabled = false
timer.invalidate()
}
}
}
self.batteryLevelDisposable = (batteryLevelIsLowSignal()
|> deliverOnMainQueue).start(next: { [weak self] batteryLevelIsLow in
if let strongSelf = self, let ongoingContext = strongSelf.ongoingContext {
ongoingContext.setIsLowBatteryLevel(batteryLevelIsLow)
}
})
if sessionState.isOutgoing {
self.callKitIntegration?.reportOutgoingCallConnected(uuid: sessionState.id, at: Date())
}

View File

@ -634,9 +634,9 @@ public final class SharedAccountContextImpl: SharedAccountContext {
if let strongSelf = self {
let resolvedText: CallStatusText
if let state = state {
if [.active, .paused].contains(state.videoState) || [.active, .paused].contains(state.remoteVideoState) {
resolvedText = .none
} else {
// if [.active, .paused].contains(state.videoState) || [.active, .paused].contains(state.remoteVideoState) {
// resolvedText = .none
// } else {
switch state.state {
case .connecting, .requesting, .terminating, .ringing, .waiting:
resolvedText = .inProgress(nil)
@ -645,7 +645,7 @@ public final class SharedAccountContextImpl: SharedAccountContext {
case .active(let timestamp, _, _), .reconnecting(let timestamp, _, _):
resolvedText = .inProgress(timestamp)
}
}
// }
} else {
resolvedText = .none
}

View File

@ -262,6 +262,7 @@ private func ongoingDataSavingForTypeWebrtc(_ type: VoiceCallDataSaving) -> Ongo
private protocol OngoingCallThreadLocalContextProtocol: class {
func nativeSetNetworkType(_ type: NetworkType)
func nativeSetIsMuted(_ value: Bool)
func nativeSetIsLowBatteryLevel(_ value: Bool)
func nativeRequestVideo(_ capturer: OngoingCallVideoCapturer)
func nativeDisableVideo()
func nativeStop(_ completion: @escaping (String?, Int64, Int64, Int64, Int64) -> Void)
@ -295,6 +296,9 @@ extension OngoingCallThreadLocalContext: OngoingCallThreadLocalContextProtocol {
self.setIsMuted(value)
}
func nativeSetIsLowBatteryLevel(_ value: Bool) {
}
func nativeRequestVideo(_ capturer: OngoingCallVideoCapturer) {
}
@ -373,6 +377,10 @@ extension OngoingCallThreadLocalContextWebrtc: OngoingCallThreadLocalContextProt
self.setIsMuted(value)
}
func nativeSetIsLowBatteryLevel(_ value: Bool) {
self.setIsLowBatteryLevel(value)
}
func nativeRequestVideo(_ capturer: OngoingCallVideoCapturer) {
self.requestVideo(capturer.impl)
}
@ -707,7 +715,7 @@ public final class OngoingCallContext {
self.audioSessionDisposable.dispose()
self.networkTypeDisposable?.dispose()
}
private func withContext(_ f: @escaping (OngoingCallThreadLocalContextProtocol) -> Void) {
self.queue.async {
if let contextRef = self.contextRef {
@ -766,6 +774,12 @@ public final class OngoingCallContext {
}
}
public func setIsLowBatteryLevel(_ value: Bool) {
self.withContext { context in
context.nativeSetIsLowBatteryLevel(value)
}
}
public func requestVideo(_ capturer: OngoingCallVideoCapturer) {
self.withContext { context in
context.nativeRequestVideo(capturer)