forked from phonegap/phonegap-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeadsetWatcher.java
More file actions
76 lines (60 loc) · 2.12 KB
/
Copy pathHeadsetWatcher.java
File metadata and controls
76 lines (60 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
* 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);
}
}
}
}