3939
4040#import < AudioToolbox/AudioToolbox.h>
4141
42+ #import " CallViewController.h"
43+
4244// #define MX_CALL_STACK_OPENWEBRTC
4345#ifdef MX_CALL_STACK_OPENWEBRTC
4446#import < MatrixOpenWebRTCWrapper/MatrixOpenWebRTCWrapper.h>
5658#define MAKE_NS_STRING (x ) @MAKE_STRING(x)
5759
5860NSString *const kAppDelegateDidTapStatusBarNotification = @" kAppDelegateDidTapStatusBarNotification" ;
61+ NSString *const kAppDelegateNetworkStatusDidChangeNotification = @" kAppDelegateNetworkStatusDidChangeNotification" ;
5962
6063@interface AppDelegate ()
6164{
@@ -88,7 +91,7 @@ @interface AppDelegate ()
8891 /* *
8992 The current call view controller (if any).
9093 */
91- MXKCallViewController *currentCallViewController;
94+ CallViewController *currentCallViewController;
9295
9396 /* *
9497 Call status window displayed when user goes back to app during a call.
@@ -148,6 +151,7 @@ The room id of the current handled remote notification (if any)
148151}
149152
150153@property (strong , nonatomic ) MXKAlert *mxInAppNotification;
154+ @property (strong , nonatomic ) MXKAlert *incomingCallNotification;
151155
152156@end
153157
@@ -222,7 +226,12 @@ - (void)setIsOffline:(BOOL)isOffline
222226 }];
223227 }
224228
225- _isOffline = isOffline;
229+ if (_isOffline != isOffline)
230+ {
231+ _isOffline = isOffline;
232+
233+ [[NSNotificationCenter defaultCenter ] postNotificationName: kAppDelegateNetworkStatusDidChangeNotification object: nil ];
234+ }
226235}
227236
228237#pragma mark - UIApplicationDelegate
@@ -362,7 +371,6 @@ - (void)applicationDidEnterBackground:(UIApplication *)application
362371 }
363372 [[AFNetworkReachabilityManager sharedManager ] setReachabilityStatusChangeBlock: nil ];
364373 [[AFNetworkReachabilityManager sharedManager ] stopMonitoring ];
365- _isOffline = NO ;
366374
367375 // check if some media must be released to reduce the cache size
368376 [MXKMediaManager reduceCacheSizeToInsert: 0 ];
@@ -482,7 +490,7 @@ - (void)applicationWillTerminate:(UIApplication *)application
482490
483491- (BOOL )application : (UIApplication *)application continueUserActivity : (NSUserActivity *)userActivity restorationHandler : (void (^)(NSArray * _Nullable))restorationHandler
484492{
485- BOOL continueUserActivity;
493+ BOOL continueUserActivity = NO ;
486494
487495 if ([userActivity.activityType isEqualToString: NSUserActivityTypeBrowsingWeb ])
488496 {
@@ -1267,6 +1275,9 @@ - (void)initMatrixSessions
12671275 [self enableNoVoIPOnMatrixSession: mxSession];
12681276 }
12691277
1278+ // Ignore the room member profile changes during the last message process in each room.
1279+ mxSession.ignoreProfileChangesDuringLastMessageProcessing = YES ;
1280+
12701281 // Each room member will be considered as a potential contact.
12711282 [MXKContactManager sharedManager ].contactManagerMXRoomSource = MXKContactManagerMXRoomSourceAll;
12721283 }
@@ -1356,6 +1367,15 @@ - (void)initMatrixSessions
13561367
13571368 }];
13581369
1370+ [[NSNotificationCenter defaultCenter ] addObserverForName: kMXSessionDidCorruptDataNotification object: nil queue: [NSOperationQueue mainQueue ] usingBlock: ^(NSNotification * _Nonnull notif) {
1371+
1372+ NSLog (@" [AppDelegate] kMXSessionDidCorruptDataNotification received. Reload the app" );
1373+
1374+ // Reload entirely the app when a session has corrupted its data
1375+ [[AppDelegate theDelegate ] reloadMatrixSessions: YES ];
1376+
1377+ }];
1378+
13591379 // Observe settings changes
13601380 [[MXKAppSettings standardAppSettings ] addObserver: self forKeyPath: @" showAllEventsInRoomHistory" options: 0 context: nil ];
13611381
@@ -1517,17 +1537,88 @@ - (void)addMatrixCallObserver
15171537 {
15181538 MXCall *mxCall = (MXCall*)notif.object ;
15191539
1520- currentCallViewController = [MXKCallViewController callViewController: mxCall];
1540+ // Prepare the call view controller
1541+ currentCallViewController = [CallViewController callViewController: mxCall];
15211542 currentCallViewController.delegate = self;
15221543
1523- // FIXME GFO Check whether present call from self.window.rootViewController is working
1524- [self .window.rootViewController presentViewController: currentCallViewController animated: YES completion: ^{
1525- currentCallViewController.isPresented = YES ;
1526- }];
1527-
1528- // Hide system status bar
1529- [UIApplication sharedApplication ].statusBarHidden = YES ;
1544+ if (mxCall.isIncoming )
1545+ {
1546+ // Prompt user before presenting the call view controller
1547+ NSString *callPromptFormat = mxCall.isVideoCall ? NSLocalizedStringFromTable(@" call_incoming_video_prompt" , @" Vector" , nil ) : NSLocalizedStringFromTable(@" call_incoming_voice_prompt" , @" Vector" , nil );
1548+ NSString *callerName = currentCallViewController.peer .displayname ;
1549+ if (!callerName.length )
1550+ {
1551+ callerName = currentCallViewController.peer .userId ;
1552+ }
1553+ NSString *callPrompt = [NSString stringWithFormat: callPromptFormat, callerName];
1554+
1555+ __weak typeof (self) weakSelf = self;
1556+
1557+ // Removing existing notification (if any)
1558+ [_incomingCallNotification dismiss: NO ];
1559+
1560+
1561+
1562+ _incomingCallNotification = [[MXKAlert alloc ] initWithTitle: callPrompt
1563+ message: nil
1564+ style: MXKAlertStyleAlert];
1565+
1566+ _incomingCallNotification.cancelButtonIndex = [_incomingCallNotification addActionWithTitle: NSLocalizedStringFromTable(@" decline" , @" Vector" , nil )
1567+ style: MXKAlertActionStyleDefault
1568+ handler: ^(MXKAlert *alert) {
1569+
1570+ if (weakSelf)
1571+ {
1572+ __strong __typeof (weakSelf)strongSelf = weakSelf;
1573+
1574+ strongSelf.incomingCallNotification = nil ;
1575+
1576+ [strongSelf->currentCallViewController onButtonPressed: strongSelf->currentCallViewController.rejectCallButton];
1577+
1578+ mxCall.delegate = nil ;
1579+ currentCallViewController = nil ;
1580+ }
1581+
1582+ }];
1583+
1584+ [_incomingCallNotification addActionWithTitle: NSLocalizedStringFromTable(@" accept" , @" Vector" , nil )
1585+ style: MXKAlertActionStyleDefault
1586+ handler: ^(MXKAlert *alert) {
1587+ if (weakSelf)
1588+ {
1589+ __strong __typeof (weakSelf)strongSelf = weakSelf;
1590+
1591+ strongSelf.incomingCallNotification = nil ;
1592+
1593+ [strongSelf->currentCallViewController onButtonPressed: strongSelf->currentCallViewController.answerCallButton];
1594+
1595+ [strongSelf.window.rootViewController presentViewController: strongSelf->currentCallViewController animated: YES completion: ^{
1596+
1597+ strongSelf->currentCallViewController .isPresented = YES ;
1598+
1599+ }];
1600+
1601+ // Hide system status bar
1602+ [UIApplication sharedApplication ].statusBarHidden = YES ;
1603+
1604+ }
1605+ }];
1606+
1607+ [_incomingCallNotification showInViewController: self .window.rootViewController];
1608+ }
1609+ else
1610+ {
1611+ [self .window.rootViewController presentViewController: currentCallViewController animated: YES completion: ^{
1612+
1613+ currentCallViewController.isPresented = YES ;
1614+
1615+ }];
1616+
1617+ // Hide system status bar
1618+ [UIApplication sharedApplication ].statusBarHidden = YES ;
1619+ }
15301620 }
1621+
15311622 }];
15321623}
15331624
@@ -1776,12 +1867,28 @@ - (void)startPrivateOneToOneRoomWithUserId:(NSString*)userId completion:(void (^
17761867
17771868#pragma mark - MXKCallViewControllerDelegate
17781869
1779- - (void )dismissCallViewController : (MXKCallViewController *)callViewController
1870+ - (void )dismissCallViewController : (MXKCallViewController *)callViewController completion : ( void (^)()) completion
17801871{
17811872 if (callViewController == currentCallViewController)
17821873 {
1783-
1784- if (callViewController.isPresented )
1874+ if (_incomingCallNotification)
1875+ {
1876+ // The user was prompted for an incoming call which ended
1877+ // The call view controller was not presented yet.
1878+ [_incomingCallNotification dismiss: NO ];
1879+ _incomingCallNotification = nil ;
1880+
1881+ // Release properly
1882+ currentCallViewController.mxCall .delegate = nil ;
1883+ currentCallViewController.delegate = nil ;
1884+ currentCallViewController = nil ;
1885+
1886+ if (completion)
1887+ {
1888+ completion ();
1889+ }
1890+ }
1891+ else if (callViewController.isPresented )
17851892 {
17861893 BOOL callIsEnded = (callViewController.mxCall .state == MXCallStateEnded);
17871894 NSLog (@" Call view controller is dismissed (%d )" , callIsEnded);
@@ -1793,6 +1900,11 @@ - (void)dismissCallViewController:(MXKCallViewController *)callViewController
17931900 {
17941901 [self addCallStatusBar ];
17951902 }
1903+
1904+ if (completion)
1905+ {
1906+ completion ();
1907+ }
17961908 }];
17971909
17981910 if (callIsEnded)
@@ -1807,12 +1919,13 @@ - (void)dismissCallViewController:(MXKCallViewController *)callViewController
18071919 currentCallViewController.delegate = nil ;
18081920 currentCallViewController = nil ;
18091921 }
1810- } else
1922+ }
1923+ else
18111924 {
18121925 // Here the presentation of the call view controller is in progress
18131926 // Postpone the dismiss
18141927 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t )(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue (), ^{
1815- [self dismissCallViewController: callViewController];
1928+ [self dismissCallViewController: callViewController completion: completion ];
18161929 });
18171930 }
18181931 }
@@ -1837,7 +1950,7 @@ - (void)addCallStatusBar
18371950 [callStatusBarButton setTitle: btnTitle forState: UIControlStateHighlighted];
18381951 callStatusBarButton.titleLabel .textColor = [UIColor whiteColor ];
18391952
1840- [callStatusBarButton setBackgroundColor: [UIColor blueColor ] ];
1953+ [callStatusBarButton setBackgroundColor: kVectorColorGreen ];
18411954 [callStatusBarButton addTarget: self action: @selector (returnToCallView ) forControlEvents: UIControlEventTouchUpInside];
18421955
18431956 // Place button into the new window
@@ -1875,7 +1988,6 @@ - (void)returnToCallView
18751988{
18761989 [self removeCallStatusBar ];
18771990
1878- // FIXME GFO check whether self.window.rootViewController may present the call
18791991 [self .window.rootViewController presentViewController: currentCallViewController animated: YES completion: ^{
18801992 currentCallViewController.isPresented = YES ;
18811993 }];
0 commit comments