OLD | NEW |
| (Empty) |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
6 #error "This file requires ARC support." | |
7 #endif | |
8 | |
9 #import "remoting/client/ios/app/client_connection_view_controller.h" | |
10 | |
11 #import "ios/third_party/material_components_ios/src/components/ActivityIndicato
r/src/MDCActivityIndicator.h" | |
12 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" | |
13 #import "ios/third_party/material_components_ios/src/components/NavigationBar/sr
c/MaterialNavigationBar.h" | |
14 #import "remoting/client/ios/app/pin_entry_view.h" | |
15 #import "remoting/client/ios/domain/client_session_details.h" | |
16 #import "remoting/client/ios/session/remoting_client.h" | |
17 | |
18 #include "base/strings/sys_string_conversions.h" | |
19 #include "remoting/protocol/client_authentication_config.h" | |
20 | |
21 static const CGFloat kIconRadius = 30.f; | |
22 static const CGFloat kActivityIndicatorStrokeWidth = 3.f; | |
23 static const CGFloat kActivityIndicatorRadius = 33.f; | |
24 | |
25 static const CGFloat kPinEntryViewWidth = 240.f; | |
26 static const CGFloat kPinEntryViewHeight = 90.f; | |
27 | |
28 static const CGFloat kCenterShift = -80.f; | |
29 static const CGFloat kPadding = 20.f; | |
30 static const CGFloat kMargin = 20.f; | |
31 | |
32 static const CGFloat kBarHeight = 58.f; | |
33 | |
34 @interface ClientConnectionViewController ()<PinEntryDelegate> { | |
35 UIImageView* _iconView; | |
36 MDCActivityIndicator* _activityIndicator; | |
37 UILabel* _statusLabel; | |
38 MDCNavigationBar* _navBar; | |
39 PinEntryView* _pinEntryView; | |
40 NSString* _remoteHostName; | |
41 } | |
42 @end | |
43 | |
44 @implementation ClientConnectionViewController | |
45 | |
46 @synthesize state = _state; | |
47 @synthesize delegate = _delegate; | |
48 | |
49 - (id)init { | |
50 self = [super init]; | |
51 if (self) { | |
52 self.navigationItem.rightBarButtonItem = | |
53 [[UIBarButtonItem alloc] initWithTitle:@"CANCEL" | |
54 style:UIBarButtonItemStylePlain | |
55 target:self | |
56 action:@selector(didTapCancel:)]; | |
57 | |
58 _navBar = [[MDCNavigationBar alloc] initWithFrame:CGRectZero]; | |
59 [_navBar observeNavigationItem:self.navigationItem]; | |
60 | |
61 [_navBar setBackgroundColor:[UIColor blackColor]]; | |
62 MDCNavigationBarTextColorAccessibilityMutator* mutator = | |
63 [[MDCNavigationBarTextColorAccessibilityMutator alloc] init]; | |
64 [mutator mutate:_navBar]; | |
65 [self.view addSubview:_navBar]; | |
66 _navBar.translatesAutoresizingMaskIntoConstraints = NO; | |
67 _remoteHostName = @""; | |
68 } | |
69 return self; | |
70 } | |
71 | |
72 #pragma mark - UIViewController | |
73 | |
74 - (void)loadView { | |
75 [super loadView]; | |
76 | |
77 self.view.backgroundColor = [UIColor blackColor]; | |
78 | |
79 _activityIndicator = [[MDCActivityIndicator alloc] initWithFrame:CGRectZero]; | |
80 [self.view addSubview:_activityIndicator]; | |
81 | |
82 _statusLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
83 [self.view addSubview:_statusLabel]; | |
84 | |
85 _iconView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
86 [self.view addSubview:_iconView]; | |
87 | |
88 _pinEntryView = [[PinEntryView alloc] init]; | |
89 [self.view addSubview:_pinEntryView]; | |
90 _pinEntryView.delegate = self; | |
91 } | |
92 | |
93 - (void)viewDidLoad { | |
94 [super viewDidLoad]; | |
95 | |
96 _iconView.contentMode = UIViewContentModeCenter; | |
97 _iconView.alpha = 0.87f; | |
98 _iconView.backgroundColor = UIColor.lightGrayColor; | |
99 _iconView.layer.cornerRadius = kIconRadius; | |
100 _iconView.layer.masksToBounds = YES; | |
101 _iconView.image = [UIImage imageNamed:@"ic_desktop"]; | |
102 | |
103 _activityIndicator.radius = kActivityIndicatorRadius; | |
104 _activityIndicator.trackEnabled = YES; | |
105 _activityIndicator.strokeWidth = kActivityIndicatorStrokeWidth; | |
106 _activityIndicator.cycleColors = @[ [UIColor whiteColor] ]; | |
107 | |
108 _statusLabel.numberOfLines = 1; | |
109 _statusLabel.lineBreakMode = NSLineBreakByTruncatingTail; | |
110 _statusLabel.textColor = [UIColor whiteColor]; | |
111 _statusLabel.textAlignment = NSTextAlignmentCenter; | |
112 | |
113 _pinEntryView.hidden = YES; | |
114 } | |
115 | |
116 - (void)viewWillLayoutSubviews { | |
117 [super viewWillLayoutSubviews]; | |
118 | |
119 _navBar.frame = CGRectMake(0.f, 0.f, self.view.frame.size.width, kBarHeight); | |
120 [_navBar setNeedsLayout]; | |
121 | |
122 _iconView.frame = CGRectMake(0, 0, kIconRadius * 2, kIconRadius * 2.f); | |
123 _iconView.center = | |
124 CGPointMake(self.view.center.x, self.view.center.y + kCenterShift); | |
125 | |
126 [_activityIndicator sizeToFit]; | |
127 _activityIndicator.center = _iconView.center; | |
128 | |
129 _statusLabel.frame = | |
130 CGRectMake(kMargin, _activityIndicator.center.y + kIconRadius + kPadding, | |
131 self.view.frame.size.width - kMargin * 2.f, | |
132 _statusLabel.font.pointSize * _statusLabel.numberOfLines); | |
133 | |
134 _pinEntryView.frame = CGRectMake( | |
135 (self.view.frame.size.width - kPinEntryViewWidth) / 2.f, | |
136 _statusLabel.frame.origin.y + _statusLabel.frame.size.height + kPadding, | |
137 kPinEntryViewWidth, kPinEntryViewHeight); | |
138 } | |
139 | |
140 - (void)viewWillAppear:(BOOL)animated { | |
141 [super viewWillAppear:animated]; | |
142 [self.navigationController setNavigationBarHidden:YES animated:animated]; | |
143 | |
144 [[NSNotificationCenter defaultCenter] | |
145 addObserver:self | |
146 selector:@selector(hostSessionStatusChanged:) | |
147 name:kHostSessionStatusChanged | |
148 object:nil]; | |
149 } | |
150 | |
151 - (void)viewDidAppear:(BOOL)animated { | |
152 [super viewDidAppear:animated]; | |
153 [[NSNotificationCenter defaultCenter] | |
154 addObserver:self | |
155 selector:@selector(keyboardWillShow:) | |
156 name:UIKeyboardWillShowNotification | |
157 object:nil]; | |
158 | |
159 [[NSNotificationCenter defaultCenter] | |
160 addObserver:self | |
161 selector:@selector(keyboardWillHide:) | |
162 name:UIKeyboardWillHideNotification | |
163 object:nil]; | |
164 | |
165 [_activityIndicator startAnimating]; | |
166 } | |
167 | |
168 - (void)viewWillDisappear:(BOOL)animated { | |
169 [super viewWillDisappear:animated]; | |
170 [_activityIndicator stopAnimating]; | |
171 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
172 } | |
173 | |
174 #pragma mark - Keyboard | |
175 | |
176 - (void)keyboardWillShow:(NSNotification*)notification { | |
177 CGSize keyboardSize = | |
178 [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] | |
179 CGRectValue] | |
180 .size; | |
181 | |
182 [UIView | |
183 animateWithDuration:0.3 | |
184 animations:^{ | |
185 CGRect f = self.view.frame; | |
186 CGFloat newHeight = | |
187 self.view.frame.size.height - keyboardSize.height; | |
188 CGFloat overlap = | |
189 newHeight - (_pinEntryView.frame.origin.y + | |
190 _pinEntryView.frame.size.height + kPadding); | |
191 if (overlap < 0) { | |
192 f.origin.y = overlap; | |
193 self.view.frame = f; | |
194 } | |
195 }]; | |
196 } | |
197 | |
198 - (void)keyboardWillHide:(NSNotification*)notification { | |
199 [UIView animateWithDuration:0.3 | |
200 animations:^{ | |
201 CGRect f = self.view.frame; | |
202 f.origin.y = 0.f; | |
203 self.view.frame = f; | |
204 }]; | |
205 } | |
206 | |
207 #pragma mark - Properties | |
208 | |
209 - (void)setState:(ClientConnectionViewState)state { | |
210 _state = state; | |
211 switch (_state) { | |
212 case ClientViewConnecting: | |
213 [self showConnectingState]; | |
214 break; | |
215 case ClientViewPinPrompt: | |
216 [self showPinPromptState]; | |
217 break; | |
218 case ClientViewConnected: | |
219 [self showConnectedState]; | |
220 break; | |
221 case ClientViewClosed: | |
222 [self dismissViewControllerAnimated:YES completion:nil]; | |
223 break; | |
224 } | |
225 } | |
226 | |
227 - (void)setDelegate:(id<ClientConnectionViewControllerDelegate>)delegate { | |
228 _delegate = delegate; | |
229 if (_delegate) { | |
230 _remoteHostName = [_delegate getConnectingHostName]; | |
231 // To get the view to use the new remote host name. | |
232 [self setState:_state]; | |
233 } | |
234 } | |
235 | |
236 #pragma mark - Private | |
237 | |
238 - (void)showConnectingState { | |
239 [_pinEntryView endEditing:YES]; | |
240 _statusLabel.text = | |
241 [NSString stringWithFormat:@"Connecting to %@", _remoteHostName]; | |
242 [_activityIndicator stopAnimating]; | |
243 _activityIndicator.cycleColors = @[ [UIColor whiteColor] ]; | |
244 _activityIndicator.indicatorMode = MDCActivityIndicatorModeIndeterminate; | |
245 _activityIndicator.hidden = NO; | |
246 _pinEntryView.hidden = YES; | |
247 [_activityIndicator startAnimating]; | |
248 } | |
249 | |
250 - (void)showPinPromptState { | |
251 _statusLabel.text = [NSString stringWithFormat:@"%@", _remoteHostName]; | |
252 [_activityIndicator stopAnimating]; | |
253 _activityIndicator.hidden = YES; | |
254 _pinEntryView.hidden = NO; | |
255 [_pinEntryView becomeFirstResponder]; | |
256 } | |
257 | |
258 - (void)showConnectedState { | |
259 [_pinEntryView endEditing:YES]; | |
260 _statusLabel.text = | |
261 [NSString stringWithFormat:@"Connected to %@", _remoteHostName]; | |
262 _activityIndicator.progress = 0.0; | |
263 _pinEntryView.hidden = YES; | |
264 _activityIndicator.hidden = NO; | |
265 _activityIndicator.indicatorMode = MDCActivityIndicatorModeDeterminate; | |
266 _activityIndicator.cycleColors = @[ [UIColor greenColor] ]; | |
267 [_activityIndicator startAnimating]; | |
268 _activityIndicator.progress = 1.0; | |
269 [self dismissViewControllerAnimated:YES | |
270 completion:^{ | |
271 [_delegate clientConnected]; | |
272 }]; | |
273 } | |
274 | |
275 - (void)didProvidePin:(NSString*)pin createPairing:(BOOL)createPairing { | |
276 // TODO(nicholss): There is an open question if createPairing is supported on | |
277 // iOS. Need to fingure this out. | |
278 [[NSNotificationCenter defaultCenter] | |
279 postNotificationName:kHostSessionPinProvided | |
280 object:self | |
281 userInfo:[NSDictionary dictionaryWithObject:pin | |
282 forKey:kHostSessionPin]]; | |
283 } | |
284 | |
285 - (void)didTapCancel:(id)sender { | |
286 NSLog(@"%@ was tapped.", NSStringFromClass([sender class])); | |
287 // TODO(nicholss): Need to cancel the pending connection. | |
288 [self dismissViewControllerAnimated:YES completion:nil]; | |
289 } | |
290 | |
291 - (void)hostSessionStatusChanged:(NSNotification*)notification { | |
292 ClientConnectionViewState state; | |
293 ClientSessionDetails* sessionDetails = | |
294 [[notification userInfo] objectForKey:kSessionDetails]; | |
295 switch (sessionDetails.state) { | |
296 case SessionInitializing: | |
297 // Same as HostConnecting in UI. Fall-though. | |
298 case SessionAuthenticated: | |
299 // Same as HostConnecting in UI. Fall-though. | |
300 case SessionConnecting: | |
301 state = ClientViewConnecting; | |
302 break; | |
303 case SessionPinPrompt: | |
304 state = ClientViewPinPrompt; | |
305 break; | |
306 case SessionConnected: | |
307 state = ClientViewConnected; | |
308 break; | |
309 case SessionFailed: | |
310 // TODO(nicholss): Implement an error screen. | |
311 case SessionClosed: | |
312 state = ClientViewClosed; | |
313 break; | |
314 default: | |
315 LOG(ERROR) << "Unknown State for Session, " << sessionDetails.state; | |
316 return; | |
317 } | |
318 [[NSOperationQueue mainQueue] addOperationWithBlock:^{ | |
319 [self setState:state]; | |
320 }]; | |
321 } | |
322 | |
323 @end | |
OLD | NEW |