acceptCallViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. //
  2. // acceptCallViewController.m
  3. // shiku_im
  4. //
  5. // Created by MacZ on 2017/8/7.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "acceptCallViewController.h"
  9. #import "JXCustomButton.h"
  10. #import "JXAVCallViewController.h"
  11. #import "JXChatViewController.h"
  12. @interface acceptCallViewController ()<JXActionSheetVCDelegate>
  13. @property (strong, nonatomic) UIView *viewTop;
  14. @property (strong, nonatomic) UIImageView *headerImage;
  15. @property (strong, nonatomic) UILabel *labelStatus;
  16. @property (strong, nonatomic) UILabel *labelRemoteParty;
  17. @property (strong, nonatomic) UIView *viewCenter;
  18. @property (strong, nonatomic) UIImageView *imageSecure;
  19. @property (nonatomic, strong) UIImageView *animationImgV;
  20. @property (strong, nonatomic) UIView *viewBottom;
  21. @property (strong, nonatomic) UIButton *buttonHangup;
  22. @property (strong, nonatomic) UIButton *buttonSendmsg;
  23. @property (strong, nonatomic) UIButton *buttonChangeaccept;
  24. @property (nonatomic, strong) NSTimer *timer;
  25. @property (nonatomic, assign) int timerNum;
  26. @property (nonatomic, strong) JXActionSheetVC *callSendMsgView;
  27. @property (nonatomic, strong) NSMutableArray *msgArray;
  28. @end
  29. #define Button_Width 63
  30. #define Button_Height 63
  31. #define BtnImage_big 63
  32. #define BtnImage_small 34
  33. @implementation acceptCallViewController
  34. - (id)init
  35. {
  36. self = [super init];
  37. if (self) {
  38. self.isGotoBack = YES;
  39. self.heightHeader = 0;
  40. self.heightFooter = 0;
  41. self.view.frame = g_window.bounds;
  42. [self createHeadAndFoot];
  43. [self customView];
  44. JXUserObject *user = [[JXUserObject sharedInstance] getUserById:self.toUserId];
  45. if ([user.offlineNoPushMsg intValue] != 1) {
  46. _player = [[JXAudioPlayer alloc]init];
  47. _player.isOpenProximityMonitoring = NO;
  48. _player.audioFile = [NSString stringWithFormat:@"%@Ring.mp3",imageFilePath];
  49. [_player open];
  50. [_player play];
  51. _player.player.numberOfLoops = 10000;
  52. }
  53. self.msgArray = [NSMutableArray arrayWithObjects:Localized(@"JX_Customize"), Localized(@"JX_NoTimePleaseContactMeLater"),Localized(@"JX_MeetingPleaseContactMeLater"),Localized(@"JX_InconvenientTextContact") ,nil];
  54. g_meeting.isMeeting = YES;
  55. [g_notify addObserver:self selector:@selector(newMsgCome:) name:kXMPPNewMsgNotifaction object:nil];
  56. [g_notify addObserver:self selector:@selector(callAnswerNotification:) name:kCallAnswerNotification object:nil];
  57. [g_notify addObserver:self selector:@selector(callEndNotification:) name:kCallEndNotification object:nil];
  58. }
  59. return self;
  60. }
  61. -(void)callAnswerNotification:(NSNotification *)notifacation{
  62. [self doInCall];
  63. }
  64. - (void)doInCall {
  65. NSLog(@"callAnswer - callView");
  66. if (g_meeting.hasAnswer) {
  67. g_meeting.hasAnswer = NO;
  68. JXAVCallViewController *avVC = [[JXAVCallViewController alloc] init];
  69. if([self.type intValue] == kWCMessageTypeAudioMeetingInvite || [self.type intValue] == kWCMessageTypeAudioChatAsk){
  70. avVC.isAudio = YES;
  71. }
  72. if([self.type intValue] == kWCMessageTypeAudioMeetingInvite || [self.type intValue] == kWCMessageTypeVideoMeetingInvite){
  73. avVC.isGroup = YES;
  74. avVC.roomNum = self.roomNum;
  75. }else if ([self.type intValue] == kWCMessageTypeAudioChatAsk) {
  76. avVC.roomNum = self.roomNum;
  77. [g_meeting sendAccept:kWCMessageTypeAudioChatAccept toUserId:self.toUserId toUserName:self.toUserName objectId:self.roomNum];
  78. }else if ([self.type intValue] == kWCMessageTypeVideoChatAsk) {
  79. avVC.roomNum = self.roomNum;
  80. [g_meeting sendAccept:kWCMessageTypeVideoChatAccept toUserId:self.toUserId toUserName:self.toUserName objectId:self.roomNum];
  81. }
  82. avVC.toUserId = self.toUserId;
  83. avVC.toUserName = self.toUserName;
  84. avVC.view.frame = [UIScreen mainScreen].bounds;
  85. [g_window addSubview:avVC.view];
  86. [_player stop];
  87. _player = nil;
  88. [self actionQuit];
  89. }
  90. }
  91. -(void)callEndNotification:(NSNotification *)notifacation{
  92. [self onCancel];
  93. }
  94. - (void) customView {
  95. self.tableBody.backgroundColor = [UIColor whiteColor];
  96. _labelRemoteParty = [[UILabel alloc] init];
  97. _labelRemoteParty.frame = CGRectMake(0, 129, JX_SCREEN_WIDTH, 25);
  98. _labelRemoteParty.textColor = HEXCOLOR(0x333333);
  99. _labelRemoteParty.font = [UIFont systemFontOfSize:24];
  100. _labelRemoteParty.textAlignment = NSTextAlignmentCenter;
  101. _labelRemoteParty.text = self.toUserName;
  102. [self.tableBody addSubview:_labelRemoteParty];
  103. _labelStatus = [[UILabel alloc] init];
  104. _labelStatus.frame = CGRectMake(0, CGRectGetMaxY(_labelRemoteParty.frame) + 20, JX_SCREEN_WIDTH, 18);
  105. _labelStatus.textColor = HEXCOLOR(0x333333);
  106. _labelStatus.font = [UIFont systemFontOfSize:17];
  107. _labelStatus.textAlignment = NSTextAlignmentCenter;
  108. [self.tableBody addSubview:_labelStatus];
  109. // NSString *str;
  110. // if ([self.type intValue] == kWCMessageTypeAudioChatAsk) {
  111. // str = @"邀请您语音通话...";
  112. // }else if ([self.type intValue] == kWCMessageTypeVideoChatAsk) {
  113. // str = @"邀请您视频通话...";
  114. // }
  115. _labelStatus.text = self.title;
  116. //viewHeader viewTop
  117. // _viewTop = [[UIView alloc] init];
  118. // _viewTop.frame = CGRectMake(0, 40, JX_SCREEN_WIDTH, 67);
  119. // _viewTop.userInteractionEnabled = YES;
  120. // _viewTop.center = CGPointMake(self.view.frame.size.width / 2, 0);
  121. // [self.tableBody addSubview:_viewTop];
  122. //viewFooter viewBottom
  123. // _viewBottom = [[UIView alloc] init];
  124. // _viewBottom.frame = CGRectMake(0,CGRectGetMaxY(_labelStatus.frame), JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT - CGRectGetMaxY(_labelStatus.frame));
  125. // _viewBottom.userInteractionEnabled = YES;
  126. // [self.tableBody addSubview:_viewBottom];
  127. // CGFloat margX = 20;
  128. // CGFloat margWidth = (JX_SCREEN_WIDTH-(4*Button_Width+margX*2))/3;
  129. CGFloat y = (JX_SCREEN_HEIGHT - CGRectGetMaxY(_labelStatus.frame)-63*2-175)/2;
  130. // 发消息
  131. _buttonSendmsg = [self createBottomButtonWithImage:@"sendmsg" SelectedImg:nil selector:@selector(onSendmsg) btnWidth:Button_Width imageWidth:BtnImage_big];
  132. [_buttonSendmsg setTitle:Localized(@"JX_SendMessage") forState:UIControlStateNormal];
  133. _buttonSendmsg.frame = CGRectMake(48, CGRectGetMaxY(_labelStatus.frame)+y, Button_Width, Button_Height);
  134. // 切换语音/视频
  135. if ([self.type intValue] == kWCMessageTypeVideoChatAsk) {
  136. _buttonChangeaccept = [self createBottomButtonWithImage:@"changeaccept" SelectedImg:nil selector:@selector(onChangeaccept) btnWidth:Button_Width imageWidth:BtnImage_big];
  137. [_buttonChangeaccept setTitle:Localized(@"JX_SwitchVoiceCall") forState:UIControlStateNormal];
  138. }else{
  139. _buttonChangeaccept = [self createBottomButtonWithImage:@"switch_video_calls" SelectedImg:nil selector:@selector(onChangeaccept) btnWidth:Button_Width imageWidth:BtnImage_big];
  140. [_buttonChangeaccept setTitle:Localized(@"JX_SwitchVideoCall") forState:UIControlStateNormal];
  141. }
  142. _buttonChangeaccept.frame = CGRectMake(JX_SCREEN_WIDTH-Button_Width-48, _buttonSendmsg.frame.origin.y, Button_Width, Button_Height);
  143. if (self.isGroup) {
  144. [self hiddenSendmsgAndChangeaccept];
  145. }
  146. NSMutableArray *images = [[NSMutableArray alloc] init];
  147. for (int i = 1; i <= 3; i ++) {
  148. UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:@"Talk_Animation_%d",i]];
  149. [images addObject:img];
  150. }
  151. _animationImgV = [[UIImageView alloc] initWithFrame:CGRectMake((JX_SCREEN_WIDTH-145)/2, CGRectGetMaxY(_buttonSendmsg.frame)+15, 145, 145)];
  152. _animationImgV.animationImages = images;
  153. _animationImgV.animationDuration = 1.5f;
  154. [self.tableBody addSubview:_animationImgV];
  155. [_animationImgV startAnimating];
  156. _headerImage = [[UIImageView alloc] init];
  157. _headerImage.frame = CGRectMake((_animationImgV.frame.size.width-100)/2, (_animationImgV.frame.size.height-100)/2, 100, 100);
  158. _headerImage.userInteractionEnabled = YES;
  159. _headerImage.layer.cornerRadius = _headerImage.frame.size.width / 2;
  160. _headerImage.layer.masksToBounds = YES;
  161. [g_server getHeadImageLarge:self.toUserId userName:self.toUserName imageView:_headerImage];
  162. [_animationImgV addSubview:_headerImage];
  163. // 挂断
  164. _buttonHangup = [self createBottomButtonWithImage:@"hang_up" SelectedImg:nil selector:@selector(onCancel) btnWidth:Button_Width imageWidth:BtnImage_big];
  165. [_buttonHangup setTitle:Localized(@"JXMeeting_Hangup") forState:UIControlStateNormal];
  166. _buttonHangup.frame = CGRectMake(48, CGRectGetMaxY(_buttonSendmsg.frame) + 175, Button_Width, Button_Height);
  167. // 接听
  168. _buttonAccept = [self createBottomButtonWithImage:@"answer_icon" SelectedImg:nil selector:@selector(onAcceptCall) btnWidth:Button_Width imageWidth:BtnImage_big];
  169. [_buttonAccept setTitle:Localized(@"JXMeeting_Accept") forState:UIControlStateNormal];
  170. _buttonAccept.frame = CGRectMake(JX_SCREEN_WIDTH-Button_Width-48, _buttonHangup.frame.origin.y, Button_Width, Button_Height);
  171. self.tableBody.contentSize = CGSizeMake(0, 0);
  172. }
  173. - (void)hiddenSendmsgAndChangeaccept{
  174. _buttonSendmsg.hidden = YES;
  175. _buttonChangeaccept.hidden = YES;
  176. }
  177. - (void)viewDidLoad {
  178. [super viewDidLoad];
  179. _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
  180. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  181. [self doInCall];
  182. });
  183. }
  184. // 30秒无响应 自动挂断
  185. - (void)timerAction:(NSTimer *)timer {
  186. _timerNum ++;
  187. NSLog(@"timerNum = %d", _timerNum);
  188. if (_timerNum > 30) {
  189. [_animationImgV stopAnimating];
  190. [timer invalidate];
  191. timer = nil;
  192. _timerNum = 0;
  193. [self onCancel];
  194. }
  195. }
  196. -(JXCustomButton *)createBottomButtonWithImage:(NSString *)Image SelectedImg:(NSString *)selectedImage selector:(SEL)selector btnWidth:(CGFloat)btnWidth imageWidth:(CGFloat)imageWidth{
  197. JXCustomButton * button = [JXCustomButton buttonWithType:UIButtonTypeCustom];
  198. [button setImage:[UIImage imageNamed:Image] forState:UIControlStateNormal];
  199. [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
  200. [button.titleLabel setFont:g_factory.font12];
  201. [button.titleLabel setTextAlignment:NSTextAlignmentCenter];
  202. button.titleRect = CGRectMake(0, imageWidth+(btnWidth-imageWidth)/2, btnWidth, 20);
  203. button.imageRect = CGRectMake((btnWidth-imageWidth)/2, 0, imageWidth, imageWidth);
  204. if (selector)
  205. [button addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
  206. [self.tableBody addSubview:button];
  207. return button;
  208. }
  209. #pragma mark 接受新消息广播
  210. -(void)newMsgCome:(NSNotification *)notifacation{
  211. JXMessageObject *msg = (JXMessageObject *)notifacation.object;
  212. if ([msg.type intValue] == kWCMessageTypeAudioChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatEnd || [msg.type intValue] == kWCMessageTypeAudioChatEnd) {
  213. [_player stop];
  214. _player = nil;
  215. g_meeting.isMeeting = NO;
  216. [self actionQuit];
  217. [g_App endCall];
  218. }
  219. // 多点登录处理
  220. if ([msg.fromUserId isEqualToString:MY_USER_ID]) {
  221. if([msg.type intValue] == kWCMessageTypeAudioChatAccept){
  222. [_player stop];
  223. _player = nil;
  224. g_meeting.isMeeting = NO;
  225. [self actionQuit];
  226. [g_App endCall];
  227. }else if ([msg.type intValue] == kWCMessageTypeVideoChatAccept) {
  228. [_player stop];
  229. _player = nil;
  230. g_meeting.isMeeting = NO;
  231. [self actionQuit];
  232. [g_App endCall];
  233. }
  234. if ([msg.type intValue] == kWCMessageTypeAudioChatCancel || [msg.type intValue] == kWCMessageTypeVideoChatCancel) {
  235. [_player stop];
  236. _player = nil;
  237. g_meeting.isMeeting = NO;
  238. [self actionQuit];
  239. [g_App endCall];
  240. }
  241. }
  242. }
  243. -(void)dealloc{
  244. }
  245. - (void)didReceiveMemoryWarning {
  246. [super didReceiveMemoryWarning];
  247. // Dispose of any resources that can be recreated.
  248. }
  249. -(void)onAcceptCall{
  250. [_player stop];
  251. _player = nil;
  252. if(self.delegate != nil && [self.delegate respondsToSelector:self.didTouch])
  253. [self.delegate performSelectorOnMainThread:self.didTouch withObject:self waitUntilDone:NO];
  254. [self actionQuit];
  255. }
  256. -(void)onCancel{
  257. [_player stop];
  258. _player = nil;
  259. g_meeting.hasAnswer = NO;
  260. g_meeting.isMeeting = NO;
  261. [g_App endCall];
  262. if (self.isGroup) {
  263. [self actionQuit];
  264. return;
  265. }
  266. int n;
  267. if([self.type intValue] == kWCMessageTypeAudioChatAsk)
  268. n = kWCMessageTypeAudioChatCancel;
  269. else
  270. n = kWCMessageTypeVideoChatCancel;
  271. [g_meeting sendNoAnswer:n toUserId:self.toUserId toUserName:self.toUserName];
  272. [self actionQuit];
  273. }
  274. - (void)onSendmsg{
  275. self.callSendMsgView = [[JXActionSheetVC alloc] initWithImages:nil names:self.msgArray];
  276. self.callSendMsgView.delegate = self;
  277. [self presentViewController:self.callSendMsgView animated:YES completion:nil];
  278. }
  279. - (void)onChangeaccept{
  280. if ([self.type intValue] == kWCMessageTypeVideoChatAsk) {
  281. [_player stop];
  282. _player = nil;
  283. if(self.delegate != nil && [self.delegate respondsToSelector:self.changeAudio])
  284. [self.delegate performSelectorOnMainThread:self.changeAudio withObject:self waitUntilDone:NO];
  285. [self actionQuit];
  286. }else{
  287. [_player stop];
  288. _player = nil;
  289. if(self.delegate != nil && [self.delegate respondsToSelector:self.changeVideo])
  290. [self.delegate performSelectorOnMainThread:self.changeVideo withObject:self waitUntilDone:NO];
  291. [self actionQuit];
  292. }
  293. }
  294. - (void)actionSheet:(JXActionSheetVC *)actionSheet didButtonWithIndex:(NSInteger)index{
  295. if (index == 0) {
  296. [self sendCustomMsg];
  297. }else{
  298. NSString *msg = self.msgArray[index];
  299. [self sendMsg:msg];
  300. }
  301. }
  302. - (void)sendMsg:(NSString *)Msg{
  303. [self onCancel];
  304. JXMessageObject *message = [[JXMessageObject alloc] init];
  305. message.fromUserId = MY_USER_ID;
  306. message.fromUserName = MY_USER_NAME;
  307. message.toUserId = self.toUserId;
  308. message.toUserName = self.toUserName;
  309. message.timeSend = [NSDate date];
  310. message.type = [NSNumber numberWithInt:kWCMessageTypeText];
  311. message.isSend = [NSNumber numberWithInt:transfer_status_ing];
  312. message.isRead = [NSNumber numberWithBool:NO];
  313. message.content = Msg;
  314. message.isGroup = NO;
  315. message.isReadDel = [NSNumber numberWithBool:NO];
  316. [message insert:message.toUserId];
  317. if ([message.toUserId isEqualToString:current_chat_userId]) {
  318. [g_notify postNotificationName:UpdateAcceptCallMsg object:message];
  319. }else{
  320. [g_xmpp sendMessage:message roomName:nil];
  321. }
  322. }
  323. - (void)sendCustomMsg{
  324. [self onCancel];
  325. JXUserObject *user = [[JXUserObject alloc] init];
  326. user.userId = self.toUserId;
  327. if ([user.userId isEqualToString:current_chat_userId]) {
  328. }else{
  329. JXChatViewController *chatView = [[JXChatViewController alloc] init];
  330. chatView.chatPerson = user;
  331. [g_navigation pushViewController:chatView animated:YES];
  332. }
  333. }
  334. -(void)actionQuit{
  335. [g_notify removeObserver:self];//移除监听
  336. [_timer invalidate];
  337. _timer = nil;
  338. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  339. [super actionQuit];
  340. });
  341. }
  342. @end