KKPasscodeLock.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // Copyright 2011-2012 Kosher Penguin LLC
  3. // Created by Adar Porat (https://github.com/aporat) on 1/16/2012.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. #import "KKPasscodeLock.h"
  18. #import "KKKeychain.h"
  19. #import "KKPasscodeViewController.h"
  20. #import "Utils.h"
  21. #import "BundleUtil.h"
  22. #import "AppGroup.h"
  23. static KKPasscodeLock *sharedLock = nil;
  24. @implementation KKPasscodeLock
  25. @synthesize eraseOption = _eraseOption;
  26. @synthesize attemptsAllowed = _attemptsAllowed;
  27. @synthesize lastUnlockTime = _lastUnlockTime;
  28. + (KKPasscodeLock*)sharedLock
  29. {
  30. @synchronized(self) {
  31. if (sharedLock == nil) {
  32. sharedLock = [[self alloc] init];
  33. sharedLock.eraseOption = YES;
  34. sharedLock.attemptsAllowed = 5;
  35. sharedLock.lastUnlockTime = 0;
  36. }
  37. }
  38. return sharedLock;
  39. }
  40. - (BOOL)isPasscodeRequired
  41. {
  42. return [[KKKeychain getStringForKey:@"passcode_on"] isEqualToString:@"YES"];
  43. }
  44. - (BOOL)isTouchIdOn {
  45. return [[KKKeychain getStringForKey:@"touch_id_on"] isEqualToString:@"YES"];
  46. }
  47. - (BOOL)isWithinGracePeriod {
  48. int gracePeriod = [[KKKeychain getStringForKey:@"grace_period"] intValue];
  49. if (gracePeriod > 0) {
  50. time_t uptime = [Utils systemUptime];
  51. if (uptime > 0 && self.lastUnlockTime > 0 && (uptime - self.lastUnlockTime) < gracePeriod)
  52. return YES;
  53. }
  54. return NO;
  55. }
  56. - (void)setDefaultSettings
  57. {
  58. if (![KKKeychain getStringForKey:@"passcode_on"]) {
  59. [KKKeychain setString:@"NO" forKey:@"passcode_on"];
  60. }
  61. if (![KKKeychain getStringForKey:@"erase_data_on"]) {
  62. [KKKeychain setString:@"NO" forKey:@"erase_data_on"];
  63. }
  64. if (![KKKeychain getStringForKey:@"grace_period"]) {
  65. [KKKeychain setString:@"0" forKey:@"grace_period"];
  66. }
  67. if (![KKKeychain getStringForKey:@"touch_id_on"]) {
  68. [KKKeychain setString:@"NO" forKey:@"touch_id_on"];
  69. }
  70. }
  71. - (void)upgradeAccessibility {
  72. [KKKeychain upgradeAccessibilityForKey:@"passcode"];
  73. [KKKeychain upgradeAccessibilityForKey:@"passcode_on"];
  74. [KKKeychain upgradeAccessibilityForKey:@"erase_data_on"];
  75. [KKKeychain upgradeAccessibilityForKey:@"grace_period"];
  76. [KKKeychain upgradeAccessibilityForKey:@"touch_id_on"];
  77. }
  78. - (void)disablePasscode {
  79. [KKKeychain setString:@"NO" forKey:@"passcode_on"];
  80. [KKKeychain setString:@"NO" forKey:@"erase_data_on"];
  81. [KKKeychain setString:@"0" forKey:@"grace_period"];
  82. [KKKeychain setString:@"NO" forKey:@"touch_id_on"];
  83. [[AppGroup userDefaults] setInteger:0 forKey:@"FailedCodeAttempts"];
  84. [[AppGroup userDefaults] synchronize];
  85. }
  86. - (NSString *)localizedStringForKey:(NSString *)key value:(NSString *)value
  87. {
  88. static NSBundle *bundle = nil;
  89. if (bundle == nil)
  90. {
  91. NSString *bundlePath = [[BundleUtil frameworkBundle] pathForResource:@"KKPasscodeLock" ofType:@"bundle"];
  92. bundle = [NSBundle bundleWithPath:bundlePath] ?: [NSBundle mainBundle];
  93. }
  94. value = [bundle localizedStringForKey:key value:value table:nil];
  95. return [[BundleUtil mainBundle] localizedStringForKey:key value:value table:nil];
  96. }
  97. - (void)updateLastUnlockTime {
  98. self.lastUnlockTime = [Utils systemUptime];
  99. }
  100. @end