From 92cbeec76e3337c1c43b8814e1d3a3eb0d456193 Mon Sep 17 00:00:00 2001 From: giomfo Date: Tue, 22 Nov 2016 11:16:59 +0100 Subject: [PATCH 1/2] Settings: Add cryptography information --- Vector/Assets/en.lproj/Vector.strings | 7 + Vector/Base.lproj/Main.storyboard | 50 ++++--- .../ViewController/SettingsViewController.h | 3 +- .../ViewController/SettingsViewController.m | 122 +++++++++++++++++- 4 files changed, 160 insertions(+), 22 deletions(-) diff --git a/Vector/Assets/en.lproj/Vector.strings b/Vector/Assets/en.lproj/Vector.strings index b6f0bbf0d..5a5b3ff0b 100644 --- a/Vector/Assets/en.lproj/Vector.strings +++ b/Vector/Assets/en.lproj/Vector.strings @@ -235,6 +235,7 @@ "room_preview_try_join_an_unknown_room_default" = "a room"; // Settings +"settings_title" = "Settings"; "account_logout_all" = "Logout all accounts"; "settings_config_no_build_info" = "(no build info)"; @@ -251,6 +252,8 @@ "settings_advanced" = "ADVANCED"; "settings_other" = "OTHER"; "settings_labs" = "LABS"; +"settings_devices" = "DEVICES"; +"settings_cryptography" = "CRYPTOGRAPHY"; "settings_sign_out" = "Sign Out"; "settings_profile_picture" = "Profile Picture"; @@ -301,6 +304,10 @@ "settings_fail_to_update_password" = "Fail to update password"; "settings_password_updated" = "Your password has been updated"; +"settings_crypto_device_name" = "Device name: "; +"settings_crypto_device_id" = "\nDevice ID: "; +"settings_crypto_device_key" = "\nDevice key: "; + // Room Details "room_details_title" = "Room Details"; "room_details_people" = "Members"; diff --git a/Vector/Base.lproj/Main.storyboard b/Vector/Base.lproj/Main.storyboard index e33da565d..ef057b833 100644 --- a/Vector/Base.lproj/Main.storyboard +++ b/Vector/Base.lproj/Main.storyboard @@ -1,5 +1,5 @@ - - + + @@ -132,23 +132,41 @@ - + - - + + + + + + - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + - + @@ -199,7 +217,7 @@ - + diff --git a/Vector/ViewController/SettingsViewController.h b/Vector/ViewController/SettingsViewController.h index e6699ed97..9d4e9229b 100644 --- a/Vector/ViewController/SettingsViewController.h +++ b/Vector/ViewController/SettingsViewController.h @@ -18,8 +18,9 @@ #import "MediaPickerViewController.h" -@interface SettingsViewController : MXKTableViewController +@interface SettingsViewController : MXKViewController +@property (nonatomic) IBOutlet UITableView *tableView; @end diff --git a/Vector/ViewController/SettingsViewController.m b/Vector/ViewController/SettingsViewController.m index e319125a8..fbcd23029 100644 --- a/Vector/ViewController/SettingsViewController.m +++ b/Vector/ViewController/SettingsViewController.m @@ -39,6 +39,7 @@ enum { SETTINGS_SECTION_CONTACTS_INDEX, #endif SETTINGS_SECTION_LABS_INDEX, + SETTINGS_SECTION_CRYPTOGRAPHY_INDEX, SETTINGS_SECTION_COUNT }; @@ -153,12 +154,18 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); self.enableBarTintColorStatusChange = NO; self.rageShakeManager = [RageShakeManager sharedManager]; + self.navigationItem.title = NSLocalizedStringFromTable(@"settings_title", @"Vector", nil); + self.tableView.backgroundColor = kVectorColorLightGrey; [self.tableView registerClass:MXKTableViewCellWithLabelAndTextField.class forCellReuseIdentifier:[MXKTableViewCellWithLabelAndTextField defaultReuseIdentifier]]; [self.tableView registerClass:MXKTableViewCellWithLabelAndSwitch.class forCellReuseIdentifier:[MXKTableViewCellWithLabelAndSwitch defaultReuseIdentifier]]; [self.tableView registerClass:MXKTableViewCellWithLabelAndMXKImageView.class forCellReuseIdentifier:[MXKTableViewCellWithLabelAndMXKImageView defaultReuseIdentifier]]; + // Enable self sizing cells + self.tableView.rowHeight = UITableViewAutomaticDimension; + self.tableView.estimatedRowHeight = 50; + // Add observer to handle removed accounts removedAccountObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kMXKAccountManagerDidRemoveAccountNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { @@ -294,6 +301,9 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); // Refresh linked emails in parallel [self loadLinkedEmails]; + // Refresh the current device information in parallel + [self loadCurrentDeviceInformation]; + // Observe kAppDelegateDidTapStatusBarNotificationObserver. kAppDelegateDidTapStatusBarNotificationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:kAppDelegateDidTapStatusBarNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *notif) { @@ -345,6 +355,14 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); #pragma mark - Internal methods +- (void)dismissKeyboard +{ + [currentPasswordTextField resignFirstResponder]; + [newPasswordTextField1 resignFirstResponder]; + [newPasswordTextField2 resignFirstResponder]; + [newEmailTextField resignFirstResponder]; +} + - (void)reset { // Remove observers @@ -522,6 +540,61 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); }]; } +- (void)loadCurrentDeviceInformation +{ + // Refresh the current device information + MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject; + [account loadDeviceInformation:^{ + + // Refresh all the table (A slide down animation is observed when we limit the refresh to the concerned section). + // Note: The use of 'reloadData' handles the case where the account has been logged out. + [self.tableView reloadData]; + + } failure:^(NSError *error) { + + // Display the data that has been loaded last time + // Note: The use of 'reloadData' handles the case where the account has been logged out. + [self.tableView reloadData]; + + }]; +} + +- (NSAttributedString*)cryptographyInformation +{ + // TODO Handle multi accounts + MXKAccount* account = [MXKAccountManager sharedManager].activeAccounts.firstObject; + + // Crypto information + NSMutableAttributedString *cryptoInformationString = [[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"settings_crypto_device_name", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:17]}]; + [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:account.device.displayName.length ? account.device.displayName : @"" + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; + + [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"settings_crypto_device_id", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; + [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:account.device.deviceId + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; + + [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:NSLocalizedStringFromTable(@"settings_crypto_device_key", @"Vector", nil) + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; + [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] + initWithString:account.mxSession.crypto.olmDevice.deviceEd25519Key + attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, + NSFontAttributeName: [UIFont boldSystemFontOfSize:17]}]]; + + return cryptoInformationString; +} + #pragma mark - Segues - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender @@ -601,6 +674,14 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); { count = LABS_COUNT; } + else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) + { + // Check whether this section is visible. + if (self.mainSession.crypto) + { + count = 1; + } + } return count; } @@ -1087,6 +1168,19 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); cell = labelAndSwitchCell; } } + else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) + { + MXKTableViewCell *cryptoCell = [tableView dequeueReusableCellWithIdentifier:[MXKTableViewCell defaultReuseIdentifier]]; + if (!cryptoCell) + { + cryptoCell = [[MXKTableViewCell alloc] init]; + } + + cryptoCell.textLabel.attributedText = [self cryptographyInformation]; + cryptoCell.textLabel.numberOfLines = 0; + + cell = cryptoCell; + } return cell; } @@ -1103,6 +1197,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); } else if (section == SETTINGS_SECTION_IGNORED_USERS_INDEX) { + // Check whether this section is visible if ([AppDelegate theDelegate].mxSessions.count > 0) { MXSession* session = [[AppDelegate theDelegate].mxSessions objectAtIndex:0]; @@ -1111,9 +1206,6 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); return NSLocalizedStringFromTable(@"settings_ignored_users", @"Vector", nil); } } - - // Hide this section - return nil; } else if (section == SETTINGS_SECTION_CONTACTS_INDEX) { @@ -1131,6 +1223,14 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); { return NSLocalizedStringFromTable(@"settings_labs", @"Vector", nil); } + else if (section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) + { + // Check whether this section is visible + if (self.mainSession.crypto) + { + return NSLocalizedStringFromTable(@"settings_cryptography", @"Vector", nil); + } + } return nil; } @@ -1166,6 +1266,15 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); [label sizeToFit]; return label.frame.size.height + 16; } + else if (indexPath.section == SETTINGS_SECTION_CRYPTOGRAPHY_INDEX) + { + UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 50)]; + label.numberOfLines = 0; + label.attributedText = [self cryptographyInformation]; + [label sizeToFit]; + + return label.frame.size.height + 16; + } return 50; } @@ -1185,7 +1294,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); } } - return [super tableView:tableView heightForHeaderInSection:section]; + return 24; } - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section @@ -1203,7 +1312,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); } } - return [super tableView:tableView heightForFooterInSection:section]; + return 24; } - (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath @@ -1496,6 +1605,9 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); [self stopActivityIndicator]; + // Refresh table view to add cryptography information. + [self.tableView reloadData]; + } failure:^(NSError *error) { [self stopActivityIndicator]; From 2e31836f55741fb250447fc3683fd66e5f63159f Mon Sep 17 00:00:00 2001 From: giomfo Date: Tue, 22 Nov 2016 14:19:41 +0100 Subject: [PATCH 2/2] Settings: Add cryptography information - take into account Manu's comment --- Vector/ViewController/SettingsViewController.m | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/Vector/ViewController/SettingsViewController.m b/Vector/ViewController/SettingsViewController.m index fbcd23029..a8e91ebce 100644 --- a/Vector/ViewController/SettingsViewController.m +++ b/Vector/ViewController/SettingsViewController.m @@ -550,13 +550,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); // Note: The use of 'reloadData' handles the case where the account has been logged out. [self.tableView reloadData]; - } failure:^(NSError *error) { - - // Display the data that has been loaded last time - // Note: The use of 'reloadData' handles the case where the account has been logged out. - [self.tableView reloadData]; - - }]; + } failure:nil]; } - (NSAttributedString*)cryptographyInformation @@ -570,7 +564,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont systemFontOfSize:17]}]; [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] - initWithString:account.device.displayName.length ? account.device.displayName : @"" + initWithString:account.device.displayName ? account.device.displayName : @"" attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; @@ -579,7 +573,7 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] - initWithString:account.device.deviceId + initWithString:account.device.deviceId ? account.device.deviceId : @"" attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; @@ -587,8 +581,9 @@ typedef void (^blockSettingsViewController_onReadyToDestroy)(); initWithString:NSLocalizedStringFromTable(@"settings_crypto_device_key", @"Vector", nil) attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont systemFontOfSize:17]}]]; + NSString *fingerprint = account.mxSession.crypto.olmDevice.deviceEd25519Key; [cryptoInformationString appendAttributedString:[[NSMutableAttributedString alloc] - initWithString:account.mxSession.crypto.olmDevice.deviceEd25519Key + initWithString:fingerprint ? fingerprint : @"" attributes:@{NSForegroundColorAttributeName : kVectorTextColorBlack, NSFontAttributeName: [UIFont boldSystemFontOfSize:17]}]];