Skip to content

Commit

Permalink
Added event listners for android when headset is attached or removed
Browse files Browse the repository at this point in the history
  • Loading branch information
gerhardsletten committed Jun 27, 2016
1 parent 8d264fd commit b3768fe
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
73 changes: 70 additions & 3 deletions src/android/nl/xservices/plugins/HeadsetDetection.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,69 @@
package nl.xservices.plugins;

import android.content.Context;
import android.media.AudioManager;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaWebView;

import android.content.Context;
import android.media.AudioManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.content.BroadcastReceiver;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HeadsetDetection extends CordovaPlugin {

private static final String LOG_TAG = "HeadsetDetection";

private static final String ACTION_DETECT = "detect";
private static final String ACTION_EVENT = "registerRemoteEvents";
protected static CordovaWebView mCachedWebView = null;

BroadcastReceiver receiver;

public HeadsetDetection() {
this.receiver = null;
}

@Override
public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView);
mCachedWebView = webView;
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Intent.ACTION_HEADSET_PLUG);
this.receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
Log.d(LOG_TAG, "Headset is unplugged");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetRemoved();");
break;
case 1:
Log.d(LOG_TAG, "Headset is plugged");
mCachedWebView.sendJavascript("cordova.require('cordova-plugin-headsetdetection.HeadsetDetection').remoteHeadsetAdded();");
break;
default:
Log.d(LOG_TAG, "I have no idea what the headset state is");
}
}
}
};
mCachedWebView.getContext().registerReceiver(this.receiver, intentFilter);
}

@Override
public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException {
try {
if (ACTION_DETECT.equals(action)) {
if (ACTION_DETECT.equals(action) || ACTION_EVENT.equals(action)) {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, isHeadsetEnabled()));
return true;
} else {
Expand All @@ -34,4 +82,23 @@ private boolean isHeadsetEnabled() {
audioManager.isBluetoothA2dpOn() ||
audioManager.isBluetoothScoOn();
}

public void onDestroy() {
removeHeadsetListener();
}

public void onReset() {
removeHeadsetListener();
}

private void removeHeadsetListener() {
if (this.receiver != null) {
try {
mCachedWebView.getContext().unregisterReceiver(this.receiver);
this.receiver = null;
} catch (Exception e) {
Log.e(LOG_TAG, "Error unregistering battery receiver: " + e.getMessage(), e);
}
}
}
}
2 changes: 2 additions & 0 deletions www/HeadsetDetection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ var HeadsetDetection = {
exec(successCallback, errorCallback, "HeadsetDetection", "detect", []);
},
registerRemoteEvents: function(actionCallback) {
// Need to call a native function to start recieve events on android
exec(null, null, "HeadsetDetection", "registerRemoteEvents", []);
this.actionCallback = actionCallback;
},
remoteHeadsetRemoved: function() {
Expand Down

0 comments on commit b3768fe

Please sign in to comment.