Permalink
Browse files

Add HeadsetWatcher plugin.

  • Loading branch information...
ascorbic committed Aug 18, 2012
1 parent 2422b3c commit 75ba65afe3b928ced115508bc583328bc72d1c43
@@ -0,0 +1,76 @@
+/**
+ * HeadsetWatcher plugin for Cordova/Phonegap
+ * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text.
+ *
+ * Copyright (c) Triggertrap Ltd. 2012
+ *
+ */
+
+
+package com.triggertrap;
+
+import org.apache.cordova.api.Plugin;
+import org.apache.cordova.api.PluginResult;
+import org.apache.cordova.api.PluginResult.Status;
+import org.json.JSONArray;
+import org.json.JSONObject;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+import android.util.Log;
+
+public class HeadsetWatcher extends Plugin {
+
+ private String callback;
+ public HeadsetBroadcastReceiver headsetReceiver;
+ @Override
+ public PluginResult execute(String action, JSONArray data, String callbackId) {
+ this.callback = callbackId;
+ headsetReceiver = new HeadsetBroadcastReceiver(this);
+ PluginResult result = new PluginResult(Status.NO_RESULT);
+ this.cordova.getActivity().registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
+ result.setKeepCallback(true);
+ return result;
+ }
+
+ public void changed(int state) {
+
+ JSONObject status = new JSONObject();
+ try {
+ status.put("plugged", state == 1 ? true : false);
+ } catch (Exception ex) {
+ Log.e("Headset", "JSON error " + ex.toString());
+ return;
+ }
+ PluginResult result = new PluginResult(PluginResult.Status.OK, status);
+ result.setKeepCallback(true);
+ this.success(result, this.callback);
+ }
+
+ public class HeadsetBroadcastReceiver extends BroadcastReceiver
+ {
+ protected HeadsetWatcher watcher;
+
+ public HeadsetBroadcastReceiver(HeadsetWatcher watcher) {
+ super();
+ this.watcher = watcher;
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ String action = intent.getAction();
+ Log.d("ACTION_HEADSET_PLUG Received", action);
+ if( (action.compareTo(Intent.ACTION_HEADSET_PLUG)) == 0) {
+ int headsetState = intent.getIntExtra("state", 0);
+ watcher.changed(headsetState);
+ }
+
+ }
+
+ }
+
+
+}
+
@@ -0,0 +1,19 @@
+/**
+ * HeadsetWatcher plugin for Cordova/Phonegap
+ *
+ * Copyright (c) Triggertrap Ltd. 2012. All Rights Reserved.
+ * Available under the terms of the MIT License.
+ *
+ */
+var HeadsetWatcher = {
+ watch: function(callback) {
+ return cordova.exec(function(result) {
+ HeadsetWatcher.plugged = result.plugged;
+ if(callback) {
+ callback(result);
+ }
+
+ }, HeadsetWatcher.fail, "HeadsetWatcher", "watch", []);
+ },
+ plugged: false
+}
@@ -0,0 +1,63 @@
+# HeadsetWatcher plugin for Cordova/Phonegap #
+By Matt Kane / Triggertrap Ltd.
+
+This plugin allows you to watch for headphones being plugged or unplugged from a device.
+It does this by registering for ACTION_HEADSET_PLUG notifications.
+
+
+## Adding the Plugin to your project ##
+
+1. To install the plugin, move HeadsetWatcher.js to your project's www folder and include a reference to it in your html files.
+2. Create a folder called 'com/triggertrap/' within your project's src folder.
+3. And copy the HeadsetWatcher.java file into that new folder.
+4. In your res/xml/plugins.xml file add the following line:
+
+ `<plugin name="HeadsetWatcher" value="com.triggertrap.HeadsetWatcher"/>`
+
+## Using the plugin ##
+
+There is a single method to call: `HeadsetWatcher.watch(callback)`.
+The callback is a function, which is passed an object with the single boolean
+property `plugged`, which indicates whether the headset is currently plugged in.
+It is a persistent callback, and will be called whenever the action occurs.
+The callback will also be called immediately when first registered.
+
+The static property HeadsetWatcher.plugged is also set once watch has been called.
+
+```javascript
+
+ HeadsetWatcher.watch(function(result) {
+ if(result.plugged) {
+ alert("The headphones have been plugged in!");
+ } else {
+ alert("The headphones have been unplugged!");
+ }
+
+ })
+
+
+```
+
+## Licence ##
+
+The MIT License
+
+Copyright © 2012 Triggertrap Ltd.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.

0 comments on commit 75ba65a

Please sign in to comment.