2
0

WFCUFloatingWindow.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // WFCUFloatingWindow.m
  3. // WFDemo
  4. //
  5. // Created by heavyrain on 17/9/27.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #if WFCU_SUPPORT_VOIP
  9. #import "WFCUFloatingWindow.h"
  10. #import <CoreTelephony/CTCall.h>
  11. #import <CoreTelephony/CTCallCenter.h>
  12. #import <UIKit/UIKit.h>
  13. @interface WFCUFloatingWindow () <WFAVCallSessionDelegate>
  14. @property(nonatomic, strong) NSTimer *activeTimer;
  15. @property(nonatomic, copy) void (^touchedBlock)(WFAVCallSession *callSession);
  16. @property(nonatomic, strong) CTCallCenter *callCenter;
  17. @property(nonatomic, strong) NSString *focusUserId;
  18. @end
  19. static WFCUFloatingWindow *staticWindow = nil;
  20. static NSString *kFloatingWindowPosX = @"kFloatingWindowPosX";
  21. static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
  22. @implementation WFCUFloatingWindow
  23. + (void)startCallFloatingWindow:(WFAVCallSession *)callSession focusUser:(NSString *)focusUserId
  24. withTouchedBlock:(void (^)(WFAVCallSession *callSession))touchedBlock {
  25. staticWindow = [[WFCUFloatingWindow alloc] init];
  26. staticWindow.callSession = callSession;
  27. [staticWindow.callSession setDelegate:staticWindow];
  28. staticWindow.touchedBlock = touchedBlock;
  29. staticWindow.focusUserId = focusUserId;
  30. [staticWindow initWindow];
  31. }
  32. + (void)stopCallFloatingWindow {
  33. [staticWindow hideCallFloatingWindow];
  34. [staticWindow clearCallFloatingWindow];
  35. staticWindow = nil;
  36. }
  37. - (void)initWindow {
  38. if (self.callSession.state == kWFAVEngineStateIdle) {
  39. [self performSelector:@selector(clearCallFloatingWindow) withObject:nil afterDelay:2];
  40. }
  41. [self updateActiveTimer];
  42. [self startActiveTimer];
  43. [self updateWindow];
  44. [self registerTelephonyEvent];
  45. [[NSNotificationCenter defaultCenter] addObserver:self
  46. selector:@selector(onDeviceOrientationDidChange:)
  47. name:UIDeviceOrientationDidChangeNotification
  48. object:nil];
  49. [self addProximityMonitoringObserver];
  50. }
  51. - (void)registerTelephonyEvent {
  52. self.callCenter = [[CTCallCenter alloc] init];
  53. __weak __typeof(self) weakSelf = self;
  54. self.callCenter.callEventHandler = ^(CTCall *call) {
  55. if ([call.callState isEqualToString:CTCallStateConnected]) {
  56. [weakSelf.callSession endCall];
  57. }
  58. };
  59. }
  60. - (void)onDeviceOrientationDidChange:(NSNotification *)notification {
  61. [self updateWindow];
  62. }
  63. - (void)startActiveTimer {
  64. self.activeTimer = [NSTimer scheduledTimerWithTimeInterval:1
  65. target:self
  66. selector:@selector(updateActiveTimer)
  67. userInfo:nil
  68. repeats:YES];
  69. [self.activeTimer fire];
  70. }
  71. - (void)stopActiveTimer {
  72. if (self.activeTimer) {
  73. [self.activeTimer invalidate];
  74. self.activeTimer = nil;
  75. }
  76. }
  77. - (void)updateActiveTimer {
  78. long sec = [[NSDate date] timeIntervalSince1970] - self.callSession.connectedTime / 1000;
  79. if (self.callSession.state == kWFAVEngineStateConnected && ![self isVideoViewEnabled]) {
  80. NSString *timeStr;
  81. if (sec < 60 * 60) {
  82. timeStr = [NSString stringWithFormat:@"%02ld:%02ld", sec / 60, sec % 60];
  83. } else {
  84. timeStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", sec / 60 / 60, (sec / 60) % 60, sec % 60];
  85. }
  86. BOOL showFloatingButton = NO;
  87. if (!self.floatingButton.titleLabel.text) {
  88. self.floatingButton.hidden = YES;
  89. showFloatingButton = YES;
  90. }
  91. [self.floatingButton setTitle:timeStr forState:UIControlStateNormal];
  92. [self layoutTimeText:self.floatingButton];
  93. if (showFloatingButton) {
  94. dispatch_async(dispatch_get_main_queue(), ^{
  95. [self.floatingButton setTitle:timeStr forState:UIControlStateNormal];
  96. [self layoutTimeText:self.floatingButton];
  97. self.floatingButton.hidden = NO;
  98. });
  99. }
  100. }
  101. }
  102. //1.决定当前界面是否开启自动转屏,如果返回NO,后面两个方法也不会被调用,只是会支持默认的方向
  103. - (BOOL)shouldAutorotate {
  104. return YES;
  105. }
  106. //2.返回支持的旋转方向
  107. //iPad设备上,默认返回值UIInterfaceOrientationMaskAllButUpSideDwon
  108. //iPad设备上,默认返回值是UIInterfaceOrientationMaskAll
  109. - (UIInterfaceOrientationMask)supportedInterfaceOrientations{
  110. return UIDeviceOrientationLandscapeLeft | UIDeviceOrientationLandscapeRight | UIDeviceOrientationPortrait;
  111. }
  112. //3.返回进入界面默认显示方向
  113. - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
  114. return UIInterfaceOrientationPortrait;
  115. }
  116. - (void)updateWindow {
  117. CGFloat posX = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosX] floatValue];
  118. CGFloat posY = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosY] floatValue];
  119. posX = posX ? posX : 30;
  120. posY = posY ? posY : 30;
  121. CGRect screenBounds = [UIScreen mainScreen].bounds;
  122. posX = (posX + 30) > screenBounds.size.width ? (screenBounds.size.width - 30) : posX;
  123. posY = (posY + 48) > screenBounds.size.height ? (screenBounds.size.height - 48) : posY;
  124. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
  125. self.window.transform = CGAffineTransformMakeRotation(M_PI / 2);
  126. self.window.frame = CGRectMake(posX, posY, 64, 96);
  127. self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
  128. if ([self isVideoViewEnabled]) {
  129. self.videoView.frame = CGRectMake(0, 0, 96, 64);
  130. }
  131. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  132. self.window.transform = CGAffineTransformMakeRotation(-M_PI / 2);
  133. self.window.frame = CGRectMake(posX, posY, 64, 96);
  134. self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
  135. if ([self isVideoViewEnabled]) {
  136. self.videoView.frame = CGRectMake(0, 0, 96, 64);
  137. }
  138. } else {
  139. if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  140. self.window.transform = CGAffineTransformMakeRotation(M_PI);
  141. } else {
  142. self.window.transform = CGAffineTransformMakeRotation(0);
  143. }
  144. self.window.frame = CGRectMake(posX, posY, 64, 96);
  145. self.floatingButton.frame = CGRectMake(0, 0, 64, 96);
  146. if ([self isVideoViewEnabled]) {
  147. self.videoView.frame = CGRectMake(0, 0, 64, 96);
  148. }
  149. }
  150. if ([self isVideoViewEnabled]) {
  151. if (self.callSession.state == kWFAVEngineStateIncomming) {
  152. [self.floatingButton setTitle:@"等待接听" forState:UIControlStateNormal];
  153. self.videoView.hidden = YES;
  154. } else if (self.callSession.state == kWFAVEngineStateOutgoing) {
  155. self.videoView.hidden = NO;
  156. [self.floatingButton setTitle:@"" forState:UIControlStateNormal];
  157. [self.callSession setupLocalVideoView:self.videoView scalingType:kWFAVVideoScalingTypeAspectFit];
  158. } else if (self.callSession.state == kWFAVEngineStateConnected) {
  159. self.videoView.hidden = NO;
  160. [self.floatingButton setTitle:@"" forState:UIControlStateNormal];
  161. if ([self.focusUserId isEqualToString:[WFCCNetworkService sharedInstance].userId]) {
  162. [self.callSession setupLocalVideoView:self.videoView scalingType:kWFAVVideoScalingTypeAspectFit];
  163. } else {
  164. [self.callSession setupRemoteVideoView:self.videoView scalingType:kWFAVVideoScalingTypeAspectFit forUser:self.focusUserId];
  165. }
  166. } else if (self.callSession.state == kWFAVEngineStateIdle) {
  167. UILabel *videoStopTips =
  168. [[UILabel alloc] initWithFrame:CGRectMake(0, self.videoView.frame.size.height / 2 - 10,
  169. self.videoView.frame.size.width, 20)];
  170. videoStopTips.textAlignment = NSTextAlignmentCenter;
  171. videoStopTips.text = WFCString(@"Ended");
  172. videoStopTips.textColor = HEXCOLOR(0x0195ff);
  173. [self.videoView addSubview:videoStopTips];
  174. } else { //connecting...
  175. self.videoView.hidden = NO;
  176. [self.floatingButton setTitle:@"" forState:UIControlStateNormal];
  177. }
  178. } else {
  179. if (self.callSession.state == kWFAVEngineStateIdle) {
  180. [self.floatingButton setBackgroundColor:[UIColor clearColor]];
  181. [self.floatingButton setTitle:WFCString(@"Ended")
  182. forState:UIControlStateNormal];
  183. } else {
  184. if (self.callSession.state == kWFAVEngineStateOutgoing) {
  185. [self.floatingButton setTitle:@"等待接听" forState:UIControlStateNormal];
  186. } else if (self.callSession.state == kWFAVEngineStateIncomming) {
  187. [self.floatingButton setTitle:@"等待接听" forState:UIControlStateNormal];
  188. } else {
  189. [self.floatingButton setTitle:@"" forState:UIControlStateNormal];
  190. [self.floatingButton setImage:[UIImage imageNamed:@"floatingaudio"]
  191. forState:UIControlStateNormal];
  192. }
  193. }
  194. }
  195. }
  196. - (UIWindow *)window {
  197. if (!_window) {
  198. CGFloat posX = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosX] floatValue];
  199. CGFloat posY = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosY] floatValue];
  200. posX = (posX - 30) ? posX : 30;
  201. posY = (posY - 48) ? posY : 48;
  202. CGRect screenBounds = [UIScreen mainScreen].bounds;
  203. posX = (posX + 30) > screenBounds.size.width ? (screenBounds.size.width - 30) : posX;
  204. posY = (posY + 48) > screenBounds.size.height ? (screenBounds.size.height - 48) : posY;
  205. _window = [[UIWindow alloc] initWithFrame:CGRectMake(posX, posY, 64, 96)];
  206. _window.backgroundColor = [UIColor whiteColor];
  207. _window.windowLevel = UIWindowLevelAlert + 1;
  208. _window.layer.cornerRadius = 4;
  209. _window.layer.masksToBounds = YES;
  210. _window.layer.borderWidth = 1;
  211. _window.layer.borderColor = [HEXCOLOR(0x0A88E1) CGColor];
  212. [_window makeKeyAndVisible]; //关键语句,显示window
  213. UIPanGestureRecognizer *panGestureRecognizer =
  214. [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
  215. panGestureRecognizer.minimumNumberOfTouches = 1;
  216. panGestureRecognizer.maximumNumberOfTouches = 1;
  217. [_window addGestureRecognizer:panGestureRecognizer];
  218. }
  219. return _window;
  220. }
  221. - (UIView *)videoView {
  222. if (!_videoView) {
  223. _videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 64, 96)];
  224. _videoView.backgroundColor = [UIColor blackColor];
  225. CGRect windowFrame = self.window.frame;
  226. windowFrame.size.width = _videoView.frame.size.width;
  227. windowFrame.size.height = _videoView.frame.size.height;
  228. self.window.frame = windowFrame;
  229. [self.window addSubview:_videoView];
  230. UITapGestureRecognizer *tap =
  231. [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchedWindow:)];
  232. [_videoView addGestureRecognizer:tap];
  233. }
  234. return _videoView;
  235. }
  236. - (UIButton *)floatingButton {
  237. if (!_floatingButton) {
  238. _floatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
  239. [_floatingButton setTitle:@"" forState:UIControlStateNormal];
  240. _floatingButton.backgroundColor = [UIColor clearColor];
  241. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft || [UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  242. _floatingButton.frame = CGRectMake(0, 0, 64, 96);
  243. } else {
  244. _floatingButton.frame = CGRectMake(0, 0, 96, 64);
  245. }
  246. [_floatingButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
  247. _floatingButton.titleLabel.font = [UIFont systemFontOfSize:14];
  248. CGRect windowFrame = self.window.frame;
  249. windowFrame.size.width = _floatingButton.frame.size.width;
  250. windowFrame.size.height = _floatingButton.frame.size.height;
  251. self.window.frame = windowFrame;
  252. [_floatingButton addTarget:self action:@selector(touchedWindow:) forControlEvents:UIControlEventTouchUpInside];
  253. [self.window addSubview:_floatingButton];
  254. }
  255. return _floatingButton;
  256. }
  257. - (void)handlePanGestures:(UIPanGestureRecognizer *)paramSender {
  258. if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed) {
  259. CGPoint location = [paramSender locationInView:[UIApplication sharedApplication].windows[0]];
  260. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft) {
  261. CGFloat tmp = location.x;
  262. location.x = [UIScreen mainScreen].bounds.size.height - location.y;
  263. location.y = tmp;
  264. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight) {
  265. CGFloat tmp = location.x;
  266. location.x = location.y;
  267. location.y = [UIScreen mainScreen].bounds.size.width - tmp;
  268. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown) {
  269. CGFloat tmp = location.x;
  270. location.x = [UIScreen mainScreen].bounds.size.height - location.y;
  271. location.y = [UIScreen mainScreen].bounds.size.width - tmp;
  272. }
  273. CGRect frame = self.window.frame;
  274. frame.origin.x = location.x - frame.size.width / 2;
  275. frame.origin.y = location.y - frame.size.height / 2;
  276. if (frame.origin.x < 0) {
  277. frame.origin.x = 2;
  278. }
  279. if (frame.origin.y < 0) {
  280. frame.origin.y = 2;
  281. }
  282. CGRect screenBounds = [UIScreen mainScreen].bounds;
  283. BOOL isLandscape = screenBounds.size.width > screenBounds.size.height;
  284. if (isLandscape) {
  285. if (frame.origin.y + frame.size.height > [UIScreen mainScreen].bounds.size.width) {
  286. frame.origin.y = [UIScreen mainScreen].bounds.size.width - 2 - frame.size.height;
  287. }
  288. if (frame.origin.x + frame.size.width > [UIScreen mainScreen].bounds.size.height) {
  289. frame.origin.x = [UIScreen mainScreen].bounds.size.height - 2 - frame.size.width;
  290. }
  291. } else {
  292. if (frame.origin.x + frame.size.width > [UIScreen mainScreen].bounds.size.width) {
  293. frame.origin.x = [UIScreen mainScreen].bounds.size.width - 2 - frame.size.width;
  294. }
  295. if (frame.origin.y + frame.size.height > [UIScreen mainScreen].bounds.size.height) {
  296. frame.origin.y = [UIScreen mainScreen].bounds.size.height - 2 - frame.size.height;
  297. }
  298. }
  299. self.window.frame = frame;
  300. } else if (paramSender.state == UIGestureRecognizerStateEnded) {
  301. CGRect frame = self.window.frame;
  302. [[NSUserDefaults standardUserDefaults] setObject:@(frame.origin.x) forKey:kFloatingWindowPosX];
  303. [[NSUserDefaults standardUserDefaults] setObject:@(frame.origin.y) forKey:kFloatingWindowPosY];
  304. [[NSUserDefaults standardUserDefaults] synchronize];
  305. }
  306. }
  307. - (void)touchedWindow:(id)sender {
  308. [self hideCallFloatingWindow];
  309. if (self.touchedBlock) {
  310. self.touchedBlock(self.callSession);
  311. }
  312. [self clearCallFloatingWindow];
  313. }
  314. - (void)hideCallFloatingWindow {
  315. [self stopActiveTimer];
  316. if (_videoView) {
  317. [_videoView removeFromSuperview];
  318. _videoView = nil;
  319. }
  320. if (_floatingButton) {
  321. [_floatingButton removeFromSuperview];
  322. _floatingButton = nil;
  323. }
  324. [_window setHidden:YES];
  325. }
  326. - (void)clearCallFloatingWindow {
  327. [[NSNotificationCenter defaultCenter] removeObserver:self];
  328. _activeTimer = nil;
  329. _callSession = nil;
  330. _touchedBlock = nil;
  331. _floatingButton = nil;
  332. _videoView = nil;
  333. _window = nil;
  334. staticWindow = nil;
  335. }
  336. - (void)layoutTimeText:(UIButton *)button {
  337. // button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
  338. [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
  339. [button setTitleColor:HEXCOLOR(0x0195ff) forState:UIControlStateNormal];
  340. //top left buttom right
  341. button.titleEdgeInsets = UIEdgeInsetsMake(button.imageView.frame.size.height / 2, -button.imageView.frame.size.width,
  342. -button.imageView.frame.size.height, 0);
  343. button.imageEdgeInsets = UIEdgeInsetsMake(0,
  344. 0, button.imageView.frame.size.height / 2, -button.titleLabel.bounds.size.width);
  345. }
  346. - (BOOL)isVideoViewEnabled {
  347. return !self.callSession.isAudioOnly;
  348. }
  349. #pragma mark - WFAVCallSessionDelegate
  350. - (void)didChangeState:(WFAVEngineState)state {
  351. switch (state) {
  352. case kWFAVEngineStateIdle:
  353. [self updateWindow];
  354. [self performSelector:@selector(clearCallFloatingWindow) withObject:nil afterDelay:2];
  355. [self removeProximityMonitoringObserver];
  356. break;
  357. case kWFAVEngineStateConnected:
  358. [self updateWindow];
  359. break;
  360. default:
  361. [self updateWindow];
  362. break;
  363. }
  364. }
  365. - (void)didCallEndWithReason:(WFAVCallEndReason)reason {
  366. }
  367. - (void)didParticipantJoined:(NSString *)userId {
  368. }
  369. - (void)didParticipantConnected:(NSString *)userId {
  370. }
  371. - (void)didParticipantLeft:(NSString *)userId withReason:(WFAVCallEndReason)reason {
  372. }
  373. - (void)didError:(NSError *)error {
  374. }
  375. - (void)didGetStats:(NSArray *)stats {
  376. }
  377. - (void)didCreateLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
  378. }
  379. - (void)didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack fromUser:(NSString *)userId {
  380. }
  381. - (void)didVideoMuted:(BOOL)videoMuted fromUser:(NSString *)userId {
  382. }
  383. - (void)didReportAudioVolume:(NSInteger)volume ofUser:(NSString *)userId {
  384. }
  385. - (void)didChangeMode:(BOOL)isAudioOnly {
  386. [self.videoView removeFromSuperview];
  387. [self initWindow];
  388. }
  389. - (void)didChangeInitiator:(NSString * _Nullable)initiator {
  390. }
  391. - (void)addProximityMonitoringObserver {
  392. [UIDevice currentDevice].proximityMonitoringEnabled = YES;
  393. [[NSNotificationCenter defaultCenter] addObserver:self
  394. selector:@selector(proximityStatueChanged:)
  395. name:UIDeviceProximityStateDidChangeNotification
  396. object:nil];
  397. }
  398. - (void)removeProximityMonitoringObserver {
  399. [UIDevice currentDevice].proximityMonitoringEnabled = NO;
  400. [[NSNotificationCenter defaultCenter] removeObserver:self
  401. name:UIDeviceProximityStateDidChangeNotification
  402. object:nil];
  403. }
  404. - (void)proximityStatueChanged:(NSNotificationCenter *)notification {
  405. }
  406. @end
  407. #endif