xref: /aosp_15_r20/external/walt/ios/WALT/SettingsController.m (revision bf47c6829f95be9dd55f4c5bbc44a71c90aad403)
1*bf47c682SAndroid Build Coastguard Worker/*
2*bf47c682SAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*bf47c682SAndroid Build Coastguard Worker *
4*bf47c682SAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*bf47c682SAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*bf47c682SAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*bf47c682SAndroid Build Coastguard Worker *
8*bf47c682SAndroid Build Coastguard Worker *      http://www.apache.org/licenses/LICENSE-2.0
9*bf47c682SAndroid Build Coastguard Worker *
10*bf47c682SAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*bf47c682SAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*bf47c682SAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*bf47c682SAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*bf47c682SAndroid Build Coastguard Worker * limitations under the License.
15*bf47c682SAndroid Build Coastguard Worker */
16*bf47c682SAndroid Build Coastguard Worker
17*bf47c682SAndroid Build Coastguard Worker#import "SettingsController.h"
18*bf47c682SAndroid Build Coastguard Worker
19*bf47c682SAndroid Build Coastguard Worker#import "UIAlertView+Extensions.h"
20*bf47c682SAndroid Build Coastguard Worker#import "WALTAppDelegate.h"
21*bf47c682SAndroid Build Coastguard Worker#import "WALTClient.h"
22*bf47c682SAndroid Build Coastguard Worker
23*bf47c682SAndroid Build Coastguard Worker@implementation SettingsController {
24*bf47c682SAndroid Build Coastguard Worker  WALTClient *_client;
25*bf47c682SAndroid Build Coastguard Worker  NSString *_status;
26*bf47c682SAndroid Build Coastguard Worker}
27*bf47c682SAndroid Build Coastguard Worker
28*bf47c682SAndroid Build Coastguard Worker- (void)viewDidLoad {
29*bf47c682SAndroid Build Coastguard Worker  [super viewDidLoad];
30*bf47c682SAndroid Build Coastguard Worker
31*bf47c682SAndroid Build Coastguard Worker  _client = ((WALTAppDelegate *)[UIApplication sharedApplication].delegate).client;
32*bf47c682SAndroid Build Coastguard Worker}
33*bf47c682SAndroid Build Coastguard Worker
34*bf47c682SAndroid Build Coastguard Worker- (void)viewWillAppear:(BOOL)animated {
35*bf47c682SAndroid Build Coastguard Worker  [super viewWillAppear:animated];
36*bf47c682SAndroid Build Coastguard Worker
37*bf47c682SAndroid Build Coastguard Worker  _status = [NSString string];
38*bf47c682SAndroid Build Coastguard Worker  [self.tableView reloadData];
39*bf47c682SAndroid Build Coastguard Worker}
40*bf47c682SAndroid Build Coastguard Worker
41*bf47c682SAndroid Build Coastguard Worker- (IBAction)ping:(id)sender {
42*bf47c682SAndroid Build Coastguard Worker  NSTimeInterval start = _client.currentTime;
43*bf47c682SAndroid Build Coastguard Worker  NSError *error = nil;
44*bf47c682SAndroid Build Coastguard Worker  if (![_client sendCommand:WALTPingCommand error:&error]) {
45*bf47c682SAndroid Build Coastguard Worker    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error];
46*bf47c682SAndroid Build Coastguard Worker    [alert show];
47*bf47c682SAndroid Build Coastguard Worker  } else {
48*bf47c682SAndroid Build Coastguard Worker    NSData *response = [_client readResponseWithTimeout:kWALTReadTimeout];
49*bf47c682SAndroid Build Coastguard Worker    if (!response) {
50*bf47c682SAndroid Build Coastguard Worker      _status = @"Timed out waiting for ping response.";
51*bf47c682SAndroid Build Coastguard Worker    } else {
52*bf47c682SAndroid Build Coastguard Worker      NSTimeInterval delta = _client.currentTime - start;
53*bf47c682SAndroid Build Coastguard Worker      _status = [NSString stringWithFormat:@"Ping response in %.2f ms.", delta * 1000];
54*bf47c682SAndroid Build Coastguard Worker    }
55*bf47c682SAndroid Build Coastguard Worker  }
56*bf47c682SAndroid Build Coastguard Worker  [self.tableView reloadData];
57*bf47c682SAndroid Build Coastguard Worker}
58*bf47c682SAndroid Build Coastguard Worker
59*bf47c682SAndroid Build Coastguard Worker- (IBAction)checkDrift:(id)sender {
60*bf47c682SAndroid Build Coastguard Worker  NSError *error = nil;
61*bf47c682SAndroid Build Coastguard Worker  if (![_client updateSyncBoundsWithError:&error]) {
62*bf47c682SAndroid Build Coastguard Worker    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WALT Connection Error" error:error];
63*bf47c682SAndroid Build Coastguard Worker    [alert show];
64*bf47c682SAndroid Build Coastguard Worker  } else {
65*bf47c682SAndroid Build Coastguard Worker    _status = [NSString stringWithFormat:@"Remote clock delayed between %lld and %lld µs.",
66*bf47c682SAndroid Build Coastguard Worker                  _client.minError, _client.maxError];
67*bf47c682SAndroid Build Coastguard Worker  }
68*bf47c682SAndroid Build Coastguard Worker  [self.tableView reloadData];
69*bf47c682SAndroid Build Coastguard Worker}
70*bf47c682SAndroid Build Coastguard Worker
71*bf47c682SAndroid Build Coastguard Worker- (NSIndexPath *)tableView:(UITableView *)tableView
72*bf47c682SAndroid Build Coastguard Worker  willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
73*bf47c682SAndroid Build Coastguard Worker  if (indexPath.section == 0 && indexPath.row == 0) {
74*bf47c682SAndroid Build Coastguard Worker    // "Ping"
75*bf47c682SAndroid Build Coastguard Worker    [self ping:tableView];
76*bf47c682SAndroid Build Coastguard Worker    return nil;
77*bf47c682SAndroid Build Coastguard Worker  } else if (indexPath.section == 0 && indexPath.row == 1) {
78*bf47c682SAndroid Build Coastguard Worker    // "Check Drift"
79*bf47c682SAndroid Build Coastguard Worker    [self checkDrift:tableView];
80*bf47c682SAndroid Build Coastguard Worker    return nil;
81*bf47c682SAndroid Build Coastguard Worker  }
82*bf47c682SAndroid Build Coastguard Worker  return indexPath;
83*bf47c682SAndroid Build Coastguard Worker}
84*bf47c682SAndroid Build Coastguard Worker
85*bf47c682SAndroid Build Coastguard Worker- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
86*bf47c682SAndroid Build Coastguard Worker  return (section == 0 ? _status : nil);
87*bf47c682SAndroid Build Coastguard Worker}
88*bf47c682SAndroid Build Coastguard Worker@end
89