VLCSettingsController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 "VLCLibraryViewController.h"
  16. #import "IASKSettingsReader.h"
  17. #import "PAPasscodeViewController.h"
  18. #import <LocalAuthentication/LocalAuthentication.h>
  19. #import "VLC_iOS-Swift.h"
  20. @interface VLCSettingsController ()<PAPasscodeViewControllerDelegate, IASKSettingsDelegate>
  21. @end
  22. @implementation VLCSettingsController
  23. - (instancetype)initWithStyle:(UITableViewStyle)style
  24. {
  25. self = [super initWithStyle:style];
  26. if (self) {
  27. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(settingDidChange:) name:kIASKAppSettingChanged object:nil];
  28. }
  29. return self;
  30. }
  31. - (void)viewDidLoad
  32. {
  33. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(dismiss:)];
  34. self.modalPresentationStyle = UIModalPresentationFormSheet;
  35. self.delegate = self;
  36. self.showDoneButton = NO;
  37. self.showCreditsFooter = NO;
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. [self filterCellsWithAnimation:NO];
  43. }
  44. - (NSSet *)hiddenBiometryKeys
  45. {
  46. if (@available(iOS 11.0, *)) {
  47. LAContext *laContext = [[LAContext alloc] init];
  48. if ([laContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics error:nil]) {
  49. switch (laContext.biometryType) {
  50. case LABiometryTypeFaceID:
  51. return [NSSet setWithObject:kVLCSettingPasscodeAllowTouchID];
  52. case LABiometryTypeTouchID:
  53. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  54. case LABiometryNone:
  55. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  56. }
  57. }
  58. return [NSSet setWithObjects:kVLCSettingPasscodeAllowFaceID, kVLCSettingPasscodeAllowTouchID, nil];
  59. }
  60. return [NSSet setWithObject:kVLCSettingPasscodeAllowFaceID];
  61. }
  62. - (void)filterCellsWithAnimation:(BOOL)shouldAnimate
  63. {
  64. NSMutableSet *hideKeys = [[NSMutableSet alloc] init];
  65. if (![VLCKeychainCoordinator passcodeLockEnabled]) {
  66. [hideKeys addObject:kVLCSettingPasscodeAllowTouchID];
  67. [hideKeys addObject:kVLCSettingPasscodeAllowFaceID];
  68. [self setHiddenKeys:hideKeys animated:shouldAnimate];
  69. return;
  70. }
  71. [self setHiddenKeys:[self hiddenBiometryKeys] animated:shouldAnimate];
  72. }
  73. - (void)settingDidChange:(NSNotification*)notification
  74. {
  75. if ([notification.object isEqual:kVLCSettingPasscodeOnKey]) {
  76. BOOL passcodeOn = [[notification.userInfo objectForKey:kVLCSettingPasscodeOnKey] boolValue];
  77. if (passcodeOn) {
  78. PAPasscodeViewController *passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionSet];
  79. passcodeLockController.delegate = self;
  80. [self presentViewController:passcodeLockController animated:YES completion:nil];
  81. } else {
  82. [self updateForPasscode:nil];
  83. }
  84. }
  85. }
  86. - (void)updateUIAndCoreSpotlightForPasscodeSetting:(BOOL)passcodeOn
  87. {
  88. [self filterCellsWithAnimation:YES];
  89. [[MLMediaLibrary sharedMediaLibrary] setSpotlightIndexingEnabled:!passcodeOn];
  90. if (passcodeOn) {
  91. // delete whole index for VLC
  92. [[CSSearchableIndex defaultSearchableIndex] deleteAllSearchableItemsWithCompletionHandler:nil];
  93. }
  94. }
  95. #pragma mark - IASKSettings delegate
  96. - (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController*)sender
  97. {
  98. [[VLCSidebarController sharedInstance] toggleSidebar];
  99. }
  100. #pragma mark - PAPasscode delegate
  101. - (void)PAPasscodeViewControllerDidCancel:(PAPasscodeViewController *)controller
  102. {
  103. [self updateForPasscode:nil];
  104. }
  105. - (void)PAPasscodeViewControllerDidSetPasscode:(PAPasscodeViewController *)controller
  106. {
  107. [self updateForPasscode:controller.passcode];
  108. }
  109. - (void)updateForPasscode:(NSString *)passcode
  110. {
  111. NSError *error = nil;
  112. [VLCKeychainCoordinator setPasscodeWithPasscode:passcode error:&error];
  113. if (error == nil) {
  114. if (passcode == nil) {
  115. //Set manually the value to NO to disable the UISwitch.
  116. [[NSUserDefaults standardUserDefaults] setBool:NO forKey:kVLCSettingPasscodeOnKey];
  117. }
  118. [self updateUIAndCoreSpotlightForPasscodeSetting:passcode != nil];
  119. }
  120. if ([self.navigationController.presentedViewController isKindOfClass:[PAPasscodeViewController class]]) {
  121. [self.navigationController.presentedViewController dismissViewControllerAnimated:YES completion:nil];
  122. }
  123. }
  124. @end