JLChen
2021-08-02 38f4fb064df09f344fc3237409c76a9fba2a8a9e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-iphone
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
 
#import "StatusBarView.h"
#import "LinphoneManager.h"
#import "PhoneMainView.h"
#import <UserNotifications/UserNotifications.h>
 
@implementation StatusBarView {
 
    NSTimer *callQualityTimer;
    NSTimer *callSecurityTimer;
    int messagesUnreadCount;
}
 
#pragma mark - Lifecycle Functions
 
- (void)dealloc {
    [NSNotificationCenter.defaultCenter removeObserver:self];
    [callQualityTimer invalidate];
}
 
#pragma mark - ViewController Functions
 
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
 
    // Set observer
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(registrationUpdate:)
                                               name:kLinphoneRegistrationUpdate
                                             object:nil];
 
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(globalStateUpdate:)
                                               name:kLinphoneGlobalStateUpdate
                                             object:nil];
 
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(notifyReceived:)
                                               name:kLinphoneNotifyReceived
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(mainViewChanged:)
                                               name:kLinphoneMainViewChange
                                             object:nil];
 
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(callUpdate:)
                                               name:kLinphoneCallUpdate
                                             object:nil];
    [NSNotificationCenter.defaultCenter addObserver:self
                                           selector:@selector(onCallEncryptionChanged:)
                                               name:kLinphoneCallEncryptionChanged
                                             object:nil];
 
    // Update to default state
    LinphoneAccount *account = linphone_core_get_default_account(LC);
    messagesUnreadCount = linphone_config_get_int(linphone_core_get_config(LC), "app", "voice_mail_messages_count", 0);
 
    [self accountUpdate:account];
    [self updateUI:linphone_core_get_calls_nb(LC)];
    [self updateVoicemail];
}
 
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
 
    // Remove observer
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneRegistrationUpdate object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneGlobalStateUpdate object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneNotifyReceived object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneCallUpdate object:nil];
    [NSNotificationCenter.defaultCenter removeObserver:self name:kLinphoneMainViewChange object:nil];
 
    if (callQualityTimer != nil) {
        [callQualityTimer invalidate];
        callQualityTimer = nil;
    }
    if (callSecurityTimer != nil) {
        [callSecurityTimer invalidate];
        callSecurityTimer = nil;
    }
 
    if (securityDialog != nil) {
        [securityDialog dismiss];
        securityDialog = nil;
    }
}
 
#pragma mark - Event Functions
 
- (void)registrationUpdate:(NSNotification *)notif {
    LinphoneAccount *account = linphone_core_get_default_account(LC);
    [self accountUpdate:account];
}
 
- (void)globalStateUpdate:(NSNotification *)notif {
    [self registrationUpdate:nil];
}
 
- (void)mainViewChanged:(NSNotification *)notif {
    [self registrationUpdate:nil];
}
 
- (void)onCallEncryptionChanged:(NSNotification *)notif {
    LinphoneCall *call = linphone_core_get_current_call(LC);
 
    if (call && (linphone_call_params_get_media_encryption(linphone_call_get_current_params(call)) ==
                 LinphoneMediaEncryptionZRTP) &&
        (!linphone_call_get_authentication_token_verified(call))) {
        [self onSecurityClick:nil];
    }
}
 
- (void)notifyReceived:(NSNotification *)notif {
    const LinphoneContent *content = [[notif.userInfo objectForKey:@"content"] pointerValue];
 
    if ((content == NULL) || (strcmp("application", linphone_content_get_type(content)) != 0) ||
        (strcmp("simple-message-summary", linphone_content_get_subtype(content)) != 0) ||
        (linphone_content_get_buffer(content) == NULL)) {
        return;
    }
    const uint8_t *bodyTmp = linphone_content_get_buffer(content);
    const char *body = (const char *)bodyTmp;
    if ((body = strstr(body, "voice-message: ")) == NULL) {
        LOGW(@"Received new NOTIFY from voice mail but could not find 'voice-message' in BODY. Ignoring it.");
        return;
    }
 
    sscanf((const char *)body, "voice-message: %d", &messagesUnreadCount);
 
    LOGI(@"Received new NOTIFY from voice mail: there is/are now %d message(s) unread", messagesUnreadCount);
 
    // save in lpconfig for future
    linphone_config_set_int(linphone_core_get_config(LC), "app", "voice_mail_messages_count", messagesUnreadCount);
 
    [self updateVoicemail];
}
 
- (void)updateVoicemail {
    _voicemailButton.hidden = (messagesUnreadCount <= 0);
    _voicemailButton.titleLabel.text = @(messagesUnreadCount).stringValue;
}
 
- (void)callUpdate:(NSNotification *)notif {
    // show voice mail only when there is no call
    [self updateUI:linphone_core_get_calls(LC) != NULL];
    [self updateVoicemail];
}
 
#pragma mark -
 
