From 82c3b33cedb1285c1d4db20d3607f6c0b03b9c42 Mon Sep 17 00:00:00 2001 From: Pete Hodgson Date: Fri, 21 Sep 2012 19:36:28 -0700 Subject: [PATCH] use PA to *set* orientation --- src/OrientationCommand.m | 88 +++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 10 deletions(-) diff --git a/src/OrientationCommand.m b/src/OrientationCommand.m index b2a48e22..6c3d1d8e 100644 --- a/src/OrientationCommand.m +++ b/src/OrientationCommand.m @@ -9,14 +9,52 @@ #import "OrientationCommand.h" #import "NSObject+Franks_SBJSON.h" +#import "PublicAutomation/PublicAutomation.h" +#import "FranklyProtocolHelper.h" + @implementation OrientationCommand -- (NSString *)getOrientationDescriptionViaStatusBar{ - if( UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]) ) - return @"portrait"; - else - return @"landscape"; +- (NSDictionary *)representOrientation:(NSString *)orientation withDetailedOrientation:(NSString *)detailedOrientation{ + return [NSDictionary dictionaryWithObjectsAndKeys:orientation,@"orientation", detailedOrientation,@"detailed_orientation",nil]; +} + +- (NSDictionary *)getOrientationRepresentationViaStatusBar{ + switch([[UIApplication sharedApplication] statusBarOrientation]){ + case UIInterfaceOrientationLandscapeLeft: + return [self representOrientation:@"landscape" withDetailedOrientation:@"landscape_left"]; + case UIInterfaceOrientationLandscapeRight: + return [self representOrientation:@"landscape" withDetailedOrientation:@"landscape_right"]; + case UIInterfaceOrientationPortrait: + return [self representOrientation:@"portrait" withDetailedOrientation:@"portrait"]; + case UIInterfaceOrientationPortraitUpsideDown: + return [self representOrientation:@"portrait" withDetailedOrientation:@"portrait_upside_down"]; + default: + NSLog(@"Device orientation via status bar is unknown"); + return nil; + } +} + +- (NSDictionary *)getOrientationRepresentationViaDevice{ + switch ( [UIDevice currentDevice].orientation ) { + case UIDeviceOrientationLandscapeRight: + return [self representOrientation:@"landscape" withDetailedOrientation:@"landscape_right"]; + case UIDeviceOrientationLandscapeLeft: + return [self representOrientation:@"landscape" withDetailedOrientation:@"landscape_left"]; + case UIDeviceOrientationPortrait: + return [self representOrientation:@"portrait" withDetailedOrientation:@"portrait"]; + case UIDeviceOrientationPortraitUpsideDown: + return [self representOrientation:@"portrait" withDetailedOrientation:@"portrait_upside_down"]; + case UIDeviceOrientationFaceUp: + NSLog(@"Device orientation is face up"); + return nil; + case UIDeviceOrientationFaceDown: + NSLog(@"Device orientation is face down"); + return nil; + default: + NSLog(@"Device orientation via device is unknown"); + return nil; + } } - (NSString *)getOrientationDescriptionViaDevice{ @@ -41,13 +79,43 @@ - (NSString *)getOrientationDescriptionViaDevice{ } } -- (NSString *)handleCommandWithRequestBody:(NSString *)requestBody { - NSString *orientationDescription = [self getOrientationDescriptionViaDevice]; +- (NSString *)handleGet{ + NSDictionary *orientationDescription = [self getOrientationRepresentationViaDevice]; if( !orientationDescription ) - orientationDescription = [self getOrientationDescriptionViaStatusBar]; + orientationDescription = [self getOrientationRepresentationViaDevice]; - NSDictionary *dom = [NSDictionary dictionaryWithObject:orientationDescription forKey:@"orientation"]; - return [dom JSONRepresentation]; + return [orientationDescription JSONRepresentation]; +} + +- (NSString *)handlePost:(NSString *)requestBody{ + requestBody = [requestBody lowercaseString]; + + UIDeviceOrientation requestedOrientation = UIDeviceOrientationUnknown; + if( [requestBody isEqualToString:@"landscape_right"] ){ + requestedOrientation = UIDeviceOrientationLandscapeRight; + }else if( [requestBody isEqualToString:@"landscape_left"] ){ + requestedOrientation = UIDeviceOrientationLandscapeLeft; + }else if( [requestBody isEqualToString:@"portrait"] ){ + requestedOrientation = UIDeviceOrientationPortrait; + }else if( [requestBody isEqualToString:@"portrait_upside_down"] ){ + requestedOrientation = UIDeviceOrientationPortraitUpsideDown; + } + + if( requestedOrientation == UIDeviceOrientationUnknown){ + return [FranklyProtocolHelper generateErrorResponseWithReason:@"unrecognized orientation" + andDetails:[NSString stringWithFormat:@"orientation '%@' is invalid. Use 'landscape_right','landscape_left','portrait', or 'portrait_upside_down'", requestBody]]; + } + + [UIAutomationBridge setOrientation:requestedOrientation]; + + return [FranklyProtocolHelper generateSuccessResponseWithoutResults]; +} + +- (NSString *)handleCommandWithRequestBody:(NSString *)requestBody { + if( !requestBody || [requestBody isEqualToString:@""] ) + return [self handleGet]; + else + return [self handlePost:requestBody]; } @end \ No newline at end of file