Skip to content

Commit

Permalink
Merge branch 'feature/cordova-2.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
grgar committed Apr 19, 2013
2 parents 2eca1a1 + 9de46b5 commit ecb83fc
Show file tree
Hide file tree
Showing 36 changed files with 1,355 additions and 835 deletions.
3 changes: 2 additions & 1 deletion CordovaLib/Classes/CDVAvailability.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#define __CORDOVA_2_2_0 20200
#define __CORDOVA_2_3_0 20300
#define __CORDOVA_2_4_0 20400
#define __CORDOVA_2_5_0 20500
#define __CORDOVA_NA 99999 /* not available */

/*
Expand All @@ -46,7 +47,7 @@
#endif
*/
#ifndef CORDOVA_VERSION_MIN_REQUIRED
#define CORDOVA_VERSION_MIN_REQUIRED __CORDOVA_2_4_0
#define CORDOVA_VERSION_MIN_REQUIRED __CORDOVA_2_5_0
#endif

/*
Expand Down
1 change: 1 addition & 0 deletions CordovaLib/Classes/CDVCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ typedef NSUInteger CDVMediaType;
- (void)takePicture:(CDVInvokedUrlCommand*)command;
- (void)postImage:(UIImage*)anImage withFilename:(NSString*)filename toUrl:(NSURL*)url;
- (void)cleanup:(CDVInvokedUrlCommand*)command;
- (void)repositionPopover:(CDVInvokedUrlCommand*)command;

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info;
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingImage:(UIImage*)image editingInfo:(NSDictionary*)editingInfo;
Expand Down
162 changes: 92 additions & 70 deletions CordovaLib/Classes/CDVCamera.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ - (void)takePicture:(CDVInvokedUrlCommand*)command
targetSize = CGSizeMake([targetWidth floatValue], [targetHeight floatValue]);
}

// If a popover is already open, close it; we only want one at a time.
if (([[self pickerController] popoverController] != nil) && [[[self pickerController] popoverController] isPopoverVisible]) {
[[[self pickerController] popoverController] dismissPopoverAnimated:YES];
[[[self pickerController] popoverController] setDelegate:nil];
[[self pickerController] setPopoverController:nil];
}

CDVCameraPicker* cameraPicker = [[CDVCameraPicker alloc] init];
self.pickerController = cameraPicker;

Expand Down Expand Up @@ -128,28 +135,8 @@ - (void)takePicture:(CDVInvokedUrlCommand*)command
if (cameraPicker.popoverController == nil) {
cameraPicker.popoverController = [[NSClassFromString (@"UIPopoverController")alloc] initWithContentViewController:cameraPicker];
}
int x = 0;
int y = 32;
int width = 320;
int height = 480;
UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirectionAny;
NSDictionary* options = [command.arguments objectAtIndex:10 withDefault:nil];
if (options) {
x = [options integerValueForKey:@"x" defaultValue:0];
y = [options integerValueForKey:@"y" defaultValue:32];
width = [options integerValueForKey:@"width" defaultValue:320];
height = [options integerValueForKey:@"height" defaultValue:480];
arrowDirection = [options integerValueForKey:@"arrowDir" defaultValue:UIPopoverArrowDirectionAny];
if (![org_apache_cordova_validArrowDirections containsObject:[NSNumber numberWithInt:arrowDirection]]) {
arrowDirection = UIPopoverArrowDirectionAny;
}
}

cameraPicker.popoverController.delegate = self;
[cameraPicker.popoverController presentPopoverFromRect:CGRectMake(x, y, width, height)
inView:[self.webView superview]
permittedArrowDirections:arrowDirection
animated:YES];
[self displayPopover:options];
} else {
if ([self.viewController respondsToSelector:@selector(presentViewController:::)]) {
[self.viewController presentViewController:cameraPicker animated:YES completion:nil];
Expand All @@ -160,6 +147,39 @@ - (void)takePicture:(CDVInvokedUrlCommand*)command
self.hasPendingOperation = YES;
}

- (void)repositionPopover:(CDVInvokedUrlCommand*)command
{
NSDictionary* options = [command.arguments objectAtIndex:0 withDefault:nil];

[self displayPopover:options];
}

- (void)displayPopover:(NSDictionary*)options
{
int x = 0;
int y = 32;
int width = 320;
int height = 480;
UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirectionAny;

if (options) {
x = [options integerValueForKey:@"x" defaultValue:0];
y = [options integerValueForKey:@"y" defaultValue:32];
width = [options integerValueForKey:@"width" defaultValue:320];
height = [options integerValueForKey:@"height" defaultValue:480];
arrowDirection = [options integerValueForKey:@"arrowDir" defaultValue:UIPopoverArrowDirectionAny];
if (![org_apache_cordova_validArrowDirections containsObject:[NSNumber numberWithInt:arrowDirection]]) {
arrowDirection = UIPopoverArrowDirectionAny;
}
}

[[[self pickerController] popoverController] setDelegate:self];
[[[self pickerController] popoverController] presentPopoverFromRect:CGRectMake(x, y, width, height)
inView:[self.webView superview]
permittedArrowDirections:arrowDirection
animated:YES];
}

- (void)cleanup:(CDVInvokedUrlCommand*)command
{
// empty the tmp directory
Expand Down Expand Up @@ -232,66 +252,68 @@ - (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingM
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// IMAGE TYPE
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
// get the image
UIImage* image = nil;
if (cameraPicker.allowsEditing && [info objectForKey:UIImagePickerControllerEditedImage]) {
image = [info objectForKey:UIImagePickerControllerEditedImage];
if (cameraPicker.returnType == DestinationTypeNativeUri) {
NSString* nativeUri = [(NSURL*)[info objectForKey:UIImagePickerControllerReferenceURL] absoluteString];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nativeUri];
} else {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}
// get the image
UIImage* image = nil;
if (cameraPicker.allowsEditing && [info objectForKey:UIImagePickerControllerEditedImage]) {
image = [info objectForKey:UIImagePickerControllerEditedImage];
} else {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
}

if (cameraPicker.saveToPhotoAlbum) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
if (cameraPicker.saveToPhotoAlbum) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

if (cameraPicker.correctOrientation) {
image = [self imageCorrectedForCaptureOrientation:image];
}
if (cameraPicker.correctOrientation) {
image = [self imageCorrectedForCaptureOrientation:image];
}

UIImage* scaledImage = nil;
UIImage* scaledImage = nil;

if ((cameraPicker.targetSize.width > 0) && (cameraPicker.targetSize.height > 0)) {
// if cropToSize, resize image and crop to target size, otherwise resize to fit target without cropping
if (cameraPicker.cropToSize) {
scaledImage = [self imageByScalingAndCroppingForSize:image toSize:cameraPicker.targetSize];
} else {
scaledImage = [self imageByScalingNotCroppingForSize:image toSize:cameraPicker.targetSize];
if ((cameraPicker.targetSize.width > 0) && (cameraPicker.targetSize.height > 0)) {
// if cropToSize, resize image and crop to target size, otherwise resize to fit target without cropping
if (cameraPicker.cropToSize) {
scaledImage = [self imageByScalingAndCroppingForSize:image toSize:cameraPicker.targetSize];
} else {
scaledImage = [self imageByScalingNotCroppingForSize:image toSize:cameraPicker.targetSize];
}
}
}

NSData* data = nil;
NSData* data = nil;

if (cameraPicker.encodingType == EncodingTypePNG) {
data = UIImagePNGRepresentation(scaledImage == nil ? image : scaledImage);
} else {
data = UIImageJPEGRepresentation(scaledImage == nil ? image : scaledImage, cameraPicker.quality / 100.0f);
}
if (cameraPicker.encodingType == EncodingTypePNG) {
data = UIImagePNGRepresentation(scaledImage == nil ? image : scaledImage);
} else {
data = UIImageJPEGRepresentation(scaledImage == nil ? image : scaledImage, cameraPicker.quality / 100.0f);
}

if (cameraPicker.returnType == DestinationTypeFileUri) {
// write to temp directory and return URI
// get the temp directory path
NSString* docsPath = [NSTemporaryDirectory ()stringByStandardizingPath];
NSError* err = nil;
NSFileManager* fileMgr = [[NSFileManager alloc] init]; // recommended by apple (vs [NSFileManager defaultManager]) to be threadsafe
// generate unique file name
NSString* filePath;

int i = 1;
do {
filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, cameraPicker.encodingType == EncodingTypePNG ? @"png":@"jpg"];
} while ([fileMgr fileExistsAtPath:filePath]);

// save file
if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
if (cameraPicker.returnType == DestinationTypeFileUri) {
// write to temp directory and return URI
// get the temp directory path
NSString* docsPath = [NSTemporaryDirectory ()stringByStandardizingPath];
NSError* err = nil;
NSFileManager* fileMgr = [[NSFileManager alloc] init]; // recommended by apple (vs [NSFileManager defaultManager]) to be threadsafe
// generate unique file name
NSString* filePath;

int i = 1;
do {
filePath = [NSString stringWithFormat:@"%@/%@%03d.%@", docsPath, CDV_PHOTO_PREFIX, i++, cameraPicker.encodingType == EncodingTypePNG ? @"png":@"jpg"];
} while ([fileMgr fileExistsAtPath:filePath]);

// save file
if (![data writeToFile:filePath options:NSAtomicWrite error:&err]) {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_IO_EXCEPTION messageAsString:[err localizedDescription]];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[NSURL fileURLWithPath:filePath] absoluteString]];
}
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[[NSURL fileURLWithPath:filePath] absoluteString]];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[data base64EncodedString]];
}
} else if (cameraPicker.returnType == DestinationTypeNativeUri) {
NSString* nativeUri = [(NSURL*)[info objectForKey:UIImagePickerControllerReferenceURL] absoluteString];
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:nativeUri];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[data base64EncodedString]];
}
}
// NOT IMAGE TYPE (MOVIE)
Expand Down
1 change: 0 additions & 1 deletion CordovaLib/Classes/CDVCapture.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one
#import "CDVCapture.h"
#import "CDVJSON.h"
#import "CDVAvailability.h"
#import "CDVViewController.h"

#define kW3CMediaFormatHeight @"height"
#define kW3CMediaFormatWidth @"width"
Expand Down
5 changes: 5 additions & 0 deletions CordovaLib/Classes/CDVCommandDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@

@class CDVPlugin;
@class CDVPluginResult;
@class CDVWhitelist;

@protocol CDVCommandDelegate <NSObject>

@property (nonatomic, readonly) NSDictionary* settings;

- (NSString*)pathForResource:(NSString*)resourcepath;
- (id)getCommandInstance:(NSString*)pluginName;
- (void)registerPlugin:(CDVPlugin*)plugin withClassName:(NSString*)className CDV_DEPRECATED(2.2, "Use CDVViewController to register plugins, or use config.xml.");
Expand All @@ -46,5 +49,7 @@
- (void)runInBackground:(void (^)())block;
// Returns the User-Agent of the associated UIWebView.
- (NSString*)userAgent;
// Returns whether the given URL passes the white-list.
- (BOOL)URLIsWhitelisted:(NSURL*)url;

@end
11 changes: 11 additions & 0 deletions CordovaLib/Classes/CDVCommandDelegateImpl.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,15 @@ - (NSString*)userAgent
return [_viewController userAgent];
}

- (BOOL)URLIsWhitelisted:(NSURL*)url
{
return ![_viewController.whitelist schemeIsAllowed:[url scheme]] ||
[_viewController.whitelist URLIsAllowed:url];
}

- (NSDictionary*)settings
{
return _viewController.settings;
}

@end
3 changes: 1 addition & 2 deletions CordovaLib/Classes/CDVConfigParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
@property (nonatomic, readonly, strong) NSMutableDictionary* pluginsDict;
@property (nonatomic, readonly, strong) NSMutableDictionary* settings;
@property (nonatomic, readonly, strong) NSMutableArray* whitelistHosts;
@property (nonatomic, readonly, strong) NSMutableArray* startupPluginNames;
@property (nonatomic, readonly, strong) NSString* startPage;

- (NSString*)getStartPage;

@end
31 changes: 14 additions & 17 deletions CordovaLib/Classes/CDVConfigParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,41 @@ @interface CDVConfigParser ()
@property (nonatomic, readwrite, strong) NSMutableDictionary* pluginsDict;
@property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
@property (nonatomic, readwrite, strong) NSMutableArray* whitelistHosts;
@property (nonatomic, readwrite, strong) NSMutableArray* startupPluginNames;
@property (nonatomic, readwrite, strong) NSString* startPage;

@end

@implementation CDVConfigParser

@synthesize pluginsDict, settings, whitelistHosts, startPage;
@synthesize pluginsDict, settings, whitelistHosts, startPage, startupPluginNames;

- (id)init
{
self = [super init];
if (self != nil) {
self.pluginsDict = [[NSMutableDictionary alloc] initWithCapacity:4];
self.settings = [[NSMutableDictionary alloc] initWithCapacity:4];
self.whitelistHosts = [[NSMutableArray alloc] initWithCapacity:1];
self.pluginsDict = [[NSMutableDictionary alloc] initWithCapacity:30];
self.settings = [[NSMutableDictionary alloc] initWithCapacity:30];
self.whitelistHosts = [[NSMutableArray alloc] initWithCapacity:30];
self.startupPluginNames = [[NSMutableArray alloc] initWithCapacity:8];
}
return self;
}

- (void)parser:(NSXMLParser*)parser didStartElement:(NSString*)elementName namespaceURI:(NSString*)namespaceURI qualifiedName:(NSString*)qualifiedName attributes:(NSDictionary*)attributeDict
{
if ([elementName isEqualToString:@"preference"]) {
[settings setObject:[attributeDict objectForKey:@"value"] forKey:[attributeDict objectForKey:@"name"]];
settings[attributeDict[@"name"]] = attributeDict[@"value"];
} else if ([elementName isEqualToString:@"plugin"]) {
[pluginsDict setObject:[attributeDict objectForKey:@"value"] forKey:[attributeDict objectForKey:@"name"]];
NSString* name = [attributeDict[@"name"] lowercaseString];
pluginsDict[name] = attributeDict[@"value"];
if ([@"true" isEqualToString:attributeDict[@"onload"]]) {
[self.startupPluginNames addObject:name];
}
} else if ([elementName isEqualToString:@"access"]) {
[whitelistHosts addObject:[attributeDict objectForKey:@"origin"]];
[whitelistHosts addObject:attributeDict[@"origin"]];
} else if ([elementName isEqualToString:@"content"]) {
self.startPage = [attributeDict objectForKey:@"src"];
self.startPage = attributeDict[@"src"];
}
}

Expand All @@ -61,13 +67,4 @@ - (void)parser:(NSXMLParser*)parser parseErrorOccurred:(NSError*)parseError
NSAssert(NO, @"config.xml parse error line %d col %d", [parser lineNumber], [parser columnNumber]);
}

- (NSString*)getStartPage
{
if (self.startPage != nil) {
return self.startPage;
} else {
return @"index.html";
}
}

@end
1 change: 1 addition & 0 deletions CordovaLib/Classes/CDVDevice.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ - (void)getDeviceInfo:(CDVInvokedUrlCommand*)command
NSDictionary* temp = [CDVViewController getBundlePlist:@"Settings"];

if ([temp respondsToSelector:@selector(JSONString)]) {
NSLog(@"Deprecation warning: window.Setting will be removed Aug 2013. Refer to https://issues.apache.org/jira/browse/CB-2433");
NSString* js = [NSString stringWithFormat:@"window.Settings = %@;", [temp JSONString]];
[self.commandDelegate evalJs:js];
}
Expand Down
Loading

0 comments on commit ecb83fc

Please sign in to comment.