下面列出了android.media.AudioManager#setBluetoothScoOn ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void setWantsConnection(boolean enabled) {
synchronized (LOCK) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
this.wantsConnection = enabled;
if (wantsConnection && isBluetoothAvailable() && scoConnection == ScoConnection.DISCONNECTED) {
audioManager.startBluetoothSco();
scoConnection = ScoConnection.IN_PROGRESS;
} else if (!wantsConnection && scoConnection == ScoConnection.CONNECTED) {
audioManager.stopBluetoothSco();
audioManager.setBluetoothScoOn(false);
scoConnection = ScoConnection.DISCONNECTED;
} else if (!wantsConnection && scoConnection == ScoConnection.IN_PROGRESS) {
audioManager.stopBluetoothSco();
scoConnection = ScoConnection.DISCONNECTED;
}
}
}
public void stop(boolean playDisconnected) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
incomingRinger.stop();
outgoingRinger.stop();
if (playDisconnected) {
soundPool.play(disconnectedSoundId, 1.0f, 1.0f, 0, 0, 1.0f);
}
if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
}
audioManager.setSpeakerphoneOn(false);
audioManager.setMicrophoneMute(false);
audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.abandonAudioFocus(null);
}
@Override
public void onNotifySCOAudioStateChange(int scoAudioState) {
switch (scoAudioState) {
case AudioManager.SCO_AUDIO_STATE_CONNECTED:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
am.setBluetoothScoOn(true);
}
break;
case AudioManager.SCO_AUDIO_STATE_DISCONNECTED:
Log.d("onNotifyHeadsetState",
"onNotifySCOAudioStateChange: " + headsetPlugReceiver.isBluetoothConnected());
if (headsetPlugReceiver.isBluetoothConnected()) {
startBluetoothSco();
}
break;
}
}
@Override
public void execute(AudioManager audioManager) {
if (voiceMediator.isBlueToothHeadSet()) {
if (!voiceMediator.isSuportA2DP()) {
if (audioManager.getMode() != AudioManager.MODE_NORMAL) {
Log.e(TAG, "playInChannel>>setMode(AudioManager.MODE_NORMAL)");
audioManager.setMode(AudioManager.MODE_NORMAL);
}
if (audioManager.isBluetoothScoOn()) {
audioManager.setBluetoothScoOn(false);
audioManager.stopBluetoothSco();
}
} else {
if (!audioManager.isBluetoothA2dpOn()) {
Log.e(TAG, "playInChannel>>setBluetoothA2dpOn(true)");
audioManager.setBluetoothA2dpOn(true);
}
}
}
}
public boolean streamAudioStart(Context context) {
try{
AudioManager localAudioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
if( localAudioManager == null ){ return false; }
localAudioManager.setMode(0);
localAudioManager.setBluetoothScoOn(true);
localAudioManager.startBluetoothSco();
localAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
mAudioStatus = true;
}catch( Exception ex){
System.out.println("Exception on Start " + ex.getMessage() );
mAudioStatus = false;
return false;
}
return mAudioStatus;
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) return;
Log.i(TAG, "onReceive");
synchronized (LOCK) {
if (getScoChangeIntent().equals(intent.getAction())) {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
if (bluetoothHeadset != null) {
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
for (BluetoothDevice device : devices) {
if (bluetoothHeadset.isAudioConnected(device)) {
int deviceClass = device.getBluetoothClass().getDeviceClass();
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
{
scoConnection = ScoConnection.CONNECTED;
if (wantsConnection) {
AudioManager audioManager = ServiceUtil.getAudioManager(context);
audioManager.setBluetoothScoOn(true);
}
}
}
}
}
}
}
}
handleBluetoothStateChange();
}
private void setUseFrontSpeaker(boolean value) {
useFrontSpeaker = value;
AudioManager audioManager = NotificationsController.audioManager;
if (useFrontSpeaker) {
audioManager.setBluetoothScoOn(false);
audioManager.setSpeakerphoneOn(false);
} else {
audioManager.setSpeakerphoneOn(true);
}
}
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
return;
}
Log.w(TAG, "onReceive");
synchronized (LOCK) {
if (getScoChangeIntent().equals(intent.getAction())) {
int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);
if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED && bluetoothHeadset != null) {
List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();
for (BluetoothDevice device : devices) {
if (bluetoothHeadset.isAudioConnected(device)) {
int deviceClass = device.getBluetoothClass().getDeviceClass();
if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
scoConnection = ScoConnection.CONNECTED;
if (wantsConnection) {
AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
audioManager.setBluetoothScoOn(true);
}
}
}
}
}
}
}
handleBluetoothStateChange();
}
protected void updateBluetoothHeadsetState(boolean connected){
if(connected==isBtHeadsetConnected)
return;
if(BuildVars.LOGS_ENABLED)
FileLog.d("updateBluetoothHeadsetState: "+connected);
isBtHeadsetConnected=connected;
final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
if(connected && !isRinging() && currentState!=0){
if(bluetoothScoActive){
if(BuildVars.LOGS_ENABLED)
FileLog.d("SCO already active, setting audio routing");
am.setSpeakerphoneOn(false);
am.setBluetoothScoOn(true);
}else{
if(BuildVars.LOGS_ENABLED)
FileLog.d("startBluetoothSco");
needSwitchToBluetoothAfterScoActivates=true;
// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}
}, 500);
}
}else{
bluetoothScoActive=false;
}
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
/**
* 设置是否使用蓝牙 SCO 耳机进行通讯
* @param on {@code true} yes, {@code false} no
* @return {@code true} success, {@code false} fail
*/
public static boolean setBluetoothScoOn(final boolean on) {
AudioManager audioManager = AppUtils.getAudioManager();
if (audioManager != null) {
try {
audioManager.setBluetoothScoOn(on);
return true;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "setBluetoothScoOn");
}
}
return false;
}
protected void updateBluetoothHeadsetState(boolean connected){
if(connected==isBtHeadsetConnected)
return;
if(BuildVars.LOGS_ENABLED)
FileLog.d("updateBluetoothHeadsetState: "+connected);
isBtHeadsetConnected=connected;
final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
if(connected && !isRinging() && currentState!=0){
if(bluetoothScoActive){
if(BuildVars.LOGS_ENABLED)
FileLog.d("SCO already active, setting audio routing");
am.setSpeakerphoneOn(false);
am.setBluetoothScoOn(true);
}else{
if(BuildVars.LOGS_ENABLED)
FileLog.d("startBluetoothSco");
needSwitchToBluetoothAfterScoActivates=true;
// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}
}, 500);
}
}else{
bluetoothScoActive=false;
}
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
protected void updateBluetoothHeadsetState(boolean connected){
if(connected==isBtHeadsetConnected)
return;
if(BuildVars.LOGS_ENABLED)
FileLog.d("updateBluetoothHeadsetState: "+connected);
isBtHeadsetConnected=connected;
final AudioManager am=(AudioManager)getSystemService(AUDIO_SERVICE);
if(connected && !isRinging() && currentState!=0){
if(bluetoothScoActive){
if(BuildVars.LOGS_ENABLED)
FileLog.d("SCO already active, setting audio routing");
am.setSpeakerphoneOn(false);
am.setBluetoothScoOn(true);
}else{
if(BuildVars.LOGS_ENABLED)
FileLog.d("startBluetoothSco");
needSwitchToBluetoothAfterScoActivates=true;
// some devices ignore startBluetoothSco when called immediately after the headset is connected, so delay it
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}
}, 500);
}
}else{
bluetoothScoActive=false;
}
for (StateListener l : stateListeners)
l.onAudioSettingsChanged();
}
private void setUseFrontSpeaker(boolean value) {
useFrontSpeaker = value;
AudioManager audioManager = NotificationsController.audioManager;
if (useFrontSpeaker) {
audioManager.setBluetoothScoOn(false);
audioManager.setSpeakerphoneOn(false);
} else {
audioManager.setSpeakerphoneOn(true);
}
}
protected void configureDeviceForCall() {
needPlayEndSound = true;
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
if(!bluetoothScoActive){
needSwitchToBluetoothAfterScoActivates=true;
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}else{
am.setBluetoothScoOn(true);
am.setSpeakerphoneOn(false);
}
break;
case AUDIO_ROUTE_EARPIECE:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
break;
case AUDIO_ROUTE_SPEAKER:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(true);
break;
}
}else if(isBluetoothHeadsetConnected()){
am.setBluetoothScoOn(speakerphoneStateToSet);
}else{
am.setSpeakerphoneOn(speakerphoneStateToSet);
}
}/*else{
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
break;
case AUDIO_ROUTE_EARPIECE:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
break;
case AUDIO_ROUTE_SPEAKER:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
break;
}
}else{
if(hasEarpiece())
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
else
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
}
}*/
updateOutputGainControlState();
audioConfigured=true;
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
try{
if(proximity!=null){
proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
}
}catch(Exception x){
if (BuildVars.LOGS_ENABLED) {
FileLog.e("Error initializing proximity sensor", x);
}
}
}
protected void configureDeviceForCall() {
needPlayEndSound = true;
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
if(!bluetoothScoActive){
needSwitchToBluetoothAfterScoActivates=true;
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}else{
am.setBluetoothScoOn(true);
am.setSpeakerphoneOn(false);
}
break;
case AUDIO_ROUTE_EARPIECE:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
break;
case AUDIO_ROUTE_SPEAKER:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(true);
break;
}
}else if(isBluetoothHeadsetConnected()){
am.setBluetoothScoOn(speakerphoneStateToSet);
}else{
am.setSpeakerphoneOn(speakerphoneStateToSet);
}
}/*else{
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
break;
case AUDIO_ROUTE_EARPIECE:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
break;
case AUDIO_ROUTE_SPEAKER:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
break;
}
}else{
if(hasEarpiece())
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
else
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
}
}*/
updateOutputGainControlState();
audioConfigured=true;
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
try{
if(proximity!=null){
proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
}
}catch(Exception x){
if (BuildVars.LOGS_ENABLED) {
FileLog.e("Error initializing proximity sensor", x);
}
}
}
protected void configureDeviceForCall() {
needPlayEndSound = true;
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
if(!bluetoothScoActive){
needSwitchToBluetoothAfterScoActivates=true;
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}else{
am.setBluetoothScoOn(true);
am.setSpeakerphoneOn(false);
}
break;
case AUDIO_ROUTE_EARPIECE:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
break;
case AUDIO_ROUTE_SPEAKER:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(true);
break;
}
}else if(isBluetoothHeadsetConnected()){
am.setBluetoothScoOn(speakerphoneStateToSet);
}else{
am.setSpeakerphoneOn(speakerphoneStateToSet);
}
}/*else{
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
break;
case AUDIO_ROUTE_EARPIECE:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
break;
case AUDIO_ROUTE_SPEAKER:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
break;
}
}else{
if(hasEarpiece())
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
else
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
}
}*/
updateOutputGainControlState();
audioConfigured=true;
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
try{
if(proximity!=null){
proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
}
}catch(Exception x){
if (BuildVars.LOGS_ENABLED) {
FileLog.e("Error initializing proximity sensor", x);
}
}
}
@Override
public void onNotifyHeadsetState(boolean connected, int type) {
try {
if (connected) {
HeadsetPlugReceiverState = true;
if (type == 0) {
startBluetoothSco();
}
if (null != btnMuteSpeaker) {
btnMuteSpeaker.setBackgroundResource(R.drawable.img_capture_gray);
btnMuteSpeaker.setSelected(false);
btnMuteSpeaker.setEnabled(false);
btnMuteSpeaker.setClickable(false);
audioManager.onToggleSpeaker(false);
}
} else {
if (type == 1 && BluetoothUtil.hasBluetoothA2dpConnected()) {
return;
}
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am != null) {
if (am.getMode() != AudioManager.MODE_IN_COMMUNICATION) {
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
}
if (type == 0) {
am.stopBluetoothSco();
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(!muteSpeaker);
} else {
RCRTCEngine.getInstance().enableSpeaker(!this.muteSpeaker);
}
audioManager.onToggleSpeaker(!muteSpeaker);
}
if (null != btnMuteSpeaker) {
btnMuteSpeaker.setBackgroundResource(R.drawable.selector_checkbox_capture);
btnMuteSpeaker.setSelected(false);
btnMuteSpeaker.setEnabled(true);
btnMuteSpeaker.setClickable(true);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected void configureDeviceForCall() {
needPlayEndSound = true;
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
am.setMode(AudioManager.MODE_IN_COMMUNICATION);
am.requestAudioFocus(this, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
if(!bluetoothScoActive){
needSwitchToBluetoothAfterScoActivates=true;
try {
am.startBluetoothSco();
} catch (Throwable ignore) {
}
}else{
am.setBluetoothScoOn(true);
am.setSpeakerphoneOn(false);
}
break;
case AUDIO_ROUTE_EARPIECE:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
break;
case AUDIO_ROUTE_SPEAKER:
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(true);
break;
}
}else if(isBluetoothHeadsetConnected()){
am.setBluetoothScoOn(speakerphoneStateToSet);
}else{
am.setSpeakerphoneOn(speakerphoneStateToSet);
}
}/*else{
if(isBluetoothHeadsetConnected() && hasEarpiece()){
switch(audioRouteToSet){
case AUDIO_ROUTE_BLUETOOTH:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_BLUETOOTH);
break;
case AUDIO_ROUTE_EARPIECE:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_WIRED_OR_EARPIECE);
break;
case AUDIO_ROUTE_SPEAKER:
systemCallConnection.setAudioRoute(CallAudioState.ROUTE_SPEAKER);
break;
}
}else{
if(hasEarpiece())
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_SPEAKER);
else
systemCallConnection.setAudioRoute(!speakerphoneStateToSet ? CallAudioState.ROUTE_WIRED_OR_EARPIECE : CallAudioState.ROUTE_BLUETOOTH);
}
}*/
updateOutputGainControlState();
audioConfigured=true;
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
try{
if(proximity!=null){
proximityWakelock=((PowerManager) getSystemService(Context.POWER_SERVICE)).newWakeLock(PROXIMITY_SCREEN_OFF_WAKE_LOCK, "telegram-voip-prx");
sm.registerListener(this, proximity, SensorManager.SENSOR_DELAY_NORMAL);
}
}catch(Exception x){
if (BuildVars.LOGS_ENABLED) {
FileLog.e("Error initializing proximity sensor", x);
}
}
}
@Override
public void onDestroy() {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STOPPING ===============");
}
stopForeground(true);
stopRinging();
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximity != null) {
sm.unregisterListener(this);
}
if (proximityWakelock != null && proximityWakelock.isHeld()) {
proximityWakelock.release();
}
unregisterReceiver(receiver);
if(timeoutRunnable!=null){
AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
timeoutRunnable=null;
}
super.onDestroy();
sharedInstance = null;
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
}
});
if (tgVoip != null) {
updateTrafficStats();
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
onTgVoipPreStop();
onTgVoipStop(tgVoip.stop());
prevTrafficStats = null;
callStartTime = 0;
tgVoip = null;
}
cpuWakelock.release();
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
if(isBtHeadsetConnected && !playingSound){
am.stopBluetoothSco();
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
}
try{
am.setMode(AudioManager.MODE_NORMAL);
}catch(SecurityException x){
if(BuildVars.LOGS_ENABLED){
FileLog.e("Error setting audio more to normal", x);
}
}
am.abandonAudioFocus(this);
}
am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if (haveAudioFocus)
am.abandonAudioFocus(this);
if (!playingSound)
soundPool.release();
if(USE_CONNECTION_SERVICE){
if(!didDeleteConnectionServiceContact)
ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
if(systemCallConnection!=null && !playingSound){
systemCallConnection.destroy();
}
}
ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
VoIPHelper.lastCallTime=System.currentTimeMillis();
}
@Override
public void onDestroy() {
if (BuildVars.LOGS_ENABLED) {
FileLog.d("=============== VoIPService STOPPING ===============");
}
stopForeground(true);
stopRinging();
NotificationCenter.getInstance(currentAccount).removeObserver(this, NotificationCenter.appDidLogout);
SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
Sensor proximity = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
if (proximity != null) {
sm.unregisterListener(this);
}
if (proximityWakelock != null && proximityWakelock.isHeld()) {
proximityWakelock.release();
}
unregisterReceiver(receiver);
if(timeoutRunnable!=null){
AndroidUtilities.cancelRunOnUIThread(timeoutRunnable);
timeoutRunnable=null;
}
super.onDestroy();
sharedInstance = null;
AndroidUtilities.runOnUIThread(new Runnable(){
@Override
public void run(){
NotificationCenter.getGlobalInstance().postNotificationName(NotificationCenter.didEndCall);
}
});
if (tgVoip != null) {
updateTrafficStats();
StatsController.getInstance(currentAccount).incrementTotalCallsTime(getStatsNetworkType(), (int) (getCallDuration() / 1000) % 5);
onTgVoipPreStop();
onTgVoipStop(tgVoip.stop());
prevTrafficStats = null;
callStartTime = 0;
tgVoip = null;
}
cpuWakelock.release();
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
if(!USE_CONNECTION_SERVICE){
if(isBtHeadsetConnected && !playingSound){
am.stopBluetoothSco();
am.setBluetoothScoOn(false);
am.setSpeakerphoneOn(false);
}
try{
am.setMode(AudioManager.MODE_NORMAL);
}catch(SecurityException x){
if(BuildVars.LOGS_ENABLED){
FileLog.e("Error setting audio more to normal", x);
}
}
am.abandonAudioFocus(this);
}
am.unregisterMediaButtonEventReceiver(new ComponentName(this, VoIPMediaButtonReceiver.class));
if (haveAudioFocus)
am.abandonAudioFocus(this);
if (!playingSound)
soundPool.release();
if(USE_CONNECTION_SERVICE){
if(!didDeleteConnectionServiceContact)
ContactsController.getInstance(currentAccount).deleteConnectionServiceContact();
if(systemCallConnection!=null && !playingSound){
systemCallConnection.destroy();
}
}
ConnectionsManager.getInstance(currentAccount).setAppPaused(true, false);
VoIPHelper.lastCallTime=System.currentTimeMillis();
}