+ (UIImage *)imageForState:(LinphoneRegistrationState)state {
    switch (state) {
        case LinphoneRegistrationFailed:
            return [UIImage imageNamed:@"led_error.png"];
        case LinphoneRegistrationCleared:
        case LinphoneRegistrationNone:
            return [UIImage imageNamed:@"led_disconnected.png"];
        case LinphoneRegistrationProgress:
            return [UIImage imageNamed:@"led_inprogress.png"];
        case LinphoneRegistrationOk:
            return [UIImage imageNamed:@"led_connected.png"];
    }
}
- (void)accountUpdate:(LinphoneAccount *)account {
    LinphoneRegistrationState state = LinphoneRegistrationNone;
    NSString *message = nil;
    LinphoneGlobalState gstate = linphone_core_get_global_state(LC);
    
    if ([PhoneMainView.instance.currentView equal:AssistantView.compositeViewDescription] || [PhoneMainView.instance.currentView equal:CountryListView.compositeViewDescription]) {
        message = NSLocalizedString(@"Configuring account", nil);
    } else if (gstate == LinphoneGlobalOn && !linphone_core_is_network_reachable(LC)) {
        message = NSLocalizedString(@"Network down", nil);
    } else if (gstate == LinphoneGlobalConfiguring) {
        message = NSLocalizedString(@"Fetching remote configuration", nil);
    } else if (account == NULL) {
        state = LinphoneRegistrationNone;
        if (linphone_core_get_account_list(LC) != NULL) {
            message = NSLocalizedString(@"No default account", nil);
        } else {
            message = NSLocalizedString(@"No account configured", nil);
        }
 
    } else {
        state = linphone_account_get_state(account);
 
        switch (state) {
            case LinphoneRegistrationOk:
                message = NSLocalizedString(@"Connected", nil);
                break;
            case LinphoneRegistrationNone:
            case LinphoneRegistrationCleared:
                message = NSLocalizedString(@"Not connected", nil);
                break;
            case LinphoneRegistrationFailed:
                message = NSLocalizedString(@"Connection failed", nil);
                break;
            case LinphoneRegistrationProgress:
                message = NSLocalizedString(@"Connection in progress", nil);
                break;
            default:
                break;
        }
    }
    [_registrationState setTitle:message forState:UIControlStateNormal];
    _registrationState.accessibilityValue = message;
    [_registrationState setImage:[self.class imageForState:state] forState:UIControlStateNormal];
}
 
#pragma mark -
 
- (void)updateUI:(BOOL)inCall {
    BOOL hasChanged = (_outcallView.hidden != inCall);
 
    _outcallView.hidden = inCall;
    _incallView.hidden = !inCall;
 
    if (!hasChanged)
        return;
 
    if (callQualityTimer) {
        [callQualityTimer invalidate];
        callQualityTimer = nil;
    }
    if (callSecurityTimer) {
        [callSecurityTimer invalidate];
        callSecurityTimer = nil;
    }
    if (securityDialog) {
        [securityDialog dismiss];
    }
 
    // if we are in call, we have to update quality and security icons every sec
    if (inCall) {
        callQualityTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                            target:self
                                                          selector:@selector(callQualityUpdate)
                                                          userInfo:nil
                                                           repeats:YES];
        callSecurityTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                             target:self
                                                           selector:@selector(callSecurityUpdate)
                                                           userInfo:nil
                                                            repeats:YES];
    }
}
 
