File: OTPTableViewCell.m

package info (click to toggle)
google-authenticator 20130529-2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,584 kB
  • ctags: 855
  • sloc: objc: 3,416; java: 3,324; ansic: 2,922; xml: 210; makefile: 83; python: 10
file content (390 lines) | stat: -rw-r--r-- 12,344 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//
//  OTPTableViewCell.m
//
//  Copyright 2011 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//  http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import "OTPTableViewCell.h"
#import "HOTPGenerator.h"
#import "OTPAuthURL.h"
#import "UIColor+MobileColors.h"
#import "GTMLocalizedString.h"
#import "GTMRoundedRectPath.h"
#import "GTMSystemVersion.h"

@interface OTPTableViewCell ()
@property (readwrite, retain, nonatomic) OTPAuthURL *authURL;
@property (readwrite, assign, nonatomic) BOOL showingInfo;
@property (readonly, nonatomic) BOOL shouldHideInfoButton;

- (void)updateUIForAuthURL:(OTPAuthURL *)authURL;
@end

@interface HOTPTableViewCell ()
- (void)otpAuthURLDidGenerateNewOTP:(NSNotification *)notification;
@end

@interface TOTPTableViewCell ()
- (void)otpAuthURLWillGenerateNewOTP:(NSNotification *)notification;
- (void)otpAuthURLDidGenerateNewOTP:(NSNotification *)notification;
@end

@implementation OTPTableViewCell

@synthesize frontCodeLabel = frontCodeLabel_;
@synthesize frontWarningLabel = frontWarningLabel_;
@synthesize backCheckLabel = backCheckLabel_;
@synthesize backIntegrityCheckLabel = backIntegrityCheckLabel_;
@synthesize frontNameTextField = frontNameTextField_;
@synthesize frontRefreshButton = frontRefreshButton_;
@synthesize frontInfoButton = frontInfoButton_;
@synthesize frontView = frontView_;
@synthesize backView = backView_;
@synthesize authURL = authURL_;
@synthesize showingInfo = showingInfo_;

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier {
  if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
  }
  return self;
}

- (void)dealloc {
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc removeObserver:self];
  self.frontCodeLabel = nil;
  self.frontWarningLabel = nil;
  self.backCheckLabel = nil;
  self.backIntegrityCheckLabel = nil;
  self.frontNameTextField = nil;
  self.frontRefreshButton = nil;
  self.frontInfoButton = nil;
  self.frontView = nil;
  self.backView = nil;
  self.authURL = nil;

  [super dealloc];
}

- (void)layoutSubviews {
  [super layoutSubviews];
  if (!self.frontView) {
    [[NSBundle mainBundle] loadNibNamed:@"OTPTableViewCell"
                                  owner:self
                                options:nil];
    CGRect bounds = self.contentView.bounds;
    self.frontView.frame = bounds;
    [self.contentView addSubview:self.frontView];
    [self updateUIForAuthURL:self.authURL];
    self.backIntegrityCheckLabel.text =
        GTMLocalizedString(@"Integrity Check Value",
                           @"Integerity Check Value label");
  }
}

- (void)updateUIForAuthURL:(OTPAuthURL *)authURL {
  self.frontNameTextField.text = authURL.name;
  NSString *otpCode = authURL.otpCode;
  self.frontCodeLabel.text = otpCode;
  self.frontWarningLabel.text = otpCode;
  self.backCheckLabel.text = authURL.checkCode;
  self.frontInfoButton.hidden = self.shouldHideInfoButton;
}

- (void)setAuthURL:(OTPAuthURL *)authURL {
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc removeObserver:self
                name:OTPAuthURLDidGenerateNewOTPNotification
              object:authURL_];
  [authURL_ autorelease];
  authURL_ = [authURL retain];
  [self updateUIForAuthURL:authURL_];
  [nc addObserver:self
         selector:@selector(otpAuthURLDidGenerateNewOTP:)
             name:OTPAuthURLDidGenerateNewOTPNotification
           object:authURL_];
}

- (void)willBeginEditing {
  [self.frontNameTextField becomeFirstResponder];
}

- (void)didEndEditing {
  [self.frontNameTextField resignFirstResponder];
}

