Permalink
Browse files

[CB-1036] factored device info into its own plugin

  • Loading branch information...
filmaj committed Jul 11, 2012
1 parent 73d0cd0 commit 1f4966acc8899169f1550d72a55d35aee38e1cfd
@@ -0,0 +1,33 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+#import <UIKit/UIKit.h>
+#import "CDVPlugin.h"
+
+@interface CDVDevice : CDVPlugin
+{
+}
+
++ (NSString*) cordovaVersion;
+
+- (void)getDeviceInfo:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
+
+@end
+
+
@@ -0,0 +1,102 @@
+/*
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+ */
+
+
+#import "CDVDevice.h"
+#import "CDVViewController.h"
+
+@interface CDVDevice () {
+}
+@end
+
+
+@implementation CDVDevice
+
+- (void)getDeviceInfo:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
+{
+ NSString* cbId = [arguments objectAtIndex:0];
+ NSDictionary *deviceProperties = [self deviceProperties];
+ NSMutableString* result = [[NSMutableString alloc] initWithFormat:@""];
+ CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:deviceProperties];
+
+ /* Settings.plist
+ * Read the optional Settings.plist file and push these user-defined settings down into the web application.
+ * This can be useful for supplying build-time configuration variables down to the app to change its behaviour,
+ * such as specifying Full / Lite version, or localization (English vs German, for instance).
+ */
+ // TODO: turn this into an iOS only plugin
+ NSDictionary *temp = [CDVViewController getBundlePlist:@"Settings"];
+ if ([temp respondsToSelector:@selector(JSONString)]) {
+ [result appendFormat:@"\nwindow.Settings = %@;", [temp JSONString]];
+ }
+
+ NSString* jsResult = [self.webView stringByEvaluatingJavaScriptFromString:result];
+ // if jsResult is not nil nor empty, an error
+ if (jsResult != nil && [jsResult length] > 0) {
+ NSLog(@"%@", jsResult);
+ }
+
+ [result release];
+
+ [self success:pluginResult callbackId:cbId];
+
+}
+
+- (NSDictionary*) deviceProperties
+{
+ UIDevice *device = [UIDevice currentDevice];
+ NSMutableDictionary *devProps = [NSMutableDictionary dictionaryWithCapacity:4];
+ [devProps setObject:[device model] forKey:@"platform"];
+ [devProps setObject:[device systemVersion] forKey:@"version"];
+ [devProps setObject:[device uniqueAppInstanceIdentifier] forKey:@"uuid"];
+ [devProps setObject:[device name] forKey:@"name"];
+ [devProps setObject:[[self class] cordovaVersion] forKey:@"cordova"];
+
+ NSDictionary *devReturn = [NSDictionary dictionaryWithDictionary:devProps];
+ return devReturn;
+}
+
+/**
+ Returns the current version of Cordova as read from the VERSION file
+ This only touches the filesystem once and stores the result in the class variable cdvVersion
+ */
+static NSString* cdvVersion;
++ (NSString*) cordovaVersion
+{
+#ifdef CDV_VERSION
+ cdvVersion = SYMBOL_TO_NSSTRING(CDV_VERSION);
+#else
+
+ if (cdvVersion == nil) {
+ NSBundle *mainBundle = [NSBundle mainBundle];
+ NSString *filename = [mainBundle pathForResource:@"VERSION" ofType:nil];
+ // read from the filesystem and save in the variable
+ // first, separate by new line
+ NSString* fileContents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:NULL];
+ NSArray* all_lines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
+ NSString* first_line = [all_lines objectAtIndex:0];
+
+ cdvVersion = [first_line retain];
+ }
+#endif
+ return cdvVersion;
+}
+
+
+@end
@@ -48,7 +48,6 @@
@property (nonatomic, readwrite, copy) NSString* startPage;
+ (NSDictionary*) getBundlePlist:(NSString*)plistName;
-+ (NSString*) cordovaVersion;
+ (NSString*) applicationDocumentsDirectory;
- (void) dispose;
@@ -114,8 +114,7 @@ - (void) dispose {
- (void) printDeprecationNotice
{
if (!IsAtLeastiOSVersion(@"4.2")) { // TODO: change WARNING to CRITICAL for 2.0
- NSLog(@"WARNING: For Cordova 2.0 (you are using Cordova %@), you will need to upgrade to at least iOS 4.2 or greater. Your current version of iOS is %@.",
- [CDVViewController cordovaVersion],
+ NSLog(@"WARNING: For Cordova 2.0, you will need to upgrade to at least iOS 4.2 or greater. Your current version of iOS is %@.",
[[UIDevice currentDevice] systemVersion]
);
}
@@ -452,36 +451,6 @@ - (void) webViewDidStartLoad:(UIWebView*)theWebView
*/
- (void) webViewDidFinishLoad:(UIWebView*)theWebView
{
- NSDictionary *deviceProperties = [ self deviceProperties];
- NSMutableString* result = [[NSMutableString alloc] initWithFormat:
- @"(function() { \
- try { \
- cordova.require('cordova/plugin/ios/device').setInfo(%@); \
- } catch (e) { \
- return \"Error: executing module function 'setInfo' in module 'cordova/plugin/ios/device'. Have you included the iOS version of the cordova-%@.js file?\"; \
- } \
- })()",
- [deviceProperties JSONString], [CDVViewController cordovaVersion]];
-
- /* Settings.plist
- * Read the optional Settings.plist file and push these user-defined settings down into the web application.
- * This can be useful for supplying build-time configuration variables down to the app to change its behaviour,
- * such as specifying Full / Lite version, or localization (English vs German, for instance).
- */
- // TODO: turn this into an iOS only plugin
- NSDictionary *temp = [[self class] getBundlePlist:@"Settings"];
- if ([temp respondsToSelector:@selector(JSONString)]) {
- [result appendFormat:@"\nwindow.Settings = %@;", [temp JSONString]];
- }
-
- NSString* jsResult = [theWebView stringByEvaluatingJavaScriptFromString:result];
- // if jsResult is not nil nor empty, an error
- if (jsResult != nil && [jsResult length] > 0) {
- NSLog(@"%@", jsResult);
- }
-
- [result release];
-
/*
* Hide the Top Activity THROBBER in the Battery Bar
*/
@@ -921,20 +890,6 @@ - (id) getCommandInstance:(NSString*)pluginName
#pragma mark -
-- (NSDictionary*) deviceProperties
-{
- UIDevice *device = [UIDevice currentDevice];
- NSMutableDictionary *devProps = [NSMutableDictionary dictionaryWithCapacity:4];
- [devProps setObject:[device model] forKey:@"platform"];
- [devProps setObject:[device systemVersion] forKey:@"version"];
- [devProps setObject:[device uniqueAppInstanceIdentifier] forKey:@"uuid"];
- [devProps setObject:[device name] forKey:@"name"];
- [devProps setObject:[CDVViewController cordovaVersion] forKey:@"cordova"];
-
- NSDictionary *devReturn = [NSDictionary dictionaryWithDictionary:devProps];
- return devReturn;
-}
-
- (NSString*) appURLScheme
{
NSString* URLScheme = nil;
@@ -969,33 +924,6 @@ + (NSDictionary*) getBundlePlist:(NSString*)plistName
return temp;
}
-/**
- Returns the current version of Cordova as read from the VERSION file
- This only touches the filesystem once and stores the result in the class variable gapVersion
- */
-static NSString* cdvVersion;
-+ (NSString*) cordovaVersion
-{
-#ifdef CDV_VERSION
- cdvVersion = SYMBOL_TO_NSSTRING(CDV_VERSION);
-#else
-
- if (cdvVersion == nil) {
- NSBundle *mainBundle = [NSBundle mainBundle];
- NSString *filename = [mainBundle pathForResource:@"VERSION" ofType:nil];
- // read from the filesystem and save in the variable
- // first, separate by new line
- NSString* fileContents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:NULL];
- NSArray* all_lines = [fileContents componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];
- NSString* first_line = [all_lines objectAtIndex:0];
-
- cdvVersion = [first_line retain];
- }
-#endif
- return cdvVersion;
-}
-
-
#pragma mark -
#pragma mark UIApplicationDelegate impl
Oops, something went wrong.

0 comments on commit 1f4966a

Please sign in to comment.