VLCSettingsController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. @interface VLCSettingsController ()<PAPasscodeViewControllerDelegate>
  20. @end
  21. @implementation VLCSettingsController
  22. - (instancetype)initWithStyle:(UITableViewStyle)style
  23. {
  24. self = [super initWithStyle:style];
  25. if (self) {
  26. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  27. }
  28. return self;
  29. }
  30. - (void)viewDidLoad
  31. {
  32. self.modalPresentationStyle = UIModalPresentationFormSheet;
  33. self.delegate = self;
  34. self.showDoneButton = NO;
  35. self.showCreditsFooter = NO;
  36. [self themeDidChange];
  37. }
  38. - (void)themeDidChange
  39. {
  40. self.view.backgroundColor = PresentationTheme.current.colors.settingsBackground;
  41. self.tableView.separatorColor = PresentationTheme.current.colors.settingsSeparatorColor;
  42. [self.tableView reloadData];
  43. }
  44. - (void)viewWillAppear:(BOOL)animated
  45. {
  46. [super viewWillAppear:animated];
  47. [self filterCellsWithAnimation:NO];
  48. }
  49. - (NSSet *)hiddenBiometryKeys
  50. {
  51. if (@available(iOS 11.0, *)) {
  52. LAContext *laContext = [[LAContext alloc] init];
  53. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  54. switch (laContext.biometryType) {
  55. case LABiometryTypeFaceID:
  56. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  57. case LABiometryTypeTouchID:
  58. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  59. case LABiometryNone:
  60. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  61. }
  62. }
  63. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  64. }
  65. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  66. }
  67. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  68. {
  69. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  70. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  71. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  72. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  73. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  74. return;
  75. }
  76. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  77. }
  78. - (void)settingDidChange:(NSNotification*)notification
  79. {
  80. if ([notification.object isEqual:kVLCSettingPasscodeOnKey]) {
  81. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  82. if (passcodeOn) {
  83. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  84. passcodeLockController.delegate = self;
  85. [self presentViewController:passcodeLockController animated:YES completion:nil];
  86. } else {
  87. [self updateForPasscode:nil];
  88. }
  89. }
  90. if ([notification.object isEqual:kVLCSettingAppTheme]) {
  91. BOOL darkTheme = [[notification.userInfo objectForKey:kVLCSettingAppTheme] boolValue];
  92. PresentationTheme.current = darkTheme ? PresentationTheme.darkTheme : PresentationTheme.brightTheme;
  93. [self themeDidChange];
  94. }
  95. }
  96. - (void)updateUIAndCoreSpotlightForPasscodeSetting:(BOOL)passcodeOn
  97. {
  98. [self filterCellsWithAnimation:YES];
  99. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:!passcodeOn];
  100. if (passcodeOn) {
  101. // delete whole index for VLC
  102. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  103. }
  104. }
  105. #pragma mark - IASKSettings delegate
  106. #pragma mark - PAPasscode delegate
  107. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  108. {
  109. [self updateForPasscode:nil];
  110. }
  111. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  112. {
  113. [self updateForPasscode:controller.passcode];
  114. }
  115. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. UITableViewCell *cell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
  118. cell.backgroundColor = PresentationTheme.current.colors.settingsCellBackground;
  119. cell.textLabel.textColor = PresentationTheme.current.colors.cellTextColor;
  120. cell.detailTextLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  121. return cell;
  122. }
  123. - (void)updateForPasscode:(NSString *)passcode
  124. {
  125. NSError *error = nil;
  126. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  127. if (error == nil) {
  128. if (passcode == nil) {
  129. //Set manually the value to NO to disable the UISwitch.
  130. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kVLCSettingPasscodeOnKey];
  131. }
  132. [self updateUIAndCoreSpotlightForPasscodeSetting:passcode != nil];
  133. }
  134. if ([self.navigationController.presentedViewController isKindOfClass:[PAPasscodeViewController class]]) {
  135. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  136. }
  137. }
  138. @end