diff --git a/README.md b/README.md index 3f333de..eb75b28 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,30 @@ Returns the build identifier of the app Returns the version number of the app +### getMetaData(propertyName) + +Returns a custom property from app settings. This could be +- a "meta-data" element from an Android manifest file +- a custom element from an iOS Info.plist file +- a preference value from a Windows config.xml file + +Can be used in combination with [cordova-custom-config](https://github.com/dpa99c/cordova-custom-config) plugin. Example config.xml: +```xml +... + + + + + + + DEVELOPMENT + + + + +... +``` + ## Credits Written by [Robert (Jamie) Munro](http://twitter.com/rjmunro) at diff --git a/src/android/AppVersion.java b/src/android/AppVersion.java index 204ed7e..e68830c 100644 --- a/src/android/AppVersion.java +++ b/src/android/AppVersion.java @@ -35,6 +35,19 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo callbackContext.success(packageManager.getPackageInfo(this.cordova.getActivity().getPackageName(), 0).versionCode); return true; } + if (action.equals("getMetaData")) { + PackageManager packageManager = this.cordova.getActivity().getPackageManager(); + ApplicationInfo app = packageManager.getApplicationInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_META_DATA); + Object metadata = app.metaData.get(args.getString(0)); + + if (metadata instanceof Integer) { + callbackContext.success((Integer)metadata); + } else { + callbackContext.success(metadata.toString()); + } + + return true; + } return false; } catch (NameNotFoundException e) { callbackContext.success("N/A"); diff --git a/src/ios/AppVersion.h b/src/ios/AppVersion.h index 87c0330..24edc72 100644 --- a/src/ios/AppVersion.h +++ b/src/ios/AppVersion.h @@ -10,4 +10,6 @@ - (void)getVersionCode:(CDVInvokedUrlCommand*)command; +- (void)getMetaData:(CDVInvokedUrlCommand*)command; + @end diff --git a/src/ios/AppVersion.m b/src/ios/AppVersion.m index a73c78f..65cd46d 100644 --- a/src/ios/AppVersion.m +++ b/src/ios/AppVersion.m @@ -44,4 +44,14 @@ - (void)getVersionCode:(CDVInvokedUrlCommand*)command [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId]; } +- (void)getMetaData:(CDVInvokedUrlCommand*)command +{ + NSString* callbackId = command.callbackId; + // get property parameter content + NSString *propertyName = command.arguments.count == 0 ? @"" : [command.arguments objectAtIndex:0]; + NSString* version = [[[NSBundle mainBundle] infoDictionary] objectForKey:propertyName]; + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:version]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId]; +} + @end diff --git a/src/windows/AppVersionProxy.js b/src/windows/AppVersionProxy.js index 124c298..d6eefb3 100644 --- a/src/windows/AppVersionProxy.js +++ b/src/windows/AppVersionProxy.js @@ -28,6 +28,22 @@ AppVersionProxy = { getVersionCode: function (successCallback, failCallback, args) { var build = Windows.ApplicationModel.Package.current.id.version.build; successCallback(build); + }, + getMetaData: function (successCallback, failCallback, args) { + Windows.ApplicationModel.Package.current.installedLocation.getFileAsync("config.xml").then(function (file) { + Windows.Data.Xml.Dom.XmlDocument.loadFromFileAsync(file).then(function (xdoc) { + xdoc.getElementsByTagName("preference").forEach(function(preference, index, array) { + var attributes = preference.attributes; + if ( attributes && attributes.length === 2 ) { + if ( attributes[0].nodeName === "name" && attributes[0].nodeValue === args[0] ) { + successCallback(attributes[1].nodeValue); + } else if ( attributes[1].nodeName === "name" && attributes[1].nodeValue === args[0] ) { + successCallback(attributes[0].nodeValue); + } + } + }); + }, (failCallback || function(){})); + }, (failCallback || function(){})); } }; cordova.commandProxy.add("AppVersion", AppVersionProxy); diff --git a/www/AppVersionPlugin.js b/www/AppVersionPlugin.js index abb9381..7bf665e 100644 --- a/www/AppVersionPlugin.js +++ b/www/AppVersionPlugin.js @@ -3,7 +3,7 @@ "use strict"; // Returns a jQuery or AngularJS deferred object, or pass a success and fail callbacks if you don't want to use jQuery or AngularJS -var getPromisedCordovaExec = function (command, success, fail) { +var getPromisedCordovaExec = function (command, success, fail, params) { var toReturn, deferred, injector, $q; if (success === undefined) { if (window.jQuery) { @@ -38,28 +38,32 @@ var getPromisedCordovaExec = function (command, success, fail) { } } // 5th param is NOT optional. must be at least empty array - cordova.exec(success, fail, "AppVersion", command, []); + cordova.exec(success, fail, "AppVersion", command, params); return toReturn; }; var getAppVersion = function (success, fail) { - return getPromisedCordovaExec('getVersionNumber', success, fail); + return getPromisedCordovaExec('getVersionNumber', success, fail, []); }; getAppVersion.getAppName = function (success, fail) { - return getPromisedCordovaExec('getAppName', success, fail); + return getPromisedCordovaExec('getAppName', success, fail, []); }; getAppVersion.getPackageName = function (success, fail) { - return getPromisedCordovaExec('getPackageName', success, fail); + return getPromisedCordovaExec('getPackageName', success, fail, []); }; getAppVersion.getVersionNumber = function (success, fail) { - return getPromisedCordovaExec('getVersionNumber', success, fail); + return getPromisedCordovaExec('getVersionNumber', success, fail, []); }; getAppVersion.getVersionCode = function (success, fail) { - return getPromisedCordovaExec('getVersionCode', success, fail); + return getPromisedCordovaExec('getVersionCode', success, fail, []); +}; + +getAppVersion.getMetaData = function (property, success, fail) { + return getPromisedCordovaExec('getMetaData', success, fail, [ property ]); }; module.exports = getAppVersion;