Browse Source

Comply w/ coding style

- Maximum of 80 characters per line.
- Group first and then sort in alphabetical order.
- Fields should begin with a lowercase letter.
master
Lyubomir Marinov 8 years ago
parent
commit
ef39baab47

+ 1
- 1
android/app/src/main/AndroidManifest.xml View File

3
     android:versionCode="1"
3
     android:versionCode="1"
4
     android:versionName="1.0">
4
     android:versionName="1.0">
5
 
5
 
6
-    <!-- XXX: ACCESS_NETWORK_STATE is needed by react-native-webrtc -->
6
+    <!-- XXX: ACCESS_NETWORK_STATE is required by WebRTC. -->
7
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
7
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8
     <uses-permission android:name="android.permission.BLUETOOTH" />
8
     <uses-permission android:name="android.permission.BLUETOOTH" />
9
     <uses-permission android:name="android.permission.CAMERA" />
9
     <uses-permission android:name="android.permission.CAMERA" />

+ 140
- 117
android/app/src/main/java/org/jitsi/meet/audiomode/AudioModeModule.java View File

22
 import java.util.List;
22
 import java.util.List;
23
 import java.util.Map;
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
 public class AudioModeModule extends ReactContextBaseJavaModule {
40
 public class AudioModeModule extends ReactContextBaseJavaModule {
40
     /**
41
     /**
41
      * Constants representing the audio mode.
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
     private static final int DEFAULT    = 0;
50
     private static final int DEFAULT    = 0;
49
     private static final int AUDIO_CALL = 1;
51
     private static final int AUDIO_CALL = 1;
50
     private static final int VIDEO_CALL = 2;
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
      * React Native module name.
63
      * React Native module name.
54
      */
64
      */
60
     static final String TAG = MODULE_NAME;
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
      * {@link Handler} for running all operations on the main thread.
85
      * {@link Handler} for running all operations on the main thread.
80
     private final Runnable mainThreadRunner;
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
     public AudioModeModule(ReactApplicationContext reactContext) {
106
     public AudioModeModule(ReactApplicationContext reactContext) {
101
         super(reactContext);
107
         super(reactContext);
102
 
108
 
103
-        audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
109
+        audioManager
110
+            = (AudioManager)
111
+                reactContext.getSystemService(Context.AUDIO_SERVICE);
104
         mainThreadHandler = new Handler(Looper.getMainLooper());
112
         mainThreadHandler = new Handler(Looper.getMainLooper());
105
         mainThreadRunner = new Runnable() {
113
         mainThreadRunner = new Runnable() {
106
             @Override
114
             @Override
111
             }
119
             }
112
         };
120
         };
113
 
121
 
114
-        // Setup runtime device change detection
122
+        // Setup runtime device change detection.
115
         setupAudioRouteChangeDetection();
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
      * Gets a mapping with the constants this module is exporting.
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
     @Override
132
     @Override
134
     public Map<String, Object> getConstants() {
133
     public Map<String, Object> getConstants() {
135
         Map<String, Object> constants = new HashMap<>();
134
         Map<String, Object> constants = new HashMap<>();
136
-        constants.put("DEFAULT", DEFAULT);
135
+
137
         constants.put("AUDIO_CALL", AUDIO_CALL);
136
         constants.put("AUDIO_CALL", AUDIO_CALL);
137
+        constants.put("DEFAULT", DEFAULT);
138
         constants.put("VIDEO_CALL", VIDEO_CALL);
138
         constants.put("VIDEO_CALL", VIDEO_CALL);
139
+
139
         return constants;
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
         } else {
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
      * Public method to set the current audio mode.
177
      * Public method to set the current audio mode.
200
      *
178
      *
201
      * @param mode the desired audio mode.
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
     @ReactMethod
183
     @ReactMethod
206
     public void setMode(final int mode, final Promise promise) {
184
     public void setMode(final int mode, final Promise promise) {
216
                     AudioModeModule.this.mode = mode;
194
                     AudioModeModule.this.mode = mode;
217
                     promise.resolve(null);
195
                     promise.resolve(null);
218
                 } else {
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
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
211
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
232
             setupAudioRouteChangeDetectionM();
212
             setupAudioRouteChangeDetectionM();
233
         } else {
213
         } else {
234
-            setupAudioRouteChangeDetectionOld();
214
+            setupAudioRouteChangeDetectionPreM();
235
         }
215
         }
236
     }
216
     }
237
 
217
 
243
         android.media.AudioDeviceCallback audioDeviceCallback =
223
         android.media.AudioDeviceCallback audioDeviceCallback =
244
                 new android.media.AudioDeviceCallback() {
224
                 new android.media.AudioDeviceCallback() {
245
                     @Override
225
                     @Override
246
-                    public void onAudioDevicesAdded(AudioDeviceInfo[] addedDevices) {
226
+                    public void onAudioDevicesAdded(
227
+                            AudioDeviceInfo[] addedDevices) {
247
                         Log.d(TAG, "Audio devices added");
228
                         Log.d(TAG, "Audio devices added");
248
                         onAudioDeviceChange();
229
                         onAudioDeviceChange();
249
                     }
230
                     }
250
 
231
 
251
                     @Override
232
                     @Override
252
-                    public void onAudioDevicesRemoved(AudioDeviceInfo[] removedDevices) {
233
+                    public void onAudioDevicesRemoved(
234
+                            AudioDeviceInfo[] removedDevices) {
253
                         Log.d(TAG, "Audio devices removed");
235
                         Log.d(TAG, "Audio devices removed");
254
                         onAudioDeviceChange();
236
                         onAudioDeviceChange();
255
                     }
237
                     }
261
     /**
243
     /**
262
      * Audio route change detection mechanism for Android API < 23.
244
      * Audio route change detection mechanism for Android API < 23.
263
      */
245
      */
264
-    private void setupAudioRouteChangeDetectionOld() {
246
+    private void setupAudioRouteChangeDetectionPreM() {
265
         ReactContext reactContext = getReactApplicationContext();
247
         ReactContext reactContext = getReactApplicationContext();
266
 
248
 
267
-        // Detect changes in wired headset connections
249
+        // Detect changes in wired headset connections.
268
         IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);
250
         IntentFilter wiredHeadSetFilter = new IntentFilter(ACTION_HEADSET_PLUG);
269
         BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
251
         BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
270
             @Override
252
             @Override
275
         };
257
         };
276
         reactContext.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);
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
         bluetoothHeadsetMonitor.start();
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
         } else {
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
 }

+ 16
- 9
android/app/src/main/java/org/jitsi/meet/audiomode/AudioModePackage.java View File

10
 import java.util.Collections;
10
 import java.util.Collections;
11
 import java.util.List;
11
 import java.util.List;
12
 
12
 
13
+/**
14
+ * Implements {@link ReactPackage} for {@link AudioModeModule}.
15
+ */
13
 public class AudioModePackage implements ReactPackage {
16
 public class AudioModePackage implements ReactPackage {
14
     /**
17
     /**
15
      * {@inheritDoc}
18
      * {@inheritDoc}
16
-     * @return List of native modules to be exposed by React Native.
17
      */
19
      */
18
     @Override
20
     @Override
19
-    public List<NativeModule> createNativeModules(
20
-                                ReactApplicationContext reactContext) {
21
-        List<NativeModule> modules = new ArrayList<>();
22
-        modules.add(new AudioModeModule(reactContext));
23
-        return modules;
21
+    public List<Class<? extends JavaScriptModule>> createJSModules() {
22
+        return Collections.emptyList();
24
     }
23
     }
25
 
24
 
26
     /**
25
     /**
27
      * {@inheritDoc}
26
      * {@inheritDoc}
27
+     *
28
+     * @return List of native modules to be exposed by React Native.
28
      */
29
      */
29
     @Override
30
     @Override
30
-    public List<Class<? extends JavaScriptModule>> createJSModules() {
31
-        return Collections.emptyList();
31
+    public List<NativeModule> createNativeModules(
32
+            ReactApplicationContext reactContext) {
33
+        List<NativeModule> modules = new ArrayList<>();
34
+
35
+        modules.add(new AudioModeModule(reactContext));
36
+
37
+        return modules;
32
     }
38
     }
33
 
39
 
34
     /**
40
     /**
35
      * {@inheritDoc}
41
      * {@inheritDoc}
36
      */
42
      */
37
     @Override
43
     @Override
38
-    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
44
+    public List<ViewManager> createViewManagers(
45
+            ReactApplicationContext reactContext) {
39
         return Collections.emptyList();
46
         return Collections.emptyList();
40
     }
47
     }
41
 }
48
 }

