From 36814a20f775c4ed8b9dc255f3ae79db350995c9 Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Mon, 21 Nov 2016 16:15:26 +0100 Subject: [PATCH 1/5] Added getMetaData Android function. --- src/android/AppVersion.java | 6 ++++++ www/AppVersionPlugin.js | 18 +++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/android/AppVersion.java b/src/android/AppVersion.java index 204ed7e..f0de6b0 100644 --- a/src/android/AppVersion.java +++ b/src/android/AppVersion.java @@ -35,6 +35,12 @@ 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); + callbackContext.success((String)app.metaData.get(args.getString(0))); + return true; + } return false; } catch (NameNotFoundException e) { callbackContext.success("N/A"); 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; From 71556b4fface6f2c2ffba9f7c0898b8f9201653f Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Mon, 21 Nov 2016 17:18:17 +0100 Subject: [PATCH 2/5] Added getMetaData iOS implementation and modified README.md --- README.md | 7 +++++++ src/ios/AppVersion.h | 2 ++ src/ios/AppVersion.m | 10 ++++++++++ 3 files changed, 19 insertions(+) diff --git a/README.md b/README.md index 3f333de..ce1b136 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,13 @@ 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 custom element from a Windows .appx file + ## Credits Written by [Robert (Jamie) Munro](http://twitter.com/rjmunro) at 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 From 04e5ed71e6a4ea6b6b2489de667c100f2de56fb6 Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Tue, 6 Dec 2016 15:43:44 +0100 Subject: [PATCH 3/5] Added getMetaData windows implementation --- README.md | 19 ++++++++++++++++++- src/windows/AppVersionProxy.js | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ce1b136..dada8fe 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,24 @@ Returns the version number of the app 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 custom element from a Windows .appx 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: +``` +... + + + + + + + DEVELOPMENT + + + + +... +``` ## Credits 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); From a0fa99841cc77b9a21ebf4aba68ea2f84560abc0 Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Tue, 6 Dec 2016 15:48:33 +0100 Subject: [PATCH 4/5] Changed example syntax higlighting settings --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dada8fe..eb75b28 100644 --- a/README.md +++ b/README.md @@ -66,7 +66,7 @@ Returns a custom property from app settings. This could be - 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 ... From 3e6d4709eb0db3b6c16dd1440fa21d5ea05cd54c Mon Sep 17 00:00:00 2001 From: Sebastian Westemeyer Date: Mon, 2 Jan 2017 09:54:13 +0100 Subject: [PATCH 5/5] Prevent ClassCastException. --- src/android/AppVersion.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/android/AppVersion.java b/src/android/AppVersion.java index f0de6b0..e68830c 100644 --- a/src/android/AppVersion.java +++ b/src/android/AppVersion.java @@ -38,7 +38,14 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo if (action.equals("getMetaData")) { PackageManager packageManager = this.cordova.getActivity().getPackageManager(); ApplicationInfo app = packageManager.getApplicationInfo(this.cordova.getActivity().getPackageName(), PackageManager.GET_META_DATA); - callbackContext.success((String)app.metaData.get(args.getString(0))); + 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;