PageRenderTime 31ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/iMoney/Classes/LockScreenView.m

http://imoney-iphone.googlecode.com/
Objective C | 375 lines | 269 code | 87 blank | 19 comment | 61 complexity | c164020f8a2db060d1741da32ace2525 MD5 | raw file
  1. //
  2. // LockScreenView.m
  3. // iMoney
  4. //
  5. // Created by Sean McGrail on 3/4/10.
  6. // Copyright 2010 __MyCompanyName__. All rights reserved.
  7. //
  8. #import "LockScreenView.h"
  9. #import "SFHFKeychainUtils.h"
  10. @implementation LockScreenView
  11. @synthesize delegate;
  12. - (id)initWithFrame:(CGRect)frame {
  13. if (self = [super initWithFrame:frame]) {
  14. [[NSNotificationCenter defaultCenter] addObserver:self
  15. selector:@selector(keyboardWillShow:)
  16. name:UIKeyboardWillShowNotification
  17. object:nil];
  18. }
  19. return self;
  20. }
  21. -(void) setUpPassword
  22. {
  23. NSError* error;
  24. #if TARGET_IPHONE_SIMULATOR
  25. //mainPassword = [[SFHFKeychainUtils getPasswordForUsername:[[UIDevice currentDevice] name] andServiceName:@"lockScreen" error:&error] retain];
  26. mainPassword= @"1234";
  27. #else
  28. mainPassword = [[SFHFKeychainUtils getPassword:[[UIDevice currentDevice] name] error:&error] retain];
  29. #endif
  30. if(mainPassword == nil)
  31. {
  32. UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"No Password Set" message:@"Please input new password" delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
  33. // optional - add more buttons:
  34. [alert addButtonWithTitle:@"OK"];
  35. [alert show];
  36. passwordSet= NO;
  37. }
  38. else
  39. {
  40. passwordSet=YES;
  41. }
  42. }
  43. -(void) createMainLockView:(UIImageView*)backgroundImage
  44. {
  45. //[self setBackgroundColor:[UIColor whiteColor]];
  46. UIView* bImage=nil;
  47. if(backgroundImage == nil){
  48. bImage= [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  49. bImage.backgroundColor = [UIColor groupTableViewBackgroundColor];
  50. }
  51. else
  52. {
  53. bImage = [backgroundImage autorelease];
  54. }
  55. const NSInteger XLOC = 20;
  56. const NSInteger YLOC = 100;
  57. const NSInteger WIDTH = 60;
  58. const NSInteger HEIGHT = 60;
  59. const NSInteger TEXT_WIDTH = 300;
  60. UILabel *name = [[UILabel alloc ] initWithFrame:CGRectMake(70 , 20, TEXT_WIDTH, 40)];
  61. name.textAlignment = UITextAlignmentLeft;
  62. name.textColor = [UIColor whiteColor];
  63. name.backgroundColor = [UIColor clearColor];
  64. name.font = [UIFont fontWithName:@"Arial Rounded MT Bold" size:(20.0)];
  65. name.text = @"Input Password:";
  66. first = [[UITextField alloc] initWithFrame:CGRectMake(XLOC, YLOC, WIDTH, HEIGHT)];
  67. first.delegate = self;
  68. first.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:(28.0)];
  69. first.borderStyle = UITextBorderStyleLine;
  70. [first setBackgroundColor:[UIColor whiteColor]];
  71. first.keyboardType = UIKeyboardTypeNumberPad;
  72. first.enablesReturnKeyAutomatically = YES;
  73. first.secureTextEntry= YES;
  74. first.textAlignment= UITextAlignmentCenter;
  75. first.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
  76. [first addTarget:self action:@selector(characterEntered:) forControlEvents:UIControlEventEditingChanged];
  77. second = [[UITextField alloc] initWithFrame:CGRectMake(XLOC+70, YLOC, WIDTH, HEIGHT)];
  78. second.delegate = self;
  79. second.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:(28.0)];
  80. second.adjustsFontSizeToFitWidth= YES;
  81. second.borderStyle = UITextBorderStyleLine;
  82. [second setBackgroundColor:[UIColor whiteColor]];
  83. second.keyboardType = UIKeyboardTypeNumberPad;
  84. second.enablesReturnKeyAutomatically = YES;
  85. second.secureTextEntry= YES;
  86. second.textAlignment= UITextAlignmentCenter;
  87. second.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
  88. [second addTarget:self action:@selector(characterEntered:) forControlEvents:UIControlEventEditingChanged];
  89. third = [[UITextField alloc] initWithFrame:CGRectMake(XLOC+140, YLOC, WIDTH, HEIGHT)];
  90. third.delegate = self;
  91. third.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:(28.0)];
  92. third.adjustsFontSizeToFitWidth= YES;
  93. third.borderStyle = UITextBorderStyleLine;
  94. [third setBackgroundColor:[UIColor whiteColor]];
  95. third.keyboardType = UIKeyboardTypeNumberPad;
  96. third.enablesReturnKeyAutomatically = YES;
  97. third.secureTextEntry= YES;
  98. third.textAlignment= UITextAlignmentCenter;
  99. third.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
  100. [third addTarget:self action:@selector(characterEntered:) forControlEvents:UIControlEventEditingChanged];
  101. fourth = [[UITextField alloc] initWithFrame:CGRectMake(XLOC+210, YLOC, WIDTH, HEIGHT)];
  102. fourth.delegate = self;
  103. fourth.font= [UIFont fontWithName:@"Arial Rounded MT Bold" size:(28.0)];
  104. fourth.adjustsFontSizeToFitWidth= YES;
  105. fourth.borderStyle = UITextBorderStyleLine;
  106. [fourth setBackgroundColor:[UIColor whiteColor]];
  107. fourth.keyboardType = UIKeyboardTypeNumberPad;
  108. fourth.enablesReturnKeyAutomatically = YES;
  109. fourth.secureTextEntry= YES;
  110. fourth.textAlignment= UITextAlignmentCenter;
  111. fourth.contentVerticalAlignment= UIControlContentVerticalAlignmentCenter;
  112. [fourth addTarget:self action:@selector(characterEntered:) forControlEvents:UIControlEventEditingChanged];
  113. [self addSubview:bImage];
  114. [self addSubview:name];
  115. [self addSubview:first];
  116. [self addSubview:second];
  117. [self addSubview:third];
  118. [self addSubview:fourth];
  119. [first becomeFirstResponder];
  120. }
  121. -(id)initLockScreen:(id<LockScreenInputDoneDelegate>) adelegate:(UIImageView*) backgroundImage
  122. {
  123. [self initWithFrame:[[UIScreen mainScreen] applicationFrame]];
  124. self.delegate = adelegate;
  125. [self setUpPassword];
  126. [self createMainLockView:backgroundImage];
  127. return self;
  128. }
  129. -(id)initLockScreen:(id<LockScreenInputDoneDelegate>) adelegate
  130. {
  131. return [self initLockScreen:nil];
  132. }
  133. -(NSString*) getPasswordFromInputs
  134. {
  135. NSString* pass = first.text;
  136. return [[[pass stringByAppendingString:second.text] stringByAppendingString:third.text] stringByAppendingString:fourth.text];
  137. }
  138. -(void) resetToFirstInput
  139. {
  140. first.text = @"";
  141. second.text = @"";
  142. third.text = @"";
  143. fourth.text = @"";
  144. [first becomeFirstResponder];
  145. }
  146. -(void) validatedCorrectly
  147. {
  148. [fourth resignFirstResponder];
  149. [[NSNotificationCenter defaultCenter] removeObserver:self];
  150. [self.delegate inputSucceded];
  151. }
  152. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
  153. if(alertView == checkFirstTime)
  154. {
  155. if(buttonIndex == 0)
  156. {
  157. NSError* error=nil;
  158. #if TARGET_IPHONE_SIMULATOR
  159. //[SFHFKeychainUtils storeUsername:[[UIDevice currentDevice] name] andPassword:[self getPasswordFromInputs] forServiceName:@"lockScreen" updateExisting:YES error:&error];
  160. #else
  161. [SFHFKeychainUtils createPassword:[self getPasswordFromInputs] forIdentifier:[[UIDevice currentDevice] name] error:&error];
  162. #endif
  163. [self validatedCorrectly];
  164. }
  165. else if(buttonIndex == 1)
  166. [self resetToFirstInput];
  167. }
  168. else if(alertView == checkInput)
  169. {
  170. [self resetToFirstInput];
  171. }
  172. }
  173. //- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  174. //{
  175. //
  176. //
  177. //}
  178. -(void) checkAndValidatePassword
  179. {
  180. if(!passwordSet)
  181. {
  182. checkFirstTime = [[[UIAlertView alloc] initWithTitle:[[self getPasswordFromInputs] stringByAppendingString:@" Will be the set password"] message:@"Do You want to keep this?"
  183. delegate:self cancelButtonTitle:nil otherButtonTitles:@"Yes", @"No", nil] autorelease];
  184. [checkFirstTime show];
  185. }
  186. else {
  187. if(![[self getPasswordFromInputs] compare:mainPassword] == NSOrderedSame)
  188. {
  189. checkInput = [[[UIAlertView alloc] initWithTitle:@"Password not correct" message:@""
  190. delegate:self cancelButtonTitle:nil otherButtonTitles:nil] autorelease];
  191. [checkInput addButtonWithTitle:@"Ok"];
  192. [checkInput show];
  193. }
  194. else {
  195. [self validatedCorrectly];
  196. }
  197. }
  198. }
  199. - (void)characterEntered:(id)sender {
  200. if (sender == first)
  201. {
  202. if (first.text && [first.text length] == 1)
  203. {
  204. [second becomeFirstResponder];
  205. }
  206. }
  207. else if (sender == second)
  208. {
  209. if (second.text && [second.text length] == 1)
  210. {
  211. [third becomeFirstResponder];
  212. }
  213. }
  214. else if (sender == third)
  215. {
  216. if (third.text && [third.text length] == 1)
  217. {
  218. [fourth becomeFirstResponder];
  219. }
  220. }
  221. else if (sender == fourth)
  222. {
  223. if (fourth.text && [fourth.text length] == 1)
  224. {
  225. [self checkAndValidatePassword];
  226. }
  227. }
  228. }
  229. -(void) backButton:(id)sender
  230. {
  231. UITextField* currentText;
  232. for (UIView *subView in self.subviews) {
  233. if([subView isKindOfClass:[UITextField class]] )
  234. {
  235. if([((UITextField*)subView) isFirstResponder])
  236. {
  237. currentText = ((UITextField*)subView);
  238. }
  239. }
  240. }
  241. if(currentText == second)
  242. {
  243. first.text = @"";
  244. second.text = @"";
  245. [first becomeFirstResponder];
  246. }
  247. else if(currentText == third)
  248. {
  249. second.text = @"";
  250. third.text = @"";
  251. [second becomeFirstResponder];
  252. }
  253. else if(currentText == fourth)
  254. {
  255. third.text = @"";
  256. fourth.text = @"";
  257. [third becomeFirstResponder];
  258. }
  259. }
  260. - (void)keyboardWillShow:(NSNotification *)note {
  261. // create custom butto
  262. UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
  263. backButton.frame = CGRectMake(215, 165, 106, 53);
  264. backButton.adjustsImageWhenHighlighted = NO;
  265. [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
  266. [backButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateHighlighted];
  267. [backButton addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
  268. // locate keyboard view
  269. UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
  270. UIView* keyboard;
  271. for(int i=0; i<[tempWindow.subviews count]; i++) {
  272. keyboard = [tempWindow.subviews objectAtIndex:i];
  273. // keyboard view found; add the custom button to it
  274. if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
  275. [keyboard addSubview:backButton];
  276. }
  277. }
  278. - (void)drawRect:(CGRect)rect {
  279. }
  280. - (void)dealloc {
  281. if(checkFirstTime != nil)
  282. [checkFirstTime release];
  283. if(checkInput != nil)
  284. [checkInput release];
  285. [mainPassword release];
  286. [super dealloc];
  287. }
  288. @end