|
@@ -22,33 +22,43 @@ import java.util.HashMap;
|
22
|
22
|
import java.util.List;
|
23
|
23
|
import java.util.Map;
|
24
|
24
|
|
25
|
|
-
|
26
|
25
|
/**
|
27
|
|
- * Module implementing a simple API to select the appropriate audio device for a conference call.
|
|
26
|
+ * Module implementing a simple API to select the appropriate audio device for a
|
|
27
|
+ * conference call.
|
28
|
28
|
*
|
29
|
|
- * Audio calls should use <tt>AudioModeModule.AUDIO_CALL</tt>, which uses the builtin earpiece,
|
30
|
|
- * wired headset or bluetooth headset. The builtin earpiece is the default audio device.
|
|
29
|
+ * Audio calls should use <tt>AudioModeModule.AUDIO_CALL</tt>, which uses the
|
|
30
|
+ * builtin earpiece, wired headset or bluetooth headset. The builtin earpiece is
|
|
31
|
+ * the default audio device.
|
31
|
32
|
*
|
32
|
|
- * Video calls should should use <tt>AudioModeModule.VIDEO_CALL</tt>, which uses the builtin
|
33
|
|
- * speaker, earpiece, wired headset or bluetooth headset. The builtin speaker is the default
|
34
|
|
- * audio device.
|
|
33
|
+ * Video calls should should use <tt>AudioModeModule.VIDEO_CALL</tt>, which uses
|
|
34
|
+ * the builtin speaker, earpiece, wired headset or bluetooth headset. The
|
|
35
|
+ * builtin speaker is the default audio device.
|
35
|
36
|
*
|
36
|
|
- * Before a call has started and after it has ended the <tt>AudioModeModule.DEFAULT</tt> mode
|
37
|
|
- * should be used.
|
|
37
|
+ * Before a call has started and after it has ended the
|
|
38
|
+ * <tt>AudioModeModule.DEFAULT</tt> mode should be used.
|
38
|
39
|
*/
|
39
|
40
|
public class AudioModeModule extends ReactContextBaseJavaModule {
|
40
|
41
|
/**
|
41
|
42
|
* Constants representing the audio mode.
|
42
|
|
- * - DEFAULT: Used before and after every call. It represents the default audio routing scheme.
|
43
|
|
- * - AUDIO_CALL: Used for audio only calls. It will use the earpiece by default, unless a
|
44
|
|
- * wired or Bluetooth headset is connected.
|
45
|
|
- * - VIDEO_CALL: Used for video calls. It will use the speaker by default, unless a wired or
|
46
|
|
- * Bluetooth headset is connected.
|
|
43
|
+ * - DEFAULT: Used before and after every call. It represents the default
|
|
44
|
+ * audio routing scheme.
|
|
45
|
+ * - AUDIO_CALL: Used for audio only calls. It will use the earpiece by
|
|
46
|
+ * default, unless a wired or Bluetooth headset is connected.
|
|
47
|
+ * - VIDEO_CALL: Used for video calls. It will use the speaker by default,
|
|
48
|
+ * unless a wired or Bluetooth headset is connected.
|
47
|
49
|
*/
|
48
|
50
|
private static final int DEFAULT = 0;
|
49
|
51
|
private static final int AUDIO_CALL = 1;
|
50
|
52
|
private static final int VIDEO_CALL = 2;
|
51
|
53
|
|
|
54
|
+ /**
|
|
55
|
+ *
|
|
56
|
+ */
|
|
57
|
+ private static final String ACTION_HEADSET_PLUG
|
|
58
|
+ = (android.os.Build.VERSION.SDK_INT >= 21)
|
|
59
|
+ ? AudioManager.ACTION_HEADSET_PLUG
|
|
60
|
+ : Intent.ACTION_HEADSET_PLUG;
|
|
61
|
+
|
52
|
62
|
/**
|
53
|
63
|
* React Native module name.
|
54
|
64
|
*/
|
|
@@ -60,14 +70,16 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
60
|
70
|
static final String TAG = MODULE_NAME;
|
61
|
71
|
|
62
|
72
|
/**
|
63
|
|
- * Audio mode currently in use.
|
|
73
|
+ * {@link AudioManager} instance used to interact with the Android audio
|
|
74
|
+ * subsystem.
|
64
|
75
|
*/
|
65
|
|
- private int mode = -1;
|
|
76
|
+ private final AudioManager audioManager;
|
66
|
77
|
|
67
|
78
|
/**
|
68
|
|
- * {@link AudioManager} instance used to interact with the Android audio subsystem.
|
|
79
|
+ * {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in
|
|
80
|
+ * old (< M) Android versions.
|
69
|
81
|
*/
|
70
|
|
- private final AudioManager audioManager;
|
|
82
|
+ private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
|
71
|
83
|
|
72
|
84
|
/**
|
73
|
85
|
* {@link Handler} for running all operations on the main thread.
|
|
@@ -80,27 +92,23 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
80
|
92
|
private final Runnable mainThreadRunner;
|
81
|
93
|
|
82
|
94
|
/**
|
83
|
|
- * {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in old (< M)
|
84
|
|
- * Android versions.
|
85
|
|
- */
|
86
|
|
- private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
|
87
|
|
-
|
88
|
|
- /**
|
89
|
|
- *
|
|
95
|
+ * Audio mode currently in use.
|
90
|
96
|
*/
|
91
|
|
- private static final String ACTION_HEADSET_PLUG = (android.os.Build.VERSION.SDK_INT >= 21) ?
|
92
|
|
- AudioManager.ACTION_HEADSET_PLUG : Intent.ACTION_HEADSET_PLUG;
|
|
97
|
+ private int mode = -1;
|
93
|
98
|
|
94
|
99
|
/**
|
95
|
|
- * Initializes a new module instance. There shall be a single instance of this module throughout
|
96
|
|
- * the lifetime of the application.
|
|
100
|
+ * Initializes a new module instance. There shall be a single instance of
|
|
101
|
+ * this module throughout the lifetime of the application.
|
97
|
102
|
*
|
98
|
|
- * @param reactContext the {@link ReactApplicationContext} where this module is created.
|
|
103
|
+ * @param reactContext the {@link ReactApplicationContext} where this module
|
|
104
|
+ * is created.
|
99
|
105
|
*/
|
100
|
106
|
public AudioModeModule(ReactApplicationContext reactContext) {
|
101
|
107
|
super(reactContext);
|
102
|
108
|
|
103
|
|
- audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
|
|
109
|
+ audioManager
|
|
110
|
+ = (AudioManager)
|
|
111
|
+ reactContext.getSystemService(Context.AUDIO_SERVICE);
|
104
|
112
|
mainThreadHandler = new Handler(Looper.getMainLooper());
|
105
|
113
|
mainThreadRunner = new Runnable() {
|
106
|
114
|
@Override
|
|
@@ -111,96 +119,66 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
111
|
119
|
}
|
112
|
120
|
};
|
113
|
121
|
|
114
|
|
- // Setup runtime device change detection
|
|
122
|
+ // Setup runtime device change detection.
|
115
|
123
|
setupAudioRouteChangeDetection();
|
116
|
124
|
}
|
117
|
125
|
|
118
|
|
- /**
|
119
|
|
- * Gets the name for this module, to be used in the React Native bridge.
|
120
|
|
- *
|
121
|
|
- * @return a string with the module name.
|
122
|
|
- */
|
123
|
|
- @Override
|
124
|
|
- public String getName() {
|
125
|
|
- return MODULE_NAME;
|
126
|
|
- }
|
127
|
|
-
|
128
|
126
|
/**
|
129
|
127
|
* Gets a mapping with the constants this module is exporting.
|
130
|
128
|
*
|
131
|
|
- * @return a {@link Map} mapping the constants to be exported with their values.
|
|
129
|
+ * @return a {@link Map} mapping the constants to be exported with their
|
|
130
|
+ * values.
|
132
|
131
|
*/
|
133
|
132
|
@Override
|
134
|
133
|
public Map<String, Object> getConstants() {
|
135
|
134
|
Map<String, Object> constants = new HashMap<>();
|
136
|
|
- constants.put("DEFAULT", DEFAULT);
|
|
135
|
+
|
137
|
136
|
constants.put("AUDIO_CALL", AUDIO_CALL);
|
|
137
|
+ constants.put("DEFAULT", DEFAULT);
|
138
|
138
|
constants.put("VIDEO_CALL", VIDEO_CALL);
|
|
139
|
+
|
139
|
140
|
return constants;
|
140
|
141
|
}
|
141
|
142
|
|
142
|
143
|
/**
|
143
|
|
- * Updates the audio route for the given mode.
|
|
144
|
+ * Gets the name for this module, to be used in the React Native bridge.
|
144
|
145
|
*
|
145
|
|
- * @param mode the audio mode to be used when computing the audio route.
|
146
|
|
- * @return true if the audio route was updated successfully, false otherwise.
|
|
146
|
+ * @return a string with the module name.
|
147
|
147
|
*/
|
148
|
|
- private boolean updateAudioRoute(int mode) {
|
149
|
|
- Log.d(TAG, "Update audio route for mode: " + mode);
|
150
|
|
-
|
151
|
|
- if (mode == DEFAULT) {
|
152
|
|
- audioManager.setMode(AudioManager.MODE_NORMAL);
|
153
|
|
- audioManager.abandonAudioFocus(null);
|
154
|
|
- audioManager.setSpeakerphoneOn(false);
|
155
|
|
- audioManager.setMicrophoneMute(true);
|
156
|
|
- setBluetoothAudioRoute(false);
|
157
|
|
-
|
158
|
|
- return true;
|
159
|
|
- }
|
160
|
|
-
|
161
|
|
- audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
|
162
|
|
- audioManager.setMicrophoneMute(false);
|
163
|
|
-
|
164
|
|
- if (audioManager.requestAudioFocus(
|
165
|
|
- null,
|
166
|
|
- AudioManager.STREAM_VOICE_CALL,
|
167
|
|
- AudioManager.AUDIOFOCUS_GAIN)
|
168
|
|
- == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
|
169
|
|
- Log.d(TAG, "Audio focus request failed");
|
170
|
|
- return false;
|
171
|
|
- }
|
|
148
|
+ @Override
|
|
149
|
+ public String getName() {
|
|
150
|
+ return MODULE_NAME;
|
|
151
|
+ }
|
172
|
152
|
|
173
|
|
- boolean useSpeaker = (mode == VIDEO_CALL);
|
|
153
|
+ /**
|
|
154
|
+ * Helper method to trigger an audio route update when devices change. It
|
|
155
|
+ * makes sure the operation is performed on the main thread.
|
|
156
|
+ */
|
|
157
|
+ void onAudioDeviceChange() {
|
|
158
|
+ mainThreadHandler.post(mainThreadRunner);
|
|
159
|
+ }
|
174
|
160
|
|
175
|
|
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
176
|
|
- // On Android >= M we use the AudioDeviceCallback API, so turn on Bluetooth SCO
|
177
|
|
- // from the start.
|
178
|
|
- if (audioManager.isBluetoothScoAvailableOffCall()) {
|
179
|
|
- audioManager.startBluetoothSco();
|
180
|
|
- }
|
|
161
|
+ /**
|
|
162
|
+ * Helper method to set the output route to a Bluetooth device.
|
|
163
|
+ *
|
|
164
|
+ * @param enabled true if Bluetooth should use used, false otherwise.
|
|
165
|
+ */
|
|
166
|
+ private void setBluetoothAudioRoute(boolean enabled) {
|
|
167
|
+ if (enabled) {
|
|
168
|
+ audioManager.startBluetoothSco();
|
|
169
|
+ audioManager.setBluetoothScoOn(true);
|
181
|
170
|
} else {
|
182
|
|
- // On older Android versions we must set the Bluetooth route manually. Also disable
|
183
|
|
- // the speaker in that case.
|
184
|
|
- setBluetoothAudioRoute(bluetoothHeadsetMonitor.isHeadsetAvailable());
|
185
|
|
- if (bluetoothHeadsetMonitor.isHeadsetAvailable()) {
|
186
|
|
- useSpeaker = false;
|
187
|
|
- }
|
|
171
|
+ audioManager.setBluetoothScoOn(false);
|
|
172
|
+ audioManager.stopBluetoothSco();
|
188
|
173
|
}
|
189
|
|
-
|
190
|
|
- // XXX: isWiredHeadsetOn is not deprecated when used just for knowing if there is a wired
|
191
|
|
- // headset connected, regardless of audio being routed to it.
|
192
|
|
- audioManager.setSpeakerphoneOn(useSpeaker &&
|
193
|
|
- !(audioManager.isWiredHeadsetOn() || audioManager.isBluetoothScoOn()));
|
194
|
|
-
|
195
|
|
- return true;
|
196
|
174
|
}
|
197
|
175
|
|
198
|
176
|
/**
|
199
|
177
|
* Public method to set the current audio mode.
|
200
|
178
|
*
|
201
|
179
|
* @param mode the desired audio mode.
|
202
|
|
- * @param promise a {@link Promise} which will be resolved if the audio mode could be updated
|
203
|
|
- * successfully, and it will be rejected otherwise.
|
|
180
|
+ * @param promise a {@link Promise} which will be resolved if the audio mode
|
|
181
|
+ * could be updated successfully, and it will be rejected otherwise.
|
204
|
182
|
*/
|
205
|
183
|
@ReactMethod
|
206
|
184
|
public void setMode(final int mode, final Promise promise) {
|
|
@@ -216,7 +194,9 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
216
|
194
|
AudioModeModule.this.mode = mode;
|
217
|
195
|
promise.resolve(null);
|
218
|
196
|
} else {
|
219
|
|
- promise.reject("setMode", "Failed to set the requested audio mode");
|
|
197
|
+ promise.reject(
|
|
198
|
+ "setMode",
|
|
199
|
+ "Failed to set the requested audio mode");
|
220
|
200
|
}
|
221
|
201
|
}
|
222
|
202
|
};
|
|
@@ -231,7 +211,7 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
231
|
211
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
232
|
212
|
setupAudioRouteChangeDetectionM();
|
233
|
213
|
} else {
|
234
|
|
- setupAudioRouteChangeDetectionOld();
|
|
214
|
+ setupAudioRouteChangeDetectionPreM();
|
235
|
215
|
}
|
236
|
216
|
}
|
237
|
217
|
|
|
@@ -243,13 +223,15 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
243
|
223
|
android.media.AudioDeviceCallback audioDeviceCallback =
|
244
|
224
|
new android.media.AudioDeviceCallback() {
|
245
|
225
|
@Override
|
246
|
|
- public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
|
|
226
|
+ public void onAudioDevicesAdded(
|
|
227
|
+ AudioDeviceInfo[] addedDevices) {
|
247
|
228
|
Log.d(TAG, "Audio devices added");
|
248
|
229
|
onAudioDeviceChange();
|
249
|
230
|
}
|
250
|
231
|
|
251
|
232
|
@Override
|
252
|
|
- public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
|
|
233
|
+ public void onAudioDevicesRemoved(
|
|
234
|
+ AudioDeviceInfo[] removedDevices) {
|
253
|
235
|
Log.d(TAG, "Audio devices removed");
|
254
|
236
|
onAudioDeviceChange();
|
255
|
237
|
}
|
|
@@ -261,10 +243,10 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
261
|
243
|
/**
|
262
|
244
|
* Audio route change detection mechanism for Android API < 23.
|
263
|
245
|
*/
|
264
|
|
- private void setupAudioRouteChangeDetectionOld() {
|
|
246
|
+ private void setupAudioRouteChangeDetectionPreM() {
|
265
|
247
|
ReactContext reactContext = getReactApplicationContext();
|
266
|
248
|
|
267
|
|
- // Detect changes in wired headset connections
|
|
249
|
+ // Detect changes in wired headset connections.
|
268
|
250
|
IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);
|
269
|
251
|
BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
|
270
|
252
|
@Override
|
|
@@ -275,31 +257,72 @@ public class AudioModeModule extends ReactContextBaseJavaModule {
|
275
|
257
|
};
|
276
|
258
|
reactContext.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);
|
277
|
259
|
|
278
|
|
- // Detect Bluetooth device changes
|
279
|
|
- bluetoothHeadsetMonitor =
|
280
|
|
- new BluetoothHeadsetMonitor(this, this.getReactApplicationContext());
|
|
260
|
+ // Detect Bluetooth device changes.
|
|
261
|
+ bluetoothHeadsetMonitor
|
|
262
|
+ = new BluetoothHeadsetMonitor(
|
|
263
|
+ this,
|
|
264
|
+ getReactApplicationContext());
|
281
|
265
|
bluetoothHeadsetMonitor.start();
|
282
|
266
|
}
|
283
|
267
|
|
284
|
268
|
/**
|
285
|
|
- * Helper method to set the output route to a Bluetooth device.
|
286
|
|
- * @param enabled true if Bluetooth should use used, false otherwise.
|
|
269
|
+ * Updates the audio route for the given mode.
|
|
270
|
+ *
|
|
271
|
+ * @param mode the audio mode to be used when computing the audio route.
|
|
272
|
+ * @return true if the audio route was updated successfully, false
|
|
273
|
+ * otherwise.
|
287
|
274
|
*/
|
288
|
|
- private void setBluetoothAudioRoute(boolean enabled) {
|
289
|
|
- if (enabled) {
|
290
|
|
- audioManager.startBluetoothSco();
|
291
|
|
- audioManager.setBluetoothScoOn(true);
|
|
275
|
+ private boolean updateAudioRoute(int mode) {
|
|
276
|
+ Log.d(TAG, "Update audio route for mode: " + mode);
|
|
277
|
+
|
|
278
|
+ if (mode == DEFAULT) {
|
|
279
|
+ audioManager.setMode(AudioManager.MODE_NORMAL);
|
|
280
|
+ audioManager.abandonAudioFocus(null);
|
|
281
|
+ audioManager.setSpeakerphoneOn(false);
|
|
282
|
+ audioManager.setMicrophoneMute(true);
|
|
283
|
+ setBluetoothAudioRoute(false);
|
|
284
|
+
|
|
285
|
+ return true;
|
|
286
|
+ }
|
|
287
|
+
|
|
288
|
+ audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
|
|
289
|
+ audioManager.setMicrophoneMute(false);
|
|
290
|
+
|
|
291
|
+ if (audioManager.requestAudioFocus(
|
|
292
|
+ null,
|
|
293
|
+ AudioManager.STREAM_VOICE_CALL,
|
|
294
|
+ AudioManager.AUDIOFOCUS_GAIN)
|
|
295
|
+ == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
|
|
296
|
+ Log.d(TAG, "Audio focus request failed");
|
|
297
|
+ return false;
|
|
298
|
+ }
|
|
299
|
+
|
|
300
|
+ boolean useSpeaker = (mode == VIDEO_CALL);
|
|
301
|
+
|
|
302
|
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|
303
|
+ // On Android >= M we use the AudioDeviceCallback API, so turn on
|
|
304
|
+ // Bluetooth SCO from the start.
|
|
305
|
+ if (audioManager.isBluetoothScoAvailableOffCall()) {
|
|
306
|
+ audioManager.startBluetoothSco();
|
|
307
|
+ }
|
292
|
308
|
} else {
|
293
|
|
- audioManager.setBluetoothScoOn(false);
|
294
|
|
- audioManager.stopBluetoothSco();
|
|
309
|
+ // On older Android versions we must set the Bluetooth route
|
|
310
|
+ // manually. Also disable the speaker in that case.
|
|
311
|
+ setBluetoothAudioRoute(
|
|
312
|
+ bluetoothHeadsetMonitor.isHeadsetAvailable());
|
|
313
|
+ if (bluetoothHeadsetMonitor.isHeadsetAvailable()) {
|
|
314
|
+ useSpeaker = false;
|
|
315
|
+ }
|
295
|
316
|
}
|
296
|
|
- }
|
297
|
317
|
|
298
|
|
- /**
|
299
|
|
- * Helper method to trigger an audio route update when devices change. It makes sure the
|
300
|
|
- * operation is performed on the main thread.
|
301
|
|
- */
|
302
|
|
- void onAudioDeviceChange() {
|
303
|
|
- mainThreadHandler.post(mainThreadRunner);
|
|
318
|
+ // XXX: isWiredHeadsetOn is not deprecated when used just for knowing if
|
|
319
|
+ // there is a wired headset connected, regardless of audio being routed
|
|
320
|
+ // to it.
|
|
321
|
+ audioManager.setSpeakerphoneOn(
|
|
322
|
+ useSpeaker
|
|
323
|
+ && !(audioManager.isWiredHeadsetOn()
|
|
324
|
+ || audioManager.isBluetoothScoOn()));
|
|
325
|
+
|
|
326
|
+ return true;
|
304
|
327
|
}
|
305
|
328
|
}
|