WFCUFloatingWindow.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // WFCUFloatingWindow.m
  3. // WFDemo
  4. //
  5. // Created by heavyrain on 17/9/27.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUFloatingWindow.h"
  9. #import <CoreTelephony/CTCall.h>
  10. #import <CoreTelephony/CTCallCenter.h>
  11. #import <UIKit/UIKit.h>
  12. @interface WFCUFloatingWindow () <WFAVCallSessionDelegate>
  13. @property(nonatomic, strong) NSTimer *activeTimer;
  14. @property(nonatomic, copy) void (^touchedBlock)(WFAVCallSession *callSession);
  15. @property(nonatomic, strong) CTCallCenter *callCenter;
  16. @end
  17. static WFCUFloatingWindow *staticWindow = nil;
  18. static NSString *kFloatingWindowPosX = @"kFloatingWindowPosX";
  19. static NSString *kFloatingWindowPosY = @"kFloatingWindowPosY";
  20. @implementation WFCUFloatingWindow
  21. + (void)startCallFloatingWindow:(WFAVCallSession *)callSession
  22. withTouchedBlock:(void (^)(WFAVCallSession *callSession))touchedBlock {
  23. staticWindow = [[WFCUFloatingWindow alloc] init];
  24. staticWindow.callSession = callSession;
  25. [staticWindow.callSession setDelegate:staticWindow];
  26. staticWindow.touchedBlock = touchedBlock;
  27. [staticWindow initWindow];
  28. }
  29. + (void)stopCallFloatingWindow {
  30. [staticWindow hideCallFloatingWindow];
  31. [staticWindow clearCallFloatingWindow];
  32. staticWindow = nil;
  33. }
  34. - (void)initWindow {
  35. if (self.callSession.state == kWFAVEngineStateIdle) {
  36. [self performSelector:@selector(clearCallFloatingWindow) withObject:nil afterDelay:2];
  37. }
  38. [self updateActiveTimer];
  39. [self startActiveTimer];
  40. [self updateWindow];
  41. [self registerTelephonyEvent];
  42. [[NSNotificationCenter defaultCenter] addObserver:self
  43. selector:@selector(onOrientationChanged:)
  44. name:UIApplicationDidChangeStatusBarOrientationNotification
  45. object:nil];
  46. [self addProximityMonitoringObserver];
  47. }
  48. - (void)registerTelephonyEvent {
  49. self.callCenter = [[CTCallCenter alloc] init];
  50. __weak __typeof(self) weakSelf = self;
  51. self.callCenter.callEventHandler = ^(CTCall *call) {
  52. if ([call.callState isEqualToString:CTCallStateConnected]) {
  53. [weakSelf.callSession endCall];
  54. }
  55. };
  56. }
  57. - (void)onOrientationChanged:(NSNotification *)notification {
  58. [self updateWindow];
  59. }
  60. - (void)startActiveTimer {
  61. self.activeTimer = [NSTimer scheduledTimerWithTimeInterval:1
  62. target:self
  63. selector:@selector(updateActiveTimer)
  64. userInfo:nil
  65. repeats:YES];
  66. [self.activeTimer fire];
  67. }
  68. - (void)stopActiveTimer {
  69. if (self.activeTimer) {
  70. [self.activeTimer invalidate];
  71. self.activeTimer = nil;
  72. }
  73. }
  74. - (void)updateActiveTimer {
  75. long sec = [[NSDate date] timeIntervalSince1970] - self.callSession.connectedTime / 1000;
  76. if (self.callSession.state == kWFAVEngineStateConnected && ![self isVideoViewEnabledSession]) {
  77. NSString *timeStr;
  78. if (sec < 60 * 60) {
  79. timeStr = [NSString stringWithFormat:@"%02ld:%02ld", sec / 60, sec % 60];
  80. } else {
  81. timeStr = [NSString stringWithFormat:@"%02ld:%02ld:%02ld", sec / 60 / 60, (sec / 60) % 60, sec % 60];
  82. }
  83. [self.floatingButton setTitle:timeStr forState:UIControlStateNormal];
  84. [self layoutTextUnderImageButton:self.floatingButton];
  85. }
  86. }
  87. - (void)updateWindow {
  88. CGFloat posX = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosX] floatValue];
  89. CGFloat posY = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosY] floatValue];
  90. posX = posX ? posX : 30;
  91. posY = posY ? posY : 30;
  92. CGRect screenBounds = [UIScreen mainScreen].bounds;
  93. posX = (posX + 30) > screenBounds.size.width ? (screenBounds.size.width - 30) : posX;
  94. posY = (posY + 48) > screenBounds.size.height ? (screenBounds.size.height - 48) : posY;
  95. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft &&
  96. [self isSupportOrientation:UIInterfaceOrientationLandscapeLeft]) {
  97. self.window.transform = CGAffineTransformMakeRotation(M_PI / 2);
  98. self.window.frame = CGRectMake(posX, posY, 64, 96);
  99. self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
  100. if ([self isVideoViewEnabledSession]) {
  101. self.videoView.frame = CGRectMake(0, 0, 96, 64);
  102. }
  103. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight &&
  104. [self isSupportOrientation:UIInterfaceOrientationLandscapeRight]) {
  105. self.window.transform = CGAffineTransformMakeRotation(-M_PI / 2);
  106. self.window.frame = CGRectMake(posX, posY, 64, 96);
  107. self.floatingButton.frame = CGRectMake(0, 0, 96, 64);
  108. if ([self isVideoViewEnabledSession]) {
  109. self.videoView.frame = CGRectMake(0, 0, 96, 64);
  110. }
  111. } else {
  112. if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown &&
  113. [self isSupportOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
  114. self.window.transform = CGAffineTransformMakeRotation(M_PI);
  115. } else {
  116. self.window.transform = CGAffineTransformMakeRotation(0);
  117. }
  118. self.window.frame = CGRectMake(posX, posY, 64, 96);
  119. self.floatingButton.frame = CGRectMake(0, 0, 64, 96);
  120. if ([self isVideoViewEnabledSession]) {
  121. self.videoView.frame = CGRectMake(0, 0, 64, 96);
  122. }
  123. }
  124. if ([self isVideoViewEnabledSession]) {
  125. if (self.callSession.state == kWFAVEngineStateConnected) {
  126. [self.callSession setupRemoteVideoView:self.videoView scalingType:kWFAVVideoScalingTypeAspectBalanced];
  127. [self.callSession setupLocalVideoView:nil scalingType:kWFAVVideoScalingTypeAspectBalanced];
  128. } else if (self.callSession.state == kWFAVEngineStateIdle) {
  129. UILabel *videoStopTips =
  130. [[UILabel alloc] initWithFrame:CGRectMake(0, self.videoView.frame.size.height / 2 - 10,
  131. self.videoView.frame.size.width, 20)];
  132. videoStopTips.textAlignment = NSTextAlignmentCenter;
  133. videoStopTips.text = @"已结束";
  134. videoStopTips.textColor = HEXCOLOR(0x0195ff);
  135. [self.videoView addSubview:videoStopTips];
  136. }
  137. } else {
  138. if (self.callSession.state == kWFAVEngineStateConnected) {
  139. [self.floatingButton setBackgroundColor:[UIColor clearColor]];
  140. } else if (self.callSession.state == kWFAVEngineStateIdle) {
  141. [self.floatingButton setTitle:@"已结束"
  142. forState:UIControlStateNormal];
  143. }
  144. }
  145. }
  146. - (UIWindow *)window {
  147. if (!_window) {
  148. CGFloat posX = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosX] floatValue];
  149. CGFloat posY = [[[NSUserDefaults standardUserDefaults] objectForKey:kFloatingWindowPosY] floatValue];
  150. posX = (posX - 30) ? posX : 30;
  151. posY = (posY - 48) ? posY : 48;
  152. CGRect screenBounds = [UIScreen mainScreen].bounds;
  153. posX = (posX + 30) > screenBounds.size.width ? (screenBounds.size.width - 30) : posX;
  154. posY = (posY + 48) > screenBounds.size.height ? (screenBounds.size.height - 48) : posY;
  155. _window = [[UIWindow alloc] initWithFrame:CGRectMake(posX, posY, 64, 96)];
  156. _window.backgroundColor = [UIColor whiteColor];
  157. _window.windowLevel = UIWindowLevelAlert + 1;
  158. _window.layer.cornerRadius = 4;
  159. _window.layer.masksToBounds = YES;
  160. _window.layer.borderWidth = 1;
  161. _window.layer.borderColor = [HEXCOLOR(0x0A88E1) CGColor];
  162. [_window makeKeyAndVisible]; //关键语句,显示window
  163. UIPanGestureRecognizer *panGestureRecognizer =
  164. [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGestures:)];
  165. panGestureRecognizer.minimumNumberOfTouches = 1;
  166. panGestureRecognizer.maximumNumberOfTouches = 1;
  167. [_window addGestureRecognizer:panGestureRecognizer];
  168. }
  169. return _window;
  170. }
  171. - (UIView *)videoView {
  172. if (!_videoView) {
  173. _videoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 64, 96)];
  174. _videoView.backgroundColor = [UIColor blackColor];
  175. CGRect windowFrame = self.window.frame;
  176. windowFrame.size.width = _videoView.frame.size.width;
  177. windowFrame.size.height = _videoView.frame.size.height;
  178. self.window.frame = windowFrame;
  179. [self.window addSubview:_videoView];
  180. UITapGestureRecognizer *tap =
  181. [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchedWindow:)];
  182. [_videoView addGestureRecognizer:tap];
  183. }
  184. return _videoView;
  185. }
  186. - (UIButton *)floatingButton {
  187. if (!_floatingButton) {
  188. _floatingButton = [UIButton buttonWithType:UIButtonTypeCustom];
  189. if (false /*self.callSession.mediaType == Audio*/) {
  190. [_floatingButton setImage:[UIImage imageNamed:@"floatingaudio"]
  191. forState:UIControlStateNormal];
  192. } else {
  193. [_floatingButton setImage:[UIImage imageNamed:@"floatingvideo"]
  194. forState:UIControlStateNormal];
  195. }
  196. [_floatingButton setTitle:@"" forState:UIControlStateNormal];
  197. _floatingButton.backgroundColor = [UIColor clearColor];
  198. _floatingButton.frame = CGRectMake(0, 0, 64, 96);
  199. CGRect windowFrame = self.window.frame;
  200. windowFrame.size.width = _floatingButton.frame.size.width;
  201. windowFrame.size.height = _floatingButton.frame.size.height;
  202. self.window.frame = windowFrame;
  203. [_floatingButton addTarget:self action:@selector(touchedWindow:) forControlEvents:UIControlEventTouchUpInside];
  204. [self.window addSubview:_floatingButton];
  205. }
  206. return _floatingButton;
  207. }
  208. - (BOOL)isSupportOrientation:(UIInterfaceOrientation)orientation {
  209. UIInterfaceOrientationMask mask =
  210. [[UIApplication sharedApplication] supportedInterfaceOrientationsForWindow:self.window];
  211. return mask & (1 << orientation);
  212. }
  213. - (void)handlePanGestures:(UIPanGestureRecognizer *)paramSender {
  214. if (paramSender.state != UIGestureRecognizerStateEnded && paramSender.state != UIGestureRecognizerStateFailed) {
  215. CGPoint location = [paramSender locationInView:[UIApplication sharedApplication].windows[0]];
  216. if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeLeft &&
  217. [self isSupportOrientation:UIInterfaceOrientationLandscapeLeft]) {
  218. CGFloat tmp = location.x;
  219. location.x = [UIScreen mainScreen].bounds.size.height - location.y;
  220. location.y = tmp;
  221. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationLandscapeRight &&
  222. [self isSupportOrientation:UIInterfaceOrientationLandscapeRight]) {
  223. CGFloat tmp = location.x;
  224. location.x = location.y;
  225. location.y = [UIScreen mainScreen].bounds.size.width - tmp;
  226. } else if ([UIDevice currentDevice].orientation == UIDeviceOrientationPortraitUpsideDown &&
  227. [self isSupportOrientation:UIInterfaceOrientationPortraitUpsideDown]) {
  228. CGFloat tmp = location.x;
  229. location.x = [UIScreen mainScreen].bounds.size.height - location.y;
  230. location.y = [UIScreen mainScreen].bounds.size.width - tmp;
  231. }
  232. CGRect frame = self.window.frame;
  233. frame.origin.x = location.x - frame.size.width / 2;
  234. frame.origin.y = location.y - frame.size.height / 2;
  235. if (frame.origin.x < 0) {
  236. frame.origin.x = 2;
  237. }
  238. if (frame.origin.y < 0) {
  239. frame.origin.y = 2;
  240. }
  241. CGRect screenBounds = [UIScreen mainScreen].bounds;
  242. BOOL isLandscape = screenBounds.size.width > screenBounds.size.height;
  243. if (isLandscape && [self isSupportOrientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation]) {
  244. if (frame.origin.y + frame.size.height > [UIScreen mainScreen].bounds.size.width) {
  245. frame.origin.y = [UIScreen mainScreen].bounds.size.width - 2 - frame.size.height;
  246. }
  247. if (frame.origin.x + frame.size.width > [UIScreen mainScreen].bounds.size.height) {
  248. frame.origin.x = [UIScreen mainScreen].bounds.size.height - 2 - frame.size.width;
  249. }
  250. } else {
  251. if (frame.origin.x + frame.size.width > [UIScreen mainScreen].bounds.size.width) {
  252. frame.origin.x = [UIScreen mainScreen].bounds.size.width - 2 - frame.size.width;
  253. }
  254. if (frame.origin.y + frame.size.height > [UIScreen mainScreen].bounds.size.height) {
  255. frame.origin.y = [UIScreen mainScreen].bounds.size.height - 2 - frame.size.height;
  256. }
  257. }
  258. self.window.frame = frame;
  259. } else if (paramSender.state == UIGestureRecognizerStateEnded) {
  260. CGRect frame = self.window.frame;
  261. [[NSUserDefaults standardUserDefaults] setObject:@(frame.origin.x) forKey:kFloatingWindowPosX];
  262. [[NSUserDefaults standardUserDefaults] setObject:@(frame.origin.y) forKey:kFloatingWindowPosY];
  263. [[NSUserDefaults standardUserDefaults] synchronize];
  264. }
  265. }
  266. - (void)touchedWindow:(id)sender {
  267. [self hideCallFloatingWindow];
  268. if (self.touchedBlock) {
  269. self.touchedBlock(self.callSession);
  270. }
  271. [self clearCallFloatingWindow];
  272. }
  273. - (void)hideCallFloatingWindow {
  274. [self stopActiveTimer];
  275. if (_videoView) {
  276. [_videoView removeFromSuperview];
  277. _videoView = nil;
  278. }
  279. if (_floatingButton) {
  280. [_floatingButton removeFromSuperview];
  281. _floatingButton = nil;
  282. }
  283. [_window setHidden:YES];
  284. }
  285. - (void)clearCallFloatingWindow {
  286. [[NSNotificationCenter defaultCenter] removeObserver:self];
  287. _activeTimer = nil;
  288. _callSession = nil;
  289. _touchedBlock = nil;
  290. _floatingButton = nil;
  291. _videoView = nil;
  292. _window = nil;
  293. staticWindow = nil;
  294. }
  295. - (void)layoutTextUnderImageButton:(UIButton *)button {
  296. [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
  297. [button setTitleColor:HEXCOLOR(0x0195ff) forState:UIControlStateNormal];
  298. button.titleEdgeInsets = UIEdgeInsetsMake(0, -button.imageView.frame.size.width,
  299. -button.imageView.frame.size.height - 6 / 2, 0);
  300. // button.imageEdgeInsets =
  301. // UIEdgeInsetsMake(-button.titleLabel.frame.size.height-offset/2, 0, 0,
  302. // -button.titleLabel.frame.size.width);
  303. // 由于iOS8中titleLabel的size为0,用上面这样设置有问题,修改一下即可
  304. button.imageEdgeInsets = UIEdgeInsetsMake(-button.titleLabel.intrinsicContentSize.height - 6 / 2,
  305. 0, 0, -button.titleLabel.intrinsicContentSize.width);
  306. }
  307. - (BOOL)isVideoViewEnabledSession {
  308. if (YES/*self.callSession.mediaType == video && !self.callSession.isMultiCall*/) {
  309. return YES;
  310. } else {
  311. return NO;
  312. }
  313. }
  314. #pragma mark - WFAVCallSessionDelegate
  315. - (void)didChangeState:(WFAVEngineState)state {
  316. switch (state) {
  317. case kWFAVEngineStateIdle:
  318. [self updateWindow];
  319. [self performSelector:@selector(clearCallFloatingWindow) withObject:nil afterDelay:2];
  320. [self removeProximityMonitoringObserver];
  321. break;
  322. case kWFAVEngineStateConnected:
  323. [self updateWindow];
  324. break;
  325. default:
  326. break;
  327. }
  328. }
  329. - (void)didCallEndWithReason:(WFAVCallEndReason)reason {
  330. }
  331. - (void)didError:(NSError *)error {
  332. }
  333. - (void)didGetStats:(NSArray *)stats {
  334. }
  335. - (void)didCreateLocalVideoTrack:(RTCVideoTrack *)localVideoTrack {
  336. }
  337. - (void)didReceiveRemoteVideoTrack:(RTCVideoTrack *)remoteVideoTrack {
  338. }
  339. - (void)didChangeMode:(BOOL)isAudioOnly {
  340. [self.videoView removeFromSuperview];
  341. [self initWindow];
  342. }
  343. - (void)addProximityMonitoringObserver {
  344. [UIDevice currentDevice].proximityMonitoringEnabled = YES;
  345. [[NSNotificationCenter defaultCenter] addObserver:self
  346. selector:@selector(proximityStatueChanged:)
  347. name:UIDeviceProximityStateDidChangeNotification
  348. object:nil];
  349. }
  350. - (void)removeProximityMonitoringObserver {
  351. [UIDevice currentDevice].proximityMonitoringEnabled = NO;
  352. [[NSNotificationCenter defaultCenter] removeObserver:self
  353. name:UIDeviceProximityStateDidChangeNotification
  354. object:nil];
  355. }
  356. - (void)proximityStatueChanged:(NSNotificationCenter *)notification {
  357. }
  358. @end