Skip to content

Commit

Permalink
use PA to *set* orientation
Browse files Browse the repository at this point in the history
  • Loading branch information
moredip committed Sep 22, 2012
1 parent ecf7621 commit 82c3b33
Showing 1 changed file with 78 additions and 10 deletions.
88 changes: 78 additions & 10 deletions src/OrientationCommand.m
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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

0 comments on commit 82c3b33

Please sign in to comment.