VLCSettingsController.m 7.7 KB

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