VLCSettingsController.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*****************************************************************************
  2. * VLCSettingsController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCSettingsController.h"
  15. #import "IASKSettingsReader.h"
  16. #import "PAPasscodeViewController.h"
  17. #import <LocalAuthentication/LocalAuthentication.h>
  18. #import "VLC_iOS-Swift.h"
  19. NSString * const kVLCSectionTableHeaderViewIdentifier = @"VLCSectionTableHeaderViewIdentifier";
  20. @interface VLCSettingsController ()<PAPasscodeViewControllerDelegate>
  21. @end
  22. @implementation VLCSettingsController
  23. - (instancetype)initWithStyle:(UITableViewStyle)style
  24. {
  25. self = [super initWithStyle:style];
  26. if (self) {
  27. [self setupUI];
  28. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  29. }
  30. return self;
  31. }
  32. - (void)setupUI
  33. {
  34. self.title = NSLocalizedString(@"Settings", nil);
  35. self.tabBarItem = [[UITabBarItem alloc] initWithTitle: NSLocalizedString(@"Settings", nil)
  36. image: [UIImage imageNamed:@"Settings"]
  37. selectedImage: [UIImage imageNamed:@"Settings"]];
  38. self.tabBarItem.accessibilityIdentifier = VLCAccessibilityIdentifier.settings;
  39. }
  40. - (void)viewDidLoad
  41. {
  42. [super viewDidLoad];
  43. self.modalPresentationStyle = UIModalPresentationFormSheet;
  44. self.delegate = self;
  45. self.showDoneButton = NO;
  46. self.showCreditsFooter = NO;
  47. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_ABOUT", nil) style:UIBarButtonItemStylePlain target:self action:@selector(showAbout)];
  48. self.navigationItem.leftBarButtonItem.accessibilityIdentifier = VLCAccessibilityIdentifier.about;
  49. self.neverShowPrivacySettings = YES;
  50. self.tableView.rowHeight = UITableViewAutomaticDimension;
  51. self.tableView.estimatedRowHeight = 100;
  52. self.tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
  53. self.tableView.estimatedSectionHeaderHeight = 64;
  54. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  55. [self.tableView registerClass:[VLCSectionTableHeaderView class] forHeaderFooterViewReuseIdentifier:kVLCSectionTableHeaderViewIdentifier];
  56. [self themeDidChange];
  57. }
  58. - (void)themeDidChange
  59. {
  60. self.view.backgroundColor = PresentationTheme.current.colors.background;
  61. [self setNeedsStatusBarAppearanceUpdate];
  62. }
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [super viewWillAppear:animated];
  66. [self filterCellsWithAnimation:NO];
  67. }
  68. - (UIStatusBarStyle)preferredStatusBarStyle
  69. {
  70. return PresentationTheme.current.colors.statusBarStyle;
  71. }
  72. - (NSSet *)hiddenBiometryKeys
  73. {
  74. if (@available(iOS 11.0.1, *)) {
  75. LAContext *laContext = [[LAContext alloc] init];
  76. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  77. switch (laContext.biometryType) {
  78. case LABiometryTypeFaceID:
  79. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  80. case LABiometryTypeTouchID:
  81. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  82. case LABiometryNone:
  83. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  84. }
  85. }
  86. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  87. }
  88. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  89. }
  90. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  91. {
  92. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  93. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  94. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  95. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  96. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  97. return;
  98. }
  99. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  100. }
  101. - (void)settingDidChange:(NSNotification*)notification
  102. {
  103. if ([notification.object isEqual:kVLCSettingPasscodeOnKey]) {
  104. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  105. if (passcodeOn) {
  106. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  107. passcodeLockController.delegate = self;
  108. [self presentViewController:passcodeLockController animated:YES completion:nil];
  109. } else {
  110. [self updateForPasscode:nil];
  111. }
  112. }
  113. if ([notification.object isEqual:kVLCSettingAppTheme]) {
  114. BOOL darkTheme = [[notification.userInfo objectForKey:kVLCSettingAppTheme] boolValue];
  115. PresentationTheme.current = darkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme;
  116. [self themeDidChange];
  117. }
  118. }
  119. - (void)updateUIAndCoreSpotlightForPasscodeSetting:(BOOL)passcodeOn
  120. {
  121. [self filterCellsWithAnimation:YES];
  122. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:!passcodeOn];
  123. if (passcodeOn) {
  124. // delete whole index for VLC
  125. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  126. }
  127. }
  128. - (void)showAbout
  129. {
  130. VLCAboutViewController *aboutVC = [[VLCAboutViewController alloc] init];
  131. UINavigationController *modalNavigationController = [[UINavigationController alloc] initWithRootViewController:aboutVC];
  132. [self presentViewController:modalNavigationController animated:YES completion:nil];
  133. }
  134. #pragma mark - PAPasscode delegate
  135. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  136. {
  137. [self updateForPasscode:nil];
  138. }
  139. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  140. {
  141. [self updateForPasscode:controller.passcode];
  142. }
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. IASKSpecifier *specifier = [self.settingsReader specifierForIndexPath:indexPath];
  146. VLCSettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:specifier.type];
  147. if (!cell) {
  148. cell = [[VLCSettingsTableViewCell alloc] initWithReuseIdentifier:specifier.type target:self];
  149. }
  150. [cell configureWithSpecifier:specifier settingsValue:[self.settingsStore objectForKey:specifier.key]];
  151. return cell;
  152. }
  153. - (void)updateForPasscode:(NSString *)passcode
  154. {
  155. NSError *error = nil;
  156. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  157. if (error == nil) {
  158. if (passcode == nil) {
  159. //Set manually the value to NO to disable the UISwitch.
  160. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kVLCSettingPasscodeOnKey];
  161. }
  162. [self updateUIAndCoreSpotlightForPasscodeSetting:passcode != nil];
  163. }
  164. if ([self.navigationController.presentedViewController isKindOfClass:[PAPasscodeViewController class]]) {
  165. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  166. }
  167. }
  168. #pragma mark - InAppSettings customization
  169. - (UIView *)settingsViewController:(id<IASKViewController>)settingsViewController tableView:(UITableView *)tableView viewForHeaderForSection:(NSInteger)section
  170. {
  171. if (section == 0) {
  172. return nil;
  173. }
  174. VLCSectionTableHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kVLCSectionTableHeaderViewIdentifier];
  175. header.label.text = [self.settingsReader titleForSection:section];
  176. return header;
  177. }
  178. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  179. {
  180. return nil;
  181. }
  182. @end