Skip to content

Added getMetaData function #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
...
<platform name="android">
<config-file target="AndroidManifest.xml" parent="./application">
<meta-data android:name="ENVIRONMENT" android:value="DEVELOPMENT" />
</config-file>
</platform>
<platform name="ios">
<config-file platform="ios" target="*-Info.plist" parent="ENVIRONMENT"><string>DEVELOPMENT</string></config-file>
</platform>
<platform name="windows">
<preference name="ENVIRONMENT" value="DEVELOPMENT" />
</platform>
...
```

## Credits

Written by [Robert (Jamie) Munro](http://twitter.com/rjmunro) at
Expand Down
13 changes: 13 additions & 0 deletions src/android/AppVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 2 additions & 0 deletions src/ios/AppVersion.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@

- (void)getVersionCode:(CDVInvokedUrlCommand*)command;

- (void)getMetaData:(CDVInvokedUrlCommand*)command;

@end
10 changes: 10 additions & 0 deletions src/ios/AppVersion.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions src/windows/AppVersionProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
18 changes: 11 additions & 7 deletions www/AppVersionPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;