diff options
Diffstat (limited to 'plugins/uk.co.whiteoctober.cordova.appversion/src')
5 files changed, 136 insertions, 0 deletions
diff --git a/plugins/uk.co.whiteoctober.cordova.appversion/src/android/AppVersion.java b/plugins/uk.co.whiteoctober.cordova.appversion/src/android/AppVersion.java new file mode 100644 index 00000000..204ed7e8 --- /dev/null +++ b/plugins/uk.co.whiteoctober.cordova.appversion/src/android/AppVersion.java @@ -0,0 +1,45 @@ +package uk.co.whiteoctober.cordova; + +import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CallbackContext; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.pm.ApplicationInfo; +import android.content.pm.PackageManager.NameNotFoundException; +import android.content.pm.PackageManager; + +public class AppVersion extends CordovaPlugin { + @Override + public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { + + try { + if (action.equals("getAppName")) { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + ApplicationInfo app = packageManager.getApplicationInfo(this.cordova.getActivity().getPackageName(), 0); + callbackContext.success((String)packageManager.getApplicationLabel(app)); + return true; + } + if (action.equals("getPackageName")) { + callbackContext.success(this.cordova.getActivity().getPackageName()); + return true; + } + if (action.equals("getVersionNumber")) { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + callbackContext.success(packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionName); + return true; + } + if (action.equals("getVersionCode")) { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + callbackContext.success(packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode); + return true; + } + return false; + } catch (NameNotFoundException e) { + callbackContext.success("N/A"); + return true; + } + } + +} diff --git a/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.h b/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.h new file mode 100644 index 00000000..87c0330b --- /dev/null +++ b/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.h @@ -0,0 +1,13 @@ +#import <Cordova/CDVPlugin.h> + +@interface AppVersion : CDVPlugin + +- (void)getAppName:(CDVInvokedUrlCommand*)command; + +- (void)getPackageName:(CDVInvokedUrlCommand*)command; + +- (void)getVersionNumber:(CDVInvokedUrlCommand*)command; + +- (void)getVersionCode:(CDVInvokedUrlCommand*)command; + +@end diff --git a/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.m b/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.m new file mode 100644 index 00000000..a73c78f9 --- /dev/null +++ b/plugins/uk.co.whiteoctober.cordova.appversion/src/ios/AppVersion.m @@ -0,0 +1,47 @@ +#import "AppVersion.h" +#import <Cordova/CDVPluginResult.h> + +@implementation AppVersion + +- (void)getAppName : (CDVInvokedUrlCommand *)command +{ + NSString * callbackId = command.callbackId; + NSString * version =[[[NSBundle mainBundle]infoDictionary]objectForKey :@"CFBundleDisplayName"]; + CDVPluginResult * pluginResult =[CDVPluginResult resultWithStatus : CDVCommandStatus_OK messageAsString : version]; + [self.commandDelegate sendPluginResult : pluginResult callbackId : callbackId]; +} + +- (void)getPackageName:(CDVInvokedUrlCommand*)command +{ + NSString* callbackId = command.callbackId; + NSString* packageName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:packageName]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId]; +} + +- (void)getVersionNumber:(CDVInvokedUrlCommand*)command +{ + NSString* callbackId = command.callbackId; + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; + if (version == nil) { + NSLog(@"CFBundleShortVersionString was nil, attempting CFBundleVersion"); + version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; + if (version == nil) { + NSLog(@"CFBundleVersion was also nil, giving up"); + // not calling error callback here to maintain backward compatibility + } + } + + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId]; +} + +- (void)getVersionCode:(CDVInvokedUrlCommand*)command +{ + NSString* callbackId = command.callbackId; + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId]; +} + +@end diff --git a/plugins/uk.co.whiteoctober.cordova.appversion/src/windows8/AppVersionProxy.js b/plugins/uk.co.whiteoctober.cordova.appversion/src/windows8/AppVersionProxy.js new file mode 100644 index 00000000..3b4c50ee --- /dev/null +++ b/plugins/uk.co.whiteoctober.cordova.appversion/src/windows8/AppVersionProxy.js @@ -0,0 +1,7 @@ +AppVersionProxy = { + getVersionNumber: function (successCallback, failCallback, args) { + var version = Windows.ApplicationModel.Package.current.id.version; + successCallback([version.major, version.minor, version.build, version.revision].join('.')); + } +}; +cordova.commandProxy.add("AppVersion", AppVersionProxy); diff --git a/plugins/uk.co.whiteoctober.cordova.appversion/src/wp8/AppVersion.cs b/plugins/uk.co.whiteoctober.cordova.appversion/src/wp8/AppVersion.cs new file mode 100644 index 00000000..2f3d2c95 --- /dev/null +++ b/plugins/uk.co.whiteoctober.cordova.appversion/src/wp8/AppVersion.cs @@ -0,0 +1,24 @@ +using System; +using System.Windows; +using System.Windows.Navigation; +using Microsoft.Phone.Controls; +using WPCordovaClassLib.Cordova; +using WPCordovaClassLib.Cordova.Commands; +using WPCordovaClassLib.Cordova.JSON; +using Windows.ApplicationModel; +using System.Xml.Linq; + +namespace Cordova.Extension.Commands +{ + public class AppVersion : BaseCommand + { + public void getVersionNumber(string empty) + { + //Windows.ApplicationModel.Package.current.id.version is NOT working in Windows Phone 8 + //Workaround based on http://stackoverflow.com/questions/14371275/how-can-i-get-my-windows-store-apps-title-and-version-info + String version= XDocument.Load("WMAppManifest.xml").Root.Element("App").Attribute("Version").Value; + + this.DispatchCommandResult(new PluginResult(PluginResult.Status.OK, version)); + } + } +} |