- (void)callSecurityUpdate {
    BOOL pending = false;
    BOOL security = true;
 
    const MSList *list = linphone_core_get_calls(LC);
    if (list == NULL) {
        if (securityDialog) {
            [securityDialog dismiss];
        }
    } else {
        _callSecurityButton.hidden = NO;
        while (list != NULL) {
            LinphoneCall *call = (LinphoneCall *)list->data;
            LinphoneMediaEncryption enc =
                linphone_call_params_get_media_encryption(linphone_call_get_current_params(call));
            if (enc == LinphoneMediaEncryptionNone)
                security = false;
            else if (enc == LinphoneMediaEncryptionZRTP) {
                if (!linphone_call_get_authentication_token_verified(call)) {
                    pending = true;
                }
            }
            list = list->next;
        }
        NSString *imageName =
            (security ? (pending ? @"security_pending.png" : @"security_ok.png") : @"security_ko.png");
        [_callSecurityButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    }
}
 
- (void)callQualityUpdate {
    LinphoneCall *call = linphone_core_get_current_call(LC);
    if (call != NULL) {
        int quality = MIN(4, floor(linphone_call_get_current_quality(call)));
        NSString *accessibilityValue = [NSString stringWithFormat:NSLocalizedString(@"Call quality: %d", nil), quality];
        if (![accessibilityValue isEqualToString:_callQualityButton.accessibilityValue]) {
            _callQualityButton.accessibilityValue = accessibilityValue;
            _callQualityButton.hidden = NO; //(quality == -1.f);
            UIImage *image =
                (quality == -1.f)
                    ? [UIImage imageNamed:@"call_quality_indicator_0.png"] // nil
                    : [UIImage imageNamed:[NSString stringWithFormat:@"call_quality_indicator_%d.png", quality]];
            [_callQualityButton setImage:image forState:UIControlStateNormal];
        }
    }
}
 
#pragma mark - Action Functions
 
- (IBAction)onSecurityClick:(id)sender {
    if (linphone_core_get_calls_nb(LC)) {
        LinphoneCall *call = linphone_core_get_current_call(LC);
        if (call != NULL) {
            LinphoneMediaEncryption enc =
                linphone_call_params_get_media_encryption(linphone_call_get_current_params(call));
            if (enc == LinphoneMediaEncryptionZRTP) {
                NSString *code = [NSString stringWithUTF8String:linphone_call_get_authentication_token(call)];
                NSString *myCode;
                NSString *correspondantCode;
                if (linphone_call_get_dir(call) == LinphoneCallIncoming) {
                    myCode = [code substringToIndex:2];
                    correspondantCode = [code substringFromIndex:2];
                } else {
                    correspondantCode = [code substringToIndex:2];
                    myCode = [code substringFromIndex:2];
                }
                NSString *message =
                    [NSString stringWithFormat:NSLocalizedString(@"\nConfirmation security\n\n"
                                                                 @"Say: %@\n"
                                                                 @"Confirm that your interlocutor\n"
                                                                 @"says: %@",
                                                                 nil),
                                               myCode.uppercaseString, correspondantCode.uppercaseString];
                
                if ([UIApplication sharedApplication].applicationState != UIApplicationStateActive &&
                    floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_9_x_Max) {
                    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                    content.title = NSLocalizedString(@"ZRTP verification", nil);
                    content.body = message;
                    content.categoryIdentifier = @"zrtp_request";
                    content.userInfo = @{
                        @"CallId" : [NSString
                            stringWithUTF8String:linphone_call_log_get_call_id(linphone_call_get_call_log(call))]
                    };
 
                    UNNotificationRequest *req =
                        [UNNotificationRequest requestWithIdentifier:@"zrtp_request" content:content trigger:NULL];
                    [[UNUserNotificationCenter currentNotificationCenter]
                        addNotificationRequest:req
                         withCompletionHandler:^(NSError *_Nullable error) {
                           // Enable or disable features based on authorization.
                           if (error) {
                               LOGD(@"Error while adding notification request :");
                               LOGD(error.description);
                           }
                         }];
                } else {
                    if (securityDialog == nil) {
                        __block __strong StatusBarView *weakSelf = self;
                        // define font of message
                        NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:message];
                        NSUInteger length = [message length];
                        UIFont *baseFont = [UIFont systemFontOfSize:21.0];
                        [attrString addAttribute:NSFontAttributeName value:baseFont range:NSMakeRange(0, length)];
                        UIFont *boldFont = [UIFont boldSystemFontOfSize:23.0];
                        [attrString addAttribute:NSFontAttributeName value:boldFont range:[message rangeOfString:@"Confirmation security"]];
                        UIColor *color = [UIColor colorWithRed:(150 / 255.0) green:(193 / 255.0) blue:(31 / 255.0) alpha:1.0];
                        [attrString addAttribute:NSForegroundColorAttributeName value:color range:[message rangeOfString:myCode.uppercaseString]];
                        [attrString addAttribute:NSForegroundColorAttributeName value:color range:[message rangeOfString:correspondantCode.uppercaseString]];
                        
                        securityDialog = [UIConfirmationDialog ShowWithAttributedMessage:attrString
                            cancelMessage:NSLocalizedString(@"DENY", nil)
                            confirmMessage:NSLocalizedString(@"ACCEPT", nil)
                            onCancelClick:^() {
                              if (linphone_core_get_current_call(LC) == call) {
                                  linphone_call_set_authentication_token_verified(call, NO);
                              }
                              weakSelf->securityDialog = nil;
                              [LinphoneManager.instance lpConfigSetString:[NSString stringWithUTF8String:linphone_call_get_remote_address_as_string(call)] forKey:@"sas_dialog_denied"];
                            }
                            onConfirmationClick:^() {
                              if (linphone_core_get_current_call(LC) == call) {
                                  linphone_call_set_authentication_token_verified(call, YES);
                              }
                              weakSelf->securityDialog = nil;
                                [LinphoneManager.instance lpConfigSetString:nil forKey:@"sas_dialog_denied"];
                            } ];
                        
                        securityDialog.securityImage.hidden = FALSE;
                        [securityDialog setSpecialColor];
                    }
                }
            }
        }
    }
}
 
- (IBAction)onSideMenuClick:(id)sender {
    UICompositeView *cvc = PhoneMainView.instance.mainViewController;
    [cvc hideSideMenu:(cvc.sideMenuView.frame.origin.x == 0)];
}
 
- (IBAction)onRegistrationStateClick:(id)sender {
    if (linphone_core_get_default_account(LC)) {
        linphone_core_refresh_registers(LC);
    } else if (linphone_core_get_account_list(LC)) {
        [PhoneMainView.instance changeCurrentView:SettingsView.compositeViewDescription];
    } else {
        [PhoneMainView.instance changeCurrentView:AssistantView.compositeViewDescription];
    }
}
 
@end