Skip to content

Commit

Permalink
Remove usage of plugin namespace and added support for event when ear…
Browse files Browse the repository at this point in the history
…phones are removed on ios
  • Loading branch information
gerhardsletten committed Jun 3, 2016
1 parent ace06e3 commit 9ea1287
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 17 deletions.
3 changes: 2 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</engines>

<js-module src="www/HeadsetDetection.js" name="HeadsetDetection">
<clobbers target="window.plugins.headsetdetection" />
<clobbers target="window.HeadsetDetection" />
</js-module>

<!-- ios -->
Expand All @@ -24,6 +24,7 @@
<config-file target="config.xml" parent="/*">
<feature name="HeadsetDetection">
<param name="ios-package" value="HeadsetDetection"/>
<param name="onload" value="true" />
</feature>
</config-file>

Expand Down
14 changes: 14 additions & 0 deletions src/ios/HeadsetDetection.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

@implementation HeadsetDetection

- (void) pluginInitialize {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChanged:) name:AVAudioSessionRouteChangeNotification object:nil];
}

- (void)routeChanged:(NSNotification *)notification {
NSNumber *reason = [notification.userInfo objectForKey:AVAudioSessionRouteChangeReasonKey];

if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonNewDeviceAvailable) {
[self.commandDelegate evalJs:@"cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetAdded()"];
} else if ([reason unsignedIntegerValue] == AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
[self.commandDelegate evalJs:@"cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetRemoved()"];
}
}

- (void) detect:(CDVInvokedUrlCommand*)command {
CDVPluginResult * pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsBool:[self isHeadsetEnabled]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
Expand Down
33 changes: 17 additions & 16 deletions www/HeadsetDetection.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
function HeadsetDetection() {
}
cordova.define("cordova-plugin-headsetdetection.HeadsetDetection", function(require, exports, module) {
var HeadsetDetection = {
detect: function (successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "HeadsetDetection", "detect", []);
},
registerRemoteEvents: function(actionCallback) {
this.actionCallback = actionCallback;
},
remoteHeadsetRemoved: function() {
this.actionCallback && this.actionCallback('headsetRemove');
},
remoteHeadsetAdded: function() {
this.actionCallback && this.actionCallback('headsetAdded');
}
};

HeadsetDetection.prototype.detect = function (successCallback, errorCallback) {
cordova.exec(successCallback, errorCallback, "HeadsetDetection", "detect", []);
};

HeadsetDetection.install = function () {
if (!window.plugins) {
window.plugins = {};
}

window.plugins.headsetdetection = new HeadsetDetection();
return window.plugins.headsetdetection;
};

cordova.addConstructor(HeadsetDetection.install);
module.exports = HeadsetDetection;
});

0 comments on commit 9ea1287

Please sign in to comment.