|
@@ -1,230 +0,0 @@
|
1
|
|
-/*
|
2
|
|
- * Copyright @ 2017-present 8x8, Inc.
|
3
|
|
- *
|
4
|
|
- * Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
|
- * you may not use this file except in compliance with the License.
|
6
|
|
- * You may obtain a copy of the License at
|
7
|
|
- *
|
8
|
|
- * http://www.apache.org/licenses/LICENSE-2.0
|
9
|
|
- *
|
10
|
|
- * Unless required by applicable law or agreed to in writing, software
|
11
|
|
- * distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
|
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
|
- * See the License for the specific language governing permissions and
|
14
|
|
- * limitations under the License.
|
15
|
|
- */
|
16
|
|
-
|
17
|
|
-package org.jitsi.meet.sdk;
|
18
|
|
-
|
19
|
|
-import android.content.BroadcastReceiver;
|
20
|
|
-import android.content.Context;
|
21
|
|
-import android.content.Intent;
|
22
|
|
-import android.content.IntentFilter;
|
23
|
|
-import android.content.pm.PackageManager;
|
24
|
|
-import android.media.AudioManager;
|
25
|
|
-
|
26
|
|
-import org.jitsi.meet.sdk.log.JitsiMeetLogger;
|
27
|
|
-
|
28
|
|
-
|
29
|
|
-/**
|
30
|
|
- * {@link AudioModeModule.AudioDeviceHandlerInterface} module implementing device handling for
|
31
|
|
- * legacy (pre-M) Android versions.
|
32
|
|
- */
|
33
|
|
-class AudioDeviceHandlerLegacy implements
|
34
|
|
- AudioModeModule.AudioDeviceHandlerInterface,
|
35
|
|
- AudioManager.OnAudioFocusChangeListener,
|
36
|
|
- BluetoothHeadsetMonitor.Listener {
|
37
|
|
-
|
38
|
|
- private final static String TAG = AudioDeviceHandlerLegacy.class.getSimpleName();
|
39
|
|
-
|
40
|
|
- /**
|
41
|
|
- * Reference to the main {@code AudioModeModule}.
|
42
|
|
- */
|
43
|
|
- private AudioModeModule module;
|
44
|
|
-
|
45
|
|
- /**
|
46
|
|
- * Indicator that we have lost audio focus.
|
47
|
|
- */
|
48
|
|
- private boolean audioFocusLost = false;
|
49
|
|
-
|
50
|
|
- /**
|
51
|
|
- * {@link AudioManager} instance used to interact with the Android audio
|
52
|
|
- * subsystem.
|
53
|
|
- */
|
54
|
|
- private AudioManager audioManager;
|
55
|
|
-
|
56
|
|
- /**
|
57
|
|
- * {@link BluetoothHeadsetMonitor} for detecting Bluetooth device changes in
|
58
|
|
- * old (< M) Android versions.
|
59
|
|
- */
|
60
|
|
- private BluetoothHeadsetMonitor bluetoothHeadsetMonitor;
|
61
|
|
-
|
62
|
|
- public AudioDeviceHandlerLegacy(AudioManager audioManager) {
|
63
|
|
- this.audioManager = audioManager;
|
64
|
|
- }
|
65
|
|
-
|
66
|
|
- /**
|
67
|
|
- * Helper method to trigger an audio route update when Bluetooth devices are
|
68
|
|
- * connected / disconnected.
|
69
|
|
- */
|
70
|
|
- @Override
|
71
|
|
- public void onBluetoothDeviceChange(final boolean deviceAvailable) {
|
72
|
|
- module.runInAudioThread(new Runnable() {
|
73
|
|
- @Override
|
74
|
|
- public void run() {
|
75
|
|
- if (deviceAvailable) {
|
76
|
|
- module.addDevice(AudioModeModule.DEVICE_BLUETOOTH);
|
77
|
|
- } else {
|
78
|
|
- module.removeDevice(AudioModeModule.DEVICE_BLUETOOTH);
|
79
|
|
- }
|
80
|
|
-
|
81
|
|
- module.updateAudioRoute();
|
82
|
|
- }
|
83
|
|
- });
|
84
|
|
- }
|
85
|
|
-
|
86
|
|
- /**
|
87
|
|
- * Helper method to trigger an audio route update when a headset is plugged
|
88
|
|
- * or unplugged.
|
89
|
|
- */
|
90
|
|
- private void onHeadsetDeviceChange() {
|
91
|
|
- module.runInAudioThread(new Runnable() {
|
92
|
|
- @Override
|
93
|
|
- public void run() {
|
94
|
|
- // XXX: isWiredHeadsetOn is not deprecated when used just for
|
95
|
|
- // knowing if there is a wired headset connected, regardless of
|
96
|
|
- // audio being routed to it.
|
97
|
|
- //noinspection deprecation
|
98
|
|
- if (audioManager.isWiredHeadsetOn()) {
|
99
|
|
- module.addDevice(AudioModeModule.DEVICE_HEADPHONES);
|
100
|
|
- } else {
|
101
|
|
- module.removeDevice(AudioModeModule.DEVICE_HEADPHONES);
|
102
|
|
- }
|
103
|
|
-
|
104
|
|
- module.updateAudioRoute();
|
105
|
|
- }
|
106
|
|
- });
|
107
|
|
- }
|
108
|
|
-
|
109
|
|
- /**
|
110
|
|
- * {@link AudioManager.OnAudioFocusChangeListener} interface method. Called
|
111
|
|
- * when the audio focus of the system is updated.
|
112
|
|
- *
|
113
|
|
- * @param focusChange - The type of focus change.
|
114
|
|
- */
|
115
|
|
- @Override
|
116
|
|
- public void onAudioFocusChange(final int focusChange) {
|
117
|
|
- module.runInAudioThread(new Runnable() {
|
118
|
|
- @Override
|
119
|
|
- public void run() {
|
120
|
|
- switch (focusChange) {
|
121
|
|
- case AudioManager.AUDIOFOCUS_GAIN: {
|
122
|
|
- JitsiMeetLogger.d(TAG + " Audio focus gained");
|
123
|
|
- // Some other application potentially stole our audio focus
|
124
|
|
- // temporarily. Restore our mode.
|
125
|
|
- if (audioFocusLost) {
|
126
|
|
- module.updateAudioRoute();
|
127
|
|
- }
|
128
|
|
- audioFocusLost = false;
|
129
|
|
- break;
|
130
|
|
- }
|
131
|
|
- case AudioManager.AUDIOFOCUS_LOSS:
|
132
|
|
- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
|
133
|
|
- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: {
|
134
|
|
- JitsiMeetLogger.d(TAG + " Audio focus lost");
|
135
|
|
- audioFocusLost = true;
|
136
|
|
- break;
|
137
|
|
- }
|
138
|
|
- }
|
139
|
|
- }
|
140
|
|
- });
|
141
|
|
- }
|
142
|
|
-
|
143
|
|
- /**
|
144
|
|
- * Helper method to set the output route to a Bluetooth device.
|
145
|
|
- *
|
146
|
|
- * @param enabled true if Bluetooth should use used, false otherwise.
|
147
|
|
- */
|
148
|
|
- private void setBluetoothAudioRoute(boolean enabled) {
|
149
|
|
- if (enabled) {
|
150
|
|
- audioManager.startBluetoothSco();
|
151
|
|
- audioManager.setBluetoothScoOn(true);
|
152
|
|
- } else {
|
153
|
|
- audioManager.setBluetoothScoOn(false);
|
154
|
|
- audioManager.stopBluetoothSco();
|
155
|
|
- }
|
156
|
|
- }
|
157
|
|
-
|
158
|
|
- @Override
|
159
|
|
- public void start(AudioModeModule audioModeModule) {
|
160
|
|
- JitsiMeetLogger.i("Using " + TAG + " as the audio device handler");
|
161
|
|
-
|
162
|
|
- module = audioModeModule;
|
163
|
|
- Context context = module.getContext();
|
164
|
|
-
|
165
|
|
- // Setup runtime device change detection.
|
166
|
|
- //
|
167
|
|
-
|
168
|
|
- // Detect changes in wired headset connections.
|
169
|
|
- IntentFilter wiredHeadSetFilter = new IntentFilter(AudioManager.ACTION_HEADSET_PLUG);
|
170
|
|
- BroadcastReceiver wiredHeadsetReceiver = new BroadcastReceiver() {
|
171
|
|
- @Override
|
172
|
|
- public void onReceive(Context context, Intent intent) {
|
173
|
|
- JitsiMeetLogger.d(TAG + " Wired headset added / removed");
|
174
|
|
- onHeadsetDeviceChange();
|
175
|
|
- }
|
176
|
|
- };
|
177
|
|
- context.registerReceiver(wiredHeadsetReceiver, wiredHeadSetFilter);
|
178
|
|
-
|
179
|
|
- // Detect Bluetooth device changes.
|
180
|
|
- bluetoothHeadsetMonitor = new BluetoothHeadsetMonitor(context, this);
|
181
|
|
-
|
182
|
|
- // On Android < M, detect if we have an earpiece.
|
183
|
|
- PackageManager pm = context.getPackageManager();
|
184
|
|
- if (pm.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
|
185
|
|
- module.addDevice(AudioModeModule.DEVICE_EARPIECE);
|
186
|
|
- }
|
187
|
|
-
|
188
|
|
- // Always assume there is a speaker.
|
189
|
|
- module.addDevice(AudioModeModule.DEVICE_SPEAKER);
|
190
|
|
- }
|
191
|
|
-
|
192
|
|
- @Override
|
193
|
|
- public void stop() {
|
194
|
|
- bluetoothHeadsetMonitor.stop();
|
195
|
|
- bluetoothHeadsetMonitor = null;
|
196
|
|
- }
|
197
|
|
-
|
198
|
|
- @Override
|
199
|
|
- public void setAudioRoute(String device) {
|
200
|
|
- // Turn speaker on / off
|
201
|
|
- audioManager.setSpeakerphoneOn(device.equals(AudioModeModule.DEVICE_SPEAKER));
|
202
|
|
-
|
203
|
|
- // Turn bluetooth on / off
|
204
|
|
- setBluetoothAudioRoute(device.equals(AudioModeModule.DEVICE_BLUETOOTH));
|
205
|
|
- }
|
206
|
|
-
|
207
|
|
- @Override
|
208
|
|
- public boolean setMode(int mode) {
|
209
|
|
- if (mode == AudioModeModule.DEFAULT) {
|
210
|
|
- audioFocusLost = false;
|
211
|
|
- audioManager.setMode(AudioManager.MODE_NORMAL);
|
212
|
|
- audioManager.abandonAudioFocus(this);
|
213
|
|
- audioManager.setSpeakerphoneOn(false);
|
214
|
|
- setBluetoothAudioRoute(false);
|
215
|
|
-
|
216
|
|
- return true;
|
217
|
|
- }
|
218
|
|
-
|
219
|
|
- audioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
|
220
|
|
- audioManager.setMicrophoneMute(false);
|
221
|
|
-
|
222
|
|
- if (audioManager.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN)
|
223
|
|
- == AudioManager.AUDIOFOCUS_REQUEST_FAILED) {
|
224
|
|
- JitsiMeetLogger.w(TAG + " Audio focus request failed");
|
225
|
|
- return false;
|
226
|
|
- }
|
227
|
|
-
|
228
|
|
- return true;
|
229
|
|
- }
|
230
|
|
-}
|