- (void)otpChangeDidStop:(NSString *)animationID
                finished:(NSNumber *)finished
                 context:(void *)context {
  // We retain ourself whenever we start an animation that calls
  // setAnimationStopSelector, so we must release ourself when we are actually
  // called. This is so that we don't disappear out from underneath the
  // animation while it is running.
  if ([animationID isEqual:@"otpFadeOut"]) {
    self.frontWarningLabel.alpha = 0;
    self.frontCodeLabel.alpha = 0;
    NSString *otpCode = self.authURL.otpCode;
    self.frontCodeLabel.text = otpCode;
    self.frontWarningLabel.text = otpCode;
    [UIView beginAnimations:@"otpFadeIn" context:nil];
    [UIView setAnimationDelegate:self];
    [self retain];
    [UIView setAnimationDidStopSelector:@selector(otpChangeDidStop:finished:context:)];
    self.frontCodeLabel.alpha = 1;
    [UIView commitAnimations];
  } else {
    self.frontCodeLabel.alpha = 1;
    self.frontWarningLabel.alpha = 0;
    self.frontWarningLabel.hidden = YES;
  }
  [self release];
}

- (BOOL)canBecomeFirstResponder {
  return YES;
}

- (void)showCopyMenu:(CGPoint)location {
  if (self.showingInfo) return;
  UIView *view = self.frontCodeLabel;
  CGRect selectionRect = [view frame];
  if (CGRectContainsPoint(selectionRect, location) &&
      [self becomeFirstResponder]) {
    UIMenuController *theMenu = [UIMenuController sharedMenuController];
    [theMenu setTargetRect:selectionRect inView:[view superview]];
    [theMenu setMenuVisible:YES animated:YES];
  }
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
  BOOL canPerform = NO;
  if (action == @selector(copy:)) {
    canPerform = YES;
  } else {
    canPerform = [super canPerformAction:action withSender:sender];
  }
  return canPerform;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  [textField resignFirstResponder];
  return YES;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  [super setEditing:editing animated:animated];
  if (!editing) {
    if (![self.authURL.name isEqual:self.frontNameTextField.text]) {
      self.authURL.name = self.frontNameTextField.text;
      // Write out the changes.
      [self.authURL saveToKeychain];
    }
    [self.frontNameTextField resignFirstResponder];
    self.frontNameTextField.userInteractionEnabled = NO;
    self.frontNameTextField.borderStyle = UITextBorderStyleNone;
    if (!self.shouldHideInfoButton) {
      self.frontInfoButton.hidden = NO;
    }
  } else {
    self.frontNameTextField.userInteractionEnabled = YES;
    self.frontNameTextField.borderStyle = UITextBorderStyleRoundedRect;
    self.frontInfoButton.hidden = YES;
    [self hideInfo:self];
  }
}

- (BOOL)shouldHideInfoButton {
  return [self.authURL isKindOfClass:[TOTPAuthURL class]];
}

#pragma mark -
#pragma mark Actions

- (IBAction)copy:(id)sender {
  UIPasteboard *pb = [UIPasteboard generalPasteboard];
  [pb setValue:self.frontCodeLabel.text forPasteboardType:@"public.utf8-plain-text"];
}

- (IBAction)showInfo:(id)sender {
  if (!self.showingInfo) {
    self.backView.frame = self.contentView.bounds;
    [UIView beginAnimations:@"showInfo" context:NULL];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                           forView:self.contentView
                             cache:YES];
    [self.frontView removeFromSuperview];
    [self.contentView addSubview:self.backView];
    [UIView commitAnimations];
    self.showingInfo = YES;
  }
}

- (IBAction)hideInfo:(id)sender {
  if (self.showingInfo) {
    self.frontView.frame = self.contentView.bounds;
    [UIView beginAnimations:@"hideInfo" context:NULL];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:self.contentView
                             cache:YES];
    [backView_ removeFromSuperview];
    [self.contentView addSubview:self.frontView];
    [UIView commitAnimations];
    self.showingInfo = NO;
  }
}

- (IBAction)refreshAuthURL:(id)sender {
  // For subclasses to override.
}

@end

#pragma mark -

@implementation HOTPTableViewCell

