Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 1 | /* |
Alexandre Lision | c1024c0 | 2014-01-06 11:12:53 -0500 | [diff] [blame] | 2 | * Copyright (C) 2004-2014 Savoir-Faire Linux Inc. |
Alexandre Lision | a8b7872 | 2013-12-13 10:18:33 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | * |
| 20 | * Additional permission under GNU GPL version 3 section 7: |
| 21 | * |
| 22 | * If you modify this program, or any covered work, by linking or |
| 23 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 24 | * modified version of that library), containing parts covered by the |
| 25 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 26 | * grants you additional permission to convey the resulting work. |
| 27 | * Corresponding Source for a non-source form of such a combination |
| 28 | * shall include the source code for the parts of OpenSSL used as well |
| 29 | * as that of the covered work. |
| 30 | */ |
| 31 | |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 32 | package org.sflphone.utils; |
| 33 | |
| 34 | import org.sflphone.service.SipService; |
| 35 | |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 36 | import android.content.Context; |
| 37 | import android.media.AudioManager; |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 38 | import android.media.AudioManager.OnAudioFocusChangeListener; |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 39 | import android.os.Handler; |
| 40 | import android.util.Log; |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 41 | import org.sflphone.utils.bluetooth.BluetoothWrapper; |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 42 | |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 43 | public class MediaManager implements OnAudioFocusChangeListener, BluetoothWrapper.BluetoothChangeListener { |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 44 | |
| 45 | private static final String TAG = MediaManager.class.getSimpleName(); |
| 46 | private SipService mService; |
| 47 | private SettingsContentObserver mSettingsContentObserver; |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 48 | AudioManager mAudioManager; |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 49 | private Ringer ringer; |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 50 | //Bluetooth related |
| 51 | private BluetoothWrapper bluetoothWrapper; |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 52 | |
| 53 | public MediaManager(SipService aService) { |
| 54 | mService = aService; |
| 55 | mSettingsContentObserver = new SettingsContentObserver(mService, new Handler()); |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 56 | mAudioManager = (AudioManager) aService.getSystemService(Context.AUDIO_SERVICE); |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 57 | |
| 58 | ringer = new Ringer(aService); |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | public void startService() { |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 62 | if(bluetoothWrapper == null) { |
| 63 | bluetoothWrapper = BluetoothWrapper.getInstance(mService); |
| 64 | bluetoothWrapper.setBluetoothChangeListener(this); |
| 65 | bluetoothWrapper.register(); |
| 66 | } |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 67 | mService.getApplicationContext().getContentResolver() |
| 68 | .registerContentObserver(android.provider.Settings.System.CONTENT_URI, true, mSettingsContentObserver); |
| 69 | } |
| 70 | |
| 71 | public void stopService() { |
| 72 | Log.i(TAG, "Remove media manager...."); |
| 73 | mService.getApplicationContext().getContentResolver().unregisterContentObserver(mSettingsContentObserver); |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 74 | if(bluetoothWrapper != null) { |
| 75 | bluetoothWrapper.unregister(); |
| 76 | bluetoothWrapper.setBluetoothChangeListener(null); |
| 77 | bluetoothWrapper = null; |
| 78 | } |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 79 | } |
| 80 | |
Alexandre Lision | 63870a7 | 2013-10-28 16:33:47 -0400 | [diff] [blame] | 81 | public AudioManager getAudioManager() { |
| 82 | return mAudioManager; |
| 83 | } |
| 84 | |
Alexandre Lision | baeeb21 | 2013-12-09 12:54:47 -0500 | [diff] [blame] | 85 | public void obtainAudioFocus(boolean requestSpeakerOn) { |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 86 | mAudioManager.requestAudioFocus(this, Compatibility.getInCallStream(false), AudioManager.AUDIOFOCUS_GAIN_TRANSIENT); |
Alexandre Lision | 7e39065 | 2013-12-06 16:58:22 -0500 | [diff] [blame] | 87 | mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION); |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 88 | if(bluetoothWrapper != null && bluetoothWrapper.canBluetooth()) { |
| 89 | Log.d(TAG, "Try to enable bluetooth"); |
| 90 | bluetoothWrapper.setBluetoothOn(true); |
| 91 | } else if (requestSpeakerOn && !mAudioManager.isWiredHeadsetOn()){ |
Alexandre Lision | baeeb21 | 2013-12-09 12:54:47 -0500 | [diff] [blame] | 92 | RouteToSpeaker(); |
| 93 | } |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
| 97 | public void onAudioFocusChange(int arg0) { |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 98 | |
Alexandre Lision | 6e9c7e5 | 2013-10-29 15:54:11 -0400 | [diff] [blame] | 99 | } |
| 100 | |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 101 | public void abandonAudioFocus() { |
| 102 | mAudioManager.abandonAudioFocus(this); |
| 103 | if (mAudioManager.isSpeakerphoneOn()) { |
| 104 | mAudioManager.setSpeakerphoneOn(false); |
| 105 | } |
Alexandre Lision | 7e39065 | 2013-12-06 16:58:22 -0500 | [diff] [blame] | 106 | mAudioManager.setMode(AudioManager.MODE_NORMAL); |
Alexandre Lision | 3557713 | 2013-12-06 15:21:15 -0500 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | public void RouteToSpeaker() { |
| 110 | mAudioManager.setSpeakerphoneOn(true); |
| 111 | } |
| 112 | |
| 113 | public void RouteToInternalSpeaker() { |
| 114 | mAudioManager.setSpeakerphoneOn(false); |
| 115 | } |
Alexandre Lision | f02190d | 2013-12-12 17:26:12 -0500 | [diff] [blame] | 116 | |
| 117 | |
| 118 | /** |
| 119 | * Start ringing announce for a given contact. |
| 120 | * It will also focus audio for us. |
| 121 | * @param remoteContact the contact to ring for. May resolve the contact ringtone if any. |
| 122 | */ |
| 123 | synchronized public void startRing(String remoteContact) { |
| 124 | |
| 125 | if(!ringer.isRinging()) { |
| 126 | ringer.ring(remoteContact, "USELESS"); |
| 127 | }else { |
| 128 | Log.d(TAG, "Already ringing ...."); |
| 129 | } |
| 130 | |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Stop all ringing. <br/> |
| 135 | * Warning, this will not unfocus audio. |
| 136 | */ |
| 137 | synchronized public void stopRing() { |
| 138 | if(ringer.isRinging()) { |
| 139 | ringer.stopRing(); |
| 140 | } |
| 141 | } |
Alexandre Lision | 3c37dca | 2014-02-21 14:13:26 -0500 | [diff] [blame] | 142 | |
| 143 | @Override |
| 144 | public void onBluetoothStateChanged(int status) { |
| 145 | //setSoftwareVolume(); |
| 146 | //broadcastMediaChanged(); |
| 147 | } |
| 148 | |
Alexandre Lision | 6d867b9 | 2013-10-25 15:36:28 -0400 | [diff] [blame] | 149 | } |