+ 78
- 52
android/app/src/main/java/org/jitsi/meet/audiomode/BluetoothHeadsetMonitor.java View File

18
 import java.util.List;
18
 import java.util.List;
19
 
19
 
20
 /**
20
 /**
21
- * Helper class to detect and handle Bluetooth device changes.  It monitors Bluetooth headsets
22
- * being connected / disconnected and notifies the module about device changes when this occurs.
21
+ * Helper class to detect and handle Bluetooth device changes.  It monitors
22
+ * Bluetooth headsets being connected / disconnected and notifies the module
23
+ * about device changes when this occurs.
23
  */
24
  */
24
 public class BluetoothHeadsetMonitor {
25
 public class BluetoothHeadsetMonitor {
25
     /**
26
     /**
26
-     * {@link AudioModeModule} where this monitor reports.
27
-     */
28
-    private final AudioModeModule AudioModeModule;
29
-
30
-    /**
31
-     * {@link AudioManager} instance used to interact with the Android audio subsystem.
27
+     * {@link AudioManager} instance used to interact with the Android audio
28
+     * subsystem.
32
      */
29
      */
33
     private final AudioManager audioManager;
30
     private final AudioManager audioManager;
34
 
31
 
35
     /**
32
     /**
36
-     * {@link ReactContext} instance where the main module runs.
33
+     * {@link AudioModeModule} where this monitor reports.
37
      */
34
      */
38
-    private final ReactContext reactContext;
35
+    private final AudioModeModule audioModeModule;
39
 
36
 
40
     /**
37
     /**
41
-     * Reference to the Bluetooth adapter, needed for managing <tt>BluetoothProfile.HEADSET</tt>
42
-     * devices.
38
+     * Reference to the Bluetooth adapter, needed for managing
39
+     * <tt>BluetoothProfile.HEADSET</tt> devices.
43
      */
40
      */
44
     private BluetoothAdapter bluetoothAdapter;
41
     private BluetoothAdapter bluetoothAdapter;
45
 
42
 
49
     private BluetoothHeadset bluetoothHeadset;
46
     private BluetoothHeadset bluetoothHeadset;
50
 
47
 
51
     /**
48
     /**
52
-     * Listener for Bluetooth service profiles, allows us to get the proxy object to
53
-     * {@link BluetoothHeadset}.
49
+     * Listener for Bluetooth service profiles, allows us to get the proxy
50
+     * object to {@link BluetoothHeadset}.
54
      */
51
      */
55
     private BluetoothProfile.ServiceListener bluetoothProfileListener;
52
     private BluetoothProfile.ServiceListener bluetoothProfileListener;
56
 
53
 
57
-    /**
58
-     * {@link Handler} for running all operations on the main thread.
59
-     */
60
-    private final Handler mainThreadHandler;
61
-
62
     /**
54
     /**
63
      * Helper for running Bluetooth operations on the main thread.
55
      * Helper for running Bluetooth operations on the main thread.
64
      */
56
      */
65
     private Runnable bluetoothRunnable;
57
     private Runnable bluetoothRunnable;
66
 
58
 
67
     /**
59
     /**
68
-     * Flag indicating if there are any Bluetooth headset devices currently available.
60
+     * Flag indicating if there are any Bluetooth headset devices currently
61
+     * available.
69
      */
62
      */
70
     private boolean headsetAvailable = false;
63
     private boolean headsetAvailable = false;
71
 
64
 
72
-    public BluetoothHeadsetMonitor(AudioModeModule audioModeModule, ReactContext reactContext) {
73
-        this.AudioModeModule = audioModeModule;
65
+    /**
66
+     * {@link Handler} for running all operations on the main thread.
67
+     */
68
+    private final Handler mainThreadHandler;
69
+
70
+    /**
71
+     * {@link ReactContext} instance where the main module runs.
72
+     */
73
+    private final ReactContext reactContext;
74
+
75
+    public BluetoothHeadsetMonitor(
76
+            AudioModeModule audioModeModule,
77
+            ReactContext reactContext) {
78
+        this.audioModeModule = audioModeModule;
74
         this.reactContext = reactContext;
79
         this.reactContext = reactContext;
75
 
80
 
76
-        audioManager = ((AudioManager) reactContext.getSystemService(Context.AUDIO_SERVICE));
81
+        audioManager
82
+            = (AudioManager)
83
+                reactContext.getSystemService(Context.AUDIO_SERVICE);
77
         bluetoothAdapter = null;
84
         bluetoothAdapter = null;
78
         bluetoothHeadset = null;
85
         bluetoothHeadset = null;
79
         bluetoothProfileListener = null;
86
         bluetoothProfileListener = null;
80
         mainThreadHandler = new Handler(Looper.getMainLooper());
87
         mainThreadHandler = new Handler(Looper.getMainLooper());
81
     }
88
     }
82
 
89
 
90
+    /**
91
+     * Returns the current headset availability.
92
+     *
93
+     * @return true if there is a Bluetooth headset connected, false otherwise.
94
+     */
95
+    public boolean isHeadsetAvailable() {
96
+        return headsetAvailable;
97
+    }
98
+
83
     /**
99
     /**
84
      * Start monitoring Bluetooth device activity.
100
      * Start monitoring Bluetooth device activity.
85
      */
101
      */
90
                 if (bluetoothHeadset == null) {
106
                 if (bluetoothHeadset == null) {
91
                     headsetAvailable = false;
107
                     headsetAvailable = false;
92
                 } else {
108
                 } else {
93
-                    List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
109
+                    List<BluetoothDevice> devices
110
+                        = bluetoothHeadset.getConnectedDevices();
94
                     headsetAvailable = !devices.isEmpty();
111
                     headsetAvailable = !devices.isEmpty();
95
                 }
112
                 }
96
-                BluetoothHeadsetMonitor.this.AudioModeModule.onAudioDeviceChange();
113
+                audioModeModule.onAudioDeviceChange();
97
             }
114
             }