- (void)layoutSubviews {
  [super layoutSubviews];
  self.frontRefreshButton.hidden = self.isEditing;
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
  [super setEditing:editing animated:animated];
  if (!editing) {
    self.frontRefreshButton.hidden = NO;
  } else {
    self.frontRefreshButton.hidden = YES;
  }
}

- (IBAction)refreshAuthURL:(id)sender {
  [(HOTPAuthURL *)self.authURL generateNextOTPCode];
}

- (void)otpAuthURLDidGenerateNewOTP:(NSNotification *)notification {
  self.frontCodeLabel.alpha = 1;
  self.frontWarningLabel.alpha = 0;
  [UIView beginAnimations:@"otpFadeOut" context:nil];
  [UIView setAnimationDelegate:self];
  [self retain];
  [UIView setAnimationDidStopSelector:@selector(otpChangeDidStop:finished:context:)];
  self.frontCodeLabel.alpha = 0;
  [UIView commitAnimations];
}

@end

#pragma mark -

@implementation TOTPTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style
    reuseIdentifier:(NSString *)reuseIdentifier {
  if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {
    // Only support backgrounding in iOS 4+.
    if (&UIApplicationWillEnterForegroundNotification != NULL) {
      NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
      [nc addObserver:self
             selector:@selector(applicationWillEnterForeground:)
                 name:UIApplicationWillEnterForegroundNotification
               object:nil];
    }
  }
  return self;
}

- (void)dealloc {
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc removeObserver:self];
  [super dealloc];
}

// On iOS4+ we need to make sure our timer based codes are up to date
// if we have been hidden in the background.
- (void)applicationWillEnterForeground:(UIApplication *)application {
  NSString *code = self.authURL.otpCode;
  NSString *frontText = self.frontCodeLabel.text;
  if (![code isEqual:frontText]) {
    [self otpAuthURLDidGenerateNewOTP:nil];
  }
}

- (void)setAuthURL:(OTPAuthURL *)authURL {
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc removeObserver:self
                name:OTPAuthURLWillGenerateNewOTPWarningNotification
              object:self.authURL];
  super.authURL = authURL;
  [nc addObserver:self
         selector:@selector(otpAuthURLWillGenerateNewOTP:)
             name:OTPAuthURLWillGenerateNewOTPWarningNotification
           object:self.authURL];
}

- (void)otpAuthURLWillGenerateNewOTP:(NSNotification *)notification {
  NSDictionary *userInfo = [notification userInfo];
  NSNumber *nsSeconds
    = [userInfo objectForKey:OTPAuthURLSecondsBeforeNewOTPKey];
  NSUInteger seconds = [nsSeconds unsignedIntegerValue];
  self.frontWarningLabel.alpha = 0;
  self.frontWarningLabel.hidden = NO;
  [UIView beginAnimations:@"Warning" context:nil];
  [UIView setAnimationDuration:seconds];
  self.frontCodeLabel.alpha = 0;
  self.frontWarningLabel.alpha = 1;
  [UIView commitAnimations];
}

- (void)otpAuthURLDidGenerateNewOTP:(NSNotification *)notification {
  self.frontCodeLabel.alpha = 0;
  self.frontWarningLabel.alpha = 1;
  [UIView beginAnimations:@"otpFadeOut" context:nil];
  [UIView setAnimationDelegate:self];
  [self retain];
  [UIView setAnimationDidStopSelector:@selector(otpChangeDidStop:finished:context:)];
  self.frontWarningLabel.alpha = 0;
  [UIView commitAnimations];
}

@end

#pragma mark -

@implementation OTPTableViewCellBackView

- (id)initWithFrame:(CGRect)frame {
  if ((self = [super initWithFrame:frame])) {
    self.opaque = NO;
    self.clearsContextBeforeDrawing = YES;
  }
  return self;
}

- (void)drawRect:(CGRect)rect {
  CGGradientRef gradient = GoogleCreateBlueBarGradient();
  if (gradient) {
    CGContextRef context = UIGraphicsGetCurrentContext();
    GTMCGContextAddRoundRect(context, self.bounds, 8);
    CGContextClip(context);
    CGPoint midTop = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
    CGPoint midBottom = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
    CGContextDrawLinearGradient(context, gradient, midTop, midBottom, 0);
    CFRelease(gradient);
  }
}

@end