98
         };
115
         };
99
 
116
 
108
             return;
125
             return;
109
         }
126
         }
110
 
127
 
111
-        // XXX: The profile listener listens for system services of the given type being available
112
-        // to the application.  That is, if our Bluetooth adapter has the "headset" profile.
128
+        // XXX: The profile listener listens for system services of the given
129
+        // type being available to the application. That is, if our Bluetooth
130
+        // adapter has the "headset" profile.
113
         bluetoothProfileListener = new BluetoothProfile.ServiceListener() {
131
         bluetoothProfileListener = new BluetoothProfile.ServiceListener() {
114
             @Override
132
             @Override
115
-            public void onServiceConnected(int profile, BluetoothProfile proxy) {
133
+            public void onServiceConnected(
134
+                    int profile,
135
+                    BluetoothProfile proxy) {
116
                 if (profile == BluetoothProfile.HEADSET) {
136
                 if (profile == BluetoothProfile.HEADSET) {
117
                     bluetoothHeadset = (BluetoothHeadset) proxy;
137
                     bluetoothHeadset = (BluetoothHeadset) proxy;
118
                     updateDevices();
138
                     updateDevices();
133
 
153
 
134
         IntentFilter bluetoothFilter = new IntentFilter();
154
         IntentFilter bluetoothFilter = new IntentFilter();
135
         bluetoothFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
155
         bluetoothFilter.addAction(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED);
136
-        bluetoothFilter.addAction(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
156
+        bluetoothFilter.addAction(
157
+                BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED);
137
         BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
158
         BroadcastReceiver bluetoothReceiver = new BroadcastReceiver() {
138
             @Override
159
             @Override
139
             public void onReceive(Context context, Intent intent) {
160
             public void onReceive(Context context, Intent intent) {
140
                 final String action = intent.getAction();
161
                 final String action = intent.getAction();
141
-                if (action.equals(BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
142
-                    // XXX: This action will be fired when a Bluetooth headset is connected or
143
-                    // disconnected to the system.  This is not related to audio routing.
144
-                    final int state = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -99);
162
+                if (action.equals(
163
+                        BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED)) {
164
+                    // XXX: This action will be fired when a Bluetooth headset
165
+                    // is connected or disconnected to the system. This is not
166
+                    // related to audio routing.
167
+                    final int state
168
+                        = intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, -99);
145
                     switch (state) {
169
                     switch (state) {
146
                     case BluetoothHeadset.STATE_CONNECTED:
170
                     case BluetoothHeadset.STATE_CONNECTED:
147
                     case BluetoothHeadset.STATE_DISCONNECTED:
171
                     case BluetoothHeadset.STATE_DISCONNECTED:
148
-                        Log.d(AudioModeModule.TAG, "BT headset connection state changed: " + state);
172
+                        Log.d(
173
+                                AudioModeModule.TAG,
174
+                                "BT headset connection state changed: "
175
+                                    + state);
149
                         updateDevices();
176
                         updateDevices();
150
                         break;
177
                         break;
151
                     default:
178
                     default:
152
                         break;
179
                         break;
153
                     }
180
                     }
154
-                } else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
155
-                    // XXX: This action will be fired when the connection established with a
156
-                    // Bluetooth headset (called a SCO connection) changes state.  When the SCO
157
-                    // connection is active we route audio to it.
158
-                    final int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, -99);
181
+                } else if (action.equals(
182
+                        AudioManager.ACTION_SCO_AUDIO_STATE_UPDATED)) {
183
+                    // XXX: This action will be fired when the connection
184
+                    // established with a Bluetooth headset (called a SCO
185
+                    // connection) changes state.  When the SCO connection is
186
+                    // active we route audio to it.
187
+                    final int state
188
+                        = intent.getIntExtra(
189
+                                AudioManager.EXTRA_SCO_AUDIO_STATE,
190
+                                -99);
159
                     switch (state) {
191
                     switch (state) {
160
                     case AudioManager.SCO_AUDIO_STATE_CONNECTED:
192
                     case AudioManager.SCO_AUDIO_STATE_CONNECTED:
161
                     case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
193
                     case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
162
-                        Log.d(AudioModeModule.TAG, "BT SCO connection state changed: " + state);
194
+                        Log.d(
195
+                                AudioModeModule.TAG,
196
+                                "BT SCO connection state changed: " + state);
163
                         updateDevices();
197
                         updateDevices();
164
                         break;
198
                         break;
165
                     default:
199
                     default:
170
         };
204
         };
171
         reactContext.registerReceiver(bluetoothReceiver, bluetoothFilter);
205
         reactContext.registerReceiver(bluetoothReceiver, bluetoothFilter);
172
 
206
 
173
-        // Initial detection
207
+        // Initial detection.
174
         updateDevices();
208
         updateDevices();
175
     }
209
     }
176
 
210
 
177
     /**
211
     /**
178
-     * Detect if there are new devices connected / disconnected and fires the
179
-     * <tt>onAudioDeviceChange</tt> callback.
212
+     * Detects if there are new devices connected / disconnected and fires the
213
+     * {@link AudioModeModule#onAudioDeviceChange()} callback.
180
      */
214
      */
181
     private void updateDevices() {
215
     private void updateDevices() {
182
         mainThreadHandler.post(bluetoothRunnable);
216
         mainThreadHandler.post(bluetoothRunnable);
183
     }
217
     }
184
-
185
-    /**
186
-     * Returns the current headset availability.
187
-     * @return true if there is a Bluetooth headset connected, false otherwise.
188
-     */
189
-    public boolean isHeadsetAvailable() {
190
-        return headsetAvailable;
191
-    }
192
-}
218
+}

Loading…
Cancel
Save